blob: 1684a9effea8dfbf3d1e68f0da842db5431528f3 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view;
18
Christopher Tatea53146c2010-09-07 11:57:52 -070019import android.content.ClipData;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.Context;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080021import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.res.Resources;
23import android.content.res.TypedArray;
24import android.graphics.Bitmap;
Adam Powell2b342f02010-08-18 18:14:13 -070025import android.graphics.Camera;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.graphics.Canvas;
Philip Milne1557fd72012-04-04 23:41:34 -070027import android.graphics.Insets;
Mike Cleronf116bf82009-09-27 19:14:12 -070028import android.graphics.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.graphics.LinearGradient;
30import android.graphics.Matrix;
31import android.graphics.Paint;
32import android.graphics.PixelFormat;
svetoslavganov75986cf2009-05-14 22:28:01 -070033import android.graphics.Point;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.graphics.PorterDuff;
35import android.graphics.PorterDuffXfermode;
36import android.graphics.Rect;
Adam Powell6e346362010-07-23 10:18:23 -070037import android.graphics.RectF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.graphics.Region;
39import android.graphics.Shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.graphics.drawable.ColorDrawable;
41import android.graphics.drawable.Drawable;
Svetoslav Ganovaa780c12012-04-19 23:01:39 -070042import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.os.Handler;
44import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.os.Parcel;
46import android.os.Parcelable;
47import android.os.RemoteException;
48import android.os.SystemClock;
Philip Milne10ca24a2012-04-23 15:38:27 -070049import android.os.SystemProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.util.AttributeSet;
Doug Feltcb3791202011-07-07 11:57:48 -070051import android.util.FloatProperty;
52import android.util.LocaleUtil;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070054import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070055import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070056import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.util.Pools;
Doug Feltcb3791202011-07-07 11:57:48 -070058import android.util.Property;
svetoslavganov75986cf2009-05-14 22:28:01 -070059import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080060import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.view.ContextMenu.ContextMenuInfo;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -070062import android.view.AccessibilityIterators.TextSegmentIterator;
63import android.view.AccessibilityIterators.CharacterTextSegmentIterator;
64import android.view.AccessibilityIterators.WordTextSegmentIterator;
65import android.view.AccessibilityIterators.ParagraphTextSegmentIterator;
svetoslavganov75986cf2009-05-14 22:28:01 -070066import android.view.accessibility.AccessibilityEvent;
67import android.view.accessibility.AccessibilityEventSource;
68import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070069import android.view.accessibility.AccessibilityNodeInfo;
Svetoslav Ganov02107852011-10-03 17:06:56 -070070import android.view.accessibility.AccessibilityNodeProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070072import android.view.animation.AnimationUtils;
Chet Haase64a48c12012-02-13 16:33:29 -080073import android.view.animation.Transformation;
svetoslavganov75986cf2009-05-14 22:28:01 -070074import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.view.inputmethod.InputConnection;
76import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.widget.ScrollBarDrawable;
78
Romain Guy1ef3fdb2011-09-09 15:30:30 -070079import static android.os.Build.VERSION_CODES.*;
Philip Milne6c8ea062012-04-03 17:38:43 -070080import static java.lang.Math.max;
Romain Guy1ef3fdb2011-09-09 15:30:30 -070081
Doug Feltcb3791202011-07-07 11:57:48 -070082import com.android.internal.R;
83import com.android.internal.util.Predicate;
84import com.android.internal.view.menu.MenuBuilder;
85
Christopher Tatea0374192010-10-05 13:06:41 -070086import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070087import java.lang.reflect.InvocationTargetException;
88import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089import java.util.ArrayList;
90import java.util.Arrays;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070091import java.util.Locale;
Adam Powell4afd62b2011-02-18 15:02:18 -080092import java.util.concurrent.CopyOnWriteArrayList;
Adam Powella9108a22012-07-18 11:18:09 -070093import java.util.concurrent.atomic.AtomicInteger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094
95/**
96 * <p>
97 * This class represents the basic building block for user interface components. A View
98 * occupies a rectangular area on the screen and is responsible for drawing and
99 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -0700100 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
102 * are invisible containers that hold other Views (or other ViewGroups) and define
103 * their layout properties.
104 * </p>
105 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700106 * <div class="special reference">
107 * <h3>Developer Guides</h3>
108 * <p>For information about using this class to develop your application's user interface,
109 * 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 -0800110 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700111 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 * <a name="Using"></a>
113 * <h3>Using Views</h3>
114 * <p>
115 * All of the views in a window are arranged in a single tree. You can add views
116 * either from code or by specifying a tree of views in one or more XML layout
117 * files. There are many specialized subclasses of views that act as controls or
118 * are capable of displaying text, images, or other content.
119 * </p>
120 * <p>
121 * Once you have created a tree of views, there are typically a few types of
122 * common operations you may wish to perform:
123 * <ul>
124 * <li><strong>Set properties:</strong> for example setting the text of a
125 * {@link android.widget.TextView}. The available properties and the methods
126 * that set them will vary among the different subclasses of views. Note that
127 * properties that are known at build time can be set in the XML layout
128 * files.</li>
129 * <li><strong>Set focus:</strong> The framework will handled moving focus in
130 * response to user input. To force focus to a specific view, call
131 * {@link #requestFocus}.</li>
132 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
133 * that will be notified when something interesting happens to the view. For
134 * example, all views will let you set a listener to be notified when the view
135 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700136 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
Philip Milne6c8ea062012-04-03 17:38:43 -0700137 * Other view subclasses offer more specialized listeners. For example, a Button
Romain Guy5c22a8c2011-05-13 11:48:45 -0700138 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700140 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 * </ul>
142 * </p>
143 * <p><em>
144 * Note: The Android framework is responsible for measuring, laying out and
145 * drawing views. You should not call methods that perform these actions on
146 * views yourself unless you are actually implementing a
147 * {@link android.view.ViewGroup}.
148 * </em></p>
149 *
150 * <a name="Lifecycle"></a>
151 * <h3>Implementing a Custom View</h3>
152 *
153 * <p>
154 * To implement a custom view, you will usually begin by providing overrides for
155 * some of the standard methods that the framework calls on all views. You do
156 * not need to override all of these methods. In fact, you can start by just
157 * overriding {@link #onDraw(android.graphics.Canvas)}.
158 * <table border="2" width="85%" align="center" cellpadding="5">
159 * <thead>
160 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
161 * </thead>
162 *
163 * <tbody>
164 * <tr>
165 * <td rowspan="2">Creation</td>
166 * <td>Constructors</td>
167 * <td>There is a form of the constructor that are called when the view
168 * is created from code and a form that is called when the view is
169 * inflated from a layout file. The second form should parse and apply
170 * any attributes defined in the layout file.
171 * </td>
172 * </tr>
173 * <tr>
174 * <td><code>{@link #onFinishInflate()}</code></td>
175 * <td>Called after a view and all of its children has been inflated
176 * from XML.</td>
177 * </tr>
178 *
179 * <tr>
180 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700181 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 * <td>Called to determine the size requirements for this view and all
183 * of its children.
184 * </td>
185 * </tr>
186 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700187 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 * <td>Called when this view should assign a size and position to all
189 * of its children.
190 * </td>
191 * </tr>
192 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700193 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 * <td>Called when the size of this view has changed.
195 * </td>
196 * </tr>
197 *
198 * <tr>
199 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700200 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 * <td>Called when the view should render its content.
202 * </td>
203 * </tr>
204 *
205 * <tr>
206 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700207 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
Jean Chalard405bc512012-05-29 19:12:34 +0900208 * <td>Called when a new hardware key event occurs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 * </td>
210 * </tr>
211 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700212 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
Jean Chalard405bc512012-05-29 19:12:34 +0900213 * <td>Called when a hardware key up event occurs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 * </td>
215 * </tr>
216 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700217 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 * <td>Called when a trackball motion event occurs.
219 * </td>
220 * </tr>
221 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700222 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 * <td>Called when a touch screen motion event occurs.
224 * </td>
225 * </tr>
226 *
227 * <tr>
228 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700229 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 * <td>Called when the view gains or loses focus.
231 * </td>
232 * </tr>
233 *
234 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700235 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 * <td>Called when the window containing the view gains or loses focus.
237 * </td>
238 * </tr>
239 *
240 * <tr>
241 * <td rowspan="3">Attaching</td>
242 * <td><code>{@link #onAttachedToWindow()}</code></td>
243 * <td>Called when the view is attached to a window.
244 * </td>
245 * </tr>
246 *
247 * <tr>
248 * <td><code>{@link #onDetachedFromWindow}</code></td>
249 * <td>Called when the view is detached from its window.
250 * </td>
251 * </tr>
252 *
253 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700254 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 * <td>Called when the visibility of the window containing the view
256 * has changed.
257 * </td>
258 * </tr>
259 * </tbody>
260 *
261 * </table>
262 * </p>
263 *
264 * <a name="IDs"></a>
265 * <h3>IDs</h3>
266 * Views may have an integer id associated with them. These ids are typically
267 * assigned in the layout XML files, and are used to find specific views within
268 * the view tree. A common pattern is to:
269 * <ul>
270 * <li>Define a Button in the layout file and assign it a unique ID.
271 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700272 * &lt;Button
273 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 * android:layout_width="wrap_content"
275 * android:layout_height="wrap_content"
276 * android:text="@string/my_button_text"/&gt;
277 * </pre></li>
278 * <li>From the onCreate method of an Activity, find the Button
279 * <pre class="prettyprint">
280 * Button myButton = (Button) findViewById(R.id.my_button);
281 * </pre></li>
282 * </ul>
283 * <p>
284 * View IDs need not be unique throughout the tree, but it is good practice to
285 * ensure that they are at least unique within the part of the tree you are
286 * searching.
287 * </p>
288 *
289 * <a name="Position"></a>
290 * <h3>Position</h3>
291 * <p>
292 * The geometry of a view is that of a rectangle. A view has a location,
293 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
294 * two dimensions, expressed as a width and a height. The unit for location
295 * and dimensions is the pixel.
296 * </p>
297 *
298 * <p>
299 * It is possible to retrieve the location of a view by invoking the methods
300 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
301 * coordinate of the rectangle representing the view. The latter returns the
302 * top, or Y, coordinate of the rectangle representing the view. These methods
303 * both return the location of the view relative to its parent. For instance,
304 * when getLeft() returns 20, that means the view is located 20 pixels to the
305 * right of the left edge of its direct parent.
306 * </p>
307 *
308 * <p>
309 * In addition, several convenience methods are offered to avoid unnecessary
310 * computations, namely {@link #getRight()} and {@link #getBottom()}.
311 * These methods return the coordinates of the right and bottom edges of the
312 * rectangle representing the view. For instance, calling {@link #getRight()}
313 * is similar to the following computation: <code>getLeft() + getWidth()</code>
314 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
315 * </p>
316 *
317 * <a name="SizePaddingMargins"></a>
318 * <h3>Size, padding and margins</h3>
319 * <p>
320 * The size of a view is expressed with a width and a height. A view actually
321 * possess two pairs of width and height values.
322 * </p>
323 *
324 * <p>
325 * The first pair is known as <em>measured width</em> and
326 * <em>measured height</em>. These dimensions define how big a view wants to be
327 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
328 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
329 * and {@link #getMeasuredHeight()}.
330 * </p>
331 *
332 * <p>
333 * The second pair is simply known as <em>width</em> and <em>height</em>, or
334 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
335 * dimensions define the actual size of the view on screen, at drawing time and
336 * after layout. These values may, but do not have to, be different from the
337 * measured width and height. The width and height can be obtained by calling
338 * {@link #getWidth()} and {@link #getHeight()}.
339 * </p>
340 *
341 * <p>
342 * To measure its dimensions, a view takes into account its padding. The padding
343 * is expressed in pixels for the left, top, right and bottom parts of the view.
344 * Padding can be used to offset the content of the view by a specific amount of
345 * pixels. For instance, a left padding of 2 will push the view's content by
346 * 2 pixels to the right of the left edge. Padding can be set using the
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -0700347 * {@link #setPadding(int, int, int, int)} or {@link #setPaddingRelative(int, int, int, int)}
348 * method and queried by calling {@link #getPaddingLeft()}, {@link #getPaddingTop()},
349 * {@link #getPaddingRight()}, {@link #getPaddingBottom()}, {@link #getPaddingStart()},
350 * {@link #getPaddingEnd()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 * </p>
352 *
353 * <p>
354 * Even though a view can define a padding, it does not provide any support for
355 * margins. However, view groups provide such a support. Refer to
356 * {@link android.view.ViewGroup} and
357 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
358 * </p>
359 *
360 * <a name="Layout"></a>
361 * <h3>Layout</h3>
362 * <p>
363 * Layout is a two pass process: a measure pass and a layout pass. The measuring
364 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
365 * of the view tree. Each view pushes dimension specifications down the tree
366 * during the recursion. At the end of the measure pass, every view has stored
367 * its measurements. The second pass happens in
368 * {@link #layout(int,int,int,int)} and is also top-down. During
369 * this pass each parent is responsible for positioning all of its children
370 * using the sizes computed in the measure pass.
371 * </p>
372 *
373 * <p>
374 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
375 * {@link #getMeasuredHeight()} values must be set, along with those for all of
376 * that view's descendants. A view's measured width and measured height values
377 * must respect the constraints imposed by the view's parents. This guarantees
378 * that at the end of the measure pass, all parents accept all of their
379 * children's measurements. A parent view may call measure() more than once on
380 * its children. For example, the parent may measure each child once with
381 * unspecified dimensions to find out how big they want to be, then call
382 * measure() on them again with actual numbers if the sum of all the children's
383 * unconstrained sizes is too big or too small.
384 * </p>
385 *
386 * <p>
387 * The measure pass uses two classes to communicate dimensions. The
388 * {@link MeasureSpec} class is used by views to tell their parents how they
389 * want to be measured and positioned. The base LayoutParams class just
390 * describes how big the view wants to be for both width and height. For each
391 * dimension, it can specify one of:
392 * <ul>
393 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800394 * <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 -0800395 * (minus padding)
396 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
397 * enclose its content (plus padding).
398 * </ul>
399 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
400 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
401 * an X and Y value.
402 * </p>
403 *
404 * <p>
405 * MeasureSpecs are used to push requirements down the tree from parent to
406 * child. A MeasureSpec can be in one of three modes:
407 * <ul>
408 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
409 * of a child view. For example, a LinearLayout may call measure() on its child
410 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
411 * tall the child view wants to be given a width of 240 pixels.
412 * <li>EXACTLY: This is used by the parent to impose an exact size on the
413 * child. The child must use this size, and guarantee that all of its
414 * descendants will fit within this size.
415 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
416 * child. The child must gurantee that it and all of its descendants will fit
417 * within this size.
418 * </ul>
419 * </p>
420 *
421 * <p>
422 * To intiate a layout, call {@link #requestLayout}. This method is typically
423 * called by a view on itself when it believes that is can no longer fit within
424 * its current bounds.
425 * </p>
426 *
427 * <a name="Drawing"></a>
428 * <h3>Drawing</h3>
429 * <p>
430 * Drawing is handled by walking the tree and rendering each view that
Joe Fernandez558459f2011-10-13 16:47:36 -0700431 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 * this means that parents will draw before (i.e., behind) their children, with
433 * siblings drawn in the order they appear in the tree.
434 * If you set a background drawable for a View, then the View will draw it for you
435 * before calling back to its <code>onDraw()</code> method.
436 * </p>
437 *
438 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700439 * 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 -0800440 * </p>
441 *
442 * <p>
443 * To force a view to draw, call {@link #invalidate()}.
444 * </p>
445 *
446 * <a name="EventHandlingThreading"></a>
447 * <h3>Event Handling and Threading</h3>
448 * <p>
449 * The basic cycle of a view is as follows:
450 * <ol>
451 * <li>An event comes in and is dispatched to the appropriate view. The view
452 * handles the event and notifies any listeners.</li>
453 * <li>If in the course of processing the event, the view's bounds may need
454 * to be changed, the view will call {@link #requestLayout()}.</li>
455 * <li>Similarly, if in the course of processing the event the view's appearance
456 * may need to be changed, the view will call {@link #invalidate()}.</li>
457 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
458 * the framework will take care of measuring, laying out, and drawing the tree
459 * as appropriate.</li>
460 * </ol>
461 * </p>
462 *
463 * <p><em>Note: The entire view tree is single threaded. You must always be on
464 * the UI thread when calling any method on any view.</em>
465 * If you are doing work on other threads and want to update the state of a view
466 * from that thread, you should use a {@link Handler}.
467 * </p>
468 *
469 * <a name="FocusHandling"></a>
470 * <h3>Focus Handling</h3>
471 * <p>
472 * The framework will handle routine focus movement in response to user input.
473 * This includes changing the focus as views are removed or hidden, or as new
474 * views become available. Views indicate their willingness to take focus
475 * through the {@link #isFocusable} method. To change whether a view can take
476 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
477 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
478 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
479 * </p>
480 * <p>
481 * Focus movement is based on an algorithm which finds the nearest neighbor in a
482 * given direction. In rare cases, the default algorithm may not match the
483 * intended behavior of the developer. In these situations, you can provide
484 * explicit overrides by using these XML attributes in the layout file:
485 * <pre>
486 * nextFocusDown
487 * nextFocusLeft
488 * nextFocusRight
489 * nextFocusUp
490 * </pre>
491 * </p>
492 *
493 *
494 * <p>
495 * To get a particular view to take focus, call {@link #requestFocus()}.
496 * </p>
497 *
498 * <a name="TouchMode"></a>
499 * <h3>Touch Mode</h3>
500 * <p>
501 * When a user is navigating a user interface via directional keys such as a D-pad, it is
502 * necessary to give focus to actionable items such as buttons so the user can see
503 * what will take input. If the device has touch capabilities, however, and the user
504 * begins interacting with the interface by touching it, it is no longer necessary to
505 * always highlight, or give focus to, a particular view. This motivates a mode
506 * for interaction named 'touch mode'.
507 * </p>
508 * <p>
509 * For a touch capable device, once the user touches the screen, the device
510 * will enter touch mode. From this point onward, only views for which
511 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
512 * Other views that are touchable, like buttons, will not take focus when touched; they will
513 * only fire the on click listeners.
514 * </p>
515 * <p>
516 * Any time a user hits a directional key, such as a D-pad direction, the view device will
517 * exit touch mode, and find a view to take focus, so that the user may resume interacting
518 * with the user interface without touching the screen again.
519 * </p>
520 * <p>
521 * The touch mode state is maintained across {@link android.app.Activity}s. Call
522 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
523 * </p>
524 *
525 * <a name="Scrolling"></a>
526 * <h3>Scrolling</h3>
527 * <p>
528 * The framework provides basic support for views that wish to internally
529 * scroll their content. This includes keeping track of the X and Y scroll
530 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800531 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700532 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 * </p>
534 *
535 * <a name="Tags"></a>
536 * <h3>Tags</h3>
537 * <p>
538 * Unlike IDs, tags are not used to identify views. Tags are essentially an
539 * extra piece of information that can be associated with a view. They are most
540 * often used as a convenience to store data related to views in the views
541 * themselves rather than by putting them in a separate structure.
542 * </p>
543 *
Chet Haasecb150fe2012-05-03 15:15:05 -0700544 * <a name="Properties"></a>
545 * <h3>Properties</h3>
546 * <p>
547 * The View class exposes an {@link #ALPHA} property, as well as several transform-related
548 * properties, such as {@link #TRANSLATION_X} and {@link #TRANSLATION_Y}. These properties are
549 * available both in the {@link Property} form as well as in similarly-named setter/getter
550 * methods (such as {@link #setAlpha(float)} for {@link #ALPHA}). These properties can
551 * be used to set persistent state associated with these rendering-related properties on the view.
552 * The properties and methods can also be used in conjunction with
553 * {@link android.animation.Animator Animator}-based animations, described more in the
554 * <a href="#Animation">Animation</a> section.
555 * </p>
556 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 * <a name="Animation"></a>
558 * <h3>Animation</h3>
559 * <p>
Chet Haasecb150fe2012-05-03 15:15:05 -0700560 * Starting with Android 3.0, the preferred way of animating views is to use the
561 * {@link android.animation} package APIs. These {@link android.animation.Animator Animator}-based
562 * classes change actual properties of the View object, such as {@link #setAlpha(float) alpha} and
563 * {@link #setTranslationX(float) translationX}. This behavior is contrasted to that of the pre-3.0
564 * {@link android.view.animation.Animation Animation}-based classes, which instead animate only
565 * how the view is drawn on the display. In particular, the {@link ViewPropertyAnimator} class
566 * makes animating these View properties particularly easy and efficient.
567 * </p>
568 * <p>
569 * 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 -0800570 * You can attach an {@link Animation} object to a view using
571 * {@link #setAnimation(Animation)} or
572 * {@link #startAnimation(Animation)}. The animation can alter the scale,
573 * rotation, translation and alpha of a view over time. If the animation is
574 * attached to a view that has children, the animation will affect the entire
575 * subtree rooted by that node. When an animation is started, the framework will
576 * take care of redrawing the appropriate views until the animation completes.
577 * </p>
578 *
Jeff Brown85a31762010-09-01 17:01:00 -0700579 * <a name="Security"></a>
580 * <h3>Security</h3>
581 * <p>
582 * Sometimes it is essential that an application be able to verify that an action
583 * is being performed with the full knowledge and consent of the user, such as
584 * granting a permission request, making a purchase or clicking on an advertisement.
585 * Unfortunately, a malicious application could try to spoof the user into
586 * performing these actions, unaware, by concealing the intended purpose of the view.
587 * As a remedy, the framework offers a touch filtering mechanism that can be used to
588 * improve the security of views that provide access to sensitive functionality.
589 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700590 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800591 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700592 * will discard touches that are received whenever the view's window is obscured by
593 * another visible window. As a result, the view will not receive touches whenever a
594 * toast, dialog or other window appears above the view's window.
595 * </p><p>
596 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700597 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
598 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700599 * </p>
600 *
Romain Guy171c5922011-01-06 10:04:23 -0800601 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700602 * @attr ref android.R.styleable#View_background
603 * @attr ref android.R.styleable#View_clickable
604 * @attr ref android.R.styleable#View_contentDescription
605 * @attr ref android.R.styleable#View_drawingCacheQuality
606 * @attr ref android.R.styleable#View_duplicateParentState
607 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700608 * @attr ref android.R.styleable#View_requiresFadingEdge
Philip Milne6c8ea062012-04-03 17:38:43 -0700609 * @attr ref android.R.styleable#View_fadeScrollbars
Romain Guyd6a463a2009-05-21 23:10:10 -0700610 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700611 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700613 * @attr ref android.R.styleable#View_isScrollContainer
614 * @attr ref android.R.styleable#View_focusable
615 * @attr ref android.R.styleable#View_focusableInTouchMode
616 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
617 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800618 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700619 * @attr ref android.R.styleable#View_longClickable
620 * @attr ref android.R.styleable#View_minHeight
621 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 * @attr ref android.R.styleable#View_nextFocusDown
623 * @attr ref android.R.styleable#View_nextFocusLeft
624 * @attr ref android.R.styleable#View_nextFocusRight
625 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700626 * @attr ref android.R.styleable#View_onClick
627 * @attr ref android.R.styleable#View_padding
628 * @attr ref android.R.styleable#View_paddingBottom
629 * @attr ref android.R.styleable#View_paddingLeft
630 * @attr ref android.R.styleable#View_paddingRight
631 * @attr ref android.R.styleable#View_paddingTop
Fabrice Di Meglio101d5aa2012-02-16 18:36:06 -0800632 * @attr ref android.R.styleable#View_paddingStart
633 * @attr ref android.R.styleable#View_paddingEnd
Romain Guyd6a463a2009-05-21 23:10:10 -0700634 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800635 * @attr ref android.R.styleable#View_rotation
636 * @attr ref android.R.styleable#View_rotationX
637 * @attr ref android.R.styleable#View_rotationY
638 * @attr ref android.R.styleable#View_scaleX
639 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 * @attr ref android.R.styleable#View_scrollX
641 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700642 * @attr ref android.R.styleable#View_scrollbarSize
643 * @attr ref android.R.styleable#View_scrollbarStyle
644 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700645 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
646 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
648 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 * @attr ref android.R.styleable#View_scrollbarThumbVertical
650 * @attr ref android.R.styleable#View_scrollbarTrackVertical
651 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
652 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700653 * @attr ref android.R.styleable#View_soundEffectsEnabled
654 * @attr ref android.R.styleable#View_tag
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -0700655 * @attr ref android.R.styleable#View_textAlignment
Chet Haase73066682010-11-29 15:55:32 -0800656 * @attr ref android.R.styleable#View_transformPivotX
657 * @attr ref android.R.styleable#View_transformPivotY
658 * @attr ref android.R.styleable#View_translationX
659 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700660 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 *
662 * @see android.view.ViewGroup
663 */
Fabrice Di Megliob03b4342012-06-04 12:55:30 -0700664public class View implements Drawable.Callback, KeyEvent.Callback,
Adam Powell8fc54f92011-09-07 16:40:45 -0700665 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 private static final boolean DBG = false;
667
668 /**
669 * The logging tag used by this class with android.util.Log.
670 */
671 protected static final String VIEW_LOG_TAG = "View";
672
673 /**
Guang Zhu0d607fb2012-05-11 19:34:56 -0700674 * When set to true, apps will draw debugging information about their layouts.
Romain Guy4b8c4f82012-04-27 15:48:35 -0700675 *
676 * @hide
677 */
678 public static final String DEBUG_LAYOUT_PROPERTY = "debug.layout";
679
680 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 * Used to mark a View that has no ID.
682 */
683 public static final int NO_ID = -1;
684
685 /**
686 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
687 * calling setFlags.
688 */
689 private static final int NOT_FOCUSABLE = 0x00000000;
690
691 /**
692 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
693 * setFlags.
694 */
695 private static final int FOCUSABLE = 0x00000001;
696
697 /**
698 * Mask for use with setFlags indicating bits used for focus.
699 */
700 private static final int FOCUSABLE_MASK = 0x00000001;
701
702 /**
703 * This view will adjust its padding to fit sytem windows (e.g. status bar)
704 */
705 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
706
707 /**
Scott Main812634c22011-07-27 13:22:35 -0700708 * This view is visible.
709 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
710 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 */
712 public static final int VISIBLE = 0x00000000;
713
714 /**
715 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700716 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
717 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718 */
719 public static final int INVISIBLE = 0x00000004;
720
721 /**
722 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700723 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
724 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 */
726 public static final int GONE = 0x00000008;
727
728 /**
729 * Mask for use with setFlags indicating bits used for visibility.
730 * {@hide}
731 */
732 static final int VISIBILITY_MASK = 0x0000000C;
733
734 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
735
736 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700737 * This view is enabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 * Use with ENABLED_MASK when calling setFlags.
739 * {@hide}
740 */
741 static final int ENABLED = 0x00000000;
742
743 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700744 * This view is disabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 * Use with ENABLED_MASK when calling setFlags.
746 * {@hide}
747 */
748 static final int DISABLED = 0x00000020;
749
750 /**
751 * Mask for use with setFlags indicating bits used for indicating whether
752 * this view is enabled
753 * {@hide}
754 */
755 static final int ENABLED_MASK = 0x00000020;
756
757 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700758 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
759 * called and further optimizations will be performed. It is okay to have
760 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 * {@hide}
762 */
763 static final int WILL_NOT_DRAW = 0x00000080;
764
765 /**
766 * Mask for use with setFlags indicating bits used for indicating whether
767 * this view is will draw
768 * {@hide}
769 */
770 static final int DRAW_MASK = 0x00000080;
771
772 /**
773 * <p>This view doesn't show scrollbars.</p>
774 * {@hide}
775 */
776 static final int SCROLLBARS_NONE = 0x00000000;
777
778 /**
779 * <p>This view shows horizontal scrollbars.</p>
780 * {@hide}
781 */
782 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
783
784 /**
785 * <p>This view shows vertical scrollbars.</p>
786 * {@hide}
787 */
788 static final int SCROLLBARS_VERTICAL = 0x00000200;
789
790 /**
791 * <p>Mask for use with setFlags indicating bits used for indicating which
792 * scrollbars are enabled.</p>
793 * {@hide}
794 */
795 static final int SCROLLBARS_MASK = 0x00000300;
796
Jeff Brown85a31762010-09-01 17:01:00 -0700797 /**
798 * Indicates that the view should filter touches when its window is obscured.
799 * Refer to the class comments for more information about this security feature.
800 * {@hide}
801 */
802 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
803
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700804 /**
805 * Set for framework elements that use FITS_SYSTEM_WINDOWS, to indicate
806 * that they are optional and should be skipped if the window has
807 * requested system UI flags that ignore those insets for layout.
808 */
809 static final int OPTIONAL_FITS_SYSTEM_WINDOWS = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810
811 /**
812 * <p>This view doesn't show fading edges.</p>
813 * {@hide}
814 */
815 static final int FADING_EDGE_NONE = 0x00000000;
816
817 /**
818 * <p>This view shows horizontal fading edges.</p>
819 * {@hide}
820 */
821 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
822
823 /**
824 * <p>This view shows vertical fading edges.</p>
825 * {@hide}
826 */
827 static final int FADING_EDGE_VERTICAL = 0x00002000;
828
829 /**
830 * <p>Mask for use with setFlags indicating bits used for indicating which
831 * fading edges are enabled.</p>
832 * {@hide}
833 */
834 static final int FADING_EDGE_MASK = 0x00003000;
835
836 /**
837 * <p>Indicates this view can be clicked. When clickable, a View reacts
838 * to clicks by notifying the OnClickListener.<p>
839 * {@hide}
840 */
841 static final int CLICKABLE = 0x00004000;
842
843 /**
844 * <p>Indicates this view is caching its drawing into a bitmap.</p>
845 * {@hide}
846 */
847 static final int DRAWING_CACHE_ENABLED = 0x00008000;
848
849 /**
850 * <p>Indicates that no icicle should be saved for this view.<p>
851 * {@hide}
852 */
853 static final int SAVE_DISABLED = 0x000010000;
854
855 /**
856 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
857 * property.</p>
858 * {@hide}
859 */
860 static final int SAVE_DISABLED_MASK = 0x000010000;
861
862 /**
863 * <p>Indicates that no drawing cache should ever be created for this view.<p>
864 * {@hide}
865 */
866 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
867
868 /**
869 * <p>Indicates this view can take / keep focus when int touch mode.</p>
870 * {@hide}
871 */
872 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
873
874 /**
875 * <p>Enables low quality mode for the drawing cache.</p>
876 */
877 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
878
879 /**
880 * <p>Enables high quality mode for the drawing cache.</p>
881 */
882 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
883
884 /**
885 * <p>Enables automatic quality mode for the drawing cache.</p>
886 */
887 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
888
889 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
890 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
891 };
892
893 /**
894 * <p>Mask for use with setFlags indicating bits used for the cache
895 * quality property.</p>
896 * {@hide}
897 */
898 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
899
900 /**
901 * <p>
902 * Indicates this view can be long clicked. When long clickable, a View
903 * reacts to long clicks by notifying the OnLongClickListener or showing a
904 * context menu.
905 * </p>
906 * {@hide}
907 */
908 static final int LONG_CLICKABLE = 0x00200000;
909
910 /**
911 * <p>Indicates that this view gets its drawable states from its direct parent
912 * and ignores its original internal states.</p>
913 *
914 * @hide
915 */
916 static final int DUPLICATE_PARENT_STATE = 0x00400000;
917
918 /**
919 * The scrollbar style to display the scrollbars inside the content area,
920 * without increasing the padding. The scrollbars will be overlaid with
921 * translucency on the view's content.
922 */
923 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
924
925 /**
926 * The scrollbar style to display the scrollbars inside the padded area,
927 * increasing the padding of the view. The scrollbars will not overlap the
928 * content area of the view.
929 */
930 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
931
932 /**
933 * The scrollbar style to display the scrollbars at the edge of the view,
934 * without increasing the padding. The scrollbars will be overlaid with
935 * translucency.
936 */
937 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
938
939 /**
940 * The scrollbar style to display the scrollbars at the edge of the view,
941 * increasing the padding of the view. The scrollbars will only overlap the
942 * background, if any.
943 */
944 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
945
946 /**
947 * Mask to check if the scrollbar style is overlay or inset.
948 * {@hide}
949 */
950 static final int SCROLLBARS_INSET_MASK = 0x01000000;
951
952 /**
953 * Mask to check if the scrollbar style is inside or outside.
954 * {@hide}
955 */
956 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
957
958 /**
959 * Mask for scrollbar style.
960 * {@hide}
961 */
962 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
963
964 /**
965 * View flag indicating that the screen should remain on while the
966 * window containing this view is visible to the user. This effectively
967 * takes care of automatically setting the WindowManager's
968 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
969 */
970 public static final int KEEP_SCREEN_ON = 0x04000000;
971
972 /**
973 * View flag indicating whether this view should have sound effects enabled
974 * for events such as clicking and touching.
975 */
976 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
977
978 /**
979 * View flag indicating whether this view should have haptic feedback
980 * enabled for events such as long presses.
981 */
982 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
983
984 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700985 * <p>Indicates that the view hierarchy should stop saving state when
986 * it reaches this view. If state saving is initiated immediately at
987 * the view, it will be allowed.
988 * {@hide}
989 */
990 static final int PARENT_SAVE_DISABLED = 0x20000000;
991
992 /**
993 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
994 * {@hide}
995 */
996 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
997
998 /**
svetoslavganov75986cf2009-05-14 22:28:01 -0700999 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1000 * should add all focusable Views regardless if they are focusable in touch mode.
1001 */
1002 public static final int FOCUSABLES_ALL = 0x00000000;
1003
1004 /**
1005 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1006 * should add only Views focusable in touch mode.
1007 */
1008 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1009
1010 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001011 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 * item.
1013 */
1014 public static final int FOCUS_BACKWARD = 0x00000001;
1015
1016 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001017 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 * item.
1019 */
1020 public static final int FOCUS_FORWARD = 0x00000002;
1021
1022 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001023 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 */
1025 public static final int FOCUS_LEFT = 0x00000011;
1026
1027 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001028 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 */
1030 public static final int FOCUS_UP = 0x00000021;
1031
1032 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001033 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 */
1035 public static final int FOCUS_RIGHT = 0x00000042;
1036
1037 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001038 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 */
1040 public static final int FOCUS_DOWN = 0x00000082;
1041
Svetoslav Ganov42138042012-03-20 11:51:39 -07001042 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001043 * Bits of {@link #getMeasuredWidthAndState()} and
1044 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1045 */
1046 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1047
1048 /**
1049 * Bits of {@link #getMeasuredWidthAndState()} and
1050 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1051 */
1052 public static final int MEASURED_STATE_MASK = 0xff000000;
1053
1054 /**
1055 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1056 * for functions that combine both width and height into a single int,
1057 * such as {@link #getMeasuredState()} and the childState argument of
1058 * {@link #resolveSizeAndState(int, int, int)}.
1059 */
1060 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1061
1062 /**
1063 * Bit of {@link #getMeasuredWidthAndState()} and
1064 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1065 * is smaller that the space the view would like to have.
1066 */
1067 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1068
1069 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 * Base View state sets
1071 */
1072 // Singles
1073 /**
1074 * Indicates the view has no states set. States are used with
1075 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1076 * view depending on its state.
1077 *
1078 * @see android.graphics.drawable.Drawable
1079 * @see #getDrawableState()
1080 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001081 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082 /**
1083 * Indicates the view is enabled. States are used with
1084 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1085 * view depending on its state.
1086 *
1087 * @see android.graphics.drawable.Drawable
1088 * @see #getDrawableState()
1089 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001090 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 /**
1092 * Indicates the view is focused. States are used with
1093 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1094 * view depending on its state.
1095 *
1096 * @see android.graphics.drawable.Drawable
1097 * @see #getDrawableState()
1098 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001099 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 /**
1101 * Indicates the view is selected. States are used with
1102 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1103 * view depending on its state.
1104 *
1105 * @see android.graphics.drawable.Drawable
1106 * @see #getDrawableState()
1107 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001108 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109 /**
1110 * Indicates the view is pressed. States are used with
1111 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1112 * view depending on its state.
1113 *
1114 * @see android.graphics.drawable.Drawable
1115 * @see #getDrawableState()
1116 * @hide
1117 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001118 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 /**
1120 * Indicates the view's window has focus. States are used with
1121 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1122 * view depending on its state.
1123 *
1124 * @see android.graphics.drawable.Drawable
1125 * @see #getDrawableState()
1126 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001127 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 // Doubles
1129 /**
1130 * Indicates the view is enabled and has the focus.
1131 *
1132 * @see #ENABLED_STATE_SET
1133 * @see #FOCUSED_STATE_SET
1134 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001135 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 /**
1137 * Indicates the view is enabled and selected.
1138 *
1139 * @see #ENABLED_STATE_SET
1140 * @see #SELECTED_STATE_SET
1141 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001142 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 /**
1144 * Indicates the view is enabled and that its window has focus.
1145 *
1146 * @see #ENABLED_STATE_SET
1147 * @see #WINDOW_FOCUSED_STATE_SET
1148 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001149 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 /**
1151 * Indicates the view is focused and selected.
1152 *
1153 * @see #FOCUSED_STATE_SET
1154 * @see #SELECTED_STATE_SET
1155 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001156 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 /**
1158 * Indicates the view has the focus and that its window has the focus.
1159 *
1160 * @see #FOCUSED_STATE_SET
1161 * @see #WINDOW_FOCUSED_STATE_SET
1162 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001163 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 /**
1165 * Indicates the view is selected and that its window has the focus.
1166 *
1167 * @see #SELECTED_STATE_SET
1168 * @see #WINDOW_FOCUSED_STATE_SET
1169 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001170 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 // Triples
1172 /**
1173 * Indicates the view is enabled, focused and selected.
1174 *
1175 * @see #ENABLED_STATE_SET
1176 * @see #FOCUSED_STATE_SET
1177 * @see #SELECTED_STATE_SET
1178 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001179 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 /**
1181 * Indicates the view is enabled, focused and its window has the focus.
1182 *
1183 * @see #ENABLED_STATE_SET
1184 * @see #FOCUSED_STATE_SET
1185 * @see #WINDOW_FOCUSED_STATE_SET
1186 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001187 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 /**
1189 * Indicates the view is enabled, selected and its window has the focus.
1190 *
1191 * @see #ENABLED_STATE_SET
1192 * @see #SELECTED_STATE_SET
1193 * @see #WINDOW_FOCUSED_STATE_SET
1194 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001195 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 /**
1197 * Indicates the view is focused, selected and its window has the focus.
1198 *
1199 * @see #FOCUSED_STATE_SET
1200 * @see #SELECTED_STATE_SET
1201 * @see #WINDOW_FOCUSED_STATE_SET
1202 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001203 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 /**
1205 * Indicates the view is enabled, focused, selected and its window
1206 * has the focus.
1207 *
1208 * @see #ENABLED_STATE_SET
1209 * @see #FOCUSED_STATE_SET
1210 * @see #SELECTED_STATE_SET
1211 * @see #WINDOW_FOCUSED_STATE_SET
1212 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001213 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 /**
1215 * Indicates the view is pressed and its window has the focus.
1216 *
1217 * @see #PRESSED_STATE_SET
1218 * @see #WINDOW_FOCUSED_STATE_SET
1219 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001220 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 /**
1222 * Indicates the view is pressed and selected.
1223 *
1224 * @see #PRESSED_STATE_SET
1225 * @see #SELECTED_STATE_SET
1226 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001227 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 /**
1229 * Indicates the view is pressed, selected and its window has the focus.
1230 *
1231 * @see #PRESSED_STATE_SET
1232 * @see #SELECTED_STATE_SET
1233 * @see #WINDOW_FOCUSED_STATE_SET
1234 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001235 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 /**
1237 * Indicates the view is pressed and focused.
1238 *
1239 * @see #PRESSED_STATE_SET
1240 * @see #FOCUSED_STATE_SET
1241 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001242 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 /**
1244 * Indicates the view is pressed, focused and its window has the focus.
1245 *
1246 * @see #PRESSED_STATE_SET
1247 * @see #FOCUSED_STATE_SET
1248 * @see #WINDOW_FOCUSED_STATE_SET
1249 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001250 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 /**
1252 * Indicates the view is pressed, focused and selected.
1253 *
1254 * @see #PRESSED_STATE_SET
1255 * @see #SELECTED_STATE_SET
1256 * @see #FOCUSED_STATE_SET
1257 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001258 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 /**
1260 * Indicates the view is pressed, focused, selected and its window has the focus.
1261 *
1262 * @see #PRESSED_STATE_SET
1263 * @see #FOCUSED_STATE_SET
1264 * @see #SELECTED_STATE_SET
1265 * @see #WINDOW_FOCUSED_STATE_SET
1266 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001267 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 /**
1269 * Indicates the view is pressed and enabled.
1270 *
1271 * @see #PRESSED_STATE_SET
1272 * @see #ENABLED_STATE_SET
1273 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001274 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275 /**
1276 * Indicates the view is pressed, enabled and its window has the focus.
1277 *
1278 * @see #PRESSED_STATE_SET
1279 * @see #ENABLED_STATE_SET
1280 * @see #WINDOW_FOCUSED_STATE_SET
1281 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001282 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 /**
1284 * Indicates the view is pressed, enabled and selected.
1285 *
1286 * @see #PRESSED_STATE_SET
1287 * @see #ENABLED_STATE_SET
1288 * @see #SELECTED_STATE_SET
1289 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001290 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 /**
1292 * Indicates the view is pressed, enabled, selected and its window has the
1293 * focus.
1294 *
1295 * @see #PRESSED_STATE_SET
1296 * @see #ENABLED_STATE_SET
1297 * @see #SELECTED_STATE_SET
1298 * @see #WINDOW_FOCUSED_STATE_SET
1299 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001300 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 /**
1302 * Indicates the view is pressed, enabled and focused.
1303 *
1304 * @see #PRESSED_STATE_SET
1305 * @see #ENABLED_STATE_SET
1306 * @see #FOCUSED_STATE_SET
1307 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001308 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 /**
1310 * Indicates the view is pressed, enabled, focused and its window has the
1311 * focus.
1312 *
1313 * @see #PRESSED_STATE_SET
1314 * @see #ENABLED_STATE_SET
1315 * @see #FOCUSED_STATE_SET
1316 * @see #WINDOW_FOCUSED_STATE_SET
1317 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001318 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001319 /**
1320 * Indicates the view is pressed, enabled, focused and selected.
1321 *
1322 * @see #PRESSED_STATE_SET
1323 * @see #ENABLED_STATE_SET
1324 * @see #SELECTED_STATE_SET
1325 * @see #FOCUSED_STATE_SET
1326 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001327 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001328 /**
1329 * Indicates the view is pressed, enabled, focused, selected and its window
1330 * has the focus.
1331 *
1332 * @see #PRESSED_STATE_SET
1333 * @see #ENABLED_STATE_SET
1334 * @see #SELECTED_STATE_SET
1335 * @see #FOCUSED_STATE_SET
1336 * @see #WINDOW_FOCUSED_STATE_SET
1337 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001338 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339
1340 /**
1341 * The order here is very important to {@link #getDrawableState()}
1342 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001343 private static final int[][] VIEW_STATE_SETS;
1344
Romain Guyb051e892010-09-28 19:09:36 -07001345 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1346 static final int VIEW_STATE_SELECTED = 1 << 1;
1347 static final int VIEW_STATE_FOCUSED = 1 << 2;
1348 static final int VIEW_STATE_ENABLED = 1 << 3;
1349 static final int VIEW_STATE_PRESSED = 1 << 4;
1350 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001351 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001352 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001353 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1354 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001355
1356 static final int[] VIEW_STATE_IDS = new int[] {
1357 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1358 R.attr.state_selected, VIEW_STATE_SELECTED,
1359 R.attr.state_focused, VIEW_STATE_FOCUSED,
1360 R.attr.state_enabled, VIEW_STATE_ENABLED,
1361 R.attr.state_pressed, VIEW_STATE_PRESSED,
1362 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001363 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001364 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001365 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
Svetoslav Ganov42138042012-03-20 11:51:39 -07001366 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001367 };
1368
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001369 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001370 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1371 throw new IllegalStateException(
1372 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1373 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001374 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001375 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001376 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001377 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001378 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001379 orderedIds[i * 2] = viewState;
1380 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001381 }
1382 }
1383 }
Romain Guyb051e892010-09-28 19:09:36 -07001384 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1385 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1386 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001387 int numBits = Integer.bitCount(i);
1388 int[] set = new int[numBits];
1389 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001390 for (int j = 0; j < orderedIds.length; j += 2) {
1391 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001392 set[pos++] = orderedIds[j];
1393 }
1394 }
1395 VIEW_STATE_SETS[i] = set;
1396 }
1397
1398 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1399 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1400 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1401 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1402 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1403 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1404 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1405 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1406 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1407 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1408 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1409 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1410 | VIEW_STATE_FOCUSED];
1411 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1412 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1413 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1414 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1415 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1416 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1417 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1418 | VIEW_STATE_ENABLED];
1419 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1420 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1421 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1422 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1423 | VIEW_STATE_ENABLED];
1424 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1425 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1426 | VIEW_STATE_ENABLED];
1427 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1428 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1429 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1430
1431 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1432 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1433 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1434 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1435 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1436 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1437 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1438 | VIEW_STATE_PRESSED];
1439 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1440 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1441 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1442 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1443 | VIEW_STATE_PRESSED];
1444 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1445 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1446 | VIEW_STATE_PRESSED];
1447 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1448 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1449 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1450 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1451 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1452 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1453 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1454 | VIEW_STATE_PRESSED];
1455 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1456 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1457 | VIEW_STATE_PRESSED];
1458 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1459 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1460 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1461 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1462 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1463 | VIEW_STATE_PRESSED];
1464 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1465 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1466 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1467 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1468 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1469 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1470 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1471 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1472 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1473 | VIEW_STATE_PRESSED];
1474 }
1475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001476 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001477 * Accessibility event types that are dispatched for text population.
1478 */
1479 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1480 AccessibilityEvent.TYPE_VIEW_CLICKED
1481 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1482 | AccessibilityEvent.TYPE_VIEW_SELECTED
1483 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1484 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1485 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001486 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
Svetoslav Ganov84dd52e2011-11-18 10:24:00 -08001487 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
Svetoslav Ganov42138042012-03-20 11:51:39 -07001488 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001489 | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED
1490 | AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001491
1492 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001493 * Temporary Rect currently for use in setBackground(). This will probably
1494 * be extended in the future to hold our own class with more than just
1495 * a Rect. :)
1496 */
1497 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001498
1499 /**
1500 * Map used to store views' tags.
1501 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001502 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001505 * The next available accessiiblity id.
1506 */
1507 private static int sNextAccessibilityViewId;
1508
1509 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001510 * The animation currently associated with this view.
1511 * @hide
1512 */
1513 protected Animation mCurrentAnimation = null;
1514
1515 /**
1516 * Width as measured during measure pass.
1517 * {@hide}
1518 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001519 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001520 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521
1522 /**
1523 * Height as measured during measure pass.
1524 * {@hide}
1525 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001526 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001527 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528
1529 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001530 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1531 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1532 * its display list. This flag, used only when hw accelerated, allows us to clear the
1533 * flag while retaining this information until it's needed (at getDisplayList() time and
1534 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1535 *
1536 * {@hide}
1537 */
1538 boolean mRecreateDisplayList = false;
1539
1540 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 * The view's identifier.
1542 * {@hide}
1543 *
1544 * @see #setId(int)
1545 * @see #getId()
1546 */
1547 @ViewDebug.ExportedProperty(resolveId = true)
1548 int mID = NO_ID;
1549
1550 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001551 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001552 */
1553 int mAccessibilityViewId = NO_ID;
1554
1555 /**
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001556 * @hide
1557 */
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07001558 private int mAccessibilityCursorPosition = ACCESSIBILITY_CURSOR_POSITION_UNDEFINED;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001559
1560 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 * The view's tag.
1562 * {@hide}
1563 *
1564 * @see #setTag(Object)
1565 * @see #getTag()
1566 */
1567 protected Object mTag;
1568
1569 // for mPrivateFlags:
1570 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001571 static final int PFLAG_WANTS_FOCUS = 0x00000001;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001572 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001573 static final int PFLAG_FOCUSED = 0x00000002;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001574 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001575 static final int PFLAG_SELECTED = 0x00000004;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001576 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001577 static final int PFLAG_IS_ROOT_NAMESPACE = 0x00000008;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001578 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001579 static final int PFLAG_HAS_BOUNDS = 0x00000010;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001580 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001581 static final int PFLAG_DRAWN = 0x00000020;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001582 /**
1583 * When this flag is set, this view is running an animation on behalf of its
1584 * children and should therefore not cancel invalidate requests, even if they
1585 * lie outside of this view's bounds.
1586 *
1587 * {@hide}
1588 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001589 static final int PFLAG_DRAW_ANIMATION = 0x00000040;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001590 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001591 static final int PFLAG_SKIP_DRAW = 0x00000080;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001592 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001593 static final int PFLAG_ONLY_DRAWS_BACKGROUND = 0x00000100;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001595 static final int PFLAG_REQUEST_TRANSPARENT_REGIONS = 0x00000200;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001596 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001597 static final int PFLAG_DRAWABLE_STATE_DIRTY = 0x00000400;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001599 static final int PFLAG_MEASURED_DIMENSION_SET = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001600 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001601 static final int PFLAG_FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001602 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001603 static final int PFLAG_LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001604
Dianne Hackborn4702a852012-08-17 15:18:29 -07001605 private static final int PFLAG_PRESSED = 0x00004000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606
1607 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001608 static final int PFLAG_DRAWING_CACHE_VALID = 0x00008000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609 /**
1610 * Flag used to indicate that this view should be drawn once more (and only once
1611 * more) after its animation has completed.
1612 * {@hide}
1613 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001614 static final int PFLAG_ANIMATION_STARTED = 0x00010000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001615
Dianne Hackborn4702a852012-08-17 15:18:29 -07001616 private static final int PFLAG_SAVE_STATE_CALLED = 0x00020000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001617
1618 /**
1619 * Indicates that the View returned true when onSetAlpha() was called and that
1620 * the alpha must be restored.
1621 * {@hide}
1622 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001623 static final int PFLAG_ALPHA_SET = 0x00040000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624
1625 /**
1626 * Set by {@link #setScrollContainer(boolean)}.
1627 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001628 static final int PFLAG_SCROLL_CONTAINER = 0x00080000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001629
1630 /**
1631 * Set by {@link #setScrollContainer(boolean)}.
1632 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001633 static final int PFLAG_SCROLL_CONTAINER_ADDED = 0x00100000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001634
1635 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001636 * View flag indicating whether this view was invalidated (fully or partially.)
1637 *
1638 * @hide
1639 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001640 static final int PFLAG_DIRTY = 0x00200000;
Romain Guy809a7f62009-05-14 15:44:42 -07001641
1642 /**
1643 * View flag indicating whether this view was invalidated by an opaque
1644 * invalidate request.
1645 *
1646 * @hide
1647 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001648 static final int PFLAG_DIRTY_OPAQUE = 0x00400000;
Romain Guy809a7f62009-05-14 15:44:42 -07001649
1650 /**
Dianne Hackborn4702a852012-08-17 15:18:29 -07001651 * Mask for {@link #PFLAG_DIRTY} and {@link #PFLAG_DIRTY_OPAQUE}.
Romain Guy809a7f62009-05-14 15:44:42 -07001652 *
1653 * @hide
1654 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001655 static final int PFLAG_DIRTY_MASK = 0x00600000;
Romain Guy809a7f62009-05-14 15:44:42 -07001656
1657 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001658 * Indicates whether the background is opaque.
1659 *
1660 * @hide
1661 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001662 static final int PFLAG_OPAQUE_BACKGROUND = 0x00800000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001663
1664 /**
1665 * Indicates whether the scrollbars are opaque.
1666 *
1667 * @hide
1668 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001669 static final int PFLAG_OPAQUE_SCROLLBARS = 0x01000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001670
1671 /**
1672 * Indicates whether the view is opaque.
1673 *
1674 * @hide
1675 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001676 static final int PFLAG_OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001677
Adam Powelle14579b2009-12-16 18:39:52 -08001678 /**
1679 * Indicates a prepressed state;
1680 * the short time between ACTION_DOWN and recognizing
1681 * a 'real' press. Prepressed is used to recognize quick taps
1682 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001683 *
Adam Powelle14579b2009-12-16 18:39:52 -08001684 * @hide
1685 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001686 private static final int PFLAG_PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001687
Adam Powellc9fbaab2010-02-16 17:16:19 -08001688 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001689 * Indicates whether the view is temporarily detached.
1690 *
1691 * @hide
1692 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001693 static final int PFLAG_CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001694
Adam Powell8568c3a2010-04-19 14:26:11 -07001695 /**
1696 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001697 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001698 * @hide
1699 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001700 private static final int PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001701
1702 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001703 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1704 * @hide
1705 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001706 private static final int PFLAG_HOVERED = 0x10000000;
Jeff Browna032cc02011-03-07 16:56:21 -08001707
1708 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001709 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1710 * for transform operations
1711 *
1712 * @hide
1713 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001714 private static final int PFLAG_PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001715
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001716 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001717 static final int PFLAG_ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001718
Chet Haasefd2b0022010-08-06 13:08:56 -07001719 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001720 * Indicates that this view was specifically invalidated, not just dirtied because some
1721 * child view was invalidated. The flag is used to determine when we need to recreate
1722 * a view's display list (as opposed to just returning a reference to its existing
1723 * display list).
1724 *
1725 * @hide
1726 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001727 static final int PFLAG_INVALIDATED = 0x80000000;
Chet Haasedaf98e92011-01-10 14:10:36 -08001728
Christopher Tate3d4bf172011-03-28 16:16:46 -07001729 /* Masks for mPrivateFlags2 */
1730
1731 /**
1732 * Indicates that this view has reported that it can accept the current drag's content.
1733 * Cleared when the drag operation concludes.
1734 * @hide
1735 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001736 static final int PFLAG2_DRAG_CAN_ACCEPT = 0x00000001;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001737
1738 /**
1739 * Indicates that this view is currently directly under the drag location in a
1740 * drag-and-drop operation involving content that it can accept. Cleared when
1741 * the drag exits the view, or when the drag operation concludes.
1742 * @hide
1743 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001744 static final int PFLAG2_DRAG_HOVERED = 0x00000002;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001745
Cibu Johny86666632010-02-22 13:01:02 -08001746 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001747 * Horizontal layout direction of this view is from Left to Right.
1748 * Use with {@link #setLayoutDirection}.
Cibu Johny86666632010-02-22 13:01:02 -08001749 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001750 public static final int LAYOUT_DIRECTION_LTR = 0;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001751
1752 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001753 * Horizontal layout direction of this view is from Right to Left.
1754 * Use with {@link #setLayoutDirection}.
1755 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001756 public static final int LAYOUT_DIRECTION_RTL = 1;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001757
1758 /**
1759 * Horizontal layout direction of this view is inherited from its parent.
1760 * Use with {@link #setLayoutDirection}.
1761 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001762 public static final int LAYOUT_DIRECTION_INHERIT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001763
1764 /**
1765 * Horizontal layout direction of this view is from deduced from the default language
1766 * script for the locale. Use with {@link #setLayoutDirection}.
1767 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001768 public static final int LAYOUT_DIRECTION_LOCALE = 3;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001769
1770 /**
1771 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001772 * @hide
1773 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001774 static final int PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001775
1776 /**
1777 * Mask for use with private flags indicating bits used for horizontal layout direction.
1778 * @hide
1779 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001780 static final int PFLAG2_LAYOUT_DIRECTION_MASK = 0x00000003 << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001781
1782 /**
1783 * Indicates whether the view horizontal layout direction has been resolved and drawn to the
1784 * right-to-left direction.
1785 * @hide
1786 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001787 static final int PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL = 4 << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001788
1789 /**
1790 * Indicates whether the view horizontal layout direction has been resolved.
1791 * @hide
1792 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001793 static final int PFLAG2_LAYOUT_DIRECTION_RESOLVED = 8 << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001794
1795 /**
1796 * Mask for use with private flags indicating bits used for resolved horizontal layout direction.
1797 * @hide
1798 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001799 static final int PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK = 0x0000000C
1800 << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001801
1802 /*
1803 * Array of horizontal layout direction flags for mapping attribute "layoutDirection" to correct
1804 * flag value.
1805 * @hide
1806 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001807 private static final int[] LAYOUT_DIRECTION_FLAGS = {
1808 LAYOUT_DIRECTION_LTR,
1809 LAYOUT_DIRECTION_RTL,
1810 LAYOUT_DIRECTION_INHERIT,
1811 LAYOUT_DIRECTION_LOCALE
1812 };
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001813
1814 /**
1815 * Default horizontal layout direction.
1816 * @hide
1817 */
1818 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001819
Adam Powell539ee872012-02-03 19:00:49 -08001820 /**
1821 * Indicates that the view is tracking some sort of transient state
1822 * that the app should not need to be aware of, but that the framework
1823 * should take special care to preserve.
1824 *
1825 * @hide
1826 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001827 static final int PFLAG2_HAS_TRANSIENT_STATE = 0x00000100;
Adam Powell539ee872012-02-03 19:00:49 -08001828
1829
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001830 /**
1831 * Text direction is inherited thru {@link ViewGroup}
1832 */
1833 public static final int TEXT_DIRECTION_INHERIT = 0;
1834
1835 /**
1836 * Text direction is using "first strong algorithm". The first strong directional character
1837 * determines the paragraph direction. If there is no strong directional character, the
1838 * paragraph direction is the view's resolved layout direction.
1839 */
1840 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
1841
1842 /**
1843 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
1844 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
1845 * If there are neither, the paragraph direction is the view's resolved layout direction.
1846 */
1847 public static final int TEXT_DIRECTION_ANY_RTL = 2;
1848
1849 /**
1850 * Text direction is forced to LTR.
1851 */
1852 public static final int TEXT_DIRECTION_LTR = 3;
1853
1854 /**
1855 * Text direction is forced to RTL.
1856 */
1857 public static final int TEXT_DIRECTION_RTL = 4;
1858
1859 /**
1860 * Text direction is coming from the system Locale.
1861 */
1862 public static final int TEXT_DIRECTION_LOCALE = 5;
1863
1864 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001865 * Default text direction is inherited
1866 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001867 public static int TEXT_DIRECTION_DEFAULT = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001868
1869 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001870 * Bit shift to get the horizontal layout direction. (bits after LAYOUT_DIRECTION_RESOLVED)
1871 * @hide
1872 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001873 static final int PFLAG2_TEXT_DIRECTION_MASK_SHIFT = 6;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001874
1875 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001876 * Mask for use with private flags indicating bits used for text direction.
1877 * @hide
1878 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001879 static final int PFLAG2_TEXT_DIRECTION_MASK = 0x00000007
1880 << PFLAG2_TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001881
1882 /**
1883 * Array of text direction flags for mapping attribute "textDirection" to correct
1884 * flag value.
1885 * @hide
1886 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001887 private static final int[] PFLAG2_TEXT_DIRECTION_FLAGS = {
1888 TEXT_DIRECTION_INHERIT << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1889 TEXT_DIRECTION_FIRST_STRONG << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1890 TEXT_DIRECTION_ANY_RTL << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1891 TEXT_DIRECTION_LTR << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1892 TEXT_DIRECTION_RTL << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1893 TEXT_DIRECTION_LOCALE << PFLAG2_TEXT_DIRECTION_MASK_SHIFT
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001894 };
1895
1896 /**
1897 * Indicates whether the view text direction has been resolved.
1898 * @hide
1899 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001900 static final int PFLAG2_TEXT_DIRECTION_RESOLVED = 0x00000008
1901 << PFLAG2_TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001902
1903 /**
1904 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1905 * @hide
1906 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001907 static final int PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT = 10;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001908
1909 /**
1910 * Mask for use with private flags indicating bits used for resolved text direction.
1911 * @hide
1912 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001913 static final int PFLAG2_TEXT_DIRECTION_RESOLVED_MASK = 0x00000007
1914 << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001915
1916 /**
1917 * Indicates whether the view text direction has been resolved to the "first strong" heuristic.
1918 * @hide
1919 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001920 static final int PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT =
1921 TEXT_DIRECTION_FIRST_STRONG << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001922
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001923 /*
1924 * Default text alignment. The text alignment of this View is inherited from its parent.
1925 * Use with {@link #setTextAlignment(int)}
1926 */
1927 public static final int TEXT_ALIGNMENT_INHERIT = 0;
1928
1929 /**
1930 * Default for the root view. The gravity determines the text alignment, ALIGN_NORMAL,
1931 * ALIGN_CENTER, or ALIGN_OPPOSITE, which are relative to each paragraph’s text direction.
1932 *
1933 * Use with {@link #setTextAlignment(int)}
1934 */
1935 public static final int TEXT_ALIGNMENT_GRAVITY = 1;
1936
1937 /**
1938 * Align to the start of the paragraph, e.g. ALIGN_NORMAL.
1939 *
1940 * Use with {@link #setTextAlignment(int)}
1941 */
1942 public static final int TEXT_ALIGNMENT_TEXT_START = 2;
1943
1944 /**
1945 * Align to the end of the paragraph, e.g. ALIGN_OPPOSITE.
1946 *
1947 * Use with {@link #setTextAlignment(int)}
1948 */
1949 public static final int TEXT_ALIGNMENT_TEXT_END = 3;
1950
1951 /**
1952 * Center the paragraph, e.g. ALIGN_CENTER.
1953 *
1954 * Use with {@link #setTextAlignment(int)}
1955 */
1956 public static final int TEXT_ALIGNMENT_CENTER = 4;
1957
1958 /**
1959 * Align to the start of the view, which is ALIGN_LEFT if the view’s resolved
1960 * layoutDirection is LTR, and ALIGN_RIGHT otherwise.
1961 *
1962 * Use with {@link #setTextAlignment(int)}
1963 */
1964 public static final int TEXT_ALIGNMENT_VIEW_START = 5;
1965
1966 /**
1967 * Align to the end of the view, which is ALIGN_RIGHT if the view’s resolved
1968 * layoutDirection is LTR, and ALIGN_LEFT otherwise.
1969 *
1970 * Use with {@link #setTextAlignment(int)}
1971 */
1972 public static final int TEXT_ALIGNMENT_VIEW_END = 6;
1973
1974 /**
1975 * Default text alignment is inherited
1976 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001977 public static int TEXT_ALIGNMENT_DEFAULT = TEXT_ALIGNMENT_GRAVITY;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001978
1979 /**
1980 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1981 * @hide
1982 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001983 static final int PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT = 13;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001984
1985 /**
1986 * Mask for use with private flags indicating bits used for text alignment.
1987 * @hide
1988 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001989 static final int PFLAG2_TEXT_ALIGNMENT_MASK = 0x00000007 << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001990
1991 /**
1992 * Array of text direction flags for mapping attribute "textAlignment" to correct
1993 * flag value.
1994 * @hide
1995 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001996 private static final int[] PFLAG2_TEXT_ALIGNMENT_FLAGS = {
1997 TEXT_ALIGNMENT_INHERIT << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
1998 TEXT_ALIGNMENT_GRAVITY << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
1999 TEXT_ALIGNMENT_TEXT_START << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2000 TEXT_ALIGNMENT_TEXT_END << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2001 TEXT_ALIGNMENT_CENTER << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2002 TEXT_ALIGNMENT_VIEW_START << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2003 TEXT_ALIGNMENT_VIEW_END << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002004 };
2005
2006 /**
2007 * Indicates whether the view text alignment has been resolved.
2008 * @hide
2009 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002010 static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED = 0x00000008 << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002011
2012 /**
2013 * Bit shift to get the resolved text alignment.
2014 * @hide
2015 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002016 static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT = 17;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002017
2018 /**
2019 * Mask for use with private flags indicating bits used for text alignment.
2020 * @hide
2021 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002022 static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK = 0x00000007
2023 << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002024
2025 /**
2026 * Indicates whether if the view text alignment has been resolved to gravity
2027 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002028 private static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT =
2029 TEXT_ALIGNMENT_GRAVITY << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002030
Svetoslav Ganov42138042012-03-20 11:51:39 -07002031 // Accessiblity constants for mPrivateFlags2
2032
2033 /**
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07002034 * Shift for the bits in {@link #mPrivateFlags2} related to the
2035 * "importantForAccessibility" attribute.
Svetoslav Ganov42138042012-03-20 11:51:39 -07002036 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002037 static final int PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT = 20;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002038
2039 /**
2040 * Automatically determine whether a view is important for accessibility.
2041 */
2042 public static final int IMPORTANT_FOR_ACCESSIBILITY_AUTO = 0x00000000;
2043
2044 /**
2045 * The view is important for accessibility.
2046 */
2047 public static final int IMPORTANT_FOR_ACCESSIBILITY_YES = 0x00000001;
2048
2049 /**
2050 * The view is not important for accessibility.
2051 */
2052 public static final int IMPORTANT_FOR_ACCESSIBILITY_NO = 0x00000002;
2053
2054 /**
2055 * The default whether the view is important for accessiblity.
2056 */
2057 static final int IMPORTANT_FOR_ACCESSIBILITY_DEFAULT = IMPORTANT_FOR_ACCESSIBILITY_AUTO;
2058
2059 /**
2060 * Mask for obtainig the bits which specify how to determine
2061 * whether a view is important for accessibility.
2062 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002063 static final int PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK = (IMPORTANT_FOR_ACCESSIBILITY_AUTO
Svetoslav Ganov42138042012-03-20 11:51:39 -07002064 | IMPORTANT_FOR_ACCESSIBILITY_YES | IMPORTANT_FOR_ACCESSIBILITY_NO)
Dianne Hackborn4702a852012-08-17 15:18:29 -07002065 << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002066
2067 /**
2068 * Flag indicating whether a view has accessibility focus.
2069 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002070 static final int PFLAG2_ACCESSIBILITY_FOCUSED = 0x00000040 << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002071
2072 /**
2073 * Flag indicating whether a view state for accessibility has changed.
2074 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002075 static final int PFLAG2_ACCESSIBILITY_STATE_CHANGED = 0x00000080
2076 << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07002077
Chet Haaseafd5c3e2012-05-10 13:21:10 -07002078 /**
Chet Haase1a3ab172012-05-11 08:41:20 -07002079 * Flag indicating whether a view failed the quickReject() check in draw(). This condition
2080 * is used to check whether later changes to the view's transform should invalidate the
2081 * view to force the quickReject test to run again.
2082 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002083 static final int PFLAG2_VIEW_QUICK_REJECTED = 0x10000000;
Chet Haase1a3ab172012-05-11 08:41:20 -07002084
Adam Powell0090f202012-08-07 17:15:30 -07002085 /**
2086 * Flag indicating that start/end padding has been resolved into left/right padding
2087 * for use in measurement, layout, drawing, etc. This is set by {@link #resolvePadding()}
2088 * and checked by {@link #measure(int, int)} to determine if padding needs to be resolved
2089 * during measurement. In some special cases this is required such as when an adapter-based
2090 * view measures prospective children without attaching them to a window.
2091 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002092 static final int PFLAG2_PADDING_RESOLVED = 0x20000000;
Adam Powell0090f202012-08-07 17:15:30 -07002093
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07002094 // There are a couple of flags left in mPrivateFlags2
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07002095
Christopher Tate3d4bf172011-03-28 16:16:46 -07002096 /* End of masks for mPrivateFlags2 */
2097
Chet Haase21433372012-06-05 07:54:09 -07002098 /* Masks for mPrivateFlags3 */
2099
2100 /**
2101 * Flag indicating that view has a transform animation set on it. This is used to track whether
2102 * an animation is cleared between successive frames, in order to tell the associated
2103 * DisplayList to clear its animation matrix.
2104 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002105 static final int PFLAG3_VIEW_IS_ANIMATING_TRANSFORM = 0x1;
Chet Haase21433372012-06-05 07:54:09 -07002106
2107 /**
2108 * Flag indicating that view has an alpha animation set on it. This is used to track whether an
2109 * animation is cleared between successive frames, in order to tell the associated
2110 * DisplayList to restore its alpha value.
2111 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002112 static final int PFLAG3_VIEW_IS_ANIMATING_ALPHA = 0x2;
Chet Haase21433372012-06-05 07:54:09 -07002113
2114
2115 /* End of masks for mPrivateFlags3 */
2116
Dianne Hackborn4702a852012-08-17 15:18:29 -07002117 static final int DRAG_MASK = PFLAG2_DRAG_CAN_ACCEPT | PFLAG2_DRAG_HOVERED;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002118
Chet Haasedaf98e92011-01-10 14:10:36 -08002119 /**
Adam Powell637d3372010-08-25 14:37:03 -07002120 * Always allow a user to over-scroll this view, provided it is a
2121 * view that can scroll.
2122 *
2123 * @see #getOverScrollMode()
2124 * @see #setOverScrollMode(int)
2125 */
2126 public static final int OVER_SCROLL_ALWAYS = 0;
2127
2128 /**
2129 * Allow a user to over-scroll this view only if the content is large
2130 * enough to meaningfully scroll, provided it is a view that can scroll.
2131 *
2132 * @see #getOverScrollMode()
2133 * @see #setOverScrollMode(int)
2134 */
2135 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
2136
2137 /**
2138 * Never allow a user to over-scroll this view.
2139 *
2140 * @see #getOverScrollMode()
2141 * @see #setOverScrollMode(int)
2142 */
2143 public static final int OVER_SCROLL_NEVER = 2;
2144
2145 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002146 * Special constant for {@link #setSystemUiVisibility(int)}: View has
2147 * requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08002148 *
Joe Malin32736f02011-01-19 16:14:20 -08002149 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002150 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002151 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08002152
2153 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002154 * Flag for {@link #setSystemUiVisibility(int)}: View has requested the
2155 * system UI to enter an unobtrusive "low profile" mode.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002156 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002157 * <p>This is for use in games, book readers, video players, or any other
Philip Milne6c8ea062012-04-03 17:38:43 -07002158 * "immersive" application where the usual system chrome is deemed too distracting.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002159 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002160 * <p>In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08002161 *
Joe Malin32736f02011-01-19 16:14:20 -08002162 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002163 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002164 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
2165
2166 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002167 * Flag for {@link #setSystemUiVisibility(int)}: View has requested that the
2168 * system navigation be temporarily hidden.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002169 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002170 * <p>This is an even less obtrusive state than that called for by
Daniel Sandler60ee2562011-07-22 12:34:33 -04002171 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
2172 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
2173 * those to disappear. This is useful (in conjunction with the
Philip Milne6c8ea062012-04-03 17:38:43 -07002174 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
Daniel Sandler60ee2562011-07-22 12:34:33 -04002175 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
2176 * window flags) for displaying content using every last pixel on the display.
2177 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002178 * <p>There is a limitation: because navigation controls are so important, the least user
2179 * interaction will cause them to reappear immediately. When this happens, both
2180 * this flag and {@link #SYSTEM_UI_FLAG_FULLSCREEN} will be cleared automatically,
2181 * so that both elements reappear at the same time.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002182 *
2183 * @see #setSystemUiVisibility(int)
2184 */
2185 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
2186
2187 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002188 * Flag for {@link #setSystemUiVisibility(int)}: View has requested to go
2189 * into the normal fullscreen mode so that its content can take over the screen
2190 * while still allowing the user to interact with the application.
2191 *
2192 * <p>This has the same visual effect as
2193 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN
2194 * WindowManager.LayoutParams.FLAG_FULLSCREEN},
2195 * meaning that non-critical screen decorations (such as the status bar) will be
2196 * hidden while the user is in the View's window, focusing the experience on
2197 * that content. Unlike the window flag, if you are using ActionBar in
2198 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2199 * Window.FEATURE_ACTION_BAR_OVERLAY}, then enabling this flag will also
2200 * hide the action bar.
2201 *
2202 * <p>This approach to going fullscreen is best used over the window flag when
2203 * it is a transient state -- that is, the application does this at certain
2204 * points in its user interaction where it wants to allow the user to focus
2205 * on content, but not as a continuous state. For situations where the application
2206 * would like to simply stay full screen the entire time (such as a game that
2207 * wants to take over the screen), the
2208 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN window flag}
2209 * is usually a better approach. The state set here will be removed by the system
2210 * in various situations (such as the user moving to another application) like
2211 * the other system UI states.
2212 *
2213 * <p>When using this flag, the application should provide some easy facility
2214 * for the user to go out of it. A common example would be in an e-book
2215 * reader, where tapping on the screen brings back whatever screen and UI
2216 * decorations that had been hidden while the user was immersed in reading
2217 * the book.
2218 *
2219 * @see #setSystemUiVisibility(int)
2220 */
2221 public static final int SYSTEM_UI_FLAG_FULLSCREEN = 0x00000004;
2222
2223 /**
2224 * Flag for {@link #setSystemUiVisibility(int)}: When using other layout
2225 * flags, we would like a stable view of the content insets given to
2226 * {@link #fitSystemWindows(Rect)}. This means that the insets seen there
2227 * will always represent the worst case that the application can expect
Dianne Hackborn5b5cc4d2012-05-16 13:15:00 -07002228 * as a continuous state. In the stock Android UI this is the space for
2229 * the system bar, nav bar, and status bar, but not more transient elements
2230 * such as an input method.
2231 *
2232 * The stable layout your UI sees is based on the system UI modes you can
2233 * switch to. That is, if you specify {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
2234 * then you will get a stable layout for changes of the
2235 * {@link #SYSTEM_UI_FLAG_FULLSCREEN} mode; if you specify
2236 * {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN} and
2237 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION}, then you can transition
2238 * to {@link #SYSTEM_UI_FLAG_FULLSCREEN} and {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}
2239 * with a stable layout. (Note that you should avoid using
2240 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} by itself.)
2241 *
Jeff Smitha45746e2012-07-19 14:19:24 -05002242 * If you have set the window flag {@link WindowManager.LayoutParams#FLAG_FULLSCREEN}
Dianne Hackborn5b5cc4d2012-05-16 13:15:00 -07002243 * to hide the status bar (instead of using {@link #SYSTEM_UI_FLAG_FULLSCREEN}),
2244 * then a hidden status bar will be considered a "stable" state for purposes
2245 * here. This allows your UI to continually hide the status bar, while still
2246 * using the system UI flags to hide the action bar while still retaining
2247 * a stable layout. Note that changing the window fullscreen flag will never
2248 * provide a stable layout for a clean transition.
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002249 *
2250 * <p>If you are using ActionBar in
2251 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2252 * Window.FEATURE_ACTION_BAR_OVERLAY}, this flag will also impact the
2253 * insets it adds to those given to the application.
2254 */
2255 public static final int SYSTEM_UI_FLAG_LAYOUT_STABLE = 0x00000100;
2256
2257 /**
2258 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2259 * to be layed out as if it has requested
2260 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, even if it currently hasn't. This
2261 * allows it to avoid artifacts when switching in and out of that mode, at
2262 * the expense that some of its user interface may be covered by screen
2263 * decorations when they are shown. You can perform layout of your inner
2264 * UI elements to account for the navagation system UI through the
2265 * {@link #fitSystemWindows(Rect)} method.
2266 */
2267 public static final int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 0x00000200;
2268
2269 /**
2270 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2271 * to be layed out as if it has requested
2272 * {@link #SYSTEM_UI_FLAG_FULLSCREEN}, even if it currently hasn't. This
2273 * allows it to avoid artifacts when switching in and out of that mode, at
2274 * the expense that some of its user interface may be covered by screen
2275 * decorations when they are shown. You can perform layout of your inner
2276 * UI elements to account for non-fullscreen system UI through the
2277 * {@link #fitSystemWindows(Rect)} method.
2278 */
2279 public static final int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 0x00000400;
2280
2281 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04002282 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
2283 */
2284 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
2285
2286 /**
2287 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
2288 */
2289 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08002290
2291 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002292 * @hide
2293 *
2294 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2295 * out of the public fields to keep the undefined bits out of the developer's way.
2296 *
2297 * Flag to make the status bar not expandable. Unless you also
2298 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
2299 */
2300 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
2301
2302 /**
2303 * @hide
2304 *
2305 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2306 * out of the public fields to keep the undefined bits out of the developer's way.
2307 *
2308 * Flag to hide notification icons and scrolling ticker text.
2309 */
2310 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
2311
2312 /**
2313 * @hide
2314 *
2315 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2316 * out of the public fields to keep the undefined bits out of the developer's way.
2317 *
2318 * Flag to disable incoming notification alerts. This will not block
2319 * icons, but it will block sound, vibrating and other visual or aural notifications.
2320 */
2321 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
2322
2323 /**
2324 * @hide
2325 *
2326 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2327 * out of the public fields to keep the undefined bits out of the developer's way.
2328 *
2329 * Flag to hide only the scrolling ticker. Note that
2330 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
2331 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
2332 */
2333 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
2334
2335 /**
2336 * @hide
2337 *
2338 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2339 * out of the public fields to keep the undefined bits out of the developer's way.
2340 *
2341 * Flag to hide the center system info area.
2342 */
2343 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
2344
2345 /**
2346 * @hide
2347 *
2348 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2349 * out of the public fields to keep the undefined bits out of the developer's way.
2350 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002351 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002352 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2353 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04002354 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002355
2356 /**
2357 * @hide
2358 *
2359 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2360 * out of the public fields to keep the undefined bits out of the developer's way.
2361 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002362 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08002363 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2364 */
2365 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
2366
2367 /**
2368 * @hide
2369 *
2370 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2371 * out of the public fields to keep the undefined bits out of the developer's way.
2372 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002373 * Flag to hide only the clock. You might use this if your activity has
2374 * its own clock making the status bar's clock redundant.
2375 */
Joe Onorato6478adc2011-01-27 21:15:01 -08002376 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
2377
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002378 /**
2379 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04002380 *
2381 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2382 * out of the public fields to keep the undefined bits out of the developer's way.
2383 *
2384 * Flag to hide only the recent apps button. Don't use this
2385 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2386 */
2387 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
2388
2389 /**
2390 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002391 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002392 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002393
2394 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002395 * These are the system UI flags that can be cleared by events outside
2396 * of an application. Currently this is just the ability to tap on the
2397 * screen while hiding the navigation bar to have it return.
2398 * @hide
2399 */
2400 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002401 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION
2402 | SYSTEM_UI_FLAG_FULLSCREEN;
2403
2404 /**
2405 * Flags that can impact the layout in relation to system UI.
2406 */
2407 public static final int SYSTEM_UI_LAYOUT_FLAGS =
2408 SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
2409 | SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002410
2411 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07002412 * Find views that render the specified text.
2413 *
2414 * @see #findViewsWithText(ArrayList, CharSequence, int)
2415 */
2416 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
2417
2418 /**
2419 * Find find views that contain the specified content description.
2420 *
2421 * @see #findViewsWithText(ArrayList, CharSequence, int)
2422 */
2423 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
2424
2425 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07002426 * Find views that contain {@link AccessibilityNodeProvider}. Such
2427 * a View is a root of virtual view hierarchy and may contain the searched
2428 * text. If this flag is set Views with providers are automatically
2429 * added and it is a responsibility of the client to call the APIs of
2430 * the provider to determine whether the virtual tree rooted at this View
2431 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
2432 * represeting the virtual views with this text.
2433 *
2434 * @see #findViewsWithText(ArrayList, CharSequence, int)
2435 *
2436 * @hide
2437 */
2438 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
2439
2440 /**
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07002441 * The undefined cursor position.
2442 */
2443 private static final int ACCESSIBILITY_CURSOR_POSITION_UNDEFINED = -1;
2444
2445 /**
Romain Guybb9908b2012-03-08 11:14:07 -08002446 * Indicates that the screen has changed state and is now off.
2447 *
2448 * @see #onScreenStateChanged(int)
2449 */
2450 public static final int SCREEN_STATE_OFF = 0x0;
2451
2452 /**
2453 * Indicates that the screen has changed state and is now on.
2454 *
Romain Guy1e3d3132012-03-08 15:55:56 -08002455 * @see #onScreenStateChanged(int)
Romain Guybb9908b2012-03-08 11:14:07 -08002456 */
2457 public static final int SCREEN_STATE_ON = 0x1;
2458
2459 /**
Adam Powell637d3372010-08-25 14:37:03 -07002460 * Controls the over-scroll mode for this view.
2461 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2462 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2463 * and {@link #OVER_SCROLL_NEVER}.
2464 */
2465 private int mOverScrollMode;
2466
2467 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002468 * The parent this view is attached to.
2469 * {@hide}
2470 *
2471 * @see #getParent()
2472 */
2473 protected ViewParent mParent;
2474
2475 /**
2476 * {@hide}
2477 */
2478 AttachInfo mAttachInfo;
2479
2480 /**
2481 * {@hide}
2482 */
Romain Guy809a7f62009-05-14 15:44:42 -07002483 @ViewDebug.ExportedProperty(flagMapping = {
Dianne Hackborn4702a852012-08-17 15:18:29 -07002484 @ViewDebug.FlagToString(mask = PFLAG_FORCE_LAYOUT, equals = PFLAG_FORCE_LAYOUT,
Romain Guy809a7f62009-05-14 15:44:42 -07002485 name = "FORCE_LAYOUT"),
Dianne Hackborn4702a852012-08-17 15:18:29 -07002486 @ViewDebug.FlagToString(mask = PFLAG_LAYOUT_REQUIRED, equals = PFLAG_LAYOUT_REQUIRED,
Romain Guy809a7f62009-05-14 15:44:42 -07002487 name = "LAYOUT_REQUIRED"),
Dianne Hackborn4702a852012-08-17 15:18:29 -07002488 @ViewDebug.FlagToString(mask = PFLAG_DRAWING_CACHE_VALID, equals = PFLAG_DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002489 name = "DRAWING_CACHE_INVALID", outputIf = false),
Dianne Hackborn4702a852012-08-17 15:18:29 -07002490 @ViewDebug.FlagToString(mask = PFLAG_DRAWN, equals = PFLAG_DRAWN, name = "DRAWN", outputIf = true),
2491 @ViewDebug.FlagToString(mask = PFLAG_DRAWN, equals = PFLAG_DRAWN, name = "NOT_DRAWN", outputIf = false),
2492 @ViewDebug.FlagToString(mask = PFLAG_DIRTY_MASK, equals = PFLAG_DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2493 @ViewDebug.FlagToString(mask = PFLAG_DIRTY_MASK, equals = PFLAG_DIRTY, name = "DIRTY")
Romain Guy809a7f62009-05-14 15:44:42 -07002494 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002495 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002496 int mPrivateFlags2;
Chet Haase21433372012-06-05 07:54:09 -07002497 int mPrivateFlags3;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002498
2499 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002500 * This view's request for the visibility of the status bar.
2501 * @hide
2502 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002503 @ViewDebug.ExportedProperty(flagMapping = {
2504 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2505 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2506 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2507 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2508 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2509 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2510 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2511 equals = SYSTEM_UI_FLAG_VISIBLE,
2512 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2513 })
Joe Onorato664644d2011-01-23 17:53:23 -08002514 int mSystemUiVisibility;
2515
2516 /**
Chet Haase563d4f22012-04-18 16:20:08 -07002517 * Reference count for transient state.
2518 * @see #setHasTransientState(boolean)
2519 */
2520 int mTransientStateCount = 0;
2521
2522 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002523 * Count of how many windows this view has been attached to.
2524 */
2525 int mWindowAttachCount;
2526
2527 /**
2528 * The layout parameters associated with this view and used by the parent
2529 * {@link android.view.ViewGroup} to determine how this view should be
2530 * laid out.
2531 * {@hide}
2532 */
2533 protected ViewGroup.LayoutParams mLayoutParams;
2534
2535 /**
2536 * The view flags hold various views states.
2537 * {@hide}
2538 */
2539 @ViewDebug.ExportedProperty
2540 int mViewFlags;
2541
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002542 static class TransformationInfo {
2543 /**
2544 * The transform matrix for the View. This transform is calculated internally
2545 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2546 * is used by default. Do *not* use this variable directly; instead call
2547 * getMatrix(), which will automatically recalculate the matrix if necessary
2548 * to get the correct matrix based on the latest rotation and scale properties.
2549 */
2550 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002551
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002552 /**
2553 * The transform matrix for the View. This transform is calculated internally
2554 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2555 * is used by default. Do *not* use this variable directly; instead call
2556 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2557 * to get the correct matrix based on the latest rotation and scale properties.
2558 */
2559 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002560
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002561 /**
2562 * An internal variable that tracks whether we need to recalculate the
2563 * transform matrix, based on whether the rotation or scaleX/Y properties
2564 * have changed since the matrix was last calculated.
2565 */
2566 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002567
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002568 /**
2569 * An internal variable that tracks whether we need to recalculate the
2570 * transform matrix, based on whether the rotation or scaleX/Y properties
2571 * have changed since the matrix was last calculated.
2572 */
2573 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002574
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002575 /**
2576 * A variable that tracks whether we need to recalculate the
2577 * transform matrix, based on whether the rotation or scaleX/Y properties
2578 * have changed since the matrix was last calculated. This variable
2579 * is only valid after a call to updateMatrix() or to a function that
2580 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2581 */
2582 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002583
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002584 /**
2585 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2586 */
2587 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002588
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002589 /**
2590 * This matrix is used when computing the matrix for 3D rotations.
2591 */
2592 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002593
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002594 /**
2595 * These prev values are used to recalculate a centered pivot point when necessary. The
2596 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2597 * set), so thes values are only used then as well.
2598 */
2599 private int mPrevWidth = -1;
2600 private int mPrevHeight = -1;
Philip Milne6c8ea062012-04-03 17:38:43 -07002601
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002602 /**
2603 * The degrees rotation around the vertical axis through the pivot point.
2604 */
2605 @ViewDebug.ExportedProperty
2606 float mRotationY = 0f;
2607
2608 /**
2609 * The degrees rotation around the horizontal axis through the pivot point.
2610 */
2611 @ViewDebug.ExportedProperty
2612 float mRotationX = 0f;
2613
2614 /**
2615 * The degrees rotation around the pivot point.
2616 */
2617 @ViewDebug.ExportedProperty
2618 float mRotation = 0f;
2619
2620 /**
2621 * The amount of translation of the object away from its left property (post-layout).
2622 */
2623 @ViewDebug.ExportedProperty
2624 float mTranslationX = 0f;
2625
2626 /**
2627 * The amount of translation of the object away from its top property (post-layout).
2628 */
2629 @ViewDebug.ExportedProperty
2630 float mTranslationY = 0f;
2631
2632 /**
2633 * The amount of scale in the x direction around the pivot point. A
2634 * value of 1 means no scaling is applied.
2635 */
2636 @ViewDebug.ExportedProperty
2637 float mScaleX = 1f;
2638
2639 /**
2640 * The amount of scale in the y direction around the pivot point. A
2641 * value of 1 means no scaling is applied.
2642 */
2643 @ViewDebug.ExportedProperty
2644 float mScaleY = 1f;
2645
2646 /**
Chet Haasea33de552012-02-03 16:28:24 -08002647 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002648 */
2649 @ViewDebug.ExportedProperty
2650 float mPivotX = 0f;
2651
2652 /**
Chet Haasea33de552012-02-03 16:28:24 -08002653 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002654 */
2655 @ViewDebug.ExportedProperty
2656 float mPivotY = 0f;
2657
2658 /**
2659 * The opacity of the View. This is a value from 0 to 1, where 0 means
2660 * completely transparent and 1 means completely opaque.
2661 */
2662 @ViewDebug.ExportedProperty
2663 float mAlpha = 1f;
2664 }
2665
2666 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002667
Joe Malin32736f02011-01-19 16:14:20 -08002668 private boolean mLastIsOpaque;
2669
Chet Haasefd2b0022010-08-06 13:08:56 -07002670 /**
2671 * Convenience value to check for float values that are close enough to zero to be considered
2672 * zero.
2673 */
Romain Guy2542d192010-08-18 11:47:12 -07002674 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002675
2676 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002677 * The distance in pixels from the left edge of this view's parent
2678 * to the left edge of this view.
2679 * {@hide}
2680 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002681 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002682 protected int mLeft;
2683 /**
2684 * The distance in pixels from the left edge of this view's parent
2685 * to the right edge of this view.
2686 * {@hide}
2687 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002688 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002689 protected int mRight;
2690 /**
2691 * The distance in pixels from the top edge of this view's parent
2692 * to the top edge of this view.
2693 * {@hide}
2694 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002695 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002696 protected int mTop;
2697 /**
2698 * The distance in pixels from the top edge of this view's parent
2699 * to the bottom edge of this view.
2700 * {@hide}
2701 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002702 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002703 protected int mBottom;
2704
2705 /**
2706 * The offset, in pixels, by which the content of this view is scrolled
2707 * horizontally.
2708 * {@hide}
2709 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002710 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002711 protected int mScrollX;
2712 /**
2713 * The offset, in pixels, by which the content of this view is scrolled
2714 * vertically.
2715 * {@hide}
2716 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002717 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002718 protected int mScrollY;
2719
2720 /**
2721 * The left padding in pixels, that is the distance in pixels between the
2722 * left edge of this view and the left edge of its content.
2723 * {@hide}
2724 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002725 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002726 protected int mPaddingLeft;
2727 /**
2728 * The right padding in pixels, that is the distance in pixels between the
2729 * right edge of this view and the right edge of its content.
2730 * {@hide}
2731 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002732 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002733 protected int mPaddingRight;
2734 /**
2735 * The top padding in pixels, that is the distance in pixels between the
2736 * top edge of this view and the top edge of its content.
2737 * {@hide}
2738 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002739 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002740 protected int mPaddingTop;
2741 /**
2742 * The bottom padding in pixels, that is the distance in pixels between the
2743 * bottom edge of this view and the bottom edge of its content.
2744 * {@hide}
2745 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002746 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002747 protected int mPaddingBottom;
2748
2749 /**
Philip Milne1557fd72012-04-04 23:41:34 -07002750 * The layout insets in pixels, that is the distance in pixels between the
2751 * visible edges of this view its bounds.
2752 */
2753 private Insets mLayoutInsets;
2754
2755 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002756 * Briefly describes the view and is primarily used for accessibility support.
2757 */
2758 private CharSequence mContentDescription;
2759
2760 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002761 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002762 *
2763 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002764 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002765 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002766 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002767
2768 /**
2769 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002770 *
2771 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002772 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002773 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002774 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002775
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002776 /**
Adam Powell20232d02010-12-08 21:08:53 -08002777 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002778 *
2779 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002780 */
2781 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002782 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002783
2784 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002785 * Cache the paddingStart set by the user to append to the scrollbar's size.
2786 *
2787 */
2788 @ViewDebug.ExportedProperty(category = "padding")
2789 int mUserPaddingStart;
2790
2791 /**
2792 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2793 *
2794 */
2795 @ViewDebug.ExportedProperty(category = "padding")
2796 int mUserPaddingEnd;
2797
2798 /**
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07002799 * Whether a left padding has been defined during layout inflation.
2800 *
2801 * @hide
2802 */
2803 boolean mUserPaddingLeftDefined = false;
2804
2805 /**
2806 * Whether a right padding has been defined during layout inflation.
2807 *
2808 * @hide
2809 */
2810 boolean mUserPaddingRightDefined = false;
2811
2812 /**
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07002813 * Default undefined padding
2814 */
2815 private static final int UNDEFINED_PADDING = Integer.MIN_VALUE;
2816
2817 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002818 * @hide
2819 */
2820 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2821 /**
2822 * @hide
2823 */
2824 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002825
Philip Milne6c8ea062012-04-03 17:38:43 -07002826 private Drawable mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002827
2828 private int mBackgroundResource;
2829 private boolean mBackgroundSizeChanged;
2830
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002831 static class ListenerInfo {
2832 /**
2833 * Listener used to dispatch focus change events.
2834 * This field should be made private, so it is hidden from the SDK.
2835 * {@hide}
2836 */
2837 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002838
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002839 /**
2840 * Listeners for layout change events.
2841 */
2842 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002843
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002844 /**
2845 * Listeners for attach events.
2846 */
2847 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002848
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002849 /**
2850 * Listener used to dispatch click events.
2851 * This field should be made private, so it is hidden from the SDK.
2852 * {@hide}
2853 */
2854 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002856 /**
2857 * Listener used to dispatch long click events.
2858 * This field should be made private, so it is hidden from the SDK.
2859 * {@hide}
2860 */
2861 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002862
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002863 /**
2864 * Listener used to build the context menu.
2865 * This field should be made private, so it is hidden from the SDK.
2866 * {@hide}
2867 */
2868 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002869
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002870 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002871
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002872 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002873
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002874 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002875
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002876 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002877
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002878 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002879
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002880 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2881 }
2882
2883 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002885 /**
2886 * The application environment this view lives in.
2887 * This field should be made private, so it is hidden from the SDK.
2888 * {@hide}
2889 */
2890 protected Context mContext;
2891
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002892 private final Resources mResources;
2893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002894 private ScrollabilityCache mScrollCache;
2895
2896 private int[] mDrawableState = null;
2897
Romain Guy0211a0a2011-02-14 16:34:59 -08002898 /**
2899 * Set to true when drawing cache is enabled and cannot be created.
Philip Milne6c8ea062012-04-03 17:38:43 -07002900 *
Romain Guy0211a0a2011-02-14 16:34:59 -08002901 * @hide
2902 */
2903 public boolean mCachingFailed;
2904
Romain Guy02890fd2010-08-06 17:58:44 -07002905 private Bitmap mDrawingCache;
2906 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002907 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002908 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002909
2910 /**
2911 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2912 * the user may specify which view to go to next.
2913 */
2914 private int mNextFocusLeftId = View.NO_ID;
2915
2916 /**
2917 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2918 * the user may specify which view to go to next.
2919 */
2920 private int mNextFocusRightId = View.NO_ID;
2921
2922 /**
2923 * When this view has focus and the next focus is {@link #FOCUS_UP},
2924 * the user may specify which view to go to next.
2925 */
2926 private int mNextFocusUpId = View.NO_ID;
2927
2928 /**
2929 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2930 * the user may specify which view to go to next.
2931 */
2932 private int mNextFocusDownId = View.NO_ID;
2933
Jeff Brown4e6319b2010-12-13 10:36:51 -08002934 /**
2935 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2936 * the user may specify which view to go to next.
2937 */
2938 int mNextFocusForwardId = View.NO_ID;
2939
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002940 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002941 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002942 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002943 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002944
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002945 private UnsetPressedState mUnsetPressedState;
2946
2947 /**
2948 * Whether the long press's action has been invoked. The tap's action is invoked on the
2949 * up event while a long press is invoked as soon as the long press duration is reached, so
2950 * a long press could be performed before the tap is checked, in which case the tap's action
2951 * should not be invoked.
2952 */
2953 private boolean mHasPerformedLongPress;
2954
2955 /**
2956 * The minimum height of the view. We'll try our best to have the height
2957 * of this view to at least this amount.
2958 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002959 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002960 private int mMinHeight;
2961
2962 /**
2963 * The minimum width of the view. We'll try our best to have the width
2964 * of this view to at least this amount.
2965 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002966 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002967 private int mMinWidth;
2968
2969 /**
2970 * The delegate to handle touch events that are physically in this view
2971 * but should be handled by another view.
2972 */
2973 private TouchDelegate mTouchDelegate = null;
2974
2975 /**
2976 * Solid color to use as a background when creating the drawing cache. Enables
2977 * the cache to use 16 bit bitmaps instead of 32 bit.
2978 */
2979 private int mDrawingCacheBackgroundColor = 0;
2980
2981 /**
2982 * Special tree observer used when mAttachInfo is null.
2983 */
2984 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002985
Adam Powelle14579b2009-12-16 18:39:52 -08002986 /**
2987 * Cache the touch slop from the context that created the view.
2988 */
2989 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002991 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002992 * Object that handles automatic animation of view properties.
2993 */
2994 private ViewPropertyAnimator mAnimator = null;
2995
2996 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002997 * Flag indicating that a drag can cross window boundaries. When
2998 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2999 * with this flag set, all visible applications will be able to participate
3000 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08003001 *
3002 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08003003 */
3004 public static final int DRAG_FLAG_GLOBAL = 1;
3005
3006 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003007 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
3008 */
3009 private float mVerticalScrollFactor;
3010
3011 /**
Adam Powell20232d02010-12-08 21:08:53 -08003012 * Position of the vertical scroll bar.
3013 */
3014 private int mVerticalScrollbarPosition;
3015
3016 /**
3017 * Position the scroll bar at the default position as determined by the system.
3018 */
3019 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
3020
3021 /**
3022 * Position the scroll bar along the left edge.
3023 */
3024 public static final int SCROLLBAR_POSITION_LEFT = 1;
3025
3026 /**
3027 * Position the scroll bar along the right edge.
3028 */
3029 public static final int SCROLLBAR_POSITION_RIGHT = 2;
3030
3031 /**
Romain Guy171c5922011-01-06 10:04:23 -08003032 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08003033 *
3034 * @see #getLayerType()
3035 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003036 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08003037 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003038 */
3039 public static final int LAYER_TYPE_NONE = 0;
3040
3041 /**
3042 * <p>Indicates that the view has a software layer. A software layer is backed
3043 * by a bitmap and causes the view to be rendered using Android's software
3044 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003045 *
Romain Guy171c5922011-01-06 10:04:23 -08003046 * <p>Software layers have various usages:</p>
3047 * <p>When the application is not using hardware acceleration, a software layer
3048 * is useful to apply a specific color filter and/or blending mode and/or
3049 * translucency to a view and all its children.</p>
3050 * <p>When the application is using hardware acceleration, a software layer
3051 * is useful to render drawing primitives not supported by the hardware
3052 * accelerated pipeline. It can also be used to cache a complex view tree
3053 * into a texture and reduce the complexity of drawing operations. For instance,
3054 * when animating a complex view tree with a translation, a software layer can
3055 * be used to render the view tree only once.</p>
3056 * <p>Software layers should be avoided when the affected view tree updates
3057 * often. Every update will require to re-render the software layer, which can
3058 * potentially be slow (particularly when hardware acceleration is turned on
3059 * since the layer will have to be uploaded into a hardware texture after every
3060 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08003061 *
3062 * @see #getLayerType()
3063 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003064 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08003065 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003066 */
3067 public static final int LAYER_TYPE_SOFTWARE = 1;
3068
3069 /**
3070 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
3071 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
3072 * OpenGL hardware) and causes the view to be rendered using Android's hardware
3073 * rendering pipeline, but only if hardware acceleration is turned on for the
3074 * view hierarchy. When hardware acceleration is turned off, hardware layers
3075 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003076 *
Romain Guy171c5922011-01-06 10:04:23 -08003077 * <p>A hardware layer is useful to apply a specific color filter and/or
3078 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08003079 * <p>A hardware layer can be used to cache a complex view tree into a
3080 * texture and reduce the complexity of drawing operations. For instance,
3081 * when animating a complex view tree with a translation, a hardware layer can
3082 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08003083 * <p>A hardware layer can also be used to increase the rendering quality when
3084 * rotation transformations are applied on a view. It can also be used to
3085 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003086 *
3087 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08003088 * @see #setLayerType(int, android.graphics.Paint)
3089 * @see #LAYER_TYPE_NONE
3090 * @see #LAYER_TYPE_SOFTWARE
3091 */
3092 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08003093
Romain Guy3aaff3a2011-01-12 14:18:47 -08003094 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
3095 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
3096 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
3097 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
3098 })
Romain Guy171c5922011-01-06 10:04:23 -08003099 int mLayerType = LAYER_TYPE_NONE;
3100 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08003101 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08003102
3103 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07003104 * Set to true when the view is sending hover accessibility events because it
3105 * is the innermost hovered view.
3106 */
3107 private boolean mSendingHoverAccessibilityEvents;
3108
Dianne Hackborn4702a852012-08-17 15:18:29 -07003109 /**
3110 * Delegate for injecting accessiblity functionality.
3111 */
3112 AccessibilityDelegate mAccessibilityDelegate;
3113
3114 /**
3115 * Consistency verifier for debugging purposes.
3116 * @hide
3117 */
3118 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
3119 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
3120 new InputEventConsistencyVerifier(this, 0) : null;
3121
Adam Powella9108a22012-07-18 11:18:09 -07003122 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
3123
Jeff Brown87b7f802011-06-21 18:35:45 -07003124 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003125 * Simple constructor to use when creating a view from code.
3126 *
3127 * @param context The Context the view is running in, through which it can
3128 * access the current theme, resources, etc.
3129 */
3130 public View(Context context) {
3131 mContext = context;
3132 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003133 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003134 // Set layout and text direction defaults
Dianne Hackborn4702a852012-08-17 15:18:29 -07003135 mPrivateFlags2 = (LAYOUT_DIRECTION_DEFAULT << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) |
3136 (TEXT_DIRECTION_DEFAULT << PFLAG2_TEXT_DIRECTION_MASK_SHIFT) |
3137 (TEXT_ALIGNMENT_DEFAULT << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT) |
3138 (IMPORTANT_FOR_ACCESSIBILITY_DEFAULT << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT);
Adam Powelle14579b2009-12-16 18:39:52 -08003139 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07003140 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003141 mUserPaddingStart = UNDEFINED_PADDING;
3142 mUserPaddingEnd = UNDEFINED_PADDING;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003143 }
3144
3145 /**
3146 * Constructor that is called when inflating a view from XML. This is called
3147 * when a view is being constructed from an XML file, supplying attributes
3148 * that were specified in the XML file. This version uses a default style of
3149 * 0, so the only attribute values applied are those in the Context's Theme
3150 * and the given AttributeSet.
3151 *
3152 * <p>
3153 * The method onFinishInflate() will be called after all children have been
3154 * added.
3155 *
3156 * @param context The Context the view is running in, through which it can
3157 * access the current theme, resources, etc.
3158 * @param attrs The attributes of the XML tag that is inflating the view.
3159 * @see #View(Context, AttributeSet, int)
3160 */
3161 public View(Context context, AttributeSet attrs) {
3162 this(context, attrs, 0);
3163 }
3164
3165 /**
3166 * Perform inflation from XML and apply a class-specific base style. This
3167 * constructor of View allows subclasses to use their own base style when
3168 * they are inflating. For example, a Button class's constructor would call
3169 * this version of the super class constructor and supply
3170 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
3171 * the theme's button style to modify all of the base view attributes (in
3172 * particular its background) as well as the Button class's attributes.
3173 *
3174 * @param context The Context the view is running in, through which it can
3175 * access the current theme, resources, etc.
3176 * @param attrs The attributes of the XML tag that is inflating the view.
3177 * @param defStyle The default style to apply to this view. If 0, no style
3178 * will be applied (beyond what is included in the theme). This may
3179 * either be an attribute resource, whose value will be retrieved
3180 * from the current theme, or an explicit style resource.
3181 * @see #View(Context, AttributeSet)
3182 */
3183 public View(Context context, AttributeSet attrs, int defStyle) {
3184 this(context);
3185
3186 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
3187 defStyle, 0);
3188
3189 Drawable background = null;
3190
3191 int leftPadding = -1;
3192 int topPadding = -1;
3193 int rightPadding = -1;
3194 int bottomPadding = -1;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003195 int startPadding = UNDEFINED_PADDING;
3196 int endPadding = UNDEFINED_PADDING;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003197
3198 int padding = -1;
3199
3200 int viewFlagValues = 0;
3201 int viewFlagMasks = 0;
3202
3203 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07003204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003205 int x = 0;
3206 int y = 0;
3207
Chet Haase73066682010-11-29 15:55:32 -08003208 float tx = 0;
3209 float ty = 0;
3210 float rotation = 0;
3211 float rotationX = 0;
3212 float rotationY = 0;
3213 float sx = 1f;
3214 float sy = 1f;
3215 boolean transformSet = false;
3216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003217 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
Adam Powell637d3372010-08-25 14:37:03 -07003218 int overScrollMode = mOverScrollMode;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003219 boolean initializeScrollbars = false;
3220
3221 final int targetSdkVersion = context.getApplicationInfo().targetSdkVersion;
3222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003223 final int N = a.getIndexCount();
3224 for (int i = 0; i < N; i++) {
3225 int attr = a.getIndex(i);
3226 switch (attr) {
3227 case com.android.internal.R.styleable.View_background:
3228 background = a.getDrawable(attr);
3229 break;
3230 case com.android.internal.R.styleable.View_padding:
3231 padding = a.getDimensionPixelSize(attr, -1);
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003232 mUserPaddingLeftDefined = true;
3233 mUserPaddingRightDefined = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003234 break;
3235 case com.android.internal.R.styleable.View_paddingLeft:
3236 leftPadding = a.getDimensionPixelSize(attr, -1);
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003237 mUserPaddingLeftDefined = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003238 break;
3239 case com.android.internal.R.styleable.View_paddingTop:
3240 topPadding = a.getDimensionPixelSize(attr, -1);
3241 break;
3242 case com.android.internal.R.styleable.View_paddingRight:
3243 rightPadding = a.getDimensionPixelSize(attr, -1);
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003244 mUserPaddingRightDefined = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003245 break;
3246 case com.android.internal.R.styleable.View_paddingBottom:
3247 bottomPadding = a.getDimensionPixelSize(attr, -1);
3248 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003249 case com.android.internal.R.styleable.View_paddingStart:
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003250 startPadding = a.getDimensionPixelSize(attr, UNDEFINED_PADDING);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003251 break;
3252 case com.android.internal.R.styleable.View_paddingEnd:
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003253 endPadding = a.getDimensionPixelSize(attr, UNDEFINED_PADDING);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003254 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003255 case com.android.internal.R.styleable.View_scrollX:
3256 x = a.getDimensionPixelOffset(attr, 0);
3257 break;
3258 case com.android.internal.R.styleable.View_scrollY:
3259 y = a.getDimensionPixelOffset(attr, 0);
3260 break;
Chet Haase73066682010-11-29 15:55:32 -08003261 case com.android.internal.R.styleable.View_alpha:
3262 setAlpha(a.getFloat(attr, 1f));
3263 break;
3264 case com.android.internal.R.styleable.View_transformPivotX:
3265 setPivotX(a.getDimensionPixelOffset(attr, 0));
3266 break;
3267 case com.android.internal.R.styleable.View_transformPivotY:
3268 setPivotY(a.getDimensionPixelOffset(attr, 0));
3269 break;
3270 case com.android.internal.R.styleable.View_translationX:
3271 tx = a.getDimensionPixelOffset(attr, 0);
3272 transformSet = true;
3273 break;
3274 case com.android.internal.R.styleable.View_translationY:
3275 ty = a.getDimensionPixelOffset(attr, 0);
3276 transformSet = true;
3277 break;
3278 case com.android.internal.R.styleable.View_rotation:
3279 rotation = a.getFloat(attr, 0);
3280 transformSet = true;
3281 break;
3282 case com.android.internal.R.styleable.View_rotationX:
3283 rotationX = a.getFloat(attr, 0);
3284 transformSet = true;
3285 break;
3286 case com.android.internal.R.styleable.View_rotationY:
3287 rotationY = a.getFloat(attr, 0);
3288 transformSet = true;
3289 break;
3290 case com.android.internal.R.styleable.View_scaleX:
3291 sx = a.getFloat(attr, 1f);
3292 transformSet = true;
3293 break;
3294 case com.android.internal.R.styleable.View_scaleY:
3295 sy = a.getFloat(attr, 1f);
3296 transformSet = true;
3297 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003298 case com.android.internal.R.styleable.View_id:
3299 mID = a.getResourceId(attr, NO_ID);
3300 break;
3301 case com.android.internal.R.styleable.View_tag:
3302 mTag = a.getText(attr);
3303 break;
3304 case com.android.internal.R.styleable.View_fitsSystemWindows:
3305 if (a.getBoolean(attr, false)) {
3306 viewFlagValues |= FITS_SYSTEM_WINDOWS;
3307 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
3308 }
3309 break;
3310 case com.android.internal.R.styleable.View_focusable:
3311 if (a.getBoolean(attr, false)) {
3312 viewFlagValues |= FOCUSABLE;
3313 viewFlagMasks |= FOCUSABLE_MASK;
3314 }
3315 break;
3316 case com.android.internal.R.styleable.View_focusableInTouchMode:
3317 if (a.getBoolean(attr, false)) {
3318 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
3319 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
3320 }
3321 break;
3322 case com.android.internal.R.styleable.View_clickable:
3323 if (a.getBoolean(attr, false)) {
3324 viewFlagValues |= CLICKABLE;
3325 viewFlagMasks |= CLICKABLE;
3326 }
3327 break;
3328 case com.android.internal.R.styleable.View_longClickable:
3329 if (a.getBoolean(attr, false)) {
3330 viewFlagValues |= LONG_CLICKABLE;
3331 viewFlagMasks |= LONG_CLICKABLE;
3332 }
3333 break;
3334 case com.android.internal.R.styleable.View_saveEnabled:
3335 if (!a.getBoolean(attr, true)) {
3336 viewFlagValues |= SAVE_DISABLED;
3337 viewFlagMasks |= SAVE_DISABLED_MASK;
3338 }
3339 break;
3340 case com.android.internal.R.styleable.View_duplicateParentState:
3341 if (a.getBoolean(attr, false)) {
3342 viewFlagValues |= DUPLICATE_PARENT_STATE;
3343 viewFlagMasks |= DUPLICATE_PARENT_STATE;
3344 }
3345 break;
3346 case com.android.internal.R.styleable.View_visibility:
3347 final int visibility = a.getInt(attr, 0);
3348 if (visibility != 0) {
3349 viewFlagValues |= VISIBILITY_FLAGS[visibility];
3350 viewFlagMasks |= VISIBILITY_MASK;
3351 }
3352 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003353 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003354 // Clear any layout direction flags (included resolved bits) already set
Dianne Hackborn4702a852012-08-17 15:18:29 -07003355 mPrivateFlags2 &= ~(PFLAG2_LAYOUT_DIRECTION_MASK | PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003356 // Set the layout direction flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003357 final int layoutDirection = a.getInt(attr, -1);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003358 final int value = (layoutDirection != -1) ?
3359 LAYOUT_DIRECTION_FLAGS[layoutDirection] : LAYOUT_DIRECTION_DEFAULT;
Dianne Hackborn4702a852012-08-17 15:18:29 -07003360 mPrivateFlags2 |= (value << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT);
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07003361 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003362 case com.android.internal.R.styleable.View_drawingCacheQuality:
3363 final int cacheQuality = a.getInt(attr, 0);
3364 if (cacheQuality != 0) {
3365 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
3366 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
3367 }
3368 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07003369 case com.android.internal.R.styleable.View_contentDescription:
Svetoslav Ganove47957a2012-06-05 14:46:50 -07003370 setContentDescription(a.getString(attr));
svetoslavganov75986cf2009-05-14 22:28:01 -07003371 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003372 case com.android.internal.R.styleable.View_soundEffectsEnabled:
3373 if (!a.getBoolean(attr, true)) {
3374 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
3375 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
3376 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003377 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003378 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
3379 if (!a.getBoolean(attr, true)) {
3380 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
3381 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
3382 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003383 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003384 case R.styleable.View_scrollbars:
3385 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
3386 if (scrollbars != SCROLLBARS_NONE) {
3387 viewFlagValues |= scrollbars;
3388 viewFlagMasks |= SCROLLBARS_MASK;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003389 initializeScrollbars = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003390 }
3391 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07003392 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003393 case R.styleable.View_fadingEdge:
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003394 if (targetSdkVersion >= ICE_CREAM_SANDWICH) {
Romain Guy1ef3fdb2011-09-09 15:30:30 -07003395 // Ignore the attribute starting with ICS
3396 break;
3397 }
3398 // With builds < ICS, fall through and apply fading edges
3399 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003400 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
3401 if (fadingEdge != FADING_EDGE_NONE) {
3402 viewFlagValues |= fadingEdge;
3403 viewFlagMasks |= FADING_EDGE_MASK;
3404 initializeFadingEdge(a);
3405 }
3406 break;
3407 case R.styleable.View_scrollbarStyle:
3408 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3409 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3410 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3411 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3412 }
3413 break;
3414 case R.styleable.View_isScrollContainer:
3415 setScrollContainer = true;
3416 if (a.getBoolean(attr, false)) {
3417 setScrollContainer(true);
3418 }
3419 break;
3420 case com.android.internal.R.styleable.View_keepScreenOn:
3421 if (a.getBoolean(attr, false)) {
3422 viewFlagValues |= KEEP_SCREEN_ON;
3423 viewFlagMasks |= KEEP_SCREEN_ON;
3424 }
3425 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003426 case R.styleable.View_filterTouchesWhenObscured:
3427 if (a.getBoolean(attr, false)) {
3428 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3429 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3430 }
3431 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003432 case R.styleable.View_nextFocusLeft:
3433 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3434 break;
3435 case R.styleable.View_nextFocusRight:
3436 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3437 break;
3438 case R.styleable.View_nextFocusUp:
3439 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3440 break;
3441 case R.styleable.View_nextFocusDown:
3442 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3443 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003444 case R.styleable.View_nextFocusForward:
3445 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3446 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003447 case R.styleable.View_minWidth:
3448 mMinWidth = a.getDimensionPixelSize(attr, 0);
3449 break;
3450 case R.styleable.View_minHeight:
3451 mMinHeight = a.getDimensionPixelSize(attr, 0);
3452 break;
Romain Guy9a817362009-05-01 10:57:14 -07003453 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003454 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003455 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003456 + "be used within a restricted context");
3457 }
3458
Romain Guy9a817362009-05-01 10:57:14 -07003459 final String handlerName = a.getString(attr);
3460 if (handlerName != null) {
3461 setOnClickListener(new OnClickListener() {
3462 private Method mHandler;
3463
3464 public void onClick(View v) {
3465 if (mHandler == null) {
3466 try {
3467 mHandler = getContext().getClass().getMethod(handlerName,
3468 View.class);
3469 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003470 int id = getId();
3471 String idText = id == NO_ID ? "" : " with id '"
3472 + getContext().getResources().getResourceEntryName(
3473 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003474 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003475 handlerName + "(View) in the activity "
3476 + getContext().getClass() + " for onClick handler"
3477 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003478 }
3479 }
3480
3481 try {
3482 mHandler.invoke(getContext(), View.this);
3483 } catch (IllegalAccessException e) {
3484 throw new IllegalStateException("Could not execute non "
3485 + "public method of the activity", e);
3486 } catch (InvocationTargetException e) {
3487 throw new IllegalStateException("Could not execute "
3488 + "method of the activity", e);
3489 }
3490 }
3491 });
3492 }
3493 break;
Adam Powell637d3372010-08-25 14:37:03 -07003494 case R.styleable.View_overScrollMode:
3495 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3496 break;
Adam Powell20232d02010-12-08 21:08:53 -08003497 case R.styleable.View_verticalScrollbarPosition:
3498 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3499 break;
Romain Guy171c5922011-01-06 10:04:23 -08003500 case R.styleable.View_layerType:
3501 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3502 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003503 case R.styleable.View_textDirection:
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003504 // Clear any text direction flag already set
Dianne Hackborn4702a852012-08-17 15:18:29 -07003505 mPrivateFlags2 &= ~PFLAG2_TEXT_DIRECTION_MASK;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003506 // Set the text direction flags depending on the value of the attribute
3507 final int textDirection = a.getInt(attr, -1);
3508 if (textDirection != -1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07003509 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_FLAGS[textDirection];
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003510 }
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003511 break;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003512 case R.styleable.View_textAlignment:
3513 // Clear any text alignment flag already set
Dianne Hackborn4702a852012-08-17 15:18:29 -07003514 mPrivateFlags2 &= ~PFLAG2_TEXT_ALIGNMENT_MASK;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003515 // Set the text alignment flag depending on the value of the attribute
3516 final int textAlignment = a.getInt(attr, TEXT_ALIGNMENT_DEFAULT);
Dianne Hackborn4702a852012-08-17 15:18:29 -07003517 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_FLAGS[textAlignment];
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003518 break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07003519 case R.styleable.View_importantForAccessibility:
3520 setImportantForAccessibility(a.getInt(attr,
3521 IMPORTANT_FOR_ACCESSIBILITY_DEFAULT));
Svetoslav Ganov86783472012-06-06 21:12:20 -07003522 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003523 }
3524 }
3525
Adam Powell637d3372010-08-25 14:37:03 -07003526 setOverScrollMode(overScrollMode);
3527
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003528 // Cache start/end user padding as we cannot fully resolve padding here (we dont have yet
3529 // the resolved layout direction). Those cached values will be used later during padding
3530 // resolution.
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003531 mUserPaddingStart = startPadding;
3532 mUserPaddingEnd = endPadding;
3533
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003534 if (background != null) {
3535 setBackground(background);
3536 }
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003538 if (padding >= 0) {
3539 leftPadding = padding;
3540 topPadding = padding;
3541 rightPadding = padding;
3542 bottomPadding = padding;
3543 }
3544
3545 // If the user specified the padding (either with android:padding or
3546 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3547 // use the default padding or the padding from the background drawable
3548 // (stored at this point in mPadding*)
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003549 internalSetPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003550 topPadding >= 0 ? topPadding : mPaddingTop,
3551 rightPadding >= 0 ? rightPadding : mPaddingRight,
3552 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3553
3554 if (viewFlagMasks != 0) {
3555 setFlags(viewFlagValues, viewFlagMasks);
3556 }
3557
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003558 if (initializeScrollbars) {
3559 initializeScrollbars(a);
3560 }
3561
3562 a.recycle();
3563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003564 // Needs to be called after mViewFlags is set
3565 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3566 recomputePadding();
3567 }
3568
3569 if (x != 0 || y != 0) {
3570 scrollTo(x, y);
3571 }
3572
Chet Haase73066682010-11-29 15:55:32 -08003573 if (transformSet) {
3574 setTranslationX(tx);
3575 setTranslationY(ty);
3576 setRotation(rotation);
3577 setRotationX(rotationX);
3578 setRotationY(rotationY);
3579 setScaleX(sx);
3580 setScaleY(sy);
3581 }
3582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003583 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3584 setScrollContainer(true);
3585 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003586
3587 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003588 }
3589
3590 /**
3591 * Non-public constructor for use in testing
3592 */
3593 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003594 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003595 }
3596
Dianne Hackborn4702a852012-08-17 15:18:29 -07003597 public String toString() {
3598 StringBuilder out = new StringBuilder(128);
3599 out.append(getClass().getName());
3600 out.append('{');
3601 out.append(Integer.toHexString(System.identityHashCode(this)));
3602 out.append(' ');
3603 switch (mViewFlags&VISIBILITY_MASK) {
3604 case VISIBLE: out.append('V'); break;
3605 case INVISIBLE: out.append('I'); break;
3606 case GONE: out.append('G'); break;
3607 default: out.append('.'); break;
3608 }
3609 out.append((mViewFlags&FOCUSABLE_MASK) == FOCUSABLE ? 'F' : '.');
3610 out.append((mViewFlags&ENABLED_MASK) == ENABLED ? 'E' : '.');
3611 out.append((mViewFlags&DRAW_MASK) == WILL_NOT_DRAW ? '.' : 'D');
3612 out.append((mViewFlags&SCROLLBARS_HORIZONTAL) != 0 ? 'H' : '.');
3613 out.append((mViewFlags&SCROLLBARS_VERTICAL) != 0 ? 'V' : '.');
3614 out.append((mViewFlags&CLICKABLE) != 0 ? 'C' : '.');
3615 out.append((mViewFlags&LONG_CLICKABLE) != 0 ? 'L' : '.');
3616 out.append(' ');
3617 out.append((mPrivateFlags&PFLAG_IS_ROOT_NAMESPACE) != 0 ? 'R' : '.');
3618 out.append((mPrivateFlags&PFLAG_FOCUSED) != 0 ? 'F' : '.');
3619 out.append((mPrivateFlags&PFLAG_SELECTED) != 0 ? 'S' : '.');
3620 if ((mPrivateFlags&PFLAG_PREPRESSED) != 0) {
3621 out.append('p');
3622 } else {
3623 out.append((mPrivateFlags&PFLAG_PRESSED) != 0 ? 'P' : '.');
3624 }
3625 out.append((mPrivateFlags&PFLAG_HOVERED) != 0 ? 'H' : '.');
3626 out.append((mPrivateFlags&PFLAG_ACTIVATED) != 0 ? 'A' : '.');
3627 out.append((mPrivateFlags&PFLAG_INVALIDATED) != 0 ? 'I' : '.');
3628 out.append((mPrivateFlags&PFLAG_DIRTY_MASK) != 0 ? 'D' : '.');
3629 out.append(' ');
3630 out.append(mLeft);
3631 out.append(',');
3632 out.append(mTop);
3633 out.append('-');
3634 out.append(mRight);
3635 out.append(',');
3636 out.append(mBottom);
3637 final int id = getId();
3638 if (id != NO_ID) {
3639 out.append(" #");
3640 out.append(Integer.toHexString(id));
3641 final Resources r = mResources;
3642 if (id != 0 && r != null) {
3643 try {
3644 String pkgname;
3645 switch (id&0xff000000) {
3646 case 0x7f000000:
3647 pkgname="app";
3648 break;
3649 case 0x01000000:
3650 pkgname="android";
3651 break;
3652 default:
3653 pkgname = r.getResourcePackageName(id);
3654 break;
3655 }
3656 String typename = r.getResourceTypeName(id);
3657 String entryname = r.getResourceEntryName(id);
3658 out.append(" ");
3659 out.append(pkgname);
3660 out.append(":");
3661 out.append(typename);
3662 out.append("/");
3663 out.append(entryname);
3664 } catch (Resources.NotFoundException e) {
3665 }
3666 }
3667 }
3668 out.append("}");
3669 return out.toString();
3670 }
3671
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003672 /**
3673 * <p>
3674 * Initializes the fading edges from a given set of styled attributes. This
3675 * method should be called by subclasses that need fading edges and when an
3676 * instance of these subclasses is created programmatically rather than
3677 * being inflated from XML. This method is automatically called when the XML
3678 * is inflated.
3679 * </p>
3680 *
3681 * @param a the styled attributes set to initialize the fading edges from
3682 */
3683 protected void initializeFadingEdge(TypedArray a) {
3684 initScrollCache();
3685
3686 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3687 R.styleable.View_fadingEdgeLength,
3688 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3689 }
3690
3691 /**
3692 * Returns the size of the vertical faded edges used to indicate that more
3693 * content in this view is visible.
3694 *
3695 * @return The size in pixels of the vertical faded edge or 0 if vertical
3696 * faded edges are not enabled for this view.
3697 * @attr ref android.R.styleable#View_fadingEdgeLength
3698 */
3699 public int getVerticalFadingEdgeLength() {
3700 if (isVerticalFadingEdgeEnabled()) {
3701 ScrollabilityCache cache = mScrollCache;
3702 if (cache != null) {
3703 return cache.fadingEdgeLength;
3704 }
3705 }
3706 return 0;
3707 }
3708
3709 /**
3710 * Set the size of the faded edge used to indicate that more content in this
3711 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003712 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3713 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3714 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003715 *
3716 * @param length The size in pixels of the faded edge used to indicate that more
3717 * content in this view is visible.
3718 */
3719 public void setFadingEdgeLength(int length) {
3720 initScrollCache();
3721 mScrollCache.fadingEdgeLength = length;
3722 }
3723
3724 /**
3725 * Returns the size of the horizontal faded edges used to indicate that more
3726 * content in this view is visible.
3727 *
3728 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3729 * faded edges are not enabled for this view.
3730 * @attr ref android.R.styleable#View_fadingEdgeLength
3731 */
3732 public int getHorizontalFadingEdgeLength() {
3733 if (isHorizontalFadingEdgeEnabled()) {
3734 ScrollabilityCache cache = mScrollCache;
3735 if (cache != null) {
3736 return cache.fadingEdgeLength;
3737 }
3738 }
3739 return 0;
3740 }
3741
3742 /**
3743 * Returns the width of the vertical scrollbar.
3744 *
3745 * @return The width in pixels of the vertical scrollbar or 0 if there
3746 * is no vertical scrollbar.
3747 */
3748 public int getVerticalScrollbarWidth() {
3749 ScrollabilityCache cache = mScrollCache;
3750 if (cache != null) {
3751 ScrollBarDrawable scrollBar = cache.scrollBar;
3752 if (scrollBar != null) {
3753 int size = scrollBar.getSize(true);
3754 if (size <= 0) {
3755 size = cache.scrollBarSize;
3756 }
3757 return size;
3758 }
3759 return 0;
3760 }
3761 return 0;
3762 }
3763
3764 /**
3765 * Returns the height of the horizontal scrollbar.
3766 *
3767 * @return The height in pixels of the horizontal scrollbar or 0 if
3768 * there is no horizontal scrollbar.
3769 */
3770 protected int getHorizontalScrollbarHeight() {
3771 ScrollabilityCache cache = mScrollCache;
3772 if (cache != null) {
3773 ScrollBarDrawable scrollBar = cache.scrollBar;
3774 if (scrollBar != null) {
3775 int size = scrollBar.getSize(false);
3776 if (size <= 0) {
3777 size = cache.scrollBarSize;
3778 }
3779 return size;
3780 }
3781 return 0;
3782 }
3783 return 0;
3784 }
3785
3786 /**
3787 * <p>
3788 * Initializes the scrollbars from a given set of styled attributes. This
3789 * method should be called by subclasses that need scrollbars and when an
3790 * instance of these subclasses is created programmatically rather than
3791 * being inflated from XML. This method is automatically called when the XML
3792 * is inflated.
3793 * </p>
3794 *
3795 * @param a the styled attributes set to initialize the scrollbars from
3796 */
3797 protected void initializeScrollbars(TypedArray a) {
3798 initScrollCache();
3799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003800 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003801
Mike Cleronf116bf82009-09-27 19:14:12 -07003802 if (scrollabilityCache.scrollBar == null) {
3803 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3804 }
Joe Malin32736f02011-01-19 16:14:20 -08003805
Romain Guy8bda2482010-03-02 11:42:11 -08003806 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003807
Mike Cleronf116bf82009-09-27 19:14:12 -07003808 if (!fadeScrollbars) {
3809 scrollabilityCache.state = ScrollabilityCache.ON;
3810 }
3811 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003812
3813
Mike Cleronf116bf82009-09-27 19:14:12 -07003814 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3815 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3816 .getScrollBarFadeDuration());
3817 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3818 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3819 ViewConfiguration.getScrollDefaultDelay());
3820
Joe Malin32736f02011-01-19 16:14:20 -08003821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003822 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3823 com.android.internal.R.styleable.View_scrollbarSize,
3824 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3825
3826 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3827 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3828
3829 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3830 if (thumb != null) {
3831 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3832 }
3833
3834 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3835 false);
3836 if (alwaysDraw) {
3837 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3838 }
3839
3840 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3841 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3842
3843 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3844 if (thumb != null) {
3845 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3846 }
3847
3848 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3849 false);
3850 if (alwaysDraw) {
3851 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3852 }
3853
Fabrice Di Megliob03b4342012-06-04 12:55:30 -07003854 // Apply layout direction to the new Drawables if needed
3855 final int layoutDirection = getResolvedLayoutDirection();
3856 if (track != null) {
3857 track.setLayoutDirection(layoutDirection);
3858 }
3859 if (thumb != null) {
3860 thumb.setLayoutDirection(layoutDirection);
3861 }
3862
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003863 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003864 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003865 }
3866
3867 /**
3868 * <p>
3869 * Initalizes the scrollability cache if necessary.
3870 * </p>
3871 */
3872 private void initScrollCache() {
3873 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003874 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003875 }
3876 }
3877
Philip Milne6c8ea062012-04-03 17:38:43 -07003878 private ScrollabilityCache getScrollCache() {
3879 initScrollCache();
3880 return mScrollCache;
3881 }
3882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003883 /**
Adam Powell20232d02010-12-08 21:08:53 -08003884 * Set the position of the vertical scroll bar. Should be one of
3885 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3886 * {@link #SCROLLBAR_POSITION_RIGHT}.
3887 *
3888 * @param position Where the vertical scroll bar should be positioned.
3889 */
3890 public void setVerticalScrollbarPosition(int position) {
3891 if (mVerticalScrollbarPosition != position) {
3892 mVerticalScrollbarPosition = position;
3893 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003894 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003895 }
3896 }
3897
3898 /**
3899 * @return The position where the vertical scroll bar will show, if applicable.
3900 * @see #setVerticalScrollbarPosition(int)
3901 */
3902 public int getVerticalScrollbarPosition() {
3903 return mVerticalScrollbarPosition;
3904 }
3905
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003906 ListenerInfo getListenerInfo() {
3907 if (mListenerInfo != null) {
3908 return mListenerInfo;
3909 }
3910 mListenerInfo = new ListenerInfo();
3911 return mListenerInfo;
3912 }
3913
Adam Powell20232d02010-12-08 21:08:53 -08003914 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003915 * Register a callback to be invoked when focus of this view changed.
3916 *
3917 * @param l The callback that will run.
3918 */
3919 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003920 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003921 }
3922
3923 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003924 * Add a listener that will be called when the bounds of the view change due to
3925 * layout processing.
3926 *
3927 * @param listener The listener that will be called when layout bounds change.
3928 */
3929 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003930 ListenerInfo li = getListenerInfo();
3931 if (li.mOnLayoutChangeListeners == null) {
3932 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003933 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003934 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3935 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003936 }
Chet Haase21cd1382010-09-01 17:42:29 -07003937 }
3938
3939 /**
3940 * Remove a listener for layout changes.
3941 *
3942 * @param listener The listener for layout bounds change.
3943 */
3944 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003945 ListenerInfo li = mListenerInfo;
3946 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003947 return;
3948 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003949 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003950 }
3951
3952 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003953 * Add a listener for attach state changes.
3954 *
3955 * This listener will be called whenever this view is attached or detached
3956 * from a window. Remove the listener using
3957 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3958 *
3959 * @param listener Listener to attach
3960 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3961 */
3962 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003963 ListenerInfo li = getListenerInfo();
3964 if (li.mOnAttachStateChangeListeners == null) {
3965 li.mOnAttachStateChangeListeners
3966 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003967 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003968 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003969 }
3970
3971 /**
3972 * Remove a listener for attach state changes. The listener will receive no further
3973 * notification of window attach/detach events.
3974 *
3975 * @param listener Listener to remove
3976 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3977 */
3978 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003979 ListenerInfo li = mListenerInfo;
3980 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003981 return;
3982 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003983 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003984 }
3985
3986 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003987 * Returns the focus-change callback registered for this view.
3988 *
3989 * @return The callback, or null if one is not registered.
3990 */
3991 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003992 ListenerInfo li = mListenerInfo;
3993 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003994 }
3995
3996 /**
3997 * Register a callback to be invoked when this view is clicked. If this view is not
3998 * clickable, it becomes clickable.
3999 *
4000 * @param l The callback that will run
4001 *
4002 * @see #setClickable(boolean)
4003 */
4004 public void setOnClickListener(OnClickListener l) {
4005 if (!isClickable()) {
4006 setClickable(true);
4007 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004008 getListenerInfo().mOnClickListener = l;
4009 }
4010
4011 /**
4012 * Return whether this view has an attached OnClickListener. Returns
4013 * true if there is a listener, false if there is none.
4014 */
4015 public boolean hasOnClickListeners() {
4016 ListenerInfo li = mListenerInfo;
4017 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004018 }
4019
4020 /**
4021 * Register a callback to be invoked when this view is clicked and held. If this view is not
4022 * long clickable, it becomes long clickable.
4023 *
4024 * @param l The callback that will run
4025 *
4026 * @see #setLongClickable(boolean)
4027 */
4028 public void setOnLongClickListener(OnLongClickListener l) {
4029 if (!isLongClickable()) {
4030 setLongClickable(true);
4031 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004032 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004033 }
4034
4035 /**
4036 * Register a callback to be invoked when the context menu for this view is
4037 * being built. If this view is not long clickable, it becomes long clickable.
4038 *
4039 * @param l The callback that will run
4040 *
4041 */
4042 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
4043 if (!isLongClickable()) {
4044 setLongClickable(true);
4045 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004046 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004047 }
4048
4049 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004050 * Call this view's OnClickListener, if it is defined. Performs all normal
4051 * actions associated with clicking: reporting accessibility event, playing
4052 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004053 *
4054 * @return True there was an assigned OnClickListener that was called, false
4055 * otherwise is returned.
4056 */
4057 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07004058 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
4059
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004060 ListenerInfo li = mListenerInfo;
4061 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004062 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004063 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004064 return true;
4065 }
4066
4067 return false;
4068 }
4069
4070 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004071 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
4072 * this only calls the listener, and does not do any associated clicking
4073 * actions like reporting an accessibility event.
4074 *
4075 * @return True there was an assigned OnClickListener that was called, false
4076 * otherwise is returned.
4077 */
4078 public boolean callOnClick() {
4079 ListenerInfo li = mListenerInfo;
4080 if (li != null && li.mOnClickListener != null) {
4081 li.mOnClickListener.onClick(this);
4082 return true;
4083 }
4084 return false;
4085 }
4086
4087 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004088 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
4089 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004090 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004091 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004092 */
4093 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07004094 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
4095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004096 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004097 ListenerInfo li = mListenerInfo;
4098 if (li != null && li.mOnLongClickListener != null) {
4099 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004100 }
4101 if (!handled) {
4102 handled = showContextMenu();
4103 }
4104 if (handled) {
4105 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
4106 }
4107 return handled;
4108 }
4109
4110 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004111 * Performs button-related actions during a touch down event.
4112 *
4113 * @param event The event.
4114 * @return True if the down was consumed.
4115 *
4116 * @hide
4117 */
4118 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
4119 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
4120 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
4121 return true;
4122 }
4123 }
4124 return false;
4125 }
4126
4127 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004128 * Bring up the context menu for this view.
4129 *
4130 * @return Whether a context menu was displayed.
4131 */
4132 public boolean showContextMenu() {
4133 return getParent().showContextMenuForChild(this);
4134 }
4135
4136 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004137 * Bring up the context menu for this view, referring to the item under the specified point.
4138 *
4139 * @param x The referenced x coordinate.
4140 * @param y The referenced y coordinate.
4141 * @param metaState The keyboard modifiers that were pressed.
4142 * @return Whether a context menu was displayed.
4143 *
4144 * @hide
4145 */
4146 public boolean showContextMenu(float x, float y, int metaState) {
4147 return showContextMenu();
4148 }
4149
4150 /**
Adam Powell6e346362010-07-23 10:18:23 -07004151 * Start an action mode.
4152 *
4153 * @param callback Callback that will control the lifecycle of the action mode
4154 * @return The new action mode if it is started, null otherwise
4155 *
4156 * @see ActionMode
4157 */
4158 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08004159 ViewParent parent = getParent();
4160 if (parent == null) return null;
4161 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07004162 }
4163
4164 /**
Jean Chalard405bc512012-05-29 19:12:34 +09004165 * Register a callback to be invoked when a hardware key is pressed in this view.
4166 * Key presses in software input methods will generally not trigger the methods of
4167 * this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004168 * @param l the key listener to attach to this view
4169 */
4170 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004171 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004172 }
4173
4174 /**
4175 * Register a callback to be invoked when a touch event is sent to this view.
4176 * @param l the touch listener to attach to this view
4177 */
4178 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004179 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004180 }
4181
4182 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004183 * Register a callback to be invoked when a generic motion event is sent to this view.
4184 * @param l the generic motion listener to attach to this view
4185 */
4186 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004187 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08004188 }
4189
4190 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07004191 * Register a callback to be invoked when a hover event is sent to this view.
4192 * @param l the hover listener to attach to this view
4193 */
4194 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004195 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07004196 }
4197
4198 /**
Joe Malin32736f02011-01-19 16:14:20 -08004199 * Register a drag event listener callback object for this View. The parameter is
4200 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
4201 * View, the system calls the
4202 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
4203 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07004204 */
4205 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004206 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07004207 }
4208
4209 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07004210 * Give this view focus. This will cause
4211 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004212 *
4213 * Note: this does not check whether this {@link View} should get focus, it just
4214 * gives it focus no matter what. It should only be called internally by framework
4215 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
4216 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004217 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
4218 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004219 * focus moved when requestFocus() is called. It may not always
4220 * apply, in which case use the default View.FOCUS_DOWN.
4221 * @param previouslyFocusedRect The rectangle of the view that had focus
4222 * prior in this View's coordinate system.
4223 */
4224 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
4225 if (DBG) {
4226 System.out.println(this + " requestFocus()");
4227 }
4228
Dianne Hackborn4702a852012-08-17 15:18:29 -07004229 if ((mPrivateFlags & PFLAG_FOCUSED) == 0) {
4230 mPrivateFlags |= PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004231
4232 if (mParent != null) {
4233 mParent.requestChildFocus(this, this);
4234 }
4235
4236 onFocusChanged(true, direction, previouslyFocusedRect);
4237 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004238
4239 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4240 notifyAccessibilityStateChanged();
4241 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004242 }
4243 }
4244
4245 /**
4246 * Request that a rectangle of this view be visible on the screen,
4247 * scrolling if necessary just enough.
4248 *
4249 * <p>A View should call this if it maintains some notion of which part
4250 * of its content is interesting. For example, a text editing view
4251 * should call this when its cursor moves.
4252 *
4253 * @param rectangle The rectangle.
4254 * @return Whether any parent scrolled.
4255 */
4256 public boolean requestRectangleOnScreen(Rect rectangle) {
4257 return requestRectangleOnScreen(rectangle, false);
4258 }
4259
4260 /**
4261 * Request that a rectangle of this view be visible on the screen,
4262 * scrolling if necessary just enough.
4263 *
4264 * <p>A View should call this if it maintains some notion of which part
4265 * of its content is interesting. For example, a text editing view
4266 * should call this when its cursor moves.
4267 *
4268 * <p>When <code>immediate</code> is set to true, scrolling will not be
4269 * animated.
4270 *
4271 * @param rectangle The rectangle.
4272 * @param immediate True to forbid animated scrolling, false otherwise
4273 * @return Whether any parent scrolled.
4274 */
4275 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
4276 View child = this;
4277 ViewParent parent = mParent;
4278 boolean scrolled = false;
4279 while (parent != null) {
4280 scrolled |= parent.requestChildRectangleOnScreen(child,
4281 rectangle, immediate);
4282
4283 // offset rect so next call has the rectangle in the
4284 // coordinate system of its direct child.
4285 rectangle.offset(child.getLeft(), child.getTop());
4286 rectangle.offset(-child.getScrollX(), -child.getScrollY());
4287
4288 if (!(parent instanceof View)) {
4289 break;
4290 }
Romain Guy8506ab42009-06-11 17:35:47 -07004291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004292 child = (View) parent;
4293 parent = child.getParent();
4294 }
4295 return scrolled;
4296 }
4297
4298 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004299 * Called when this view wants to give up focus. If focus is cleared
4300 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
4301 * <p>
4302 * <strong>Note:</strong> When a View clears focus the framework is trying
4303 * to give focus to the first focusable View from the top. Hence, if this
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004304 * View is the first from the top that can take focus, then all callbacks
4305 * related to clearing focus will be invoked after wich the framework will
4306 * give focus to this view.
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004307 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004308 */
4309 public void clearFocus() {
4310 if (DBG) {
4311 System.out.println(this + " clearFocus()");
4312 }
4313
Dianne Hackborn4702a852012-08-17 15:18:29 -07004314 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
4315 mPrivateFlags &= ~PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004316
4317 if (mParent != null) {
4318 mParent.clearChildFocus(this);
4319 }
4320
4321 onFocusChanged(false, 0, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004323 refreshDrawableState();
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004324
4325 ensureInputFocusOnFirstFocusable();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004326
4327 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4328 notifyAccessibilityStateChanged();
4329 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004330 }
4331 }
4332
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004333 void ensureInputFocusOnFirstFocusable() {
4334 View root = getRootView();
4335 if (root != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004336 root.requestFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004337 }
4338 }
4339
4340 /**
4341 * Called internally by the view system when a new view is getting focus.
4342 * This is what clears the old focus.
4343 */
4344 void unFocus() {
4345 if (DBG) {
4346 System.out.println(this + " unFocus()");
4347 }
4348
Dianne Hackborn4702a852012-08-17 15:18:29 -07004349 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
4350 mPrivateFlags &= ~PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004351
4352 onFocusChanged(false, 0, null);
4353 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004354
4355 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4356 notifyAccessibilityStateChanged();
4357 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004358 }
4359 }
4360
4361 /**
4362 * Returns true if this view has focus iteself, or is the ancestor of the
4363 * view that has focus.
4364 *
4365 * @return True if this view has or contains focus, false otherwise.
4366 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004367 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004368 public boolean hasFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07004369 return (mPrivateFlags & PFLAG_FOCUSED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004370 }
4371
4372 /**
4373 * Returns true if this view is focusable or if it contains a reachable View
4374 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
4375 * is a View whose parents do not block descendants focus.
4376 *
4377 * Only {@link #VISIBLE} views are considered focusable.
4378 *
4379 * @return True if the view is focusable or if the view contains a focusable
4380 * View, false otherwise.
4381 *
4382 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
4383 */
4384 public boolean hasFocusable() {
4385 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4386 }
4387
4388 /**
4389 * Called by the view system when the focus state of this view changes.
4390 * When the focus change event is caused by directional navigation, direction
4391 * and previouslyFocusedRect provide insight into where the focus is coming from.
4392 * When overriding, be sure to call up through to the super class so that
4393 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004394 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004395 * @param gainFocus True if the View has focus; false otherwise.
4396 * @param direction The direction focus has moved when requestFocus()
4397 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004398 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4399 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4400 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004401 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4402 * system, of the previously focused view. If applicable, this will be
4403 * passed in as finer grained information about where the focus is coming
4404 * from (in addition to direction). Will be <code>null</code> otherwise.
4405 */
4406 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004407 if (gainFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004408 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4409 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004410 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004411 }
4412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004413 InputMethodManager imm = InputMethodManager.peekInstance();
4414 if (!gainFocus) {
4415 if (isPressed()) {
4416 setPressed(false);
4417 }
4418 if (imm != null && mAttachInfo != null
4419 && mAttachInfo.mHasWindowFocus) {
4420 imm.focusOut(this);
4421 }
Romain Guya2431d02009-04-30 16:30:00 -07004422 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004423 } else if (imm != null && mAttachInfo != null
4424 && mAttachInfo.mHasWindowFocus) {
4425 imm.focusIn(this);
4426 }
Romain Guy8506ab42009-06-11 17:35:47 -07004427
Romain Guy0fd89bf2011-01-26 15:41:30 -08004428 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004429 ListenerInfo li = mListenerInfo;
4430 if (li != null && li.mOnFocusChangeListener != null) {
4431 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004432 }
Joe Malin32736f02011-01-19 16:14:20 -08004433
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004434 if (mAttachInfo != null) {
4435 mAttachInfo.mKeyDispatchState.reset(this);
4436 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004437 }
4438
4439 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004440 * Sends an accessibility event of the given type. If accessiiblity is
4441 * not enabled this method has no effect. The default implementation calls
4442 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4443 * to populate information about the event source (this View), then calls
4444 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4445 * populate the text content of the event source including its descendants,
4446 * and last calls
4447 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4448 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004449 * <p>
4450 * If an {@link AccessibilityDelegate} has been specified via calling
4451 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4452 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4453 * responsible for handling this call.
4454 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004455 *
Scott Mainb303d832011-10-12 16:45:18 -07004456 * @param eventType The type of the event to send, as defined by several types from
4457 * {@link android.view.accessibility.AccessibilityEvent}, such as
4458 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4459 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004460 *
4461 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4462 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4463 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004464 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004465 */
4466 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004467 if (mAccessibilityDelegate != null) {
4468 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4469 } else {
4470 sendAccessibilityEventInternal(eventType);
4471 }
4472 }
4473
4474 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004475 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4476 * {@link AccessibilityEvent} to make an announcement which is related to some
4477 * sort of a context change for which none of the events representing UI transitions
4478 * is a good fit. For example, announcing a new page in a book. If accessibility
4479 * is not enabled this method does nothing.
4480 *
4481 * @param text The announcement text.
4482 */
4483 public void announceForAccessibility(CharSequence text) {
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004484 if (AccessibilityManager.getInstance(mContext).isEnabled() && mParent != null) {
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004485 AccessibilityEvent event = AccessibilityEvent.obtain(
4486 AccessibilityEvent.TYPE_ANNOUNCEMENT);
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004487 onInitializeAccessibilityEvent(event);
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004488 event.getText().add(text);
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004489 event.setContentDescription(null);
4490 mParent.requestSendAccessibilityEvent(this, event);
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004491 }
4492 }
4493
4494 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004495 * @see #sendAccessibilityEvent(int)
4496 *
4497 * Note: Called from the default {@link AccessibilityDelegate}.
4498 */
4499 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004500 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4501 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4502 }
4503 }
4504
4505 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004506 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4507 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004508 * perform a check whether accessibility is enabled.
4509 * <p>
4510 * If an {@link AccessibilityDelegate} has been specified via calling
4511 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4512 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4513 * is responsible for handling this call.
4514 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004515 *
4516 * @param event The event to send.
4517 *
4518 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004519 */
4520 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004521 if (mAccessibilityDelegate != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004522 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004523 } else {
4524 sendAccessibilityEventUncheckedInternal(event);
4525 }
4526 }
4527
4528 /**
4529 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4530 *
4531 * Note: Called from the default {@link AccessibilityDelegate}.
4532 */
4533 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004534 if (!isShown()) {
4535 return;
4536 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004537 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004538 // Only a subset of accessibility events populates text content.
4539 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4540 dispatchPopulateAccessibilityEvent(event);
4541 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004542 // In the beginning we called #isShown(), so we know that getParent() is not null.
4543 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004544 }
4545
4546 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004547 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4548 * to its children for adding their text content to the event. Note that the
4549 * event text is populated in a separate dispatch path since we add to the
4550 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004551 * A typical implementation will call
4552 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4553 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4554 * on each child. Override this method if custom population of the event text
4555 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004556 * <p>
4557 * If an {@link AccessibilityDelegate} has been specified via calling
4558 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4559 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4560 * is responsible for handling this call.
4561 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004562 * <p>
4563 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4564 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4565 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004566 *
4567 * @param event The event.
4568 *
4569 * @return True if the event population was completed.
4570 */
4571 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004572 if (mAccessibilityDelegate != null) {
4573 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4574 } else {
4575 return dispatchPopulateAccessibilityEventInternal(event);
4576 }
4577 }
4578
4579 /**
4580 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4581 *
4582 * Note: Called from the default {@link AccessibilityDelegate}.
4583 */
4584 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004585 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004586 return false;
4587 }
4588
4589 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004590 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004591 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004592 * text content. While this method is free to modify event
4593 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004594 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4595 * <p>
4596 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004597 * to the text added by the super implementation:
4598 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004599 * super.onPopulateAccessibilityEvent(event);
4600 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4601 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4602 * mCurrentDate.getTimeInMillis(), flags);
4603 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004604 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004605 * <p>
4606 * If an {@link AccessibilityDelegate} has been specified via calling
4607 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4608 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4609 * is responsible for handling this call.
4610 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004611 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4612 * information to the event, in case the default implementation has basic information to add.
4613 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004614 *
4615 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004616 *
4617 * @see #sendAccessibilityEvent(int)
4618 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004619 */
4620 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004621 if (mAccessibilityDelegate != null) {
4622 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4623 } else {
4624 onPopulateAccessibilityEventInternal(event);
4625 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004626 }
4627
4628 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004629 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4630 *
4631 * Note: Called from the default {@link AccessibilityDelegate}.
4632 */
4633 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4634
4635 }
4636
4637 /**
4638 * Initializes an {@link AccessibilityEvent} with information about
4639 * this View which is the event source. In other words, the source of
4640 * an accessibility event is the view whose state change triggered firing
4641 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004642 * <p>
4643 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004644 * to properties set by the super implementation:
4645 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4646 * super.onInitializeAccessibilityEvent(event);
4647 * event.setPassword(true);
4648 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004649 * <p>
4650 * If an {@link AccessibilityDelegate} has been specified via calling
4651 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4652 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4653 * is responsible for handling this call.
4654 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004655 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4656 * information to the event, in case the default implementation has basic information to add.
4657 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004658 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004659 *
4660 * @see #sendAccessibilityEvent(int)
4661 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4662 */
4663 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004664 if (mAccessibilityDelegate != null) {
4665 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4666 } else {
4667 onInitializeAccessibilityEventInternal(event);
4668 }
4669 }
4670
4671 /**
4672 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4673 *
4674 * Note: Called from the default {@link AccessibilityDelegate}.
4675 */
4676 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004677 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004678 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004679 event.setPackageName(getContext().getPackageName());
4680 event.setEnabled(isEnabled());
4681 event.setContentDescription(mContentDescription);
4682
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004683 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004684 ArrayList<View> focusablesTempList = mAttachInfo.mTempArrayList;
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004685 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4686 FOCUSABLES_ALL);
4687 event.setItemCount(focusablesTempList.size());
4688 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4689 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004690 }
4691 }
4692
4693 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004694 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4695 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4696 * This method is responsible for obtaining an accessibility node info from a
4697 * pool of reusable instances and calling
4698 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4699 * initialize the former.
4700 * <p>
4701 * Note: The client is responsible for recycling the obtained instance by calling
4702 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4703 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004704 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004705 * @return A populated {@link AccessibilityNodeInfo}.
4706 *
4707 * @see AccessibilityNodeInfo
4708 */
4709 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004710 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4711 if (provider != null) {
4712 return provider.createAccessibilityNodeInfo(View.NO_ID);
4713 } else {
4714 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4715 onInitializeAccessibilityNodeInfo(info);
4716 return info;
4717 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004718 }
4719
4720 /**
4721 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4722 * The base implementation sets:
4723 * <ul>
4724 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004725 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4726 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004727 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4728 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4729 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4730 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4731 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4732 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4733 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4734 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4735 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4736 * </ul>
4737 * <p>
4738 * Subclasses should override this method, call the super implementation,
4739 * and set additional attributes.
4740 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004741 * <p>
4742 * If an {@link AccessibilityDelegate} has been specified via calling
4743 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4744 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4745 * is responsible for handling this call.
4746 * </p>
4747 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004748 * @param info The instance to initialize.
4749 */
4750 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004751 if (mAccessibilityDelegate != null) {
4752 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4753 } else {
4754 onInitializeAccessibilityNodeInfoInternal(info);
4755 }
4756 }
4757
4758 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004759 * Gets the location of this view in screen coordintates.
4760 *
4761 * @param outRect The output location
4762 */
4763 private void getBoundsOnScreen(Rect outRect) {
4764 if (mAttachInfo == null) {
4765 return;
4766 }
4767
4768 RectF position = mAttachInfo.mTmpTransformRect;
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004769 position.set(0, 0, mRight - mLeft, mBottom - mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004770
4771 if (!hasIdentityMatrix()) {
4772 getMatrix().mapRect(position);
4773 }
4774
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004775 position.offset(mLeft, mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004776
4777 ViewParent parent = mParent;
4778 while (parent instanceof View) {
4779 View parentView = (View) parent;
4780
4781 position.offset(-parentView.mScrollX, -parentView.mScrollY);
4782
4783 if (!parentView.hasIdentityMatrix()) {
4784 parentView.getMatrix().mapRect(position);
4785 }
4786
4787 position.offset(parentView.mLeft, parentView.mTop);
4788
4789 parent = parentView.mParent;
4790 }
4791
4792 if (parent instanceof ViewRootImpl) {
4793 ViewRootImpl viewRootImpl = (ViewRootImpl) parent;
4794 position.offset(0, -viewRootImpl.mCurScrollY);
4795 }
4796
4797 position.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
4798
4799 outRect.set((int) (position.left + 0.5f), (int) (position.top + 0.5f),
4800 (int) (position.right + 0.5f), (int) (position.bottom + 0.5f));
4801 }
4802
4803 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004804 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4805 *
4806 * Note: Called from the default {@link AccessibilityDelegate}.
4807 */
4808 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004809 Rect bounds = mAttachInfo.mTmpInvalRect;
Svetoslav Ganov983119a2012-07-03 21:04:10 -07004810
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004811 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004812 info.setBoundsInParent(bounds);
4813
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004814 getBoundsOnScreen(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004815 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004816
Svetoslav Ganovc406be92012-05-11 16:12:32 -07004817 ViewParent parent = getParentForAccessibility();
4818 if (parent instanceof View) {
4819 info.setParent((View) parent);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004820 }
4821
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004822 info.setVisibleToUser(isVisibleToUser());
4823
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004824 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004825 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004826 info.setContentDescription(getContentDescription());
4827
4828 info.setEnabled(isEnabled());
4829 info.setClickable(isClickable());
4830 info.setFocusable(isFocusable());
4831 info.setFocused(isFocused());
Svetoslav Ganov42138042012-03-20 11:51:39 -07004832 info.setAccessibilityFocused(isAccessibilityFocused());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004833 info.setSelected(isSelected());
4834 info.setLongClickable(isLongClickable());
4835
4836 // TODO: These make sense only if we are in an AdapterView but all
4837 // views can be selected. Maybe from accessiiblity perspective
4838 // we should report as selectable view in an AdapterView.
4839 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4840 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4841
4842 if (isFocusable()) {
4843 if (isFocused()) {
4844 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4845 } else {
4846 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4847 }
4848 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004849
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07004850 if (!isAccessibilityFocused()) {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07004851 info.addAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07004852 } else {
4853 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
4854 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004855
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07004856 if (isClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004857 info.addAction(AccessibilityNodeInfo.ACTION_CLICK);
4858 }
4859
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07004860 if (isLongClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004861 info.addAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
4862 }
4863
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004864 if (mContentDescription != null && mContentDescription.length() > 0) {
Svetoslav Ganov2b435aa2012-05-04 17:16:37 -07004865 info.addAction(AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY);
4866 info.addAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY);
4867 info.setMovementGranularities(AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004868 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD
4869 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH);
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004870 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004871 }
4872
4873 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004874 * Computes whether this view is visible to the user. Such a view is
4875 * attached, visible, all its predecessors are visible, it is not clipped
4876 * entirely by its predecessors, and has an alpha greater than zero.
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004877 *
4878 * @return Whether the view is visible on the screen.
Guang Zhu0d607fb2012-05-11 19:34:56 -07004879 *
4880 * @hide
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004881 */
Guang Zhu0d607fb2012-05-11 19:34:56 -07004882 protected boolean isVisibleToUser() {
4883 return isVisibleToUser(null);
4884 }
4885
4886 /**
Romain Guyf0af1d52012-07-11 18:31:21 -07004887 * Computes whether the given portion of this view is visible to the user.
4888 * Such a view is attached, visible, all its predecessors are visible,
4889 * has an alpha greater than zero, and the specified portion is not
4890 * clipped entirely by its predecessors.
Guang Zhu0d607fb2012-05-11 19:34:56 -07004891 *
4892 * @param boundInView the portion of the view to test; coordinates should be relative; may be
4893 * <code>null</code>, and the entire view will be tested in this case.
4894 * When <code>true</code> is returned by the function, the actual visible
4895 * region will be stored in this parameter; that is, if boundInView is fully
4896 * contained within the view, no modification will be made, otherwise regions
4897 * outside of the visible area of the view will be clipped.
4898 *
4899 * @return Whether the specified portion of the view is visible on the screen.
4900 *
4901 * @hide
4902 */
4903 protected boolean isVisibleToUser(Rect boundInView) {
Romain Guyf0af1d52012-07-11 18:31:21 -07004904 if (mAttachInfo != null) {
4905 Rect visibleRect = mAttachInfo.mTmpInvalRect;
4906 Point offset = mAttachInfo.mPoint;
4907 // The first two checks are made also made by isShown() which
4908 // however traverses the tree up to the parent to catch that.
4909 // Therefore, we do some fail fast check to minimize the up
4910 // tree traversal.
4911 boolean isVisible = mAttachInfo.mWindowVisibility == View.VISIBLE
4912 && getAlpha() > 0
4913 && isShown()
4914 && getGlobalVisibleRect(visibleRect, offset);
Guang Zhu0d607fb2012-05-11 19:34:56 -07004915 if (isVisible && boundInView != null) {
4916 visibleRect.offset(-offset.x, -offset.y);
Romain Guyf0af1d52012-07-11 18:31:21 -07004917 // isVisible is always true here, use a simple assignment
4918 isVisible = boundInView.intersect(visibleRect);
Guang Zhu0d607fb2012-05-11 19:34:56 -07004919 }
4920 return isVisible;
Romain Guyf0af1d52012-07-11 18:31:21 -07004921 }
4922
4923 return false;
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004924 }
4925
4926 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004927 * Sets a delegate for implementing accessibility support via compositon as
4928 * opposed to inheritance. The delegate's primary use is for implementing
4929 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4930 *
4931 * @param delegate The delegate instance.
4932 *
4933 * @see AccessibilityDelegate
4934 */
4935 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4936 mAccessibilityDelegate = delegate;
4937 }
4938
4939 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004940 * Gets the provider for managing a virtual view hierarchy rooted at this View
4941 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4942 * that explore the window content.
4943 * <p>
4944 * If this method returns an instance, this instance is responsible for managing
4945 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4946 * View including the one representing the View itself. Similarly the returned
4947 * instance is responsible for performing accessibility actions on any virtual
4948 * view or the root view itself.
4949 * </p>
4950 * <p>
4951 * If an {@link AccessibilityDelegate} has been specified via calling
4952 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4953 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4954 * is responsible for handling this call.
4955 * </p>
4956 *
4957 * @return The provider.
4958 *
4959 * @see AccessibilityNodeProvider
4960 */
4961 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4962 if (mAccessibilityDelegate != null) {
4963 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4964 } else {
4965 return null;
4966 }
4967 }
4968
4969 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004970 * Gets the unique identifier of this view on the screen for accessibility purposes.
4971 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4972 *
4973 * @return The view accessibility id.
4974 *
4975 * @hide
4976 */
4977 public int getAccessibilityViewId() {
4978 if (mAccessibilityViewId == NO_ID) {
4979 mAccessibilityViewId = sNextAccessibilityViewId++;
4980 }
4981 return mAccessibilityViewId;
4982 }
4983
4984 /**
4985 * Gets the unique identifier of the window in which this View reseides.
4986 *
4987 * @return The window accessibility id.
4988 *
4989 * @hide
4990 */
4991 public int getAccessibilityWindowId() {
4992 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4993 }
4994
4995 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004996 * Gets the {@link View} description. It briefly describes the view and is
4997 * primarily used for accessibility support. Set this property to enable
4998 * better accessibility support for your application. This is especially
4999 * true for views that do not have textual representation (For example,
5000 * ImageButton).
5001 *
Svetoslav Ganov42138042012-03-20 11:51:39 -07005002 * @return The content description.
svetoslavganov75986cf2009-05-14 22:28:01 -07005003 *
5004 * @attr ref android.R.styleable#View_contentDescription
5005 */
Svetoslav Ganov42138042012-03-20 11:51:39 -07005006 @ViewDebug.ExportedProperty(category = "accessibility")
svetoslavganov75986cf2009-05-14 22:28:01 -07005007 public CharSequence getContentDescription() {
5008 return mContentDescription;
5009 }
5010
5011 /**
5012 * Sets the {@link View} description. It briefly describes the view and is
5013 * primarily used for accessibility support. Set this property to enable
5014 * better accessibility support for your application. This is especially
5015 * true for views that do not have textual representation (For example,
5016 * ImageButton).
5017 *
5018 * @param contentDescription The content description.
5019 *
5020 * @attr ref android.R.styleable#View_contentDescription
5021 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07005022 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07005023 public void setContentDescription(CharSequence contentDescription) {
5024 mContentDescription = contentDescription;
Svetoslav Ganove47957a2012-06-05 14:46:50 -07005025 final boolean nonEmptyDesc = contentDescription != null && contentDescription.length() > 0;
5026 if (nonEmptyDesc && getImportantForAccessibility() == IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
5027 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
5028 }
svetoslavganov75986cf2009-05-14 22:28:01 -07005029 }
5030
5031 /**
Romain Guya2431d02009-04-30 16:30:00 -07005032 * Invoked whenever this view loses focus, either by losing window focus or by losing
5033 * focus within its window. This method can be used to clear any state tied to the
5034 * focus. For instance, if a button is held pressed with the trackball and the window
5035 * loses focus, this method can be used to cancel the press.
5036 *
5037 * Subclasses of View overriding this method should always call super.onFocusLost().
5038 *
5039 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07005040 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07005041 *
5042 * @hide pending API council approval
5043 */
5044 protected void onFocusLost() {
5045 resetPressedState();
5046 }
5047
5048 private void resetPressedState() {
5049 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5050 return;
5051 }
5052
5053 if (isPressed()) {
5054 setPressed(false);
5055
5056 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05005057 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005058 }
5059 }
5060 }
5061
5062 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005063 * Returns true if this view has focus
5064 *
5065 * @return True if this view has focus, false otherwise.
5066 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005067 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005068 public boolean isFocused() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005069 return (mPrivateFlags & PFLAG_FOCUSED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005070 }
5071
5072 /**
5073 * Find the view in the hierarchy rooted at this view that currently has
5074 * focus.
5075 *
5076 * @return The view that currently has focus, or null if no focused view can
5077 * be found.
5078 */
5079 public View findFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005080 return (mPrivateFlags & PFLAG_FOCUSED) != 0 ? this : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005081 }
5082
5083 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07005084 * Indicates whether this view is one of the set of scrollable containers in
5085 * its window.
5086 *
5087 * @return whether this view is one of the set of scrollable containers in
5088 * its window
5089 *
5090 * @attr ref android.R.styleable#View_isScrollContainer
5091 */
5092 public boolean isScrollContainer() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005093 return (mPrivateFlags & PFLAG_SCROLL_CONTAINER_ADDED) != 0;
Philip Milne6c8ea062012-04-03 17:38:43 -07005094 }
5095
5096 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005097 * Change whether this view is one of the set of scrollable containers in
5098 * its window. This will be used to determine whether the window can
5099 * resize or must pan when a soft input area is open -- scrollable
5100 * containers allow the window to use resize mode since the container
5101 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07005102 *
5103 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005104 */
5105 public void setScrollContainer(boolean isScrollContainer) {
5106 if (isScrollContainer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005107 if (mAttachInfo != null && (mPrivateFlags&PFLAG_SCROLL_CONTAINER_ADDED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005108 mAttachInfo.mScrollContainers.add(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -07005109 mPrivateFlags |= PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005110 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005111 mPrivateFlags |= PFLAG_SCROLL_CONTAINER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005112 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005113 if ((mPrivateFlags&PFLAG_SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005114 mAttachInfo.mScrollContainers.remove(this);
5115 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005116 mPrivateFlags &= ~(PFLAG_SCROLL_CONTAINER|PFLAG_SCROLL_CONTAINER_ADDED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005117 }
5118 }
5119
5120 /**
5121 * Returns the quality of the drawing cache.
5122 *
5123 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5124 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5125 *
5126 * @see #setDrawingCacheQuality(int)
5127 * @see #setDrawingCacheEnabled(boolean)
5128 * @see #isDrawingCacheEnabled()
5129 *
5130 * @attr ref android.R.styleable#View_drawingCacheQuality
5131 */
5132 public int getDrawingCacheQuality() {
5133 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
5134 }
5135
5136 /**
5137 * Set the drawing cache quality of this view. This value is used only when the
5138 * drawing cache is enabled
5139 *
5140 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5141 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5142 *
5143 * @see #getDrawingCacheQuality()
5144 * @see #setDrawingCacheEnabled(boolean)
5145 * @see #isDrawingCacheEnabled()
5146 *
5147 * @attr ref android.R.styleable#View_drawingCacheQuality
5148 */
5149 public void setDrawingCacheQuality(int quality) {
5150 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
5151 }
5152
5153 /**
5154 * Returns whether the screen should remain on, corresponding to the current
5155 * value of {@link #KEEP_SCREEN_ON}.
5156 *
5157 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
5158 *
5159 * @see #setKeepScreenOn(boolean)
5160 *
5161 * @attr ref android.R.styleable#View_keepScreenOn
5162 */
5163 public boolean getKeepScreenOn() {
5164 return (mViewFlags & KEEP_SCREEN_ON) != 0;
5165 }
5166
5167 /**
5168 * Controls whether the screen should remain on, modifying the
5169 * value of {@link #KEEP_SCREEN_ON}.
5170 *
5171 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
5172 *
5173 * @see #getKeepScreenOn()
5174 *
5175 * @attr ref android.R.styleable#View_keepScreenOn
5176 */
5177 public void setKeepScreenOn(boolean keepScreenOn) {
5178 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
5179 }
5180
5181 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005182 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5183 * @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 -08005184 *
5185 * @attr ref android.R.styleable#View_nextFocusLeft
5186 */
5187 public int getNextFocusLeftId() {
5188 return mNextFocusLeftId;
5189 }
5190
5191 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005192 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5193 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
5194 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005195 *
5196 * @attr ref android.R.styleable#View_nextFocusLeft
5197 */
5198 public void setNextFocusLeftId(int nextFocusLeftId) {
5199 mNextFocusLeftId = nextFocusLeftId;
5200 }
5201
5202 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005203 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5204 * @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 -08005205 *
5206 * @attr ref android.R.styleable#View_nextFocusRight
5207 */
5208 public int getNextFocusRightId() {
5209 return mNextFocusRightId;
5210 }
5211
5212 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005213 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5214 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
5215 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005216 *
5217 * @attr ref android.R.styleable#View_nextFocusRight
5218 */
5219 public void setNextFocusRightId(int nextFocusRightId) {
5220 mNextFocusRightId = nextFocusRightId;
5221 }
5222
5223 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005224 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5225 * @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 -08005226 *
5227 * @attr ref android.R.styleable#View_nextFocusUp
5228 */
5229 public int getNextFocusUpId() {
5230 return mNextFocusUpId;
5231 }
5232
5233 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005234 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5235 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
5236 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005237 *
5238 * @attr ref android.R.styleable#View_nextFocusUp
5239 */
5240 public void setNextFocusUpId(int nextFocusUpId) {
5241 mNextFocusUpId = nextFocusUpId;
5242 }
5243
5244 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005245 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5246 * @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 -08005247 *
5248 * @attr ref android.R.styleable#View_nextFocusDown
5249 */
5250 public int getNextFocusDownId() {
5251 return mNextFocusDownId;
5252 }
5253
5254 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005255 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5256 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
5257 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005258 *
5259 * @attr ref android.R.styleable#View_nextFocusDown
5260 */
5261 public void setNextFocusDownId(int nextFocusDownId) {
5262 mNextFocusDownId = nextFocusDownId;
5263 }
5264
5265 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005266 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5267 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
5268 *
5269 * @attr ref android.R.styleable#View_nextFocusForward
5270 */
5271 public int getNextFocusForwardId() {
5272 return mNextFocusForwardId;
5273 }
5274
5275 /**
5276 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5277 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
5278 * decide automatically.
5279 *
5280 * @attr ref android.R.styleable#View_nextFocusForward
5281 */
5282 public void setNextFocusForwardId(int nextFocusForwardId) {
5283 mNextFocusForwardId = nextFocusForwardId;
5284 }
5285
5286 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005287 * Returns the visibility of this view and all of its ancestors
5288 *
5289 * @return True if this view and all of its ancestors are {@link #VISIBLE}
5290 */
5291 public boolean isShown() {
5292 View current = this;
5293 //noinspection ConstantConditions
5294 do {
5295 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5296 return false;
5297 }
5298 ViewParent parent = current.mParent;
5299 if (parent == null) {
5300 return false; // We are not attached to the view root
5301 }
5302 if (!(parent instanceof View)) {
5303 return true;
5304 }
5305 current = (View) parent;
5306 } while (current != null);
5307
5308 return false;
5309 }
5310
5311 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005312 * Called by the view hierarchy when the content insets for a window have
5313 * changed, to allow it to adjust its content to fit within those windows.
5314 * The content insets tell you the space that the status bar, input method,
5315 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005316 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005317 * <p>You do not normally need to deal with this function, since the default
5318 * window decoration given to applications takes care of applying it to the
5319 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
5320 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
5321 * and your content can be placed under those system elements. You can then
5322 * use this method within your view hierarchy if you have parts of your UI
5323 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005324 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005325 * <p>The default implementation of this method simply applies the content
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005326 * inset's to the view's padding, consuming that content (modifying the
5327 * insets to be 0), and returning true. This behavior is off by default, but can
5328 * be enabled through {@link #setFitsSystemWindows(boolean)}.
5329 *
5330 * <p>This function's traversal down the hierarchy is depth-first. The same content
5331 * insets object is propagated down the hierarchy, so any changes made to it will
5332 * be seen by all following views (including potentially ones above in
5333 * the hierarchy since this is a depth-first traversal). The first view
5334 * that returns true will abort the entire traversal.
5335 *
5336 * <p>The default implementation works well for a situation where it is
5337 * used with a container that covers the entire window, allowing it to
5338 * apply the appropriate insets to its content on all edges. If you need
5339 * a more complicated layout (such as two different views fitting system
5340 * windows, one on the top of the window, and one on the bottom),
5341 * you can override the method and handle the insets however you would like.
5342 * Note that the insets provided by the framework are always relative to the
5343 * far edges of the window, not accounting for the location of the called view
5344 * within that window. (In fact when this method is called you do not yet know
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005345 * where the layout will place the view, as it is done before layout happens.)
5346 *
5347 * <p>Note: unlike many View methods, there is no dispatch phase to this
5348 * call. If you are overriding it in a ViewGroup and want to allow the
5349 * call to continue to your children, you must be sure to call the super
5350 * implementation.
5351 *
Dianne Hackborncf675782012-05-10 15:07:24 -07005352 * <p>Here is a sample layout that makes use of fitting system windows
5353 * to have controls for a video view placed inside of the window decorations
5354 * that it hides and shows. This can be used with code like the second
5355 * sample (video player) shown in {@link #setSystemUiVisibility(int)}.
5356 *
5357 * {@sample development/samples/ApiDemos/res/layout/video_player.xml complete}
5358 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005359 * @param insets Current content insets of the window. Prior to
5360 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
5361 * the insets or else you and Android will be unhappy.
5362 *
5363 * @return Return true if this view applied the insets and it should not
5364 * continue propagating further down the hierarchy, false otherwise.
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005365 * @see #getFitsSystemWindows()
Romain Guyf0af1d52012-07-11 18:31:21 -07005366 * @see #setFitsSystemWindows(boolean)
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005367 * @see #setSystemUiVisibility(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005368 */
5369 protected boolean fitSystemWindows(Rect insets) {
5370 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07005371 mUserPaddingStart = UNDEFINED_PADDING;
5372 mUserPaddingEnd = UNDEFINED_PADDING;
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005373 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
5374 || mAttachInfo == null
5375 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
5376 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
5377 return true;
5378 } else {
5379 internalSetPadding(0, 0, 0, 0);
5380 return false;
5381 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005382 }
5383 return false;
5384 }
5385
5386 /**
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005387 * Sets whether or not this view should account for system screen decorations
5388 * such as the status bar and inset its content; that is, controlling whether
5389 * the default implementation of {@link #fitSystemWindows(Rect)} will be
5390 * executed. See that method for more details.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005391 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005392 * <p>Note that if you are providing your own implementation of
5393 * {@link #fitSystemWindows(Rect)}, then there is no need to set this
5394 * flag to true -- your implementation will be overriding the default
5395 * implementation that checks this flag.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005396 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005397 * @param fitSystemWindows If true, then the default implementation of
5398 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005399 *
5400 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005401 * @see #getFitsSystemWindows()
5402 * @see #fitSystemWindows(Rect)
5403 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005404 */
5405 public void setFitsSystemWindows(boolean fitSystemWindows) {
5406 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
5407 }
5408
5409 /**
Dianne Hackborncf675782012-05-10 15:07:24 -07005410 * Check for state of {@link #setFitsSystemWindows(boolean). If this method
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005411 * returns true, the default implementation of {@link #fitSystemWindows(Rect)}
5412 * will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005413 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005414 * @return Returns true if the default implementation of
5415 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005416 *
5417 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005418 * @see #setFitsSystemWindows()
5419 * @see #fitSystemWindows(Rect)
5420 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005421 */
Dianne Hackborncf675782012-05-10 15:07:24 -07005422 public boolean getFitsSystemWindows() {
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005423 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
5424 }
5425
Dianne Hackbornb1b55e62012-05-10 16:25:54 -07005426 /** @hide */
5427 public boolean fitsSystemWindows() {
5428 return getFitsSystemWindows();
5429 }
5430
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005431 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005432 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
5433 */
5434 public void requestFitSystemWindows() {
5435 if (mParent != null) {
5436 mParent.requestFitSystemWindows();
5437 }
5438 }
5439
5440 /**
5441 * For use by PhoneWindow to make its own system window fitting optional.
5442 * @hide
5443 */
5444 public void makeOptionalFitsSystemWindows() {
5445 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5446 }
5447
5448 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005449 * Returns the visibility status for this view.
5450 *
5451 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5452 * @attr ref android.R.styleable#View_visibility
5453 */
5454 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005455 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5456 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5457 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005458 })
5459 public int getVisibility() {
5460 return mViewFlags & VISIBILITY_MASK;
5461 }
5462
5463 /**
5464 * Set the enabled state of this view.
5465 *
5466 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5467 * @attr ref android.R.styleable#View_visibility
5468 */
5469 @RemotableViewMethod
5470 public void setVisibility(int visibility) {
5471 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005472 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005473 }
5474
5475 /**
5476 * Returns the enabled status for this view. The interpretation of the
5477 * enabled state varies by subclass.
5478 *
5479 * @return True if this view is enabled, false otherwise.
5480 */
5481 @ViewDebug.ExportedProperty
5482 public boolean isEnabled() {
5483 return (mViewFlags & ENABLED_MASK) == ENABLED;
5484 }
5485
5486 /**
5487 * Set the enabled state of this view. The interpretation of the enabled
5488 * state varies by subclass.
5489 *
5490 * @param enabled True if this view is enabled, false otherwise.
5491 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005492 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005493 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005494 if (enabled == isEnabled()) return;
5495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005496 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5497
5498 /*
5499 * The View most likely has to change its appearance, so refresh
5500 * the drawable state.
5501 */
5502 refreshDrawableState();
5503
5504 // Invalidate too, since the default behavior for views is to be
5505 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005506 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005507 }
5508
5509 /**
5510 * Set whether this view can receive the focus.
5511 *
5512 * Setting this to false will also ensure that this view is not focusable
5513 * in touch mode.
5514 *
5515 * @param focusable If true, this view can receive the focus.
5516 *
5517 * @see #setFocusableInTouchMode(boolean)
5518 * @attr ref android.R.styleable#View_focusable
5519 */
5520 public void setFocusable(boolean focusable) {
5521 if (!focusable) {
5522 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5523 }
5524 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5525 }
5526
5527 /**
5528 * Set whether this view can receive focus while in touch mode.
5529 *
5530 * Setting this to true will also ensure that this view is focusable.
5531 *
5532 * @param focusableInTouchMode If true, this view can receive the focus while
5533 * in touch mode.
5534 *
5535 * @see #setFocusable(boolean)
5536 * @attr ref android.R.styleable#View_focusableInTouchMode
5537 */
5538 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5539 // Focusable in touch mode should always be set before the focusable flag
5540 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5541 // which, in touch mode, will not successfully request focus on this view
5542 // because the focusable in touch mode flag is not set
5543 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5544 if (focusableInTouchMode) {
5545 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5546 }
5547 }
5548
5549 /**
5550 * Set whether this view should have sound effects enabled for events such as
5551 * clicking and touching.
5552 *
5553 * <p>You may wish to disable sound effects for a view if you already play sounds,
5554 * for instance, a dial key that plays dtmf tones.
5555 *
5556 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5557 * @see #isSoundEffectsEnabled()
5558 * @see #playSoundEffect(int)
5559 * @attr ref android.R.styleable#View_soundEffectsEnabled
5560 */
5561 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5562 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5563 }
5564
5565 /**
5566 * @return whether this view should have sound effects enabled for events such as
5567 * clicking and touching.
5568 *
5569 * @see #setSoundEffectsEnabled(boolean)
5570 * @see #playSoundEffect(int)
5571 * @attr ref android.R.styleable#View_soundEffectsEnabled
5572 */
5573 @ViewDebug.ExportedProperty
5574 public boolean isSoundEffectsEnabled() {
5575 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5576 }
5577
5578 /**
5579 * Set whether this view should have haptic feedback for events such as
5580 * long presses.
5581 *
5582 * <p>You may wish to disable haptic feedback if your view already controls
5583 * its own haptic feedback.
5584 *
5585 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5586 * @see #isHapticFeedbackEnabled()
5587 * @see #performHapticFeedback(int)
5588 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5589 */
5590 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5591 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5592 }
5593
5594 /**
5595 * @return whether this view should have haptic feedback enabled for events
5596 * long presses.
5597 *
5598 * @see #setHapticFeedbackEnabled(boolean)
5599 * @see #performHapticFeedback(int)
5600 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5601 */
5602 @ViewDebug.ExportedProperty
5603 public boolean isHapticFeedbackEnabled() {
5604 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5605 }
5606
5607 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005608 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005609 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005610 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5611 * {@link #LAYOUT_DIRECTION_RTL},
5612 * {@link #LAYOUT_DIRECTION_INHERIT} or
5613 * {@link #LAYOUT_DIRECTION_LOCALE}.
5614 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005615 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005616 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005617 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5618 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5619 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5620 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005621 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005622 public int getLayoutDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005623 return (mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_MASK) >> PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005624 }
5625
5626 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005627 * Set the layout direction for this view. This will propagate a reset of layout direction
5628 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005629 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005630 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
5631 * {@link #LAYOUT_DIRECTION_RTL},
5632 * {@link #LAYOUT_DIRECTION_INHERIT} or
5633 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005634 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005635 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005636 */
5637 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005638 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005639 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005640 // Reset the current layout direction and the resolved one
Dianne Hackborn4702a852012-08-17 15:18:29 -07005641 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07005642 resetResolvedLayoutDirection();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005643 // Reset padding resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -07005644 mPrivateFlags2 &= ~PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005645 // Set the new layout direction (filtered)
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005646 mPrivateFlags2 |=
Dianne Hackborn4702a852012-08-17 15:18:29 -07005647 ((layoutDirection << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) & PFLAG2_LAYOUT_DIRECTION_MASK);
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005648 resolveRtlProperties();
5649 // ... and ask for a layout pass
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005650 requestLayout();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005651 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005652 }
5653
5654 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005655 * Returns the resolved layout direction for this view.
5656 *
5657 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005658 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005659 */
5660 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005661 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5662 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005663 })
5664 public int getResolvedLayoutDirection() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005665 final int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion;
5666 if (targetSdkVersion < JELLY_BEAN_MR1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005667 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005668 return LAYOUT_DIRECTION_LTR;
5669 }
Fabrice Di Megliob93911f2012-06-26 19:43:15 -07005670 // The layout direction will be resolved only if needed
Dianne Hackborn4702a852012-08-17 15:18:29 -07005671 if ((mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_RESOLVED) != PFLAG2_LAYOUT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -07005672 resolveLayoutDirection();
5673 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005674 return ((mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL) == PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005675 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
5676 }
5677
5678 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005679 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5680 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005681 *
5682 * @return true if the layout is right-to-left.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005683 */
5684 @ViewDebug.ExportedProperty(category = "layout")
5685 public boolean isLayoutRtl() {
5686 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
5687 }
5688
5689 /**
Adam Powell539ee872012-02-03 19:00:49 -08005690 * Indicates whether the view is currently tracking transient state that the
5691 * app should not need to concern itself with saving and restoring, but that
5692 * the framework should take special note to preserve when possible.
5693 *
Adam Powell785c4472012-05-02 21:25:39 -07005694 * <p>A view with transient state cannot be trivially rebound from an external
5695 * data source, such as an adapter binding item views in a list. This may be
5696 * because the view is performing an animation, tracking user selection
5697 * of content, or similar.</p>
5698 *
Adam Powell539ee872012-02-03 19:00:49 -08005699 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005700 */
5701 @ViewDebug.ExportedProperty(category = "layout")
5702 public boolean hasTransientState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005703 return (mPrivateFlags2 & PFLAG2_HAS_TRANSIENT_STATE) == PFLAG2_HAS_TRANSIENT_STATE;
Adam Powell539ee872012-02-03 19:00:49 -08005704 }
5705
5706 /**
5707 * Set whether this view is currently tracking transient state that the
Chet Haase563d4f22012-04-18 16:20:08 -07005708 * framework should attempt to preserve when possible. This flag is reference counted,
5709 * so every call to setHasTransientState(true) should be paired with a later call
5710 * to setHasTransientState(false).
Adam Powell539ee872012-02-03 19:00:49 -08005711 *
Adam Powell785c4472012-05-02 21:25:39 -07005712 * <p>A view with transient state cannot be trivially rebound from an external
5713 * data source, such as an adapter binding item views in a list. This may be
5714 * because the view is performing an animation, tracking user selection
5715 * of content, or similar.</p>
5716 *
Adam Powell539ee872012-02-03 19:00:49 -08005717 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005718 */
5719 public void setHasTransientState(boolean hasTransientState) {
Chet Haase563d4f22012-04-18 16:20:08 -07005720 mTransientStateCount = hasTransientState ? mTransientStateCount + 1 :
5721 mTransientStateCount - 1;
5722 if (mTransientStateCount < 0) {
5723 mTransientStateCount = 0;
5724 Log.e(VIEW_LOG_TAG, "hasTransientState decremented below 0: " +
5725 "unmatched pair of setHasTransientState calls");
5726 }
5727 if ((hasTransientState && mTransientStateCount == 1) ||
Adam Powell057a5852012-05-11 10:28:38 -07005728 (!hasTransientState && mTransientStateCount == 0)) {
Chet Haase563d4f22012-04-18 16:20:08 -07005729 // update flag if we've just incremented up from 0 or decremented down to 0
Dianne Hackborn4702a852012-08-17 15:18:29 -07005730 mPrivateFlags2 = (mPrivateFlags2 & ~PFLAG2_HAS_TRANSIENT_STATE) |
5731 (hasTransientState ? PFLAG2_HAS_TRANSIENT_STATE : 0);
Chet Haase563d4f22012-04-18 16:20:08 -07005732 if (mParent != null) {
5733 try {
5734 mParent.childHasTransientStateChanged(this, hasTransientState);
5735 } catch (AbstractMethodError e) {
5736 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5737 " does not fully implement ViewParent", e);
5738 }
Adam Powell539ee872012-02-03 19:00:49 -08005739 }
5740 }
5741 }
5742
5743 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005744 * If this view doesn't do any drawing on its own, set this flag to
5745 * allow further optimizations. By default, this flag is not set on
5746 * View, but could be set on some View subclasses such as ViewGroup.
5747 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005748 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5749 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005750 *
5751 * @param willNotDraw whether or not this View draw on its own
5752 */
5753 public void setWillNotDraw(boolean willNotDraw) {
5754 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5755 }
5756
5757 /**
5758 * Returns whether or not this View draws on its own.
5759 *
5760 * @return true if this view has nothing to draw, false otherwise
5761 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005762 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005763 public boolean willNotDraw() {
5764 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
5765 }
5766
5767 /**
5768 * When a View's drawing cache is enabled, drawing is redirected to an
5769 * offscreen bitmap. Some views, like an ImageView, must be able to
5770 * bypass this mechanism if they already draw a single bitmap, to avoid
5771 * unnecessary usage of the memory.
5772 *
5773 * @param willNotCacheDrawing true if this view does not cache its
5774 * drawing, false otherwise
5775 */
5776 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
5777 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
5778 }
5779
5780 /**
5781 * Returns whether or not this View can cache its drawing or not.
5782 *
5783 * @return true if this view does not cache its drawing, false otherwise
5784 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005785 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005786 public boolean willNotCacheDrawing() {
5787 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5788 }
5789
5790 /**
5791 * Indicates whether this view reacts to click events or not.
5792 *
5793 * @return true if the view is clickable, false otherwise
5794 *
5795 * @see #setClickable(boolean)
5796 * @attr ref android.R.styleable#View_clickable
5797 */
5798 @ViewDebug.ExportedProperty
5799 public boolean isClickable() {
5800 return (mViewFlags & CLICKABLE) == CLICKABLE;
5801 }
5802
5803 /**
5804 * Enables or disables click events for this view. When a view
5805 * is clickable it will change its state to "pressed" on every click.
5806 * Subclasses should set the view clickable to visually react to
5807 * user's clicks.
5808 *
5809 * @param clickable true to make the view clickable, false otherwise
5810 *
5811 * @see #isClickable()
5812 * @attr ref android.R.styleable#View_clickable
5813 */
5814 public void setClickable(boolean clickable) {
5815 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5816 }
5817
5818 /**
5819 * Indicates whether this view reacts to long click events or not.
5820 *
5821 * @return true if the view is long clickable, false otherwise
5822 *
5823 * @see #setLongClickable(boolean)
5824 * @attr ref android.R.styleable#View_longClickable
5825 */
5826 public boolean isLongClickable() {
5827 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5828 }
5829
5830 /**
5831 * Enables or disables long click events for this view. When a view is long
5832 * clickable it reacts to the user holding down the button for a longer
5833 * duration than a tap. This event can either launch the listener or a
5834 * context menu.
5835 *
5836 * @param longClickable true to make the view long clickable, false otherwise
5837 * @see #isLongClickable()
5838 * @attr ref android.R.styleable#View_longClickable
5839 */
5840 public void setLongClickable(boolean longClickable) {
5841 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5842 }
5843
5844 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005845 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005846 *
5847 * @see #isClickable()
5848 * @see #setClickable(boolean)
5849 *
5850 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5851 * the View's internal state from a previously set "pressed" state.
5852 */
5853 public void setPressed(boolean pressed) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005854 final boolean needsRefresh = pressed != ((mPrivateFlags & PFLAG_PRESSED) == PFLAG_PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005855
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005856 if (pressed) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005857 mPrivateFlags |= PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005858 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005859 mPrivateFlags &= ~PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005860 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005861
5862 if (needsRefresh) {
5863 refreshDrawableState();
5864 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005865 dispatchSetPressed(pressed);
5866 }
5867
5868 /**
5869 * Dispatch setPressed to all of this View's children.
5870 *
5871 * @see #setPressed(boolean)
5872 *
5873 * @param pressed The new pressed state
5874 */
5875 protected void dispatchSetPressed(boolean pressed) {
5876 }
5877
5878 /**
5879 * Indicates whether the view is currently in pressed state. Unless
5880 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5881 * the pressed state.
5882 *
Philip Milne6c8ea062012-04-03 17:38:43 -07005883 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005884 * @see #isClickable()
5885 * @see #setClickable(boolean)
5886 *
5887 * @return true if the view is currently pressed, false otherwise
5888 */
5889 public boolean isPressed() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005890 return (mPrivateFlags & PFLAG_PRESSED) == PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005891 }
5892
5893 /**
5894 * Indicates whether this view will save its state (that is,
5895 * whether its {@link #onSaveInstanceState} method will be called).
5896 *
5897 * @return Returns true if the view state saving is enabled, else false.
5898 *
5899 * @see #setSaveEnabled(boolean)
5900 * @attr ref android.R.styleable#View_saveEnabled
5901 */
5902 public boolean isSaveEnabled() {
5903 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5904 }
5905
5906 /**
5907 * Controls whether the saving of this view's state is
5908 * enabled (that is, whether its {@link #onSaveInstanceState} method
5909 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005910 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005911 * for its state to be saved. This flag can only disable the
5912 * saving of this view; any child views may still have their state saved.
5913 *
5914 * @param enabled Set to false to <em>disable</em> state saving, or true
5915 * (the default) to allow it.
5916 *
5917 * @see #isSaveEnabled()
5918 * @see #setId(int)
5919 * @see #onSaveInstanceState()
5920 * @attr ref android.R.styleable#View_saveEnabled
5921 */
5922 public void setSaveEnabled(boolean enabled) {
5923 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5924 }
5925
Jeff Brown85a31762010-09-01 17:01:00 -07005926 /**
5927 * Gets whether the framework should discard touches when the view's
5928 * window is obscured by another visible window.
5929 * Refer to the {@link View} security documentation for more details.
5930 *
5931 * @return True if touch filtering is enabled.
5932 *
5933 * @see #setFilterTouchesWhenObscured(boolean)
5934 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5935 */
5936 @ViewDebug.ExportedProperty
5937 public boolean getFilterTouchesWhenObscured() {
5938 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5939 }
5940
5941 /**
5942 * Sets whether the framework should discard touches when the view's
5943 * window is obscured by another visible window.
5944 * Refer to the {@link View} security documentation for more details.
5945 *
5946 * @param enabled True if touch filtering should be enabled.
5947 *
5948 * @see #getFilterTouchesWhenObscured
5949 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5950 */
5951 public void setFilterTouchesWhenObscured(boolean enabled) {
5952 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5953 FILTER_TOUCHES_WHEN_OBSCURED);
5954 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005955
5956 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005957 * Indicates whether the entire hierarchy under this view will save its
5958 * state when a state saving traversal occurs from its parent. The default
5959 * is true; if false, these views will not be saved unless
5960 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5961 *
5962 * @return Returns true if the view state saving from parent is enabled, else false.
5963 *
5964 * @see #setSaveFromParentEnabled(boolean)
5965 */
5966 public boolean isSaveFromParentEnabled() {
5967 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5968 }
5969
5970 /**
5971 * Controls whether the entire hierarchy under this view will save its
5972 * state when a state saving traversal occurs from its parent. The default
5973 * is true; if false, these views will not be saved unless
5974 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5975 *
5976 * @param enabled Set to false to <em>disable</em> state saving, or true
5977 * (the default) to allow it.
5978 *
5979 * @see #isSaveFromParentEnabled()
5980 * @see #setId(int)
5981 * @see #onSaveInstanceState()
5982 */
5983 public void setSaveFromParentEnabled(boolean enabled) {
5984 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5985 }
5986
5987
5988 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005989 * Returns whether this View is able to take focus.
5990 *
5991 * @return True if this view can take focus, or false otherwise.
5992 * @attr ref android.R.styleable#View_focusable
5993 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005994 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005995 public final boolean isFocusable() {
5996 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
5997 }
5998
5999 /**
6000 * When a view is focusable, it may not want to take focus when in touch mode.
6001 * For example, a button would like focus when the user is navigating via a D-pad
6002 * so that the user can click on it, but once the user starts touching the screen,
6003 * the button shouldn't take focus
6004 * @return Whether the view is focusable in touch mode.
6005 * @attr ref android.R.styleable#View_focusableInTouchMode
6006 */
6007 @ViewDebug.ExportedProperty
6008 public final boolean isFocusableInTouchMode() {
6009 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
6010 }
6011
6012 /**
6013 * Find the nearest view in the specified direction that can take focus.
6014 * This does not actually give focus to that view.
6015 *
6016 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6017 *
6018 * @return The nearest focusable in the specified direction, or null if none
6019 * can be found.
6020 */
6021 public View focusSearch(int direction) {
6022 if (mParent != null) {
6023 return mParent.focusSearch(this, direction);
6024 } else {
6025 return null;
6026 }
6027 }
6028
6029 /**
6030 * This method is the last chance for the focused view and its ancestors to
6031 * respond to an arrow key. This is called when the focused view did not
6032 * consume the key internally, nor could the view system find a new view in
6033 * the requested direction to give focus to.
6034 *
6035 * @param focused The currently focused view.
6036 * @param direction The direction focus wants to move. One of FOCUS_UP,
6037 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
6038 * @return True if the this view consumed this unhandled move.
6039 */
6040 public boolean dispatchUnhandledMove(View focused, int direction) {
6041 return false;
6042 }
6043
6044 /**
6045 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08006046 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006047 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08006048 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
6049 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006050 * @return The user specified next view, or null if there is none.
6051 */
6052 View findUserSetNextFocus(View root, int direction) {
6053 switch (direction) {
6054 case FOCUS_LEFT:
6055 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006056 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006057 case FOCUS_RIGHT:
6058 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006059 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006060 case FOCUS_UP:
6061 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006062 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006063 case FOCUS_DOWN:
6064 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006065 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006066 case FOCUS_FORWARD:
6067 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006068 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006069 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08006070 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08006071 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006072 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08006073 @Override
6074 public boolean apply(View t) {
6075 return t.mNextFocusForwardId == id;
6076 }
6077 });
6078 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006079 }
6080 return null;
6081 }
6082
Jeff Brown4dfbec22011-08-15 14:55:37 -07006083 private View findViewInsideOutShouldExist(View root, final int childViewId) {
6084 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
6085 @Override
6086 public boolean apply(View t) {
6087 return t.mID == childViewId;
6088 }
6089 });
6090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006091 if (result == null) {
6092 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
6093 + "by user for id " + childViewId);
6094 }
6095 return result;
6096 }
6097
6098 /**
6099 * Find and return all focusable views that are descendants of this view,
6100 * possibly including this view if it is focusable itself.
6101 *
6102 * @param direction The direction of the focus
6103 * @return A list of focusable views
6104 */
6105 public ArrayList<View> getFocusables(int direction) {
6106 ArrayList<View> result = new ArrayList<View>(24);
6107 addFocusables(result, direction);
6108 return result;
6109 }
6110
6111 /**
6112 * Add any focusable views that are descendants of this view (possibly
6113 * including this view if it is focusable itself) to views. If we are in touch mode,
6114 * only add views that are also focusable in touch mode.
6115 *
6116 * @param views Focusable views found so far
6117 * @param direction The direction of the focus
6118 */
6119 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006120 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
6121 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006122
svetoslavganov75986cf2009-05-14 22:28:01 -07006123 /**
6124 * Adds any focusable views that are descendants of this view (possibly
6125 * including this view if it is focusable itself) to views. This method
6126 * adds all focusable views regardless if we are in touch mode or
Svetoslav Ganov42138042012-03-20 11:51:39 -07006127 * only views focusable in touch mode if we are in touch mode or
6128 * only views that can take accessibility focus if accessibility is enabeld
6129 * depending on the focusable mode paramater.
svetoslavganov75986cf2009-05-14 22:28:01 -07006130 *
6131 * @param views Focusable views found so far or null if all we are interested is
6132 * the number of focusables.
6133 * @param direction The direction of the focus.
6134 * @param focusableMode The type of focusables to be added.
6135 *
6136 * @see #FOCUSABLES_ALL
6137 * @see #FOCUSABLES_TOUCH_MODE
6138 */
6139 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006140 if (views == null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006141 return;
6142 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006143 if (!isFocusable()) {
6144 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07006145 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006146 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE
6147 && isInTouchMode() && !isFocusableInTouchMode()) {
6148 return;
6149 }
6150 views.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006151 }
6152
6153 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006154 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006155 * The search is performed by either the text that the View renders or the content
6156 * description that describes the view for accessibility purposes and the view does
6157 * not render or both. Clients can specify how the search is to be performed via
6158 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
6159 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006160 *
6161 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006162 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07006163 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006164 * @see #FIND_VIEWS_WITH_TEXT
6165 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
6166 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006167 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006168 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07006169 if (getAccessibilityNodeProvider() != null) {
6170 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
6171 outViews.add(this);
6172 }
6173 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006174 && (searched != null && searched.length() > 0)
6175 && (mContentDescription != null && mContentDescription.length() > 0)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006176 String searchedLowerCase = searched.toString().toLowerCase();
6177 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
6178 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
6179 outViews.add(this);
6180 }
6181 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006182 }
6183
6184 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006185 * Find and return all touchable views that are descendants of this view,
6186 * possibly including this view if it is touchable itself.
6187 *
6188 * @return A list of touchable views
6189 */
6190 public ArrayList<View> getTouchables() {
6191 ArrayList<View> result = new ArrayList<View>();
6192 addTouchables(result);
6193 return result;
6194 }
6195
6196 /**
6197 * Add any touchable views that are descendants of this view (possibly
6198 * including this view if it is touchable itself) to views.
6199 *
6200 * @param views Touchable views found so far
6201 */
6202 public void addTouchables(ArrayList<View> views) {
6203 final int viewFlags = mViewFlags;
6204
6205 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
6206 && (viewFlags & ENABLED_MASK) == ENABLED) {
6207 views.add(this);
6208 }
6209 }
6210
6211 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006212 * Returns whether this View is accessibility focused.
6213 *
6214 * @return True if this View is accessibility focused.
6215 */
6216 boolean isAccessibilityFocused() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006217 return (mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006218 }
6219
6220 /**
6221 * Call this to try to give accessibility focus to this view.
6222 *
6223 * A view will not actually take focus if {@link AccessibilityManager#isEnabled()}
6224 * returns false or the view is no visible or the view already has accessibility
6225 * focus.
6226 *
6227 * See also {@link #focusSearch(int)}, which is what you call to say that you
6228 * have focus, and you want your parent to look for the next one.
6229 *
6230 * @return Whether this view actually took accessibility focus.
6231 *
6232 * @hide
6233 */
6234 public boolean requestAccessibilityFocus() {
Svetoslav Ganov07b726c2012-04-30 12:24:57 -07006235 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
6236 if (!manager.isEnabled() || !manager.isTouchExplorationEnabled()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006237 return false;
6238 }
6239 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6240 return false;
6241 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07006242 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) == 0) {
6243 mPrivateFlags2 |= PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006244 ViewRootImpl viewRootImpl = getViewRootImpl();
6245 if (viewRootImpl != null) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006246 viewRootImpl.setAccessibilityFocus(this, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07006247 }
6248 invalidate();
6249 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
6250 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006251 return true;
6252 }
6253 return false;
6254 }
6255
6256 /**
6257 * Call this to try to clear accessibility focus of this view.
6258 *
6259 * See also {@link #focusSearch(int)}, which is what you call to say that you
6260 * have focus, and you want your parent to look for the next one.
6261 *
6262 * @hide
6263 */
6264 public void clearAccessibilityFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006265 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0) {
6266 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006267 invalidate();
6268 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
6269 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006270 }
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006271 // Clear the global reference of accessibility focus if this
6272 // view or any of its descendants had accessibility focus.
6273 ViewRootImpl viewRootImpl = getViewRootImpl();
6274 if (viewRootImpl != null) {
6275 View focusHost = viewRootImpl.getAccessibilityFocusedHost();
6276 if (focusHost != null && ViewRootImpl.isViewDescendantOf(focusHost, this)) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006277 viewRootImpl.setAccessibilityFocus(null, null);
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006278 }
6279 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006280 }
6281
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07006282 private void sendAccessibilityHoverEvent(int eventType) {
6283 // Since we are not delivering to a client accessibility events from not
6284 // important views (unless the clinet request that) we need to fire the
6285 // event from the deepest view exposed to the client. As a consequence if
6286 // the user crosses a not exposed view the client will see enter and exit
6287 // of the exposed predecessor followed by and enter and exit of that same
6288 // predecessor when entering and exiting the not exposed descendant. This
6289 // is fine since the client has a clear idea which view is hovered at the
6290 // price of a couple more events being sent. This is a simple and
6291 // working solution.
6292 View source = this;
6293 while (true) {
6294 if (source.includeForAccessibility()) {
6295 source.sendAccessibilityEvent(eventType);
6296 return;
6297 }
6298 ViewParent parent = source.getParent();
6299 if (parent instanceof View) {
6300 source = (View) parent;
6301 } else {
6302 return;
6303 }
6304 }
6305 }
6306
Svetoslav Ganov42138042012-03-20 11:51:39 -07006307 /**
6308 * Clears accessibility focus without calling any callback methods
6309 * normally invoked in {@link #clearAccessibilityFocus()}. This method
6310 * is used for clearing accessibility focus when giving this focus to
6311 * another view.
6312 */
6313 void clearAccessibilityFocusNoCallbacks() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006314 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0) {
6315 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006316 invalidate();
6317 }
6318 }
6319
6320 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006321 * Call this to try to give focus to a specific view or to one of its
6322 * descendants.
6323 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006324 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6325 * false), or if it is focusable and it is not focusable in touch mode
6326 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006327 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006328 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006329 * have focus, and you want your parent to look for the next one.
6330 *
6331 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
6332 * {@link #FOCUS_DOWN} and <code>null</code>.
6333 *
6334 * @return Whether this view or one of its descendants actually took focus.
6335 */
6336 public final boolean requestFocus() {
6337 return requestFocus(View.FOCUS_DOWN);
6338 }
6339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006340 /**
6341 * Call this to try to give focus to a specific view or to one of its
6342 * descendants and give it a hint about what direction focus is heading.
6343 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006344 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6345 * false), or if it is focusable and it is not focusable in touch mode
6346 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006347 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006348 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006349 * have focus, and you want your parent to look for the next one.
6350 *
6351 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
6352 * <code>null</code> set for the previously focused rectangle.
6353 *
6354 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6355 * @return Whether this view or one of its descendants actually took focus.
6356 */
6357 public final boolean requestFocus(int direction) {
6358 return requestFocus(direction, null);
6359 }
6360
6361 /**
6362 * Call this to try to give focus to a specific view or to one of its descendants
6363 * and give it hints about the direction and a specific rectangle that the focus
6364 * is coming from. The rectangle can help give larger views a finer grained hint
6365 * about where focus is coming from, and therefore, where to show selection, or
6366 * forward focus change internally.
6367 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006368 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6369 * false), or if it is focusable and it is not focusable in touch mode
6370 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006371 *
6372 * A View will not take focus if it is not visible.
6373 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006374 * A View will not take focus if one of its parents has
6375 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
6376 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006377 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006378 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006379 * have focus, and you want your parent to look for the next one.
6380 *
6381 * You may wish to override this method if your custom {@link View} has an internal
6382 * {@link View} that it wishes to forward the request to.
6383 *
6384 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6385 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
6386 * to give a finer grained hint about where focus is coming from. May be null
6387 * if there is no hint.
6388 * @return Whether this view or one of its descendants actually took focus.
6389 */
6390 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006391 return requestFocusNoSearch(direction, previouslyFocusedRect);
6392 }
6393
6394 private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006395 // need to be focusable
6396 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
6397 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6398 return false;
6399 }
6400
6401 // need to be focusable in touch mode if in touch mode
6402 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006403 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
6404 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006405 }
6406
6407 // need to not have any parents blocking us
6408 if (hasAncestorThatBlocksDescendantFocus()) {
6409 return false;
6410 }
6411
6412 handleFocusGainInternal(direction, previouslyFocusedRect);
6413 return true;
6414 }
6415
6416 /**
6417 * Call this to try to give focus to a specific view or to one of its descendants. This is a
6418 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
6419 * touch mode to request focus when they are touched.
6420 *
6421 * @return Whether this view or one of its descendants actually took focus.
6422 *
6423 * @see #isInTouchMode()
6424 *
6425 */
6426 public final boolean requestFocusFromTouch() {
6427 // Leave touch mode if we need to
6428 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006429 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07006430 if (viewRoot != null) {
6431 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006432 }
6433 }
6434 return requestFocus(View.FOCUS_DOWN);
6435 }
6436
6437 /**
6438 * @return Whether any ancestor of this view blocks descendant focus.
6439 */
6440 private boolean hasAncestorThatBlocksDescendantFocus() {
6441 ViewParent ancestor = mParent;
6442 while (ancestor instanceof ViewGroup) {
6443 final ViewGroup vgAncestor = (ViewGroup) ancestor;
6444 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
6445 return true;
6446 } else {
6447 ancestor = vgAncestor.getParent();
6448 }
6449 }
6450 return false;
6451 }
6452
6453 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006454 * Gets the mode for determining whether this View is important for accessibility
6455 * which is if it fires accessibility events and if it is reported to
6456 * accessibility services that query the screen.
6457 *
6458 * @return The mode for determining whether a View is important for accessibility.
6459 *
6460 * @attr ref android.R.styleable#View_importantForAccessibility
6461 *
6462 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6463 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6464 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6465 */
6466 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07006467 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_AUTO, to = "auto"),
6468 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_YES, to = "yes"),
6469 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_NO, to = "no")
Svetoslav Ganov42138042012-03-20 11:51:39 -07006470 })
6471 public int getImportantForAccessibility() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006472 return (mPrivateFlags2 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK)
6473 >> PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006474 }
6475
6476 /**
6477 * Sets how to determine whether this view is important for accessibility
6478 * which is if it fires accessibility events and if it is reported to
6479 * accessibility services that query the screen.
6480 *
6481 * @param mode How to determine whether this view is important for accessibility.
6482 *
6483 * @attr ref android.R.styleable#View_importantForAccessibility
6484 *
6485 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6486 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6487 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6488 */
6489 public void setImportantForAccessibility(int mode) {
6490 if (mode != getImportantForAccessibility()) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006491 mPrivateFlags2 &= ~PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK;
6492 mPrivateFlags2 |= (mode << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT)
6493 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006494 notifyAccessibilityStateChanged();
6495 }
6496 }
6497
6498 /**
6499 * Gets whether this view should be exposed for accessibility.
6500 *
6501 * @return Whether the view is exposed for accessibility.
6502 *
6503 * @hide
6504 */
6505 public boolean isImportantForAccessibility() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006506 final int mode = (mPrivateFlags2 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK)
6507 >> PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006508 switch (mode) {
6509 case IMPORTANT_FOR_ACCESSIBILITY_YES:
6510 return true;
6511 case IMPORTANT_FOR_ACCESSIBILITY_NO:
6512 return false;
6513 case IMPORTANT_FOR_ACCESSIBILITY_AUTO:
Svetoslav Ganov34caec92012-07-19 18:07:58 -07006514 return isActionableForAccessibility() || hasListenersForAccessibility()
6515 || getAccessibilityNodeProvider() != null;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006516 default:
6517 throw new IllegalArgumentException("Unknow important for accessibility mode: "
6518 + mode);
6519 }
6520 }
6521
6522 /**
6523 * Gets the parent for accessibility purposes. Note that the parent for
6524 * accessibility is not necessary the immediate parent. It is the first
6525 * predecessor that is important for accessibility.
6526 *
6527 * @return The parent for accessibility purposes.
6528 */
6529 public ViewParent getParentForAccessibility() {
6530 if (mParent instanceof View) {
6531 View parentView = (View) mParent;
6532 if (parentView.includeForAccessibility()) {
6533 return mParent;
6534 } else {
6535 return mParent.getParentForAccessibility();
6536 }
6537 }
6538 return null;
6539 }
6540
6541 /**
6542 * Adds the children of a given View for accessibility. Since some Views are
6543 * not important for accessibility the children for accessibility are not
6544 * necessarily direct children of the riew, rather they are the first level of
6545 * descendants important for accessibility.
6546 *
6547 * @param children The list of children for accessibility.
6548 */
6549 public void addChildrenForAccessibility(ArrayList<View> children) {
6550 if (includeForAccessibility()) {
6551 children.add(this);
6552 }
6553 }
6554
6555 /**
6556 * Whether to regard this view for accessibility. A view is regarded for
6557 * accessibility if it is important for accessibility or the querying
6558 * accessibility service has explicitly requested that view not
6559 * important for accessibility are regarded.
6560 *
6561 * @return Whether to regard the view for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006562 *
6563 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006564 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006565 public boolean includeForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006566 if (mAttachInfo != null) {
Romain Guyf0af1d52012-07-11 18:31:21 -07006567 return mAttachInfo.mIncludeNotImportantViews || isImportantForAccessibility();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006568 }
6569 return false;
6570 }
6571
6572 /**
6573 * Returns whether the View is considered actionable from
6574 * accessibility perspective. Such view are important for
6575 * accessiiblity.
6576 *
6577 * @return True if the view is actionable for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006578 *
6579 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006580 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006581 public boolean isActionableForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006582 return (isClickable() || isLongClickable() || isFocusable());
6583 }
6584
6585 /**
6586 * Returns whether the View has registered callbacks wich makes it
6587 * important for accessiiblity.
6588 *
6589 * @return True if the view is actionable for accessibility.
6590 */
6591 private boolean hasListenersForAccessibility() {
6592 ListenerInfo info = getListenerInfo();
6593 return mTouchDelegate != null || info.mOnKeyListener != null
6594 || info.mOnTouchListener != null || info.mOnGenericMotionListener != null
6595 || info.mOnHoverListener != null || info.mOnDragListener != null;
6596 }
6597
6598 /**
6599 * Notifies accessibility services that some view's important for
6600 * accessibility state has changed. Note that such notifications
6601 * are made at most once every
6602 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}
6603 * to avoid unnecessary load to the system. Also once a view has
6604 * made a notifucation this method is a NOP until the notification has
6605 * been sent to clients.
6606 *
6607 * @hide
6608 *
6609 * TODO: Makse sure this method is called for any view state change
6610 * that is interesting for accessilility purposes.
6611 */
6612 public void notifyAccessibilityStateChanged() {
Svetoslav Ganovc406be92012-05-11 16:12:32 -07006613 if (!AccessibilityManager.getInstance(mContext).isEnabled()) {
6614 return;
6615 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07006616 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_STATE_CHANGED) == 0) {
6617 mPrivateFlags2 |= PFLAG2_ACCESSIBILITY_STATE_CHANGED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006618 if (mParent != null) {
6619 mParent.childAccessibilityStateChanged(this);
6620 }
6621 }
6622 }
6623
6624 /**
6625 * Reset the state indicating the this view has requested clients
6626 * interested in its accessiblity state to be notified.
6627 *
6628 * @hide
6629 */
6630 public void resetAccessibilityStateChanged() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006631 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_STATE_CHANGED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006632 }
6633
6634 /**
6635 * Performs the specified accessibility action on the view. For
6636 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
alanv8eeefef2012-05-07 16:57:53 -07006637 * <p>
6638 * If an {@link AccessibilityDelegate} has been specified via calling
6639 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
6640 * {@link AccessibilityDelegate#performAccessibilityAction(View, int, Bundle)}
6641 * is responsible for handling this call.
6642 * </p>
Svetoslav Ganov42138042012-03-20 11:51:39 -07006643 *
6644 * @param action The action to perform.
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006645 * @param arguments Optional action arguments.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006646 * @return Whether the action was performed.
6647 */
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006648 public boolean performAccessibilityAction(int action, Bundle arguments) {
alanv8eeefef2012-05-07 16:57:53 -07006649 if (mAccessibilityDelegate != null) {
6650 return mAccessibilityDelegate.performAccessibilityAction(this, action, arguments);
6651 } else {
6652 return performAccessibilityActionInternal(action, arguments);
6653 }
6654 }
6655
6656 /**
6657 * @see #performAccessibilityAction(int, Bundle)
6658 *
6659 * Note: Called from the default {@link AccessibilityDelegate}.
6660 */
6661 boolean performAccessibilityActionInternal(int action, Bundle arguments) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006662 switch (action) {
6663 case AccessibilityNodeInfo.ACTION_CLICK: {
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006664 if (isClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006665 return performClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006666 }
6667 } break;
6668 case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
6669 if (isLongClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006670 return performLongClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006671 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006672 } break;
6673 case AccessibilityNodeInfo.ACTION_FOCUS: {
6674 if (!hasFocus()) {
6675 // Get out of touch mode since accessibility
6676 // wants to move focus around.
6677 getViewRootImpl().ensureTouchMode(false);
6678 return requestFocus();
6679 }
6680 } break;
6681 case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS: {
6682 if (hasFocus()) {
6683 clearFocus();
6684 return !isFocused();
6685 }
6686 } break;
6687 case AccessibilityNodeInfo.ACTION_SELECT: {
6688 if (!isSelected()) {
6689 setSelected(true);
6690 return isSelected();
6691 }
6692 } break;
6693 case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
6694 if (isSelected()) {
6695 setSelected(false);
6696 return !isSelected();
6697 }
6698 } break;
6699 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07006700 if (!isAccessibilityFocused()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006701 return requestAccessibilityFocus();
6702 }
6703 } break;
6704 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
6705 if (isAccessibilityFocused()) {
6706 clearAccessibilityFocus();
6707 return true;
6708 }
6709 } break;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006710 case AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY: {
6711 if (arguments != null) {
6712 final int granularity = arguments.getInt(
6713 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6714 return nextAtGranularity(granularity);
6715 }
6716 } break;
6717 case AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY: {
6718 if (arguments != null) {
6719 final int granularity = arguments.getInt(
6720 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6721 return previousAtGranularity(granularity);
6722 }
6723 } break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006724 }
6725 return false;
6726 }
6727
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006728 private boolean nextAtGranularity(int granularity) {
6729 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006730 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006731 return false;
6732 }
6733 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6734 if (iterator == null) {
6735 return false;
6736 }
6737 final int current = getAccessibilityCursorPosition();
6738 final int[] range = iterator.following(current);
6739 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006740 return false;
6741 }
6742 final int start = range[0];
6743 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006744 setAccessibilityCursorPosition(end);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006745 sendViewTextTraversedAtGranularityEvent(
6746 AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY,
6747 granularity, start, end);
6748 return true;
6749 }
6750
6751 private boolean previousAtGranularity(int granularity) {
6752 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006753 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006754 return false;
6755 }
6756 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6757 if (iterator == null) {
6758 return false;
6759 }
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006760 int current = getAccessibilityCursorPosition();
6761 if (current == ACCESSIBILITY_CURSOR_POSITION_UNDEFINED) {
6762 current = text.length();
6763 } else if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
6764 // When traversing by character we always put the cursor after the character
6765 // to ease edit and have to compensate before asking the for previous segment.
6766 current--;
6767 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006768 final int[] range = iterator.preceding(current);
6769 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006770 return false;
6771 }
6772 final int start = range[0];
6773 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006774 // Always put the cursor after the character to ease edit.
6775 if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
6776 setAccessibilityCursorPosition(end);
6777 } else {
6778 setAccessibilityCursorPosition(start);
6779 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006780 sendViewTextTraversedAtGranularityEvent(
6781 AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
6782 granularity, start, end);
6783 return true;
6784 }
6785
6786 /**
6787 * Gets the text reported for accessibility purposes.
6788 *
6789 * @return The accessibility text.
6790 *
6791 * @hide
6792 */
6793 public CharSequence getIterableTextForAccessibility() {
6794 return mContentDescription;
6795 }
6796
6797 /**
6798 * @hide
6799 */
6800 public int getAccessibilityCursorPosition() {
6801 return mAccessibilityCursorPosition;
6802 }
6803
6804 /**
6805 * @hide
6806 */
6807 public void setAccessibilityCursorPosition(int position) {
6808 mAccessibilityCursorPosition = position;
6809 }
6810
6811 private void sendViewTextTraversedAtGranularityEvent(int action, int granularity,
6812 int fromIndex, int toIndex) {
6813 if (mParent == null) {
6814 return;
6815 }
6816 AccessibilityEvent event = AccessibilityEvent.obtain(
6817 AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY);
6818 onInitializeAccessibilityEvent(event);
6819 onPopulateAccessibilityEvent(event);
6820 event.setFromIndex(fromIndex);
6821 event.setToIndex(toIndex);
6822 event.setAction(action);
6823 event.setMovementGranularity(granularity);
6824 mParent.requestSendAccessibilityEvent(this, event);
6825 }
6826
6827 /**
6828 * @hide
6829 */
6830 public TextSegmentIterator getIteratorForGranularity(int granularity) {
6831 switch (granularity) {
6832 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER: {
6833 CharSequence text = getIterableTextForAccessibility();
6834 if (text != null && text.length() > 0) {
6835 CharacterTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07006836 CharacterTextSegmentIterator.getInstance(
6837 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006838 iterator.initialize(text.toString());
6839 return iterator;
6840 }
6841 } break;
6842 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD: {
6843 CharSequence text = getIterableTextForAccessibility();
6844 if (text != null && text.length() > 0) {
6845 WordTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07006846 WordTextSegmentIterator.getInstance(
6847 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006848 iterator.initialize(text.toString());
6849 return iterator;
6850 }
6851 } break;
6852 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH: {
6853 CharSequence text = getIterableTextForAccessibility();
6854 if (text != null && text.length() > 0) {
6855 ParagraphTextSegmentIterator iterator =
6856 ParagraphTextSegmentIterator.getInstance();
6857 iterator.initialize(text.toString());
6858 return iterator;
6859 }
6860 } break;
6861 }
6862 return null;
6863 }
6864
Svetoslav Ganov42138042012-03-20 11:51:39 -07006865 /**
Romain Guya440b002010-02-24 15:57:54 -08006866 * @hide
6867 */
6868 public void dispatchStartTemporaryDetach() {
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07006869 clearAccessibilityFocus();
Romain Guy38c2ece2012-05-24 14:20:56 -07006870 clearDisplayList();
6871
Romain Guya440b002010-02-24 15:57:54 -08006872 onStartTemporaryDetach();
6873 }
6874
6875 /**
6876 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006877 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
6878 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08006879 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006880 */
6881 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08006882 removeUnsetPressCallback();
Dianne Hackborn4702a852012-08-17 15:18:29 -07006883 mPrivateFlags |= PFLAG_CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08006884 }
6885
6886 /**
6887 * @hide
6888 */
6889 public void dispatchFinishTemporaryDetach() {
6890 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006891 }
Romain Guy8506ab42009-06-11 17:35:47 -07006892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006893 /**
6894 * Called after {@link #onStartTemporaryDetach} when the container is done
6895 * changing the view.
6896 */
6897 public void onFinishTemporaryDetach() {
6898 }
Romain Guy8506ab42009-06-11 17:35:47 -07006899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006900 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006901 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
6902 * for this view's window. Returns null if the view is not currently attached
6903 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07006904 * just use the standard high-level event callbacks like
6905 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006906 */
6907 public KeyEvent.DispatcherState getKeyDispatcherState() {
6908 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
6909 }
Joe Malin32736f02011-01-19 16:14:20 -08006910
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006911 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006912 * Dispatch a key event before it is processed by any input method
6913 * associated with the view hierarchy. This can be used to intercept
6914 * key events in special situations before the IME consumes them; a
6915 * typical example would be handling the BACK key to update the application's
6916 * UI instead of allowing the IME to see it and close itself.
6917 *
6918 * @param event The key event to be dispatched.
6919 * @return True if the event was handled, false otherwise.
6920 */
6921 public boolean dispatchKeyEventPreIme(KeyEvent event) {
6922 return onKeyPreIme(event.getKeyCode(), event);
6923 }
6924
6925 /**
6926 * Dispatch a key event to the next view on the focus path. This path runs
6927 * from the top of the view tree down to the currently focused view. If this
6928 * view has focus, it will dispatch to itself. Otherwise it will dispatch
6929 * the next node down the focus path. This method also fires any key
6930 * listeners.
6931 *
6932 * @param event The key event to be dispatched.
6933 * @return True if the event was handled, false otherwise.
6934 */
6935 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006936 if (mInputEventConsistencyVerifier != null) {
6937 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
6938 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006939
Jeff Brown21bc5c92011-02-28 18:27:14 -08006940 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07006941 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006942 ListenerInfo li = mListenerInfo;
6943 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6944 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006945 return true;
6946 }
6947
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006948 if (event.dispatch(this, mAttachInfo != null
6949 ? mAttachInfo.mKeyDispatchState : null, this)) {
6950 return true;
6951 }
6952
6953 if (mInputEventConsistencyVerifier != null) {
6954 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6955 }
6956 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006957 }
6958
6959 /**
6960 * Dispatches a key shortcut event.
6961 *
6962 * @param event The key event to be dispatched.
6963 * @return True if the event was handled by the view, false otherwise.
6964 */
6965 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
6966 return onKeyShortcut(event.getKeyCode(), event);
6967 }
6968
6969 /**
6970 * Pass the touch screen motion event down to the target view, or this
6971 * view if it is the target.
6972 *
6973 * @param event The motion event to be dispatched.
6974 * @return True if the event was handled by the view, false otherwise.
6975 */
6976 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006977 if (mInputEventConsistencyVerifier != null) {
6978 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
6979 }
6980
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006981 if (onFilterTouchEventForSecurity(event)) {
6982 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006983 ListenerInfo li = mListenerInfo;
6984 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6985 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006986 return true;
6987 }
6988
6989 if (onTouchEvent(event)) {
6990 return true;
6991 }
Jeff Brown85a31762010-09-01 17:01:00 -07006992 }
6993
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006994 if (mInputEventConsistencyVerifier != null) {
6995 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006996 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006997 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006998 }
6999
7000 /**
Jeff Brown85a31762010-09-01 17:01:00 -07007001 * Filter the touch event to apply security policies.
7002 *
7003 * @param event The motion event to be filtered.
7004 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08007005 *
Jeff Brown85a31762010-09-01 17:01:00 -07007006 * @see #getFilterTouchesWhenObscured
7007 */
7008 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07007009 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07007010 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
7011 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
7012 // Window is obscured, drop this touch.
7013 return false;
7014 }
7015 return true;
7016 }
7017
7018 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007019 * Pass a trackball motion event down to the focused view.
7020 *
7021 * @param event The motion event to be dispatched.
7022 * @return True if the event was handled by the view, false otherwise.
7023 */
7024 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007025 if (mInputEventConsistencyVerifier != null) {
7026 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
7027 }
7028
Romain Guy02ccac62011-06-24 13:20:23 -07007029 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007030 }
7031
7032 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007033 * Dispatch a generic motion event.
7034 * <p>
7035 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7036 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08007037 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07007038 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007039 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08007040 *
7041 * @param event The motion event to be dispatched.
7042 * @return True if the event was handled by the view, false otherwise.
7043 */
7044 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007045 if (mInputEventConsistencyVerifier != null) {
7046 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
7047 }
7048
Jeff Browna032cc02011-03-07 16:56:21 -08007049 final int source = event.getSource();
7050 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
7051 final int action = event.getAction();
7052 if (action == MotionEvent.ACTION_HOVER_ENTER
7053 || action == MotionEvent.ACTION_HOVER_MOVE
7054 || action == MotionEvent.ACTION_HOVER_EXIT) {
7055 if (dispatchHoverEvent(event)) {
7056 return true;
7057 }
7058 } else if (dispatchGenericPointerEvent(event)) {
7059 return true;
7060 }
7061 } else if (dispatchGenericFocusedEvent(event)) {
7062 return true;
7063 }
7064
Jeff Brown10b62902011-06-20 16:40:37 -07007065 if (dispatchGenericMotionEventInternal(event)) {
7066 return true;
7067 }
7068
7069 if (mInputEventConsistencyVerifier != null) {
7070 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7071 }
7072 return false;
7073 }
7074
7075 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07007076 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007077 ListenerInfo li = mListenerInfo;
7078 if (li != null && li.mOnGenericMotionListener != null
7079 && (mViewFlags & ENABLED_MASK) == ENABLED
7080 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007081 return true;
7082 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007083
7084 if (onGenericMotionEvent(event)) {
7085 return true;
7086 }
7087
7088 if (mInputEventConsistencyVerifier != null) {
7089 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7090 }
7091 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08007092 }
7093
7094 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007095 * Dispatch a hover event.
7096 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007097 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07007098 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007099 * </p>
7100 *
7101 * @param event The motion event to be dispatched.
7102 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007103 */
7104 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07007105 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007106 ListenerInfo li = mListenerInfo;
7107 if (li != null && li.mOnHoverListener != null
7108 && (mViewFlags & ENABLED_MASK) == ENABLED
7109 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07007110 return true;
7111 }
7112
Jeff Browna032cc02011-03-07 16:56:21 -08007113 return onHoverEvent(event);
7114 }
7115
7116 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007117 * Returns true if the view has a child to which it has recently sent
7118 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
7119 * it does not have a hovered child, then it must be the innermost hovered view.
7120 * @hide
7121 */
7122 protected boolean hasHoveredChild() {
7123 return false;
7124 }
7125
7126 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007127 * Dispatch a generic motion event to the view under the first pointer.
7128 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007129 * Do not call this method directly.
7130 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007131 * </p>
7132 *
7133 * @param event The motion event to be dispatched.
7134 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007135 */
7136 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
7137 return false;
7138 }
7139
7140 /**
7141 * Dispatch a generic motion event to the currently focused view.
7142 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007143 * Do not call this method directly.
7144 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007145 * </p>
7146 *
7147 * @param event The motion event to be dispatched.
7148 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007149 */
7150 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
7151 return false;
7152 }
7153
7154 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007155 * Dispatch a pointer event.
7156 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007157 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
7158 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
7159 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08007160 * and should not be expected to handle other pointing device features.
7161 * </p>
7162 *
7163 * @param event The motion event to be dispatched.
7164 * @return True if the event was handled by the view, false otherwise.
7165 * @hide
7166 */
7167 public final boolean dispatchPointerEvent(MotionEvent event) {
7168 if (event.isTouchEvent()) {
7169 return dispatchTouchEvent(event);
7170 } else {
7171 return dispatchGenericMotionEvent(event);
7172 }
7173 }
7174
7175 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007176 * Called when the window containing this view gains or loses window focus.
7177 * ViewGroups should override to route to their children.
7178 *
7179 * @param hasFocus True if the window containing this view now has focus,
7180 * false otherwise.
7181 */
7182 public void dispatchWindowFocusChanged(boolean hasFocus) {
7183 onWindowFocusChanged(hasFocus);
7184 }
7185
7186 /**
7187 * Called when the window containing this view gains or loses focus. Note
7188 * that this is separate from view focus: to receive key events, both
7189 * your view and its window must have focus. If a window is displayed
7190 * on top of yours that takes input focus, then your own window will lose
7191 * focus but the view focus will remain unchanged.
7192 *
7193 * @param hasWindowFocus True if the window containing this view now has
7194 * focus, false otherwise.
7195 */
7196 public void onWindowFocusChanged(boolean hasWindowFocus) {
7197 InputMethodManager imm = InputMethodManager.peekInstance();
7198 if (!hasWindowFocus) {
7199 if (isPressed()) {
7200 setPressed(false);
7201 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07007202 if (imm != null && (mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007203 imm.focusOut(this);
7204 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007205 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08007206 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07007207 onFocusLost();
Dianne Hackborn4702a852012-08-17 15:18:29 -07007208 } else if (imm != null && (mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007209 imm.focusIn(this);
7210 }
7211 refreshDrawableState();
7212 }
7213
7214 /**
7215 * Returns true if this view is in a window that currently has window focus.
7216 * Note that this is not the same as the view itself having focus.
7217 *
7218 * @return True if this view is in a window that currently has window focus.
7219 */
7220 public boolean hasWindowFocus() {
7221 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
7222 }
7223
7224 /**
Adam Powell326d8082009-12-09 15:10:07 -08007225 * Dispatch a view visibility change down the view hierarchy.
7226 * ViewGroups should override to route to their children.
7227 * @param changedView The view whose visibility changed. Could be 'this' or
7228 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007229 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7230 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007231 */
7232 protected void dispatchVisibilityChanged(View changedView, int visibility) {
7233 onVisibilityChanged(changedView, visibility);
7234 }
7235
7236 /**
7237 * Called when the visibility of the view or an ancestor of the view is changed.
7238 * @param changedView The view whose visibility changed. Could be 'this' or
7239 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007240 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7241 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007242 */
7243 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007244 if (visibility == VISIBLE) {
7245 if (mAttachInfo != null) {
7246 initialAwakenScrollBars();
7247 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07007248 mPrivateFlags |= PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH;
Adam Powell8568c3a2010-04-19 14:26:11 -07007249 }
7250 }
Adam Powell326d8082009-12-09 15:10:07 -08007251 }
7252
7253 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08007254 * Dispatch a hint about whether this view is displayed. For instance, when
7255 * a View moves out of the screen, it might receives a display hint indicating
7256 * the view is not displayed. Applications should not <em>rely</em> on this hint
7257 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007258 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007259 * @param hint A hint about whether or not this view is displayed:
7260 * {@link #VISIBLE} or {@link #INVISIBLE}.
7261 */
7262 public void dispatchDisplayHint(int hint) {
7263 onDisplayHint(hint);
7264 }
7265
7266 /**
7267 * Gives this view a hint about whether is displayed or not. For instance, when
7268 * a View moves out of the screen, it might receives a display hint indicating
7269 * the view is not displayed. Applications should not <em>rely</em> on this hint
7270 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007271 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007272 * @param hint A hint about whether or not this view is displayed:
7273 * {@link #VISIBLE} or {@link #INVISIBLE}.
7274 */
7275 protected void onDisplayHint(int hint) {
7276 }
7277
7278 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007279 * Dispatch a window visibility change down the view hierarchy.
7280 * ViewGroups should override to route to their children.
7281 *
7282 * @param visibility The new visibility of the window.
7283 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007284 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007285 */
7286 public void dispatchWindowVisibilityChanged(int visibility) {
7287 onWindowVisibilityChanged(visibility);
7288 }
7289
7290 /**
7291 * Called when the window containing has change its visibility
7292 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
7293 * that this tells you whether or not your window is being made visible
7294 * to the window manager; this does <em>not</em> tell you whether or not
7295 * your window is obscured by other windows on the screen, even if it
7296 * is itself visible.
7297 *
7298 * @param visibility The new visibility of the window.
7299 */
7300 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007301 if (visibility == VISIBLE) {
7302 initialAwakenScrollBars();
7303 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007304 }
7305
7306 /**
7307 * Returns the current visibility of the window this view is attached to
7308 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
7309 *
7310 * @return Returns the current visibility of the view's window.
7311 */
7312 public int getWindowVisibility() {
7313 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
7314 }
7315
7316 /**
7317 * Retrieve the overall visible display size in which the window this view is
7318 * attached to has been positioned in. This takes into account screen
7319 * decorations above the window, for both cases where the window itself
7320 * is being position inside of them or the window is being placed under
7321 * then and covered insets are used for the window to position its content
7322 * inside. In effect, this tells you the available area where content can
7323 * be placed and remain visible to users.
7324 *
7325 * <p>This function requires an IPC back to the window manager to retrieve
7326 * the requested information, so should not be used in performance critical
7327 * code like drawing.
7328 *
7329 * @param outRect Filled in with the visible display frame. If the view
7330 * is not attached to a window, this is simply the raw display size.
7331 */
7332 public void getWindowVisibleDisplayFrame(Rect outRect) {
7333 if (mAttachInfo != null) {
7334 try {
7335 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
7336 } catch (RemoteException e) {
7337 return;
7338 }
7339 // XXX This is really broken, and probably all needs to be done
7340 // in the window manager, and we need to know more about whether
7341 // we want the area behind or in front of the IME.
7342 final Rect insets = mAttachInfo.mVisibleInsets;
7343 outRect.left += insets.left;
7344 outRect.top += insets.top;
7345 outRect.right -= insets.right;
7346 outRect.bottom -= insets.bottom;
7347 return;
7348 }
7349 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07007350 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007351 }
7352
7353 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007354 * Dispatch a notification about a resource configuration change down
7355 * the view hierarchy.
7356 * ViewGroups should override to route to their children.
7357 *
7358 * @param newConfig The new resource configuration.
7359 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007360 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007361 */
7362 public void dispatchConfigurationChanged(Configuration newConfig) {
7363 onConfigurationChanged(newConfig);
7364 }
7365
7366 /**
7367 * Called when the current configuration of the resources being used
7368 * by the application have changed. You can use this to decide when
7369 * to reload resources that can changed based on orientation and other
7370 * configuration characterstics. You only need to use this if you are
7371 * not relying on the normal {@link android.app.Activity} mechanism of
7372 * recreating the activity instance upon a configuration change.
7373 *
7374 * @param newConfig The new resource configuration.
7375 */
7376 protected void onConfigurationChanged(Configuration newConfig) {
7377 }
7378
7379 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007380 * Private function to aggregate all per-view attributes in to the view
7381 * root.
7382 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007383 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7384 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007385 }
7386
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007387 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7388 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08007389 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007390 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007391 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007392 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007393 ListenerInfo li = mListenerInfo;
7394 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007395 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007396 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007397 }
7398 }
7399
7400 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08007401 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007402 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08007403 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
7404 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007405 ai.mRecomputeGlobalAttributes = true;
7406 }
7407 }
7408 }
7409
7410 /**
7411 * Returns whether the device is currently in touch mode. Touch mode is entered
7412 * once the user begins interacting with the device by touch, and affects various
7413 * things like whether focus is always visible to the user.
7414 *
7415 * @return Whether the device is in touch mode.
7416 */
7417 @ViewDebug.ExportedProperty
7418 public boolean isInTouchMode() {
7419 if (mAttachInfo != null) {
7420 return mAttachInfo.mInTouchMode;
7421 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07007422 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007423 }
7424 }
7425
7426 /**
7427 * Returns the context the view is running in, through which it can
7428 * access the current theme, resources, etc.
7429 *
7430 * @return The view's Context.
7431 */
7432 @ViewDebug.CapturedViewProperty
7433 public final Context getContext() {
7434 return mContext;
7435 }
7436
7437 /**
7438 * Handle a key event before it is processed by any input method
7439 * associated with the view hierarchy. This can be used to intercept
7440 * key events in special situations before the IME consumes them; a
7441 * typical example would be handling the BACK key to update the application's
7442 * UI instead of allowing the IME to see it and close itself.
7443 *
7444 * @param keyCode The value in event.getKeyCode().
7445 * @param event Description of the key event.
7446 * @return If you handled the event, return true. If you want to allow the
7447 * event to be handled by the next receiver, return false.
7448 */
7449 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7450 return false;
7451 }
7452
7453 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007454 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7455 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007456 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7457 * is released, if the view is enabled and clickable.
7458 *
Jean Chalard405bc512012-05-29 19:12:34 +09007459 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7460 * although some may elect to do so in some situations. Do not rely on this to
7461 * catch software key presses.
7462 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007463 * @param keyCode A key code that represents the button pressed, from
7464 * {@link android.view.KeyEvent}.
7465 * @param event The KeyEvent object that defines the button action.
7466 */
7467 public boolean onKeyDown(int keyCode, KeyEvent event) {
7468 boolean result = false;
7469
7470 switch (keyCode) {
7471 case KeyEvent.KEYCODE_DPAD_CENTER:
7472 case KeyEvent.KEYCODE_ENTER: {
7473 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7474 return true;
7475 }
7476 // Long clickable items don't necessarily have to be clickable
7477 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7478 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7479 (event.getRepeatCount() == 0)) {
7480 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007481 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007482 return true;
7483 }
7484 break;
7485 }
7486 }
7487 return result;
7488 }
7489
7490 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007491 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7492 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7493 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007494 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7495 * although some may elect to do so in some situations. Do not rely on this to
7496 * catch software key presses.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007497 */
7498 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7499 return false;
7500 }
7501
7502 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007503 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7504 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007505 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7506 * {@link KeyEvent#KEYCODE_ENTER} is released.
Jean Chalard405bc512012-05-29 19:12:34 +09007507 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7508 * although some may elect to do so in some situations. Do not rely on this to
7509 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007510 *
7511 * @param keyCode A key code that represents the button pressed, from
7512 * {@link android.view.KeyEvent}.
7513 * @param event The KeyEvent object that defines the button action.
7514 */
7515 public boolean onKeyUp(int keyCode, KeyEvent event) {
7516 boolean result = false;
7517
7518 switch (keyCode) {
7519 case KeyEvent.KEYCODE_DPAD_CENTER:
7520 case KeyEvent.KEYCODE_ENTER: {
7521 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7522 return true;
7523 }
7524 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7525 setPressed(false);
7526
7527 if (!mHasPerformedLongPress) {
7528 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007529 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007530
7531 result = performClick();
7532 }
7533 }
7534 break;
7535 }
7536 }
7537 return result;
7538 }
7539
7540 /**
7541 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7542 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7543 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007544 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7545 * although some may elect to do so in some situations. Do not rely on this to
7546 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007547 *
7548 * @param keyCode A key code that represents the button pressed, from
7549 * {@link android.view.KeyEvent}.
7550 * @param repeatCount The number of times the action was made.
7551 * @param event The KeyEvent object that defines the button action.
7552 */
7553 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7554 return false;
7555 }
7556
7557 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007558 * Called on the focused view when a key shortcut event is not handled.
7559 * Override this method to implement local key shortcuts for the View.
7560 * Key shortcuts can also be implemented by setting the
7561 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007562 *
7563 * @param keyCode The value in event.getKeyCode().
7564 * @param event Description of the key event.
7565 * @return If you handled the event, return true. If you want to allow the
7566 * event to be handled by the next receiver, return false.
7567 */
7568 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7569 return false;
7570 }
7571
7572 /**
7573 * Check whether the called view is a text editor, in which case it
7574 * would make sense to automatically display a soft input window for
7575 * it. Subclasses should override this if they implement
7576 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007577 * a call on that method would return a non-null InputConnection, and
7578 * they are really a first-class editor that the user would normally
7579 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007580 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007581 * <p>The default implementation always returns false. This does
7582 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7583 * will not be called or the user can not otherwise perform edits on your
7584 * view; it is just a hint to the system that this is not the primary
7585 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007586 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007587 * @return Returns true if this view is a text editor, else false.
7588 */
7589 public boolean onCheckIsTextEditor() {
7590 return false;
7591 }
Romain Guy8506ab42009-06-11 17:35:47 -07007592
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007593 /**
7594 * Create a new InputConnection for an InputMethod to interact
7595 * with the view. The default implementation returns null, since it doesn't
7596 * support input methods. You can override this to implement such support.
7597 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007598 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007599 * <p>When implementing this, you probably also want to implement
7600 * {@link #onCheckIsTextEditor()} to indicate you will return a
7601 * non-null InputConnection.
7602 *
7603 * @param outAttrs Fill in with attribute information about the connection.
7604 */
7605 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7606 return null;
7607 }
7608
7609 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007610 * Called by the {@link android.view.inputmethod.InputMethodManager}
7611 * when a view who is not the current
7612 * input connection target is trying to make a call on the manager. The
7613 * default implementation returns false; you can override this to return
7614 * true for certain views if you are performing InputConnection proxying
7615 * to them.
7616 * @param view The View that is making the InputMethodManager call.
7617 * @return Return true to allow the call, false to reject.
7618 */
7619 public boolean checkInputConnectionProxy(View view) {
7620 return false;
7621 }
Romain Guy8506ab42009-06-11 17:35:47 -07007622
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007623 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007624 * Show the context menu for this view. It is not safe to hold on to the
7625 * menu after returning from this method.
7626 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007627 * You should normally not overload this method. Overload
7628 * {@link #onCreateContextMenu(ContextMenu)} or define an
7629 * {@link OnCreateContextMenuListener} to add items to the context menu.
7630 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007631 * @param menu The context menu to populate
7632 */
7633 public void createContextMenu(ContextMenu menu) {
7634 ContextMenuInfo menuInfo = getContextMenuInfo();
7635
7636 // Sets the current menu info so all items added to menu will have
7637 // my extra info set.
7638 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7639
7640 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007641 ListenerInfo li = mListenerInfo;
7642 if (li != null && li.mOnCreateContextMenuListener != null) {
7643 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007644 }
7645
7646 // Clear the extra information so subsequent items that aren't mine don't
7647 // have my extra info.
7648 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7649
7650 if (mParent != null) {
7651 mParent.createContextMenu(menu);
7652 }
7653 }
7654
7655 /**
7656 * Views should implement this if they have extra information to associate
7657 * with the context menu. The return result is supplied as a parameter to
7658 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7659 * callback.
7660 *
7661 * @return Extra information about the item for which the context menu
7662 * should be shown. This information will vary across different
7663 * subclasses of View.
7664 */
7665 protected ContextMenuInfo getContextMenuInfo() {
7666 return null;
7667 }
7668
7669 /**
7670 * Views should implement this if the view itself is going to add items to
7671 * the context menu.
7672 *
7673 * @param menu the context menu to populate
7674 */
7675 protected void onCreateContextMenu(ContextMenu menu) {
7676 }
7677
7678 /**
7679 * Implement this method to handle trackball motion events. The
7680 * <em>relative</em> movement of the trackball since the last event
7681 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
7682 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
7683 * that a movement of 1 corresponds to the user pressing one DPAD key (so
7684 * they will often be fractional values, representing the more fine-grained
7685 * movement information available from a trackball).
7686 *
7687 * @param event The motion event.
7688 * @return True if the event was handled, false otherwise.
7689 */
7690 public boolean onTrackballEvent(MotionEvent event) {
7691 return false;
7692 }
7693
7694 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08007695 * Implement this method to handle generic motion events.
7696 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08007697 * Generic motion events describe joystick movements, mouse hovers, track pad
7698 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08007699 * {@link MotionEvent#getSource() source} of the motion event specifies
7700 * the class of input that was received. Implementations of this method
7701 * must examine the bits in the source before processing the event.
7702 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007703 * </p><p>
7704 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7705 * are delivered to the view under the pointer. All other generic motion events are
7706 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08007707 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07007708 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08007709 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007710 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
7711 * // process the joystick movement...
7712 * return true;
7713 * }
7714 * }
7715 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
7716 * switch (event.getAction()) {
7717 * case MotionEvent.ACTION_HOVER_MOVE:
7718 * // process the mouse hover movement...
7719 * return true;
7720 * case MotionEvent.ACTION_SCROLL:
7721 * // process the scroll wheel movement...
7722 * return true;
7723 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08007724 * }
7725 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07007726 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08007727 *
7728 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08007729 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08007730 */
7731 public boolean onGenericMotionEvent(MotionEvent event) {
7732 return false;
7733 }
7734
7735 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007736 * Implement this method to handle hover events.
7737 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07007738 * This method is called whenever a pointer is hovering into, over, or out of the
7739 * bounds of a view and the view is not currently being touched.
7740 * Hover events are represented as pointer events with action
7741 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
7742 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
7743 * </p>
7744 * <ul>
7745 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
7746 * when the pointer enters the bounds of the view.</li>
7747 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
7748 * when the pointer has already entered the bounds of the view and has moved.</li>
7749 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
7750 * when the pointer has exited the bounds of the view or when the pointer is
7751 * about to go down due to a button click, tap, or similar user action that
7752 * causes the view to be touched.</li>
7753 * </ul>
7754 * <p>
7755 * The view should implement this method to return true to indicate that it is
7756 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08007757 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07007758 * The default implementation calls {@link #setHovered} to update the hovered state
7759 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07007760 * is enabled and is clickable. The default implementation also sends hover
7761 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08007762 * </p>
7763 *
7764 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07007765 * @return True if the view handled the hover event.
7766 *
7767 * @see #isHovered
7768 * @see #setHovered
7769 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007770 */
7771 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007772 // The root view may receive hover (or touch) events that are outside the bounds of
7773 // the window. This code ensures that we only send accessibility events for
7774 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07007775 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007776 if (!mSendingHoverAccessibilityEvents) {
7777 if ((action == MotionEvent.ACTION_HOVER_ENTER
7778 || action == MotionEvent.ACTION_HOVER_MOVE)
7779 && !hasHoveredChild()
7780 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07007781 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007782 mSendingHoverAccessibilityEvents = true;
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007783 }
7784 } else {
7785 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07007786 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007787 && !pointInView(event.getX(), event.getY()))) {
7788 mSendingHoverAccessibilityEvents = false;
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07007789 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007790 // If the window does not have input focus we take away accessibility
7791 // focus as soon as the user stop hovering over the view.
Jeff Brown59a422e2012-04-19 15:19:19 -07007792 if (mAttachInfo != null && !mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07007793 getViewRootImpl().setAccessibilityFocus(null, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007794 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007795 }
Jeff Browna1b24182011-07-28 13:38:24 -07007796 }
7797
Jeff Brown87b7f802011-06-21 18:35:45 -07007798 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007799 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07007800 case MotionEvent.ACTION_HOVER_ENTER:
7801 setHovered(true);
7802 break;
7803 case MotionEvent.ACTION_HOVER_EXIT:
7804 setHovered(false);
7805 break;
7806 }
Jeff Browna1b24182011-07-28 13:38:24 -07007807
7808 // Dispatch the event to onGenericMotionEvent before returning true.
7809 // This is to provide compatibility with existing applications that
7810 // handled HOVER_MOVE events in onGenericMotionEvent and that would
7811 // break because of the new default handling for hoverable views
7812 // in onHoverEvent.
7813 // Note that onGenericMotionEvent will be called by default when
7814 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
7815 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07007816 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08007817 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007818
Svetoslav Ganov736c2752011-04-22 18:30:36 -07007819 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08007820 }
7821
7822 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007823 * Returns true if the view should handle {@link #onHoverEvent}
7824 * by calling {@link #setHovered} to change its hovered state.
7825 *
7826 * @return True if the view is hoverable.
7827 */
7828 private boolean isHoverable() {
7829 final int viewFlags = mViewFlags;
7830 if ((viewFlags & ENABLED_MASK) == DISABLED) {
7831 return false;
7832 }
7833
7834 return (viewFlags & CLICKABLE) == CLICKABLE
7835 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
7836 }
7837
7838 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007839 * Returns true if the view is currently hovered.
7840 *
7841 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007842 *
7843 * @see #setHovered
7844 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007845 */
Jeff Brown10b62902011-06-20 16:40:37 -07007846 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08007847 public boolean isHovered() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07007848 return (mPrivateFlags & PFLAG_HOVERED) != 0;
Jeff Browna032cc02011-03-07 16:56:21 -08007849 }
7850
7851 /**
7852 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007853 * <p>
7854 * Calling this method also changes the drawable state of the view. This
7855 * enables the view to react to hover by using different drawable resources
7856 * to change its appearance.
7857 * </p><p>
7858 * The {@link #onHoverChanged} method is called when the hovered state changes.
7859 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08007860 *
7861 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007862 *
7863 * @see #isHovered
7864 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007865 */
7866 public void setHovered(boolean hovered) {
7867 if (hovered) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07007868 if ((mPrivateFlags & PFLAG_HOVERED) == 0) {
7869 mPrivateFlags |= PFLAG_HOVERED;
Jeff Browna032cc02011-03-07 16:56:21 -08007870 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007871 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08007872 }
7873 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07007874 if ((mPrivateFlags & PFLAG_HOVERED) != 0) {
7875 mPrivateFlags &= ~PFLAG_HOVERED;
Jeff Browna032cc02011-03-07 16:56:21 -08007876 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007877 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08007878 }
7879 }
7880 }
7881
7882 /**
Jeff Brown10b62902011-06-20 16:40:37 -07007883 * Implement this method to handle hover state changes.
7884 * <p>
7885 * This method is called whenever the hover state changes as a result of a
7886 * call to {@link #setHovered}.
7887 * </p>
7888 *
7889 * @param hovered The current hover state, as returned by {@link #isHovered}.
7890 *
7891 * @see #isHovered
7892 * @see #setHovered
7893 */
7894 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07007895 }
7896
7897 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007898 * Implement this method to handle touch screen motion events.
7899 *
7900 * @param event The motion event.
7901 * @return True if the event was handled, false otherwise.
7902 */
7903 public boolean onTouchEvent(MotionEvent event) {
7904 final int viewFlags = mViewFlags;
7905
7906 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07007907 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PFLAG_PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08007908 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007909 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007910 // A disabled view that is clickable still consumes the touch
7911 // events, it just doesn't respond to them.
7912 return (((viewFlags & CLICKABLE) == CLICKABLE ||
7913 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
7914 }
7915
7916 if (mTouchDelegate != null) {
7917 if (mTouchDelegate.onTouchEvent(event)) {
7918 return true;
7919 }
7920 }
7921
7922 if (((viewFlags & CLICKABLE) == CLICKABLE ||
7923 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
7924 switch (event.getAction()) {
7925 case MotionEvent.ACTION_UP:
Dianne Hackborn4702a852012-08-17 15:18:29 -07007926 boolean prepressed = (mPrivateFlags & PFLAG_PREPRESSED) != 0;
7927 if ((mPrivateFlags & PFLAG_PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007928 // take focus if we don't have it already and we should in
7929 // touch mode.
7930 boolean focusTaken = false;
7931 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
7932 focusTaken = requestFocus();
7933 }
7934
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007935 if (prepressed) {
7936 // The button is being released before we actually
7937 // showed it as pressed. Make it show the pressed
7938 // state now (before scheduling the click) to ensure
7939 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08007940 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007941 }
Joe Malin32736f02011-01-19 16:14:20 -08007942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007943 if (!mHasPerformedLongPress) {
7944 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007945 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007946
7947 // Only perform take click actions if we were in the pressed state
7948 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08007949 // Use a Runnable and post this rather than calling
7950 // performClick directly. This lets other visual state
7951 // of the view update before click actions start.
7952 if (mPerformClick == null) {
7953 mPerformClick = new PerformClick();
7954 }
7955 if (!post(mPerformClick)) {
7956 performClick();
7957 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007958 }
7959 }
7960
7961 if (mUnsetPressedState == null) {
7962 mUnsetPressedState = new UnsetPressedState();
7963 }
7964
Adam Powelle14579b2009-12-16 18:39:52 -08007965 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08007966 postDelayed(mUnsetPressedState,
7967 ViewConfiguration.getPressedStateDuration());
7968 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007969 // If the post failed, unpress right now
7970 mUnsetPressedState.run();
7971 }
Adam Powelle14579b2009-12-16 18:39:52 -08007972 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007973 }
7974 break;
7975
7976 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08007977 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007978
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07007979 if (performButtonActionOnTouchDown(event)) {
7980 break;
7981 }
7982
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007983 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07007984 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007985
7986 // For views inside a scrolling container, delay the pressed feedback for
7987 // a short period in case this is a scroll.
7988 if (isInScrollingContainer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07007989 mPrivateFlags |= PFLAG_PREPRESSED;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007990 if (mPendingCheckForTap == null) {
7991 mPendingCheckForTap = new CheckForTap();
7992 }
7993 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
7994 } else {
7995 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08007996 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007997 checkForLongClick(0);
7998 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007999 break;
8000
8001 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08008002 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08008003 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008004 break;
8005
8006 case MotionEvent.ACTION_MOVE:
8007 final int x = (int) event.getX();
8008 final int y = (int) event.getY();
8009
8010 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07008011 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008012 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08008013 removeTapCallback();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008014 if ((mPrivateFlags & PFLAG_PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08008015 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05008016 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008017
Adam Powell4d6f0662012-02-21 15:11:11 -08008018 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008019 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008020 }
8021 break;
8022 }
8023 return true;
8024 }
8025
8026 return false;
8027 }
8028
8029 /**
Adam Powell10298662011-08-14 18:26:30 -07008030 * @hide
8031 */
8032 public boolean isInScrollingContainer() {
8033 ViewParent p = getParent();
8034 while (p != null && p instanceof ViewGroup) {
8035 if (((ViewGroup) p).shouldDelayChildPressedState()) {
8036 return true;
8037 }
8038 p = p.getParent();
8039 }
8040 return false;
8041 }
8042
8043 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05008044 * Remove the longpress detection timer.
8045 */
8046 private void removeLongPressCallback() {
8047 if (mPendingCheckForLongPress != null) {
8048 removeCallbacks(mPendingCheckForLongPress);
8049 }
8050 }
Adam Powell3cb8b632011-01-21 15:34:14 -08008051
8052 /**
8053 * Remove the pending click action
8054 */
8055 private void removePerformClickCallback() {
8056 if (mPerformClick != null) {
8057 removeCallbacks(mPerformClick);
8058 }
8059 }
8060
Adam Powelle14579b2009-12-16 18:39:52 -08008061 /**
Romain Guya440b002010-02-24 15:57:54 -08008062 * Remove the prepress detection timer.
8063 */
8064 private void removeUnsetPressCallback() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008065 if ((mPrivateFlags & PFLAG_PRESSED) != 0 && mUnsetPressedState != null) {
Romain Guya440b002010-02-24 15:57:54 -08008066 setPressed(false);
8067 removeCallbacks(mUnsetPressedState);
8068 }
8069 }
8070
8071 /**
Adam Powelle14579b2009-12-16 18:39:52 -08008072 * Remove the tap detection timer.
8073 */
8074 private void removeTapCallback() {
8075 if (mPendingCheckForTap != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008076 mPrivateFlags &= ~PFLAG_PREPRESSED;
Adam Powelle14579b2009-12-16 18:39:52 -08008077 removeCallbacks(mPendingCheckForTap);
8078 }
8079 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05008080
8081 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008082 * Cancels a pending long press. Your subclass can use this if you
8083 * want the context menu to come up if the user presses and holds
8084 * at the same place, but you don't want it to come up if they press
8085 * and then move around enough to cause scrolling.
8086 */
8087 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05008088 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08008089
8090 /*
8091 * The prepressed state handled by the tap callback is a display
8092 * construct, but the tap callback will post a long press callback
8093 * less its own timeout. Remove it here.
8094 */
8095 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008096 }
8097
8098 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008099 * Remove the pending callback for sending a
8100 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
8101 */
8102 private void removeSendViewScrolledAccessibilityEventCallback() {
8103 if (mSendViewScrolledAccessibilityEvent != null) {
8104 removeCallbacks(mSendViewScrolledAccessibilityEvent);
Svetoslav Ganov4a812ae2012-05-29 16:46:10 -07008105 mSendViewScrolledAccessibilityEvent.mIsPending = false;
Svetoslav Ganova0156172011-06-26 17:55:44 -07008106 }
8107 }
8108
8109 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008110 * Sets the TouchDelegate for this View.
8111 */
8112 public void setTouchDelegate(TouchDelegate delegate) {
8113 mTouchDelegate = delegate;
8114 }
8115
8116 /**
8117 * Gets the TouchDelegate for this View.
8118 */
8119 public TouchDelegate getTouchDelegate() {
8120 return mTouchDelegate;
8121 }
8122
8123 /**
8124 * Set flags controlling behavior of this view.
8125 *
8126 * @param flags Constant indicating the value which should be set
8127 * @param mask Constant indicating the bit range that should be changed
8128 */
8129 void setFlags(int flags, int mask) {
8130 int old = mViewFlags;
8131 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
8132
8133 int changed = mViewFlags ^ old;
8134 if (changed == 0) {
8135 return;
8136 }
8137 int privateFlags = mPrivateFlags;
8138
8139 /* Check if the FOCUSABLE bit has changed */
8140 if (((changed & FOCUSABLE_MASK) != 0) &&
Dianne Hackborn4702a852012-08-17 15:18:29 -07008141 ((privateFlags & PFLAG_HAS_BOUNDS) !=0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008142 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
Dianne Hackborn4702a852012-08-17 15:18:29 -07008143 && ((privateFlags & PFLAG_FOCUSED) != 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008144 /* Give up focus if we are no longer focusable */
8145 clearFocus();
8146 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
Dianne Hackborn4702a852012-08-17 15:18:29 -07008147 && ((privateFlags & PFLAG_FOCUSED) == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008148 /*
8149 * Tell the view system that we are now available to take focus
8150 * if no one else already has it.
8151 */
8152 if (mParent != null) mParent.focusableViewAvailable(this);
8153 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008154 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8155 notifyAccessibilityStateChanged();
8156 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008157 }
8158
8159 if ((flags & VISIBILITY_MASK) == VISIBLE) {
8160 if ((changed & VISIBILITY_MASK) != 0) {
8161 /*
Chet Haase4324ead2011-08-24 21:31:03 -07008162 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07008163 * it was not visible. Marking it drawn ensures that the invalidation will
8164 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008165 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07008166 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07008167 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008168
8169 needGlobalAttributesUpdate(true);
8170
8171 // a view becoming visible is worth notifying the parent
8172 // about in case nothing has focus. even if this specific view
8173 // isn't focusable, it may contain something that is, so let
8174 // the root view try to give this focus if nothing else does.
8175 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
8176 mParent.focusableViewAvailable(this);
8177 }
8178 }
8179 }
8180
8181 /* Check if the GONE bit has changed */
8182 if ((changed & GONE) != 0) {
8183 needGlobalAttributesUpdate(false);
8184 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008185
Romain Guyecd80ee2009-12-03 17:13:02 -08008186 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
8187 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008188 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08008189 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07008190 if (mParent instanceof View) {
8191 // GONE views noop invalidation, so invalidate the parent
8192 ((View) mParent).invalidate(true);
8193 }
8194 // Mark the view drawn to ensure that it gets invalidated properly the next
8195 // time it is visible and gets invalidated
Dianne Hackborn4702a852012-08-17 15:18:29 -07008196 mPrivateFlags |= PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008197 }
8198 if (mAttachInfo != null) {
8199 mAttachInfo.mViewVisibilityChanged = true;
8200 }
8201 }
8202
8203 /* Check if the VISIBLE bit has changed */
8204 if ((changed & INVISIBLE) != 0) {
8205 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07008206 /*
8207 * If this view is becoming invisible, set the DRAWN flag so that
8208 * the next invalidate() will not be skipped.
8209 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07008210 mPrivateFlags |= PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008211
8212 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008213 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008214 if (getRootView() != this) {
8215 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008216 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008217 }
8218 }
8219 if (mAttachInfo != null) {
8220 mAttachInfo.mViewVisibilityChanged = true;
8221 }
8222 }
8223
Adam Powell326d8082009-12-09 15:10:07 -08008224 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07008225 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08008226 ((ViewGroup) mParent).onChildVisibilityChanged(this,
8227 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08008228 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07008229 } else if (mParent != null) {
8230 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07008231 }
Adam Powell326d8082009-12-09 15:10:07 -08008232 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
8233 }
8234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008235 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
8236 destroyDrawingCache();
8237 }
8238
8239 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
8240 destroyDrawingCache();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008241 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008242 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008243 }
8244
8245 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
8246 destroyDrawingCache();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008247 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008248 }
8249
8250 if ((changed & DRAW_MASK) != 0) {
8251 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07008252 if (mBackground != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008253 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
8254 mPrivateFlags |= PFLAG_ONLY_DRAWS_BACKGROUND;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008255 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008256 mPrivateFlags |= PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008257 }
8258 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008259 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008260 }
8261 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08008262 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008263 }
8264
8265 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08008266 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008267 mParent.recomputeViewAttributes(this);
8268 }
8269 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008270
8271 if (AccessibilityManager.getInstance(mContext).isEnabled()
8272 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
8273 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
8274 notifyAccessibilityStateChanged();
8275 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008276 }
8277
8278 /**
8279 * Change the view's z order in the tree, so it's on top of other sibling
8280 * views
8281 */
8282 public void bringToFront() {
8283 if (mParent != null) {
8284 mParent.bringChildToFront(this);
8285 }
8286 }
8287
8288 /**
8289 * This is called in response to an internal scroll in this view (i.e., the
8290 * view scrolled its own contents). This is typically as a result of
8291 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
8292 * called.
8293 *
8294 * @param l Current horizontal scroll origin.
8295 * @param t Current vertical scroll origin.
8296 * @param oldl Previous horizontal scroll origin.
8297 * @param oldt Previous vertical scroll origin.
8298 */
8299 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07008300 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8301 postSendViewScrolledAccessibilityEventCallback();
8302 }
8303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008304 mBackgroundSizeChanged = true;
8305
8306 final AttachInfo ai = mAttachInfo;
8307 if (ai != null) {
8308 ai.mViewScrollChanged = true;
8309 }
8310 }
8311
8312 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008313 * Interface definition for a callback to be invoked when the layout bounds of a view
8314 * changes due to layout processing.
8315 */
8316 public interface OnLayoutChangeListener {
8317 /**
8318 * Called when the focus state of a view has changed.
8319 *
8320 * @param v The view whose state has changed.
8321 * @param left The new value of the view's left property.
8322 * @param top The new value of the view's top property.
8323 * @param right The new value of the view's right property.
8324 * @param bottom The new value of the view's bottom property.
8325 * @param oldLeft The previous value of the view's left property.
8326 * @param oldTop The previous value of the view's top property.
8327 * @param oldRight The previous value of the view's right property.
8328 * @param oldBottom The previous value of the view's bottom property.
8329 */
8330 void onLayoutChange(View v, int left, int top, int right, int bottom,
8331 int oldLeft, int oldTop, int oldRight, int oldBottom);
8332 }
8333
8334 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008335 * This is called during layout when the size of this view has changed. If
8336 * you were just added to the view hierarchy, you're called with the old
8337 * values of 0.
8338 *
8339 * @param w Current width of this view.
8340 * @param h Current height of this view.
8341 * @param oldw Old width of this view.
8342 * @param oldh Old height of this view.
8343 */
8344 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
8345 }
8346
8347 /**
8348 * Called by draw to draw the child views. This may be overridden
8349 * by derived classes to gain control just before its children are drawn
8350 * (but after its own view has been drawn).
8351 * @param canvas the canvas on which to draw the view
8352 */
8353 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008355 }
8356
8357 /**
8358 * Gets the parent of this view. Note that the parent is a
8359 * ViewParent and not necessarily a View.
8360 *
8361 * @return Parent of this view.
8362 */
8363 public final ViewParent getParent() {
8364 return mParent;
8365 }
8366
8367 /**
Chet Haasecca2c982011-05-20 14:34:18 -07008368 * Set the horizontal scrolled position of your view. This will cause a call to
8369 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8370 * invalidated.
8371 * @param value the x position to scroll to
8372 */
8373 public void setScrollX(int value) {
8374 scrollTo(value, mScrollY);
8375 }
8376
8377 /**
8378 * Set the vertical scrolled position of your view. This will cause a call to
8379 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8380 * invalidated.
8381 * @param value the y position to scroll to
8382 */
8383 public void setScrollY(int value) {
8384 scrollTo(mScrollX, value);
8385 }
8386
8387 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008388 * Return the scrolled left position of this view. This is the left edge of
8389 * the displayed part of your view. You do not need to draw any pixels
8390 * farther left, since those are outside of the frame of your view on
8391 * screen.
8392 *
8393 * @return The left edge of the displayed part of your view, in pixels.
8394 */
8395 public final int getScrollX() {
8396 return mScrollX;
8397 }
8398
8399 /**
8400 * Return the scrolled top position of this view. This is the top edge of
8401 * the displayed part of your view. You do not need to draw any pixels above
8402 * it, since those are outside of the frame of your view on screen.
8403 *
8404 * @return The top edge of the displayed part of your view, in pixels.
8405 */
8406 public final int getScrollY() {
8407 return mScrollY;
8408 }
8409
8410 /**
8411 * Return the width of the your view.
8412 *
8413 * @return The width of your view, in pixels.
8414 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008415 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008416 public final int getWidth() {
8417 return mRight - mLeft;
8418 }
8419
8420 /**
8421 * Return the height of your view.
8422 *
8423 * @return The height of your view, in pixels.
8424 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008425 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008426 public final int getHeight() {
8427 return mBottom - mTop;
8428 }
8429
8430 /**
8431 * Return the visible drawing bounds of your view. Fills in the output
8432 * rectangle with the values from getScrollX(), getScrollY(),
8433 * getWidth(), and getHeight().
8434 *
8435 * @param outRect The (scrolled) drawing bounds of the view.
8436 */
8437 public void getDrawingRect(Rect outRect) {
8438 outRect.left = mScrollX;
8439 outRect.top = mScrollY;
8440 outRect.right = mScrollX + (mRight - mLeft);
8441 outRect.bottom = mScrollY + (mBottom - mTop);
8442 }
8443
8444 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008445 * Like {@link #getMeasuredWidthAndState()}, but only returns the
8446 * raw width component (that is the result is masked by
8447 * {@link #MEASURED_SIZE_MASK}).
8448 *
8449 * @return The raw measured width of this view.
8450 */
8451 public final int getMeasuredWidth() {
8452 return mMeasuredWidth & MEASURED_SIZE_MASK;
8453 }
8454
8455 /**
8456 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008457 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008458 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008459 * This should be used during measurement and layout calculations only. Use
8460 * {@link #getWidth()} to see how wide a view is after layout.
8461 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008462 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008463 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008464 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008465 return mMeasuredWidth;
8466 }
8467
8468 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008469 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8470 * raw width component (that is the result is masked by
8471 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008472 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008473 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008474 */
8475 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008476 return mMeasuredHeight & MEASURED_SIZE_MASK;
8477 }
8478
8479 /**
8480 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008481 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008482 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8483 * This should be used during measurement and layout calculations only. Use
8484 * {@link #getHeight()} to see how wide a view is after layout.
8485 *
8486 * @return The measured width of this view as a bit mask.
8487 */
8488 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008489 return mMeasuredHeight;
8490 }
8491
8492 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008493 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8494 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8495 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8496 * and the height component is at the shifted bits
8497 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8498 */
8499 public final int getMeasuredState() {
8500 return (mMeasuredWidth&MEASURED_STATE_MASK)
8501 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8502 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8503 }
8504
8505 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008506 * The transform matrix of this view, which is calculated based on the current
8507 * roation, scale, and pivot properties.
8508 *
8509 * @see #getRotation()
8510 * @see #getScaleX()
8511 * @see #getScaleY()
8512 * @see #getPivotX()
8513 * @see #getPivotY()
8514 * @return The current transform matrix for the view
8515 */
8516 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008517 if (mTransformationInfo != null) {
8518 updateMatrix();
8519 return mTransformationInfo.mMatrix;
8520 }
8521 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008522 }
8523
8524 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008525 * Utility function to determine if the value is far enough away from zero to be
8526 * considered non-zero.
8527 * @param value A floating point value to check for zero-ness
8528 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8529 */
8530 private static boolean nonzero(float value) {
8531 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8532 }
8533
8534 /**
Jeff Brown86671742010-09-30 20:00:15 -07008535 * Returns true if the transform matrix is the identity matrix.
8536 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008537 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008538 * @return True if the transform matrix is the identity matrix, false otherwise.
8539 */
Jeff Brown86671742010-09-30 20:00:15 -07008540 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008541 if (mTransformationInfo != null) {
8542 updateMatrix();
8543 return mTransformationInfo.mMatrixIsIdentity;
8544 }
8545 return true;
8546 }
8547
8548 void ensureTransformationInfo() {
8549 if (mTransformationInfo == null) {
8550 mTransformationInfo = new TransformationInfo();
8551 }
Jeff Brown86671742010-09-30 20:00:15 -07008552 }
8553
8554 /**
8555 * Recomputes the transform matrix if necessary.
8556 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008557 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008558 final TransformationInfo info = mTransformationInfo;
8559 if (info == null) {
8560 return;
8561 }
8562 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008563 // transform-related properties have changed since the last time someone
8564 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008565
8566 // Figure out if we need to update the pivot point
Dianne Hackborn4702a852012-08-17 15:18:29 -07008567 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008568 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8569 info.mPrevWidth = mRight - mLeft;
8570 info.mPrevHeight = mBottom - mTop;
8571 info.mPivotX = info.mPrevWidth / 2f;
8572 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008573 }
8574 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008575 info.mMatrix.reset();
8576 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8577 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8578 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8579 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008580 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008581 if (info.mCamera == null) {
8582 info.mCamera = new Camera();
8583 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008584 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008585 info.mCamera.save();
8586 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8587 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8588 info.mCamera.getMatrix(info.matrix3D);
8589 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8590 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8591 info.mPivotY + info.mTranslationY);
8592 info.mMatrix.postConcat(info.matrix3D);
8593 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008594 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008595 info.mMatrixDirty = false;
8596 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8597 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008598 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008599 }
8600
8601 /**
Tobias Duboisdefdb1e2010-12-15 11:35:30 +01008602 * When searching for a view to focus this rectangle is used when considering if this view is
8603 * a good candidate for receiving focus.
8604 *
8605 * By default, the rectangle is the {@link #getDrawingRect}) of the view.
8606 *
8607 * @param r The rectangle to fill in, in this view's coordinates.
8608 */
8609 public void getFocusRect(Rect r) {
8610 getDrawingRect(r);
8611 }
8612
8613 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008614 * Utility method to retrieve the inverse of the current mMatrix property.
8615 * We cache the matrix to avoid recalculating it when transform properties
8616 * have not changed.
8617 *
8618 * @return The inverse of the current matrix of this view.
8619 */
Jeff Brown86671742010-09-30 20:00:15 -07008620 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008621 final TransformationInfo info = mTransformationInfo;
8622 if (info != null) {
8623 updateMatrix();
8624 if (info.mInverseMatrixDirty) {
8625 if (info.mInverseMatrix == null) {
8626 info.mInverseMatrix = new Matrix();
8627 }
8628 info.mMatrix.invert(info.mInverseMatrix);
8629 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008630 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008631 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008632 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008633 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008634 }
8635
8636 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008637 * Gets the distance along the Z axis from the camera to this view.
8638 *
8639 * @see #setCameraDistance(float)
8640 *
8641 * @return The distance along the Z axis.
8642 */
8643 public float getCameraDistance() {
8644 ensureTransformationInfo();
8645 final float dpi = mResources.getDisplayMetrics().densityDpi;
8646 final TransformationInfo info = mTransformationInfo;
8647 if (info.mCamera == null) {
8648 info.mCamera = new Camera();
8649 info.matrix3D = new Matrix();
8650 }
8651 return -(info.mCamera.getLocationZ() * dpi);
8652 }
8653
8654 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008655 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8656 * views are drawn) from the camera to this view. The camera's distance
8657 * affects 3D transformations, for instance rotations around the X and Y
8658 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008659 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008660 * use a camera distance that's greater than the height (X axis rotation) or
8661 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008662 *
Romain Guya5364ee2011-02-24 14:46:04 -08008663 * <p>The distance of the camera from the view plane can have an affect on the
8664 * perspective distortion of the view when it is rotated around the x or y axis.
8665 * For example, a large distance will result in a large viewing angle, and there
8666 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008667 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008668 * also result in some drawing artifacts if the rotated view ends up partially
8669 * behind the camera (which is why the recommendation is to use a distance at
8670 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008671 *
Romain Guya5364ee2011-02-24 14:46:04 -08008672 * <p>The distance is expressed in "depth pixels." The default distance depends
8673 * on the screen density. For instance, on a medium density display, the
8674 * default distance is 1280. On a high density display, the default distance
8675 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008676 *
Romain Guya5364ee2011-02-24 14:46:04 -08008677 * <p>If you want to specify a distance that leads to visually consistent
8678 * results across various densities, use the following formula:</p>
8679 * <pre>
8680 * float scale = context.getResources().getDisplayMetrics().density;
8681 * view.setCameraDistance(distance * scale);
8682 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07008683 *
Romain Guya5364ee2011-02-24 14:46:04 -08008684 * <p>The density scale factor of a high density display is 1.5,
8685 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008686 *
Romain Guya5364ee2011-02-24 14:46:04 -08008687 * @param distance The distance in "depth pixels", if negative the opposite
8688 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07008689 *
8690 * @see #setRotationX(float)
8691 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08008692 */
8693 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008694 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08008695
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008696 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08008697 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008698 final TransformationInfo info = mTransformationInfo;
8699 if (info.mCamera == null) {
8700 info.mCamera = new Camera();
8701 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08008702 }
8703
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008704 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
8705 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08008706
Chet Haase9d1992d2012-03-13 11:03:25 -07008707 invalidateViewProperty(false, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008708 if (mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07008709 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08008710 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008711 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008712 // View was rejected last time it was drawn by its parent; this may have changed
8713 invalidateParentIfNeeded();
8714 }
Romain Guya5364ee2011-02-24 14:46:04 -08008715 }
8716
8717 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008718 * The degrees that the view is rotated around the pivot point.
8719 *
Philip Milne6c8ea062012-04-03 17:38:43 -07008720 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07008721 * @see #getPivotX()
8722 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008723 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008724 * @return The degrees of rotation.
8725 */
Chet Haasea5531132012-02-02 13:41:44 -08008726 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008727 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008728 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008729 }
8730
8731 /**
Chet Haase897247b2010-09-09 14:54:47 -07008732 * Sets the degrees that the view is rotated around the pivot point. Increasing values
8733 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07008734 *
8735 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008736 *
8737 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07008738 * @see #getPivotX()
8739 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008740 * @see #setRotationX(float)
8741 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08008742 *
8743 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07008744 */
8745 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008746 ensureTransformationInfo();
8747 final TransformationInfo info = mTransformationInfo;
8748 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008749 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008750 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008751 info.mRotation = rotation;
8752 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008753 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008754 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008755 mDisplayList.setRotation(rotation);
8756 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008757 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008758 // View was rejected last time it was drawn by its parent; this may have changed
8759 invalidateParentIfNeeded();
8760 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008761 }
8762 }
8763
8764 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008765 * The degrees that the view is rotated around the vertical axis through the pivot point.
8766 *
8767 * @see #getPivotX()
8768 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008769 * @see #setRotationY(float)
8770 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008771 * @return The degrees of Y rotation.
8772 */
Chet Haasea5531132012-02-02 13:41:44 -08008773 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008774 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008775 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008776 }
8777
8778 /**
Chet Haase897247b2010-09-09 14:54:47 -07008779 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
8780 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
8781 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008782 *
Romain Guya5364ee2011-02-24 14:46:04 -08008783 * When rotating large views, it is recommended to adjust the camera distance
8784 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008785 *
8786 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008787 *
8788 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07008789 * @see #getPivotX()
8790 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008791 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008792 * @see #setRotationX(float)
8793 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008794 *
8795 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07008796 */
8797 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008798 ensureTransformationInfo();
8799 final TransformationInfo info = mTransformationInfo;
8800 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008801 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008802 info.mRotationY = rotationY;
8803 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008804 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008805 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008806 mDisplayList.setRotationY(rotationY);
8807 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008808 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008809 // View was rejected last time it was drawn by its parent; this may have changed
8810 invalidateParentIfNeeded();
8811 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008812 }
8813 }
8814
8815 /**
8816 * The degrees that the view is rotated around the horizontal axis through the pivot point.
8817 *
8818 * @see #getPivotX()
8819 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008820 * @see #setRotationX(float)
8821 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008822 * @return The degrees of X rotation.
8823 */
Chet Haasea5531132012-02-02 13:41:44 -08008824 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008825 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008826 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008827 }
8828
8829 /**
Chet Haase897247b2010-09-09 14:54:47 -07008830 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
8831 * Increasing values result in clockwise rotation from the viewpoint of looking down the
8832 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008833 *
Romain Guya5364ee2011-02-24 14:46:04 -08008834 * When rotating large views, it is recommended to adjust the camera distance
8835 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008836 *
8837 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008838 *
8839 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07008840 * @see #getPivotX()
8841 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008842 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008843 * @see #setRotationY(float)
8844 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008845 *
8846 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07008847 */
8848 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008849 ensureTransformationInfo();
8850 final TransformationInfo info = mTransformationInfo;
8851 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008852 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008853 info.mRotationX = rotationX;
8854 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008855 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008856 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008857 mDisplayList.setRotationX(rotationX);
8858 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008859 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008860 // View was rejected last time it was drawn by its parent; this may have changed
8861 invalidateParentIfNeeded();
8862 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008863 }
8864 }
8865
8866 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008867 * The amount that the view is scaled in x around the pivot point, as a proportion of
8868 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
8869 *
Joe Onorato93162322010-09-16 15:42:01 -04008870 * <p>By default, this is 1.0f.
8871 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008872 * @see #getPivotX()
8873 * @see #getPivotY()
8874 * @return The scaling factor.
8875 */
Chet Haasea5531132012-02-02 13:41:44 -08008876 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008877 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008878 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008879 }
8880
8881 /**
8882 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
8883 * the view's unscaled width. A value of 1 means that no scaling is applied.
8884 *
8885 * @param scaleX The scaling factor.
8886 * @see #getPivotX()
8887 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008888 *
8889 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07008890 */
8891 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008892 ensureTransformationInfo();
8893 final TransformationInfo info = mTransformationInfo;
8894 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008895 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008896 info.mScaleX = scaleX;
8897 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008898 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008899 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008900 mDisplayList.setScaleX(scaleX);
8901 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008902 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008903 // View was rejected last time it was drawn by its parent; this may have changed
8904 invalidateParentIfNeeded();
8905 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008906 }
8907 }
8908
8909 /**
8910 * The amount that the view is scaled in y around the pivot point, as a proportion of
8911 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
8912 *
Joe Onorato93162322010-09-16 15:42:01 -04008913 * <p>By default, this is 1.0f.
8914 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008915 * @see #getPivotX()
8916 * @see #getPivotY()
8917 * @return The scaling factor.
8918 */
Chet Haasea5531132012-02-02 13:41:44 -08008919 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008920 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008921 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008922 }
8923
8924 /**
8925 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
8926 * the view's unscaled width. A value of 1 means that no scaling is applied.
8927 *
8928 * @param scaleY The scaling factor.
8929 * @see #getPivotX()
8930 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008931 *
8932 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07008933 */
8934 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008935 ensureTransformationInfo();
8936 final TransformationInfo info = mTransformationInfo;
8937 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008938 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008939 info.mScaleY = scaleY;
8940 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008941 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008942 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008943 mDisplayList.setScaleY(scaleY);
8944 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008945 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008946 // View was rejected last time it was drawn by its parent; this may have changed
8947 invalidateParentIfNeeded();
8948 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008949 }
8950 }
8951
8952 /**
8953 * The x location of the point around which the view is {@link #setRotation(float) rotated}
8954 * and {@link #setScaleX(float) scaled}.
8955 *
8956 * @see #getRotation()
8957 * @see #getScaleX()
8958 * @see #getScaleY()
8959 * @see #getPivotY()
8960 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008961 *
8962 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008963 */
Chet Haasea5531132012-02-02 13:41:44 -08008964 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008965 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008966 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008967 }
8968
8969 /**
8970 * Sets the x location of the point around which the view is
8971 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07008972 * By default, the pivot point is centered on the object.
8973 * Setting this property disables this behavior and causes the view to use only the
8974 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008975 *
8976 * @param pivotX The x location of the pivot point.
8977 * @see #getRotation()
8978 * @see #getScaleX()
8979 * @see #getScaleY()
8980 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008981 *
8982 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008983 */
8984 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008985 ensureTransformationInfo();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008986 mPrivateFlags |= PFLAG_PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008987 final TransformationInfo info = mTransformationInfo;
8988 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008989 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008990 info.mPivotX = pivotX;
8991 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008992 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008993 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008994 mDisplayList.setPivotX(pivotX);
8995 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008996 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008997 // View was rejected last time it was drawn by its parent; this may have changed
8998 invalidateParentIfNeeded();
8999 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009000 }
9001 }
9002
9003 /**
9004 * The y location of the point around which the view is {@link #setRotation(float) rotated}
9005 * and {@link #setScaleY(float) scaled}.
9006 *
9007 * @see #getRotation()
9008 * @see #getScaleX()
9009 * @see #getScaleY()
9010 * @see #getPivotY()
9011 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009012 *
9013 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009014 */
Chet Haasea5531132012-02-02 13:41:44 -08009015 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009016 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009017 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009018 }
9019
9020 /**
9021 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07009022 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
9023 * Setting this property disables this behavior and causes the view to use only the
9024 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009025 *
9026 * @param pivotY The y location of the pivot point.
9027 * @see #getRotation()
9028 * @see #getScaleX()
9029 * @see #getScaleY()
9030 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009031 *
9032 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009033 */
9034 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009035 ensureTransformationInfo();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009036 mPrivateFlags |= PFLAG_PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009037 final TransformationInfo info = mTransformationInfo;
9038 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009039 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009040 info.mPivotY = pivotY;
9041 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009042 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009043 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009044 mDisplayList.setPivotY(pivotY);
9045 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009046 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009047 // View was rejected last time it was drawn by its parent; this may have changed
9048 invalidateParentIfNeeded();
9049 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009050 }
9051 }
9052
9053 /**
9054 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
9055 * completely transparent and 1 means the view is completely opaque.
9056 *
Joe Onorato93162322010-09-16 15:42:01 -04009057 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07009058 * @return The opacity of the view.
9059 */
Chet Haasea5531132012-02-02 13:41:44 -08009060 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009061 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009062 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009063 }
9064
9065 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07009066 * Returns whether this View has content which overlaps. This function, intended to be
9067 * overridden by specific View types, is an optimization when alpha is set on a view. If
9068 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
9069 * and then composited it into place, which can be expensive. If the view has no overlapping
9070 * rendering, the view can draw each primitive with the appropriate alpha value directly.
9071 * An example of overlapping rendering is a TextView with a background image, such as a
9072 * Button. An example of non-overlapping rendering is a TextView with no background, or
9073 * an ImageView with only the foreground image. The default implementation returns true;
9074 * subclasses should override if they have cases which can be optimized.
9075 *
9076 * @return true if the content in this view might overlap, false otherwise.
9077 */
9078 public boolean hasOverlappingRendering() {
9079 return true;
9080 }
9081
9082 /**
Romain Guy171c5922011-01-06 10:04:23 -08009083 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
9084 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009085 *
Romain Guy171c5922011-01-06 10:04:23 -08009086 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
9087 * responsible for applying the opacity itself. Otherwise, calling this method is
9088 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08009089 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07009090 *
Chet Haasea5531132012-02-02 13:41:44 -08009091 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
9092 * performance implications. It is generally best to use the alpha property sparingly and
9093 * transiently, as in the case of fading animations.</p>
9094 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009095 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08009096 *
Joe Malin32736f02011-01-19 16:14:20 -08009097 * @see #setLayerType(int, android.graphics.Paint)
9098 *
Chet Haase73066682010-11-29 15:55:32 -08009099 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07009100 */
9101 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009102 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009103 if (mTransformationInfo.mAlpha != alpha) {
9104 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009105 if (onSetAlpha((int) (alpha * 255))) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009106 mPrivateFlags |= PFLAG_ALPHA_SET;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009107 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07009108 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009109 invalidate(true);
9110 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009111 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07009112 invalidateViewProperty(true, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07009113 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009114 mDisplayList.setAlpha(alpha);
9115 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009116 }
Chet Haaseed032702010-10-01 14:05:54 -07009117 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009118 }
9119
9120 /**
Chet Haasea00f3862011-02-22 06:34:40 -08009121 * Faster version of setAlpha() which performs the same steps except there are
9122 * no calls to invalidate(). The caller of this function should perform proper invalidation
9123 * on the parent and this object. The return value indicates whether the subclass handles
9124 * alpha (the return value for onSetAlpha()).
9125 *
9126 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009127 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
9128 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08009129 */
9130 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009131 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009132 if (mTransformationInfo.mAlpha != alpha) {
9133 mTransformationInfo.mAlpha = alpha;
9134 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
9135 if (subclassHandlesAlpha) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009136 mPrivateFlags |= PFLAG_ALPHA_SET;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009137 return true;
9138 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009139 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase1271e2c2012-04-20 09:54:27 -07009140 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009141 mDisplayList.setAlpha(alpha);
9142 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009143 }
Chet Haasea00f3862011-02-22 06:34:40 -08009144 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009145 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08009146 }
9147
9148 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009149 * Top position of this view relative to its parent.
9150 *
9151 * @return The top of this view, in pixels.
9152 */
9153 @ViewDebug.CapturedViewProperty
9154 public final int getTop() {
9155 return mTop;
9156 }
9157
9158 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009159 * Sets the top position of this view relative to its parent. This method is meant to be called
9160 * by the layout system and should not generally be called otherwise, because the property
9161 * may be changed at any time by the layout.
9162 *
9163 * @param top The top of this view, in pixels.
9164 */
9165 public final void setTop(int top) {
9166 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07009167 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009168 final boolean matrixIsIdentity = mTransformationInfo == null
9169 || mTransformationInfo.mMatrixIsIdentity;
9170 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009171 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009172 int minTop;
9173 int yLoc;
9174 if (top < mTop) {
9175 minTop = top;
9176 yLoc = top - mTop;
9177 } else {
9178 minTop = mTop;
9179 yLoc = 0;
9180 }
Chet Haasee9140a72011-02-16 16:23:29 -08009181 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009182 }
9183 } else {
9184 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009185 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009186 }
9187
Chet Haaseed032702010-10-01 14:05:54 -07009188 int width = mRight - mLeft;
9189 int oldHeight = mBottom - mTop;
9190
Chet Haase21cd1382010-09-01 17:42:29 -07009191 mTop = top;
Chet Haase1271e2c2012-04-20 09:54:27 -07009192 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009193 mDisplayList.setTop(mTop);
9194 }
Chet Haase21cd1382010-09-01 17:42:29 -07009195
Chet Haaseed032702010-10-01 14:05:54 -07009196 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9197
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009198 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009199 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009200 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009201 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009202 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009203 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009204 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009205 }
Chet Haase55dbb652010-12-21 20:15:08 -08009206 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009207 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009208 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009209 // View was rejected last time it was drawn by its parent; this may have changed
9210 invalidateParentIfNeeded();
9211 }
Chet Haase21cd1382010-09-01 17:42:29 -07009212 }
9213 }
9214
9215 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009216 * Bottom position of this view relative to its parent.
9217 *
9218 * @return The bottom of this view, in pixels.
9219 */
9220 @ViewDebug.CapturedViewProperty
9221 public final int getBottom() {
9222 return mBottom;
9223 }
9224
9225 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08009226 * True if this view has changed since the last time being drawn.
9227 *
9228 * @return The dirty state of this view.
9229 */
9230 public boolean isDirty() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009231 return (mPrivateFlags & PFLAG_DIRTY_MASK) != 0;
Michael Jurkadab559a2011-01-04 20:31:51 -08009232 }
9233
9234 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009235 * Sets the bottom position of this view relative to its parent. This method is meant to be
9236 * called by the layout system and should not generally be called otherwise, because the
9237 * property may be changed at any time by the layout.
9238 *
9239 * @param bottom The bottom of this view, in pixels.
9240 */
9241 public final void setBottom(int bottom) {
9242 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07009243 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009244 final boolean matrixIsIdentity = mTransformationInfo == null
9245 || mTransformationInfo.mMatrixIsIdentity;
9246 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009247 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009248 int maxBottom;
9249 if (bottom < mBottom) {
9250 maxBottom = mBottom;
9251 } else {
9252 maxBottom = bottom;
9253 }
Chet Haasee9140a72011-02-16 16:23:29 -08009254 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009255 }
9256 } else {
9257 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009258 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009259 }
9260
Chet Haaseed032702010-10-01 14:05:54 -07009261 int width = mRight - mLeft;
9262 int oldHeight = mBottom - mTop;
9263
Chet Haase21cd1382010-09-01 17:42:29 -07009264 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -07009265 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009266 mDisplayList.setBottom(mBottom);
9267 }
Chet Haase21cd1382010-09-01 17:42:29 -07009268
Chet Haaseed032702010-10-01 14:05:54 -07009269 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9270
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009271 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009272 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009273 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009274 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009275 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009276 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009277 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009278 }
Chet Haase55dbb652010-12-21 20:15:08 -08009279 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009280 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009281 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009282 // View was rejected last time it was drawn by its parent; this may have changed
9283 invalidateParentIfNeeded();
9284 }
Chet Haase21cd1382010-09-01 17:42:29 -07009285 }
9286 }
9287
9288 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009289 * Left position of this view relative to its parent.
9290 *
9291 * @return The left edge of this view, in pixels.
9292 */
9293 @ViewDebug.CapturedViewProperty
9294 public final int getLeft() {
9295 return mLeft;
9296 }
9297
9298 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009299 * Sets the left position of this view relative to its parent. This method is meant to be called
9300 * by the layout system and should not generally be called otherwise, because the property
9301 * may be changed at any time by the layout.
9302 *
9303 * @param left The bottom of this view, in pixels.
9304 */
9305 public final void setLeft(int left) {
9306 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07009307 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009308 final boolean matrixIsIdentity = mTransformationInfo == null
9309 || mTransformationInfo.mMatrixIsIdentity;
9310 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009311 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009312 int minLeft;
9313 int xLoc;
9314 if (left < mLeft) {
9315 minLeft = left;
9316 xLoc = left - mLeft;
9317 } else {
9318 minLeft = mLeft;
9319 xLoc = 0;
9320 }
Chet Haasee9140a72011-02-16 16:23:29 -08009321 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009322 }
9323 } else {
9324 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009325 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009326 }
9327
Chet Haaseed032702010-10-01 14:05:54 -07009328 int oldWidth = mRight - mLeft;
9329 int height = mBottom - mTop;
9330
Chet Haase21cd1382010-09-01 17:42:29 -07009331 mLeft = left;
Chet Haase1271e2c2012-04-20 09:54:27 -07009332 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009333 mDisplayList.setLeft(left);
9334 }
Chet Haase21cd1382010-09-01 17:42:29 -07009335
Chet Haaseed032702010-10-01 14:05:54 -07009336 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9337
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009338 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009339 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009340 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009341 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009342 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009343 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009344 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009345 }
Chet Haase55dbb652010-12-21 20:15:08 -08009346 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009347 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009348 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009349 // View was rejected last time it was drawn by its parent; this may have changed
9350 invalidateParentIfNeeded();
9351 }
Chet Haase21cd1382010-09-01 17:42:29 -07009352 }
9353 }
9354
9355 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009356 * Right position of this view relative to its parent.
9357 *
9358 * @return The right edge of this view, in pixels.
9359 */
9360 @ViewDebug.CapturedViewProperty
9361 public final int getRight() {
9362 return mRight;
9363 }
9364
9365 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009366 * Sets the right position of this view relative to its parent. This method is meant to be called
9367 * by the layout system and should not generally be called otherwise, because the property
9368 * may be changed at any time by the layout.
9369 *
9370 * @param right The bottom of this view, in pixels.
9371 */
9372 public final void setRight(int right) {
9373 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07009374 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009375 final boolean matrixIsIdentity = mTransformationInfo == null
9376 || mTransformationInfo.mMatrixIsIdentity;
9377 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009378 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009379 int maxRight;
9380 if (right < mRight) {
9381 maxRight = mRight;
9382 } else {
9383 maxRight = right;
9384 }
Chet Haasee9140a72011-02-16 16:23:29 -08009385 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009386 }
9387 } else {
9388 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009389 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009390 }
9391
Chet Haaseed032702010-10-01 14:05:54 -07009392 int oldWidth = mRight - mLeft;
9393 int height = mBottom - mTop;
9394
Chet Haase21cd1382010-09-01 17:42:29 -07009395 mRight = right;
Chet Haase1271e2c2012-04-20 09:54:27 -07009396 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009397 mDisplayList.setRight(mRight);
9398 }
Chet Haase21cd1382010-09-01 17:42:29 -07009399
Chet Haaseed032702010-10-01 14:05:54 -07009400 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9401
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009402 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009403 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009404 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009405 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009406 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009407 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009408 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009409 }
Chet Haase55dbb652010-12-21 20:15:08 -08009410 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009411 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009412 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009413 // View was rejected last time it was drawn by its parent; this may have changed
9414 invalidateParentIfNeeded();
9415 }
Chet Haase21cd1382010-09-01 17:42:29 -07009416 }
9417 }
9418
9419 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009420 * The visual x position of this view, in pixels. This is equivalent to the
9421 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08009422 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07009423 *
Chet Haasedf030d22010-07-30 17:22:38 -07009424 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009425 */
Chet Haasea5531132012-02-02 13:41:44 -08009426 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009427 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009428 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009429 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009430
Chet Haasedf030d22010-07-30 17:22:38 -07009431 /**
9432 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
9433 * {@link #setTranslationX(float) translationX} property to be the difference between
9434 * the x value passed in and the current {@link #getLeft() left} property.
9435 *
9436 * @param x The visual x position of this view, in pixels.
9437 */
9438 public void setX(float x) {
9439 setTranslationX(x - mLeft);
9440 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009441
Chet Haasedf030d22010-07-30 17:22:38 -07009442 /**
9443 * The visual y position of this view, in pixels. This is equivalent to the
9444 * {@link #setTranslationY(float) translationY} property plus the current
9445 * {@link #getTop() top} property.
9446 *
9447 * @return The visual y position of this view, in pixels.
9448 */
Chet Haasea5531132012-02-02 13:41:44 -08009449 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009450 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009451 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009452 }
9453
9454 /**
9455 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
9456 * {@link #setTranslationY(float) translationY} property to be the difference between
9457 * the y value passed in and the current {@link #getTop() top} property.
9458 *
9459 * @param y The visual y position of this view, in pixels.
9460 */
9461 public void setY(float y) {
9462 setTranslationY(y - mTop);
9463 }
9464
9465
9466 /**
9467 * The horizontal location of this view relative to its {@link #getLeft() left} position.
9468 * This position is post-layout, in addition to wherever the object's
9469 * layout placed it.
9470 *
9471 * @return The horizontal position of this view relative to its left position, in pixels.
9472 */
Chet Haasea5531132012-02-02 13:41:44 -08009473 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009474 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009475 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07009476 }
9477
9478 /**
9479 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
9480 * This effectively positions the object post-layout, in addition to wherever the object's
9481 * layout placed it.
9482 *
9483 * @param translationX The horizontal position of this view relative to its left position,
9484 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009485 *
9486 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07009487 */
9488 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009489 ensureTransformationInfo();
9490 final TransformationInfo info = mTransformationInfo;
9491 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07009492 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009493 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009494 info.mTranslationX = translationX;
9495 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009496 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009497 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009498 mDisplayList.setTranslationX(translationX);
9499 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009500 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009501 // View was rejected last time it was drawn by its parent; this may have changed
9502 invalidateParentIfNeeded();
9503 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009504 }
9505 }
9506
9507 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009508 * The horizontal location of this view relative to its {@link #getTop() top} position.
9509 * This position is post-layout, in addition to wherever the object's
9510 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009511 *
Chet Haasedf030d22010-07-30 17:22:38 -07009512 * @return The vertical position of this view relative to its top position,
9513 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009514 */
Chet Haasea5531132012-02-02 13:41:44 -08009515 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009516 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009517 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009518 }
9519
9520 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009521 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9522 * This effectively positions the object post-layout, in addition to wherever the object's
9523 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009524 *
Chet Haasedf030d22010-07-30 17:22:38 -07009525 * @param translationY The vertical position of this view relative to its top position,
9526 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009527 *
9528 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009529 */
Chet Haasedf030d22010-07-30 17:22:38 -07009530 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009531 ensureTransformationInfo();
9532 final TransformationInfo info = mTransformationInfo;
9533 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009534 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009535 info.mTranslationY = translationY;
9536 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009537 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009538 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009539 mDisplayList.setTranslationY(translationY);
9540 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009541 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009542 // View was rejected last time it was drawn by its parent; this may have changed
9543 invalidateParentIfNeeded();
9544 }
Chet Haasedf030d22010-07-30 17:22:38 -07009545 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009546 }
9547
9548 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009549 * Hit rectangle in parent's coordinates
9550 *
9551 * @param outRect The hit rectangle of the view.
9552 */
9553 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009554 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009555 final TransformationInfo info = mTransformationInfo;
9556 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009557 outRect.set(mLeft, mTop, mRight, mBottom);
9558 } else {
9559 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009560 tmpRect.set(-info.mPivotX, -info.mPivotY,
9561 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9562 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009563 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9564 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009565 }
9566 }
9567
9568 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009569 * Determines whether the given point, in local coordinates is inside the view.
9570 */
9571 /*package*/ final boolean pointInView(float localX, float localY) {
9572 return localX >= 0 && localX < (mRight - mLeft)
9573 && localY >= 0 && localY < (mBottom - mTop);
9574 }
9575
9576 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009577 * Utility method to determine whether the given point, in local coordinates,
9578 * is inside the view, where the area of the view is expanded by the slop factor.
9579 * This method is called while processing touch-move events to determine if the event
9580 * is still within the view.
9581 */
9582 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009583 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009584 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009585 }
9586
9587 /**
9588 * When a view has focus and the user navigates away from it, the next view is searched for
9589 * starting from the rectangle filled in by this method.
9590 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009591 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
9592 * of the view. However, if your view maintains some idea of internal selection,
9593 * such as a cursor, or a selected row or column, you should override this method and
9594 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009595 *
9596 * @param r The rectangle to fill in, in this view's coordinates.
9597 */
9598 public void getFocusedRect(Rect r) {
9599 getDrawingRect(r);
9600 }
9601
9602 /**
9603 * If some part of this view is not clipped by any of its parents, then
9604 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009605 * coordinates (without taking possible View rotations into account), offset
9606 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9607 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009608 *
9609 * @param r If true is returned, r holds the global coordinates of the
9610 * visible portion of this view.
9611 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9612 * between this view and its root. globalOffet may be null.
9613 * @return true if r is non-empty (i.e. part of the view is visible at the
9614 * root level.
9615 */
9616 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9617 int width = mRight - mLeft;
9618 int height = mBottom - mTop;
9619 if (width > 0 && height > 0) {
9620 r.set(0, 0, width, height);
9621 if (globalOffset != null) {
9622 globalOffset.set(-mScrollX, -mScrollY);
9623 }
9624 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9625 }
9626 return false;
9627 }
9628
9629 public final boolean getGlobalVisibleRect(Rect r) {
9630 return getGlobalVisibleRect(r, null);
9631 }
9632
9633 public final boolean getLocalVisibleRect(Rect r) {
Romain Guyab4c4f4f2012-05-06 13:11:24 -07009634 final Point offset = mAttachInfo != null ? mAttachInfo.mPoint : new Point();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009635 if (getGlobalVisibleRect(r, offset)) {
9636 r.offset(-offset.x, -offset.y); // make r local
9637 return true;
9638 }
9639 return false;
9640 }
9641
9642 /**
9643 * Offset this view's vertical location by the specified number of pixels.
9644 *
9645 * @param offset the number of pixels to offset the view by
9646 */
9647 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009648 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009649 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009650 final boolean matrixIsIdentity = mTransformationInfo == null
9651 || mTransformationInfo.mMatrixIsIdentity;
9652 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009653 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009654 invalidateViewProperty(false, false);
9655 } else {
9656 final ViewParent p = mParent;
9657 if (p != null && mAttachInfo != null) {
9658 final Rect r = mAttachInfo.mTmpInvalRect;
9659 int minTop;
9660 int maxBottom;
9661 int yLoc;
9662 if (offset < 0) {
9663 minTop = mTop + offset;
9664 maxBottom = mBottom;
9665 yLoc = offset;
9666 } else {
9667 minTop = mTop;
9668 maxBottom = mBottom + offset;
9669 yLoc = 0;
9670 }
9671 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
9672 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009673 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009674 }
9675 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009676 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009677 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009678
Chet Haasec3aa3612010-06-17 08:50:37 -07009679 mTop += offset;
9680 mBottom += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009681 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009682 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009683 invalidateViewProperty(false, false);
9684 } else {
9685 if (!matrixIsIdentity) {
9686 invalidateViewProperty(false, true);
9687 }
9688 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009689 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009690 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009691 }
9692
9693 /**
9694 * Offset this view's horizontal location by the specified amount of pixels.
9695 *
9696 * @param offset the numer of pixels to offset the view by
9697 */
9698 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009699 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009700 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009701 final boolean matrixIsIdentity = mTransformationInfo == null
9702 || mTransformationInfo.mMatrixIsIdentity;
9703 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009704 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009705 invalidateViewProperty(false, false);
9706 } else {
9707 final ViewParent p = mParent;
9708 if (p != null && mAttachInfo != null) {
9709 final Rect r = mAttachInfo.mTmpInvalRect;
9710 int minLeft;
9711 int maxRight;
9712 if (offset < 0) {
9713 minLeft = mLeft + offset;
9714 maxRight = mRight;
9715 } else {
9716 minLeft = mLeft;
9717 maxRight = mRight + offset;
9718 }
9719 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
9720 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009721 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009722 }
9723 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009724 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009725 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009726
Chet Haasec3aa3612010-06-17 08:50:37 -07009727 mLeft += offset;
9728 mRight += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009729 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009730 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009731 invalidateViewProperty(false, false);
9732 } else {
9733 if (!matrixIsIdentity) {
9734 invalidateViewProperty(false, true);
9735 }
9736 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009737 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009738 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009739 }
9740
9741 /**
9742 * Get the LayoutParams associated with this view. All views should have
9743 * layout parameters. These supply parameters to the <i>parent</i> of this
9744 * view specifying how it should be arranged. There are many subclasses of
9745 * ViewGroup.LayoutParams, and these correspond to the different subclasses
9746 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08009747 *
9748 * This method may return null if this View is not attached to a parent
9749 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
9750 * was not invoked successfully. When a View is attached to a parent
9751 * ViewGroup, this method must not return null.
9752 *
9753 * @return The LayoutParams associated with this view, or null if no
9754 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009755 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07009756 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009757 public ViewGroup.LayoutParams getLayoutParams() {
9758 return mLayoutParams;
9759 }
9760
9761 /**
9762 * Set the layout parameters associated with this view. These supply
9763 * parameters to the <i>parent</i> of this view specifying how it should be
9764 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
9765 * correspond to the different subclasses of ViewGroup that are responsible
9766 * for arranging their children.
9767 *
Romain Guy01c174b2011-02-22 11:51:06 -08009768 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009769 */
9770 public void setLayoutParams(ViewGroup.LayoutParams params) {
9771 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08009772 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009773 }
9774 mLayoutParams = params;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07009775 resolveLayoutParams();
Philip Milned7dd8902012-01-26 16:55:30 -08009776 if (mParent instanceof ViewGroup) {
9777 ((ViewGroup) mParent).onSetLayoutParams(this, params);
9778 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009779 requestLayout();
9780 }
9781
9782 /**
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07009783 * Resolve the layout parameters depending on the resolved layout direction
9784 */
9785 private void resolveLayoutParams() {
9786 if (mLayoutParams != null) {
9787 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
9788 }
9789 }
9790
9791 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009792 * Set the scrolled position of your view. This will cause a call to
9793 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9794 * invalidated.
9795 * @param x the x position to scroll to
9796 * @param y the y position to scroll to
9797 */
9798 public void scrollTo(int x, int y) {
9799 if (mScrollX != x || mScrollY != y) {
9800 int oldX = mScrollX;
9801 int oldY = mScrollY;
9802 mScrollX = x;
9803 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009804 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009805 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07009806 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009807 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -07009808 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009809 }
9810 }
9811
9812 /**
9813 * Move the scrolled position of your view. This will cause a call to
9814 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9815 * invalidated.
9816 * @param x the amount of pixels to scroll by horizontally
9817 * @param y the amount of pixels to scroll by vertically
9818 */
9819 public void scrollBy(int x, int y) {
9820 scrollTo(mScrollX + x, mScrollY + y);
9821 }
9822
9823 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009824 * <p>Trigger the scrollbars to draw. When invoked this method starts an
9825 * animation to fade the scrollbars out after a default delay. If a subclass
9826 * provides animated scrolling, the start delay should equal the duration
9827 * of the scrolling animation.</p>
9828 *
9829 * <p>The animation starts only if at least one of the scrollbars is
9830 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
9831 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9832 * this method returns true, and false otherwise. If the animation is
9833 * started, this method calls {@link #invalidate()}; in that case the
9834 * caller should not call {@link #invalidate()}.</p>
9835 *
9836 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07009837 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07009838 *
9839 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
9840 * and {@link #scrollTo(int, int)}.</p>
9841 *
9842 * @return true if the animation is played, false otherwise
9843 *
9844 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07009845 * @see #scrollBy(int, int)
9846 * @see #scrollTo(int, int)
9847 * @see #isHorizontalScrollBarEnabled()
9848 * @see #isVerticalScrollBarEnabled()
9849 * @see #setHorizontalScrollBarEnabled(boolean)
9850 * @see #setVerticalScrollBarEnabled(boolean)
9851 */
9852 protected boolean awakenScrollBars() {
9853 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07009854 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07009855 }
9856
9857 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07009858 * Trigger the scrollbars to draw.
9859 * This method differs from awakenScrollBars() only in its default duration.
9860 * initialAwakenScrollBars() will show the scroll bars for longer than
9861 * usual to give the user more of a chance to notice them.
9862 *
9863 * @return true if the animation is played, false otherwise.
9864 */
9865 private boolean initialAwakenScrollBars() {
9866 return mScrollCache != null &&
9867 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
9868 }
9869
9870 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009871 * <p>
9872 * Trigger the scrollbars to draw. When invoked this method starts an
9873 * animation to fade the scrollbars out after a fixed delay. If a subclass
9874 * provides animated scrolling, the start delay should equal the duration of
9875 * the scrolling animation.
9876 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009877 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009878 * <p>
9879 * The animation starts only if at least one of the scrollbars is enabled,
9880 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9881 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9882 * this method returns true, and false otherwise. If the animation is
9883 * started, this method calls {@link #invalidate()}; in that case the caller
9884 * should not call {@link #invalidate()}.
9885 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009886 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009887 * <p>
9888 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07009889 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07009890 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009891 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009892 * @param startDelay the delay, in milliseconds, after which the animation
9893 * should start; when the delay is 0, the animation starts
9894 * immediately
9895 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009896 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009897 * @see #scrollBy(int, int)
9898 * @see #scrollTo(int, int)
9899 * @see #isHorizontalScrollBarEnabled()
9900 * @see #isVerticalScrollBarEnabled()
9901 * @see #setHorizontalScrollBarEnabled(boolean)
9902 * @see #setVerticalScrollBarEnabled(boolean)
9903 */
9904 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07009905 return awakenScrollBars(startDelay, true);
9906 }
Joe Malin32736f02011-01-19 16:14:20 -08009907
Mike Cleron290947b2009-09-29 18:34:32 -07009908 /**
9909 * <p>
9910 * Trigger the scrollbars to draw. When invoked this method starts an
9911 * animation to fade the scrollbars out after a fixed delay. If a subclass
9912 * provides animated scrolling, the start delay should equal the duration of
9913 * the scrolling animation.
9914 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009915 *
Mike Cleron290947b2009-09-29 18:34:32 -07009916 * <p>
9917 * The animation starts only if at least one of the scrollbars is enabled,
9918 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9919 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9920 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08009921 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07009922 * is set to true; in that case the caller
9923 * should not call {@link #invalidate()}.
9924 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009925 *
Mike Cleron290947b2009-09-29 18:34:32 -07009926 * <p>
9927 * This method should be invoked everytime a subclass directly updates the
9928 * scroll parameters.
9929 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009930 *
Mike Cleron290947b2009-09-29 18:34:32 -07009931 * @param startDelay the delay, in milliseconds, after which the animation
9932 * should start; when the delay is 0, the animation starts
9933 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08009934 *
Mike Cleron290947b2009-09-29 18:34:32 -07009935 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08009936 *
Mike Cleron290947b2009-09-29 18:34:32 -07009937 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009938 *
Mike Cleron290947b2009-09-29 18:34:32 -07009939 * @see #scrollBy(int, int)
9940 * @see #scrollTo(int, int)
9941 * @see #isHorizontalScrollBarEnabled()
9942 * @see #isVerticalScrollBarEnabled()
9943 * @see #setHorizontalScrollBarEnabled(boolean)
9944 * @see #setVerticalScrollBarEnabled(boolean)
9945 */
9946 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07009947 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08009948
Mike Cleronf116bf82009-09-27 19:14:12 -07009949 if (scrollCache == null || !scrollCache.fadeScrollBars) {
9950 return false;
9951 }
9952
9953 if (scrollCache.scrollBar == null) {
9954 scrollCache.scrollBar = new ScrollBarDrawable();
9955 }
9956
9957 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
9958
Mike Cleron290947b2009-09-29 18:34:32 -07009959 if (invalidate) {
9960 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009961 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -07009962 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009963
9964 if (scrollCache.state == ScrollabilityCache.OFF) {
9965 // FIXME: this is copied from WindowManagerService.
9966 // We should get this value from the system when it
9967 // is possible to do so.
9968 final int KEY_REPEAT_FIRST_DELAY = 750;
9969 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
9970 }
9971
9972 // Tell mScrollCache when we should start fading. This may
9973 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07009974 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07009975 scrollCache.fadeStartTime = fadeStartTime;
9976 scrollCache.state = ScrollabilityCache.ON;
9977
9978 // Schedule our fader to run, unscheduling any old ones first
9979 if (mAttachInfo != null) {
9980 mAttachInfo.mHandler.removeCallbacks(scrollCache);
9981 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
9982 }
9983
9984 return true;
9985 }
9986
9987 return false;
9988 }
9989
9990 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07009991 * Do not invalidate views which are not visible and which are not running an animation. They
9992 * will not get drawn and they should not set dirty flags as if they will be drawn
9993 */
9994 private boolean skipInvalidate() {
9995 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
9996 (!(mParent instanceof ViewGroup) ||
9997 !((ViewGroup) mParent).isViewTransitioning(this));
9998 }
9999 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010000 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010001 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
10002 * in the future. This must be called from a UI thread. To call from a non-UI
10003 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010004 *
10005 * WARNING: This method is destructive to dirty.
10006 * @param dirty the rectangle representing the bounds of the dirty region
10007 */
10008 public void invalidate(Rect dirty) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010009 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010010 return;
10011 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010012 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10013 (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID ||
10014 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED) {
10015 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
10016 mPrivateFlags |= PFLAG_INVALIDATED;
10017 mPrivateFlags |= PFLAG_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010018 final ViewParent p = mParent;
10019 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010020 //noinspection PointlessBooleanExpression,ConstantConditions
10021 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10022 if (p != null && ai != null && ai.mHardwareAccelerated) {
10023 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010024 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010025 p.invalidateChild(this, null);
10026 return;
10027 }
Romain Guyaf636eb2010-12-09 17:47:21 -080010028 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010029 if (p != null && ai != null) {
10030 final int scrollX = mScrollX;
10031 final int scrollY = mScrollY;
10032 final Rect r = ai.mTmpInvalRect;
10033 r.set(dirty.left - scrollX, dirty.top - scrollY,
10034 dirty.right - scrollX, dirty.bottom - scrollY);
10035 mParent.invalidateChild(this, r);
10036 }
10037 }
10038 }
10039
10040 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010041 * 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 -080010042 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -070010043 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
10044 * will be called at some point in the future. This must be called from
10045 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010046 * @param l the left position of the dirty region
10047 * @param t the top position of the dirty region
10048 * @param r the right position of the dirty region
10049 * @param b the bottom position of the dirty region
10050 */
10051 public void invalidate(int l, int t, int r, int b) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010052 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010053 return;
10054 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010055 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10056 (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID ||
10057 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED) {
10058 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
10059 mPrivateFlags |= PFLAG_INVALIDATED;
10060 mPrivateFlags |= PFLAG_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010061 final ViewParent p = mParent;
10062 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010063 //noinspection PointlessBooleanExpression,ConstantConditions
10064 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10065 if (p != null && ai != null && ai.mHardwareAccelerated) {
10066 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010067 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010068 p.invalidateChild(this, null);
10069 return;
10070 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010071 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010072 if (p != null && ai != null && l < r && t < b) {
10073 final int scrollX = mScrollX;
10074 final int scrollY = mScrollY;
10075 final Rect tmpr = ai.mTmpInvalRect;
10076 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
10077 p.invalidateChild(this, tmpr);
10078 }
10079 }
10080 }
10081
10082 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010083 * Invalidate the whole view. If the view is visible,
10084 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
10085 * the future. This must be called from a UI thread. To call from a non-UI thread,
10086 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010087 */
10088 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -070010089 invalidate(true);
10090 }
Joe Malin32736f02011-01-19 16:14:20 -080010091
Chet Haaseed032702010-10-01 14:05:54 -070010092 /**
10093 * This is where the invalidate() work actually happens. A full invalidate()
10094 * causes the drawing cache to be invalidated, but this function can be called with
10095 * invalidateCache set to false to skip that invalidation step for cases that do not
10096 * need it (for example, a component that remains at the same dimensions with the same
10097 * content).
10098 *
10099 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
10100 * well. This is usually true for a full invalidate, but may be set to false if the
10101 * View's contents or dimensions have not changed.
10102 */
Romain Guy849d0a32011-02-01 17:20:48 -080010103 void invalidate(boolean invalidateCache) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010104 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010105 return;
10106 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010107 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10108 (invalidateCache && (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID) ||
10109 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED || isOpaque() != mLastIsOpaque) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010110 mLastIsOpaque = isOpaque();
Dianne Hackborn4702a852012-08-17 15:18:29 -070010111 mPrivateFlags &= ~PFLAG_DRAWN;
10112 mPrivateFlags |= PFLAG_DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -070010113 if (invalidateCache) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010114 mPrivateFlags |= PFLAG_INVALIDATED;
10115 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Chet Haaseed032702010-10-01 14:05:54 -070010116 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010117 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -070010118 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -080010119 //noinspection PointlessBooleanExpression,ConstantConditions
10120 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10121 if (p != null && ai != null && ai.mHardwareAccelerated) {
10122 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010123 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010124 p.invalidateChild(this, null);
10125 return;
10126 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010127 }
Michael Jurkaebefea42010-11-15 16:04:01 -080010128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010129 if (p != null && ai != null) {
10130 final Rect r = ai.mTmpInvalRect;
10131 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10132 // Don't call invalidate -- we don't want to internally scroll
10133 // our own bounds
10134 p.invalidateChild(this, r);
10135 }
10136 }
10137 }
10138
10139 /**
Chet Haase9d1992d2012-03-13 11:03:25 -070010140 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
10141 * set any flags or handle all of the cases handled by the default invalidation methods.
10142 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
10143 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
10144 * walk up the hierarchy, transforming the dirty rect as necessary.
10145 *
10146 * The method also handles normal invalidation logic if display list properties are not
10147 * being used in this view. The invalidateParent and forceRedraw flags are used by that
10148 * backup approach, to handle these cases used in the various property-setting methods.
10149 *
10150 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
10151 * are not being used in this view
10152 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
10153 * list properties are not being used in this view
10154 */
10155 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010156 if (mDisplayList == null || (mPrivateFlags & PFLAG_DRAW_ANIMATION) == PFLAG_DRAW_ANIMATION) {
Chet Haase9d1992d2012-03-13 11:03:25 -070010157 if (invalidateParent) {
10158 invalidateParentCaches();
10159 }
10160 if (forceRedraw) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010161 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Chet Haase9d1992d2012-03-13 11:03:25 -070010162 }
10163 invalidate(false);
10164 } else {
10165 final AttachInfo ai = mAttachInfo;
10166 final ViewParent p = mParent;
10167 if (p != null && ai != null) {
10168 final Rect r = ai.mTmpInvalRect;
10169 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10170 if (mParent instanceof ViewGroup) {
10171 ((ViewGroup) mParent).invalidateChildFast(this, r);
10172 } else {
10173 mParent.invalidateChild(this, r);
10174 }
10175 }
10176 }
10177 }
10178
10179 /**
10180 * Utility method to transform a given Rect by the current matrix of this view.
10181 */
10182 void transformRect(final Rect rect) {
10183 if (!getMatrix().isIdentity()) {
10184 RectF boundingRect = mAttachInfo.mTmpTransformRect;
10185 boundingRect.set(rect);
10186 getMatrix().mapRect(boundingRect);
10187 rect.set((int) (boundingRect.left - 0.5f),
10188 (int) (boundingRect.top - 0.5f),
10189 (int) (boundingRect.right + 0.5f),
10190 (int) (boundingRect.bottom + 0.5f));
10191 }
10192 }
10193
10194 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -080010195 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -080010196 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10197 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -080010198 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
10199 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -080010200 *
10201 * @hide
10202 */
Romain Guy0fd89bf2011-01-26 15:41:30 -080010203 protected void invalidateParentCaches() {
10204 if (mParent instanceof View) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010205 ((View) mParent).mPrivateFlags |= PFLAG_INVALIDATED;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010206 }
10207 }
Joe Malin32736f02011-01-19 16:14:20 -080010208
Romain Guy0fd89bf2011-01-26 15:41:30 -080010209 /**
10210 * Used to indicate that the parent of this view should be invalidated. This functionality
10211 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10212 * which is necessary when various parent-managed properties of the view change, such as
10213 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
10214 * an invalidation event to the parent.
10215 *
10216 * @hide
10217 */
10218 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -080010219 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010220 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -080010221 }
10222 }
10223
10224 /**
Romain Guy24443ea2009-05-11 11:56:30 -070010225 * Indicates whether this View is opaque. An opaque View guarantees that it will
10226 * draw all the pixels overlapping its bounds using a fully opaque color.
10227 *
10228 * Subclasses of View should override this method whenever possible to indicate
10229 * whether an instance is opaque. Opaque Views are treated in a special way by
10230 * the View hierarchy, possibly allowing it to perform optimizations during
10231 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -070010232 *
Romain Guy24443ea2009-05-11 11:56:30 -070010233 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -070010234 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010235 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -070010236 public boolean isOpaque() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010237 return (mPrivateFlags & PFLAG_OPAQUE_MASK) == PFLAG_OPAQUE_MASK &&
Romain Guyf8773082012-07-12 18:01:00 -070010238 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1.0f) >= 1.0f);
Romain Guy8f1344f52009-05-15 16:03:59 -070010239 }
10240
Adam Powell20232d02010-12-08 21:08:53 -080010241 /**
10242 * @hide
10243 */
10244 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -070010245 // Opaque if:
10246 // - Has a background
10247 // - Background is opaque
10248 // - Doesn't have scrollbars or scrollbars are inside overlay
10249
Philip Milne6c8ea062012-04-03 17:38:43 -070010250 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010251 mPrivateFlags |= PFLAG_OPAQUE_BACKGROUND;
Romain Guy8f1344f52009-05-15 16:03:59 -070010252 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010253 mPrivateFlags &= ~PFLAG_OPAQUE_BACKGROUND;
Romain Guy8f1344f52009-05-15 16:03:59 -070010254 }
10255
10256 final int flags = mViewFlags;
10257 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
10258 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010259 mPrivateFlags |= PFLAG_OPAQUE_SCROLLBARS;
Romain Guy8f1344f52009-05-15 16:03:59 -070010260 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010261 mPrivateFlags &= ~PFLAG_OPAQUE_SCROLLBARS;
Romain Guy8f1344f52009-05-15 16:03:59 -070010262 }
10263 }
10264
10265 /**
10266 * @hide
10267 */
10268 protected boolean hasOpaqueScrollbars() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010269 return (mPrivateFlags & PFLAG_OPAQUE_SCROLLBARS) == PFLAG_OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -070010270 }
10271
10272 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010273 * @return A handler associated with the thread running the View. This
10274 * handler can be used to pump events in the UI events queue.
10275 */
10276 public Handler getHandler() {
10277 if (mAttachInfo != null) {
10278 return mAttachInfo.mHandler;
10279 }
10280 return null;
10281 }
10282
10283 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080010284 * Gets the view root associated with the View.
10285 * @return The view root, or null if none.
10286 * @hide
10287 */
10288 public ViewRootImpl getViewRootImpl() {
10289 if (mAttachInfo != null) {
10290 return mAttachInfo.mViewRootImpl;
10291 }
10292 return null;
10293 }
10294
10295 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010296 * <p>Causes the Runnable to be added to the message queue.
10297 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010298 *
Romain Guye63a4f32011-08-11 11:33:31 -070010299 * <p>This method can be invoked from outside of the UI thread
10300 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010301 *
10302 * @param action The Runnable that will be executed.
10303 *
10304 * @return Returns true if the Runnable was successfully placed in to the
10305 * message queue. Returns false on failure, usually because the
10306 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010307 *
10308 * @see #postDelayed
10309 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010310 */
10311 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010312 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010313 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010314 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010315 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010316 // Assume that post will succeed later
10317 ViewRootImpl.getRunQueue().post(action);
10318 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010319 }
10320
10321 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010322 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010323 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -070010324 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010325 *
Romain Guye63a4f32011-08-11 11:33:31 -070010326 * <p>This method can be invoked from outside of the UI thread
10327 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010328 *
10329 * @param action The Runnable that will be executed.
10330 * @param delayMillis The delay (in milliseconds) until the Runnable
10331 * will be executed.
10332 *
10333 * @return true if the Runnable was successfully placed in to the
10334 * message queue. Returns false on failure, usually because the
10335 * looper processing the message queue is exiting. Note that a
10336 * result of true does not mean the Runnable will be processed --
10337 * if the looper is quit before the delivery time of the message
10338 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010339 *
10340 * @see #post
10341 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010342 */
10343 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010344 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010345 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010346 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010347 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010348 // Assume that post will succeed later
10349 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10350 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010351 }
10352
10353 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010354 * <p>Causes the Runnable to execute on the next animation time step.
10355 * The runnable will be run on the user interface thread.</p>
10356 *
10357 * <p>This method can be invoked from outside of the UI thread
10358 * only when this View is attached to a window.</p>
10359 *
10360 * @param action The Runnable that will be executed.
10361 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010362 * @see #postOnAnimationDelayed
10363 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010364 */
10365 public void postOnAnimation(Runnable action) {
10366 final AttachInfo attachInfo = mAttachInfo;
10367 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010368 attachInfo.mViewRootImpl.mChoreographer.postCallback(
10369 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010370 } else {
10371 // Assume that post will succeed later
10372 ViewRootImpl.getRunQueue().post(action);
10373 }
10374 }
10375
10376 /**
10377 * <p>Causes the Runnable to execute on the next animation time step,
10378 * after the specified amount of time elapses.
10379 * The runnable will be run on the user interface thread.</p>
10380 *
10381 * <p>This method can be invoked from outside of the UI thread
10382 * only when this View is attached to a window.</p>
10383 *
10384 * @param action The Runnable that will be executed.
10385 * @param delayMillis The delay (in milliseconds) until the Runnable
10386 * will be executed.
10387 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010388 * @see #postOnAnimation
10389 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010390 */
10391 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
10392 final AttachInfo attachInfo = mAttachInfo;
10393 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010394 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
10395 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010396 } else {
10397 // Assume that post will succeed later
10398 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10399 }
10400 }
10401
10402 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010403 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010404 *
Romain Guye63a4f32011-08-11 11:33:31 -070010405 * <p>This method can be invoked from outside of the UI thread
10406 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010407 *
10408 * @param action The Runnable to remove from the message handling queue
10409 *
10410 * @return true if this view could ask the Handler to remove the Runnable,
10411 * false otherwise. When the returned value is true, the Runnable
10412 * may or may not have been actually removed from the message queue
10413 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010414 *
10415 * @see #post
10416 * @see #postDelayed
10417 * @see #postOnAnimation
10418 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010419 */
10420 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080010421 if (action != null) {
10422 final AttachInfo attachInfo = mAttachInfo;
10423 if (attachInfo != null) {
10424 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010425 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
10426 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -080010427 } else {
10428 // Assume that post will succeed later
10429 ViewRootImpl.getRunQueue().removeCallbacks(action);
10430 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010431 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010432 return true;
10433 }
10434
10435 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010436 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
10437 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010438 *
Romain Guye63a4f32011-08-11 11:33:31 -070010439 * <p>This method can be invoked from outside of the UI thread
10440 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010441 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010442 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010443 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010444 */
10445 public void postInvalidate() {
10446 postInvalidateDelayed(0);
10447 }
10448
10449 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010450 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10451 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010452 *
Romain Guye63a4f32011-08-11 11:33:31 -070010453 * <p>This method can be invoked from outside of the UI thread
10454 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010455 *
10456 * @param left The left coordinate of the rectangle to invalidate.
10457 * @param top The top coordinate of the rectangle to invalidate.
10458 * @param right The right coordinate of the rectangle to invalidate.
10459 * @param bottom The bottom coordinate of the rectangle to invalidate.
10460 *
10461 * @see #invalidate(int, int, int, int)
10462 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010463 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010464 */
10465 public void postInvalidate(int left, int top, int right, int bottom) {
10466 postInvalidateDelayed(0, left, top, right, bottom);
10467 }
10468
10469 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010470 * <p>Cause an invalidate to happen on a subsequent cycle through the event
10471 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010472 *
Romain Guye63a4f32011-08-11 11:33:31 -070010473 * <p>This method can be invoked from outside of the UI thread
10474 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010475 *
10476 * @param delayMilliseconds the duration in milliseconds to delay the
10477 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010478 *
10479 * @see #invalidate()
10480 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010481 */
10482 public void postInvalidateDelayed(long delayMilliseconds) {
10483 // We try only with the AttachInfo because there's no point in invalidating
10484 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010485 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010486 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010487 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010488 }
10489 }
10490
10491 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010492 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10493 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010494 *
Romain Guye63a4f32011-08-11 11:33:31 -070010495 * <p>This method can be invoked from outside of the UI thread
10496 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010497 *
10498 * @param delayMilliseconds the duration in milliseconds to delay the
10499 * invalidation by
10500 * @param left The left coordinate of the rectangle to invalidate.
10501 * @param top The top coordinate of the rectangle to invalidate.
10502 * @param right The right coordinate of the rectangle to invalidate.
10503 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010504 *
10505 * @see #invalidate(int, int, int, int)
10506 * @see #invalidate(Rect)
10507 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010508 */
10509 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
10510 int right, int bottom) {
10511
10512 // We try only with the AttachInfo because there's no point in invalidating
10513 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010514 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010515 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010516 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10517 info.target = this;
10518 info.left = left;
10519 info.top = top;
10520 info.right = right;
10521 info.bottom = bottom;
10522
Jeff Browna175a5b2012-02-15 19:18:31 -080010523 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010524 }
10525 }
10526
10527 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010528 * <p>Cause an invalidate to happen on the next animation time step, typically the
10529 * next display frame.</p>
10530 *
10531 * <p>This method can be invoked from outside of the UI thread
10532 * only when this View is attached to a window.</p>
10533 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010534 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010535 */
10536 public void postInvalidateOnAnimation() {
10537 // We try only with the AttachInfo because there's no point in invalidating
10538 // if we are not attached to our window
10539 final AttachInfo attachInfo = mAttachInfo;
10540 if (attachInfo != null) {
10541 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10542 }
10543 }
10544
10545 /**
10546 * <p>Cause an invalidate of the specified area to happen on the next animation
10547 * time step, typically the next display frame.</p>
10548 *
10549 * <p>This method can be invoked from outside of the UI thread
10550 * only when this View is attached to a window.</p>
10551 *
10552 * @param left The left coordinate of the rectangle to invalidate.
10553 * @param top The top coordinate of the rectangle to invalidate.
10554 * @param right The right coordinate of the rectangle to invalidate.
10555 * @param bottom The bottom coordinate of the rectangle to invalidate.
10556 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010557 * @see #invalidate(int, int, int, int)
10558 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010559 */
10560 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10561 // We try only with the AttachInfo because there's no point in invalidating
10562 // if we are not attached to our window
10563 final AttachInfo attachInfo = mAttachInfo;
10564 if (attachInfo != null) {
10565 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10566 info.target = this;
10567 info.left = left;
10568 info.top = top;
10569 info.right = right;
10570 info.bottom = bottom;
10571
10572 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10573 }
10574 }
10575
10576 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010577 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10578 * This event is sent at most once every
10579 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10580 */
10581 private void postSendViewScrolledAccessibilityEventCallback() {
10582 if (mSendViewScrolledAccessibilityEvent == null) {
10583 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10584 }
10585 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10586 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10587 postDelayed(mSendViewScrolledAccessibilityEvent,
10588 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10589 }
10590 }
10591
10592 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010593 * Called by a parent to request that a child update its values for mScrollX
10594 * and mScrollY if necessary. This will typically be done if the child is
10595 * animating a scroll using a {@link android.widget.Scroller Scroller}
10596 * object.
10597 */
10598 public void computeScroll() {
10599 }
10600
10601 /**
10602 * <p>Indicate whether the horizontal edges are faded when the view is
10603 * scrolled horizontally.</p>
10604 *
10605 * @return true if the horizontal edges should are faded on scroll, false
10606 * otherwise
10607 *
10608 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010609 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010610 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010611 */
10612 public boolean isHorizontalFadingEdgeEnabled() {
10613 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10614 }
10615
10616 /**
10617 * <p>Define whether the horizontal edges should be faded when this view
10618 * is scrolled horizontally.</p>
10619 *
10620 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10621 * be faded when the view is scrolled
10622 * horizontally
10623 *
10624 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010625 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010626 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010627 */
10628 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10629 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10630 if (horizontalFadingEdgeEnabled) {
10631 initScrollCache();
10632 }
10633
10634 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10635 }
10636 }
10637
10638 /**
10639 * <p>Indicate whether the vertical edges are faded when the view is
10640 * scrolled horizontally.</p>
10641 *
10642 * @return true if the vertical edges should are faded on scroll, false
10643 * otherwise
10644 *
10645 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010646 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010647 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010648 */
10649 public boolean isVerticalFadingEdgeEnabled() {
10650 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10651 }
10652
10653 /**
10654 * <p>Define whether the vertical edges should be faded when this view
10655 * is scrolled vertically.</p>
10656 *
10657 * @param verticalFadingEdgeEnabled true if the vertical edges should
10658 * be faded when the view is scrolled
10659 * vertically
10660 *
10661 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010662 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010663 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010664 */
10665 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10666 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10667 if (verticalFadingEdgeEnabled) {
10668 initScrollCache();
10669 }
10670
10671 mViewFlags ^= FADING_EDGE_VERTICAL;
10672 }
10673 }
10674
10675 /**
10676 * Returns the strength, or intensity, of the top faded edge. The strength is
10677 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10678 * returns 0.0 or 1.0 but no value in between.
10679 *
10680 * Subclasses should override this method to provide a smoother fade transition
10681 * when scrolling occurs.
10682 *
10683 * @return the intensity of the top fade as a float between 0.0f and 1.0f
10684 */
10685 protected float getTopFadingEdgeStrength() {
10686 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
10687 }
10688
10689 /**
10690 * Returns the strength, or intensity, of the bottom faded edge. The strength is
10691 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10692 * returns 0.0 or 1.0 but no value in between.
10693 *
10694 * Subclasses should override this method to provide a smoother fade transition
10695 * when scrolling occurs.
10696 *
10697 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
10698 */
10699 protected float getBottomFadingEdgeStrength() {
10700 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
10701 computeVerticalScrollRange() ? 1.0f : 0.0f;
10702 }
10703
10704 /**
10705 * Returns the strength, or intensity, of the left faded edge. The strength is
10706 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10707 * returns 0.0 or 1.0 but no value in between.
10708 *
10709 * Subclasses should override this method to provide a smoother fade transition
10710 * when scrolling occurs.
10711 *
10712 * @return the intensity of the left fade as a float between 0.0f and 1.0f
10713 */
10714 protected float getLeftFadingEdgeStrength() {
10715 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
10716 }
10717
10718 /**
10719 * Returns the strength, or intensity, of the right faded edge. The strength is
10720 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10721 * returns 0.0 or 1.0 but no value in between.
10722 *
10723 * Subclasses should override this method to provide a smoother fade transition
10724 * when scrolling occurs.
10725 *
10726 * @return the intensity of the right fade as a float between 0.0f and 1.0f
10727 */
10728 protected float getRightFadingEdgeStrength() {
10729 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
10730 computeHorizontalScrollRange() ? 1.0f : 0.0f;
10731 }
10732
10733 /**
10734 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
10735 * scrollbar is not drawn by default.</p>
10736 *
10737 * @return true if the horizontal scrollbar should be painted, false
10738 * otherwise
10739 *
10740 * @see #setHorizontalScrollBarEnabled(boolean)
10741 */
10742 public boolean isHorizontalScrollBarEnabled() {
10743 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10744 }
10745
10746 /**
10747 * <p>Define whether the horizontal scrollbar should be drawn or not. The
10748 * scrollbar is not drawn by default.</p>
10749 *
10750 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
10751 * be painted
10752 *
10753 * @see #isHorizontalScrollBarEnabled()
10754 */
10755 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
10756 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
10757 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010758 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010759 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010760 }
10761 }
10762
10763 /**
10764 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
10765 * scrollbar is not drawn by default.</p>
10766 *
10767 * @return true if the vertical scrollbar should be painted, false
10768 * otherwise
10769 *
10770 * @see #setVerticalScrollBarEnabled(boolean)
10771 */
10772 public boolean isVerticalScrollBarEnabled() {
10773 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
10774 }
10775
10776 /**
10777 * <p>Define whether the vertical scrollbar should be drawn or not. The
10778 * scrollbar is not drawn by default.</p>
10779 *
10780 * @param verticalScrollBarEnabled true if the vertical scrollbar should
10781 * be painted
10782 *
10783 * @see #isVerticalScrollBarEnabled()
10784 */
10785 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
10786 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
10787 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010788 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010789 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010790 }
10791 }
10792
Adam Powell20232d02010-12-08 21:08:53 -080010793 /**
10794 * @hide
10795 */
10796 protected void recomputePadding() {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070010797 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010798 }
Joe Malin32736f02011-01-19 16:14:20 -080010799
Mike Cleronfe81d382009-09-28 14:22:16 -070010800 /**
10801 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080010802 *
Mike Cleronfe81d382009-09-28 14:22:16 -070010803 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080010804 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010805 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070010806 */
10807 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
10808 initScrollCache();
10809 final ScrollabilityCache scrollabilityCache = mScrollCache;
10810 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010811 if (fadeScrollbars) {
10812 scrollabilityCache.state = ScrollabilityCache.OFF;
10813 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070010814 scrollabilityCache.state = ScrollabilityCache.ON;
10815 }
10816 }
Joe Malin32736f02011-01-19 16:14:20 -080010817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010818 /**
Joe Malin32736f02011-01-19 16:14:20 -080010819 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010820 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080010821 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010822 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070010823 *
10824 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070010825 */
10826 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080010827 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010828 }
Joe Malin32736f02011-01-19 16:14:20 -080010829
Mike Cleron52f0a642009-09-28 18:21:37 -070010830 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070010831 *
10832 * Returns the delay before scrollbars fade.
10833 *
10834 * @return the delay before scrollbars fade
10835 *
10836 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10837 */
10838 public int getScrollBarDefaultDelayBeforeFade() {
10839 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
10840 mScrollCache.scrollBarDefaultDelayBeforeFade;
10841 }
10842
10843 /**
10844 * Define the delay before scrollbars fade.
10845 *
10846 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
10847 *
10848 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10849 */
10850 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
10851 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
10852 }
10853
10854 /**
10855 *
10856 * Returns the scrollbar fade duration.
10857 *
10858 * @return the scrollbar fade duration
10859 *
10860 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10861 */
10862 public int getScrollBarFadeDuration() {
10863 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
10864 mScrollCache.scrollBarFadeDuration;
10865 }
10866
10867 /**
10868 * Define the scrollbar fade duration.
10869 *
10870 * @param scrollBarFadeDuration - the scrollbar fade duration
10871 *
10872 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10873 */
10874 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
10875 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
10876 }
10877
10878 /**
10879 *
10880 * Returns the scrollbar size.
10881 *
10882 * @return the scrollbar size
10883 *
10884 * @attr ref android.R.styleable#View_scrollbarSize
10885 */
10886 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070010887 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070010888 mScrollCache.scrollBarSize;
10889 }
10890
10891 /**
10892 * Define the scrollbar size.
10893 *
10894 * @param scrollBarSize - the scrollbar size
10895 *
10896 * @attr ref android.R.styleable#View_scrollbarSize
10897 */
10898 public void setScrollBarSize(int scrollBarSize) {
10899 getScrollCache().scrollBarSize = scrollBarSize;
10900 }
10901
10902 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010903 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
10904 * inset. When inset, they add to the padding of the view. And the scrollbars
10905 * can be drawn inside the padding area or on the edge of the view. For example,
10906 * if a view has a background drawable and you want to draw the scrollbars
10907 * inside the padding specified by the drawable, you can use
10908 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
10909 * appear at the edge of the view, ignoring the padding, then you can use
10910 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
10911 * @param style the style of the scrollbars. Should be one of
10912 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
10913 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
10914 * @see #SCROLLBARS_INSIDE_OVERLAY
10915 * @see #SCROLLBARS_INSIDE_INSET
10916 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10917 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010918 *
10919 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010920 */
10921 public void setScrollBarStyle(int style) {
10922 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
10923 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070010924 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010925 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010926 }
10927 }
10928
10929 /**
10930 * <p>Returns the current scrollbar style.</p>
10931 * @return the current scrollbar style
10932 * @see #SCROLLBARS_INSIDE_OVERLAY
10933 * @see #SCROLLBARS_INSIDE_INSET
10934 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10935 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010936 *
10937 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010938 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070010939 @ViewDebug.ExportedProperty(mapping = {
10940 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
10941 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
10942 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
10943 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
10944 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010945 public int getScrollBarStyle() {
10946 return mViewFlags & SCROLLBARS_STYLE_MASK;
10947 }
10948
10949 /**
10950 * <p>Compute the horizontal range that the horizontal scrollbar
10951 * represents.</p>
10952 *
10953 * <p>The range is expressed in arbitrary units that must be the same as the
10954 * units used by {@link #computeHorizontalScrollExtent()} and
10955 * {@link #computeHorizontalScrollOffset()}.</p>
10956 *
10957 * <p>The default range is the drawing width of this view.</p>
10958 *
10959 * @return the total horizontal range represented by the horizontal
10960 * scrollbar
10961 *
10962 * @see #computeHorizontalScrollExtent()
10963 * @see #computeHorizontalScrollOffset()
10964 * @see android.widget.ScrollBarDrawable
10965 */
10966 protected int computeHorizontalScrollRange() {
10967 return getWidth();
10968 }
10969
10970 /**
10971 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
10972 * within the horizontal range. This value is used to compute the position
10973 * of the thumb within the scrollbar's track.</p>
10974 *
10975 * <p>The range is expressed in arbitrary units that must be the same as the
10976 * units used by {@link #computeHorizontalScrollRange()} and
10977 * {@link #computeHorizontalScrollExtent()}.</p>
10978 *
10979 * <p>The default offset is the scroll offset of this view.</p>
10980 *
10981 * @return the horizontal offset of the scrollbar's thumb
10982 *
10983 * @see #computeHorizontalScrollRange()
10984 * @see #computeHorizontalScrollExtent()
10985 * @see android.widget.ScrollBarDrawable
10986 */
10987 protected int computeHorizontalScrollOffset() {
10988 return mScrollX;
10989 }
10990
10991 /**
10992 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
10993 * within the horizontal range. This value is used to compute the length
10994 * of the thumb within the scrollbar's track.</p>
10995 *
10996 * <p>The range is expressed in arbitrary units that must be the same as the
10997 * units used by {@link #computeHorizontalScrollRange()} and
10998 * {@link #computeHorizontalScrollOffset()}.</p>
10999 *
11000 * <p>The default extent is the drawing width of this view.</p>
11001 *
11002 * @return the horizontal extent of the scrollbar's thumb
11003 *
11004 * @see #computeHorizontalScrollRange()
11005 * @see #computeHorizontalScrollOffset()
11006 * @see android.widget.ScrollBarDrawable
11007 */
11008 protected int computeHorizontalScrollExtent() {
11009 return getWidth();
11010 }
11011
11012 /**
11013 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
11014 *
11015 * <p>The range is expressed in arbitrary units that must be the same as the
11016 * units used by {@link #computeVerticalScrollExtent()} and
11017 * {@link #computeVerticalScrollOffset()}.</p>
11018 *
11019 * @return the total vertical range represented by the vertical scrollbar
11020 *
11021 * <p>The default range is the drawing height of this view.</p>
11022 *
11023 * @see #computeVerticalScrollExtent()
11024 * @see #computeVerticalScrollOffset()
11025 * @see android.widget.ScrollBarDrawable
11026 */
11027 protected int computeVerticalScrollRange() {
11028 return getHeight();
11029 }
11030
11031 /**
11032 * <p>Compute the vertical offset of the vertical scrollbar's thumb
11033 * within the horizontal range. This value is used to compute the position
11034 * of the thumb within the scrollbar's track.</p>
11035 *
11036 * <p>The range is expressed in arbitrary units that must be the same as the
11037 * units used by {@link #computeVerticalScrollRange()} and
11038 * {@link #computeVerticalScrollExtent()}.</p>
11039 *
11040 * <p>The default offset is the scroll offset of this view.</p>
11041 *
11042 * @return the vertical offset of the scrollbar's thumb
11043 *
11044 * @see #computeVerticalScrollRange()
11045 * @see #computeVerticalScrollExtent()
11046 * @see android.widget.ScrollBarDrawable
11047 */
11048 protected int computeVerticalScrollOffset() {
11049 return mScrollY;
11050 }
11051
11052 /**
11053 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
11054 * within the vertical range. This value is used to compute the length
11055 * of the thumb within the scrollbar's track.</p>
11056 *
11057 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080011058 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011059 * {@link #computeVerticalScrollOffset()}.</p>
11060 *
11061 * <p>The default extent is the drawing height of this view.</p>
11062 *
11063 * @return the vertical extent of the scrollbar's thumb
11064 *
11065 * @see #computeVerticalScrollRange()
11066 * @see #computeVerticalScrollOffset()
11067 * @see android.widget.ScrollBarDrawable
11068 */
11069 protected int computeVerticalScrollExtent() {
11070 return getHeight();
11071 }
11072
11073 /**
Adam Powell69159442011-06-13 17:53:06 -070011074 * Check if this view can be scrolled horizontally in a certain direction.
11075 *
11076 * @param direction Negative to check scrolling left, positive to check scrolling right.
11077 * @return true if this view can be scrolled in the specified direction, false otherwise.
11078 */
11079 public boolean canScrollHorizontally(int direction) {
11080 final int offset = computeHorizontalScrollOffset();
11081 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
11082 if (range == 0) return false;
11083 if (direction < 0) {
11084 return offset > 0;
11085 } else {
11086 return offset < range - 1;
11087 }
11088 }
11089
11090 /**
11091 * Check if this view can be scrolled vertically in a certain direction.
11092 *
11093 * @param direction Negative to check scrolling up, positive to check scrolling down.
11094 * @return true if this view can be scrolled in the specified direction, false otherwise.
11095 */
11096 public boolean canScrollVertically(int direction) {
11097 final int offset = computeVerticalScrollOffset();
11098 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
11099 if (range == 0) return false;
11100 if (direction < 0) {
11101 return offset > 0;
11102 } else {
11103 return offset < range - 1;
11104 }
11105 }
11106
11107 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011108 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
11109 * scrollbars are painted only if they have been awakened first.</p>
11110 *
11111 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080011112 *
Mike Cleronf116bf82009-09-27 19:14:12 -070011113 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011114 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080011115 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011116 // scrollbars are drawn only when the animation is running
11117 final ScrollabilityCache cache = mScrollCache;
11118 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080011119
Mike Cleronf116bf82009-09-27 19:14:12 -070011120 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080011121
Mike Cleronf116bf82009-09-27 19:14:12 -070011122 if (state == ScrollabilityCache.OFF) {
11123 return;
11124 }
Joe Malin32736f02011-01-19 16:14:20 -080011125
Mike Cleronf116bf82009-09-27 19:14:12 -070011126 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080011127
Mike Cleronf116bf82009-09-27 19:14:12 -070011128 if (state == ScrollabilityCache.FADING) {
11129 // We're fading -- get our fade interpolation
11130 if (cache.interpolatorValues == null) {
11131 cache.interpolatorValues = new float[1];
11132 }
Joe Malin32736f02011-01-19 16:14:20 -080011133
Mike Cleronf116bf82009-09-27 19:14:12 -070011134 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080011135
Mike Cleronf116bf82009-09-27 19:14:12 -070011136 // Stops the animation if we're done
11137 if (cache.scrollBarInterpolator.timeToValues(values) ==
11138 Interpolator.Result.FREEZE_END) {
11139 cache.state = ScrollabilityCache.OFF;
11140 } else {
11141 cache.scrollBar.setAlpha(Math.round(values[0]));
11142 }
Joe Malin32736f02011-01-19 16:14:20 -080011143
11144 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070011145 // drawing. We only want this when we're fading so that
11146 // we prevent excessive redraws
11147 invalidate = true;
11148 } else {
11149 // We're just on -- but we may have been fading before so
11150 // reset alpha
11151 cache.scrollBar.setAlpha(255);
11152 }
11153
Joe Malin32736f02011-01-19 16:14:20 -080011154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011155 final int viewFlags = mViewFlags;
11156
11157 final boolean drawHorizontalScrollBar =
11158 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
11159 final boolean drawVerticalScrollBar =
11160 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
11161 && !isVerticalScrollBarHidden();
11162
11163 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
11164 final int width = mRight - mLeft;
11165 final int height = mBottom - mTop;
11166
11167 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011168
Mike Reede8853fc2009-09-04 14:01:48 -040011169 final int scrollX = mScrollX;
11170 final int scrollY = mScrollY;
11171 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
11172
Mike Cleronf116bf82009-09-27 19:14:12 -070011173 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080011174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011175 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011176 int size = scrollBar.getSize(false);
11177 if (size <= 0) {
11178 size = cache.scrollBarSize;
11179 }
11180
Mike Cleronf116bf82009-09-27 19:14:12 -070011181 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040011182 computeHorizontalScrollOffset(),
11183 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040011184 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070011185 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080011186 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070011187 left = scrollX + (mPaddingLeft & inside);
11188 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
11189 bottom = top + size;
11190 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
11191 if (invalidate) {
11192 invalidate(left, top, right, bottom);
11193 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011194 }
11195
11196 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011197 int size = scrollBar.getSize(true);
11198 if (size <= 0) {
11199 size = cache.scrollBarSize;
11200 }
11201
Mike Reede8853fc2009-09-04 14:01:48 -040011202 scrollBar.setParameters(computeVerticalScrollRange(),
11203 computeVerticalScrollOffset(),
11204 computeVerticalScrollExtent(), true);
Fabrice Di Meglioc91b6ca2012-06-22 14:51:15 -070011205 int verticalScrollbarPosition = mVerticalScrollbarPosition;
11206 if (verticalScrollbarPosition == SCROLLBAR_POSITION_DEFAULT) {
11207 verticalScrollbarPosition = isLayoutRtl() ?
11208 SCROLLBAR_POSITION_LEFT : SCROLLBAR_POSITION_RIGHT;
11209 }
11210 switch (verticalScrollbarPosition) {
Adam Powell20232d02010-12-08 21:08:53 -080011211 default:
Adam Powell20232d02010-12-08 21:08:53 -080011212 case SCROLLBAR_POSITION_RIGHT:
11213 left = scrollX + width - size - (mUserPaddingRight & inside);
11214 break;
11215 case SCROLLBAR_POSITION_LEFT:
11216 left = scrollX + (mUserPaddingLeft & inside);
11217 break;
11218 }
Mike Cleronf116bf82009-09-27 19:14:12 -070011219 top = scrollY + (mPaddingTop & inside);
11220 right = left + size;
11221 bottom = scrollY + height - (mUserPaddingBottom & inside);
11222 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
11223 if (invalidate) {
11224 invalidate(left, top, right, bottom);
11225 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011226 }
11227 }
11228 }
11229 }
Romain Guy8506ab42009-06-11 17:35:47 -070011230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011231 /**
Romain Guy8506ab42009-06-11 17:35:47 -070011232 * 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 -080011233 * FastScroller is visible.
11234 * @return whether to temporarily hide the vertical scrollbar
11235 * @hide
11236 */
11237 protected boolean isVerticalScrollBarHidden() {
11238 return false;
11239 }
11240
11241 /**
11242 * <p>Draw the horizontal scrollbar if
11243 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
11244 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011245 * @param canvas the canvas on which to draw the scrollbar
11246 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011247 *
11248 * @see #isHorizontalScrollBarEnabled()
11249 * @see #computeHorizontalScrollRange()
11250 * @see #computeHorizontalScrollExtent()
11251 * @see #computeHorizontalScrollOffset()
11252 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070011253 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011254 */
Romain Guy8fb95422010-08-17 18:38:51 -070011255 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
11256 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011257 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011258 scrollBar.draw(canvas);
11259 }
Mike Reede8853fc2009-09-04 14:01:48 -040011260
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011261 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011262 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
11263 * returns true.</p>
11264 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011265 * @param canvas the canvas on which to draw the scrollbar
11266 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011267 *
11268 * @see #isVerticalScrollBarEnabled()
11269 * @see #computeVerticalScrollRange()
11270 * @see #computeVerticalScrollExtent()
11271 * @see #computeVerticalScrollOffset()
11272 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040011273 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011274 */
Romain Guy8fb95422010-08-17 18:38:51 -070011275 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
11276 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040011277 scrollBar.setBounds(l, t, r, b);
11278 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011279 }
11280
11281 /**
11282 * Implement this to do your drawing.
11283 *
11284 * @param canvas the canvas on which the background will be drawn
11285 */
11286 protected void onDraw(Canvas canvas) {
11287 }
11288
11289 /*
11290 * Caller is responsible for calling requestLayout if necessary.
11291 * (This allows addViewInLayout to not request a new layout.)
11292 */
11293 void assignParent(ViewParent parent) {
11294 if (mParent == null) {
11295 mParent = parent;
11296 } else if (parent == null) {
11297 mParent = null;
11298 } else {
11299 throw new RuntimeException("view " + this + " being added, but"
11300 + " it already has a parent");
11301 }
11302 }
11303
11304 /**
11305 * This is called when the view is attached to a window. At this point it
11306 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011307 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
11308 * however it may be called any time before the first onDraw -- including
11309 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011310 *
11311 * @see #onDetachedFromWindow()
11312 */
11313 protected void onAttachedToWindow() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011314 if ((mPrivateFlags & PFLAG_REQUEST_TRANSPARENT_REGIONS) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011315 mParent.requestTransparentRegion(this);
11316 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011317
Dianne Hackborn4702a852012-08-17 15:18:29 -070011318 if ((mPrivateFlags & PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
Adam Powell8568c3a2010-04-19 14:26:11 -070011319 initialAwakenScrollBars();
Dianne Hackborn4702a852012-08-17 15:18:29 -070011320 mPrivateFlags &= ~PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH;
Adam Powell8568c3a2010-04-19 14:26:11 -070011321 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011322
Chet Haasea9b61ac2010-12-20 07:40:25 -080011323 jumpDrawablesToCurrentState();
Romain Guy2a0f2282012-05-08 14:43:12 -070011324
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011325 resolveRtlProperties();
Romain Guy2a0f2282012-05-08 14:43:12 -070011326
Svetoslav Ganov42138042012-03-20 11:51:39 -070011327 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070011328 if (isFocused()) {
11329 InputMethodManager imm = InputMethodManager.peekInstance();
11330 imm.focusIn(this);
11331 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011332
11333 if (mAttachInfo != null && mDisplayList != null) {
11334 mAttachInfo.mViewRootImpl.dequeueDisplayList(mDisplayList);
11335 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011336 }
Cibu Johny86666632010-02-22 13:01:02 -080011337
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011338 void resolveRtlProperties() {
11339 // Order is important here: LayoutDirection MUST be resolved first...
11340 resolveLayoutDirection();
11341 // ... then we can resolve the others properties depending on the resolved LayoutDirection.
11342 resolvePadding();
11343 resolveLayoutParams();
11344 resolveTextDirection();
11345 resolveTextAlignment();
11346 }
11347
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011348 /**
Romain Guybb9908b2012-03-08 11:14:07 -080011349 * @see #onScreenStateChanged(int)
11350 */
11351 void dispatchScreenStateChanged(int screenState) {
11352 onScreenStateChanged(screenState);
11353 }
11354
11355 /**
11356 * This method is called whenever the state of the screen this view is
11357 * attached to changes. A state change will usually occurs when the screen
11358 * turns on or off (whether it happens automatically or the user does it
11359 * manually.)
11360 *
11361 * @param screenState The new state of the screen. Can be either
11362 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
11363 */
11364 public void onScreenStateChanged(int screenState) {
11365 }
11366
11367 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011368 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
11369 */
11370 private boolean hasRtlSupport() {
11371 return mContext.getApplicationInfo().hasRtlSupport();
11372 }
11373
11374 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011375 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
11376 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011377 * Will call {@link View#onResolvedLayoutDirectionChanged} when resolution is done.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011378 */
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011379 public void resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011380 // Clear any previous layout direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070011381 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011382
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011383 if (hasRtlSupport()) {
11384 // Set resolved depending on layout direction
11385 switch (getLayoutDirection()) {
11386 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliob93911f2012-06-26 19:43:15 -070011387 // We cannot resolve yet. LTR is by default and let the resolution happen again
11388 // later to get the correct resolved value
11389 if (!canResolveLayoutDirection()) return;
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011390
Fabrice Di Megliob93911f2012-06-26 19:43:15 -070011391 ViewGroup viewGroup = ((ViewGroup) mParent);
11392
11393 // We cannot resolve yet on the parent too. LTR is by default and let the
11394 // resolution happen again later
11395 if (!viewGroup.canResolveLayoutDirection()) return;
11396
11397 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011398 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011399 }
11400 break;
11401 case LAYOUT_DIRECTION_RTL:
Dianne Hackborn4702a852012-08-17 15:18:29 -070011402 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011403 break;
11404 case LAYOUT_DIRECTION_LOCALE:
11405 if(isLayoutDirectionRtl(Locale.getDefault())) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011406 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011407 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011408 break;
11409 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011410 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011411 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011412 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011413
11414 // Set to resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070011415 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011416 onResolvedLayoutDirectionChanged();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011417 }
11418
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011419 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011420 * Called when layout direction has been resolved.
11421 *
11422 * The default implementation does nothing.
11423 */
11424 public void onResolvedLayoutDirectionChanged() {
11425 }
11426
11427 /**
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011428 * Return if padding has been resolved
11429 */
11430 boolean isPaddingResolved() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011431 return (mPrivateFlags2 & PFLAG2_PADDING_RESOLVED) != 0;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011432 }
11433
11434 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011435 * Resolve padding depending on layout direction.
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011436 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011437 public void resolvePadding() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011438 final int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion;
11439 if (targetSdkVersion < JELLY_BEAN_MR1 || !hasRtlSupport()) {
11440 // Pre Jelly Bean MR1 case (compatibility mode) OR no RTL support case:
11441 // left / right padding are used if defined. If they are not defined and start / end
11442 // padding are defined (e.g. in Frameworks resources), then we use start / end and
11443 // resolve them as left / right (layout direction is not taken into account).
11444 if (!mUserPaddingLeftDefined && mUserPaddingStart != UNDEFINED_PADDING) {
11445 mUserPaddingLeft = mUserPaddingStart;
11446 }
11447 if (!mUserPaddingRightDefined && mUserPaddingEnd != UNDEFINED_PADDING) {
11448 mUserPaddingRight = mUserPaddingEnd;
11449 }
11450
11451 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
11452
11453 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight,
11454 mUserPaddingBottom);
11455 } else {
11456 // Post Jelly Bean MR1 case: we need to take the resolved layout direction into account.
11457 // If start / end padding are defined, they will be resolved (hence overriding) to
11458 // left / right or right / left depending on the resolved layout direction.
11459 // If start / end padding are not defined, use the left / right ones.
11460 int resolvedLayoutDirection = getResolvedLayoutDirection();
11461 switch (resolvedLayoutDirection) {
11462 case LAYOUT_DIRECTION_RTL:
11463 if (mUserPaddingStart != UNDEFINED_PADDING) {
11464 mUserPaddingRight = mUserPaddingStart;
11465 }
11466 if (mUserPaddingEnd != UNDEFINED_PADDING) {
11467 mUserPaddingLeft = mUserPaddingEnd;
11468 }
11469 break;
11470 case LAYOUT_DIRECTION_LTR:
11471 default:
11472 if (mUserPaddingStart != UNDEFINED_PADDING) {
11473 mUserPaddingLeft = mUserPaddingStart;
11474 }
11475 if (mUserPaddingEnd != UNDEFINED_PADDING) {
11476 mUserPaddingRight = mUserPaddingEnd;
11477 }
11478 }
11479
11480 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
11481
11482 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight,
11483 mUserPaddingBottom);
11484 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011485 }
11486
Dianne Hackborn4702a852012-08-17 15:18:29 -070011487 mPrivateFlags2 |= PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011488 }
11489
11490 /**
11491 * Resolve padding depending on the layout direction. Subclasses that care about
11492 * padding resolution should override this method. The default implementation does
11493 * nothing.
11494 *
11495 * @param layoutDirection the direction of the layout
11496 *
Fabrice Di Meglioe8dc07d2012-03-09 17:10:19 -080011497 * @see {@link #LAYOUT_DIRECTION_LTR}
11498 * @see {@link #LAYOUT_DIRECTION_RTL}
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011499 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011500 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011501 }
11502
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011503 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011504 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011505 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011506 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011507 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011508 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011509 switch (getLayoutDirection()) {
11510 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011511 return (mParent != null) && (mParent instanceof ViewGroup);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011512 default:
11513 return true;
11514 }
11515 }
11516
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011517 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011518 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
11519 * when reset is done.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011520 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011521 public void resetResolvedLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070011522 // Reset the current resolved bits
Dianne Hackborn4702a852012-08-17 15:18:29 -070011523 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011524 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080011525 // Reset also the text direction
11526 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011527 }
11528
11529 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011530 * Called during reset of resolved layout direction.
11531 *
11532 * Subclasses need to override this method to clear cached information that depends on the
11533 * resolved layout direction, or to inform child views that inherit their layout direction.
11534 *
11535 * The default implementation does nothing.
11536 */
11537 public void onResolvedLayoutDirectionReset() {
11538 }
11539
11540 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011541 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011542 *
11543 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011544 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011545 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011546 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -080011547 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011548 }
11549
11550 /**
11551 * This is called when the view is detached from a window. At this point it
11552 * no longer has a surface for drawing.
11553 *
11554 * @see #onAttachedToWindow()
11555 */
11556 protected void onDetachedFromWindow() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011557 mPrivateFlags &= ~PFLAG_CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080011558
Romain Guya440b002010-02-24 15:57:54 -080011559 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050011560 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080011561 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070011562 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080011563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011564 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011565
Romain Guya998dff2012-03-23 18:58:36 -070011566 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011567
11568 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011569 if (mDisplayList != null) {
Romain Guy2a0f2282012-05-08 14:43:12 -070011570 mAttachInfo.mViewRootImpl.enqueueDisplayList(mDisplayList);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011571 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011572 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011573 } else {
Romain Guy38c2ece2012-05-24 14:20:56 -070011574 // Should never happen
11575 clearDisplayList();
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011576 }
11577
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011578 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011579
11580 resetResolvedLayoutDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011581 resetResolvedTextAlignment();
Svetoslav Ganov42138042012-03-20 11:51:39 -070011582 resetAccessibilityStateChanged();
Dianne Hackborn4702a852012-08-17 15:18:29 -070011583 mPrivateFlags2 &= ~PFLAG2_PADDING_RESOLVED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011584 }
11585
11586 /**
11587 * @return The number of times this view has been attached to a window
11588 */
11589 protected int getWindowAttachCount() {
11590 return mWindowAttachCount;
11591 }
11592
11593 /**
11594 * Retrieve a unique token identifying the window this view is attached to.
11595 * @return Return the window's token for use in
11596 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11597 */
11598 public IBinder getWindowToken() {
11599 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11600 }
11601
11602 /**
11603 * Retrieve a unique token identifying the top-level "real" window of
11604 * the window that this view is attached to. That is, this is like
11605 * {@link #getWindowToken}, except if the window this view in is a panel
11606 * window (attached to another containing window), then the token of
11607 * the containing window is returned instead.
11608 *
11609 * @return Returns the associated window token, either
11610 * {@link #getWindowToken()} or the containing window's token.
11611 */
11612 public IBinder getApplicationWindowToken() {
11613 AttachInfo ai = mAttachInfo;
11614 if (ai != null) {
11615 IBinder appWindowToken = ai.mPanelParentWindowToken;
11616 if (appWindowToken == null) {
11617 appWindowToken = ai.mWindowToken;
11618 }
11619 return appWindowToken;
11620 }
11621 return null;
11622 }
11623
11624 /**
11625 * Retrieve private session object this view hierarchy is using to
11626 * communicate with the window manager.
11627 * @return the session object to communicate with the window manager
11628 */
11629 /*package*/ IWindowSession getWindowSession() {
11630 return mAttachInfo != null ? mAttachInfo.mSession : null;
11631 }
11632
11633 /**
11634 * @param info the {@link android.view.View.AttachInfo} to associated with
11635 * this view
11636 */
11637 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
11638 //System.out.println("Attached! " + this);
11639 mAttachInfo = info;
11640 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011641 // We will need to evaluate the drawable state at least once.
Dianne Hackborn4702a852012-08-17 15:18:29 -070011642 mPrivateFlags |= PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011643 if (mFloatingTreeObserver != null) {
11644 info.mTreeObserver.merge(mFloatingTreeObserver);
11645 mFloatingTreeObserver = null;
11646 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070011647 if ((mPrivateFlags&PFLAG_SCROLL_CONTAINER) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011648 mAttachInfo.mScrollContainers.add(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -070011649 mPrivateFlags |= PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011650 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070011651 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011652 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080011653
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011654 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011655 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011656 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011657 if (listeners != null && listeners.size() > 0) {
11658 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11659 // perform the dispatching. The iterator is a safe guard against listeners that
11660 // could mutate the list by calling the various add/remove methods. This prevents
11661 // the array from being modified while we iterate it.
11662 for (OnAttachStateChangeListener listener : listeners) {
11663 listener.onViewAttachedToWindow(this);
11664 }
11665 }
11666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011667 int vis = info.mWindowVisibility;
11668 if (vis != GONE) {
11669 onWindowVisibilityChanged(vis);
11670 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070011671 if ((mPrivateFlags&PFLAG_DRAWABLE_STATE_DIRTY) != 0) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011672 // If nobody has evaluated the drawable state yet, then do it now.
11673 refreshDrawableState();
11674 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011675 }
11676
11677 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011678 AttachInfo info = mAttachInfo;
11679 if (info != null) {
11680 int vis = info.mWindowVisibility;
11681 if (vis != GONE) {
11682 onWindowVisibilityChanged(GONE);
11683 }
11684 }
11685
11686 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080011687
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011688 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011689 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011690 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011691 if (listeners != null && listeners.size() > 0) {
11692 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11693 // perform the dispatching. The iterator is a safe guard against listeners that
11694 // could mutate the list by calling the various add/remove methods. This prevents
11695 // the array from being modified while we iterate it.
11696 for (OnAttachStateChangeListener listener : listeners) {
11697 listener.onViewDetachedFromWindow(this);
11698 }
11699 }
11700
Dianne Hackborn4702a852012-08-17 15:18:29 -070011701 if ((mPrivateFlags & PFLAG_SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011702 mAttachInfo.mScrollContainers.remove(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -070011703 mPrivateFlags &= ~PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011704 }
Romain Guy01d5edc2011-01-28 11:28:53 -080011705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011706 mAttachInfo = null;
11707 }
11708
11709 /**
11710 * Store this view hierarchy's frozen state into the given container.
11711 *
11712 * @param container The SparseArray in which to save the view's state.
11713 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011714 * @see #restoreHierarchyState(android.util.SparseArray)
11715 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11716 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011717 */
11718 public void saveHierarchyState(SparseArray<Parcelable> container) {
11719 dispatchSaveInstanceState(container);
11720 }
11721
11722 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011723 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
11724 * this view and its children. May be overridden to modify how freezing happens to a
11725 * 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 -080011726 *
11727 * @param container The SparseArray in which to save the view's state.
11728 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011729 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11730 * @see #saveHierarchyState(android.util.SparseArray)
11731 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011732 */
11733 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
11734 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011735 mPrivateFlags &= ~PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011736 Parcelable state = onSaveInstanceState();
Dianne Hackborn4702a852012-08-17 15:18:29 -070011737 if ((mPrivateFlags & PFLAG_SAVE_STATE_CALLED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011738 throw new IllegalStateException(
11739 "Derived class did not call super.onSaveInstanceState()");
11740 }
11741 if (state != null) {
11742 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
11743 // + ": " + state);
11744 container.put(mID, state);
11745 }
11746 }
11747 }
11748
11749 /**
11750 * Hook allowing a view to generate a representation of its internal state
11751 * that can later be used to create a new instance with that same state.
11752 * This state should only contain information that is not persistent or can
11753 * not be reconstructed later. For example, you will never store your
11754 * current position on screen because that will be computed again when a
11755 * new instance of the view is placed in its view hierarchy.
11756 * <p>
11757 * Some examples of things you may store here: the current cursor position
11758 * in a text view (but usually not the text itself since that is stored in a
11759 * content provider or other persistent storage), the currently selected
11760 * item in a list view.
11761 *
11762 * @return Returns a Parcelable object containing the view's current dynamic
11763 * state, or null if there is nothing interesting to save. The
11764 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070011765 * @see #onRestoreInstanceState(android.os.Parcelable)
11766 * @see #saveHierarchyState(android.util.SparseArray)
11767 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011768 * @see #setSaveEnabled(boolean)
11769 */
11770 protected Parcelable onSaveInstanceState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011771 mPrivateFlags |= PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011772 return BaseSavedState.EMPTY_STATE;
11773 }
11774
11775 /**
11776 * Restore this view hierarchy's frozen state from the given container.
11777 *
11778 * @param container The SparseArray which holds previously frozen states.
11779 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011780 * @see #saveHierarchyState(android.util.SparseArray)
11781 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11782 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011783 */
11784 public void restoreHierarchyState(SparseArray<Parcelable> container) {
11785 dispatchRestoreInstanceState(container);
11786 }
11787
11788 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011789 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
11790 * state for this view and its children. May be overridden to modify how restoring
11791 * happens to a view's children; for example, some views may want to not store state
11792 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011793 *
11794 * @param container The SparseArray which holds previously saved state.
11795 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011796 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11797 * @see #restoreHierarchyState(android.util.SparseArray)
11798 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011799 */
11800 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
11801 if (mID != NO_ID) {
11802 Parcelable state = container.get(mID);
11803 if (state != null) {
11804 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
11805 // + ": " + state);
Dianne Hackborn4702a852012-08-17 15:18:29 -070011806 mPrivateFlags &= ~PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011807 onRestoreInstanceState(state);
Dianne Hackborn4702a852012-08-17 15:18:29 -070011808 if ((mPrivateFlags & PFLAG_SAVE_STATE_CALLED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011809 throw new IllegalStateException(
11810 "Derived class did not call super.onRestoreInstanceState()");
11811 }
11812 }
11813 }
11814 }
11815
11816 /**
11817 * Hook allowing a view to re-apply a representation of its internal state that had previously
11818 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
11819 * null state.
11820 *
11821 * @param state The frozen state that had previously been returned by
11822 * {@link #onSaveInstanceState}.
11823 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011824 * @see #onSaveInstanceState()
11825 * @see #restoreHierarchyState(android.util.SparseArray)
11826 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011827 */
11828 protected void onRestoreInstanceState(Parcelable state) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011829 mPrivateFlags |= PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011830 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080011831 throw new IllegalArgumentException("Wrong state class, expecting View State but "
11832 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080011833 + "when two views of different type have the same id in the same hierarchy. "
11834 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080011835 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011836 }
11837 }
11838
11839 /**
11840 * <p>Return the time at which the drawing of the view hierarchy started.</p>
11841 *
11842 * @return the drawing start time in milliseconds
11843 */
11844 public long getDrawingTime() {
11845 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
11846 }
11847
11848 /**
11849 * <p>Enables or disables the duplication of the parent's state into this view. When
11850 * duplication is enabled, this view gets its drawable state from its parent rather
11851 * than from its own internal properties.</p>
11852 *
11853 * <p>Note: in the current implementation, setting this property to true after the
11854 * view was added to a ViewGroup might have no effect at all. This property should
11855 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
11856 *
11857 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
11858 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011859 *
Gilles Debunnefb817032011-01-13 13:52:49 -080011860 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
11861 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011862 *
11863 * @param enabled True to enable duplication of the parent's drawable state, false
11864 * to disable it.
11865 *
11866 * @see #getDrawableState()
11867 * @see #isDuplicateParentStateEnabled()
11868 */
11869 public void setDuplicateParentStateEnabled(boolean enabled) {
11870 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
11871 }
11872
11873 /**
11874 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
11875 *
11876 * @return True if this view's drawable state is duplicated from the parent,
11877 * false otherwise
11878 *
11879 * @see #getDrawableState()
11880 * @see #setDuplicateParentStateEnabled(boolean)
11881 */
11882 public boolean isDuplicateParentStateEnabled() {
11883 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
11884 }
11885
11886 /**
Romain Guy171c5922011-01-06 10:04:23 -080011887 * <p>Specifies the type of layer backing this view. The layer can be
11888 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
11889 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011890 *
Romain Guy171c5922011-01-06 10:04:23 -080011891 * <p>A layer is associated with an optional {@link android.graphics.Paint}
11892 * instance that controls how the layer is composed on screen. The following
11893 * properties of the paint are taken into account when composing the layer:</p>
11894 * <ul>
11895 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
11896 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
11897 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
11898 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080011899 *
Romain Guy171c5922011-01-06 10:04:23 -080011900 * <p>If this view has an alpha value set to < 1.0 by calling
11901 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
11902 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
11903 * equivalent to setting a hardware layer on this view and providing a paint with
11904 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080011905 *
Romain Guy171c5922011-01-06 10:04:23 -080011906 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
11907 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
11908 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011909 *
Romain Guy171c5922011-01-06 10:04:23 -080011910 * @param layerType The ype of layer to use with this view, must be one of
11911 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11912 * {@link #LAYER_TYPE_HARDWARE}
11913 * @param paint The paint used to compose the layer. This argument is optional
11914 * and can be null. It is ignored when the layer type is
11915 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080011916 *
11917 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080011918 * @see #LAYER_TYPE_NONE
11919 * @see #LAYER_TYPE_SOFTWARE
11920 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080011921 * @see #setAlpha(float)
11922 *
Romain Guy171c5922011-01-06 10:04:23 -080011923 * @attr ref android.R.styleable#View_layerType
11924 */
11925 public void setLayerType(int layerType, Paint paint) {
11926 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080011927 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080011928 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
11929 }
Chet Haasedaf98e92011-01-10 14:10:36 -080011930
Romain Guyd6cd5722011-01-17 14:42:41 -080011931 if (layerType == mLayerType) {
11932 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
11933 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011934 invalidateParentCaches();
11935 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080011936 }
11937 return;
11938 }
Romain Guy171c5922011-01-06 10:04:23 -080011939
11940 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080011941 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070011942 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070011943 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011944 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080011945 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070011946 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011947 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080011948 default:
11949 break;
Romain Guy171c5922011-01-06 10:04:23 -080011950 }
11951
11952 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080011953 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
11954 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
11955 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080011956
Romain Guy0fd89bf2011-01-26 15:41:30 -080011957 invalidateParentCaches();
11958 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080011959 }
11960
11961 /**
Romain Guy59c7f802011-09-29 17:21:45 -070011962 * Indicates whether this view has a static layer. A view with layer type
11963 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
11964 * dynamic.
11965 */
11966 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080011967 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070011968 }
11969
11970 /**
Romain Guy171c5922011-01-06 10:04:23 -080011971 * Indicates what type of layer is currently associated with this view. By default
11972 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
11973 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
11974 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080011975 *
Romain Guy171c5922011-01-06 10:04:23 -080011976 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11977 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080011978 *
11979 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070011980 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080011981 * @see #LAYER_TYPE_NONE
11982 * @see #LAYER_TYPE_SOFTWARE
11983 * @see #LAYER_TYPE_HARDWARE
11984 */
11985 public int getLayerType() {
11986 return mLayerType;
11987 }
Joe Malin32736f02011-01-19 16:14:20 -080011988
Romain Guy6c319ca2011-01-11 14:29:25 -080011989 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080011990 * Forces this view's layer to be created and this view to be rendered
11991 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
11992 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070011993 *
Romain Guyf1ae1062011-03-02 18:16:04 -080011994 * This method can for instance be used to render a view into its layer before
11995 * starting an animation. If this view is complex, rendering into the layer
11996 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070011997 *
Romain Guyf1ae1062011-03-02 18:16:04 -080011998 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070011999 *
12000 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080012001 */
12002 public void buildLayer() {
12003 if (mLayerType == LAYER_TYPE_NONE) return;
12004
12005 if (mAttachInfo == null) {
12006 throw new IllegalStateException("This view must be attached to a window first");
12007 }
12008
12009 switch (mLayerType) {
12010 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080012011 if (mAttachInfo.mHardwareRenderer != null &&
12012 mAttachInfo.mHardwareRenderer.isEnabled() &&
12013 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080012014 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080012015 }
Romain Guyf1ae1062011-03-02 18:16:04 -080012016 break;
12017 case LAYER_TYPE_SOFTWARE:
12018 buildDrawingCache(true);
12019 break;
12020 }
12021 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012022
Romain Guyf1ae1062011-03-02 18:16:04 -080012023 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080012024 * <p>Returns a hardware layer that can be used to draw this view again
12025 * without executing its draw method.</p>
12026 *
12027 * @return A HardwareLayer ready to render, or null if an error occurred.
12028 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080012029 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070012030 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
12031 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080012032 return null;
12033 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012034
Romain Guy9c4b79a2011-11-10 19:23:58 -080012035 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080012036
12037 final int width = mRight - mLeft;
12038 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080012039
Romain Guy6c319ca2011-01-11 14:29:25 -080012040 if (width == 0 || height == 0) {
12041 return null;
12042 }
12043
Dianne Hackborn4702a852012-08-17 15:18:29 -070012044 if ((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
Romain Guy6c319ca2011-01-11 14:29:25 -080012045 if (mHardwareLayer == null) {
12046 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
12047 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070012048 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy846a5332012-07-11 17:44:57 -070012049 } else {
12050 if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
12051 mHardwareLayer.resize(width, height);
12052 mLocalDirtyRect.set(0, 0, width, height);
12053 }
12054
12055 // This should not be necessary but applications that change
12056 // the parameters of their background drawable without calling
12057 // this.setBackground(Drawable) can leave the view in a bad state
12058 // (for instance isOpaque() returns true, but the background is
12059 // not opaque.)
12060 computeOpaqueFlags();
12061
12062 final boolean opaque = isOpaque();
12063 if (mHardwareLayer.isOpaque() != opaque) {
12064 mHardwareLayer.setOpaque(opaque);
12065 mLocalDirtyRect.set(0, 0, width, height);
12066 }
Romain Guy6c319ca2011-01-11 14:29:25 -080012067 }
12068
Romain Guy5cd5c3f2011-10-17 17:10:02 -070012069 // The layer is not valid if the underlying GPU resources cannot be allocated
12070 if (!mHardwareLayer.isValid()) {
12071 return null;
12072 }
12073
Chet Haasea1cff502012-02-21 13:43:44 -080012074 mHardwareLayer.redraw(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
Michael Jurka7e52caf2012-03-06 15:57:06 -080012075 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080012076 }
12077
12078 return mHardwareLayer;
12079 }
Romain Guy171c5922011-01-06 10:04:23 -080012080
Romain Guy589b0bb2011-10-10 13:57:47 -070012081 /**
12082 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070012083 *
Romain Guy589b0bb2011-10-10 13:57:47 -070012084 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070012085 *
12086 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070012087 * @see #LAYER_TYPE_HARDWARE
12088 */
Romain Guya998dff2012-03-23 18:58:36 -070012089 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070012090 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012091 AttachInfo info = mAttachInfo;
12092 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070012093 info.mHardwareRenderer.isEnabled() &&
12094 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012095 mHardwareLayer.destroy();
12096 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080012097
Romain Guy9c4b79a2011-11-10 19:23:58 -080012098 invalidate(true);
12099 invalidateParentCaches();
12100 }
Romain Guy65b345f2011-07-27 18:51:50 -070012101 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070012102 }
Romain Guy65b345f2011-07-27 18:51:50 -070012103 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070012104 }
12105
Romain Guy171c5922011-01-06 10:04:23 -080012106 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080012107 * Destroys all hardware rendering resources. This method is invoked
12108 * when the system needs to reclaim resources. Upon execution of this
12109 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070012110 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012111 * Note: you <strong>must</strong> call
12112 * <code>super.destroyHardwareResources()</code> when overriding
12113 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070012114 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012115 * @hide
12116 */
12117 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070012118 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012119 }
12120
12121 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012122 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
12123 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
12124 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
12125 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
12126 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
12127 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012128 *
Romain Guy171c5922011-01-06 10:04:23 -080012129 * <p>Enabling the drawing cache is similar to
12130 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080012131 * acceleration is turned off. When hardware acceleration is turned on, enabling the
12132 * drawing cache has no effect on rendering because the system uses a different mechanism
12133 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
12134 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
12135 * for information on how to enable software and hardware layers.</p>
12136 *
12137 * <p>This API can be used to manually generate
12138 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
12139 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012140 *
12141 * @param enabled true to enable the drawing cache, false otherwise
12142 *
12143 * @see #isDrawingCacheEnabled()
12144 * @see #getDrawingCache()
12145 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080012146 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012147 */
12148 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012149 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012150 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
12151 }
12152
12153 /**
12154 * <p>Indicates whether the drawing cache is enabled for this view.</p>
12155 *
12156 * @return true if the drawing cache is enabled
12157 *
12158 * @see #setDrawingCacheEnabled(boolean)
12159 * @see #getDrawingCache()
12160 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012161 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012162 public boolean isDrawingCacheEnabled() {
12163 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
12164 }
12165
12166 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080012167 * Debugging utility which recursively outputs the dirty state of a view and its
12168 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080012169 *
Chet Haasedaf98e92011-01-10 14:10:36 -080012170 * @hide
12171 */
Romain Guy676b1732011-02-14 14:45:33 -080012172 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080012173 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012174 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.PFLAG_DIRTY_MASK) +
12175 ") DRAWN(" + (mPrivateFlags & PFLAG_DRAWN) + ")" + " CACHE_VALID(" +
12176 (mPrivateFlags & View.PFLAG_DRAWING_CACHE_VALID) +
12177 ") INVALIDATED(" + (mPrivateFlags & PFLAG_INVALIDATED) + ")");
Chet Haasedaf98e92011-01-10 14:10:36 -080012178 if (clear) {
12179 mPrivateFlags &= clearMask;
12180 }
12181 if (this instanceof ViewGroup) {
12182 ViewGroup parent = (ViewGroup) this;
12183 final int count = parent.getChildCount();
12184 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080012185 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080012186 child.outputDirtyFlags(indent + " ", clear, clearMask);
12187 }
12188 }
12189 }
12190
12191 /**
12192 * This method is used by ViewGroup to cause its children to restore or recreate their
12193 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
12194 * to recreate its own display list, which would happen if it went through the normal
12195 * draw/dispatchDraw mechanisms.
12196 *
12197 * @hide
12198 */
12199 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080012200
12201 /**
12202 * A view that is not attached or hardware accelerated cannot create a display list.
12203 * This method checks these conditions and returns the appropriate result.
12204 *
12205 * @return true if view has the ability to create a display list, false otherwise.
12206 *
12207 * @hide
12208 */
12209 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080012210 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080012211 }
Joe Malin32736f02011-01-19 16:14:20 -080012212
Chet Haasedaf98e92011-01-10 14:10:36 -080012213 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080012214 * @return The HardwareRenderer associated with that view or null if hardware rendering
12215 * is not supported or this this has not been attached to a window.
12216 *
12217 * @hide
12218 */
12219 public HardwareRenderer getHardwareRenderer() {
12220 if (mAttachInfo != null) {
12221 return mAttachInfo.mHardwareRenderer;
12222 }
12223 return null;
12224 }
12225
12226 /**
Chet Haasea1cff502012-02-21 13:43:44 -080012227 * Returns a DisplayList. If the incoming displayList is null, one will be created.
12228 * Otherwise, the same display list will be returned (after having been rendered into
12229 * along the way, depending on the invalidation state of the view).
12230 *
12231 * @param displayList The previous version of this displayList, could be null.
12232 * @param isLayer Whether the requester of the display list is a layer. If so,
12233 * the view will avoid creating a layer inside the resulting display list.
12234 * @return A new or reused DisplayList object.
12235 */
12236 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
12237 if (!canHaveDisplayList()) {
12238 return null;
12239 }
12240
Dianne Hackborn4702a852012-08-17 15:18:29 -070012241 if (((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 ||
Chet Haasea1cff502012-02-21 13:43:44 -080012242 displayList == null || !displayList.isValid() ||
12243 (!isLayer && mRecreateDisplayList))) {
12244 // Don't need to recreate the display list, just need to tell our
12245 // children to restore/recreate theirs
12246 if (displayList != null && displayList.isValid() &&
12247 !isLayer && !mRecreateDisplayList) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012248 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12249 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012250 dispatchGetDisplayList();
12251
12252 return displayList;
12253 }
12254
12255 if (!isLayer) {
12256 // If we got here, we're recreating it. Mark it as such to ensure that
12257 // we copy in child display lists into ours in drawChild()
12258 mRecreateDisplayList = true;
12259 }
12260 if (displayList == null) {
12261 final String name = getClass().getSimpleName();
12262 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
12263 // If we're creating a new display list, make sure our parent gets invalidated
12264 // since they will need to recreate their display list to account for this
12265 // new child display list.
12266 invalidateParentCaches();
12267 }
12268
12269 boolean caching = false;
12270 final HardwareCanvas canvas = displayList.start();
Chet Haasea1cff502012-02-21 13:43:44 -080012271 int width = mRight - mLeft;
12272 int height = mBottom - mTop;
12273
12274 try {
12275 canvas.setViewport(width, height);
12276 // The dirty rect should always be null for a display list
12277 canvas.onPreDraw(null);
Michael Jurkaba649742012-06-28 19:12:58 -070012278 int layerType = getLayerType();
Chet Haase1271e2c2012-04-20 09:54:27 -070012279 if (!isLayer && layerType != LAYER_TYPE_NONE) {
Chet Haaseb85967b2012-03-26 14:37:51 -070012280 if (layerType == LAYER_TYPE_HARDWARE) {
12281 final HardwareLayer layer = getHardwareLayer();
12282 if (layer != null && layer.isValid()) {
12283 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
12284 } else {
12285 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
12286 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
12287 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12288 }
12289 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080012290 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070012291 buildDrawingCache(true);
12292 Bitmap cache = getDrawingCache(true);
12293 if (cache != null) {
12294 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
12295 caching = true;
12296 }
Chet Haasea1cff502012-02-21 13:43:44 -080012297 }
Chet Haasea1cff502012-02-21 13:43:44 -080012298 } else {
12299
12300 computeScroll();
12301
Chet Haasea1cff502012-02-21 13:43:44 -080012302 canvas.translate(-mScrollX, -mScrollY);
12303 if (!isLayer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012304 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12305 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012306 }
12307
12308 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070012309 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
Chet Haasea1cff502012-02-21 13:43:44 -080012310 dispatchDraw(canvas);
12311 } else {
12312 draw(canvas);
12313 }
12314 }
12315 } finally {
Chet Haasea1cff502012-02-21 13:43:44 -080012316 canvas.onPostDraw();
12317
12318 displayList.end();
Chet Haase1271e2c2012-04-20 09:54:27 -070012319 displayList.setCaching(caching);
12320 if (isLayer) {
Chet Haasea1cff502012-02-21 13:43:44 -080012321 displayList.setLeftTopRightBottom(0, 0, width, height);
12322 } else {
12323 setDisplayListProperties(displayList);
12324 }
12325 }
12326 } else if (!isLayer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012327 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12328 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012329 }
12330
12331 return displayList;
12332 }
12333
12334 /**
12335 * Get the DisplayList for the HardwareLayer
12336 *
12337 * @param layer The HardwareLayer whose DisplayList we want
12338 * @return A DisplayList fopr the specified HardwareLayer
12339 */
12340 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
12341 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
12342 layer.setDisplayList(displayList);
12343 return displayList;
12344 }
12345
12346
12347 /**
Romain Guyb051e892010-09-28 19:09:36 -070012348 * <p>Returns a display list that can be used to draw this view again
12349 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012350 *
Romain Guyb051e892010-09-28 19:09:36 -070012351 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080012352 *
12353 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070012354 */
Chet Haasedaf98e92011-01-10 14:10:36 -080012355 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080012356 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070012357 return mDisplayList;
12358 }
12359
Romain Guy38c2ece2012-05-24 14:20:56 -070012360 private void clearDisplayList() {
12361 if (mDisplayList != null) {
12362 mDisplayList.invalidate();
12363 mDisplayList.clear();
12364 }
12365 }
12366
Romain Guyb051e892010-09-28 19:09:36 -070012367 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012368 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012369 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012370 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012371 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012372 * @see #getDrawingCache(boolean)
12373 */
12374 public Bitmap getDrawingCache() {
12375 return getDrawingCache(false);
12376 }
12377
12378 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012379 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
12380 * is null when caching is disabled. If caching is enabled and the cache is not ready,
12381 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
12382 * draw from the cache when the cache is enabled. To benefit from the cache, you must
12383 * request the drawing cache by calling this method and draw it on screen if the
12384 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012385 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012386 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12387 * this method will create a bitmap of the same size as this view. Because this bitmap
12388 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12389 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12390 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12391 * size than the view. This implies that your application must be able to handle this
12392 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012393 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012394 * @param autoScale Indicates whether the generated bitmap should be scaled based on
12395 * the current density of the screen when the application is in compatibility
12396 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012397 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012398 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012399 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012400 * @see #setDrawingCacheEnabled(boolean)
12401 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070012402 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012403 * @see #destroyDrawingCache()
12404 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012405 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012406 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
12407 return null;
12408 }
12409 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012410 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012411 }
Romain Guy02890fd2010-08-06 17:58:44 -070012412 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012413 }
12414
12415 /**
12416 * <p>Frees the resources used by the drawing cache. If you call
12417 * {@link #buildDrawingCache()} manually without calling
12418 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12419 * should cleanup the cache with this method afterwards.</p>
12420 *
12421 * @see #setDrawingCacheEnabled(boolean)
12422 * @see #buildDrawingCache()
12423 * @see #getDrawingCache()
12424 */
12425 public void destroyDrawingCache() {
12426 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012427 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012428 mDrawingCache = null;
12429 }
Romain Guyfbd8f692009-06-26 14:51:58 -070012430 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012431 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070012432 mUnscaledDrawingCache = null;
12433 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012434 }
12435
12436 /**
12437 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070012438 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012439 * view will always be drawn on top of a solid color.
12440 *
12441 * @param color The background color to use for the drawing cache's bitmap
12442 *
12443 * @see #setDrawingCacheEnabled(boolean)
12444 * @see #buildDrawingCache()
12445 * @see #getDrawingCache()
12446 */
12447 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080012448 if (color != mDrawingCacheBackgroundColor) {
12449 mDrawingCacheBackgroundColor = color;
Dianne Hackborn4702a852012-08-17 15:18:29 -070012450 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Romain Guy52e2ef82010-01-14 12:11:48 -080012451 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012452 }
12453
12454 /**
12455 * @see #setDrawingCacheBackgroundColor(int)
12456 *
12457 * @return The background color to used for the drawing cache's bitmap
12458 */
12459 public int getDrawingCacheBackgroundColor() {
12460 return mDrawingCacheBackgroundColor;
12461 }
12462
12463 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012464 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012465 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012466 * @see #buildDrawingCache(boolean)
12467 */
12468 public void buildDrawingCache() {
12469 buildDrawingCache(false);
12470 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080012471
Romain Guyfbd8f692009-06-26 14:51:58 -070012472 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012473 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
12474 *
12475 * <p>If you call {@link #buildDrawingCache()} manually without calling
12476 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12477 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012478 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012479 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12480 * this method will create a bitmap of the same size as this view. Because this bitmap
12481 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12482 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12483 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12484 * size than the view. This implies that your application must be able to handle this
12485 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012486 *
Romain Guy0d9275e2010-10-26 14:22:30 -070012487 * <p>You should avoid calling this method when hardware acceleration is enabled. If
12488 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080012489 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070012490 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012491 *
12492 * @see #getDrawingCache()
12493 * @see #destroyDrawingCache()
12494 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012495 public void buildDrawingCache(boolean autoScale) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012496 if ((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070012497 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012498 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012499
Romain Guy8506ab42009-06-11 17:35:47 -070012500 int width = mRight - mLeft;
12501 int height = mBottom - mTop;
12502
12503 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070012504 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070012505
Romain Guye1123222009-06-29 14:24:56 -070012506 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012507 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
12508 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070012509 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012510
12511 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070012512 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080012513 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012514
12515 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070012516 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080012517 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012518 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
12519 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080012520 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012521 return;
12522 }
12523
12524 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070012525 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012526
12527 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012528 Bitmap.Config quality;
12529 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080012530 // Never pick ARGB_4444 because it looks awful
12531 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012532 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
12533 case DRAWING_CACHE_QUALITY_AUTO:
12534 quality = Bitmap.Config.ARGB_8888;
12535 break;
12536 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080012537 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012538 break;
12539 case DRAWING_CACHE_QUALITY_HIGH:
12540 quality = Bitmap.Config.ARGB_8888;
12541 break;
12542 default:
12543 quality = Bitmap.Config.ARGB_8888;
12544 break;
12545 }
12546 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070012547 // Optimization for translucent windows
12548 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080012549 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012550 }
12551
12552 // Try to cleanup memory
12553 if (bitmap != null) bitmap.recycle();
12554
12555 try {
Dianne Hackborndde331c2012-08-03 14:01:57 -070012556 bitmap = Bitmap.createBitmap(mResources.getDisplayMetrics(),
12557 width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070012558 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070012559 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070012560 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012561 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070012562 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012563 }
Adam Powell26153a32010-11-08 15:22:27 -080012564 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012565 } catch (OutOfMemoryError e) {
12566 // If there is not enough memory to create the bitmap cache, just
12567 // ignore the issue as bitmap caches are not required to draw the
12568 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012569 if (autoScale) {
12570 mDrawingCache = null;
12571 } else {
12572 mUnscaledDrawingCache = null;
12573 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012574 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012575 return;
12576 }
12577
12578 clear = drawingCacheBackgroundColor != 0;
12579 }
12580
12581 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012582 if (attachInfo != null) {
12583 canvas = attachInfo.mCanvas;
12584 if (canvas == null) {
12585 canvas = new Canvas();
12586 }
12587 canvas.setBitmap(bitmap);
12588 // Temporarily clobber the cached Canvas in case one of our children
12589 // is also using a drawing cache. Without this, the children would
12590 // steal the canvas by attaching their own bitmap to it and bad, bad
12591 // thing would happen (invisible views, corrupted drawings, etc.)
12592 attachInfo.mCanvas = null;
12593 } else {
12594 // This case should hopefully never or seldom happen
12595 canvas = new Canvas(bitmap);
12596 }
12597
12598 if (clear) {
12599 bitmap.eraseColor(drawingCacheBackgroundColor);
12600 }
12601
12602 computeScroll();
12603 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080012604
Romain Guye1123222009-06-29 14:24:56 -070012605 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012606 final float scale = attachInfo.mApplicationScale;
12607 canvas.scale(scale, scale);
12608 }
Joe Malin32736f02011-01-19 16:14:20 -080012609
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012610 canvas.translate(-mScrollX, -mScrollY);
12611
Dianne Hackborn4702a852012-08-17 15:18:29 -070012612 mPrivateFlags |= PFLAG_DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080012613 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
12614 mLayerType != LAYER_TYPE_NONE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012615 mPrivateFlags |= PFLAG_DRAWING_CACHE_VALID;
Romain Guy0d9275e2010-10-26 14:22:30 -070012616 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012617
12618 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070012619 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
12620 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012621 dispatchDraw(canvas);
12622 } else {
12623 draw(canvas);
12624 }
12625
12626 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012627 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012628
12629 if (attachInfo != null) {
12630 // Restore the cached Canvas for our siblings
12631 attachInfo.mCanvas = canvas;
12632 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012633 }
12634 }
12635
12636 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012637 * Create a snapshot of the view into a bitmap. We should probably make
12638 * some form of this public, but should think about the API.
12639 */
Romain Guy223ff5c2010-03-02 17:07:47 -080012640 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012641 int width = mRight - mLeft;
12642 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012643
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012644 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070012645 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012646 width = (int) ((width * scale) + 0.5f);
12647 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080012648
Dianne Hackborndde331c2012-08-03 14:01:57 -070012649 Bitmap bitmap = Bitmap.createBitmap(mResources.getDisplayMetrics(),
12650 width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012651 if (bitmap == null) {
12652 throw new OutOfMemoryError();
12653 }
12654
Romain Guyc529d8d2011-09-06 15:01:39 -070012655 Resources resources = getResources();
12656 if (resources != null) {
12657 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
12658 }
Joe Malin32736f02011-01-19 16:14:20 -080012659
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012660 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012661 if (attachInfo != null) {
12662 canvas = attachInfo.mCanvas;
12663 if (canvas == null) {
12664 canvas = new Canvas();
12665 }
12666 canvas.setBitmap(bitmap);
12667 // Temporarily clobber the cached Canvas in case one of our children
12668 // is also using a drawing cache. Without this, the children would
12669 // steal the canvas by attaching their own bitmap to it and bad, bad
12670 // things would happen (invisible views, corrupted drawings, etc.)
12671 attachInfo.mCanvas = null;
12672 } else {
12673 // This case should hopefully never or seldom happen
12674 canvas = new Canvas(bitmap);
12675 }
12676
Romain Guy5bcdff42009-05-14 21:27:18 -070012677 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012678 bitmap.eraseColor(backgroundColor);
12679 }
12680
12681 computeScroll();
12682 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012683 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012684 canvas.translate(-mScrollX, -mScrollY);
12685
Romain Guy5bcdff42009-05-14 21:27:18 -070012686 // Temporarily remove the dirty mask
12687 int flags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070012688 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Romain Guy5bcdff42009-05-14 21:27:18 -070012689
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012690 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070012691 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012692 dispatchDraw(canvas);
12693 } else {
12694 draw(canvas);
12695 }
12696
Romain Guy5bcdff42009-05-14 21:27:18 -070012697 mPrivateFlags = flags;
12698
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012699 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012700 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012701
12702 if (attachInfo != null) {
12703 // Restore the cached Canvas for our siblings
12704 attachInfo.mCanvas = canvas;
12705 }
Romain Guy8506ab42009-06-11 17:35:47 -070012706
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012707 return bitmap;
12708 }
12709
12710 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012711 * Indicates whether this View is currently in edit mode. A View is usually
12712 * in edit mode when displayed within a developer tool. For instance, if
12713 * this View is being drawn by a visual user interface builder, this method
12714 * should return true.
12715 *
12716 * Subclasses should check the return value of this method to provide
12717 * different behaviors if their normal behavior might interfere with the
12718 * host environment. For instance: the class spawns a thread in its
12719 * constructor, the drawing code relies on device-specific features, etc.
12720 *
12721 * This method is usually checked in the drawing code of custom widgets.
12722 *
12723 * @return True if this View is in edit mode, false otherwise.
12724 */
12725 public boolean isInEditMode() {
12726 return false;
12727 }
12728
12729 /**
12730 * If the View draws content inside its padding and enables fading edges,
12731 * it needs to support padding offsets. Padding offsets are added to the
12732 * fading edges to extend the length of the fade so that it covers pixels
12733 * drawn inside the padding.
12734 *
12735 * Subclasses of this class should override this method if they need
12736 * to draw content inside the padding.
12737 *
12738 * @return True if padding offset must be applied, false otherwise.
12739 *
12740 * @see #getLeftPaddingOffset()
12741 * @see #getRightPaddingOffset()
12742 * @see #getTopPaddingOffset()
12743 * @see #getBottomPaddingOffset()
12744 *
12745 * @since CURRENT
12746 */
12747 protected boolean isPaddingOffsetRequired() {
12748 return false;
12749 }
12750
12751 /**
12752 * Amount by which to extend the left fading region. Called only when
12753 * {@link #isPaddingOffsetRequired()} returns true.
12754 *
12755 * @return The left padding offset in pixels.
12756 *
12757 * @see #isPaddingOffsetRequired()
12758 *
12759 * @since CURRENT
12760 */
12761 protected int getLeftPaddingOffset() {
12762 return 0;
12763 }
12764
12765 /**
12766 * Amount by which to extend the right fading region. Called only when
12767 * {@link #isPaddingOffsetRequired()} returns true.
12768 *
12769 * @return The right padding offset in pixels.
12770 *
12771 * @see #isPaddingOffsetRequired()
12772 *
12773 * @since CURRENT
12774 */
12775 protected int getRightPaddingOffset() {
12776 return 0;
12777 }
12778
12779 /**
12780 * Amount by which to extend the top fading region. Called only when
12781 * {@link #isPaddingOffsetRequired()} returns true.
12782 *
12783 * @return The top padding offset in pixels.
12784 *
12785 * @see #isPaddingOffsetRequired()
12786 *
12787 * @since CURRENT
12788 */
12789 protected int getTopPaddingOffset() {
12790 return 0;
12791 }
12792
12793 /**
12794 * Amount by which to extend the bottom fading region. Called only when
12795 * {@link #isPaddingOffsetRequired()} returns true.
12796 *
12797 * @return The bottom padding offset in pixels.
12798 *
12799 * @see #isPaddingOffsetRequired()
12800 *
12801 * @since CURRENT
12802 */
12803 protected int getBottomPaddingOffset() {
12804 return 0;
12805 }
12806
12807 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070012808 * @hide
12809 * @param offsetRequired
12810 */
12811 protected int getFadeTop(boolean offsetRequired) {
12812 int top = mPaddingTop;
12813 if (offsetRequired) top += getTopPaddingOffset();
12814 return top;
12815 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012816
Romain Guyf2fc4602011-07-19 15:20:03 -070012817 /**
12818 * @hide
12819 * @param offsetRequired
12820 */
12821 protected int getFadeHeight(boolean offsetRequired) {
12822 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070012823 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070012824 return mBottom - mTop - mPaddingBottom - padding;
12825 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012826
Romain Guyf2fc4602011-07-19 15:20:03 -070012827 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012828 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070012829 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012830 *
Romain Guy2bffd262010-09-12 17:40:02 -070012831 * <p>Even if this method returns true, it does not mean that every call
12832 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
12833 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012834 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070012835 * window is hardware accelerated,
12836 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
12837 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012838 *
Romain Guy2bffd262010-09-12 17:40:02 -070012839 * @return True if the view is attached to a window and the window is
12840 * hardware accelerated; false in any other case.
12841 */
12842 public boolean isHardwareAccelerated() {
12843 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
12844 }
Joe Malin32736f02011-01-19 16:14:20 -080012845
Romain Guy2bffd262010-09-12 17:40:02 -070012846 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080012847 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
12848 * case of an active Animation being run on the view.
12849 */
12850 private boolean drawAnimation(ViewGroup parent, long drawingTime,
12851 Animation a, boolean scalingRequired) {
12852 Transformation invalidationTransform;
12853 final int flags = parent.mGroupFlags;
12854 final boolean initialized = a.isInitialized();
12855 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070012856 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080012857 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
Romain Guy393a52c2012-05-22 20:21:08 -070012858 if (mAttachInfo != null) a.setListenerHandler(mAttachInfo.mHandler);
Chet Haasebcca79a2012-02-14 08:45:14 -080012859 onAnimationStart();
12860 }
12861
12862 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
12863 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
12864 if (parent.mInvalidationTransformation == null) {
12865 parent.mInvalidationTransformation = new Transformation();
12866 }
12867 invalidationTransform = parent.mInvalidationTransformation;
12868 a.getTransformation(drawingTime, invalidationTransform, 1f);
12869 } else {
12870 invalidationTransform = parent.mChildTransformation;
12871 }
Romain Guy393a52c2012-05-22 20:21:08 -070012872
Chet Haasebcca79a2012-02-14 08:45:14 -080012873 if (more) {
12874 if (!a.willChangeBounds()) {
Romain Guyf0af1d52012-07-11 18:31:21 -070012875 if ((flags & (ViewGroup.FLAG_OPTIMIZE_INVALIDATE | ViewGroup.FLAG_ANIMATION_DONE)) ==
12876 ViewGroup.FLAG_OPTIMIZE_INVALIDATE) {
12877 parent.mGroupFlags |= ViewGroup.FLAG_INVALIDATE_REQUIRED;
12878 } else if ((flags & ViewGroup.FLAG_INVALIDATE_REQUIRED) == 0) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012879 // The child need to draw an animation, potentially offscreen, so
12880 // make sure we do not cancel invalidate requests
Dianne Hackborn4702a852012-08-17 15:18:29 -070012881 parent.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
Chet Haasebcca79a2012-02-14 08:45:14 -080012882 parent.invalidate(mLeft, mTop, mRight, mBottom);
12883 }
12884 } else {
12885 if (parent.mInvalidateRegion == null) {
12886 parent.mInvalidateRegion = new RectF();
12887 }
12888 final RectF region = parent.mInvalidateRegion;
12889 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
12890 invalidationTransform);
12891
12892 // The child need to draw an animation, potentially offscreen, so
12893 // make sure we do not cancel invalidate requests
Dianne Hackborn4702a852012-08-17 15:18:29 -070012894 parent.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
Chet Haasebcca79a2012-02-14 08:45:14 -080012895
12896 final int left = mLeft + (int) region.left;
12897 final int top = mTop + (int) region.top;
12898 parent.invalidate(left, top, left + (int) (region.width() + .5f),
12899 top + (int) (region.height() + .5f));
12900 }
12901 }
12902 return more;
12903 }
12904
Chet Haasea1cff502012-02-21 13:43:44 -080012905 /**
12906 * This method is called by getDisplayList() when a display list is created or re-rendered.
12907 * It sets or resets the current value of all properties on that display list (resetting is
12908 * necessary when a display list is being re-created, because we need to make sure that
12909 * previously-set transform values
12910 */
12911 void setDisplayListProperties(DisplayList displayList) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012912 if (displayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012913 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070012914 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080012915 if (mParent instanceof ViewGroup) {
12916 displayList.setClipChildren(
12917 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
12918 }
Chet Haase9420abd2012-03-29 16:28:32 -070012919 float alpha = 1;
12920 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
12921 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
12922 ViewGroup parentVG = (ViewGroup) mParent;
12923 final boolean hasTransform =
12924 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
12925 if (hasTransform) {
12926 Transformation transform = parentVG.mChildTransformation;
12927 final int transformType = parentVG.mChildTransformation.getTransformationType();
12928 if (transformType != Transformation.TYPE_IDENTITY) {
12929 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
12930 alpha = transform.getAlpha();
12931 }
12932 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
12933 displayList.setStaticMatrix(transform.getMatrix());
12934 }
12935 }
12936 }
Chet Haasea1cff502012-02-21 13:43:44 -080012937 }
12938 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070012939 alpha *= mTransformationInfo.mAlpha;
12940 if (alpha < 1) {
12941 final int multipliedAlpha = (int) (255 * alpha);
12942 if (onSetAlpha(multipliedAlpha)) {
12943 alpha = 1;
12944 }
12945 }
12946 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080012947 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
12948 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
12949 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
12950 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070012951 if (mTransformationInfo.mCamera == null) {
12952 mTransformationInfo.mCamera = new Camera();
12953 mTransformationInfo.matrix3D = new Matrix();
12954 }
12955 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Dianne Hackborn4702a852012-08-17 15:18:29 -070012956 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == PFLAG_PIVOT_EXPLICITLY_SET) {
Chet Haasea1cff502012-02-21 13:43:44 -080012957 displayList.setPivotX(getPivotX());
12958 displayList.setPivotY(getPivotY());
12959 }
Chet Haase9420abd2012-03-29 16:28:32 -070012960 } else if (alpha < 1) {
12961 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080012962 }
12963 }
12964 }
12965
Chet Haasebcca79a2012-02-14 08:45:14 -080012966 /**
Chet Haase64a48c12012-02-13 16:33:29 -080012967 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
12968 * This draw() method is an implementation detail and is not intended to be overridden or
12969 * to be called from anywhere else other than ViewGroup.drawChild().
12970 */
12971 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012972 boolean useDisplayListProperties = mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080012973 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080012974 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080012975 final int flags = parent.mGroupFlags;
12976
Chet Haasea1cff502012-02-21 13:43:44 -080012977 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080012978 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080012979 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012980 }
12981
12982 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080012983 boolean concatMatrix = false;
12984
12985 boolean scalingRequired = false;
12986 boolean caching;
Michael Jurkaba649742012-06-28 19:12:58 -070012987 int layerType = getLayerType();
Chet Haase64a48c12012-02-13 16:33:29 -080012988
12989 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080012990 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
12991 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080012992 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070012993 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080012994 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
12995 } else {
12996 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
12997 }
12998
Chet Haasebcca79a2012-02-14 08:45:14 -080012999 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080013000 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013001 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080013002 concatMatrix = a.willChangeTransformationMatrix();
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013003 if (concatMatrix) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013004 mPrivateFlags3 |= PFLAG3_VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013005 }
Chet Haasebcca79a2012-02-14 08:45:14 -080013006 transformToApply = parent.mChildTransformation;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013007 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013008 if ((mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_TRANSFORM) == PFLAG3_VIEW_IS_ANIMATING_TRANSFORM &&
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013009 mDisplayList != null) {
13010 // No longer animating: clear out old animation matrix
13011 mDisplayList.setAnimationMatrix(null);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013012 mPrivateFlags3 &= ~PFLAG3_VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013013 }
13014 if (!useDisplayListProperties &&
13015 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
13016 final boolean hasTransform =
13017 parent.getChildStaticTransformation(this, parent.mChildTransformation);
13018 if (hasTransform) {
13019 final int transformType = parent.mChildTransformation.getTransformationType();
13020 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
13021 parent.mChildTransformation : null;
13022 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
13023 }
Chet Haase64a48c12012-02-13 16:33:29 -080013024 }
13025 }
13026
13027 concatMatrix |= !childHasIdentityMatrix;
13028
13029 // Sets the flag as early as possible to allow draw() implementations
13030 // to call invalidate() successfully when doing animations
Dianne Hackborn4702a852012-08-17 15:18:29 -070013031 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase64a48c12012-02-13 16:33:29 -080013032
Chet Haase599913d2012-07-23 16:22:05 -070013033 if (!concatMatrix && (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) == 0 &&
13034 canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Dianne Hackborn4702a852012-08-17 15:18:29 -070013035 (mPrivateFlags & PFLAG_DRAW_ANIMATION) == 0) {
13036 mPrivateFlags2 |= PFLAG2_VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013037 return more;
13038 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013039 mPrivateFlags2 &= ~PFLAG2_VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013040
13041 if (hardwareAccelerated) {
13042 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
13043 // retain the flag's value temporarily in the mRecreateDisplayList flag
Dianne Hackborn4702a852012-08-17 15:18:29 -070013044 mRecreateDisplayList = (mPrivateFlags & PFLAG_INVALIDATED) == PFLAG_INVALIDATED;
13045 mPrivateFlags &= ~PFLAG_INVALIDATED;
Chet Haase64a48c12012-02-13 16:33:29 -080013046 }
13047
Chet Haase64a48c12012-02-13 16:33:29 -080013048 DisplayList displayList = null;
13049 Bitmap cache = null;
13050 boolean hasDisplayList = false;
13051 if (caching) {
13052 if (!hardwareAccelerated) {
13053 if (layerType != LAYER_TYPE_NONE) {
13054 layerType = LAYER_TYPE_SOFTWARE;
13055 buildDrawingCache(true);
13056 }
13057 cache = getDrawingCache(true);
13058 } else {
13059 switch (layerType) {
13060 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070013061 if (useDisplayListProperties) {
13062 hasDisplayList = canHaveDisplayList();
13063 } else {
13064 buildDrawingCache(true);
13065 cache = getDrawingCache(true);
13066 }
Chet Haase64a48c12012-02-13 16:33:29 -080013067 break;
Chet Haasea1cff502012-02-21 13:43:44 -080013068 case LAYER_TYPE_HARDWARE:
13069 if (useDisplayListProperties) {
13070 hasDisplayList = canHaveDisplayList();
13071 }
13072 break;
Chet Haase64a48c12012-02-13 16:33:29 -080013073 case LAYER_TYPE_NONE:
13074 // Delay getting the display list until animation-driven alpha values are
13075 // set up and possibly passed on to the view
13076 hasDisplayList = canHaveDisplayList();
13077 break;
13078 }
13079 }
13080 }
Chet Haasea1cff502012-02-21 13:43:44 -080013081 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070013082 if (useDisplayListProperties) {
13083 displayList = getDisplayList();
13084 if (!displayList.isValid()) {
13085 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13086 // to getDisplayList(), the display list will be marked invalid and we should not
13087 // try to use it again.
13088 displayList = null;
13089 hasDisplayList = false;
13090 useDisplayListProperties = false;
13091 }
13092 }
Chet Haase64a48c12012-02-13 16:33:29 -080013093
Chet Haase526057b2012-07-12 17:50:41 -070013094 int sx = 0;
13095 int sy = 0;
13096 if (!hasDisplayList) {
13097 computeScroll();
13098 sx = mScrollX;
13099 sy = mScrollY;
13100 }
13101
Chet Haase64a48c12012-02-13 16:33:29 -080013102 final boolean hasNoCache = cache == null || hasDisplayList;
13103 final boolean offsetForScroll = cache == null && !hasDisplayList &&
13104 layerType != LAYER_TYPE_HARDWARE;
13105
Chet Haasea1cff502012-02-21 13:43:44 -080013106 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070013107 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013108 restoreTo = canvas.save();
13109 }
Chet Haase64a48c12012-02-13 16:33:29 -080013110 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013111 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080013112 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080013113 if (!useDisplayListProperties) {
13114 canvas.translate(mLeft, mTop);
13115 }
Chet Haase64a48c12012-02-13 16:33:29 -080013116 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080013117 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070013118 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080013119 restoreTo = canvas.save();
13120 }
Chet Haase64a48c12012-02-13 16:33:29 -080013121 // mAttachInfo cannot be null, otherwise scalingRequired == false
13122 final float scale = 1.0f / mAttachInfo.mApplicationScale;
13123 canvas.scale(scale, scale);
13124 }
13125 }
13126
Chet Haasea1cff502012-02-21 13:43:44 -080013127 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase21433372012-06-05 07:54:09 -070013128 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix() ||
Dianne Hackborn4702a852012-08-17 15:18:29 -070013129 (mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_ALPHA) == PFLAG3_VIEW_IS_ANIMATING_ALPHA) {
Chet Haase64a48c12012-02-13 16:33:29 -080013130 if (transformToApply != null || !childHasIdentityMatrix) {
13131 int transX = 0;
13132 int transY = 0;
13133
13134 if (offsetForScroll) {
13135 transX = -sx;
13136 transY = -sy;
13137 }
13138
13139 if (transformToApply != null) {
13140 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070013141 if (useDisplayListProperties) {
13142 displayList.setAnimationMatrix(transformToApply.getMatrix());
13143 } else {
13144 // Undo the scroll translation, apply the transformation matrix,
13145 // then redo the scroll translate to get the correct result.
13146 canvas.translate(-transX, -transY);
13147 canvas.concat(transformToApply.getMatrix());
13148 canvas.translate(transX, transY);
13149 }
Chet Haasea1cff502012-02-21 13:43:44 -080013150 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013151 }
13152
13153 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070013154 if (transformAlpha < 1) {
Chet Haase21433372012-06-05 07:54:09 -070013155 alpha *= transformAlpha;
Chet Haasea1cff502012-02-21 13:43:44 -080013156 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013157 }
13158 }
13159
Chet Haasea1cff502012-02-21 13:43:44 -080013160 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013161 canvas.translate(-transX, -transY);
13162 canvas.concat(getMatrix());
13163 canvas.translate(transX, transY);
13164 }
13165 }
13166
Chet Haase21433372012-06-05 07:54:09 -070013167 // Deal with alpha if it is or used to be <1
13168 if (alpha < 1 ||
Dianne Hackborn4702a852012-08-17 15:18:29 -070013169 (mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_ALPHA) == PFLAG3_VIEW_IS_ANIMATING_ALPHA) {
Chet Haase21433372012-06-05 07:54:09 -070013170 if (alpha < 1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013171 mPrivateFlags3 |= PFLAG3_VIEW_IS_ANIMATING_ALPHA;
Chet Haase21433372012-06-05 07:54:09 -070013172 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013173 mPrivateFlags3 &= ~PFLAG3_VIEW_IS_ANIMATING_ALPHA;
Chet Haase21433372012-06-05 07:54:09 -070013174 }
Chet Haasea1cff502012-02-21 13:43:44 -080013175 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013176 if (hasNoCache) {
13177 final int multipliedAlpha = (int) (255 * alpha);
13178 if (!onSetAlpha(multipliedAlpha)) {
13179 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080013180 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080013181 layerType != LAYER_TYPE_NONE) {
13182 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
13183 }
Chet Haase9420abd2012-03-29 16:28:32 -070013184 if (useDisplayListProperties) {
13185 displayList.setAlpha(alpha * getAlpha());
13186 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070013187 final int scrollX = hasDisplayList ? 0 : sx;
13188 final int scrollY = hasDisplayList ? 0 : sy;
13189 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
13190 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080013191 }
13192 } else {
13193 // Alpha is handled by the child directly, clobber the layer's alpha
Dianne Hackborn4702a852012-08-17 15:18:29 -070013194 mPrivateFlags |= PFLAG_ALPHA_SET;
Chet Haase64a48c12012-02-13 16:33:29 -080013195 }
13196 }
13197 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013198 } else if ((mPrivateFlags & PFLAG_ALPHA_SET) == PFLAG_ALPHA_SET) {
Chet Haase64a48c12012-02-13 16:33:29 -080013199 onSetAlpha(255);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013200 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase64a48c12012-02-13 16:33:29 -080013201 }
13202
Chet Haasea1cff502012-02-21 13:43:44 -080013203 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
13204 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013205 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013206 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080013207 } else {
13208 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013209 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080013210 } else {
13211 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
13212 }
13213 }
13214 }
13215
Chet Haase9420abd2012-03-29 16:28:32 -070013216 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080013217 displayList = getDisplayList();
13218 if (!displayList.isValid()) {
13219 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13220 // to getDisplayList(), the display list will be marked invalid and we should not
13221 // try to use it again.
13222 displayList = null;
13223 hasDisplayList = false;
13224 }
13225 }
13226
13227 if (hasNoCache) {
13228 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080013229 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080013230 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080013231 if (layer != null && layer.isValid()) {
13232 mLayerPaint.setAlpha((int) (alpha * 255));
13233 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
13234 layerRendered = true;
13235 } else {
13236 final int scrollX = hasDisplayList ? 0 : sx;
13237 final int scrollY = hasDisplayList ? 0 : sy;
13238 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080013239 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080013240 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
13241 }
13242 }
13243
13244 if (!layerRendered) {
13245 if (!hasDisplayList) {
13246 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070013247 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
13248 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase64a48c12012-02-13 16:33:29 -080013249 dispatchDraw(canvas);
13250 } else {
13251 draw(canvas);
13252 }
13253 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013254 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase1271e2c2012-04-20 09:54:27 -070013255 ((HardwareCanvas) canvas).drawDisplayList(displayList, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080013256 }
13257 }
13258 } else if (cache != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013259 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase64a48c12012-02-13 16:33:29 -080013260 Paint cachePaint;
13261
13262 if (layerType == LAYER_TYPE_NONE) {
13263 cachePaint = parent.mCachePaint;
13264 if (cachePaint == null) {
13265 cachePaint = new Paint();
13266 cachePaint.setDither(false);
13267 parent.mCachePaint = cachePaint;
13268 }
Chet Haase9420abd2012-03-29 16:28:32 -070013269 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080013270 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080013271 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
13272 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013273 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080013274 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080013275 }
13276 } else {
13277 cachePaint = mLayerPaint;
13278 cachePaint.setAlpha((int) (alpha * 255));
13279 }
13280 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
13281 }
13282
Chet Haasea1cff502012-02-21 13:43:44 -080013283 if (restoreTo >= 0) {
13284 canvas.restoreToCount(restoreTo);
13285 }
Chet Haase64a48c12012-02-13 16:33:29 -080013286
13287 if (a != null && !more) {
13288 if (!hardwareAccelerated && !a.getFillAfter()) {
13289 onSetAlpha(255);
13290 }
13291 parent.finishAnimatingView(this, a);
13292 }
13293
13294 if (more && hardwareAccelerated) {
13295 // invalidation is the trigger to recreate display lists, so if we're using
13296 // display lists to render, force an invalidate to allow the animation to
13297 // continue drawing another frame
13298 parent.invalidate(true);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013299 if (a.hasAlpha() && (mPrivateFlags & PFLAG_ALPHA_SET) == PFLAG_ALPHA_SET) {
Chet Haase64a48c12012-02-13 16:33:29 -080013300 // alpha animations should cause the child to recreate its display list
13301 invalidate(true);
13302 }
13303 }
13304
13305 mRecreateDisplayList = false;
13306
13307 return more;
13308 }
13309
13310 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013311 * Manually render this view (and all of its children) to the given Canvas.
13312 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070013313 * called. When implementing a view, implement
13314 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
13315 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013316 *
13317 * @param canvas The Canvas to which the View is rendered.
13318 */
13319 public void draw(Canvas canvas) {
Romain Guy5bcdff42009-05-14 21:27:18 -070013320 final int privateFlags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070013321 final boolean dirtyOpaque = (privateFlags & PFLAG_DIRTY_MASK) == PFLAG_DIRTY_OPAQUE &&
Romain Guy5bcdff42009-05-14 21:27:18 -070013322 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013323 mPrivateFlags = (privateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070013324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013325 /*
13326 * Draw traversal performs several drawing steps which must be executed
13327 * in the appropriate order:
13328 *
13329 * 1. Draw the background
13330 * 2. If necessary, save the canvas' layers to prepare for fading
13331 * 3. Draw view's content
13332 * 4. Draw children
13333 * 5. If necessary, draw the fading edges and restore layers
13334 * 6. Draw decorations (scrollbars for instance)
13335 */
13336
13337 // Step 1, draw the background, if needed
13338 int saveCount;
13339
Romain Guy24443ea2009-05-11 11:56:30 -070013340 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013341 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070013342 if (background != null) {
13343 final int scrollX = mScrollX;
13344 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013345
Romain Guy24443ea2009-05-11 11:56:30 -070013346 if (mBackgroundSizeChanged) {
13347 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
13348 mBackgroundSizeChanged = false;
13349 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013350
Romain Guy24443ea2009-05-11 11:56:30 -070013351 if ((scrollX | scrollY) == 0) {
13352 background.draw(canvas);
13353 } else {
13354 canvas.translate(scrollX, scrollY);
13355 background.draw(canvas);
13356 canvas.translate(-scrollX, -scrollY);
13357 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013358 }
13359 }
13360
13361 // skip step 2 & 5 if possible (common case)
13362 final int viewFlags = mViewFlags;
13363 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
13364 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
13365 if (!verticalEdges && !horizontalEdges) {
13366 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013367 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013368
13369 // Step 4, draw the children
13370 dispatchDraw(canvas);
13371
13372 // Step 6, draw decorations (scrollbars)
13373 onDrawScrollBars(canvas);
13374
13375 // we're done...
13376 return;
13377 }
13378
13379 /*
13380 * Here we do the full fledged routine...
13381 * (this is an uncommon case where speed matters less,
13382 * this is why we repeat some of the tests that have been
13383 * done above)
13384 */
13385
13386 boolean drawTop = false;
13387 boolean drawBottom = false;
13388 boolean drawLeft = false;
13389 boolean drawRight = false;
13390
13391 float topFadeStrength = 0.0f;
13392 float bottomFadeStrength = 0.0f;
13393 float leftFadeStrength = 0.0f;
13394 float rightFadeStrength = 0.0f;
13395
13396 // Step 2, save the canvas' layers
13397 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013398
13399 final boolean offsetRequired = isPaddingOffsetRequired();
13400 if (offsetRequired) {
13401 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013402 }
13403
13404 int left = mScrollX + paddingLeft;
13405 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070013406 int top = mScrollY + getFadeTop(offsetRequired);
13407 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013408
13409 if (offsetRequired) {
13410 right += getRightPaddingOffset();
13411 bottom += getBottomPaddingOffset();
13412 }
13413
13414 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070013415 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013416 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013417
13418 // clip the fade length if top and bottom fades overlap
13419 // overlapping fades produce odd-looking artifacts
13420 if (verticalEdges && (top + length > bottom - length)) {
13421 length = (bottom - top) / 2;
13422 }
13423
13424 // also clip horizontal fades if necessary
13425 if (horizontalEdges && (left + length > right - length)) {
13426 length = (right - left) / 2;
13427 }
13428
13429 if (verticalEdges) {
13430 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013431 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013432 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013433 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013434 }
13435
13436 if (horizontalEdges) {
13437 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013438 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013439 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013440 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013441 }
13442
13443 saveCount = canvas.getSaveCount();
13444
13445 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070013446 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013447 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
13448
13449 if (drawTop) {
13450 canvas.saveLayer(left, top, right, top + length, null, flags);
13451 }
13452
13453 if (drawBottom) {
13454 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
13455 }
13456
13457 if (drawLeft) {
13458 canvas.saveLayer(left, top, left + length, bottom, null, flags);
13459 }
13460
13461 if (drawRight) {
13462 canvas.saveLayer(right - length, top, right, bottom, null, flags);
13463 }
13464 } else {
13465 scrollabilityCache.setFadeColor(solidColor);
13466 }
13467
13468 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013469 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013470
13471 // Step 4, draw the children
13472 dispatchDraw(canvas);
13473
13474 // Step 5, draw the fade effect and restore layers
13475 final Paint p = scrollabilityCache.paint;
13476 final Matrix matrix = scrollabilityCache.matrix;
13477 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013478
13479 if (drawTop) {
13480 matrix.setScale(1, fadeHeight * topFadeStrength);
13481 matrix.postTranslate(left, top);
13482 fade.setLocalMatrix(matrix);
13483 canvas.drawRect(left, top, right, top + length, p);
13484 }
13485
13486 if (drawBottom) {
13487 matrix.setScale(1, fadeHeight * bottomFadeStrength);
13488 matrix.postRotate(180);
13489 matrix.postTranslate(left, bottom);
13490 fade.setLocalMatrix(matrix);
13491 canvas.drawRect(left, bottom - length, right, bottom, p);
13492 }
13493
13494 if (drawLeft) {
13495 matrix.setScale(1, fadeHeight * leftFadeStrength);
13496 matrix.postRotate(-90);
13497 matrix.postTranslate(left, top);
13498 fade.setLocalMatrix(matrix);
13499 canvas.drawRect(left, top, left + length, bottom, p);
13500 }
13501
13502 if (drawRight) {
13503 matrix.setScale(1, fadeHeight * rightFadeStrength);
13504 matrix.postRotate(90);
13505 matrix.postTranslate(right, top);
13506 fade.setLocalMatrix(matrix);
13507 canvas.drawRect(right - length, top, right, bottom, p);
13508 }
13509
13510 canvas.restoreToCount(saveCount);
13511
13512 // Step 6, draw decorations (scrollbars)
13513 onDrawScrollBars(canvas);
13514 }
13515
13516 /**
13517 * Override this if your view is known to always be drawn on top of a solid color background,
13518 * and needs to draw fading edges. Returning a non-zero color enables the view system to
13519 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
13520 * should be set to 0xFF.
13521 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013522 * @see #setVerticalFadingEdgeEnabled(boolean)
13523 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013524 *
13525 * @return The known solid color background for this view, or 0 if the color may vary
13526 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013527 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013528 public int getSolidColor() {
13529 return 0;
13530 }
13531
13532 /**
13533 * Build a human readable string representation of the specified view flags.
13534 *
13535 * @param flags the view flags to convert to a string
13536 * @return a String representing the supplied flags
13537 */
13538 private static String printFlags(int flags) {
13539 String output = "";
13540 int numFlags = 0;
13541 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
13542 output += "TAKES_FOCUS";
13543 numFlags++;
13544 }
13545
13546 switch (flags & VISIBILITY_MASK) {
13547 case INVISIBLE:
13548 if (numFlags > 0) {
13549 output += " ";
13550 }
13551 output += "INVISIBLE";
13552 // USELESS HERE numFlags++;
13553 break;
13554 case GONE:
13555 if (numFlags > 0) {
13556 output += " ";
13557 }
13558 output += "GONE";
13559 // USELESS HERE numFlags++;
13560 break;
13561 default:
13562 break;
13563 }
13564 return output;
13565 }
13566
13567 /**
13568 * Build a human readable string representation of the specified private
13569 * view flags.
13570 *
13571 * @param privateFlags the private view flags to convert to a string
13572 * @return a String representing the supplied flags
13573 */
13574 private static String printPrivateFlags(int privateFlags) {
13575 String output = "";
13576 int numFlags = 0;
13577
Dianne Hackborn4702a852012-08-17 15:18:29 -070013578 if ((privateFlags & PFLAG_WANTS_FOCUS) == PFLAG_WANTS_FOCUS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013579 output += "WANTS_FOCUS";
13580 numFlags++;
13581 }
13582
Dianne Hackborn4702a852012-08-17 15:18:29 -070013583 if ((privateFlags & PFLAG_FOCUSED) == PFLAG_FOCUSED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013584 if (numFlags > 0) {
13585 output += " ";
13586 }
13587 output += "FOCUSED";
13588 numFlags++;
13589 }
13590
Dianne Hackborn4702a852012-08-17 15:18:29 -070013591 if ((privateFlags & PFLAG_SELECTED) == PFLAG_SELECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013592 if (numFlags > 0) {
13593 output += " ";
13594 }
13595 output += "SELECTED";
13596 numFlags++;
13597 }
13598
Dianne Hackborn4702a852012-08-17 15:18:29 -070013599 if ((privateFlags & PFLAG_IS_ROOT_NAMESPACE) == PFLAG_IS_ROOT_NAMESPACE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013600 if (numFlags > 0) {
13601 output += " ";
13602 }
13603 output += "IS_ROOT_NAMESPACE";
13604 numFlags++;
13605 }
13606
Dianne Hackborn4702a852012-08-17 15:18:29 -070013607 if ((privateFlags & PFLAG_HAS_BOUNDS) == PFLAG_HAS_BOUNDS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013608 if (numFlags > 0) {
13609 output += " ";
13610 }
13611 output += "HAS_BOUNDS";
13612 numFlags++;
13613 }
13614
Dianne Hackborn4702a852012-08-17 15:18:29 -070013615 if ((privateFlags & PFLAG_DRAWN) == PFLAG_DRAWN) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013616 if (numFlags > 0) {
13617 output += " ";
13618 }
13619 output += "DRAWN";
13620 // USELESS HERE numFlags++;
13621 }
13622 return output;
13623 }
13624
13625 /**
13626 * <p>Indicates whether or not this view's layout will be requested during
13627 * the next hierarchy layout pass.</p>
13628 *
13629 * @return true if the layout will be forced during next layout pass
13630 */
13631 public boolean isLayoutRequested() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013632 return (mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013633 }
13634
13635 /**
13636 * Assign a size and position to a view and all of its
13637 * descendants
13638 *
13639 * <p>This is the second phase of the layout mechanism.
13640 * (The first is measuring). In this phase, each parent calls
13641 * layout on all of its children to position them.
13642 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080013643 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013644 *
Chet Haase9c087442011-01-12 16:20:16 -080013645 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013646 * Derived classes with children should override
13647 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080013648 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013649 *
13650 * @param l Left position, relative to parent
13651 * @param t Top position, relative to parent
13652 * @param r Right position, relative to parent
13653 * @param b Bottom position, relative to parent
13654 */
Romain Guy5429e1d2010-09-07 12:38:00 -070013655 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080013656 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070013657 int oldL = mLeft;
13658 int oldT = mTop;
13659 int oldB = mBottom;
13660 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013661 boolean changed = setFrame(l, t, r, b);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013662 if (changed || (mPrivateFlags & PFLAG_LAYOUT_REQUIRED) == PFLAG_LAYOUT_REQUIRED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013663 onLayout(changed, l, t, r, b);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013664 mPrivateFlags &= ~PFLAG_LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070013665
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013666 ListenerInfo li = mListenerInfo;
13667 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070013668 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013669 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070013670 int numListeners = listenersCopy.size();
13671 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070013672 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070013673 }
13674 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013675 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013676 mPrivateFlags &= ~PFLAG_FORCE_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013677 }
13678
13679 /**
13680 * Called from layout when this view should
13681 * assign a size and position to each of its children.
13682 *
13683 * Derived classes with children should override
13684 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070013685 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013686 * @param changed This is a new size or position for this view
13687 * @param left Left position, relative to parent
13688 * @param top Top position, relative to parent
13689 * @param right Right position, relative to parent
13690 * @param bottom Bottom position, relative to parent
13691 */
13692 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
13693 }
13694
13695 /**
13696 * Assign a size and position to this view.
13697 *
13698 * This is called from layout.
13699 *
13700 * @param left Left position, relative to parent
13701 * @param top Top position, relative to parent
13702 * @param right Right position, relative to parent
13703 * @param bottom Bottom position, relative to parent
13704 * @return true if the new size and position are different than the
13705 * previous ones
13706 * {@hide}
13707 */
13708 protected boolean setFrame(int left, int top, int right, int bottom) {
13709 boolean changed = false;
13710
13711 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070013712 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013713 + right + "," + bottom + ")");
13714 }
13715
13716 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
13717 changed = true;
13718
13719 // Remember our drawn bit
Dianne Hackborn4702a852012-08-17 15:18:29 -070013720 int drawn = mPrivateFlags & PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013721
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013722 int oldWidth = mRight - mLeft;
13723 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070013724 int newWidth = right - left;
13725 int newHeight = bottom - top;
13726 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
13727
13728 // Invalidate our old position
13729 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013730
13731 mLeft = left;
13732 mTop = top;
13733 mRight = right;
13734 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -070013735 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013736 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
13737 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013738
Dianne Hackborn4702a852012-08-17 15:18:29 -070013739 mPrivateFlags |= PFLAG_HAS_BOUNDS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013741
Chet Haase75755e22011-07-18 17:48:25 -070013742 if (sizeChanged) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013743 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013744 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013745 if (mTransformationInfo != null) {
13746 mTransformationInfo.mMatrixDirty = true;
13747 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013748 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013749 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
13750 }
13751
13752 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
13753 // If we are visible, force the DRAWN bit to on so that
13754 // this invalidate will go through (at least to our parent).
13755 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013756 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013757 // the DRAWN bit.
Dianne Hackborn4702a852012-08-17 15:18:29 -070013758 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070013759 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080013760 // parent display list may need to be recreated based on a change in the bounds
13761 // of any child
13762 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013763 }
13764
13765 // Reset drawn bit to original value (invalidate turns it off)
13766 mPrivateFlags |= drawn;
13767
13768 mBackgroundSizeChanged = true;
13769 }
13770 return changed;
13771 }
13772
13773 /**
13774 * Finalize inflating a view from XML. This is called as the last phase
13775 * of inflation, after all child views have been added.
13776 *
13777 * <p>Even if the subclass overrides onFinishInflate, they should always be
13778 * sure to call the super method, so that we get called.
13779 */
13780 protected void onFinishInflate() {
13781 }
13782
13783 /**
13784 * Returns the resources associated with this view.
13785 *
13786 * @return Resources object.
13787 */
13788 public Resources getResources() {
13789 return mResources;
13790 }
13791
13792 /**
13793 * Invalidates the specified Drawable.
13794 *
13795 * @param drawable the drawable to invalidate
13796 */
13797 public void invalidateDrawable(Drawable drawable) {
13798 if (verifyDrawable(drawable)) {
13799 final Rect dirty = drawable.getBounds();
13800 final int scrollX = mScrollX;
13801 final int scrollY = mScrollY;
13802
13803 invalidate(dirty.left + scrollX, dirty.top + scrollY,
13804 dirty.right + scrollX, dirty.bottom + scrollY);
13805 }
13806 }
13807
13808 /**
13809 * Schedules an action on a drawable to occur at a specified time.
13810 *
13811 * @param who the recipient of the action
13812 * @param what the action to run on the drawable
13813 * @param when the time at which the action must occur. Uses the
13814 * {@link SystemClock#uptimeMillis} timebase.
13815 */
13816 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080013817 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013818 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080013819 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013820 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
13821 Choreographer.CALLBACK_ANIMATION, what, who,
13822 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080013823 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013824 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080013825 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013826 }
13827 }
13828
13829 /**
13830 * Cancels a scheduled action on a drawable.
13831 *
13832 * @param who the recipient of the action
13833 * @param what the action to cancel
13834 */
13835 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080013836 if (verifyDrawable(who) && what != null) {
13837 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013838 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13839 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080013840 } else {
13841 ViewRootImpl.getRunQueue().removeCallbacks(what);
13842 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013843 }
13844 }
13845
13846 /**
13847 * Unschedule any events associated with the given Drawable. This can be
13848 * used when selecting a new Drawable into a view, so that the previous
13849 * one is completely unscheduled.
13850 *
13851 * @param who The Drawable to unschedule.
13852 *
13853 * @see #drawableStateChanged
13854 */
13855 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080013856 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013857 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13858 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013859 }
13860 }
13861
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013862 /**
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070013863 * Resolve the Drawables depending on the layout direction. This is implicitly supposing
13864 * that the View directionality can and will be resolved before its Drawables.
13865 *
13866 * Will call {@link View#onResolveDrawables} when resolution is done.
13867 */
13868 public void resolveDrawables() {
13869 if (mBackground != null) {
13870 mBackground.setLayoutDirection(getResolvedLayoutDirection());
13871 }
13872 onResolveDrawables(getResolvedLayoutDirection());
13873 }
13874
13875 /**
13876 * Called when layout direction has been resolved.
13877 *
13878 * The default implementation does nothing.
13879 *
13880 * @param layoutDirection The resolved layout direction.
13881 *
13882 * @see {@link #LAYOUT_DIRECTION_LTR}
13883 * @see {@link #LAYOUT_DIRECTION_RTL}
13884 */
13885 public void onResolveDrawables(int layoutDirection) {
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070013886 }
13887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013888 /**
13889 * If your view subclass is displaying its own Drawable objects, it should
13890 * override this function and return true for any Drawable it is
13891 * displaying. This allows animations for those drawables to be
13892 * scheduled.
13893 *
13894 * <p>Be sure to call through to the super class when overriding this
13895 * function.
13896 *
13897 * @param who The Drawable to verify. Return true if it is one you are
13898 * displaying, else return the result of calling through to the
13899 * super class.
13900 *
13901 * @return boolean If true than the Drawable is being displayed in the
13902 * view; else false and it is not allowed to animate.
13903 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013904 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
13905 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013906 */
13907 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013908 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013909 }
13910
13911 /**
13912 * This function is called whenever the state of the view changes in such
13913 * a way that it impacts the state of drawables being shown.
13914 *
13915 * <p>Be sure to call through to the superclass when overriding this
13916 * function.
13917 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013918 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013919 */
13920 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013921 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013922 if (d != null && d.isStateful()) {
13923 d.setState(getDrawableState());
13924 }
13925 }
13926
13927 /**
13928 * Call this to force a view to update its drawable state. This will cause
13929 * drawableStateChanged to be called on this view. Views that are interested
13930 * in the new state should call getDrawableState.
13931 *
13932 * @see #drawableStateChanged
13933 * @see #getDrawableState
13934 */
13935 public void refreshDrawableState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013936 mPrivateFlags |= PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013937 drawableStateChanged();
13938
13939 ViewParent parent = mParent;
13940 if (parent != null) {
13941 parent.childDrawableStateChanged(this);
13942 }
13943 }
13944
13945 /**
13946 * Return an array of resource IDs of the drawable states representing the
13947 * current state of the view.
13948 *
13949 * @return The current drawable state
13950 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013951 * @see Drawable#setState(int[])
13952 * @see #drawableStateChanged()
13953 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013954 */
13955 public final int[] getDrawableState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013956 if ((mDrawableState != null) && ((mPrivateFlags & PFLAG_DRAWABLE_STATE_DIRTY) == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013957 return mDrawableState;
13958 } else {
13959 mDrawableState = onCreateDrawableState(0);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013960 mPrivateFlags &= ~PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013961 return mDrawableState;
13962 }
13963 }
13964
13965 /**
13966 * Generate the new {@link android.graphics.drawable.Drawable} state for
13967 * this view. This is called by the view
13968 * system when the cached Drawable state is determined to be invalid. To
13969 * retrieve the current state, you should use {@link #getDrawableState}.
13970 *
13971 * @param extraSpace if non-zero, this is the number of extra entries you
13972 * would like in the returned array in which you can place your own
13973 * states.
13974 *
13975 * @return Returns an array holding the current {@link Drawable} state of
13976 * the view.
13977 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013978 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013979 */
13980 protected int[] onCreateDrawableState(int extraSpace) {
13981 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
13982 mParent instanceof View) {
13983 return ((View) mParent).onCreateDrawableState(extraSpace);
13984 }
13985
13986 int[] drawableState;
13987
13988 int privateFlags = mPrivateFlags;
13989
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013990 int viewStateIndex = 0;
Dianne Hackborn4702a852012-08-17 15:18:29 -070013991 if ((privateFlags & PFLAG_PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013992 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
13993 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Dianne Hackborn4702a852012-08-17 15:18:29 -070013994 if ((privateFlags & PFLAG_SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013995 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
Dianne Hackborn4702a852012-08-17 15:18:29 -070013996 if ((privateFlags & PFLAG_ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070013997 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
13998 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080013999 // This is set if HW acceleration is requested, even if the current
14000 // process doesn't allow it. This is just to allow app preview
14001 // windows to better match their app.
14002 viewStateIndex |= VIEW_STATE_ACCELERATED;
14003 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070014004 if ((privateFlags & PFLAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014005
Christopher Tate3d4bf172011-03-28 16:16:46 -070014006 final int privateFlags2 = mPrivateFlags2;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014007 if ((privateFlags2 & PFLAG2_DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
14008 if ((privateFlags2 & PFLAG2_DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
Christopher Tate3d4bf172011-03-28 16:16:46 -070014009
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014010 drawableState = VIEW_STATE_SETS[viewStateIndex];
14011
14012 //noinspection ConstantIfStatement
14013 if (false) {
14014 Log.i("View", "drawableStateIndex=" + viewStateIndex);
14015 Log.i("View", toString()
Dianne Hackborn4702a852012-08-17 15:18:29 -070014016 + " pressed=" + ((privateFlags & PFLAG_PRESSED) != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014017 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
14018 + " fo=" + hasFocus()
Dianne Hackborn4702a852012-08-17 15:18:29 -070014019 + " sl=" + ((privateFlags & PFLAG_SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014020 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014021 + ": " + Arrays.toString(drawableState));
14022 }
14023
14024 if (extraSpace == 0) {
14025 return drawableState;
14026 }
14027
14028 final int[] fullState;
14029 if (drawableState != null) {
14030 fullState = new int[drawableState.length + extraSpace];
14031 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
14032 } else {
14033 fullState = new int[extraSpace];
14034 }
14035
14036 return fullState;
14037 }
14038
14039 /**
14040 * Merge your own state values in <var>additionalState</var> into the base
14041 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014042 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014043 *
14044 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014045 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014046 * own additional state values.
14047 *
14048 * @param additionalState The additional state values you would like
14049 * added to <var>baseState</var>; this array is not modified.
14050 *
14051 * @return As a convenience, the <var>baseState</var> array you originally
14052 * passed into the function is returned.
14053 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014054 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014055 */
14056 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
14057 final int N = baseState.length;
14058 int i = N - 1;
14059 while (i >= 0 && baseState[i] == 0) {
14060 i--;
14061 }
14062 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
14063 return baseState;
14064 }
14065
14066 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070014067 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
14068 * on all Drawable objects associated with this view.
14069 */
14070 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014071 if (mBackground != null) {
14072 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070014073 }
14074 }
14075
14076 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014077 * Sets the background color for this view.
14078 * @param color the color of the background
14079 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014080 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014081 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070014082 if (mBackground instanceof ColorDrawable) {
14083 ((ColorDrawable) mBackground).setColor(color);
Romain Guy7d3082a2012-07-11 17:52:54 -070014084 computeOpaqueFlags();
Chet Haase70d4ba12010-10-06 09:46:45 -070014085 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070014086 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070014087 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014088 }
14089
14090 /**
14091 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070014092 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014093 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070014094 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014095 * @attr ref android.R.styleable#View_background
14096 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014097 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014098 public void setBackgroundResource(int resid) {
14099 if (resid != 0 && resid == mBackgroundResource) {
14100 return;
14101 }
14102
14103 Drawable d= null;
14104 if (resid != 0) {
14105 d = mResources.getDrawable(resid);
14106 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014107 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014108
14109 mBackgroundResource = resid;
14110 }
14111
14112 /**
14113 * Set the background to a given Drawable, or remove the background. If the
14114 * background has padding, this View's padding is set to the background's
14115 * padding. However, when a background is removed, this View's padding isn't
14116 * touched. If setting the padding is desired, please use
14117 * {@link #setPadding(int, int, int, int)}.
14118 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014119 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014120 * background
14121 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014122 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070014123 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070014124 setBackgroundDrawable(background);
14125 }
14126
14127 /**
14128 * @deprecated use {@link #setBackground(Drawable)} instead
14129 */
14130 @Deprecated
14131 public void setBackgroundDrawable(Drawable background) {
Romain Guy846a5332012-07-11 17:44:57 -070014132 computeOpaqueFlags();
14133
Philip Milne6c8ea062012-04-03 17:38:43 -070014134 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070014135 return;
14136 }
14137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014138 boolean requestLayout = false;
14139
14140 mBackgroundResource = 0;
14141
14142 /*
14143 * Regardless of whether we're setting a new background or not, we want
14144 * to clear the previous drawable.
14145 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014146 if (mBackground != null) {
14147 mBackground.setCallback(null);
14148 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014149 }
14150
Philip Milne6c8ea062012-04-03 17:38:43 -070014151 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014152 Rect padding = sThreadLocal.get();
14153 if (padding == null) {
14154 padding = new Rect();
14155 sThreadLocal.set(padding);
14156 }
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014157 background.setLayoutDirection(getResolvedLayoutDirection());
Philip Milne6c8ea062012-04-03 17:38:43 -070014158 if (background.getPadding(padding)) {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014159 // Reset padding resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070014160 mPrivateFlags2 &= ~PFLAG2_PADDING_RESOLVED;
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014161 switch (background.getLayoutDirection()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014162 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014163 internalSetPadding(padding.right, padding.top, padding.left, padding.bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014164 break;
14165 case LAYOUT_DIRECTION_LTR:
14166 default:
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014167 internalSetPadding(padding.left, padding.top, padding.right, padding.bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014168 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014169 }
14170
14171 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
14172 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070014173 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
14174 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014175 requestLayout = true;
14176 }
14177
Philip Milne6c8ea062012-04-03 17:38:43 -070014178 background.setCallback(this);
14179 if (background.isStateful()) {
14180 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014181 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014182 background.setVisible(getVisibility() == VISIBLE, false);
14183 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014184
Dianne Hackborn4702a852012-08-17 15:18:29 -070014185 if ((mPrivateFlags & PFLAG_SKIP_DRAW) != 0) {
14186 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
14187 mPrivateFlags |= PFLAG_ONLY_DRAWS_BACKGROUND;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014188 requestLayout = true;
14189 }
14190 } else {
14191 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070014192 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014193
Dianne Hackborn4702a852012-08-17 15:18:29 -070014194 if ((mPrivateFlags & PFLAG_ONLY_DRAWS_BACKGROUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014195 /*
14196 * This view ONLY drew the background before and we're removing
14197 * the background, so now it won't draw anything
14198 * (hence we SKIP_DRAW)
14199 */
Dianne Hackborn4702a852012-08-17 15:18:29 -070014200 mPrivateFlags &= ~PFLAG_ONLY_DRAWS_BACKGROUND;
14201 mPrivateFlags |= PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014202 }
14203
14204 /*
14205 * When the background is set, we try to apply its padding to this
14206 * View. When the background is removed, we don't touch this View's
14207 * padding. This is noted in the Javadocs. Hence, we don't need to
14208 * requestLayout(), the invalidate() below is sufficient.
14209 */
14210
14211 // The old background's minimum size could have affected this
14212 // View's layout, so let's requestLayout
14213 requestLayout = true;
14214 }
14215
Romain Guy8f1344f52009-05-15 16:03:59 -070014216 computeOpaqueFlags();
14217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014218 if (requestLayout) {
14219 requestLayout();
14220 }
14221
14222 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014223 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014224 }
14225
14226 /**
14227 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070014228 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014229 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070014230 *
14231 * @see #setBackground(Drawable)
14232 *
14233 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014234 */
14235 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014236 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014237 }
14238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014239 /**
14240 * Sets the padding. The view may add on the space required to display
14241 * the scrollbars, depending on the style and visibility of the scrollbars.
14242 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
14243 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
14244 * from the values set in this call.
14245 *
14246 * @attr ref android.R.styleable#View_padding
14247 * @attr ref android.R.styleable#View_paddingBottom
14248 * @attr ref android.R.styleable#View_paddingLeft
14249 * @attr ref android.R.styleable#View_paddingRight
14250 * @attr ref android.R.styleable#View_paddingTop
14251 * @param left the left padding in pixels
14252 * @param top the top padding in pixels
14253 * @param right the right padding in pixels
14254 * @param bottom the bottom padding in pixels
14255 */
14256 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014257 // Reset padding resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070014258 mPrivateFlags2 &= ~PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014259
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014260 mUserPaddingStart = UNDEFINED_PADDING;
14261 mUserPaddingEnd = UNDEFINED_PADDING;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014262
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014263 internalSetPadding(left, top, right, bottom);
14264 }
14265
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014266 /**
14267 * @hide
14268 */
14269 protected void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080014270 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014271 mUserPaddingRight = right;
14272 mUserPaddingBottom = bottom;
14273
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014274 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014275 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070014276
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014277 // Common case is there are no scroll bars.
14278 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014279 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080014280 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014281 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080014282 switch (mVerticalScrollbarPosition) {
14283 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Meglioc91b6ca2012-06-22 14:51:15 -070014284 if (isLayoutRtl()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014285 left += offset;
14286 } else {
14287 right += offset;
14288 }
14289 break;
Adam Powell20232d02010-12-08 21:08:53 -080014290 case SCROLLBAR_POSITION_RIGHT:
14291 right += offset;
14292 break;
14293 case SCROLLBAR_POSITION_LEFT:
14294 left += offset;
14295 break;
14296 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014297 }
Adam Powell20232d02010-12-08 21:08:53 -080014298 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014299 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
14300 ? 0 : getHorizontalScrollbarHeight();
14301 }
14302 }
Romain Guy8506ab42009-06-11 17:35:47 -070014303
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014304 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014305 changed = true;
14306 mPaddingLeft = left;
14307 }
14308 if (mPaddingTop != top) {
14309 changed = true;
14310 mPaddingTop = top;
14311 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014312 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014313 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014314 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014315 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014316 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014317 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014318 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014319 }
14320
14321 if (changed) {
14322 requestLayout();
14323 }
14324 }
14325
14326 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014327 * Sets the relative padding. The view may add on the space required to display
14328 * the scrollbars, depending on the style and visibility of the scrollbars.
14329 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
14330 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
14331 * from the values set in this call.
14332 *
14333 * @attr ref android.R.styleable#View_padding
14334 * @attr ref android.R.styleable#View_paddingBottom
14335 * @attr ref android.R.styleable#View_paddingStart
14336 * @attr ref android.R.styleable#View_paddingEnd
14337 * @attr ref android.R.styleable#View_paddingTop
14338 * @param start the start padding in pixels
14339 * @param top the top padding in pixels
14340 * @param end the end padding in pixels
14341 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014342 */
14343 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014344 // Reset padding resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070014345 mPrivateFlags2 &= ~PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014346
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014347 mUserPaddingStart = start;
14348 mUserPaddingEnd = end;
14349
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014350 switch(getResolvedLayoutDirection()) {
14351 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014352 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014353 break;
14354 case LAYOUT_DIRECTION_LTR:
14355 default:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014356 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014357 }
14358 }
14359
14360 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014361 * Returns the top padding of this view.
14362 *
14363 * @return the top padding in pixels
14364 */
14365 public int getPaddingTop() {
14366 return mPaddingTop;
14367 }
14368
14369 /**
14370 * Returns the bottom padding of this view. If there are inset and enabled
14371 * scrollbars, this value may include the space required to display the
14372 * scrollbars as well.
14373 *
14374 * @return the bottom padding in pixels
14375 */
14376 public int getPaddingBottom() {
14377 return mPaddingBottom;
14378 }
14379
14380 /**
14381 * Returns the left padding of this view. If there are inset and enabled
14382 * scrollbars, this value may include the space required to display the
14383 * scrollbars as well.
14384 *
14385 * @return the left padding in pixels
14386 */
14387 public int getPaddingLeft() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014388 if (!isPaddingResolved()) {
14389 resolvePadding();
14390 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014391 return mPaddingLeft;
14392 }
14393
14394 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014395 * Returns the start padding of this view depending on its resolved layout direction.
14396 * If there are inset and enabled scrollbars, this value may include the space
14397 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014398 *
14399 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014400 */
14401 public int getPaddingStart() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014402 if (!isPaddingResolved()) {
14403 resolvePadding();
14404 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014405 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14406 mPaddingRight : mPaddingLeft;
14407 }
14408
14409 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014410 * Returns the right padding of this view. If there are inset and enabled
14411 * scrollbars, this value may include the space required to display the
14412 * scrollbars as well.
14413 *
14414 * @return the right padding in pixels
14415 */
14416 public int getPaddingRight() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014417 if (!isPaddingResolved()) {
14418 resolvePadding();
14419 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014420 return mPaddingRight;
14421 }
14422
14423 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014424 * Returns the end padding of this view depending on its resolved layout direction.
14425 * If there are inset and enabled scrollbars, this value may include the space
14426 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014427 *
14428 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014429 */
14430 public int getPaddingEnd() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014431 if (!isPaddingResolved()) {
14432 resolvePadding();
14433 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014434 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14435 mPaddingLeft : mPaddingRight;
14436 }
14437
14438 /**
14439 * Return if the padding as been set thru relative values
14440 * {@link #setPaddingRelative(int, int, int, int)} or thru
14441 * @attr ref android.R.styleable#View_paddingStart or
14442 * @attr ref android.R.styleable#View_paddingEnd
14443 *
14444 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014445 */
14446 public boolean isPaddingRelative() {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014447 return (mUserPaddingStart != UNDEFINED_PADDING || mUserPaddingEnd != UNDEFINED_PADDING);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014448 }
14449
14450 /**
Philip Milne1557fd72012-04-04 23:41:34 -070014451 * @hide
14452 */
Philip Milne7a23b492012-04-24 22:12:36 -070014453 public Insets getOpticalInsets() {
Philip Milne1557fd72012-04-04 23:41:34 -070014454 if (mLayoutInsets == null) {
Philip Milnebbd51f12012-04-18 15:09:05 -070014455 mLayoutInsets = (mBackground == null) ? Insets.NONE : mBackground.getLayoutInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070014456 }
14457 return mLayoutInsets;
14458 }
14459
14460 /**
14461 * @hide
14462 */
14463 public void setLayoutInsets(Insets layoutInsets) {
14464 mLayoutInsets = layoutInsets;
14465 }
14466
14467 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014468 * Changes the selection state of this view. A view can be selected or not.
14469 * Note that selection is not the same as focus. Views are typically
14470 * selected in the context of an AdapterView like ListView or GridView;
14471 * the selected view is the view that is highlighted.
14472 *
14473 * @param selected true if the view must be selected, false otherwise
14474 */
14475 public void setSelected(boolean selected) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014476 if (((mPrivateFlags & PFLAG_SELECTED) != 0) != selected) {
14477 mPrivateFlags = (mPrivateFlags & ~PFLAG_SELECTED) | (selected ? PFLAG_SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070014478 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080014479 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014480 refreshDrawableState();
14481 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070014482 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
14483 notifyAccessibilityStateChanged();
14484 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014485 }
14486 }
14487
14488 /**
14489 * Dispatch setSelected to all of this View's children.
14490 *
14491 * @see #setSelected(boolean)
14492 *
14493 * @param selected The new selected state
14494 */
14495 protected void dispatchSetSelected(boolean selected) {
14496 }
14497
14498 /**
14499 * Indicates the selection state of this view.
14500 *
14501 * @return true if the view is selected, false otherwise
14502 */
14503 @ViewDebug.ExportedProperty
14504 public boolean isSelected() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014505 return (mPrivateFlags & PFLAG_SELECTED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014506 }
14507
14508 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014509 * Changes the activated state of this view. A view can be activated or not.
14510 * Note that activation is not the same as selection. Selection is
14511 * a transient property, representing the view (hierarchy) the user is
14512 * currently interacting with. Activation is a longer-term state that the
14513 * user can move views in and out of. For example, in a list view with
14514 * single or multiple selection enabled, the views in the current selection
14515 * set are activated. (Um, yeah, we are deeply sorry about the terminology
14516 * here.) The activated state is propagated down to children of the view it
14517 * is set on.
14518 *
14519 * @param activated true if the view must be activated, false otherwise
14520 */
14521 public void setActivated(boolean activated) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014522 if (((mPrivateFlags & PFLAG_ACTIVATED) != 0) != activated) {
14523 mPrivateFlags = (mPrivateFlags & ~PFLAG_ACTIVATED) | (activated ? PFLAG_ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014524 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014525 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070014526 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014527 }
14528 }
14529
14530 /**
14531 * Dispatch setActivated to all of this View's children.
14532 *
14533 * @see #setActivated(boolean)
14534 *
14535 * @param activated The new activated state
14536 */
14537 protected void dispatchSetActivated(boolean activated) {
14538 }
14539
14540 /**
14541 * Indicates the activation state of this view.
14542 *
14543 * @return true if the view is activated, false otherwise
14544 */
14545 @ViewDebug.ExportedProperty
14546 public boolean isActivated() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014547 return (mPrivateFlags & PFLAG_ACTIVATED) != 0;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014548 }
14549
14550 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014551 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
14552 * observer can be used to get notifications when global events, like
14553 * layout, happen.
14554 *
14555 * The returned ViewTreeObserver observer is not guaranteed to remain
14556 * valid for the lifetime of this View. If the caller of this method keeps
14557 * a long-lived reference to ViewTreeObserver, it should always check for
14558 * the return value of {@link ViewTreeObserver#isAlive()}.
14559 *
14560 * @return The ViewTreeObserver for this view's hierarchy.
14561 */
14562 public ViewTreeObserver getViewTreeObserver() {
14563 if (mAttachInfo != null) {
14564 return mAttachInfo.mTreeObserver;
14565 }
14566 if (mFloatingTreeObserver == null) {
14567 mFloatingTreeObserver = new ViewTreeObserver();
14568 }
14569 return mFloatingTreeObserver;
14570 }
14571
14572 /**
14573 * <p>Finds the topmost view in the current view hierarchy.</p>
14574 *
14575 * @return the topmost view containing this view
14576 */
14577 public View getRootView() {
14578 if (mAttachInfo != null) {
14579 final View v = mAttachInfo.mRootView;
14580 if (v != null) {
14581 return v;
14582 }
14583 }
Romain Guy8506ab42009-06-11 17:35:47 -070014584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014585 View parent = this;
14586
14587 while (parent.mParent != null && parent.mParent instanceof View) {
14588 parent = (View) parent.mParent;
14589 }
14590
14591 return parent;
14592 }
14593
14594 /**
14595 * <p>Computes the coordinates of this view on the screen. The argument
14596 * must be an array of two integers. After the method returns, the array
14597 * contains the x and y location in that order.</p>
14598 *
14599 * @param location an array of two integers in which to hold the coordinates
14600 */
14601 public void getLocationOnScreen(int[] location) {
14602 getLocationInWindow(location);
14603
14604 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070014605 if (info != null) {
14606 location[0] += info.mWindowLeft;
14607 location[1] += info.mWindowTop;
14608 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014609 }
14610
14611 /**
14612 * <p>Computes the coordinates of this view in its window. The argument
14613 * must be an array of two integers. After the method returns, the array
14614 * contains the x and y location in that order.</p>
14615 *
14616 * @param location an array of two integers in which to hold the coordinates
14617 */
14618 public void getLocationInWindow(int[] location) {
14619 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014620 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014621 }
14622
Gilles Debunne6583ce52011-12-06 18:09:02 -080014623 if (mAttachInfo == null) {
14624 // When the view is not attached to a window, this method does not make sense
14625 location[0] = location[1] = 0;
14626 return;
14627 }
14628
Gilles Debunnecea45132011-11-24 02:19:27 +010014629 float[] position = mAttachInfo.mTmpTransformLocation;
14630 position[0] = position[1] = 0.0f;
14631
14632 if (!hasIdentityMatrix()) {
14633 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014634 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014635
Gilles Debunnecea45132011-11-24 02:19:27 +010014636 position[0] += mLeft;
14637 position[1] += mTop;
14638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014639 ViewParent viewParent = mParent;
14640 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014641 final View view = (View) viewParent;
14642
14643 position[0] -= view.mScrollX;
14644 position[1] -= view.mScrollY;
14645
14646 if (!view.hasIdentityMatrix()) {
14647 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014648 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014649
14650 position[0] += view.mLeft;
14651 position[1] += view.mTop;
14652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014653 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070014654 }
Romain Guy8506ab42009-06-11 17:35:47 -070014655
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014656 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014657 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010014658 final ViewRootImpl vr = (ViewRootImpl) viewParent;
14659 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014660 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014661
14662 location[0] = (int) (position[0] + 0.5f);
14663 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014664 }
14665
14666 /**
14667 * {@hide}
14668 * @param id the id of the view to be found
14669 * @return the view of the specified id, null if cannot be found
14670 */
14671 protected View findViewTraversal(int id) {
14672 if (id == mID) {
14673 return this;
14674 }
14675 return null;
14676 }
14677
14678 /**
14679 * {@hide}
14680 * @param tag the tag of the view to be found
14681 * @return the view of specified tag, null if cannot be found
14682 */
14683 protected View findViewWithTagTraversal(Object tag) {
14684 if (tag != null && tag.equals(mTag)) {
14685 return this;
14686 }
14687 return null;
14688 }
14689
14690 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014691 * {@hide}
14692 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070014693 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080014694 * @return The first view that matches the predicate or null.
14695 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070014696 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080014697 if (predicate.apply(this)) {
14698 return this;
14699 }
14700 return null;
14701 }
14702
14703 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014704 * Look for a child view with the given id. If this view has the given
14705 * id, return this view.
14706 *
14707 * @param id The id to search for.
14708 * @return The view that has the given id in the hierarchy or null
14709 */
14710 public final View findViewById(int id) {
14711 if (id < 0) {
14712 return null;
14713 }
14714 return findViewTraversal(id);
14715 }
14716
14717 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070014718 * Finds a view by its unuque and stable accessibility id.
14719 *
14720 * @param accessibilityId The searched accessibility id.
14721 * @return The found view.
14722 */
14723 final View findViewByAccessibilityId(int accessibilityId) {
14724 if (accessibilityId < 0) {
14725 return null;
14726 }
14727 return findViewByAccessibilityIdTraversal(accessibilityId);
14728 }
14729
14730 /**
14731 * Performs the traversal to find a view by its unuque and stable accessibility id.
14732 *
14733 * <strong>Note:</strong>This method does not stop at the root namespace
14734 * boundary since the user can touch the screen at an arbitrary location
14735 * potentially crossing the root namespace bounday which will send an
14736 * accessibility event to accessibility services and they should be able
14737 * to obtain the event source. Also accessibility ids are guaranteed to be
14738 * unique in the window.
14739 *
14740 * @param accessibilityId The accessibility id.
14741 * @return The found view.
14742 */
14743 View findViewByAccessibilityIdTraversal(int accessibilityId) {
14744 if (getAccessibilityViewId() == accessibilityId) {
14745 return this;
14746 }
14747 return null;
14748 }
14749
14750 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014751 * Look for a child view with the given tag. If this view has the given
14752 * tag, return this view.
14753 *
14754 * @param tag The tag to search for, using "tag.equals(getTag())".
14755 * @return The View that has the given tag in the hierarchy or null
14756 */
14757 public final View findViewWithTag(Object tag) {
14758 if (tag == null) {
14759 return null;
14760 }
14761 return findViewWithTagTraversal(tag);
14762 }
14763
14764 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014765 * {@hide}
14766 * Look for a child view that matches the specified predicate.
14767 * If this view matches the predicate, return this view.
14768 *
14769 * @param predicate The predicate to evaluate.
14770 * @return The first view that matches the predicate or null.
14771 */
14772 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070014773 return findViewByPredicateTraversal(predicate, null);
14774 }
14775
14776 /**
14777 * {@hide}
14778 * Look for a child view that matches the specified predicate,
14779 * starting with the specified view and its descendents and then
14780 * recusively searching the ancestors and siblings of that view
14781 * until this view is reached.
14782 *
14783 * This method is useful in cases where the predicate does not match
14784 * a single unique view (perhaps multiple views use the same id)
14785 * and we are trying to find the view that is "closest" in scope to the
14786 * starting view.
14787 *
14788 * @param start The view to start from.
14789 * @param predicate The predicate to evaluate.
14790 * @return The first view that matches the predicate or null.
14791 */
14792 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
14793 View childToSkip = null;
14794 for (;;) {
14795 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
14796 if (view != null || start == this) {
14797 return view;
14798 }
14799
14800 ViewParent parent = start.getParent();
14801 if (parent == null || !(parent instanceof View)) {
14802 return null;
14803 }
14804
14805 childToSkip = start;
14806 start = (View) parent;
14807 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080014808 }
14809
14810 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014811 * Sets the identifier for this view. The identifier does not have to be
14812 * unique in this view's hierarchy. The identifier should be a positive
14813 * number.
14814 *
14815 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070014816 * @see #getId()
14817 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014818 *
14819 * @param id a number used to identify the view
14820 *
14821 * @attr ref android.R.styleable#View_id
14822 */
14823 public void setId(int id) {
14824 mID = id;
14825 }
14826
14827 /**
14828 * {@hide}
14829 *
14830 * @param isRoot true if the view belongs to the root namespace, false
14831 * otherwise
14832 */
14833 public void setIsRootNamespace(boolean isRoot) {
14834 if (isRoot) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014835 mPrivateFlags |= PFLAG_IS_ROOT_NAMESPACE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014836 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014837 mPrivateFlags &= ~PFLAG_IS_ROOT_NAMESPACE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014838 }
14839 }
14840
14841 /**
14842 * {@hide}
14843 *
14844 * @return true if the view belongs to the root namespace, false otherwise
14845 */
14846 public boolean isRootNamespace() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014847 return (mPrivateFlags&PFLAG_IS_ROOT_NAMESPACE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014848 }
14849
14850 /**
14851 * Returns this view's identifier.
14852 *
14853 * @return a positive integer used to identify the view or {@link #NO_ID}
14854 * if the view has no ID
14855 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014856 * @see #setId(int)
14857 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014858 * @attr ref android.R.styleable#View_id
14859 */
14860 @ViewDebug.CapturedViewProperty
14861 public int getId() {
14862 return mID;
14863 }
14864
14865 /**
14866 * Returns this view's tag.
14867 *
14868 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070014869 *
14870 * @see #setTag(Object)
14871 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014872 */
14873 @ViewDebug.ExportedProperty
14874 public Object getTag() {
14875 return mTag;
14876 }
14877
14878 /**
14879 * Sets the tag associated with this view. A tag can be used to mark
14880 * a view in its hierarchy and does not have to be unique within the
14881 * hierarchy. Tags can also be used to store data within a view without
14882 * resorting to another data structure.
14883 *
14884 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070014885 *
14886 * @see #getTag()
14887 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014888 */
14889 public void setTag(final Object tag) {
14890 mTag = tag;
14891 }
14892
14893 /**
Romain Guyd90a3312009-05-06 14:54:28 -070014894 * Returns the tag associated with this view and the specified key.
14895 *
14896 * @param key The key identifying the tag
14897 *
14898 * @return the Object stored in this view as a tag
14899 *
14900 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070014901 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070014902 */
14903 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014904 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070014905 return null;
14906 }
14907
14908 /**
14909 * Sets a tag associated with this view and a key. A tag can be used
14910 * to mark a view in its hierarchy and does not have to be unique within
14911 * the hierarchy. Tags can also be used to store data within a view
14912 * without resorting to another data structure.
14913 *
14914 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070014915 * application to ensure it is unique (see the <a
14916 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
14917 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070014918 * the Android framework or not associated with any package will cause
14919 * an {@link IllegalArgumentException} to be thrown.
14920 *
14921 * @param key The key identifying the tag
14922 * @param tag An Object to tag the view with
14923 *
14924 * @throws IllegalArgumentException If they specified key is not valid
14925 *
14926 * @see #setTag(Object)
14927 * @see #getTag(int)
14928 */
14929 public void setTag(int key, final Object tag) {
14930 // If the package id is 0x00 or 0x01, it's either an undefined package
14931 // or a framework id
14932 if ((key >>> 24) < 2) {
14933 throw new IllegalArgumentException("The key must be an application-specific "
14934 + "resource id.");
14935 }
14936
Adam Powell2b2f6d62011-09-23 11:15:39 -070014937 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014938 }
14939
14940 /**
14941 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
14942 * framework id.
14943 *
14944 * @hide
14945 */
14946 public void setTagInternal(int key, Object tag) {
14947 if ((key >>> 24) != 0x1) {
14948 throw new IllegalArgumentException("The key must be a framework-specific "
14949 + "resource id.");
14950 }
14951
Adam Powell2b2f6d62011-09-23 11:15:39 -070014952 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014953 }
14954
Adam Powell2b2f6d62011-09-23 11:15:39 -070014955 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014956 if (mKeyedTags == null) {
14957 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070014958 }
14959
Adam Powell7db82ac2011-09-22 19:44:04 -070014960 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014961 }
14962
14963 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014964 * Prints information about this view in the log output, with the tag
14965 * {@link #VIEW_LOG_TAG}.
14966 *
14967 * @hide
14968 */
14969 public void debug() {
14970 debug(0);
14971 }
14972
14973 /**
14974 * Prints information about this view in the log output, with the tag
14975 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
14976 * indentation defined by the <code>depth</code>.
14977 *
14978 * @param depth the indentation level
14979 *
14980 * @hide
14981 */
14982 protected void debug(int depth) {
14983 String output = debugIndent(depth - 1);
14984
14985 output += "+ " + this;
14986 int id = getId();
14987 if (id != -1) {
14988 output += " (id=" + id + ")";
14989 }
14990 Object tag = getTag();
14991 if (tag != null) {
14992 output += " (tag=" + tag + ")";
14993 }
14994 Log.d(VIEW_LOG_TAG, output);
14995
Dianne Hackborn4702a852012-08-17 15:18:29 -070014996 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014997 output = debugIndent(depth) + " FOCUSED";
14998 Log.d(VIEW_LOG_TAG, output);
14999 }
15000
15001 output = debugIndent(depth);
15002 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
15003 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
15004 + "} ";
15005 Log.d(VIEW_LOG_TAG, output);
15006
15007 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
15008 || mPaddingBottom != 0) {
15009 output = debugIndent(depth);
15010 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
15011 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
15012 Log.d(VIEW_LOG_TAG, output);
15013 }
15014
15015 output = debugIndent(depth);
15016 output += "mMeasureWidth=" + mMeasuredWidth +
15017 " mMeasureHeight=" + mMeasuredHeight;
15018 Log.d(VIEW_LOG_TAG, output);
15019
15020 output = debugIndent(depth);
15021 if (mLayoutParams == null) {
15022 output += "BAD! no layout params";
15023 } else {
15024 output = mLayoutParams.debug(output);
15025 }
15026 Log.d(VIEW_LOG_TAG, output);
15027
15028 output = debugIndent(depth);
15029 output += "flags={";
15030 output += View.printFlags(mViewFlags);
15031 output += "}";
15032 Log.d(VIEW_LOG_TAG, output);
15033
15034 output = debugIndent(depth);
15035 output += "privateFlags={";
15036 output += View.printPrivateFlags(mPrivateFlags);
15037 output += "}";
15038 Log.d(VIEW_LOG_TAG, output);
15039 }
15040
15041 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090015042 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015043 *
15044 * @param depth the indentation level
15045 * @return a String containing (depth * 2 + 3) * 2 white spaces
15046 *
15047 * @hide
15048 */
15049 protected static String debugIndent(int depth) {
15050 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
15051 for (int i = 0; i < (depth * 2) + 3; i++) {
15052 spaces.append(' ').append(' ');
15053 }
15054 return spaces.toString();
15055 }
15056
15057 /**
15058 * <p>Return the offset of the widget's text baseline from the widget's top
15059 * boundary. If this widget does not support baseline alignment, this
15060 * method returns -1. </p>
15061 *
15062 * @return the offset of the baseline within the widget's bounds or -1
15063 * if baseline alignment is not supported
15064 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070015065 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015066 public int getBaseline() {
15067 return -1;
15068 }
15069
15070 /**
15071 * Call this when something has changed which has invalidated the
15072 * layout of this view. This will schedule a layout pass of the view
15073 * tree.
15074 */
15075 public void requestLayout() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015076 mPrivateFlags |= PFLAG_FORCE_LAYOUT;
15077 mPrivateFlags |= PFLAG_INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015078
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070015079 if (mParent != null && !mParent.isLayoutRequested()) {
15080 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015081 }
15082 }
15083
15084 /**
15085 * Forces this view to be laid out during the next layout pass.
15086 * This method does not call requestLayout() or forceLayout()
15087 * on the parent.
15088 */
15089 public void forceLayout() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015090 mPrivateFlags |= PFLAG_FORCE_LAYOUT;
15091 mPrivateFlags |= PFLAG_INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015092 }
15093
15094 /**
15095 * <p>
15096 * This is called to find out how big a view should be. The parent
15097 * supplies constraint information in the width and height parameters.
15098 * </p>
15099 *
15100 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080015101 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015102 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080015103 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015104 * </p>
15105 *
15106 *
15107 * @param widthMeasureSpec Horizontal space requirements as imposed by the
15108 * parent
15109 * @param heightMeasureSpec Vertical space requirements as imposed by the
15110 * parent
15111 *
15112 * @see #onMeasure(int, int)
15113 */
15114 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015115 if ((mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015116 widthMeasureSpec != mOldWidthMeasureSpec ||
15117 heightMeasureSpec != mOldHeightMeasureSpec) {
15118
15119 // first clears the measured dimension flag
Dianne Hackborn4702a852012-08-17 15:18:29 -070015120 mPrivateFlags &= ~PFLAG_MEASURED_DIMENSION_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015121
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070015122 if (!isPaddingResolved()) {
Adam Powell0090f202012-08-07 17:15:30 -070015123 resolvePadding();
15124 }
15125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015126 // measure ourselves, this should set the measured dimension flag back
15127 onMeasure(widthMeasureSpec, heightMeasureSpec);
15128
15129 // flag not set, setMeasuredDimension() was not invoked, we raise
15130 // an exception to warn the developer
Dianne Hackborn4702a852012-08-17 15:18:29 -070015131 if ((mPrivateFlags & PFLAG_MEASURED_DIMENSION_SET) != PFLAG_MEASURED_DIMENSION_SET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015132 throw new IllegalStateException("onMeasure() did not set the"
15133 + " measured dimension by calling"
15134 + " setMeasuredDimension()");
15135 }
15136
Dianne Hackborn4702a852012-08-17 15:18:29 -070015137 mPrivateFlags |= PFLAG_LAYOUT_REQUIRED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015138 }
15139
15140 mOldWidthMeasureSpec = widthMeasureSpec;
15141 mOldHeightMeasureSpec = heightMeasureSpec;
15142 }
15143
15144 /**
15145 * <p>
15146 * Measure the view and its content to determine the measured width and the
15147 * measured height. This method is invoked by {@link #measure(int, int)} and
15148 * should be overriden by subclasses to provide accurate and efficient
15149 * measurement of their contents.
15150 * </p>
15151 *
15152 * <p>
15153 * <strong>CONTRACT:</strong> When overriding this method, you
15154 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
15155 * measured width and height of this view. Failure to do so will trigger an
15156 * <code>IllegalStateException</code>, thrown by
15157 * {@link #measure(int, int)}. Calling the superclass'
15158 * {@link #onMeasure(int, int)} is a valid use.
15159 * </p>
15160 *
15161 * <p>
15162 * The base class implementation of measure defaults to the background size,
15163 * unless a larger size is allowed by the MeasureSpec. Subclasses should
15164 * override {@link #onMeasure(int, int)} to provide better measurements of
15165 * their content.
15166 * </p>
15167 *
15168 * <p>
15169 * If this method is overridden, it is the subclass's responsibility to make
15170 * sure the measured height and width are at least the view's minimum height
15171 * and width ({@link #getSuggestedMinimumHeight()} and
15172 * {@link #getSuggestedMinimumWidth()}).
15173 * </p>
15174 *
15175 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
15176 * The requirements are encoded with
15177 * {@link android.view.View.MeasureSpec}.
15178 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
15179 * The requirements are encoded with
15180 * {@link android.view.View.MeasureSpec}.
15181 *
15182 * @see #getMeasuredWidth()
15183 * @see #getMeasuredHeight()
15184 * @see #setMeasuredDimension(int, int)
15185 * @see #getSuggestedMinimumHeight()
15186 * @see #getSuggestedMinimumWidth()
15187 * @see android.view.View.MeasureSpec#getMode(int)
15188 * @see android.view.View.MeasureSpec#getSize(int)
15189 */
15190 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
15191 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
15192 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
15193 }
15194
15195 /**
15196 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
15197 * measured width and measured height. Failing to do so will trigger an
15198 * exception at measurement time.</p>
15199 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080015200 * @param measuredWidth The measured width of this view. May be a complex
15201 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15202 * {@link #MEASURED_STATE_TOO_SMALL}.
15203 * @param measuredHeight The measured height of this view. May be a complex
15204 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15205 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015206 */
15207 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
15208 mMeasuredWidth = measuredWidth;
15209 mMeasuredHeight = measuredHeight;
15210
Dianne Hackborn4702a852012-08-17 15:18:29 -070015211 mPrivateFlags |= PFLAG_MEASURED_DIMENSION_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015212 }
15213
15214 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080015215 * Merge two states as returned by {@link #getMeasuredState()}.
15216 * @param curState The current state as returned from a view or the result
15217 * of combining multiple views.
15218 * @param newState The new view state to combine.
15219 * @return Returns a new integer reflecting the combination of the two
15220 * states.
15221 */
15222 public static int combineMeasuredStates(int curState, int newState) {
15223 return curState | newState;
15224 }
15225
15226 /**
15227 * Version of {@link #resolveSizeAndState(int, int, int)}
15228 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
15229 */
15230 public static int resolveSize(int size, int measureSpec) {
15231 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
15232 }
15233
15234 /**
15235 * Utility to reconcile a desired size and state, with constraints imposed
15236 * by a MeasureSpec. Will take the desired size, unless a different size
15237 * is imposed by the constraints. The returned value is a compound integer,
15238 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
15239 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
15240 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015241 *
15242 * @param size How big the view wants to be
15243 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080015244 * @return Size information bit mask as defined by
15245 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015246 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080015247 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015248 int result = size;
15249 int specMode = MeasureSpec.getMode(measureSpec);
15250 int specSize = MeasureSpec.getSize(measureSpec);
15251 switch (specMode) {
15252 case MeasureSpec.UNSPECIFIED:
15253 result = size;
15254 break;
15255 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080015256 if (specSize < size) {
15257 result = specSize | MEASURED_STATE_TOO_SMALL;
15258 } else {
15259 result = size;
15260 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015261 break;
15262 case MeasureSpec.EXACTLY:
15263 result = specSize;
15264 break;
15265 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080015266 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015267 }
15268
15269 /**
15270 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070015271 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015272 * by the MeasureSpec.
15273 *
15274 * @param size Default size for this view
15275 * @param measureSpec Constraints imposed by the parent
15276 * @return The size this view should be.
15277 */
15278 public static int getDefaultSize(int size, int measureSpec) {
15279 int result = size;
15280 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070015281 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015282
15283 switch (specMode) {
15284 case MeasureSpec.UNSPECIFIED:
15285 result = size;
15286 break;
15287 case MeasureSpec.AT_MOST:
15288 case MeasureSpec.EXACTLY:
15289 result = specSize;
15290 break;
15291 }
15292 return result;
15293 }
15294
15295 /**
15296 * Returns the suggested minimum height that the view should use. This
15297 * returns the maximum of the view's minimum height
15298 * and the background's minimum height
15299 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
15300 * <p>
15301 * When being used in {@link #onMeasure(int, int)}, the caller should still
15302 * ensure the returned height is within the requirements of the parent.
15303 *
15304 * @return The suggested minimum height of the view.
15305 */
15306 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015307 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015309 }
15310
15311 /**
15312 * Returns the suggested minimum width that the view should use. This
15313 * returns the maximum of the view's minimum width)
15314 * and the background's minimum width
15315 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
15316 * <p>
15317 * When being used in {@link #onMeasure(int, int)}, the caller should still
15318 * ensure the returned width is within the requirements of the parent.
15319 *
15320 * @return The suggested minimum width of the view.
15321 */
15322 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015323 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
15324 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015325
Philip Milne6c8ea062012-04-03 17:38:43 -070015326 /**
15327 * Returns the minimum height of the view.
15328 *
15329 * @return the minimum height the view will try to be.
15330 *
15331 * @see #setMinimumHeight(int)
15332 *
15333 * @attr ref android.R.styleable#View_minHeight
15334 */
15335 public int getMinimumHeight() {
15336 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015337 }
15338
15339 /**
15340 * Sets the minimum height of the view. It is not guaranteed the view will
15341 * be able to achieve this minimum height (for example, if its parent layout
15342 * constrains it with less available height).
15343 *
15344 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015345 *
15346 * @see #getMinimumHeight()
15347 *
15348 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015349 */
15350 public void setMinimumHeight(int minHeight) {
15351 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070015352 requestLayout();
15353 }
15354
15355 /**
15356 * Returns the minimum width of the view.
15357 *
15358 * @return the minimum width the view will try to be.
15359 *
15360 * @see #setMinimumWidth(int)
15361 *
15362 * @attr ref android.R.styleable#View_minWidth
15363 */
15364 public int getMinimumWidth() {
15365 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015366 }
15367
15368 /**
15369 * Sets the minimum width of the view. It is not guaranteed the view will
15370 * be able to achieve this minimum width (for example, if its parent layout
15371 * constrains it with less available width).
15372 *
15373 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015374 *
15375 * @see #getMinimumWidth()
15376 *
15377 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015378 */
15379 public void setMinimumWidth(int minWidth) {
15380 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070015381 requestLayout();
15382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015383 }
15384
15385 /**
15386 * Get the animation currently associated with this view.
15387 *
15388 * @return The animation that is currently playing or
15389 * scheduled to play for this view.
15390 */
15391 public Animation getAnimation() {
15392 return mCurrentAnimation;
15393 }
15394
15395 /**
15396 * Start the specified animation now.
15397 *
15398 * @param animation the animation to start now
15399 */
15400 public void startAnimation(Animation animation) {
15401 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
15402 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080015403 invalidateParentCaches();
15404 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015405 }
15406
15407 /**
15408 * Cancels any animations for this view.
15409 */
15410 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080015411 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080015412 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080015413 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015414 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080015415 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015416 }
15417
15418 /**
15419 * Sets the next animation to play for this view.
15420 * If you want the animation to play immediately, use
Chet Haase42428932012-05-11 15:39:07 -070015421 * {@link #startAnimation(android.view.animation.Animation)} instead.
15422 * This method provides allows fine-grained
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015423 * control over the start time and invalidation, but you
15424 * must make sure that 1) the animation has a start time set, and
Chet Haase42428932012-05-11 15:39:07 -070015425 * 2) the view's parent (which controls animations on its children)
15426 * will be invalidated when the animation is supposed to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015427 * start.
15428 *
15429 * @param animation The next animation, or null.
15430 */
15431 public void setAnimation(Animation animation) {
15432 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070015433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015434 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070015435 // If the screen is off assume the animation start time is now instead of
15436 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
15437 // would cause the animation to start when the screen turns back on
15438 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
15439 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
15440 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
15441 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015442 animation.reset();
15443 }
15444 }
15445
15446 /**
15447 * Invoked by a parent ViewGroup to notify the start of the animation
15448 * currently associated with this view. If you override this method,
15449 * always call super.onAnimationStart();
15450 *
15451 * @see #setAnimation(android.view.animation.Animation)
15452 * @see #getAnimation()
15453 */
15454 protected void onAnimationStart() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015455 mPrivateFlags |= PFLAG_ANIMATION_STARTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015456 }
15457
15458 /**
15459 * Invoked by a parent ViewGroup to notify the end of the animation
15460 * currently associated with this view. If you override this method,
15461 * always call super.onAnimationEnd();
15462 *
15463 * @see #setAnimation(android.view.animation.Animation)
15464 * @see #getAnimation()
15465 */
15466 protected void onAnimationEnd() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015467 mPrivateFlags &= ~PFLAG_ANIMATION_STARTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015468 }
15469
15470 /**
15471 * Invoked if there is a Transform that involves alpha. Subclass that can
15472 * draw themselves with the specified alpha should return true, and then
15473 * respect that alpha when their onDraw() is called. If this returns false
15474 * then the view may be redirected to draw into an offscreen buffer to
15475 * fulfill the request, which will look fine, but may be slower than if the
15476 * subclass handles it internally. The default implementation returns false.
15477 *
15478 * @param alpha The alpha (0..255) to apply to the view's drawing
15479 * @return true if the view can draw with the specified alpha.
15480 */
15481 protected boolean onSetAlpha(int alpha) {
15482 return false;
15483 }
15484
15485 /**
15486 * This is used by the RootView to perform an optimization when
15487 * the view hierarchy contains one or several SurfaceView.
15488 * SurfaceView is always considered transparent, but its children are not,
15489 * therefore all View objects remove themselves from the global transparent
15490 * region (passed as a parameter to this function).
15491 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015492 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015493 *
15494 * @return Returns true if the effective visibility of the view at this
15495 * point is opaque, regardless of the transparent region; returns false
15496 * if it is possible for underlying windows to be seen behind the view.
15497 *
15498 * {@hide}
15499 */
15500 public boolean gatherTransparentRegion(Region region) {
15501 final AttachInfo attachInfo = mAttachInfo;
15502 if (region != null && attachInfo != null) {
15503 final int pflags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070015504 if ((pflags & PFLAG_SKIP_DRAW) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015505 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
15506 // remove it from the transparent region.
15507 final int[] location = attachInfo.mTransparentLocation;
15508 getLocationInWindow(location);
15509 region.op(location[0], location[1], location[0] + mRight - mLeft,
15510 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Dianne Hackborn4702a852012-08-17 15:18:29 -070015511 } else if ((pflags & PFLAG_ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015512 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
15513 // exists, so we remove the background drawable's non-transparent
15514 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070015515 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015516 }
15517 }
15518 return true;
15519 }
15520
15521 /**
15522 * Play a sound effect for this view.
15523 *
15524 * <p>The framework will play sound effects for some built in actions, such as
15525 * clicking, but you may wish to play these effects in your widget,
15526 * for instance, for internal navigation.
15527 *
15528 * <p>The sound effect will only be played if sound effects are enabled by the user, and
15529 * {@link #isSoundEffectsEnabled()} is true.
15530 *
15531 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
15532 */
15533 public void playSoundEffect(int soundConstant) {
15534 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
15535 return;
15536 }
15537 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
15538 }
15539
15540 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015541 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015542 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015543 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015544 *
15545 * <p>The framework will provide haptic feedback for some built in actions,
15546 * such as long presses, but you may wish to provide feedback for your
15547 * own widget.
15548 *
15549 * <p>The feedback will only be performed if
15550 * {@link #isHapticFeedbackEnabled()} is true.
15551 *
15552 * @param feedbackConstant One of the constants defined in
15553 * {@link HapticFeedbackConstants}
15554 */
15555 public boolean performHapticFeedback(int feedbackConstant) {
15556 return performHapticFeedback(feedbackConstant, 0);
15557 }
15558
15559 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015560 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015561 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015562 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015563 *
15564 * @param feedbackConstant One of the constants defined in
15565 * {@link HapticFeedbackConstants}
15566 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
15567 */
15568 public boolean performHapticFeedback(int feedbackConstant, int flags) {
15569 if (mAttachInfo == null) {
15570 return false;
15571 }
Romain Guyf607bdc2010-09-10 19:20:06 -070015572 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070015573 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015574 && !isHapticFeedbackEnabled()) {
15575 return false;
15576 }
Romain Guy812ccbe2010-06-01 14:07:24 -070015577 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
15578 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015579 }
15580
15581 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015582 * Request that the visibility of the status bar or other screen/window
15583 * decorations be changed.
15584 *
15585 * <p>This method is used to put the over device UI into temporary modes
15586 * where the user's attention is focused more on the application content,
15587 * by dimming or hiding surrounding system affordances. This is typically
15588 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
15589 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
15590 * to be placed behind the action bar (and with these flags other system
15591 * affordances) so that smooth transitions between hiding and showing them
15592 * can be done.
15593 *
15594 * <p>Two representative examples of the use of system UI visibility is
15595 * implementing a content browsing application (like a magazine reader)
15596 * and a video playing application.
15597 *
15598 * <p>The first code shows a typical implementation of a View in a content
15599 * browsing application. In this implementation, the application goes
15600 * into a content-oriented mode by hiding the status bar and action bar,
15601 * and putting the navigation elements into lights out mode. The user can
15602 * then interact with content while in this mode. Such an application should
15603 * provide an easy way for the user to toggle out of the mode (such as to
15604 * check information in the status bar or access notifications). In the
15605 * implementation here, this is done simply by tapping on the content.
15606 *
15607 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
15608 * content}
15609 *
15610 * <p>This second code sample shows a typical implementation of a View
15611 * in a video playing application. In this situation, while the video is
15612 * playing the application would like to go into a complete full-screen mode,
15613 * to use as much of the display as possible for the video. When in this state
15614 * the user can not interact with the application; the system intercepts
Dianne Hackborncf675782012-05-10 15:07:24 -070015615 * touching on the screen to pop the UI out of full screen mode. See
15616 * {@link #fitSystemWindows(Rect)} for a sample layout that goes with this code.
Dianne Hackborn98014352012-04-05 18:31:41 -070015617 *
15618 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
15619 * content}
15620 *
15621 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15622 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15623 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15624 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015625 */
15626 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040015627 if (visibility != mSystemUiVisibility) {
15628 mSystemUiVisibility = visibility;
15629 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15630 mParent.recomputeViewAttributes(this);
15631 }
Joe Onorato664644d2011-01-23 17:53:23 -080015632 }
15633 }
15634
15635 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015636 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
15637 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15638 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15639 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15640 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015641 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080015642 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080015643 return mSystemUiVisibility;
15644 }
15645
Scott Mainec6331b2011-05-24 16:55:56 -070015646 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070015647 * Returns the current system UI visibility that is currently set for
15648 * the entire window. This is the combination of the
15649 * {@link #setSystemUiVisibility(int)} values supplied by all of the
15650 * views in the window.
15651 */
15652 public int getWindowSystemUiVisibility() {
15653 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
15654 }
15655
15656 /**
15657 * Override to find out when the window's requested system UI visibility
15658 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
15659 * This is different from the callbacks recieved through
15660 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
15661 * in that this is only telling you about the local request of the window,
15662 * not the actual values applied by the system.
15663 */
15664 public void onWindowSystemUiVisibilityChanged(int visible) {
15665 }
15666
15667 /**
15668 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
15669 * the view hierarchy.
15670 */
15671 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
15672 onWindowSystemUiVisibilityChanged(visible);
15673 }
15674
15675 /**
Scott Mainec6331b2011-05-24 16:55:56 -070015676 * Set a listener to receive callbacks when the visibility of the system bar changes.
15677 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
15678 */
Joe Onorato664644d2011-01-23 17:53:23 -080015679 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015680 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080015681 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15682 mParent.recomputeViewAttributes(this);
15683 }
15684 }
15685
15686 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015687 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
15688 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080015689 */
15690 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015691 ListenerInfo li = mListenerInfo;
15692 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
15693 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080015694 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080015695 }
15696 }
15697
Dianne Hackborncf675782012-05-10 15:07:24 -070015698 boolean updateLocalSystemUiVisibility(int localValue, int localChanges) {
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015699 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
15700 if (val != mSystemUiVisibility) {
15701 setSystemUiVisibility(val);
Dianne Hackborncf675782012-05-10 15:07:24 -070015702 return true;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015703 }
Dianne Hackborncf675782012-05-10 15:07:24 -070015704 return false;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015705 }
15706
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070015707 /** @hide */
15708 public void setDisabledSystemUiVisibility(int flags) {
15709 if (mAttachInfo != null) {
15710 if (mAttachInfo.mDisabledSystemUiVisibility != flags) {
15711 mAttachInfo.mDisabledSystemUiVisibility = flags;
15712 if (mParent != null) {
15713 mParent.recomputeViewAttributes(this);
15714 }
15715 }
15716 }
15717 }
15718
Joe Onorato664644d2011-01-23 17:53:23 -080015719 /**
Joe Malin32736f02011-01-19 16:14:20 -080015720 * Creates an image that the system displays during the drag and drop
15721 * operation. This is called a &quot;drag shadow&quot;. The default implementation
15722 * for a DragShadowBuilder based on a View returns an image that has exactly the same
15723 * appearance as the given View. The default also positions the center of the drag shadow
15724 * directly under the touch point. If no View is provided (the constructor with no parameters
15725 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
15726 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
15727 * default is an invisible drag shadow.
15728 * <p>
15729 * You are not required to use the View you provide to the constructor as the basis of the
15730 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
15731 * anything you want as the drag shadow.
15732 * </p>
15733 * <p>
15734 * You pass a DragShadowBuilder object to the system when you start the drag. The system
15735 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
15736 * size and position of the drag shadow. It uses this data to construct a
15737 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
15738 * so that your application can draw the shadow image in the Canvas.
15739 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070015740 *
15741 * <div class="special reference">
15742 * <h3>Developer Guides</h3>
15743 * <p>For a guide to implementing drag and drop features, read the
15744 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
15745 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070015746 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015747 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070015748 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070015749
15750 /**
Joe Malin32736f02011-01-19 16:14:20 -080015751 * Constructs a shadow image builder based on a View. By default, the resulting drag
15752 * shadow will have the same appearance and dimensions as the View, with the touch point
15753 * over the center of the View.
15754 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070015755 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015756 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070015757 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070015758 }
15759
Christopher Tate17ed60c2011-01-18 12:50:26 -080015760 /**
15761 * Construct a shadow builder object with no associated View. This
15762 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
15763 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
15764 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080015765 * reference to any View object. If they are not overridden, then the result is an
15766 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080015767 */
15768 public DragShadowBuilder() {
15769 mView = new WeakReference<View>(null);
15770 }
15771
15772 /**
15773 * Returns the View object that had been passed to the
15774 * {@link #View.DragShadowBuilder(View)}
15775 * constructor. If that View parameter was {@code null} or if the
15776 * {@link #View.DragShadowBuilder()}
15777 * constructor was used to instantiate the builder object, this method will return
15778 * null.
15779 *
15780 * @return The View object associate with this builder object.
15781 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070015782 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070015783 final public View getView() {
15784 return mView.get();
15785 }
15786
Christopher Tate2c095f32010-10-04 14:13:40 -070015787 /**
Joe Malin32736f02011-01-19 16:14:20 -080015788 * Provides the metrics for the shadow image. These include the dimensions of
15789 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070015790 * be centered under the touch location while dragging.
15791 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015792 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080015793 * same as the dimensions of the View itself and centers the shadow under
15794 * the touch point.
15795 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070015796 *
Joe Malin32736f02011-01-19 16:14:20 -080015797 * @param shadowSize A {@link android.graphics.Point} containing the width and height
15798 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
15799 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
15800 * image.
15801 *
15802 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
15803 * shadow image that should be underneath the touch point during the drag and drop
15804 * operation. Your application must set {@link android.graphics.Point#x} to the
15805 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070015806 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015807 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070015808 final View view = mView.get();
15809 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015810 shadowSize.set(view.getWidth(), view.getHeight());
15811 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070015812 } else {
15813 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
15814 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015815 }
15816
15817 /**
Joe Malin32736f02011-01-19 16:14:20 -080015818 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
15819 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015820 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070015821 *
Joe Malin32736f02011-01-19 16:14:20 -080015822 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070015823 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015824 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070015825 final View view = mView.get();
15826 if (view != null) {
15827 view.draw(canvas);
15828 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015829 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070015830 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015831 }
15832 }
15833
15834 /**
Joe Malin32736f02011-01-19 16:14:20 -080015835 * Starts a drag and drop operation. When your application calls this method, it passes a
15836 * {@link android.view.View.DragShadowBuilder} object to the system. The
15837 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
15838 * to get metrics for the drag shadow, and then calls the object's
15839 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
15840 * <p>
15841 * Once the system has the drag shadow, it begins the drag and drop operation by sending
15842 * drag events to all the View objects in your application that are currently visible. It does
15843 * this either by calling the View object's drag listener (an implementation of
15844 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
15845 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
15846 * Both are passed a {@link android.view.DragEvent} object that has a
15847 * {@link android.view.DragEvent#getAction()} value of
15848 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
15849 * </p>
15850 * <p>
15851 * Your application can invoke startDrag() on any attached View object. The View object does not
15852 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
15853 * be related to the View the user selected for dragging.
15854 * </p>
15855 * @param data A {@link android.content.ClipData} object pointing to the data to be
15856 * transferred by the drag and drop operation.
15857 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
15858 * drag shadow.
15859 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
15860 * drop operation. This Object is put into every DragEvent object sent by the system during the
15861 * current drag.
15862 * <p>
15863 * myLocalState is a lightweight mechanism for the sending information from the dragged View
15864 * to the target Views. For example, it can contain flags that differentiate between a
15865 * a copy operation and a move operation.
15866 * </p>
15867 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
15868 * so the parameter should be set to 0.
15869 * @return {@code true} if the method completes successfully, or
15870 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
15871 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070015872 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015873 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015874 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070015875 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015876 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070015877 }
15878 boolean okay = false;
15879
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015880 Point shadowSize = new Point();
15881 Point shadowTouchPoint = new Point();
15882 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070015883
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015884 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
15885 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
15886 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070015887 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015888
Chris Tatea32dcf72010-10-14 12:13:50 -070015889 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015890 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
15891 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070015892 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015893 Surface surface = new Surface();
15894 try {
15895 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015896 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070015897 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070015898 + " surface=" + surface);
15899 if (token != null) {
15900 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070015901 try {
Chris Tate6b391282010-10-14 15:48:59 -070015902 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015903 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070015904 } finally {
15905 surface.unlockCanvasAndPost(canvas);
15906 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015907
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015908 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080015909
15910 // Cache the local state object for delivery with DragEvents
15911 root.setLocalDragState(myLocalState);
15912
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015913 // repurpose 'shadowSize' for the last touch point
15914 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070015915
Christopher Tatea53146c2010-09-07 11:57:52 -070015916 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015917 shadowSize.x, shadowSize.y,
15918 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070015919 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070015920
15921 // Off and running! Release our local surface instance; the drag
15922 // shadow surface is now managed by the system process.
15923 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070015924 }
15925 } catch (Exception e) {
15926 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
15927 surface.destroy();
15928 }
15929
15930 return okay;
15931 }
15932
Christopher Tatea53146c2010-09-07 11:57:52 -070015933 /**
Joe Malin32736f02011-01-19 16:14:20 -080015934 * Handles drag events sent by the system following a call to
15935 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
15936 *<p>
15937 * When the system calls this method, it passes a
15938 * {@link android.view.DragEvent} object. A call to
15939 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
15940 * in DragEvent. The method uses these to determine what is happening in the drag and drop
15941 * operation.
15942 * @param event The {@link android.view.DragEvent} sent by the system.
15943 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
15944 * in DragEvent, indicating the type of drag event represented by this object.
15945 * @return {@code true} if the method was successful, otherwise {@code false}.
15946 * <p>
15947 * The method should return {@code true} in response to an action type of
15948 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
15949 * operation.
15950 * </p>
15951 * <p>
15952 * The method should also return {@code true} in response to an action type of
15953 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
15954 * {@code false} if it didn't.
15955 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015956 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070015957 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070015958 return false;
15959 }
15960
15961 /**
Joe Malin32736f02011-01-19 16:14:20 -080015962 * Detects if this View is enabled and has a drag event listener.
15963 * If both are true, then it calls the drag event listener with the
15964 * {@link android.view.DragEvent} it received. If the drag event listener returns
15965 * {@code true}, then dispatchDragEvent() returns {@code true}.
15966 * <p>
15967 * For all other cases, the method calls the
15968 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
15969 * method and returns its result.
15970 * </p>
15971 * <p>
15972 * This ensures that a drag event is always consumed, even if the View does not have a drag
15973 * event listener. However, if the View has a listener and the listener returns true, then
15974 * onDragEvent() is not called.
15975 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015976 */
15977 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080015978 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015979 ListenerInfo li = mListenerInfo;
15980 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
15981 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070015982 return true;
15983 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015984 return onDragEvent(event);
15985 }
15986
Christopher Tate3d4bf172011-03-28 16:16:46 -070015987 boolean canAcceptDrag() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015988 return (mPrivateFlags2 & PFLAG2_DRAG_CAN_ACCEPT) != 0;
Christopher Tate3d4bf172011-03-28 16:16:46 -070015989 }
15990
Christopher Tatea53146c2010-09-07 11:57:52 -070015991 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070015992 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
15993 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070015994 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070015995 */
15996 public void onCloseSystemDialogs(String reason) {
15997 }
Joe Malin32736f02011-01-19 16:14:20 -080015998
Dianne Hackbornffa42482009-09-23 22:20:11 -070015999 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016000 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070016001 * update a Region being computed for
16002 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016003 * that any non-transparent parts of the Drawable are removed from the
16004 * given transparent region.
16005 *
16006 * @param dr The Drawable whose transparency is to be applied to the region.
16007 * @param region A Region holding the current transparency information,
16008 * where any parts of the region that are set are considered to be
16009 * transparent. On return, this region will be modified to have the
16010 * transparency information reduced by the corresponding parts of the
16011 * Drawable that are not transparent.
16012 * {@hide}
16013 */
16014 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
16015 if (DBG) {
16016 Log.i("View", "Getting transparent region for: " + this);
16017 }
16018 final Region r = dr.getTransparentRegion();
16019 final Rect db = dr.getBounds();
16020 final AttachInfo attachInfo = mAttachInfo;
16021 if (r != null && attachInfo != null) {
16022 final int w = getRight()-getLeft();
16023 final int h = getBottom()-getTop();
16024 if (db.left > 0) {
16025 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
16026 r.op(0, 0, db.left, h, Region.Op.UNION);
16027 }
16028 if (db.right < w) {
16029 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
16030 r.op(db.right, 0, w, h, Region.Op.UNION);
16031 }
16032 if (db.top > 0) {
16033 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
16034 r.op(0, 0, w, db.top, Region.Op.UNION);
16035 }
16036 if (db.bottom < h) {
16037 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
16038 r.op(0, db.bottom, w, h, Region.Op.UNION);
16039 }
16040 final int[] location = attachInfo.mTransparentLocation;
16041 getLocationInWindow(location);
16042 r.translate(location[0], location[1]);
16043 region.op(r, Region.Op.INTERSECT);
16044 } else {
16045 region.op(db, Region.Op.DIFFERENCE);
16046 }
16047 }
16048
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016049 private void checkForLongClick(int delayOffset) {
16050 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
16051 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016052
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016053 if (mPendingCheckForLongPress == null) {
16054 mPendingCheckForLongPress = new CheckForLongPress();
16055 }
16056 mPendingCheckForLongPress.rememberWindowAttachCount();
16057 postDelayed(mPendingCheckForLongPress,
16058 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016059 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016060 }
16061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016062 /**
16063 * Inflate a view from an XML resource. This convenience method wraps the {@link
16064 * LayoutInflater} class, which provides a full range of options for view inflation.
16065 *
16066 * @param context The Context object for your activity or application.
16067 * @param resource The resource ID to inflate
16068 * @param root A view group that will be the parent. Used to properly inflate the
16069 * layout_* parameters.
16070 * @see LayoutInflater
16071 */
16072 public static View inflate(Context context, int resource, ViewGroup root) {
16073 LayoutInflater factory = LayoutInflater.from(context);
16074 return factory.inflate(resource, root);
16075 }
Romain Guy33e72ae2010-07-17 12:40:29 -070016076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016077 /**
Adam Powell637d3372010-08-25 14:37:03 -070016078 * Scroll the view with standard behavior for scrolling beyond the normal
16079 * content boundaries. Views that call this method should override
16080 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
16081 * results of an over-scroll operation.
16082 *
16083 * Views can use this method to handle any touch or fling-based scrolling.
16084 *
16085 * @param deltaX Change in X in pixels
16086 * @param deltaY Change in Y in pixels
16087 * @param scrollX Current X scroll value in pixels before applying deltaX
16088 * @param scrollY Current Y scroll value in pixels before applying deltaY
16089 * @param scrollRangeX Maximum content scroll range along the X axis
16090 * @param scrollRangeY Maximum content scroll range along the Y axis
16091 * @param maxOverScrollX Number of pixels to overscroll by in either direction
16092 * along the X axis.
16093 * @param maxOverScrollY Number of pixels to overscroll by in either direction
16094 * along the Y axis.
16095 * @param isTouchEvent true if this scroll operation is the result of a touch event.
16096 * @return true if scrolling was clamped to an over-scroll boundary along either
16097 * axis, false otherwise.
16098 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070016099 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070016100 protected boolean overScrollBy(int deltaX, int deltaY,
16101 int scrollX, int scrollY,
16102 int scrollRangeX, int scrollRangeY,
16103 int maxOverScrollX, int maxOverScrollY,
16104 boolean isTouchEvent) {
16105 final int overScrollMode = mOverScrollMode;
16106 final boolean canScrollHorizontal =
16107 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
16108 final boolean canScrollVertical =
16109 computeVerticalScrollRange() > computeVerticalScrollExtent();
16110 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
16111 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
16112 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
16113 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
16114
16115 int newScrollX = scrollX + deltaX;
16116 if (!overScrollHorizontal) {
16117 maxOverScrollX = 0;
16118 }
16119
16120 int newScrollY = scrollY + deltaY;
16121 if (!overScrollVertical) {
16122 maxOverScrollY = 0;
16123 }
16124
16125 // Clamp values if at the limits and record
16126 final int left = -maxOverScrollX;
16127 final int right = maxOverScrollX + scrollRangeX;
16128 final int top = -maxOverScrollY;
16129 final int bottom = maxOverScrollY + scrollRangeY;
16130
16131 boolean clampedX = false;
16132 if (newScrollX > right) {
16133 newScrollX = right;
16134 clampedX = true;
16135 } else if (newScrollX < left) {
16136 newScrollX = left;
16137 clampedX = true;
16138 }
16139
16140 boolean clampedY = false;
16141 if (newScrollY > bottom) {
16142 newScrollY = bottom;
16143 clampedY = true;
16144 } else if (newScrollY < top) {
16145 newScrollY = top;
16146 clampedY = true;
16147 }
16148
16149 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
16150
16151 return clampedX || clampedY;
16152 }
16153
16154 /**
16155 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
16156 * respond to the results of an over-scroll operation.
16157 *
16158 * @param scrollX New X scroll value in pixels
16159 * @param scrollY New Y scroll value in pixels
16160 * @param clampedX True if scrollX was clamped to an over-scroll boundary
16161 * @param clampedY True if scrollY was clamped to an over-scroll boundary
16162 */
16163 protected void onOverScrolled(int scrollX, int scrollY,
16164 boolean clampedX, boolean clampedY) {
16165 // Intentionally empty.
16166 }
16167
16168 /**
16169 * Returns the over-scroll mode for this view. The result will be
16170 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16171 * (allow over-scrolling only if the view content is larger than the container),
16172 * or {@link #OVER_SCROLL_NEVER}.
16173 *
16174 * @return This view's over-scroll mode.
16175 */
16176 public int getOverScrollMode() {
16177 return mOverScrollMode;
16178 }
16179
16180 /**
16181 * Set the over-scroll mode for this view. Valid over-scroll modes are
16182 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16183 * (allow over-scrolling only if the view content is larger than the container),
16184 * or {@link #OVER_SCROLL_NEVER}.
16185 *
16186 * Setting the over-scroll mode of a view will have an effect only if the
16187 * view is capable of scrolling.
16188 *
16189 * @param overScrollMode The new over-scroll mode for this view.
16190 */
16191 public void setOverScrollMode(int overScrollMode) {
16192 if (overScrollMode != OVER_SCROLL_ALWAYS &&
16193 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
16194 overScrollMode != OVER_SCROLL_NEVER) {
16195 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
16196 }
16197 mOverScrollMode = overScrollMode;
16198 }
16199
16200 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016201 * Gets a scale factor that determines the distance the view should scroll
16202 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
16203 * @return The vertical scroll scale factor.
16204 * @hide
16205 */
16206 protected float getVerticalScrollFactor() {
16207 if (mVerticalScrollFactor == 0) {
16208 TypedValue outValue = new TypedValue();
16209 if (!mContext.getTheme().resolveAttribute(
16210 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
16211 throw new IllegalStateException(
16212 "Expected theme to define listPreferredItemHeight.");
16213 }
16214 mVerticalScrollFactor = outValue.getDimension(
16215 mContext.getResources().getDisplayMetrics());
16216 }
16217 return mVerticalScrollFactor;
16218 }
16219
16220 /**
16221 * Gets a scale factor that determines the distance the view should scroll
16222 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
16223 * @return The horizontal scroll scale factor.
16224 * @hide
16225 */
16226 protected float getHorizontalScrollFactor() {
16227 // TODO: Should use something else.
16228 return getVerticalScrollFactor();
16229 }
16230
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016231 /**
16232 * Return the value specifying the text direction or policy that was set with
16233 * {@link #setTextDirection(int)}.
16234 *
16235 * @return the defined text direction. It can be one of:
16236 *
16237 * {@link #TEXT_DIRECTION_INHERIT},
16238 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16239 * {@link #TEXT_DIRECTION_ANY_RTL},
16240 * {@link #TEXT_DIRECTION_LTR},
16241 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016242 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016243 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016244 @ViewDebug.ExportedProperty(category = "text", mapping = {
16245 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
16246 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
16247 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
16248 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
16249 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
16250 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
16251 })
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016252 public int getTextDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016253 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_MASK) >> PFLAG2_TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016254 }
16255
16256 /**
16257 * Set the text direction.
16258 *
16259 * @param textDirection the direction to set. Should be one of:
16260 *
16261 * {@link #TEXT_DIRECTION_INHERIT},
16262 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16263 * {@link #TEXT_DIRECTION_ANY_RTL},
16264 * {@link #TEXT_DIRECTION_LTR},
16265 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016266 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016267 */
16268 public void setTextDirection(int textDirection) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016269 if (getTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016270 // Reset the current text direction and the resolved one
Dianne Hackborn4702a852012-08-17 15:18:29 -070016271 mPrivateFlags2 &= ~PFLAG2_TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016272 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016273 // Set the new text direction
Dianne Hackborn4702a852012-08-17 15:18:29 -070016274 mPrivateFlags2 |= ((textDirection << PFLAG2_TEXT_DIRECTION_MASK_SHIFT) & PFLAG2_TEXT_DIRECTION_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016275 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016276 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016277 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016278 }
16279 }
16280
16281 /**
16282 * Return the resolved text direction.
16283 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016284 * This needs resolution if the value is TEXT_DIRECTION_INHERIT. The resolution matches
16285 * {@link #getTextDirection()}if it is not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds
16286 * up the parent chain of the view. if there is no parent, then it will return the default
16287 * {@link #TEXT_DIRECTION_FIRST_STRONG}.
16288 *
16289 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016290 *
Doug Feltcb3791202011-07-07 11:57:48 -070016291 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16292 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016293 * {@link #TEXT_DIRECTION_LTR},
16294 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016295 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016296 */
16297 public int getResolvedTextDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070016298 // The text direction will be resolved only if needed
Dianne Hackborn4702a852012-08-17 15:18:29 -070016299 if ((mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_RESOLVED) != PFLAG2_TEXT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016300 resolveTextDirection();
16301 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070016302 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_RESOLVED_MASK) >> PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016303 }
16304
16305 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016306 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
16307 * resolution is done.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016308 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016309 public void resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016310 // Reset any previous text direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016311 mPrivateFlags2 &= ~(PFLAG2_TEXT_DIRECTION_RESOLVED | PFLAG2_TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016312
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016313 if (hasRtlSupport()) {
16314 // Set resolved text direction flag depending on text direction flag
16315 final int textDirection = getTextDirection();
16316 switch(textDirection) {
16317 case TEXT_DIRECTION_INHERIT:
16318 if (canResolveTextDirection()) {
16319 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016320
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016321 // Set current resolved direction to the same value as the parent's one
16322 final int parentResolvedDirection = viewGroup.getResolvedTextDirection();
16323 switch (parentResolvedDirection) {
16324 case TEXT_DIRECTION_FIRST_STRONG:
16325 case TEXT_DIRECTION_ANY_RTL:
16326 case TEXT_DIRECTION_LTR:
16327 case TEXT_DIRECTION_RTL:
16328 case TEXT_DIRECTION_LOCALE:
16329 mPrivateFlags2 |=
Dianne Hackborn4702a852012-08-17 15:18:29 -070016330 (parentResolvedDirection << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016331 break;
16332 default:
16333 // Default resolved direction is "first strong" heuristic
Dianne Hackborn4702a852012-08-17 15:18:29 -070016334 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016335 }
16336 } else {
16337 // We cannot do the resolution if there is no parent, so use the default one
Dianne Hackborn4702a852012-08-17 15:18:29 -070016338 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016339 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016340 break;
16341 case TEXT_DIRECTION_FIRST_STRONG:
16342 case TEXT_DIRECTION_ANY_RTL:
16343 case TEXT_DIRECTION_LTR:
16344 case TEXT_DIRECTION_RTL:
16345 case TEXT_DIRECTION_LOCALE:
16346 // Resolved direction is the same as text direction
Dianne Hackborn4702a852012-08-17 15:18:29 -070016347 mPrivateFlags2 |= (textDirection << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016348 break;
16349 default:
16350 // Default resolved direction is "first strong" heuristic
Dianne Hackborn4702a852012-08-17 15:18:29 -070016351 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016352 }
16353 } else {
16354 // Default resolved direction is "first strong" heuristic
Dianne Hackborn4702a852012-08-17 15:18:29 -070016355 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016356 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016357
16358 // Set to resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070016359 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016360 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016361 }
16362
16363 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016364 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016365 * resolution should override this method.
16366 *
16367 * The default implementation does nothing.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016368 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016369 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016370 }
16371
16372 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016373 * Check if text direction resolution can be done.
16374 *
16375 * @return true if text direction resolution can be done otherwise return false.
16376 */
16377 public boolean canResolveTextDirection() {
16378 switch (getTextDirection()) {
16379 case TEXT_DIRECTION_INHERIT:
16380 return (mParent != null) && (mParent instanceof ViewGroup);
16381 default:
16382 return true;
16383 }
16384 }
16385
16386 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016387 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016388 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016389 * reset is done.
16390 */
16391 public void resetResolvedTextDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016392 mPrivateFlags2 &= ~(PFLAG2_TEXT_DIRECTION_RESOLVED | PFLAG2_TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016393 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016394 }
16395
16396 /**
16397 * Called when text direction is reset. Subclasses that care about text direction reset should
16398 * override this method and do a reset of the text direction of their children. The default
16399 * implementation does nothing.
16400 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016401 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016402 }
16403
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016404 /**
16405 * Return the value specifying the text alignment or policy that was set with
16406 * {@link #setTextAlignment(int)}.
16407 *
16408 * @return the defined text alignment. It can be one of:
16409 *
16410 * {@link #TEXT_ALIGNMENT_INHERIT},
16411 * {@link #TEXT_ALIGNMENT_GRAVITY},
16412 * {@link #TEXT_ALIGNMENT_CENTER},
16413 * {@link #TEXT_ALIGNMENT_TEXT_START},
16414 * {@link #TEXT_ALIGNMENT_TEXT_END},
16415 * {@link #TEXT_ALIGNMENT_VIEW_START},
16416 * {@link #TEXT_ALIGNMENT_VIEW_END}
16417 */
16418 @ViewDebug.ExportedProperty(category = "text", mapping = {
16419 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16420 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16421 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16422 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16423 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16424 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16425 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16426 })
16427 public int getTextAlignment() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016428 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_MASK) >> PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016429 }
16430
16431 /**
16432 * Set the text alignment.
16433 *
16434 * @param textAlignment The text alignment to set. Should be one of
16435 *
16436 * {@link #TEXT_ALIGNMENT_INHERIT},
16437 * {@link #TEXT_ALIGNMENT_GRAVITY},
16438 * {@link #TEXT_ALIGNMENT_CENTER},
16439 * {@link #TEXT_ALIGNMENT_TEXT_START},
16440 * {@link #TEXT_ALIGNMENT_TEXT_END},
16441 * {@link #TEXT_ALIGNMENT_VIEW_START},
16442 * {@link #TEXT_ALIGNMENT_VIEW_END}
16443 *
16444 * @attr ref android.R.styleable#View_textAlignment
16445 */
16446 public void setTextAlignment(int textAlignment) {
16447 if (textAlignment != getTextAlignment()) {
16448 // Reset the current and resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016449 mPrivateFlags2 &= ~PFLAG2_TEXT_ALIGNMENT_MASK;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016450 resetResolvedTextAlignment();
16451 // Set the new text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016452 mPrivateFlags2 |= ((textAlignment << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT) & PFLAG2_TEXT_ALIGNMENT_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016453 // Refresh
16454 requestLayout();
16455 invalidate(true);
16456 }
16457 }
16458
16459 /**
16460 * Return the resolved text alignment.
16461 *
16462 * The resolved text alignment. This needs resolution if the value is
16463 * TEXT_ALIGNMENT_INHERIT. The resolution matches {@link #setTextAlignment(int)} if it is
16464 * not TEXT_ALIGNMENT_INHERIT, otherwise resolution proceeds up the parent chain of the view.
16465 *
16466 * @return the resolved text alignment. Returns one of:
16467 *
16468 * {@link #TEXT_ALIGNMENT_GRAVITY},
16469 * {@link #TEXT_ALIGNMENT_CENTER},
16470 * {@link #TEXT_ALIGNMENT_TEXT_START},
16471 * {@link #TEXT_ALIGNMENT_TEXT_END},
16472 * {@link #TEXT_ALIGNMENT_VIEW_START},
16473 * {@link #TEXT_ALIGNMENT_VIEW_END}
16474 */
16475 @ViewDebug.ExportedProperty(category = "text", mapping = {
16476 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16477 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16478 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16479 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16480 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16481 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16482 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16483 })
16484 public int getResolvedTextAlignment() {
16485 // If text alignment is not resolved, then resolve it
Dianne Hackborn4702a852012-08-17 15:18:29 -070016486 if ((mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_RESOLVED) != PFLAG2_TEXT_ALIGNMENT_RESOLVED) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016487 resolveTextAlignment();
16488 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070016489 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK) >> PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016490 }
16491
16492 /**
16493 * Resolve the text alignment. Will call {@link View#onResolvedTextAlignmentChanged} when
16494 * resolution is done.
16495 */
16496 public void resolveTextAlignment() {
16497 // Reset any previous text alignment resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016498 mPrivateFlags2 &= ~(PFLAG2_TEXT_ALIGNMENT_RESOLVED | PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016499
16500 if (hasRtlSupport()) {
16501 // Set resolved text alignment flag depending on text alignment flag
16502 final int textAlignment = getTextAlignment();
16503 switch (textAlignment) {
16504 case TEXT_ALIGNMENT_INHERIT:
16505 // Check if we can resolve the text alignment
16506 if (canResolveLayoutDirection() && mParent instanceof View) {
16507 View view = (View) mParent;
16508
16509 final int parentResolvedTextAlignment = view.getResolvedTextAlignment();
16510 switch (parentResolvedTextAlignment) {
16511 case TEXT_ALIGNMENT_GRAVITY:
16512 case TEXT_ALIGNMENT_TEXT_START:
16513 case TEXT_ALIGNMENT_TEXT_END:
16514 case TEXT_ALIGNMENT_CENTER:
16515 case TEXT_ALIGNMENT_VIEW_START:
16516 case TEXT_ALIGNMENT_VIEW_END:
16517 // Resolved text alignment is the same as the parent resolved
16518 // text alignment
16519 mPrivateFlags2 |=
Dianne Hackborn4702a852012-08-17 15:18:29 -070016520 (parentResolvedTextAlignment << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016521 break;
16522 default:
16523 // Use default resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016524 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016525 }
16526 }
16527 else {
16528 // We cannot do the resolution if there is no parent so use the default
Dianne Hackborn4702a852012-08-17 15:18:29 -070016529 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016530 }
16531 break;
16532 case TEXT_ALIGNMENT_GRAVITY:
16533 case TEXT_ALIGNMENT_TEXT_START:
16534 case TEXT_ALIGNMENT_TEXT_END:
16535 case TEXT_ALIGNMENT_CENTER:
16536 case TEXT_ALIGNMENT_VIEW_START:
16537 case TEXT_ALIGNMENT_VIEW_END:
16538 // Resolved text alignment is the same as text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016539 mPrivateFlags2 |= (textAlignment << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016540 break;
16541 default:
16542 // Use default resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016543 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016544 }
16545 } else {
16546 // Use default resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016547 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016548 }
16549
16550 // Set the resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070016551 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016552 onResolvedTextAlignmentChanged();
16553 }
16554
16555 /**
16556 * Check if text alignment resolution can be done.
16557 *
16558 * @return true if text alignment resolution can be done otherwise return false.
16559 */
16560 public boolean canResolveTextAlignment() {
16561 switch (getTextAlignment()) {
16562 case TEXT_DIRECTION_INHERIT:
16563 return (mParent != null);
16564 default:
16565 return true;
16566 }
16567 }
16568
16569 /**
16570 * Called when text alignment has been resolved. Subclasses that care about text alignment
16571 * resolution should override this method.
16572 *
16573 * The default implementation does nothing.
16574 */
16575 public void onResolvedTextAlignmentChanged() {
16576 }
16577
16578 /**
16579 * Reset resolved text alignment. Text alignment can be resolved with a call to
16580 * getResolvedTextAlignment(). Will call {@link View#onResolvedTextAlignmentReset} when
16581 * reset is done.
16582 */
16583 public void resetResolvedTextAlignment() {
16584 // Reset any previous text alignment resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016585 mPrivateFlags2 &= ~(PFLAG2_TEXT_ALIGNMENT_RESOLVED | PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016586 onResolvedTextAlignmentReset();
16587 }
16588
16589 /**
16590 * Called when text alignment is reset. Subclasses that care about text alignment reset should
16591 * override this method and do a reset of the text alignment of their children. The default
16592 * implementation does nothing.
16593 */
16594 public void onResolvedTextAlignmentReset() {
16595 }
16596
Adam Powella9108a22012-07-18 11:18:09 -070016597 /**
16598 * Generate a value suitable for use in {@link #setId(int)}.
16599 * This value will not collide with ID values generated at build time by aapt for R.id.
16600 *
16601 * @return a generated ID value
16602 */
16603 public static int generateViewId() {
16604 for (;;) {
16605 final int result = sNextGeneratedId.get();
16606 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
16607 int newValue = result + 1;
16608 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
16609 if (sNextGeneratedId.compareAndSet(result, newValue)) {
16610 return result;
16611 }
16612 }
16613 }
16614
Chet Haaseb39f0512011-05-24 14:36:40 -070016615 //
16616 // Properties
16617 //
16618 /**
16619 * A Property wrapper around the <code>alpha</code> functionality handled by the
16620 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
16621 */
Chet Haased47f1532011-12-16 11:18:52 -080016622 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016623 @Override
16624 public void setValue(View object, float value) {
16625 object.setAlpha(value);
16626 }
16627
16628 @Override
16629 public Float get(View object) {
16630 return object.getAlpha();
16631 }
16632 };
16633
16634 /**
16635 * A Property wrapper around the <code>translationX</code> functionality handled by the
16636 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
16637 */
Chet Haased47f1532011-12-16 11:18:52 -080016638 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016639 @Override
16640 public void setValue(View object, float value) {
16641 object.setTranslationX(value);
16642 }
16643
16644 @Override
16645 public Float get(View object) {
16646 return object.getTranslationX();
16647 }
16648 };
16649
16650 /**
16651 * A Property wrapper around the <code>translationY</code> functionality handled by the
16652 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
16653 */
Chet Haased47f1532011-12-16 11:18:52 -080016654 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016655 @Override
16656 public void setValue(View object, float value) {
16657 object.setTranslationY(value);
16658 }
16659
16660 @Override
16661 public Float get(View object) {
16662 return object.getTranslationY();
16663 }
16664 };
16665
16666 /**
16667 * A Property wrapper around the <code>x</code> functionality handled by the
16668 * {@link View#setX(float)} and {@link View#getX()} methods.
16669 */
Chet Haased47f1532011-12-16 11:18:52 -080016670 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016671 @Override
16672 public void setValue(View object, float value) {
16673 object.setX(value);
16674 }
16675
16676 @Override
16677 public Float get(View object) {
16678 return object.getX();
16679 }
16680 };
16681
16682 /**
16683 * A Property wrapper around the <code>y</code> functionality handled by the
16684 * {@link View#setY(float)} and {@link View#getY()} methods.
16685 */
Chet Haased47f1532011-12-16 11:18:52 -080016686 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016687 @Override
16688 public void setValue(View object, float value) {
16689 object.setY(value);
16690 }
16691
16692 @Override
16693 public Float get(View object) {
16694 return object.getY();
16695 }
16696 };
16697
16698 /**
16699 * A Property wrapper around the <code>rotation</code> functionality handled by the
16700 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
16701 */
Chet Haased47f1532011-12-16 11:18:52 -080016702 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016703 @Override
16704 public void setValue(View object, float value) {
16705 object.setRotation(value);
16706 }
16707
16708 @Override
16709 public Float get(View object) {
16710 return object.getRotation();
16711 }
16712 };
16713
16714 /**
16715 * A Property wrapper around the <code>rotationX</code> functionality handled by the
16716 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
16717 */
Chet Haased47f1532011-12-16 11:18:52 -080016718 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016719 @Override
16720 public void setValue(View object, float value) {
16721 object.setRotationX(value);
16722 }
16723
16724 @Override
16725 public Float get(View object) {
16726 return object.getRotationX();
16727 }
16728 };
16729
16730 /**
16731 * A Property wrapper around the <code>rotationY</code> functionality handled by the
16732 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
16733 */
Chet Haased47f1532011-12-16 11:18:52 -080016734 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016735 @Override
16736 public void setValue(View object, float value) {
16737 object.setRotationY(value);
16738 }
16739
16740 @Override
16741 public Float get(View object) {
16742 return object.getRotationY();
16743 }
16744 };
16745
16746 /**
16747 * A Property wrapper around the <code>scaleX</code> functionality handled by the
16748 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
16749 */
Chet Haased47f1532011-12-16 11:18:52 -080016750 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016751 @Override
16752 public void setValue(View object, float value) {
16753 object.setScaleX(value);
16754 }
16755
16756 @Override
16757 public Float get(View object) {
16758 return object.getScaleX();
16759 }
16760 };
16761
16762 /**
16763 * A Property wrapper around the <code>scaleY</code> functionality handled by the
16764 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
16765 */
Chet Haased47f1532011-12-16 11:18:52 -080016766 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016767 @Override
16768 public void setValue(View object, float value) {
16769 object.setScaleY(value);
16770 }
16771
16772 @Override
16773 public Float get(View object) {
16774 return object.getScaleY();
16775 }
16776 };
16777
Jeff Brown33bbfd22011-02-24 20:55:35 -080016778 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016779 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
16780 * Each MeasureSpec represents a requirement for either the width or the height.
16781 * A MeasureSpec is comprised of a size and a mode. There are three possible
16782 * modes:
16783 * <dl>
16784 * <dt>UNSPECIFIED</dt>
16785 * <dd>
16786 * The parent has not imposed any constraint on the child. It can be whatever size
16787 * it wants.
16788 * </dd>
16789 *
16790 * <dt>EXACTLY</dt>
16791 * <dd>
16792 * The parent has determined an exact size for the child. The child is going to be
16793 * given those bounds regardless of how big it wants to be.
16794 * </dd>
16795 *
16796 * <dt>AT_MOST</dt>
16797 * <dd>
16798 * The child can be as large as it wants up to the specified size.
16799 * </dd>
16800 * </dl>
16801 *
16802 * MeasureSpecs are implemented as ints to reduce object allocation. This class
16803 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
16804 */
16805 public static class MeasureSpec {
16806 private static final int MODE_SHIFT = 30;
16807 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
16808
16809 /**
16810 * Measure specification mode: The parent has not imposed any constraint
16811 * on the child. It can be whatever size it wants.
16812 */
16813 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
16814
16815 /**
16816 * Measure specification mode: The parent has determined an exact size
16817 * for the child. The child is going to be given those bounds regardless
16818 * of how big it wants to be.
16819 */
16820 public static final int EXACTLY = 1 << MODE_SHIFT;
16821
16822 /**
16823 * Measure specification mode: The child can be as large as it wants up
16824 * to the specified size.
16825 */
16826 public static final int AT_MOST = 2 << MODE_SHIFT;
16827
16828 /**
16829 * Creates a measure specification based on the supplied size and mode.
16830 *
16831 * The mode must always be one of the following:
16832 * <ul>
16833 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
16834 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
16835 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
16836 * </ul>
16837 *
16838 * @param size the size of the measure specification
16839 * @param mode the mode of the measure specification
16840 * @return the measure specification based on size and mode
16841 */
16842 public static int makeMeasureSpec(int size, int mode) {
16843 return size + mode;
16844 }
16845
16846 /**
16847 * Extracts the mode from the supplied measure specification.
16848 *
16849 * @param measureSpec the measure specification to extract the mode from
16850 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
16851 * {@link android.view.View.MeasureSpec#AT_MOST} or
16852 * {@link android.view.View.MeasureSpec#EXACTLY}
16853 */
16854 public static int getMode(int measureSpec) {
16855 return (measureSpec & MODE_MASK);
16856 }
16857
16858 /**
16859 * Extracts the size from the supplied measure specification.
16860 *
16861 * @param measureSpec the measure specification to extract the size from
16862 * @return the size in pixels defined in the supplied measure specification
16863 */
16864 public static int getSize(int measureSpec) {
16865 return (measureSpec & ~MODE_MASK);
16866 }
16867
16868 /**
16869 * Returns a String representation of the specified measure
16870 * specification.
16871 *
16872 * @param measureSpec the measure specification to convert to a String
16873 * @return a String with the following format: "MeasureSpec: MODE SIZE"
16874 */
16875 public static String toString(int measureSpec) {
16876 int mode = getMode(measureSpec);
16877 int size = getSize(measureSpec);
16878
16879 StringBuilder sb = new StringBuilder("MeasureSpec: ");
16880
16881 if (mode == UNSPECIFIED)
16882 sb.append("UNSPECIFIED ");
16883 else if (mode == EXACTLY)
16884 sb.append("EXACTLY ");
16885 else if (mode == AT_MOST)
16886 sb.append("AT_MOST ");
16887 else
16888 sb.append(mode).append(" ");
16889
16890 sb.append(size);
16891 return sb.toString();
16892 }
16893 }
16894
16895 class CheckForLongPress implements Runnable {
16896
16897 private int mOriginalWindowAttachCount;
16898
16899 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070016900 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016901 && mOriginalWindowAttachCount == mWindowAttachCount) {
16902 if (performLongClick()) {
16903 mHasPerformedLongPress = true;
16904 }
16905 }
16906 }
16907
16908 public void rememberWindowAttachCount() {
16909 mOriginalWindowAttachCount = mWindowAttachCount;
16910 }
16911 }
Joe Malin32736f02011-01-19 16:14:20 -080016912
Adam Powelle14579b2009-12-16 18:39:52 -080016913 private final class CheckForTap implements Runnable {
16914 public void run() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016915 mPrivateFlags &= ~PFLAG_PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080016916 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016917 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080016918 }
16919 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016920
Adam Powella35d7682010-03-12 14:48:13 -080016921 private final class PerformClick implements Runnable {
16922 public void run() {
16923 performClick();
16924 }
16925 }
16926
Dianne Hackborn63042d62011-01-26 18:56:29 -080016927 /** @hide */
16928 public void hackTurnOffWindowResizeAnim(boolean off) {
16929 mAttachInfo.mTurnOffWindowResizeAnim = off;
16930 }
Joe Malin32736f02011-01-19 16:14:20 -080016931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016932 /**
Chet Haasea00f3862011-02-22 06:34:40 -080016933 * This method returns a ViewPropertyAnimator object, which can be used to animate
16934 * specific properties on this View.
16935 *
16936 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
16937 */
16938 public ViewPropertyAnimator animate() {
16939 if (mAnimator == null) {
16940 mAnimator = new ViewPropertyAnimator(this);
16941 }
16942 return mAnimator;
16943 }
16944
16945 /**
Jean Chalard405bc512012-05-29 19:12:34 +090016946 * Interface definition for a callback to be invoked when a hardware key event is
16947 * dispatched to this view. The callback will be invoked before the key event is
16948 * given to the view. This is only useful for hardware keyboards; a software input
16949 * method has no obligation to trigger this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016950 */
16951 public interface OnKeyListener {
16952 /**
Jean Chalard405bc512012-05-29 19:12:34 +090016953 * Called when a hardware key is dispatched to a view. This allows listeners to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016954 * get a chance to respond before the target view.
Jean Chalard405bc512012-05-29 19:12:34 +090016955 * <p>Key presses in software keyboards will generally NOT trigger this method,
16956 * although some may elect to do so in some situations. Do not assume a
16957 * software input method has to be key-based; even if it is, it may use key presses
16958 * in a different way than you expect, so there is no way to reliably catch soft
16959 * input key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016960 *
16961 * @param v The view the key has been dispatched to.
16962 * @param keyCode The code for the physical key that was pressed
16963 * @param event The KeyEvent object containing full information about
16964 * the event.
16965 * @return True if the listener has consumed the event, false otherwise.
16966 */
16967 boolean onKey(View v, int keyCode, KeyEvent event);
16968 }
16969
16970 /**
16971 * Interface definition for a callback to be invoked when a touch event is
16972 * dispatched to this view. The callback will be invoked before the touch
16973 * event is given to the view.
16974 */
16975 public interface OnTouchListener {
16976 /**
16977 * Called when a touch event is dispatched to a view. This allows listeners to
16978 * get a chance to respond before the target view.
16979 *
16980 * @param v The view the touch event has been dispatched to.
16981 * @param event The MotionEvent object containing full information about
16982 * the event.
16983 * @return True if the listener has consumed the event, false otherwise.
16984 */
16985 boolean onTouch(View v, MotionEvent event);
16986 }
16987
16988 /**
Jeff Brown10b62902011-06-20 16:40:37 -070016989 * Interface definition for a callback to be invoked when a hover event is
16990 * dispatched to this view. The callback will be invoked before the hover
16991 * event is given to the view.
16992 */
16993 public interface OnHoverListener {
16994 /**
16995 * Called when a hover event is dispatched to a view. This allows listeners to
16996 * get a chance to respond before the target view.
16997 *
16998 * @param v The view the hover event has been dispatched to.
16999 * @param event The MotionEvent object containing full information about
17000 * the event.
17001 * @return True if the listener has consumed the event, false otherwise.
17002 */
17003 boolean onHover(View v, MotionEvent event);
17004 }
17005
17006 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080017007 * Interface definition for a callback to be invoked when a generic motion event is
17008 * dispatched to this view. The callback will be invoked before the generic motion
17009 * event is given to the view.
17010 */
17011 public interface OnGenericMotionListener {
17012 /**
17013 * Called when a generic motion event is dispatched to a view. This allows listeners to
17014 * get a chance to respond before the target view.
17015 *
17016 * @param v The view the generic motion event has been dispatched to.
17017 * @param event The MotionEvent object containing full information about
17018 * the event.
17019 * @return True if the listener has consumed the event, false otherwise.
17020 */
17021 boolean onGenericMotion(View v, MotionEvent event);
17022 }
17023
17024 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017025 * Interface definition for a callback to be invoked when a view has been clicked and held.
17026 */
17027 public interface OnLongClickListener {
17028 /**
17029 * Called when a view has been clicked and held.
17030 *
17031 * @param v The view that was clicked and held.
17032 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080017033 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017034 */
17035 boolean onLongClick(View v);
17036 }
17037
17038 /**
Chris Tate32affef2010-10-18 15:29:21 -070017039 * Interface definition for a callback to be invoked when a drag is being dispatched
17040 * to this view. The callback will be invoked before the hosting view's own
17041 * onDrag(event) method. If the listener wants to fall back to the hosting view's
17042 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070017043 *
17044 * <div class="special reference">
17045 * <h3>Developer Guides</h3>
17046 * <p>For a guide to implementing drag and drop features, read the
17047 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
17048 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070017049 */
17050 public interface OnDragListener {
17051 /**
17052 * Called when a drag event is dispatched to a view. This allows listeners
17053 * to get a chance to override base View behavior.
17054 *
Joe Malin32736f02011-01-19 16:14:20 -080017055 * @param v The View that received the drag event.
17056 * @param event The {@link android.view.DragEvent} object for the drag event.
17057 * @return {@code true} if the drag event was handled successfully, or {@code false}
17058 * if the drag event was not handled. Note that {@code false} will trigger the View
17059 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070017060 */
17061 boolean onDrag(View v, DragEvent event);
17062 }
17063
17064 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017065 * Interface definition for a callback to be invoked when the focus state of
17066 * a view changed.
17067 */
17068 public interface OnFocusChangeListener {
17069 /**
17070 * Called when the focus state of a view has changed.
17071 *
17072 * @param v The view whose state has changed.
17073 * @param hasFocus The new focus state of v.
17074 */
17075 void onFocusChange(View v, boolean hasFocus);
17076 }
17077
17078 /**
17079 * Interface definition for a callback to be invoked when a view is clicked.
17080 */
17081 public interface OnClickListener {
17082 /**
17083 * Called when a view has been clicked.
17084 *
17085 * @param v The view that was clicked.
17086 */
17087 void onClick(View v);
17088 }
17089
17090 /**
17091 * Interface definition for a callback to be invoked when the context menu
17092 * for this view is being built.
17093 */
17094 public interface OnCreateContextMenuListener {
17095 /**
17096 * Called when the context menu for this view is being built. It is not
17097 * safe to hold onto the menu after this method returns.
17098 *
17099 * @param menu The context menu that is being built
17100 * @param v The view for which the context menu is being built
17101 * @param menuInfo Extra information about the item for which the
17102 * context menu should be shown. This information will vary
17103 * depending on the class of v.
17104 */
17105 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
17106 }
17107
Joe Onorato664644d2011-01-23 17:53:23 -080017108 /**
17109 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017110 * visibility. This reports <strong>global</strong> changes to the system UI
Dianne Hackborncf675782012-05-10 15:07:24 -070017111 * state, not what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080017112 *
Philip Milne6c8ea062012-04-03 17:38:43 -070017113 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080017114 */
17115 public interface OnSystemUiVisibilityChangeListener {
17116 /**
17117 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070017118 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080017119 *
Dianne Hackborncf675782012-05-10 15:07:24 -070017120 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
17121 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, and {@link #SYSTEM_UI_FLAG_FULLSCREEN}.
17122 * This tells you the <strong>global</strong> state of these UI visibility
17123 * flags, not what your app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080017124 */
17125 public void onSystemUiVisibilityChange(int visibility);
17126 }
17127
Adam Powell4afd62b2011-02-18 15:02:18 -080017128 /**
17129 * Interface definition for a callback to be invoked when this view is attached
17130 * or detached from its window.
17131 */
17132 public interface OnAttachStateChangeListener {
17133 /**
17134 * Called when the view is attached to a window.
17135 * @param v The view that was attached
17136 */
17137 public void onViewAttachedToWindow(View v);
17138 /**
17139 * Called when the view is detached from a window.
17140 * @param v The view that was detached
17141 */
17142 public void onViewDetachedFromWindow(View v);
17143 }
17144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017145 private final class UnsetPressedState implements Runnable {
17146 public void run() {
17147 setPressed(false);
17148 }
17149 }
17150
17151 /**
17152 * Base class for derived classes that want to save and restore their own
17153 * state in {@link android.view.View#onSaveInstanceState()}.
17154 */
17155 public static class BaseSavedState extends AbsSavedState {
17156 /**
17157 * Constructor used when reading from a parcel. Reads the state of the superclass.
17158 *
17159 * @param source
17160 */
17161 public BaseSavedState(Parcel source) {
17162 super(source);
17163 }
17164
17165 /**
17166 * Constructor called by derived classes when creating their SavedState objects
17167 *
17168 * @param superState The state of the superclass of this view
17169 */
17170 public BaseSavedState(Parcelable superState) {
17171 super(superState);
17172 }
17173
17174 public static final Parcelable.Creator<BaseSavedState> CREATOR =
17175 new Parcelable.Creator<BaseSavedState>() {
17176 public BaseSavedState createFromParcel(Parcel in) {
17177 return new BaseSavedState(in);
17178 }
17179
17180 public BaseSavedState[] newArray(int size) {
17181 return new BaseSavedState[size];
17182 }
17183 };
17184 }
17185
17186 /**
17187 * A set of information given to a view when it is attached to its parent
17188 * window.
17189 */
17190 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017191 interface Callbacks {
17192 void playSoundEffect(int effectId);
17193 boolean performHapticFeedback(int effectId, boolean always);
17194 }
17195
17196 /**
17197 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
17198 * to a Handler. This class contains the target (View) to invalidate and
17199 * the coordinates of the dirty rectangle.
17200 *
17201 * For performance purposes, this class also implements a pool of up to
17202 * POOL_LIMIT objects that get reused. This reduces memory allocations
17203 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017204 */
Romain Guyd928d682009-03-31 17:52:16 -070017205 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017206 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070017207 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
17208 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070017209 public InvalidateInfo newInstance() {
17210 return new InvalidateInfo();
17211 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017212
Romain Guyd928d682009-03-31 17:52:16 -070017213 public void onAcquired(InvalidateInfo element) {
17214 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017215
Romain Guyd928d682009-03-31 17:52:16 -070017216 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070017217 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070017218 }
17219 }, POOL_LIMIT)
17220 );
17221
17222 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017223 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017224
17225 View target;
17226
17227 int left;
17228 int top;
17229 int right;
17230 int bottom;
17231
Romain Guyd928d682009-03-31 17:52:16 -070017232 public void setNextPoolable(InvalidateInfo element) {
17233 mNext = element;
17234 }
17235
17236 public InvalidateInfo getNextPoolable() {
17237 return mNext;
17238 }
17239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017240 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070017241 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017242 }
17243
17244 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070017245 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017246 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017247
17248 public boolean isPooled() {
17249 return mIsPooled;
17250 }
17251
17252 public void setPooled(boolean isPooled) {
17253 mIsPooled = isPooled;
17254 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017255 }
17256
17257 final IWindowSession mSession;
17258
17259 final IWindow mWindow;
17260
17261 final IBinder mWindowToken;
17262
17263 final Callbacks mRootCallbacks;
17264
Romain Guy59a12ca2011-06-09 17:48:21 -070017265 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080017266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017267 /**
17268 * The top view of the hierarchy.
17269 */
17270 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070017271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017272 IBinder mPanelParentWindowToken;
17273 Surface mSurface;
17274
Romain Guyb051e892010-09-28 19:09:36 -070017275 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080017276 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070017277 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080017278
Romain Guy7e4e5612012-03-05 14:37:29 -080017279 boolean mScreenOn;
17280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017281 /**
Romain Guy8506ab42009-06-11 17:35:47 -070017282 * Scale factor used by the compatibility mode
17283 */
17284 float mApplicationScale;
17285
17286 /**
17287 * Indicates whether the application is in compatibility mode
17288 */
17289 boolean mScalingRequired;
17290
17291 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017292 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080017293 */
17294 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080017295
Dianne Hackborn63042d62011-01-26 18:56:29 -080017296 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017297 * Left position of this view's window
17298 */
17299 int mWindowLeft;
17300
17301 /**
17302 * Top position of this view's window
17303 */
17304 int mWindowTop;
17305
17306 /**
Adam Powell26153a32010-11-08 15:22:27 -080017307 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070017308 */
Adam Powell26153a32010-11-08 15:22:27 -080017309 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070017310
17311 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017312 * For windows that are full-screen but using insets to layout inside
17313 * of the screen decorations, these are the current insets for the
17314 * content of the window.
17315 */
17316 final Rect mContentInsets = new Rect();
17317
17318 /**
17319 * For windows that are full-screen but using insets to layout inside
17320 * of the screen decorations, these are the current insets for the
17321 * actual visible parts of the window.
17322 */
17323 final Rect mVisibleInsets = new Rect();
17324
17325 /**
17326 * The internal insets given by this window. This value is
17327 * supplied by the client (through
17328 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
17329 * be given to the window manager when changed to be used in laying
17330 * out windows behind it.
17331 */
17332 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
17333 = new ViewTreeObserver.InternalInsetsInfo();
17334
17335 /**
17336 * All views in the window's hierarchy that serve as scroll containers,
17337 * used to determine if the window can be resized or must be panned
17338 * to adjust for a soft input area.
17339 */
17340 final ArrayList<View> mScrollContainers = new ArrayList<View>();
17341
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070017342 final KeyEvent.DispatcherState mKeyDispatchState
17343 = new KeyEvent.DispatcherState();
17344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017345 /**
17346 * Indicates whether the view's window currently has the focus.
17347 */
17348 boolean mHasWindowFocus;
17349
17350 /**
17351 * The current visibility of the window.
17352 */
17353 int mWindowVisibility;
17354
17355 /**
17356 * Indicates the time at which drawing started to occur.
17357 */
17358 long mDrawingTime;
17359
17360 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070017361 * Indicates whether or not ignoring the DIRTY_MASK flags.
17362 */
17363 boolean mIgnoreDirtyState;
17364
17365 /**
Romain Guy02ccac62011-06-24 13:20:23 -070017366 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
17367 * to avoid clearing that flag prematurely.
17368 */
17369 boolean mSetIgnoreDirtyState = false;
17370
17371 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017372 * Indicates whether the view's window is currently in touch mode.
17373 */
17374 boolean mInTouchMode;
17375
17376 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017377 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017378 * the next time it performs a traversal
17379 */
17380 boolean mRecomputeGlobalAttributes;
17381
17382 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017383 * Always report new attributes at next traversal.
17384 */
17385 boolean mForceReportNewAttributes;
17386
17387 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017388 * Set during a traveral if any views want to keep the screen on.
17389 */
17390 boolean mKeepScreenOn;
17391
17392 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017393 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
17394 */
17395 int mSystemUiVisibility;
17396
17397 /**
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070017398 * Hack to force certain system UI visibility flags to be cleared.
17399 */
17400 int mDisabledSystemUiVisibility;
17401
17402 /**
Dianne Hackborncf675782012-05-10 15:07:24 -070017403 * Last global system UI visibility reported by the window manager.
17404 */
17405 int mGlobalSystemUiVisibility;
17406
17407 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017408 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
17409 * attached.
17410 */
17411 boolean mHasSystemUiListeners;
17412
17413 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017414 * Set if the visibility of any views has changed.
17415 */
17416 boolean mViewVisibilityChanged;
17417
17418 /**
17419 * Set to true if a view has been scrolled.
17420 */
17421 boolean mViewScrollChanged;
17422
17423 /**
17424 * Global to the view hierarchy used as a temporary for dealing with
17425 * x/y points in the transparent region computations.
17426 */
17427 final int[] mTransparentLocation = new int[2];
17428
17429 /**
17430 * Global to the view hierarchy used as a temporary for dealing with
17431 * x/y points in the ViewGroup.invalidateChild implementation.
17432 */
17433 final int[] mInvalidateChildLocation = new int[2];
17434
Chet Haasec3aa3612010-06-17 08:50:37 -070017435
17436 /**
17437 * Global to the view hierarchy used as a temporary for dealing with
17438 * x/y location when view is transformed.
17439 */
17440 final float[] mTmpTransformLocation = new float[2];
17441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017442 /**
17443 * The view tree observer used to dispatch global events like
17444 * layout, pre-draw, touch mode change, etc.
17445 */
17446 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
17447
17448 /**
17449 * A Canvas used by the view hierarchy to perform bitmap caching.
17450 */
17451 Canvas mCanvas;
17452
17453 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080017454 * The view root impl.
17455 */
17456 final ViewRootImpl mViewRootImpl;
17457
17458 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070017459 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017460 * handler can be used to pump events in the UI events queue.
17461 */
17462 final Handler mHandler;
17463
17464 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017465 * Temporary for use in computing invalidate rectangles while
17466 * calling up the hierarchy.
17467 */
17468 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070017469
17470 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070017471 * Temporary for use in computing hit areas with transformed views
17472 */
17473 final RectF mTmpTransformRect = new RectF();
17474
17475 /**
Chet Haase599913d2012-07-23 16:22:05 -070017476 * Temporary for use in transforming invalidation rect
17477 */
17478 final Matrix mTmpMatrix = new Matrix();
17479
17480 /**
17481 * Temporary for use in transforming invalidation rect
17482 */
17483 final Transformation mTmpTransformation = new Transformation();
17484
17485 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070017486 * Temporary list for use in collecting focusable descendents of a view.
17487 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070017488 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070017489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017490 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017491 * The id of the window for accessibility purposes.
17492 */
17493 int mAccessibilityWindowId = View.NO_ID;
17494
17495 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -070017496 * Whether to ingore not exposed for accessibility Views when
17497 * reporting the view tree to accessibility services.
17498 */
17499 boolean mIncludeNotImportantViews;
17500
17501 /**
17502 * The drawable for highlighting accessibility focus.
17503 */
17504 Drawable mAccessibilityFocusDrawable;
17505
17506 /**
Philip Milne10ca24a2012-04-23 15:38:27 -070017507 * Show where the margins, bounds and layout bounds are for each view.
17508 */
Dianne Hackborna53de062012-05-08 18:53:51 -070017509 boolean mDebugLayout = SystemProperties.getBoolean(DEBUG_LAYOUT_PROPERTY, false);
Philip Milne10ca24a2012-04-23 15:38:27 -070017510
17511 /**
Romain Guyab4c4f4f2012-05-06 13:11:24 -070017512 * Point used to compute visible regions.
17513 */
17514 final Point mPoint = new Point();
17515
17516 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017517 * Creates a new set of attachment information with the specified
17518 * events handler and thread.
17519 *
17520 * @param handler the events handler the view must use
17521 */
17522 AttachInfo(IWindowSession session, IWindow window,
Jeff Browna175a5b2012-02-15 19:18:31 -080017523 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017524 mSession = session;
17525 mWindow = window;
17526 mWindowToken = window.asBinder();
Jeff Browna175a5b2012-02-15 19:18:31 -080017527 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017528 mHandler = handler;
17529 mRootCallbacks = effectPlayer;
17530 }
17531 }
17532
17533 /**
17534 * <p>ScrollabilityCache holds various fields used by a View when scrolling
17535 * is supported. This avoids keeping too many unused fields in most
17536 * instances of View.</p>
17537 */
Mike Cleronf116bf82009-09-27 19:14:12 -070017538 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080017539
Mike Cleronf116bf82009-09-27 19:14:12 -070017540 /**
17541 * Scrollbars are not visible
17542 */
17543 public static final int OFF = 0;
17544
17545 /**
17546 * Scrollbars are visible
17547 */
17548 public static final int ON = 1;
17549
17550 /**
17551 * Scrollbars are fading away
17552 */
17553 public static final int FADING = 2;
17554
17555 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080017556
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017557 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070017558 public int scrollBarDefaultDelayBeforeFade;
17559 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017560
17561 public int scrollBarSize;
17562 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070017563 public float[] interpolatorValues;
17564 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017565
17566 public final Paint paint;
17567 public final Matrix matrix;
17568 public Shader shader;
17569
Mike Cleronf116bf82009-09-27 19:14:12 -070017570 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
17571
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017572 private static final float[] OPAQUE = { 255 };
17573 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080017574
Mike Cleronf116bf82009-09-27 19:14:12 -070017575 /**
17576 * When fading should start. This time moves into the future every time
17577 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
17578 */
17579 public long fadeStartTime;
17580
17581
17582 /**
17583 * The current state of the scrollbars: ON, OFF, or FADING
17584 */
17585 public int state = OFF;
17586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017587 private int mLastColor;
17588
Mike Cleronf116bf82009-09-27 19:14:12 -070017589 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017590 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
17591 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070017592 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
17593 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017594
17595 paint = new Paint();
17596 matrix = new Matrix();
17597 // use use a height of 1, and then wack the matrix each time we
17598 // actually use it.
17599 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017601 paint.setShader(shader);
17602 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070017603 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017604 }
Romain Guy8506ab42009-06-11 17:35:47 -070017605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017606 public void setFadeColor(int color) {
17607 if (color != 0 && color != mLastColor) {
17608 mLastColor = color;
17609 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070017610
Romain Guye55e1a72009-08-27 10:42:26 -070017611 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
17612 color & 0x00FFFFFF, 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 // Restore the default transfer mode (src_over)
17616 paint.setXfermode(null);
17617 }
17618 }
Joe Malin32736f02011-01-19 16:14:20 -080017619
Mike Cleronf116bf82009-09-27 19:14:12 -070017620 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070017621 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070017622 if (now >= fadeStartTime) {
17623
17624 // the animation fades the scrollbars out by changing
17625 // the opacity (alpha) from fully opaque to fully
17626 // transparent
17627 int nextFrame = (int) now;
17628 int framesCount = 0;
17629
17630 Interpolator interpolator = scrollBarInterpolator;
17631
17632 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017633 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070017634
17635 // End transparent
17636 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017637 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070017638
17639 state = FADING;
17640
17641 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080017642 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070017643 }
17644 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070017645 }
Mike Cleronf116bf82009-09-27 19:14:12 -070017646
Svetoslav Ganova0156172011-06-26 17:55:44 -070017647 /**
17648 * Resuable callback for sending
17649 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
17650 */
17651 private class SendViewScrolledAccessibilityEvent implements Runnable {
17652 public volatile boolean mIsPending;
17653
17654 public void run() {
17655 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
17656 mIsPending = false;
17657 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017658 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017659
17660 /**
17661 * <p>
17662 * This class represents a delegate that can be registered in a {@link View}
17663 * to enhance accessibility support via composition rather via inheritance.
17664 * It is specifically targeted to widget developers that extend basic View
17665 * classes i.e. classes in package android.view, that would like their
17666 * applications to be backwards compatible.
17667 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080017668 * <div class="special reference">
17669 * <h3>Developer Guides</h3>
17670 * <p>For more information about making applications accessible, read the
17671 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
17672 * developer guide.</p>
17673 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017674 * <p>
17675 * A scenario in which a developer would like to use an accessibility delegate
17676 * is overriding a method introduced in a later API version then the minimal API
17677 * version supported by the application. For example, the method
17678 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
17679 * in API version 4 when the accessibility APIs were first introduced. If a
17680 * developer would like his application to run on API version 4 devices (assuming
17681 * all other APIs used by the application are version 4 or lower) and take advantage
17682 * of this method, instead of overriding the method which would break the application's
17683 * backwards compatibility, he can override the corresponding method in this
17684 * delegate and register the delegate in the target View if the API version of
17685 * the system is high enough i.e. the API version is same or higher to the API
17686 * version that introduced
17687 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
17688 * </p>
17689 * <p>
17690 * Here is an example implementation:
17691 * </p>
17692 * <code><pre><p>
17693 * if (Build.VERSION.SDK_INT >= 14) {
17694 * // If the API version is equal of higher than the version in
17695 * // which onInitializeAccessibilityNodeInfo was introduced we
17696 * // register a delegate with a customized implementation.
17697 * View view = findViewById(R.id.view_id);
17698 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
17699 * public void onInitializeAccessibilityNodeInfo(View host,
17700 * AccessibilityNodeInfo info) {
17701 * // Let the default implementation populate the info.
17702 * super.onInitializeAccessibilityNodeInfo(host, info);
17703 * // Set some other information.
17704 * info.setEnabled(host.isEnabled());
17705 * }
17706 * });
17707 * }
17708 * </code></pre></p>
17709 * <p>
17710 * This delegate contains methods that correspond to the accessibility methods
17711 * in View. If a delegate has been specified the implementation in View hands
17712 * off handling to the corresponding method in this delegate. The default
17713 * implementation the delegate methods behaves exactly as the corresponding
17714 * method in View for the case of no accessibility delegate been set. Hence,
17715 * to customize the behavior of a View method, clients can override only the
17716 * corresponding delegate method without altering the behavior of the rest
17717 * accessibility related methods of the host view.
17718 * </p>
17719 */
17720 public static class AccessibilityDelegate {
17721
17722 /**
17723 * Sends an accessibility event of the given type. If accessibility is not
17724 * enabled this method has no effect.
17725 * <p>
17726 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
17727 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
17728 * been set.
17729 * </p>
17730 *
17731 * @param host The View hosting the delegate.
17732 * @param eventType The type of the event to send.
17733 *
17734 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
17735 */
17736 public void sendAccessibilityEvent(View host, int eventType) {
17737 host.sendAccessibilityEventInternal(eventType);
17738 }
17739
17740 /**
alanv8eeefef2012-05-07 16:57:53 -070017741 * Performs the specified accessibility action on the view. For
17742 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
17743 * <p>
17744 * The default implementation behaves as
17745 * {@link View#performAccessibilityAction(int, Bundle)
17746 * View#performAccessibilityAction(int, Bundle)} for the case of
17747 * no accessibility delegate been set.
17748 * </p>
17749 *
17750 * @param action The action to perform.
17751 * @return Whether the action was performed.
17752 *
17753 * @see View#performAccessibilityAction(int, Bundle)
17754 * View#performAccessibilityAction(int, Bundle)
17755 */
17756 public boolean performAccessibilityAction(View host, int action, Bundle args) {
17757 return host.performAccessibilityActionInternal(action, args);
17758 }
17759
17760 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017761 * Sends an accessibility event. This method behaves exactly as
17762 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
17763 * empty {@link AccessibilityEvent} and does not perform a check whether
17764 * accessibility is enabled.
17765 * <p>
17766 * The default implementation behaves as
17767 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17768 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
17769 * the case of no accessibility delegate been set.
17770 * </p>
17771 *
17772 * @param host The View hosting the delegate.
17773 * @param event The event to send.
17774 *
17775 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17776 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17777 */
17778 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
17779 host.sendAccessibilityEventUncheckedInternal(event);
17780 }
17781
17782 /**
17783 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
17784 * to its children for adding their text content to the event.
17785 * <p>
17786 * The default implementation behaves as
17787 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17788 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
17789 * the case of no accessibility delegate been set.
17790 * </p>
17791 *
17792 * @param host The View hosting the delegate.
17793 * @param event The event.
17794 * @return True if the event population was completed.
17795 *
17796 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17797 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17798 */
17799 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17800 return host.dispatchPopulateAccessibilityEventInternal(event);
17801 }
17802
17803 /**
17804 * Gives a chance to the host View to populate the accessibility event with its
17805 * text content.
17806 * <p>
17807 * The default implementation behaves as
17808 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
17809 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
17810 * the case of no accessibility delegate been set.
17811 * </p>
17812 *
17813 * @param host The View hosting the delegate.
17814 * @param event The accessibility event which to populate.
17815 *
17816 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
17817 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
17818 */
17819 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17820 host.onPopulateAccessibilityEventInternal(event);
17821 }
17822
17823 /**
17824 * Initializes an {@link AccessibilityEvent} with information about the
17825 * the host View which is the event source.
17826 * <p>
17827 * The default implementation behaves as
17828 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
17829 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
17830 * the case of no accessibility delegate been set.
17831 * </p>
17832 *
17833 * @param host The View hosting the delegate.
17834 * @param event The event to initialize.
17835 *
17836 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
17837 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
17838 */
17839 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
17840 host.onInitializeAccessibilityEventInternal(event);
17841 }
17842
17843 /**
17844 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
17845 * <p>
17846 * The default implementation behaves as
17847 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17848 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
17849 * the case of no accessibility delegate been set.
17850 * </p>
17851 *
17852 * @param host The View hosting the delegate.
17853 * @param info The instance to initialize.
17854 *
17855 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17856 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17857 */
17858 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
17859 host.onInitializeAccessibilityNodeInfoInternal(info);
17860 }
17861
17862 /**
17863 * Called when a child of the host View has requested sending an
17864 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
17865 * to augment the event.
17866 * <p>
17867 * The default implementation behaves as
17868 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17869 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
17870 * the case of no accessibility delegate been set.
17871 * </p>
17872 *
17873 * @param host The View hosting the delegate.
17874 * @param child The child which requests sending the event.
17875 * @param event The event to be sent.
17876 * @return True if the event should be sent
17877 *
17878 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17879 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17880 */
17881 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
17882 AccessibilityEvent event) {
17883 return host.onRequestSendAccessibilityEventInternal(child, event);
17884 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070017885
17886 /**
17887 * Gets the provider for managing a virtual view hierarchy rooted at this View
17888 * and reported to {@link android.accessibilityservice.AccessibilityService}s
17889 * that explore the window content.
17890 * <p>
17891 * The default implementation behaves as
17892 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
17893 * the case of no accessibility delegate been set.
17894 * </p>
17895 *
17896 * @return The provider.
17897 *
17898 * @see AccessibilityNodeProvider
17899 */
17900 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
17901 return null;
17902 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017903 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017904}