blob: 10b8ff58a4ceb83292f149d172e8cf2239cf6dcd [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view;
18
Christopher Tatea53146c2010-09-07 11:57:52 -070019import android.content.ClipData;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.Context;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080021import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.res.Resources;
23import android.content.res.TypedArray;
24import android.graphics.Bitmap;
Adam Powell2b342f02010-08-18 18:14:13 -070025import android.graphics.Camera;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.graphics.Canvas;
Philip Milne1557fd72012-04-04 23:41:34 -070027import android.graphics.Insets;
Mike Cleronf116bf82009-09-27 19:14:12 -070028import android.graphics.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.graphics.LinearGradient;
30import android.graphics.Matrix;
31import android.graphics.Paint;
32import android.graphics.PixelFormat;
svetoslavganov75986cf2009-05-14 22:28:01 -070033import android.graphics.Point;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.graphics.PorterDuff;
35import android.graphics.PorterDuffXfermode;
36import android.graphics.Rect;
Adam Powell6e346362010-07-23 10:18:23 -070037import android.graphics.RectF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.graphics.Region;
39import android.graphics.Shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.graphics.drawable.ColorDrawable;
41import android.graphics.drawable.Drawable;
Svetoslav Ganovaa780c12012-04-19 23:01:39 -070042import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.os.Handler;
44import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.os.Parcel;
46import android.os.Parcelable;
47import android.os.RemoteException;
48import android.os.SystemClock;
Philip Milne10ca24a2012-04-23 15:38:27 -070049import android.os.SystemProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.util.AttributeSet;
Doug Feltcb3791202011-07-07 11:57:48 -070051import android.util.FloatProperty;
52import android.util.LocaleUtil;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070054import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070055import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070056import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.util.Pools;
Doug Feltcb3791202011-07-07 11:57:48 -070058import android.util.Property;
svetoslavganov75986cf2009-05-14 22:28:01 -070059import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080060import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.view.ContextMenu.ContextMenuInfo;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -070062import android.view.AccessibilityIterators.TextSegmentIterator;
63import android.view.AccessibilityIterators.CharacterTextSegmentIterator;
64import android.view.AccessibilityIterators.WordTextSegmentIterator;
65import android.view.AccessibilityIterators.ParagraphTextSegmentIterator;
svetoslavganov75986cf2009-05-14 22:28:01 -070066import android.view.accessibility.AccessibilityEvent;
67import android.view.accessibility.AccessibilityEventSource;
68import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070069import android.view.accessibility.AccessibilityNodeInfo;
Svetoslav Ganov02107852011-10-03 17:06:56 -070070import android.view.accessibility.AccessibilityNodeProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070072import android.view.animation.AnimationUtils;
Chet Haase64a48c12012-02-13 16:33:29 -080073import android.view.animation.Transformation;
svetoslavganov75986cf2009-05-14 22:28:01 -070074import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.view.inputmethod.InputConnection;
76import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.widget.ScrollBarDrawable;
78
Romain Guy1ef3fdb2011-09-09 15:30:30 -070079import static android.os.Build.VERSION_CODES.*;
Philip Milne6c8ea062012-04-03 17:38:43 -070080import static java.lang.Math.max;
Romain Guy1ef3fdb2011-09-09 15:30:30 -070081
Doug Feltcb3791202011-07-07 11:57:48 -070082import com.android.internal.R;
83import com.android.internal.util.Predicate;
84import com.android.internal.view.menu.MenuBuilder;
85
Christopher Tatea0374192010-10-05 13:06:41 -070086import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070087import java.lang.reflect.InvocationTargetException;
88import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089import java.util.ArrayList;
90import java.util.Arrays;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070091import java.util.Locale;
Adam Powell4afd62b2011-02-18 15:02:18 -080092import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093
94/**
95 * <p>
96 * This class represents the basic building block for user interface components. A View
97 * occupies a rectangular area on the screen and is responsible for drawing and
98 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070099 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
101 * are invisible containers that hold other Views (or other ViewGroups) and define
102 * their layout properties.
103 * </p>
104 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700105 * <div class="special reference">
106 * <h3>Developer Guides</h3>
107 * <p>For information about using this class to develop your application's user interface,
108 * read the <a href="{@docRoot}guide/topics/ui/index.html">User Interface</a> developer guide.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700110 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 * <a name="Using"></a>
112 * <h3>Using Views</h3>
113 * <p>
114 * All of the views in a window are arranged in a single tree. You can add views
115 * either from code or by specifying a tree of views in one or more XML layout
116 * files. There are many specialized subclasses of views that act as controls or
117 * are capable of displaying text, images, or other content.
118 * </p>
119 * <p>
120 * Once you have created a tree of views, there are typically a few types of
121 * common operations you may wish to perform:
122 * <ul>
123 * <li><strong>Set properties:</strong> for example setting the text of a
124 * {@link android.widget.TextView}. The available properties and the methods
125 * that set them will vary among the different subclasses of views. Note that
126 * properties that are known at build time can be set in the XML layout
127 * files.</li>
128 * <li><strong>Set focus:</strong> The framework will handled moving focus in
129 * response to user input. To force focus to a specific view, call
130 * {@link #requestFocus}.</li>
131 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
132 * that will be notified when something interesting happens to the view. For
133 * example, all views will let you set a listener to be notified when the view
134 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700135 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
Philip Milne6c8ea062012-04-03 17:38:43 -0700136 * Other view subclasses offer more specialized listeners. For example, a Button
Romain Guy5c22a8c2011-05-13 11:48:45 -0700137 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700139 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 * </ul>
141 * </p>
142 * <p><em>
143 * Note: The Android framework is responsible for measuring, laying out and
144 * drawing views. You should not call methods that perform these actions on
145 * views yourself unless you are actually implementing a
146 * {@link android.view.ViewGroup}.
147 * </em></p>
148 *
149 * <a name="Lifecycle"></a>
150 * <h3>Implementing a Custom View</h3>
151 *
152 * <p>
153 * To implement a custom view, you will usually begin by providing overrides for
154 * some of the standard methods that the framework calls on all views. You do
155 * not need to override all of these methods. In fact, you can start by just
156 * overriding {@link #onDraw(android.graphics.Canvas)}.
157 * <table border="2" width="85%" align="center" cellpadding="5">
158 * <thead>
159 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
160 * </thead>
161 *
162 * <tbody>
163 * <tr>
164 * <td rowspan="2">Creation</td>
165 * <td>Constructors</td>
166 * <td>There is a form of the constructor that are called when the view
167 * is created from code and a form that is called when the view is
168 * inflated from a layout file. The second form should parse and apply
169 * any attributes defined in the layout file.
170 * </td>
171 * </tr>
172 * <tr>
173 * <td><code>{@link #onFinishInflate()}</code></td>
174 * <td>Called after a view and all of its children has been inflated
175 * from XML.</td>
176 * </tr>
177 *
178 * <tr>
179 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700180 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 * <td>Called to determine the size requirements for this view and all
182 * of its children.
183 * </td>
184 * </tr>
185 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700186 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 * <td>Called when this view should assign a size and position to all
188 * of its children.
189 * </td>
190 * </tr>
191 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700192 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 * <td>Called when the size of this view has changed.
194 * </td>
195 * </tr>
196 *
197 * <tr>
198 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700199 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 * <td>Called when the view should render its content.
201 * </td>
202 * </tr>
203 *
204 * <tr>
205 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700206 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 * <td>Called when a new key event occurs.
208 * </td>
209 * </tr>
210 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700211 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 * <td>Called when a key up event occurs.
213 * </td>
214 * </tr>
215 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700216 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 * <td>Called when a trackball motion event occurs.
218 * </td>
219 * </tr>
220 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700221 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 * <td>Called when a touch screen motion event occurs.
223 * </td>
224 * </tr>
225 *
226 * <tr>
227 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700228 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 * <td>Called when the view gains or loses focus.
230 * </td>
231 * </tr>
232 *
233 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700234 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 * <td>Called when the window containing the view gains or loses focus.
236 * </td>
237 * </tr>
238 *
239 * <tr>
240 * <td rowspan="3">Attaching</td>
241 * <td><code>{@link #onAttachedToWindow()}</code></td>
242 * <td>Called when the view is attached to a window.
243 * </td>
244 * </tr>
245 *
246 * <tr>
247 * <td><code>{@link #onDetachedFromWindow}</code></td>
248 * <td>Called when the view is detached from its window.
249 * </td>
250 * </tr>
251 *
252 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700253 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 * <td>Called when the visibility of the window containing the view
255 * has changed.
256 * </td>
257 * </tr>
258 * </tbody>
259 *
260 * </table>
261 * </p>
262 *
263 * <a name="IDs"></a>
264 * <h3>IDs</h3>
265 * Views may have an integer id associated with them. These ids are typically
266 * assigned in the layout XML files, and are used to find specific views within
267 * the view tree. A common pattern is to:
268 * <ul>
269 * <li>Define a Button in the layout file and assign it a unique ID.
270 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700271 * &lt;Button
272 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 * android:layout_width="wrap_content"
274 * android:layout_height="wrap_content"
275 * android:text="@string/my_button_text"/&gt;
276 * </pre></li>
277 * <li>From the onCreate method of an Activity, find the Button
278 * <pre class="prettyprint">
279 * Button myButton = (Button) findViewById(R.id.my_button);
280 * </pre></li>
281 * </ul>
282 * <p>
283 * View IDs need not be unique throughout the tree, but it is good practice to
284 * ensure that they are at least unique within the part of the tree you are
285 * searching.
286 * </p>
287 *
288 * <a name="Position"></a>
289 * <h3>Position</h3>
290 * <p>
291 * The geometry of a view is that of a rectangle. A view has a location,
292 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
293 * two dimensions, expressed as a width and a height. The unit for location
294 * and dimensions is the pixel.
295 * </p>
296 *
297 * <p>
298 * It is possible to retrieve the location of a view by invoking the methods
299 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
300 * coordinate of the rectangle representing the view. The latter returns the
301 * top, or Y, coordinate of the rectangle representing the view. These methods
302 * both return the location of the view relative to its parent. For instance,
303 * when getLeft() returns 20, that means the view is located 20 pixels to the
304 * right of the left edge of its direct parent.
305 * </p>
306 *
307 * <p>
308 * In addition, several convenience methods are offered to avoid unnecessary
309 * computations, namely {@link #getRight()} and {@link #getBottom()}.
310 * These methods return the coordinates of the right and bottom edges of the
311 * rectangle representing the view. For instance, calling {@link #getRight()}
312 * is similar to the following computation: <code>getLeft() + getWidth()</code>
313 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
314 * </p>
315 *
316 * <a name="SizePaddingMargins"></a>
317 * <h3>Size, padding and margins</h3>
318 * <p>
319 * The size of a view is expressed with a width and a height. A view actually
320 * possess two pairs of width and height values.
321 * </p>
322 *
323 * <p>
324 * The first pair is known as <em>measured width</em> and
325 * <em>measured height</em>. These dimensions define how big a view wants to be
326 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
327 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
328 * and {@link #getMeasuredHeight()}.
329 * </p>
330 *
331 * <p>
332 * The second pair is simply known as <em>width</em> and <em>height</em>, or
333 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
334 * dimensions define the actual size of the view on screen, at drawing time and
335 * after layout. These values may, but do not have to, be different from the
336 * measured width and height. The width and height can be obtained by calling
337 * {@link #getWidth()} and {@link #getHeight()}.
338 * </p>
339 *
340 * <p>
341 * To measure its dimensions, a view takes into account its padding. The padding
342 * is expressed in pixels for the left, top, right and bottom parts of the view.
343 * Padding can be used to offset the content of the view by a specific amount of
344 * pixels. For instance, a left padding of 2 will push the view's content by
345 * 2 pixels to the right of the left edge. Padding can be set using the
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -0700346 * {@link #setPadding(int, int, int, int)} method and queried by calling
347 * {@link #getPaddingLeft()}, {@link #getPaddingTop()}, {@link #getPaddingRight()},
348 * {@link #getPaddingBottom()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 * </p>
350 *
351 * <p>
352 * Even though a view can define a padding, it does not provide any support for
353 * margins. However, view groups provide such a support. Refer to
354 * {@link android.view.ViewGroup} and
355 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
356 * </p>
357 *
358 * <a name="Layout"></a>
359 * <h3>Layout</h3>
360 * <p>
361 * Layout is a two pass process: a measure pass and a layout pass. The measuring
362 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
363 * of the view tree. Each view pushes dimension specifications down the tree
364 * during the recursion. At the end of the measure pass, every view has stored
365 * its measurements. The second pass happens in
366 * {@link #layout(int,int,int,int)} and is also top-down. During
367 * this pass each parent is responsible for positioning all of its children
368 * using the sizes computed in the measure pass.
369 * </p>
370 *
371 * <p>
372 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
373 * {@link #getMeasuredHeight()} values must be set, along with those for all of
374 * that view's descendants. A view's measured width and measured height values
375 * must respect the constraints imposed by the view's parents. This guarantees
376 * that at the end of the measure pass, all parents accept all of their
377 * children's measurements. A parent view may call measure() more than once on
378 * its children. For example, the parent may measure each child once with
379 * unspecified dimensions to find out how big they want to be, then call
380 * measure() on them again with actual numbers if the sum of all the children's
381 * unconstrained sizes is too big or too small.
382 * </p>
383 *
384 * <p>
385 * The measure pass uses two classes to communicate dimensions. The
386 * {@link MeasureSpec} class is used by views to tell their parents how they
387 * want to be measured and positioned. The base LayoutParams class just
388 * describes how big the view wants to be for both width and height. For each
389 * dimension, it can specify one of:
390 * <ul>
391 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800392 * <li>MATCH_PARENT, which means the view wants to be as big as its parent
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 * (minus padding)
394 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
395 * enclose its content (plus padding).
396 * </ul>
397 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
398 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
399 * an X and Y value.
400 * </p>
401 *
402 * <p>
403 * MeasureSpecs are used to push requirements down the tree from parent to
404 * child. A MeasureSpec can be in one of three modes:
405 * <ul>
406 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
407 * of a child view. For example, a LinearLayout may call measure() on its child
408 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
409 * tall the child view wants to be given a width of 240 pixels.
410 * <li>EXACTLY: This is used by the parent to impose an exact size on the
411 * child. The child must use this size, and guarantee that all of its
412 * descendants will fit within this size.
413 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
414 * child. The child must gurantee that it and all of its descendants will fit
415 * within this size.
416 * </ul>
417 * </p>
418 *
419 * <p>
420 * To intiate a layout, call {@link #requestLayout}. This method is typically
421 * called by a view on itself when it believes that is can no longer fit within
422 * its current bounds.
423 * </p>
424 *
425 * <a name="Drawing"></a>
426 * <h3>Drawing</h3>
427 * <p>
428 * Drawing is handled by walking the tree and rendering each view that
Joe Fernandez558459f2011-10-13 16:47:36 -0700429 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 * this means that parents will draw before (i.e., behind) their children, with
431 * siblings drawn in the order they appear in the tree.
432 * If you set a background drawable for a View, then the View will draw it for you
433 * before calling back to its <code>onDraw()</code> method.
434 * </p>
435 *
436 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700437 * Note that the framework will not draw views that are not in the invalid region.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 * </p>
439 *
440 * <p>
441 * To force a view to draw, call {@link #invalidate()}.
442 * </p>
443 *
444 * <a name="EventHandlingThreading"></a>
445 * <h3>Event Handling and Threading</h3>
446 * <p>
447 * The basic cycle of a view is as follows:
448 * <ol>
449 * <li>An event comes in and is dispatched to the appropriate view. The view
450 * handles the event and notifies any listeners.</li>
451 * <li>If in the course of processing the event, the view's bounds may need
452 * to be changed, the view will call {@link #requestLayout()}.</li>
453 * <li>Similarly, if in the course of processing the event the view's appearance
454 * may need to be changed, the view will call {@link #invalidate()}.</li>
455 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
456 * the framework will take care of measuring, laying out, and drawing the tree
457 * as appropriate.</li>
458 * </ol>
459 * </p>
460 *
461 * <p><em>Note: The entire view tree is single threaded. You must always be on
462 * the UI thread when calling any method on any view.</em>
463 * If you are doing work on other threads and want to update the state of a view
464 * from that thread, you should use a {@link Handler}.
465 * </p>
466 *
467 * <a name="FocusHandling"></a>
468 * <h3>Focus Handling</h3>
469 * <p>
470 * The framework will handle routine focus movement in response to user input.
471 * This includes changing the focus as views are removed or hidden, or as new
472 * views become available. Views indicate their willingness to take focus
473 * through the {@link #isFocusable} method. To change whether a view can take
474 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
475 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
476 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
477 * </p>
478 * <p>
479 * Focus movement is based on an algorithm which finds the nearest neighbor in a
480 * given direction. In rare cases, the default algorithm may not match the
481 * intended behavior of the developer. In these situations, you can provide
482 * explicit overrides by using these XML attributes in the layout file:
483 * <pre>
484 * nextFocusDown
485 * nextFocusLeft
486 * nextFocusRight
487 * nextFocusUp
488 * </pre>
489 * </p>
490 *
491 *
492 * <p>
493 * To get a particular view to take focus, call {@link #requestFocus()}.
494 * </p>
495 *
496 * <a name="TouchMode"></a>
497 * <h3>Touch Mode</h3>
498 * <p>
499 * When a user is navigating a user interface via directional keys such as a D-pad, it is
500 * necessary to give focus to actionable items such as buttons so the user can see
501 * what will take input. If the device has touch capabilities, however, and the user
502 * begins interacting with the interface by touching it, it is no longer necessary to
503 * always highlight, or give focus to, a particular view. This motivates a mode
504 * for interaction named 'touch mode'.
505 * </p>
506 * <p>
507 * For a touch capable device, once the user touches the screen, the device
508 * will enter touch mode. From this point onward, only views for which
509 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
510 * Other views that are touchable, like buttons, will not take focus when touched; they will
511 * only fire the on click listeners.
512 * </p>
513 * <p>
514 * Any time a user hits a directional key, such as a D-pad direction, the view device will
515 * exit touch mode, and find a view to take focus, so that the user may resume interacting
516 * with the user interface without touching the screen again.
517 * </p>
518 * <p>
519 * The touch mode state is maintained across {@link android.app.Activity}s. Call
520 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
521 * </p>
522 *
523 * <a name="Scrolling"></a>
524 * <h3>Scrolling</h3>
525 * <p>
526 * The framework provides basic support for views that wish to internally
527 * scroll their content. This includes keeping track of the X and Y scroll
528 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800529 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700530 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 * </p>
532 *
533 * <a name="Tags"></a>
534 * <h3>Tags</h3>
535 * <p>
536 * Unlike IDs, tags are not used to identify views. Tags are essentially an
537 * extra piece of information that can be associated with a view. They are most
538 * often used as a convenience to store data related to views in the views
539 * themselves rather than by putting them in a separate structure.
540 * </p>
541 *
Chet Haasecb150fe2012-05-03 15:15:05 -0700542 * <a name="Properties"></a>
543 * <h3>Properties</h3>
544 * <p>
545 * The View class exposes an {@link #ALPHA} property, as well as several transform-related
546 * properties, such as {@link #TRANSLATION_X} and {@link #TRANSLATION_Y}. These properties are
547 * available both in the {@link Property} form as well as in similarly-named setter/getter
548 * methods (such as {@link #setAlpha(float)} for {@link #ALPHA}). These properties can
549 * be used to set persistent state associated with these rendering-related properties on the view.
550 * The properties and methods can also be used in conjunction with
551 * {@link android.animation.Animator Animator}-based animations, described more in the
552 * <a href="#Animation">Animation</a> section.
553 * </p>
554 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 * <a name="Animation"></a>
556 * <h3>Animation</h3>
557 * <p>
Chet Haasecb150fe2012-05-03 15:15:05 -0700558 * Starting with Android 3.0, the preferred way of animating views is to use the
559 * {@link android.animation} package APIs. These {@link android.animation.Animator Animator}-based
560 * classes change actual properties of the View object, such as {@link #setAlpha(float) alpha} and
561 * {@link #setTranslationX(float) translationX}. This behavior is contrasted to that of the pre-3.0
562 * {@link android.view.animation.Animation Animation}-based classes, which instead animate only
563 * how the view is drawn on the display. In particular, the {@link ViewPropertyAnimator} class
564 * makes animating these View properties particularly easy and efficient.
565 * </p>
566 * <p>
567 * Alternatively, you can use the pre-3.0 animation classes to animate how Views are rendered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 * You can attach an {@link Animation} object to a view using
569 * {@link #setAnimation(Animation)} or
570 * {@link #startAnimation(Animation)}. The animation can alter the scale,
571 * rotation, translation and alpha of a view over time. If the animation is
572 * attached to a view that has children, the animation will affect the entire
573 * subtree rooted by that node. When an animation is started, the framework will
574 * take care of redrawing the appropriate views until the animation completes.
575 * </p>
576 *
Jeff Brown85a31762010-09-01 17:01:00 -0700577 * <a name="Security"></a>
578 * <h3>Security</h3>
579 * <p>
580 * Sometimes it is essential that an application be able to verify that an action
581 * is being performed with the full knowledge and consent of the user, such as
582 * granting a permission request, making a purchase or clicking on an advertisement.
583 * Unfortunately, a malicious application could try to spoof the user into
584 * performing these actions, unaware, by concealing the intended purpose of the view.
585 * As a remedy, the framework offers a touch filtering mechanism that can be used to
586 * improve the security of views that provide access to sensitive functionality.
587 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700588 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800589 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700590 * will discard touches that are received whenever the view's window is obscured by
591 * another visible window. As a result, the view will not receive touches whenever a
592 * toast, dialog or other window appears above the view's window.
593 * </p><p>
594 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700595 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
596 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700597 * </p>
598 *
Romain Guy171c5922011-01-06 10:04:23 -0800599 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700600 * @attr ref android.R.styleable#View_background
601 * @attr ref android.R.styleable#View_clickable
602 * @attr ref android.R.styleable#View_contentDescription
603 * @attr ref android.R.styleable#View_drawingCacheQuality
604 * @attr ref android.R.styleable#View_duplicateParentState
605 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700606 * @attr ref android.R.styleable#View_requiresFadingEdge
Philip Milne6c8ea062012-04-03 17:38:43 -0700607 * @attr ref android.R.styleable#View_fadeScrollbars
Romain Guyd6a463a2009-05-21 23:10:10 -0700608 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700609 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700611 * @attr ref android.R.styleable#View_isScrollContainer
612 * @attr ref android.R.styleable#View_focusable
613 * @attr ref android.R.styleable#View_focusableInTouchMode
614 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
615 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800616 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700617 * @attr ref android.R.styleable#View_longClickable
618 * @attr ref android.R.styleable#View_minHeight
619 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 * @attr ref android.R.styleable#View_nextFocusDown
621 * @attr ref android.R.styleable#View_nextFocusLeft
622 * @attr ref android.R.styleable#View_nextFocusRight
623 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700624 * @attr ref android.R.styleable#View_onClick
625 * @attr ref android.R.styleable#View_padding
626 * @attr ref android.R.styleable#View_paddingBottom
627 * @attr ref android.R.styleable#View_paddingLeft
628 * @attr ref android.R.styleable#View_paddingRight
629 * @attr ref android.R.styleable#View_paddingTop
630 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800631 * @attr ref android.R.styleable#View_rotation
632 * @attr ref android.R.styleable#View_rotationX
633 * @attr ref android.R.styleable#View_rotationY
634 * @attr ref android.R.styleable#View_scaleX
635 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 * @attr ref android.R.styleable#View_scrollX
637 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700638 * @attr ref android.R.styleable#View_scrollbarSize
639 * @attr ref android.R.styleable#View_scrollbarStyle
640 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700641 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
642 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
644 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 * @attr ref android.R.styleable#View_scrollbarThumbVertical
646 * @attr ref android.R.styleable#View_scrollbarTrackVertical
647 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
648 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700649 * @attr ref android.R.styleable#View_soundEffectsEnabled
650 * @attr ref android.R.styleable#View_tag
Chet Haase73066682010-11-29 15:55:32 -0800651 * @attr ref android.R.styleable#View_transformPivotX
652 * @attr ref android.R.styleable#View_transformPivotY
653 * @attr ref android.R.styleable#View_translationX
654 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700655 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 *
657 * @see android.view.ViewGroup
658 */
Adam Powell8fc54f92011-09-07 16:40:45 -0700659public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Callback,
660 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 private static final boolean DBG = false;
662
663 /**
664 * The logging tag used by this class with android.util.Log.
665 */
666 protected static final String VIEW_LOG_TAG = "View";
667
668 /**
Guang Zhu0d607fb2012-05-11 19:34:56 -0700669 * When set to true, apps will draw debugging information about their layouts.
Romain Guy4b8c4f82012-04-27 15:48:35 -0700670 *
671 * @hide
672 */
673 public static final String DEBUG_LAYOUT_PROPERTY = "debug.layout";
674
675 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 * Used to mark a View that has no ID.
677 */
678 public static final int NO_ID = -1;
679
680 /**
681 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
682 * calling setFlags.
683 */
684 private static final int NOT_FOCUSABLE = 0x00000000;
685
686 /**
687 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
688 * setFlags.
689 */
690 private static final int FOCUSABLE = 0x00000001;
691
692 /**
693 * Mask for use with setFlags indicating bits used for focus.
694 */
695 private static final int FOCUSABLE_MASK = 0x00000001;
696
697 /**
698 * This view will adjust its padding to fit sytem windows (e.g. status bar)
699 */
700 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
701
702 /**
Scott Main812634c22011-07-27 13:22:35 -0700703 * This view is visible.
704 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
705 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 */
707 public static final int VISIBLE = 0x00000000;
708
709 /**
710 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700711 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
712 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 */
714 public static final int INVISIBLE = 0x00000004;
715
716 /**
717 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700718 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
719 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 */
721 public static final int GONE = 0x00000008;
722
723 /**
724 * Mask for use with setFlags indicating bits used for visibility.
725 * {@hide}
726 */
727 static final int VISIBILITY_MASK = 0x0000000C;
728
729 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
730
731 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700732 * This view is enabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 * Use with ENABLED_MASK when calling setFlags.
734 * {@hide}
735 */
736 static final int ENABLED = 0x00000000;
737
738 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700739 * This view is disabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 * Use with ENABLED_MASK when calling setFlags.
741 * {@hide}
742 */
743 static final int DISABLED = 0x00000020;
744
745 /**
746 * Mask for use with setFlags indicating bits used for indicating whether
747 * this view is enabled
748 * {@hide}
749 */
750 static final int ENABLED_MASK = 0x00000020;
751
752 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700753 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
754 * called and further optimizations will be performed. It is okay to have
755 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 * {@hide}
757 */
758 static final int WILL_NOT_DRAW = 0x00000080;
759
760 /**
761 * Mask for use with setFlags indicating bits used for indicating whether
762 * this view is will draw
763 * {@hide}
764 */
765 static final int DRAW_MASK = 0x00000080;
766
767 /**
768 * <p>This view doesn't show scrollbars.</p>
769 * {@hide}
770 */
771 static final int SCROLLBARS_NONE = 0x00000000;
772
773 /**
774 * <p>This view shows horizontal scrollbars.</p>
775 * {@hide}
776 */
777 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
778
779 /**
780 * <p>This view shows vertical scrollbars.</p>
781 * {@hide}
782 */
783 static final int SCROLLBARS_VERTICAL = 0x00000200;
784
785 /**
786 * <p>Mask for use with setFlags indicating bits used for indicating which
787 * scrollbars are enabled.</p>
788 * {@hide}
789 */
790 static final int SCROLLBARS_MASK = 0x00000300;
791
Jeff Brown85a31762010-09-01 17:01:00 -0700792 /**
793 * Indicates that the view should filter touches when its window is obscured.
794 * Refer to the class comments for more information about this security feature.
795 * {@hide}
796 */
797 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
798
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700799 /**
800 * Set for framework elements that use FITS_SYSTEM_WINDOWS, to indicate
801 * that they are optional and should be skipped if the window has
802 * requested system UI flags that ignore those insets for layout.
803 */
804 static final int OPTIONAL_FITS_SYSTEM_WINDOWS = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800805
806 /**
807 * <p>This view doesn't show fading edges.</p>
808 * {@hide}
809 */
810 static final int FADING_EDGE_NONE = 0x00000000;
811
812 /**
813 * <p>This view shows horizontal fading edges.</p>
814 * {@hide}
815 */
816 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
817
818 /**
819 * <p>This view shows vertical fading edges.</p>
820 * {@hide}
821 */
822 static final int FADING_EDGE_VERTICAL = 0x00002000;
823
824 /**
825 * <p>Mask for use with setFlags indicating bits used for indicating which
826 * fading edges are enabled.</p>
827 * {@hide}
828 */
829 static final int FADING_EDGE_MASK = 0x00003000;
830
831 /**
832 * <p>Indicates this view can be clicked. When clickable, a View reacts
833 * to clicks by notifying the OnClickListener.<p>
834 * {@hide}
835 */
836 static final int CLICKABLE = 0x00004000;
837
838 /**
839 * <p>Indicates this view is caching its drawing into a bitmap.</p>
840 * {@hide}
841 */
842 static final int DRAWING_CACHE_ENABLED = 0x00008000;
843
844 /**
845 * <p>Indicates that no icicle should be saved for this view.<p>
846 * {@hide}
847 */
848 static final int SAVE_DISABLED = 0x000010000;
849
850 /**
851 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
852 * property.</p>
853 * {@hide}
854 */
855 static final int SAVE_DISABLED_MASK = 0x000010000;
856
857 /**
858 * <p>Indicates that no drawing cache should ever be created for this view.<p>
859 * {@hide}
860 */
861 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
862
863 /**
864 * <p>Indicates this view can take / keep focus when int touch mode.</p>
865 * {@hide}
866 */
867 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
868
869 /**
870 * <p>Enables low quality mode for the drawing cache.</p>
871 */
872 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
873
874 /**
875 * <p>Enables high quality mode for the drawing cache.</p>
876 */
877 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
878
879 /**
880 * <p>Enables automatic quality mode for the drawing cache.</p>
881 */
882 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
883
884 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
885 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
886 };
887
888 /**
889 * <p>Mask for use with setFlags indicating bits used for the cache
890 * quality property.</p>
891 * {@hide}
892 */
893 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
894
895 /**
896 * <p>
897 * Indicates this view can be long clicked. When long clickable, a View
898 * reacts to long clicks by notifying the OnLongClickListener or showing a
899 * context menu.
900 * </p>
901 * {@hide}
902 */
903 static final int LONG_CLICKABLE = 0x00200000;
904
905 /**
906 * <p>Indicates that this view gets its drawable states from its direct parent
907 * and ignores its original internal states.</p>
908 *
909 * @hide
910 */
911 static final int DUPLICATE_PARENT_STATE = 0x00400000;
912
913 /**
914 * The scrollbar style to display the scrollbars inside the content area,
915 * without increasing the padding. The scrollbars will be overlaid with
916 * translucency on the view's content.
917 */
918 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
919
920 /**
921 * The scrollbar style to display the scrollbars inside the padded area,
922 * increasing the padding of the view. The scrollbars will not overlap the
923 * content area of the view.
924 */
925 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
926
927 /**
928 * The scrollbar style to display the scrollbars at the edge of the view,
929 * without increasing the padding. The scrollbars will be overlaid with
930 * translucency.
931 */
932 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
933
934 /**
935 * The scrollbar style to display the scrollbars at the edge of the view,
936 * increasing the padding of the view. The scrollbars will only overlap the
937 * background, if any.
938 */
939 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
940
941 /**
942 * Mask to check if the scrollbar style is overlay or inset.
943 * {@hide}
944 */
945 static final int SCROLLBARS_INSET_MASK = 0x01000000;
946
947 /**
948 * Mask to check if the scrollbar style is inside or outside.
949 * {@hide}
950 */
951 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
952
953 /**
954 * Mask for scrollbar style.
955 * {@hide}
956 */
957 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
958
959 /**
960 * View flag indicating that the screen should remain on while the
961 * window containing this view is visible to the user. This effectively
962 * takes care of automatically setting the WindowManager's
963 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
964 */
965 public static final int KEEP_SCREEN_ON = 0x04000000;
966
967 /**
968 * View flag indicating whether this view should have sound effects enabled
969 * for events such as clicking and touching.
970 */
971 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
972
973 /**
974 * View flag indicating whether this view should have haptic feedback
975 * enabled for events such as long presses.
976 */
977 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
978
979 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700980 * <p>Indicates that the view hierarchy should stop saving state when
981 * it reaches this view. If state saving is initiated immediately at
982 * the view, it will be allowed.
983 * {@hide}
984 */
985 static final int PARENT_SAVE_DISABLED = 0x20000000;
986
987 /**
988 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
989 * {@hide}
990 */
991 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
992
993 /**
svetoslavganov75986cf2009-05-14 22:28:01 -0700994 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
995 * should add all focusable Views regardless if they are focusable in touch mode.
996 */
997 public static final int FOCUSABLES_ALL = 0x00000000;
998
999 /**
1000 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1001 * should add only Views focusable in touch mode.
1002 */
1003 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1004
1005 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07001006 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1007 * should add only accessibility focusable Views.
1008 *
1009 * @hide
1010 */
1011 public static final int FOCUSABLES_ACCESSIBILITY = 0x00000002;
1012
1013 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001014 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 * item.
1016 */
1017 public static final int FOCUS_BACKWARD = 0x00000001;
1018
1019 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001020 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 * item.
1022 */
1023 public static final int FOCUS_FORWARD = 0x00000002;
1024
1025 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001026 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 */
1028 public static final int FOCUS_LEFT = 0x00000011;
1029
1030 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001031 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 */
1033 public static final int FOCUS_UP = 0x00000021;
1034
1035 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001036 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 */
1038 public static final int FOCUS_RIGHT = 0x00000042;
1039
1040 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001041 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 */
1043 public static final int FOCUS_DOWN = 0x00000082;
1044
Svetoslav Ganov42138042012-03-20 11:51:39 -07001045 // Accessibility focus directions.
1046
1047 /**
1048 * The accessibility focus which is the current user position when
1049 * interacting with the accessibility framework.
1050 */
1051 public static final int FOCUS_ACCESSIBILITY = 0x00001000;
1052
1053 /**
1054 * Use with {@link #focusSearch(int)}. Move acessibility focus left.
1055 */
1056 public static final int ACCESSIBILITY_FOCUS_LEFT = FOCUS_LEFT | FOCUS_ACCESSIBILITY;
1057
1058 /**
1059 * Use with {@link #focusSearch(int)}. Move acessibility focus up.
1060 */
1061 public static final int ACCESSIBILITY_FOCUS_UP = FOCUS_UP | FOCUS_ACCESSIBILITY;
1062
1063 /**
1064 * Use with {@link #focusSearch(int)}. Move acessibility focus right.
1065 */
1066 public static final int ACCESSIBILITY_FOCUS_RIGHT = FOCUS_RIGHT | FOCUS_ACCESSIBILITY;
1067
1068 /**
1069 * Use with {@link #focusSearch(int)}. Move acessibility focus down.
1070 */
1071 public static final int ACCESSIBILITY_FOCUS_DOWN = FOCUS_DOWN | FOCUS_ACCESSIBILITY;
1072
1073 /**
Svetoslav Ganovd6e716d2012-04-20 18:36:11 -07001074 * Use with {@link #focusSearch(int)}. Move acessibility focus forward.
Svetoslav Ganov42138042012-03-20 11:51:39 -07001075 */
1076 public static final int ACCESSIBILITY_FOCUS_FORWARD = FOCUS_FORWARD | FOCUS_ACCESSIBILITY;
1077
1078 /**
Svetoslav Ganovd6e716d2012-04-20 18:36:11 -07001079 * Use with {@link #focusSearch(int)}. Move acessibility focus backward.
Svetoslav Ganov42138042012-03-20 11:51:39 -07001080 */
1081 public static final int ACCESSIBILITY_FOCUS_BACKWARD = FOCUS_BACKWARD | FOCUS_ACCESSIBILITY;
1082
1083 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001084 * Bits of {@link #getMeasuredWidthAndState()} and
1085 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1086 */
1087 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1088
1089 /**
1090 * Bits of {@link #getMeasuredWidthAndState()} and
1091 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1092 */
1093 public static final int MEASURED_STATE_MASK = 0xff000000;
1094
1095 /**
1096 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1097 * for functions that combine both width and height into a single int,
1098 * such as {@link #getMeasuredState()} and the childState argument of
1099 * {@link #resolveSizeAndState(int, int, int)}.
1100 */
1101 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1102
1103 /**
1104 * Bit of {@link #getMeasuredWidthAndState()} and
1105 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1106 * is smaller that the space the view would like to have.
1107 */
1108 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1109
1110 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 * Base View state sets
1112 */
1113 // Singles
1114 /**
1115 * Indicates the view has no states set. States are used with
1116 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1117 * view depending on its state.
1118 *
1119 * @see android.graphics.drawable.Drawable
1120 * @see #getDrawableState()
1121 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001122 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 /**
1124 * Indicates the view is enabled. States are used with
1125 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1126 * view depending on its state.
1127 *
1128 * @see android.graphics.drawable.Drawable
1129 * @see #getDrawableState()
1130 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001131 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 /**
1133 * Indicates the view is focused. States are used with
1134 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1135 * view depending on its state.
1136 *
1137 * @see android.graphics.drawable.Drawable
1138 * @see #getDrawableState()
1139 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001140 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 /**
1142 * Indicates the view is selected. States are used with
1143 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1144 * view depending on its state.
1145 *
1146 * @see android.graphics.drawable.Drawable
1147 * @see #getDrawableState()
1148 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001149 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 /**
1151 * Indicates the view is pressed. States are used with
1152 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1153 * view depending on its state.
1154 *
1155 * @see android.graphics.drawable.Drawable
1156 * @see #getDrawableState()
1157 * @hide
1158 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001159 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 /**
1161 * Indicates the view's window has focus. States are used with
1162 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1163 * view depending on its state.
1164 *
1165 * @see android.graphics.drawable.Drawable
1166 * @see #getDrawableState()
1167 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001168 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 // Doubles
1170 /**
1171 * Indicates the view is enabled and has the focus.
1172 *
1173 * @see #ENABLED_STATE_SET
1174 * @see #FOCUSED_STATE_SET
1175 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001176 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 /**
1178 * Indicates the view is enabled and selected.
1179 *
1180 * @see #ENABLED_STATE_SET
1181 * @see #SELECTED_STATE_SET
1182 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001183 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 /**
1185 * Indicates the view is enabled and that its window has focus.
1186 *
1187 * @see #ENABLED_STATE_SET
1188 * @see #WINDOW_FOCUSED_STATE_SET
1189 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001190 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 /**
1192 * Indicates the view is focused and selected.
1193 *
1194 * @see #FOCUSED_STATE_SET
1195 * @see #SELECTED_STATE_SET
1196 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001197 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 /**
1199 * Indicates the view has the focus and that its window has the focus.
1200 *
1201 * @see #FOCUSED_STATE_SET
1202 * @see #WINDOW_FOCUSED_STATE_SET
1203 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001204 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 /**
1206 * Indicates the view is selected and that its window has the focus.
1207 *
1208 * @see #SELECTED_STATE_SET
1209 * @see #WINDOW_FOCUSED_STATE_SET
1210 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001211 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 // Triples
1213 /**
1214 * Indicates the view is enabled, focused and selected.
1215 *
1216 * @see #ENABLED_STATE_SET
1217 * @see #FOCUSED_STATE_SET
1218 * @see #SELECTED_STATE_SET
1219 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001220 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 /**
1222 * Indicates the view is enabled, focused and its window has the focus.
1223 *
1224 * @see #ENABLED_STATE_SET
1225 * @see #FOCUSED_STATE_SET
1226 * @see #WINDOW_FOCUSED_STATE_SET
1227 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001228 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 /**
1230 * Indicates the view is enabled, selected and its window has the focus.
1231 *
1232 * @see #ENABLED_STATE_SET
1233 * @see #SELECTED_STATE_SET
1234 * @see #WINDOW_FOCUSED_STATE_SET
1235 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001236 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 /**
1238 * Indicates the view is focused, selected and its window has the focus.
1239 *
1240 * @see #FOCUSED_STATE_SET
1241 * @see #SELECTED_STATE_SET
1242 * @see #WINDOW_FOCUSED_STATE_SET
1243 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001244 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 /**
1246 * Indicates the view is enabled, focused, selected and its window
1247 * has the focus.
1248 *
1249 * @see #ENABLED_STATE_SET
1250 * @see #FOCUSED_STATE_SET
1251 * @see #SELECTED_STATE_SET
1252 * @see #WINDOW_FOCUSED_STATE_SET
1253 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001254 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 /**
1256 * Indicates the view is pressed and its window has the focus.
1257 *
1258 * @see #PRESSED_STATE_SET
1259 * @see #WINDOW_FOCUSED_STATE_SET
1260 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001261 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 /**
1263 * Indicates the view is pressed and selected.
1264 *
1265 * @see #PRESSED_STATE_SET
1266 * @see #SELECTED_STATE_SET
1267 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001268 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269 /**
1270 * Indicates the view is pressed, selected and its window has the focus.
1271 *
1272 * @see #PRESSED_STATE_SET
1273 * @see #SELECTED_STATE_SET
1274 * @see #WINDOW_FOCUSED_STATE_SET
1275 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001276 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 /**
1278 * Indicates the view is pressed and focused.
1279 *
1280 * @see #PRESSED_STATE_SET
1281 * @see #FOCUSED_STATE_SET
1282 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001283 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 /**
1285 * Indicates the view is pressed, focused and its window has the focus.
1286 *
1287 * @see #PRESSED_STATE_SET
1288 * @see #FOCUSED_STATE_SET
1289 * @see #WINDOW_FOCUSED_STATE_SET
1290 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001291 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 /**
1293 * Indicates the view is pressed, focused and selected.
1294 *
1295 * @see #PRESSED_STATE_SET
1296 * @see #SELECTED_STATE_SET
1297 * @see #FOCUSED_STATE_SET
1298 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001299 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 /**
1301 * Indicates the view is pressed, focused, selected and its window has the focus.
1302 *
1303 * @see #PRESSED_STATE_SET
1304 * @see #FOCUSED_STATE_SET
1305 * @see #SELECTED_STATE_SET
1306 * @see #WINDOW_FOCUSED_STATE_SET
1307 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001308 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 /**
1310 * Indicates the view is pressed and enabled.
1311 *
1312 * @see #PRESSED_STATE_SET
1313 * @see #ENABLED_STATE_SET
1314 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001315 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 /**
1317 * Indicates the view is pressed, enabled and its window has the focus.
1318 *
1319 * @see #PRESSED_STATE_SET
1320 * @see #ENABLED_STATE_SET
1321 * @see #WINDOW_FOCUSED_STATE_SET
1322 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001323 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 /**
1325 * Indicates the view is pressed, enabled and selected.
1326 *
1327 * @see #PRESSED_STATE_SET
1328 * @see #ENABLED_STATE_SET
1329 * @see #SELECTED_STATE_SET
1330 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001331 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 /**
1333 * Indicates the view is pressed, enabled, selected and its window has the
1334 * focus.
1335 *
1336 * @see #PRESSED_STATE_SET
1337 * @see #ENABLED_STATE_SET
1338 * @see #SELECTED_STATE_SET
1339 * @see #WINDOW_FOCUSED_STATE_SET
1340 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001341 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 /**
1343 * Indicates the view is pressed, enabled and focused.
1344 *
1345 * @see #PRESSED_STATE_SET
1346 * @see #ENABLED_STATE_SET
1347 * @see #FOCUSED_STATE_SET
1348 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001349 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 /**
1351 * Indicates the view is pressed, enabled, focused and its window has the
1352 * focus.
1353 *
1354 * @see #PRESSED_STATE_SET
1355 * @see #ENABLED_STATE_SET
1356 * @see #FOCUSED_STATE_SET
1357 * @see #WINDOW_FOCUSED_STATE_SET
1358 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001359 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 /**
1361 * Indicates the view is pressed, enabled, focused and selected.
1362 *
1363 * @see #PRESSED_STATE_SET
1364 * @see #ENABLED_STATE_SET
1365 * @see #SELECTED_STATE_SET
1366 * @see #FOCUSED_STATE_SET
1367 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001368 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 /**
1370 * Indicates the view is pressed, enabled, focused, selected and its window
1371 * has the focus.
1372 *
1373 * @see #PRESSED_STATE_SET
1374 * @see #ENABLED_STATE_SET
1375 * @see #SELECTED_STATE_SET
1376 * @see #FOCUSED_STATE_SET
1377 * @see #WINDOW_FOCUSED_STATE_SET
1378 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001379 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380
1381 /**
1382 * The order here is very important to {@link #getDrawableState()}
1383 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001384 private static final int[][] VIEW_STATE_SETS;
1385
Romain Guyb051e892010-09-28 19:09:36 -07001386 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1387 static final int VIEW_STATE_SELECTED = 1 << 1;
1388 static final int VIEW_STATE_FOCUSED = 1 << 2;
1389 static final int VIEW_STATE_ENABLED = 1 << 3;
1390 static final int VIEW_STATE_PRESSED = 1 << 4;
1391 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001392 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001393 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001394 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1395 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001396
1397 static final int[] VIEW_STATE_IDS = new int[] {
1398 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1399 R.attr.state_selected, VIEW_STATE_SELECTED,
1400 R.attr.state_focused, VIEW_STATE_FOCUSED,
1401 R.attr.state_enabled, VIEW_STATE_ENABLED,
1402 R.attr.state_pressed, VIEW_STATE_PRESSED,
1403 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001404 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001405 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001406 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
Svetoslav Ganov42138042012-03-20 11:51:39 -07001407 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 };
1409
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001410 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001411 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1412 throw new IllegalStateException(
1413 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1414 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001415 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001416 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001417 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001418 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001419 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001420 orderedIds[i * 2] = viewState;
1421 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001422 }
1423 }
1424 }
Romain Guyb051e892010-09-28 19:09:36 -07001425 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1426 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1427 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001428 int numBits = Integer.bitCount(i);
1429 int[] set = new int[numBits];
1430 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001431 for (int j = 0; j < orderedIds.length; j += 2) {
1432 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001433 set[pos++] = orderedIds[j];
1434 }
1435 }
1436 VIEW_STATE_SETS[i] = set;
1437 }
1438
1439 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1440 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1441 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1442 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1443 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1444 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1445 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1446 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1447 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1448 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1449 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1450 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1451 | VIEW_STATE_FOCUSED];
1452 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1453 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1454 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1455 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1456 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1457 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1458 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1459 | VIEW_STATE_ENABLED];
1460 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1461 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1462 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1463 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1464 | VIEW_STATE_ENABLED];
1465 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1466 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1467 | VIEW_STATE_ENABLED];
1468 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1469 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1470 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1471
1472 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1473 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1474 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1475 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1476 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1477 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1478 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1479 | VIEW_STATE_PRESSED];
1480 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1481 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1482 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1483 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1484 | VIEW_STATE_PRESSED];
1485 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1486 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1487 | VIEW_STATE_PRESSED];
1488 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1489 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1490 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1491 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1492 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1493 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1494 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1495 | VIEW_STATE_PRESSED];
1496 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1497 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1498 | VIEW_STATE_PRESSED];
1499 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1500 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1501 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1502 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1503 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1504 | VIEW_STATE_PRESSED];
1505 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1506 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1507 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1508 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1509 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1510 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1511 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1512 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1513 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1514 | VIEW_STATE_PRESSED];
1515 }
1516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001518 * Accessibility event types that are dispatched for text population.
1519 */
1520 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1521 AccessibilityEvent.TYPE_VIEW_CLICKED
1522 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1523 | AccessibilityEvent.TYPE_VIEW_SELECTED
1524 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1525 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1526 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001527 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
Svetoslav Ganov84dd52e2011-11-18 10:24:00 -08001528 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
Svetoslav Ganov42138042012-03-20 11:51:39 -07001529 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001530 | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED
1531 | AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001532
1533 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534 * Temporary Rect currently for use in setBackground(). This will probably
1535 * be extended in the future to hold our own class with more than just
1536 * a Rect. :)
1537 */
1538 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001539
1540 /**
1541 * Map used to store views' tags.
1542 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001543 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001546 * The next available accessiiblity id.
1547 */
1548 private static int sNextAccessibilityViewId;
1549
1550 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 * The animation currently associated with this view.
1552 * @hide
1553 */
1554 protected Animation mCurrentAnimation = null;
1555
1556 /**
1557 * Width as measured during measure pass.
1558 * {@hide}
1559 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001560 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001561 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562
1563 /**
1564 * Height as measured during measure pass.
1565 * {@hide}
1566 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001567 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001568 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569
1570 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001571 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1572 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1573 * its display list. This flag, used only when hw accelerated, allows us to clear the
1574 * flag while retaining this information until it's needed (at getDisplayList() time and
1575 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1576 *
1577 * {@hide}
1578 */
1579 boolean mRecreateDisplayList = false;
1580
1581 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001582 * The view's identifier.
1583 * {@hide}
1584 *
1585 * @see #setId(int)
1586 * @see #getId()
1587 */
1588 @ViewDebug.ExportedProperty(resolveId = true)
1589 int mID = NO_ID;
1590
1591 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001592 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001593 */
1594 int mAccessibilityViewId = NO_ID;
1595
1596 /**
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001597 * @hide
1598 */
1599 private int mAccessibilityCursorPosition = -1;
1600
1601 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 * The view's tag.
1603 * {@hide}
1604 *
1605 * @see #setTag(Object)
1606 * @see #getTag()
1607 */
1608 protected Object mTag;
1609
1610 // for mPrivateFlags:
1611 /** {@hide} */
1612 static final int WANTS_FOCUS = 0x00000001;
1613 /** {@hide} */
1614 static final int FOCUSED = 0x00000002;
1615 /** {@hide} */
1616 static final int SELECTED = 0x00000004;
1617 /** {@hide} */
1618 static final int IS_ROOT_NAMESPACE = 0x00000008;
1619 /** {@hide} */
1620 static final int HAS_BOUNDS = 0x00000010;
1621 /** {@hide} */
1622 static final int DRAWN = 0x00000020;
1623 /**
1624 * When this flag is set, this view is running an animation on behalf of its
1625 * children and should therefore not cancel invalidate requests, even if they
1626 * lie outside of this view's bounds.
1627 *
1628 * {@hide}
1629 */
1630 static final int DRAW_ANIMATION = 0x00000040;
1631 /** {@hide} */
1632 static final int SKIP_DRAW = 0x00000080;
1633 /** {@hide} */
1634 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1635 /** {@hide} */
1636 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1637 /** {@hide} */
1638 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1639 /** {@hide} */
1640 static final int MEASURED_DIMENSION_SET = 0x00000800;
1641 /** {@hide} */
1642 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001643 /** {@hide} */
1644 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001645
1646 private static final int PRESSED = 0x00004000;
1647
1648 /** {@hide} */
1649 static final int DRAWING_CACHE_VALID = 0x00008000;
1650 /**
1651 * Flag used to indicate that this view should be drawn once more (and only once
1652 * more) after its animation has completed.
1653 * {@hide}
1654 */
1655 static final int ANIMATION_STARTED = 0x00010000;
1656
1657 private static final int SAVE_STATE_CALLED = 0x00020000;
1658
1659 /**
1660 * Indicates that the View returned true when onSetAlpha() was called and that
1661 * the alpha must be restored.
1662 * {@hide}
1663 */
1664 static final int ALPHA_SET = 0x00040000;
1665
1666 /**
1667 * Set by {@link #setScrollContainer(boolean)}.
1668 */
1669 static final int SCROLL_CONTAINER = 0x00080000;
1670
1671 /**
1672 * Set by {@link #setScrollContainer(boolean)}.
1673 */
1674 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1675
1676 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001677 * View flag indicating whether this view was invalidated (fully or partially.)
1678 *
1679 * @hide
1680 */
1681 static final int DIRTY = 0x00200000;
1682
1683 /**
1684 * View flag indicating whether this view was invalidated by an opaque
1685 * invalidate request.
1686 *
1687 * @hide
1688 */
1689 static final int DIRTY_OPAQUE = 0x00400000;
1690
1691 /**
1692 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1693 *
1694 * @hide
1695 */
1696 static final int DIRTY_MASK = 0x00600000;
1697
1698 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001699 * Indicates whether the background is opaque.
1700 *
1701 * @hide
1702 */
1703 static final int OPAQUE_BACKGROUND = 0x00800000;
1704
1705 /**
1706 * Indicates whether the scrollbars are opaque.
1707 *
1708 * @hide
1709 */
1710 static final int OPAQUE_SCROLLBARS = 0x01000000;
1711
1712 /**
1713 * Indicates whether the view is opaque.
1714 *
1715 * @hide
1716 */
1717 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001718
Adam Powelle14579b2009-12-16 18:39:52 -08001719 /**
1720 * Indicates a prepressed state;
1721 * the short time between ACTION_DOWN and recognizing
1722 * a 'real' press. Prepressed is used to recognize quick taps
1723 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001724 *
Adam Powelle14579b2009-12-16 18:39:52 -08001725 * @hide
1726 */
1727 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001728
Adam Powellc9fbaab2010-02-16 17:16:19 -08001729 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001730 * Indicates whether the view is temporarily detached.
1731 *
1732 * @hide
1733 */
1734 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001735
Adam Powell8568c3a2010-04-19 14:26:11 -07001736 /**
1737 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001738 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001739 * @hide
1740 */
1741 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001742
1743 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001744 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1745 * @hide
1746 */
1747 private static final int HOVERED = 0x10000000;
1748
1749 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001750 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1751 * for transform operations
1752 *
1753 * @hide
1754 */
Adam Powellf37df072010-09-17 16:22:49 -07001755 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001756
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001757 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001758 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001759
Chet Haasefd2b0022010-08-06 13:08:56 -07001760 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001761 * Indicates that this view was specifically invalidated, not just dirtied because some
1762 * child view was invalidated. The flag is used to determine when we need to recreate
1763 * a view's display list (as opposed to just returning a reference to its existing
1764 * display list).
1765 *
1766 * @hide
1767 */
1768 static final int INVALIDATED = 0x80000000;
1769
Christopher Tate3d4bf172011-03-28 16:16:46 -07001770 /* Masks for mPrivateFlags2 */
1771
1772 /**
1773 * Indicates that this view has reported that it can accept the current drag's content.
1774 * Cleared when the drag operation concludes.
1775 * @hide
1776 */
1777 static final int DRAG_CAN_ACCEPT = 0x00000001;
1778
1779 /**
1780 * Indicates that this view is currently directly under the drag location in a
1781 * drag-and-drop operation involving content that it can accept. Cleared when
1782 * the drag exits the view, or when the drag operation concludes.
1783 * @hide
1784 */
1785 static final int DRAG_HOVERED = 0x00000002;
1786
Cibu Johny86666632010-02-22 13:01:02 -08001787 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001788 * Horizontal layout direction of this view is from Left to Right.
1789 * Use with {@link #setLayoutDirection}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001790 * @hide
Cibu Johny86666632010-02-22 13:01:02 -08001791 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001792 public static final int LAYOUT_DIRECTION_LTR = 0;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001793
1794 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001795 * Horizontal layout direction of this view is from Right to Left.
1796 * Use with {@link #setLayoutDirection}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001797 * @hide
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001798 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001799 public static final int LAYOUT_DIRECTION_RTL = 1;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001800
1801 /**
1802 * Horizontal layout direction of this view is inherited from its parent.
1803 * Use with {@link #setLayoutDirection}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001804 * @hide
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001805 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001806 public static final int LAYOUT_DIRECTION_INHERIT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001807
1808 /**
1809 * Horizontal layout direction of this view is from deduced from the default language
1810 * script for the locale. Use with {@link #setLayoutDirection}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001811 * @hide
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001812 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001813 public static final int LAYOUT_DIRECTION_LOCALE = 3;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001814
1815 /**
1816 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001817 * @hide
1818 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001819 static final int LAYOUT_DIRECTION_MASK_SHIFT = 2;
1820
1821 /**
1822 * Mask for use with private flags indicating bits used for horizontal layout direction.
1823 * @hide
1824 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001825 static final int LAYOUT_DIRECTION_MASK = 0x00000003 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001826
1827 /**
1828 * Indicates whether the view horizontal layout direction has been resolved and drawn to the
1829 * right-to-left direction.
1830 * @hide
1831 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001832 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 4 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001833
1834 /**
1835 * Indicates whether the view horizontal layout direction has been resolved.
1836 * @hide
1837 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001838 static final int LAYOUT_DIRECTION_RESOLVED = 8 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001839
1840 /**
1841 * Mask for use with private flags indicating bits used for resolved horizontal layout direction.
1842 * @hide
1843 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001844 static final int LAYOUT_DIRECTION_RESOLVED_MASK = 0x0000000C << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001845
1846 /*
1847 * Array of horizontal layout direction flags for mapping attribute "layoutDirection" to correct
1848 * flag value.
1849 * @hide
1850 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001851 private static final int[] LAYOUT_DIRECTION_FLAGS = {
1852 LAYOUT_DIRECTION_LTR,
1853 LAYOUT_DIRECTION_RTL,
1854 LAYOUT_DIRECTION_INHERIT,
1855 LAYOUT_DIRECTION_LOCALE
1856 };
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001857
1858 /**
1859 * Default horizontal layout direction.
1860 * @hide
1861 */
1862 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001863
Adam Powell539ee872012-02-03 19:00:49 -08001864 /**
1865 * Indicates that the view is tracking some sort of transient state
1866 * that the app should not need to be aware of, but that the framework
1867 * should take special care to preserve.
1868 *
1869 * @hide
1870 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001871 static final int HAS_TRANSIENT_STATE = 0x00000100;
Adam Powell539ee872012-02-03 19:00:49 -08001872
1873
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001874 /**
1875 * Text direction is inherited thru {@link ViewGroup}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001876 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001877 */
1878 public static final int TEXT_DIRECTION_INHERIT = 0;
1879
1880 /**
1881 * Text direction is using "first strong algorithm". The first strong directional character
1882 * determines the paragraph direction. If there is no strong directional character, the
1883 * paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001884 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001885 */
1886 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
1887
1888 /**
1889 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
1890 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
1891 * If there are neither, the paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001892 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001893 */
1894 public static final int TEXT_DIRECTION_ANY_RTL = 2;
1895
1896 /**
1897 * Text direction is forced to LTR.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001898 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001899 */
1900 public static final int TEXT_DIRECTION_LTR = 3;
1901
1902 /**
1903 * Text direction is forced to RTL.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001904 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001905 */
1906 public static final int TEXT_DIRECTION_RTL = 4;
1907
1908 /**
1909 * Text direction is coming from the system Locale.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001910 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001911 */
1912 public static final int TEXT_DIRECTION_LOCALE = 5;
1913
1914 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001915 * Default text direction is inherited
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001916 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001917 */
1918 protected static int TEXT_DIRECTION_DEFAULT = TEXT_DIRECTION_INHERIT;
1919
1920 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001921 * Bit shift to get the horizontal layout direction. (bits after LAYOUT_DIRECTION_RESOLVED)
1922 * @hide
1923 */
1924 static final int TEXT_DIRECTION_MASK_SHIFT = 6;
1925
1926 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001927 * Mask for use with private flags indicating bits used for text direction.
1928 * @hide
1929 */
1930 static final int TEXT_DIRECTION_MASK = 0x00000007 << TEXT_DIRECTION_MASK_SHIFT;
1931
1932 /**
1933 * Array of text direction flags for mapping attribute "textDirection" to correct
1934 * flag value.
1935 * @hide
1936 */
1937 private static final int[] TEXT_DIRECTION_FLAGS = {
1938 TEXT_DIRECTION_INHERIT << TEXT_DIRECTION_MASK_SHIFT,
1939 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_MASK_SHIFT,
1940 TEXT_DIRECTION_ANY_RTL << TEXT_DIRECTION_MASK_SHIFT,
1941 TEXT_DIRECTION_LTR << TEXT_DIRECTION_MASK_SHIFT,
1942 TEXT_DIRECTION_RTL << TEXT_DIRECTION_MASK_SHIFT,
1943 TEXT_DIRECTION_LOCALE << TEXT_DIRECTION_MASK_SHIFT
1944 };
1945
1946 /**
1947 * Indicates whether the view text direction has been resolved.
1948 * @hide
1949 */
1950 static final int TEXT_DIRECTION_RESOLVED = 0x00000008 << TEXT_DIRECTION_MASK_SHIFT;
1951
1952 /**
1953 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1954 * @hide
1955 */
1956 static final int TEXT_DIRECTION_RESOLVED_MASK_SHIFT = 10;
1957
1958 /**
1959 * Mask for use with private flags indicating bits used for resolved text direction.
1960 * @hide
1961 */
1962 static final int TEXT_DIRECTION_RESOLVED_MASK = 0x00000007 << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1963
1964 /**
1965 * Indicates whether the view text direction has been resolved to the "first strong" heuristic.
1966 * @hide
1967 */
1968 static final int TEXT_DIRECTION_RESOLVED_DEFAULT =
1969 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1970
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001971 /*
1972 * Default text alignment. The text alignment of this View is inherited from its parent.
1973 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001974 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001975 */
1976 public static final int TEXT_ALIGNMENT_INHERIT = 0;
1977
1978 /**
1979 * Default for the root view. The gravity determines the text alignment, ALIGN_NORMAL,
1980 * ALIGN_CENTER, or ALIGN_OPPOSITE, which are relative to each paragraph’s text direction.
1981 *
1982 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001983 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001984 */
1985 public static final int TEXT_ALIGNMENT_GRAVITY = 1;
1986
1987 /**
1988 * Align to the start of the paragraph, e.g. ALIGN_NORMAL.
1989 *
1990 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001991 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001992 */
1993 public static final int TEXT_ALIGNMENT_TEXT_START = 2;
1994
1995 /**
1996 * Align to the end of the paragraph, e.g. ALIGN_OPPOSITE.
1997 *
1998 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001999 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002000 */
2001 public static final int TEXT_ALIGNMENT_TEXT_END = 3;
2002
2003 /**
2004 * Center the paragraph, e.g. ALIGN_CENTER.
2005 *
2006 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07002007 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002008 */
2009 public static final int TEXT_ALIGNMENT_CENTER = 4;
2010
2011 /**
2012 * Align to the start of the view, which is ALIGN_LEFT if the view’s resolved
2013 * layoutDirection is LTR, and ALIGN_RIGHT otherwise.
2014 *
2015 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07002016 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002017 */
2018 public static final int TEXT_ALIGNMENT_VIEW_START = 5;
2019
2020 /**
2021 * Align to the end of the view, which is ALIGN_RIGHT if the view’s resolved
2022 * layoutDirection is LTR, and ALIGN_LEFT otherwise.
2023 *
2024 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07002025 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002026 */
2027 public static final int TEXT_ALIGNMENT_VIEW_END = 6;
2028
2029 /**
2030 * Default text alignment is inherited
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07002031 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002032 */
2033 protected static int TEXT_ALIGNMENT_DEFAULT = TEXT_ALIGNMENT_GRAVITY;
2034
2035 /**
2036 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
2037 * @hide
2038 */
2039 static final int TEXT_ALIGNMENT_MASK_SHIFT = 13;
2040
2041 /**
2042 * Mask for use with private flags indicating bits used for text alignment.
2043 * @hide
2044 */
2045 static final int TEXT_ALIGNMENT_MASK = 0x00000007 << TEXT_ALIGNMENT_MASK_SHIFT;
2046
2047 /**
2048 * Array of text direction flags for mapping attribute "textAlignment" to correct
2049 * flag value.
2050 * @hide
2051 */
2052 private static final int[] TEXT_ALIGNMENT_FLAGS = {
2053 TEXT_ALIGNMENT_INHERIT << TEXT_ALIGNMENT_MASK_SHIFT,
2054 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_MASK_SHIFT,
2055 TEXT_ALIGNMENT_TEXT_START << TEXT_ALIGNMENT_MASK_SHIFT,
2056 TEXT_ALIGNMENT_TEXT_END << TEXT_ALIGNMENT_MASK_SHIFT,
2057 TEXT_ALIGNMENT_CENTER << TEXT_ALIGNMENT_MASK_SHIFT,
2058 TEXT_ALIGNMENT_VIEW_START << TEXT_ALIGNMENT_MASK_SHIFT,
2059 TEXT_ALIGNMENT_VIEW_END << TEXT_ALIGNMENT_MASK_SHIFT
2060 };
2061
2062 /**
2063 * Indicates whether the view text alignment has been resolved.
2064 * @hide
2065 */
2066 static final int TEXT_ALIGNMENT_RESOLVED = 0x00000008 << TEXT_ALIGNMENT_MASK_SHIFT;
2067
2068 /**
2069 * Bit shift to get the resolved text alignment.
2070 * @hide
2071 */
2072 static final int TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT = 17;
2073
2074 /**
2075 * Mask for use with private flags indicating bits used for text alignment.
2076 * @hide
2077 */
2078 static final int TEXT_ALIGNMENT_RESOLVED_MASK = 0x00000007 << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
2079
2080 /**
2081 * Indicates whether if the view text alignment has been resolved to gravity
2082 */
2083 public static final int TEXT_ALIGNMENT_RESOLVED_DEFAULT =
2084 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
2085
Svetoslav Ganov42138042012-03-20 11:51:39 -07002086 // Accessiblity constants for mPrivateFlags2
2087
2088 /**
2089 * Shift for accessibility related bits in {@link #mPrivateFlags2}.
2090 */
2091 static final int IMPORTANT_FOR_ACCESSIBILITY_SHIFT = 20;
2092
2093 /**
2094 * Automatically determine whether a view is important for accessibility.
2095 */
2096 public static final int IMPORTANT_FOR_ACCESSIBILITY_AUTO = 0x00000000;
2097
2098 /**
2099 * The view is important for accessibility.
2100 */
2101 public static final int IMPORTANT_FOR_ACCESSIBILITY_YES = 0x00000001;
2102
2103 /**
2104 * The view is not important for accessibility.
2105 */
2106 public static final int IMPORTANT_FOR_ACCESSIBILITY_NO = 0x00000002;
2107
2108 /**
2109 * The default whether the view is important for accessiblity.
2110 */
2111 static final int IMPORTANT_FOR_ACCESSIBILITY_DEFAULT = IMPORTANT_FOR_ACCESSIBILITY_AUTO;
2112
2113 /**
2114 * Mask for obtainig the bits which specify how to determine
2115 * whether a view is important for accessibility.
2116 */
2117 static final int IMPORTANT_FOR_ACCESSIBILITY_MASK = (IMPORTANT_FOR_ACCESSIBILITY_AUTO
2118 | IMPORTANT_FOR_ACCESSIBILITY_YES | IMPORTANT_FOR_ACCESSIBILITY_NO)
2119 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
2120
2121 /**
2122 * Flag indicating whether a view has accessibility focus.
2123 */
2124 static final int ACCESSIBILITY_FOCUSED = 0x00000040 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
2125
2126 /**
2127 * Flag indicating whether a view state for accessibility has changed.
2128 */
2129 static final int ACCESSIBILITY_STATE_CHANGED = 0x00000080 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07002130
Chet Haaseafd5c3e2012-05-10 13:21:10 -07002131 /**
2132 * Flag indicating that view has an animation set on it. This is used to track whether an
2133 * animation is cleared between successive frames, in order to tell the associated
2134 * DisplayList to clear its animation matrix.
2135 */
2136 static final int VIEW_IS_ANIMATING_TRANSFORM = 0x10000000;
2137
Chet Haase1a3ab172012-05-11 08:41:20 -07002138 /**
2139 * Flag indicating whether a view failed the quickReject() check in draw(). This condition
2140 * is used to check whether later changes to the view's transform should invalidate the
2141 * view to force the quickReject test to run again.
2142 */
2143 static final int VIEW_QUICK_REJECTED = 0x20000000;
2144
Christopher Tate3d4bf172011-03-28 16:16:46 -07002145 /* End of masks for mPrivateFlags2 */
2146
2147 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
2148
Chet Haasedaf98e92011-01-10 14:10:36 -08002149 /**
Adam Powell637d3372010-08-25 14:37:03 -07002150 * Always allow a user to over-scroll this view, provided it is a
2151 * view that can scroll.
2152 *
2153 * @see #getOverScrollMode()
2154 * @see #setOverScrollMode(int)
2155 */
2156 public static final int OVER_SCROLL_ALWAYS = 0;
2157
2158 /**
2159 * Allow a user to over-scroll this view only if the content is large
2160 * enough to meaningfully scroll, provided it is a view that can scroll.
2161 *
2162 * @see #getOverScrollMode()
2163 * @see #setOverScrollMode(int)
2164 */
2165 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
2166
2167 /**
2168 * Never allow a user to over-scroll this view.
2169 *
2170 * @see #getOverScrollMode()
2171 * @see #setOverScrollMode(int)
2172 */
2173 public static final int OVER_SCROLL_NEVER = 2;
2174
2175 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002176 * Special constant for {@link #setSystemUiVisibility(int)}: View has
2177 * requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08002178 *
Joe Malin32736f02011-01-19 16:14:20 -08002179 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002180 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002181 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08002182
2183 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002184 * Flag for {@link #setSystemUiVisibility(int)}: View has requested the
2185 * system UI to enter an unobtrusive "low profile" mode.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002186 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002187 * <p>This is for use in games, book readers, video players, or any other
Philip Milne6c8ea062012-04-03 17:38:43 -07002188 * "immersive" application where the usual system chrome is deemed too distracting.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002189 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002190 * <p>In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08002191 *
Joe Malin32736f02011-01-19 16:14:20 -08002192 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002193 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002194 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
2195
2196 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002197 * Flag for {@link #setSystemUiVisibility(int)}: View has requested that the
2198 * system navigation be temporarily hidden.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002199 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002200 * <p>This is an even less obtrusive state than that called for by
Daniel Sandler60ee2562011-07-22 12:34:33 -04002201 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
2202 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
2203 * those to disappear. This is useful (in conjunction with the
Philip Milne6c8ea062012-04-03 17:38:43 -07002204 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
Daniel Sandler60ee2562011-07-22 12:34:33 -04002205 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
2206 * window flags) for displaying content using every last pixel on the display.
2207 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002208 * <p>There is a limitation: because navigation controls are so important, the least user
2209 * interaction will cause them to reappear immediately. When this happens, both
2210 * this flag and {@link #SYSTEM_UI_FLAG_FULLSCREEN} will be cleared automatically,
2211 * so that both elements reappear at the same time.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002212 *
2213 * @see #setSystemUiVisibility(int)
2214 */
2215 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
2216
2217 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002218 * Flag for {@link #setSystemUiVisibility(int)}: View has requested to go
2219 * into the normal fullscreen mode so that its content can take over the screen
2220 * while still allowing the user to interact with the application.
2221 *
2222 * <p>This has the same visual effect as
2223 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN
2224 * WindowManager.LayoutParams.FLAG_FULLSCREEN},
2225 * meaning that non-critical screen decorations (such as the status bar) will be
2226 * hidden while the user is in the View's window, focusing the experience on
2227 * that content. Unlike the window flag, if you are using ActionBar in
2228 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2229 * Window.FEATURE_ACTION_BAR_OVERLAY}, then enabling this flag will also
2230 * hide the action bar.
2231 *
2232 * <p>This approach to going fullscreen is best used over the window flag when
2233 * it is a transient state -- that is, the application does this at certain
2234 * points in its user interaction where it wants to allow the user to focus
2235 * on content, but not as a continuous state. For situations where the application
2236 * would like to simply stay full screen the entire time (such as a game that
2237 * wants to take over the screen), the
2238 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN window flag}
2239 * is usually a better approach. The state set here will be removed by the system
2240 * in various situations (such as the user moving to another application) like
2241 * the other system UI states.
2242 *
2243 * <p>When using this flag, the application should provide some easy facility
2244 * for the user to go out of it. A common example would be in an e-book
2245 * reader, where tapping on the screen brings back whatever screen and UI
2246 * decorations that had been hidden while the user was immersed in reading
2247 * the book.
2248 *
2249 * @see #setSystemUiVisibility(int)
2250 */
2251 public static final int SYSTEM_UI_FLAG_FULLSCREEN = 0x00000004;
2252
2253 /**
2254 * Flag for {@link #setSystemUiVisibility(int)}: When using other layout
2255 * flags, we would like a stable view of the content insets given to
2256 * {@link #fitSystemWindows(Rect)}. This means that the insets seen there
2257 * will always represent the worst case that the application can expect
Dianne Hackborn5b5cc4d2012-05-16 13:15:00 -07002258 * as a continuous state. In the stock Android UI this is the space for
2259 * the system bar, nav bar, and status bar, but not more transient elements
2260 * such as an input method.
2261 *
2262 * The stable layout your UI sees is based on the system UI modes you can
2263 * switch to. That is, if you specify {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
2264 * then you will get a stable layout for changes of the
2265 * {@link #SYSTEM_UI_FLAG_FULLSCREEN} mode; if you specify
2266 * {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN} and
2267 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION}, then you can transition
2268 * to {@link #SYSTEM_UI_FLAG_FULLSCREEN} and {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}
2269 * with a stable layout. (Note that you should avoid using
2270 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} by itself.)
2271 *
2272 * If you have set the window flag {@ WindowManager.LayoutParams#FLAG_FULLSCREEN}
2273 * to hide the status bar (instead of using {@link #SYSTEM_UI_FLAG_FULLSCREEN}),
2274 * then a hidden status bar will be considered a "stable" state for purposes
2275 * here. This allows your UI to continually hide the status bar, while still
2276 * using the system UI flags to hide the action bar while still retaining
2277 * a stable layout. Note that changing the window fullscreen flag will never
2278 * provide a stable layout for a clean transition.
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002279 *
2280 * <p>If you are using ActionBar in
2281 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2282 * Window.FEATURE_ACTION_BAR_OVERLAY}, this flag will also impact the
2283 * insets it adds to those given to the application.
2284 */
2285 public static final int SYSTEM_UI_FLAG_LAYOUT_STABLE = 0x00000100;
2286
2287 /**
2288 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2289 * to be layed out as if it has requested
2290 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, even if it currently hasn't. This
2291 * allows it to avoid artifacts when switching in and out of that mode, at
2292 * the expense that some of its user interface may be covered by screen
2293 * decorations when they are shown. You can perform layout of your inner
2294 * UI elements to account for the navagation system UI through the
2295 * {@link #fitSystemWindows(Rect)} method.
2296 */
2297 public static final int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 0x00000200;
2298
2299 /**
2300 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2301 * to be layed out as if it has requested
2302 * {@link #SYSTEM_UI_FLAG_FULLSCREEN}, even if it currently hasn't. This
2303 * allows it to avoid artifacts when switching in and out of that mode, at
2304 * the expense that some of its user interface may be covered by screen
2305 * decorations when they are shown. You can perform layout of your inner
2306 * UI elements to account for non-fullscreen system UI through the
2307 * {@link #fitSystemWindows(Rect)} method.
2308 */
2309 public static final int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 0x00000400;
2310
2311 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04002312 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
2313 */
2314 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
2315
2316 /**
2317 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
2318 */
2319 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08002320
2321 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002322 * @hide
2323 *
2324 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2325 * out of the public fields to keep the undefined bits out of the developer's way.
2326 *
2327 * Flag to make the status bar not expandable. Unless you also
2328 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
2329 */
2330 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
2331
2332 /**
2333 * @hide
2334 *
2335 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2336 * out of the public fields to keep the undefined bits out of the developer's way.
2337 *
2338 * Flag to hide notification icons and scrolling ticker text.
2339 */
2340 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
2341
2342 /**
2343 * @hide
2344 *
2345 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2346 * out of the public fields to keep the undefined bits out of the developer's way.
2347 *
2348 * Flag to disable incoming notification alerts. This will not block
2349 * icons, but it will block sound, vibrating and other visual or aural notifications.
2350 */
2351 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
2352
2353 /**
2354 * @hide
2355 *
2356 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2357 * out of the public fields to keep the undefined bits out of the developer's way.
2358 *
2359 * Flag to hide only the scrolling ticker. Note that
2360 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
2361 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
2362 */
2363 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
2364
2365 /**
2366 * @hide
2367 *
2368 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2369 * out of the public fields to keep the undefined bits out of the developer's way.
2370 *
2371 * Flag to hide the center system info area.
2372 */
2373 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
2374
2375 /**
2376 * @hide
2377 *
2378 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2379 * out of the public fields to keep the undefined bits out of the developer's way.
2380 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002381 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002382 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2383 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04002384 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002385
2386 /**
2387 * @hide
2388 *
2389 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2390 * out of the public fields to keep the undefined bits out of the developer's way.
2391 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002392 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08002393 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2394 */
2395 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
2396
2397 /**
2398 * @hide
2399 *
2400 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2401 * out of the public fields to keep the undefined bits out of the developer's way.
2402 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002403 * Flag to hide only the clock. You might use this if your activity has
2404 * its own clock making the status bar's clock redundant.
2405 */
Joe Onorato6478adc2011-01-27 21:15:01 -08002406 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
2407
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002408 /**
2409 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04002410 *
2411 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2412 * out of the public fields to keep the undefined bits out of the developer's way.
2413 *
2414 * Flag to hide only the recent apps button. Don't use this
2415 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2416 */
2417 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
2418
2419 /**
2420 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002421 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002422 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002423
2424 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002425 * These are the system UI flags that can be cleared by events outside
2426 * of an application. Currently this is just the ability to tap on the
2427 * screen while hiding the navigation bar to have it return.
2428 * @hide
2429 */
2430 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002431 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION
2432 | SYSTEM_UI_FLAG_FULLSCREEN;
2433
2434 /**
2435 * Flags that can impact the layout in relation to system UI.
2436 */
2437 public static final int SYSTEM_UI_LAYOUT_FLAGS =
2438 SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
2439 | SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002440
2441 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07002442 * Find views that render the specified text.
2443 *
2444 * @see #findViewsWithText(ArrayList, CharSequence, int)
2445 */
2446 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
2447
2448 /**
2449 * Find find views that contain the specified content description.
2450 *
2451 * @see #findViewsWithText(ArrayList, CharSequence, int)
2452 */
2453 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
2454
2455 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07002456 * Find views that contain {@link AccessibilityNodeProvider}. Such
2457 * a View is a root of virtual view hierarchy and may contain the searched
2458 * text. If this flag is set Views with providers are automatically
2459 * added and it is a responsibility of the client to call the APIs of
2460 * the provider to determine whether the virtual tree rooted at this View
2461 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
2462 * represeting the virtual views with this text.
2463 *
2464 * @see #findViewsWithText(ArrayList, CharSequence, int)
2465 *
2466 * @hide
2467 */
2468 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
2469
2470 /**
Romain Guybb9908b2012-03-08 11:14:07 -08002471 * Indicates that the screen has changed state and is now off.
2472 *
2473 * @see #onScreenStateChanged(int)
2474 */
2475 public static final int SCREEN_STATE_OFF = 0x0;
2476
2477 /**
2478 * Indicates that the screen has changed state and is now on.
2479 *
Romain Guy1e3d3132012-03-08 15:55:56 -08002480 * @see #onScreenStateChanged(int)
Romain Guybb9908b2012-03-08 11:14:07 -08002481 */
2482 public static final int SCREEN_STATE_ON = 0x1;
2483
2484 /**
Adam Powell637d3372010-08-25 14:37:03 -07002485 * Controls the over-scroll mode for this view.
2486 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2487 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2488 * and {@link #OVER_SCROLL_NEVER}.
2489 */
2490 private int mOverScrollMode;
2491
2492 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002493 * The parent this view is attached to.
2494 * {@hide}
2495 *
2496 * @see #getParent()
2497 */
2498 protected ViewParent mParent;
2499
2500 /**
2501 * {@hide}
2502 */
2503 AttachInfo mAttachInfo;
2504
2505 /**
2506 * {@hide}
2507 */
Romain Guy809a7f62009-05-14 15:44:42 -07002508 @ViewDebug.ExportedProperty(flagMapping = {
2509 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
2510 name = "FORCE_LAYOUT"),
2511 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
2512 name = "LAYOUT_REQUIRED"),
2513 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002514 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07002515 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
2516 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
2517 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2518 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
2519 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002520 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002521 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002522
2523 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002524 * This view's request for the visibility of the status bar.
2525 * @hide
2526 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002527 @ViewDebug.ExportedProperty(flagMapping = {
2528 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2529 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2530 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2531 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2532 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2533 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2534 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2535 equals = SYSTEM_UI_FLAG_VISIBLE,
2536 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2537 })
Joe Onorato664644d2011-01-23 17:53:23 -08002538 int mSystemUiVisibility;
2539
2540 /**
Chet Haase563d4f22012-04-18 16:20:08 -07002541 * Reference count for transient state.
2542 * @see #setHasTransientState(boolean)
2543 */
2544 int mTransientStateCount = 0;
2545
2546 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002547 * Count of how many windows this view has been attached to.
2548 */
2549 int mWindowAttachCount;
2550
2551 /**
2552 * The layout parameters associated with this view and used by the parent
2553 * {@link android.view.ViewGroup} to determine how this view should be
2554 * laid out.
2555 * {@hide}
2556 */
2557 protected ViewGroup.LayoutParams mLayoutParams;
2558
2559 /**
2560 * The view flags hold various views states.
2561 * {@hide}
2562 */
2563 @ViewDebug.ExportedProperty
2564 int mViewFlags;
2565
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002566 static class TransformationInfo {
2567 /**
2568 * The transform matrix for the View. This transform is calculated internally
2569 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2570 * is used by default. Do *not* use this variable directly; instead call
2571 * getMatrix(), which will automatically recalculate the matrix if necessary
2572 * to get the correct matrix based on the latest rotation and scale properties.
2573 */
2574 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002575
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002576 /**
2577 * The transform matrix for the View. This transform is calculated internally
2578 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2579 * is used by default. Do *not* use this variable directly; instead call
2580 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2581 * to get the correct matrix based on the latest rotation and scale properties.
2582 */
2583 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002584
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002585 /**
2586 * An internal variable that tracks whether we need to recalculate the
2587 * transform matrix, based on whether the rotation or scaleX/Y properties
2588 * have changed since the matrix was last calculated.
2589 */
2590 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002591
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002592 /**
2593 * An internal variable that tracks whether we need to recalculate the
2594 * transform matrix, based on whether the rotation or scaleX/Y properties
2595 * have changed since the matrix was last calculated.
2596 */
2597 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002598
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002599 /**
2600 * A variable that tracks whether we need to recalculate the
2601 * transform matrix, based on whether the rotation or scaleX/Y properties
2602 * have changed since the matrix was last calculated. This variable
2603 * is only valid after a call to updateMatrix() or to a function that
2604 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2605 */
2606 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002607
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002608 /**
2609 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2610 */
2611 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002612
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002613 /**
2614 * This matrix is used when computing the matrix for 3D rotations.
2615 */
2616 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002617
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002618 /**
2619 * These prev values are used to recalculate a centered pivot point when necessary. The
2620 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2621 * set), so thes values are only used then as well.
2622 */
2623 private int mPrevWidth = -1;
2624 private int mPrevHeight = -1;
Philip Milne6c8ea062012-04-03 17:38:43 -07002625
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002626 /**
2627 * The degrees rotation around the vertical axis through the pivot point.
2628 */
2629 @ViewDebug.ExportedProperty
2630 float mRotationY = 0f;
2631
2632 /**
2633 * The degrees rotation around the horizontal axis through the pivot point.
2634 */
2635 @ViewDebug.ExportedProperty
2636 float mRotationX = 0f;
2637
2638 /**
2639 * The degrees rotation around the pivot point.
2640 */
2641 @ViewDebug.ExportedProperty
2642 float mRotation = 0f;
2643
2644 /**
2645 * The amount of translation of the object away from its left property (post-layout).
2646 */
2647 @ViewDebug.ExportedProperty
2648 float mTranslationX = 0f;
2649
2650 /**
2651 * The amount of translation of the object away from its top property (post-layout).
2652 */
2653 @ViewDebug.ExportedProperty
2654 float mTranslationY = 0f;
2655
2656 /**
2657 * The amount of scale in the x direction around the pivot point. A
2658 * value of 1 means no scaling is applied.
2659 */
2660 @ViewDebug.ExportedProperty
2661 float mScaleX = 1f;
2662
2663 /**
2664 * The amount of scale in the y direction around the pivot point. A
2665 * value of 1 means no scaling is applied.
2666 */
2667 @ViewDebug.ExportedProperty
2668 float mScaleY = 1f;
2669
2670 /**
Chet Haasea33de552012-02-03 16:28:24 -08002671 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002672 */
2673 @ViewDebug.ExportedProperty
2674 float mPivotX = 0f;
2675
2676 /**
Chet Haasea33de552012-02-03 16:28:24 -08002677 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002678 */
2679 @ViewDebug.ExportedProperty
2680 float mPivotY = 0f;
2681
2682 /**
2683 * The opacity of the View. This is a value from 0 to 1, where 0 means
2684 * completely transparent and 1 means completely opaque.
2685 */
2686 @ViewDebug.ExportedProperty
2687 float mAlpha = 1f;
2688 }
2689
2690 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002691
Joe Malin32736f02011-01-19 16:14:20 -08002692 private boolean mLastIsOpaque;
2693
Chet Haasefd2b0022010-08-06 13:08:56 -07002694 /**
2695 * Convenience value to check for float values that are close enough to zero to be considered
2696 * zero.
2697 */
Romain Guy2542d192010-08-18 11:47:12 -07002698 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002699
2700 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002701 * The distance in pixels from the left edge of this view's parent
2702 * to the left edge of this view.
2703 * {@hide}
2704 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002705 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002706 protected int mLeft;
2707 /**
2708 * The distance in pixels from the left edge of this view's parent
2709 * to the right edge of this view.
2710 * {@hide}
2711 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002712 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002713 protected int mRight;
2714 /**
2715 * The distance in pixels from the top edge of this view's parent
2716 * to the top edge of this view.
2717 * {@hide}
2718 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002719 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002720 protected int mTop;
2721 /**
2722 * The distance in pixels from the top edge of this view's parent
2723 * to the bottom edge of this view.
2724 * {@hide}
2725 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002726 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002727 protected int mBottom;
2728
2729 /**
2730 * The offset, in pixels, by which the content of this view is scrolled
2731 * horizontally.
2732 * {@hide}
2733 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002734 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002735 protected int mScrollX;
2736 /**
2737 * The offset, in pixels, by which the content of this view is scrolled
2738 * vertically.
2739 * {@hide}
2740 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002741 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002742 protected int mScrollY;
2743
2744 /**
2745 * The left padding in pixels, that is the distance in pixels between the
2746 * left edge of this view and the left edge of its content.
2747 * {@hide}
2748 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002749 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002750 protected int mPaddingLeft;
2751 /**
2752 * The right padding in pixels, that is the distance in pixels between the
2753 * right edge of this view and the right edge of its content.
2754 * {@hide}
2755 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002756 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002757 protected int mPaddingRight;
2758 /**
2759 * The top padding in pixels, that is the distance in pixels between the
2760 * top edge of this view and the top edge of its content.
2761 * {@hide}
2762 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002763 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002764 protected int mPaddingTop;
2765 /**
2766 * The bottom padding in pixels, that is the distance in pixels between the
2767 * bottom edge of this view and the bottom edge of its content.
2768 * {@hide}
2769 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002770 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002771 protected int mPaddingBottom;
2772
2773 /**
Philip Milne1557fd72012-04-04 23:41:34 -07002774 * The layout insets in pixels, that is the distance in pixels between the
2775 * visible edges of this view its bounds.
2776 */
2777 private Insets mLayoutInsets;
2778
2779 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002780 * Briefly describes the view and is primarily used for accessibility support.
2781 */
2782 private CharSequence mContentDescription;
2783
2784 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002785 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002786 *
2787 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002788 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002789 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002790 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002791
2792 /**
2793 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002794 *
2795 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002796 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002797 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002798 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002799
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002800 /**
Adam Powell20232d02010-12-08 21:08:53 -08002801 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002802 *
2803 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002804 */
2805 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002806 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002807
2808 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002809 * Cache if the user padding is relative.
2810 *
2811 */
2812 @ViewDebug.ExportedProperty(category = "padding")
2813 boolean mUserPaddingRelative;
2814
2815 /**
2816 * Cache the paddingStart set by the user to append to the scrollbar's size.
2817 *
2818 */
2819 @ViewDebug.ExportedProperty(category = "padding")
2820 int mUserPaddingStart;
2821
2822 /**
2823 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2824 *
2825 */
2826 @ViewDebug.ExportedProperty(category = "padding")
2827 int mUserPaddingEnd;
2828
2829 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002830 * @hide
2831 */
2832 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2833 /**
2834 * @hide
2835 */
2836 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002837
Philip Milne6c8ea062012-04-03 17:38:43 -07002838 private Drawable mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002839
2840 private int mBackgroundResource;
2841 private boolean mBackgroundSizeChanged;
2842
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002843 static class ListenerInfo {
2844 /**
2845 * Listener used to dispatch focus change events.
2846 * This field should be made private, so it is hidden from the SDK.
2847 * {@hide}
2848 */
2849 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002850
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002851 /**
2852 * Listeners for layout change events.
2853 */
2854 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002855
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002856 /**
2857 * Listeners for attach events.
2858 */
2859 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002860
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002861 /**
2862 * Listener used to dispatch click events.
2863 * This field should be made private, so it is hidden from the SDK.
2864 * {@hide}
2865 */
2866 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002867
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002868 /**
2869 * Listener used to dispatch long click events.
2870 * This field should be made private, so it is hidden from the SDK.
2871 * {@hide}
2872 */
2873 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002874
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002875 /**
2876 * Listener used to build the context menu.
2877 * This field should be made private, so it is hidden from the SDK.
2878 * {@hide}
2879 */
2880 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002881
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002882 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002883
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002884 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002885
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002886 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002887
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002888 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002889
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002890 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002891
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002892 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2893 }
2894
2895 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002897 /**
2898 * The application environment this view lives in.
2899 * This field should be made private, so it is hidden from the SDK.
2900 * {@hide}
2901 */
2902 protected Context mContext;
2903
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002904 private final Resources mResources;
2905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002906 private ScrollabilityCache mScrollCache;
2907
2908 private int[] mDrawableState = null;
2909
Romain Guy0211a0a2011-02-14 16:34:59 -08002910 /**
2911 * Set to true when drawing cache is enabled and cannot be created.
Philip Milne6c8ea062012-04-03 17:38:43 -07002912 *
Romain Guy0211a0a2011-02-14 16:34:59 -08002913 * @hide
2914 */
2915 public boolean mCachingFailed;
2916
Romain Guy02890fd2010-08-06 17:58:44 -07002917 private Bitmap mDrawingCache;
2918 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002919 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002920 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002921
2922 /**
2923 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2924 * the user may specify which view to go to next.
2925 */
2926 private int mNextFocusLeftId = View.NO_ID;
2927
2928 /**
2929 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2930 * the user may specify which view to go to next.
2931 */
2932 private int mNextFocusRightId = View.NO_ID;
2933
2934 /**
2935 * When this view has focus and the next focus is {@link #FOCUS_UP},
2936 * the user may specify which view to go to next.
2937 */
2938 private int mNextFocusUpId = View.NO_ID;
2939
2940 /**
2941 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2942 * the user may specify which view to go to next.
2943 */
2944 private int mNextFocusDownId = View.NO_ID;
2945
Jeff Brown4e6319b2010-12-13 10:36:51 -08002946 /**
2947 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2948 * the user may specify which view to go to next.
2949 */
2950 int mNextFocusForwardId = View.NO_ID;
2951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002952 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002953 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002954 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002955 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002956
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002957 private UnsetPressedState mUnsetPressedState;
2958
2959 /**
2960 * Whether the long press's action has been invoked. The tap's action is invoked on the
2961 * up event while a long press is invoked as soon as the long press duration is reached, so
2962 * a long press could be performed before the tap is checked, in which case the tap's action
2963 * should not be invoked.
2964 */
2965 private boolean mHasPerformedLongPress;
2966
2967 /**
2968 * The minimum height of the view. We'll try our best to have the height
2969 * of this view to at least this amount.
2970 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002971 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002972 private int mMinHeight;
2973
2974 /**
2975 * The minimum width of the view. We'll try our best to have the width
2976 * of this view to at least this amount.
2977 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002978 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002979 private int mMinWidth;
2980
2981 /**
2982 * The delegate to handle touch events that are physically in this view
2983 * but should be handled by another view.
2984 */
2985 private TouchDelegate mTouchDelegate = null;
2986
2987 /**
2988 * Solid color to use as a background when creating the drawing cache. Enables
2989 * the cache to use 16 bit bitmaps instead of 32 bit.
2990 */
2991 private int mDrawingCacheBackgroundColor = 0;
2992
2993 /**
2994 * Special tree observer used when mAttachInfo is null.
2995 */
2996 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002997
Adam Powelle14579b2009-12-16 18:39:52 -08002998 /**
2999 * Cache the touch slop from the context that created the view.
3000 */
3001 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003002
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003003 /**
Chet Haasea00f3862011-02-22 06:34:40 -08003004 * Object that handles automatic animation of view properties.
3005 */
3006 private ViewPropertyAnimator mAnimator = null;
3007
3008 /**
Christopher Tate251602f2011-01-28 17:54:12 -08003009 * Flag indicating that a drag can cross window boundaries. When
3010 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
3011 * with this flag set, all visible applications will be able to participate
3012 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08003013 *
3014 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08003015 */
3016 public static final int DRAG_FLAG_GLOBAL = 1;
3017
3018 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003019 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
3020 */
3021 private float mVerticalScrollFactor;
3022
3023 /**
Adam Powell20232d02010-12-08 21:08:53 -08003024 * Position of the vertical scroll bar.
3025 */
3026 private int mVerticalScrollbarPosition;
3027
3028 /**
3029 * Position the scroll bar at the default position as determined by the system.
3030 */
3031 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
3032
3033 /**
3034 * Position the scroll bar along the left edge.
3035 */
3036 public static final int SCROLLBAR_POSITION_LEFT = 1;
3037
3038 /**
3039 * Position the scroll bar along the right edge.
3040 */
3041 public static final int SCROLLBAR_POSITION_RIGHT = 2;
3042
3043 /**
Romain Guy171c5922011-01-06 10:04:23 -08003044 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08003045 *
3046 * @see #getLayerType()
3047 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003048 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08003049 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003050 */
3051 public static final int LAYER_TYPE_NONE = 0;
3052
3053 /**
3054 * <p>Indicates that the view has a software layer. A software layer is backed
3055 * by a bitmap and causes the view to be rendered using Android's software
3056 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003057 *
Romain Guy171c5922011-01-06 10:04:23 -08003058 * <p>Software layers have various usages:</p>
3059 * <p>When the application is not using hardware acceleration, a software layer
3060 * is useful to apply a specific color filter and/or blending mode and/or
3061 * translucency to a view and all its children.</p>
3062 * <p>When the application is using hardware acceleration, a software layer
3063 * is useful to render drawing primitives not supported by the hardware
3064 * accelerated pipeline. It can also be used to cache a complex view tree
3065 * into a texture and reduce the complexity of drawing operations. For instance,
3066 * when animating a complex view tree with a translation, a software layer can
3067 * be used to render the view tree only once.</p>
3068 * <p>Software layers should be avoided when the affected view tree updates
3069 * often. Every update will require to re-render the software layer, which can
3070 * potentially be slow (particularly when hardware acceleration is turned on
3071 * since the layer will have to be uploaded into a hardware texture after every
3072 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08003073 *
3074 * @see #getLayerType()
3075 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003076 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08003077 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003078 */
3079 public static final int LAYER_TYPE_SOFTWARE = 1;
3080
3081 /**
3082 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
3083 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
3084 * OpenGL hardware) and causes the view to be rendered using Android's hardware
3085 * rendering pipeline, but only if hardware acceleration is turned on for the
3086 * view hierarchy. When hardware acceleration is turned off, hardware layers
3087 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003088 *
Romain Guy171c5922011-01-06 10:04:23 -08003089 * <p>A hardware layer is useful to apply a specific color filter and/or
3090 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08003091 * <p>A hardware layer can be used to cache a complex view tree into a
3092 * texture and reduce the complexity of drawing operations. For instance,
3093 * when animating a complex view tree with a translation, a hardware layer can
3094 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08003095 * <p>A hardware layer can also be used to increase the rendering quality when
3096 * rotation transformations are applied on a view. It can also be used to
3097 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003098 *
3099 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08003100 * @see #setLayerType(int, android.graphics.Paint)
3101 * @see #LAYER_TYPE_NONE
3102 * @see #LAYER_TYPE_SOFTWARE
3103 */
3104 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08003105
Romain Guy3aaff3a2011-01-12 14:18:47 -08003106 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
3107 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
3108 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
3109 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
3110 })
Romain Guy171c5922011-01-06 10:04:23 -08003111 int mLayerType = LAYER_TYPE_NONE;
3112 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08003113 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08003114
3115 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07003116 * Set to true when the view is sending hover accessibility events because it
3117 * is the innermost hovered view.
3118 */
3119 private boolean mSendingHoverAccessibilityEvents;
3120
3121 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003122 * Simple constructor to use when creating a view from code.
3123 *
3124 * @param context The Context the view is running in, through which it can
3125 * access the current theme, resources, etc.
3126 */
3127 public View(Context context) {
3128 mContext = context;
3129 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003130 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003131 // Set layout and text direction defaults
3132 mPrivateFlags2 = (LAYOUT_DIRECTION_DEFAULT << LAYOUT_DIRECTION_MASK_SHIFT) |
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003133 (TEXT_DIRECTION_DEFAULT << TEXT_DIRECTION_MASK_SHIFT) |
Svetoslav Ganov42138042012-03-20 11:51:39 -07003134 (TEXT_ALIGNMENT_DEFAULT << TEXT_ALIGNMENT_MASK_SHIFT) |
3135 (IMPORTANT_FOR_ACCESSIBILITY_DEFAULT << IMPORTANT_FOR_ACCESSIBILITY_SHIFT);
Adam Powelle14579b2009-12-16 18:39:52 -08003136 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07003137 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003138 mUserPaddingStart = -1;
3139 mUserPaddingEnd = -1;
3140 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003141 }
3142
3143 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07003144 * Delegate for injecting accessiblity functionality.
3145 */
3146 AccessibilityDelegate mAccessibilityDelegate;
3147
3148 /**
3149 * Consistency verifier for debugging purposes.
3150 * @hide
3151 */
3152 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
3153 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
3154 new InputEventConsistencyVerifier(this, 0) : null;
3155
3156 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003157 * Constructor that is called when inflating a view from XML. This is called
3158 * when a view is being constructed from an XML file, supplying attributes
3159 * that were specified in the XML file. This version uses a default style of
3160 * 0, so the only attribute values applied are those in the Context's Theme
3161 * and the given AttributeSet.
3162 *
3163 * <p>
3164 * The method onFinishInflate() will be called after all children have been
3165 * added.
3166 *
3167 * @param context The Context the view is running in, through which it can
3168 * access the current theme, resources, etc.
3169 * @param attrs The attributes of the XML tag that is inflating the view.
3170 * @see #View(Context, AttributeSet, int)
3171 */
3172 public View(Context context, AttributeSet attrs) {
3173 this(context, attrs, 0);
3174 }
3175
3176 /**
3177 * Perform inflation from XML and apply a class-specific base style. This
3178 * constructor of View allows subclasses to use their own base style when
3179 * they are inflating. For example, a Button class's constructor would call
3180 * this version of the super class constructor and supply
3181 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
3182 * the theme's button style to modify all of the base view attributes (in
3183 * particular its background) as well as the Button class's attributes.
3184 *
3185 * @param context The Context the view is running in, through which it can
3186 * access the current theme, resources, etc.
3187 * @param attrs The attributes of the XML tag that is inflating the view.
3188 * @param defStyle The default style to apply to this view. If 0, no style
3189 * will be applied (beyond what is included in the theme). This may
3190 * either be an attribute resource, whose value will be retrieved
3191 * from the current theme, or an explicit style resource.
3192 * @see #View(Context, AttributeSet)
3193 */
3194 public View(Context context, AttributeSet attrs, int defStyle) {
3195 this(context);
3196
3197 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
3198 defStyle, 0);
3199
3200 Drawable background = null;
3201
3202 int leftPadding = -1;
3203 int topPadding = -1;
3204 int rightPadding = -1;
3205 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003206 int startPadding = -1;
3207 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003208
3209 int padding = -1;
3210
3211 int viewFlagValues = 0;
3212 int viewFlagMasks = 0;
3213
3214 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07003215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003216 int x = 0;
3217 int y = 0;
3218
Chet Haase73066682010-11-29 15:55:32 -08003219 float tx = 0;
3220 float ty = 0;
3221 float rotation = 0;
3222 float rotationX = 0;
3223 float rotationY = 0;
3224 float sx = 1f;
3225 float sy = 1f;
3226 boolean transformSet = false;
3227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003228 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
3229
Adam Powell637d3372010-08-25 14:37:03 -07003230 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003231 final int N = a.getIndexCount();
3232 for (int i = 0; i < N; i++) {
3233 int attr = a.getIndex(i);
3234 switch (attr) {
3235 case com.android.internal.R.styleable.View_background:
3236 background = a.getDrawable(attr);
3237 break;
3238 case com.android.internal.R.styleable.View_padding:
3239 padding = a.getDimensionPixelSize(attr, -1);
3240 break;
3241 case com.android.internal.R.styleable.View_paddingLeft:
3242 leftPadding = a.getDimensionPixelSize(attr, -1);
3243 break;
3244 case com.android.internal.R.styleable.View_paddingTop:
3245 topPadding = a.getDimensionPixelSize(attr, -1);
3246 break;
3247 case com.android.internal.R.styleable.View_paddingRight:
3248 rightPadding = a.getDimensionPixelSize(attr, -1);
3249 break;
3250 case com.android.internal.R.styleable.View_paddingBottom:
3251 bottomPadding = a.getDimensionPixelSize(attr, -1);
3252 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003253 case com.android.internal.R.styleable.View_paddingStart:
3254 startPadding = a.getDimensionPixelSize(attr, -1);
3255 break;
3256 case com.android.internal.R.styleable.View_paddingEnd:
3257 endPadding = a.getDimensionPixelSize(attr, -1);
3258 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003259 case com.android.internal.R.styleable.View_scrollX:
3260 x = a.getDimensionPixelOffset(attr, 0);
3261 break;
3262 case com.android.internal.R.styleable.View_scrollY:
3263 y = a.getDimensionPixelOffset(attr, 0);
3264 break;
Chet Haase73066682010-11-29 15:55:32 -08003265 case com.android.internal.R.styleable.View_alpha:
3266 setAlpha(a.getFloat(attr, 1f));
3267 break;
3268 case com.android.internal.R.styleable.View_transformPivotX:
3269 setPivotX(a.getDimensionPixelOffset(attr, 0));
3270 break;
3271 case com.android.internal.R.styleable.View_transformPivotY:
3272 setPivotY(a.getDimensionPixelOffset(attr, 0));
3273 break;
3274 case com.android.internal.R.styleable.View_translationX:
3275 tx = a.getDimensionPixelOffset(attr, 0);
3276 transformSet = true;
3277 break;
3278 case com.android.internal.R.styleable.View_translationY:
3279 ty = a.getDimensionPixelOffset(attr, 0);
3280 transformSet = true;
3281 break;
3282 case com.android.internal.R.styleable.View_rotation:
3283 rotation = a.getFloat(attr, 0);
3284 transformSet = true;
3285 break;
3286 case com.android.internal.R.styleable.View_rotationX:
3287 rotationX = a.getFloat(attr, 0);
3288 transformSet = true;
3289 break;
3290 case com.android.internal.R.styleable.View_rotationY:
3291 rotationY = a.getFloat(attr, 0);
3292 transformSet = true;
3293 break;
3294 case com.android.internal.R.styleable.View_scaleX:
3295 sx = a.getFloat(attr, 1f);
3296 transformSet = true;
3297 break;
3298 case com.android.internal.R.styleable.View_scaleY:
3299 sy = a.getFloat(attr, 1f);
3300 transformSet = true;
3301 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003302 case com.android.internal.R.styleable.View_id:
3303 mID = a.getResourceId(attr, NO_ID);
3304 break;
3305 case com.android.internal.R.styleable.View_tag:
3306 mTag = a.getText(attr);
3307 break;
3308 case com.android.internal.R.styleable.View_fitsSystemWindows:
3309 if (a.getBoolean(attr, false)) {
3310 viewFlagValues |= FITS_SYSTEM_WINDOWS;
3311 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
3312 }
3313 break;
3314 case com.android.internal.R.styleable.View_focusable:
3315 if (a.getBoolean(attr, false)) {
3316 viewFlagValues |= FOCUSABLE;
3317 viewFlagMasks |= FOCUSABLE_MASK;
3318 }
3319 break;
3320 case com.android.internal.R.styleable.View_focusableInTouchMode:
3321 if (a.getBoolean(attr, false)) {
3322 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
3323 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
3324 }
3325 break;
3326 case com.android.internal.R.styleable.View_clickable:
3327 if (a.getBoolean(attr, false)) {
3328 viewFlagValues |= CLICKABLE;
3329 viewFlagMasks |= CLICKABLE;
3330 }
3331 break;
3332 case com.android.internal.R.styleable.View_longClickable:
3333 if (a.getBoolean(attr, false)) {
3334 viewFlagValues |= LONG_CLICKABLE;
3335 viewFlagMasks |= LONG_CLICKABLE;
3336 }
3337 break;
3338 case com.android.internal.R.styleable.View_saveEnabled:
3339 if (!a.getBoolean(attr, true)) {
3340 viewFlagValues |= SAVE_DISABLED;
3341 viewFlagMasks |= SAVE_DISABLED_MASK;
3342 }
3343 break;
3344 case com.android.internal.R.styleable.View_duplicateParentState:
3345 if (a.getBoolean(attr, false)) {
3346 viewFlagValues |= DUPLICATE_PARENT_STATE;
3347 viewFlagMasks |= DUPLICATE_PARENT_STATE;
3348 }
3349 break;
3350 case com.android.internal.R.styleable.View_visibility:
3351 final int visibility = a.getInt(attr, 0);
3352 if (visibility != 0) {
3353 viewFlagValues |= VISIBILITY_FLAGS[visibility];
3354 viewFlagMasks |= VISIBILITY_MASK;
3355 }
3356 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003357 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003358 // Clear any layout direction flags (included resolved bits) already set
3359 mPrivateFlags2 &= ~(LAYOUT_DIRECTION_MASK | LAYOUT_DIRECTION_RESOLVED_MASK);
3360 // Set the layout direction flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003361 final int layoutDirection = a.getInt(attr, -1);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003362 final int value = (layoutDirection != -1) ?
3363 LAYOUT_DIRECTION_FLAGS[layoutDirection] : LAYOUT_DIRECTION_DEFAULT;
3364 mPrivateFlags2 |= (value << LAYOUT_DIRECTION_MASK_SHIFT);
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07003365 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003366 case com.android.internal.R.styleable.View_drawingCacheQuality:
3367 final int cacheQuality = a.getInt(attr, 0);
3368 if (cacheQuality != 0) {
3369 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
3370 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
3371 }
3372 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07003373 case com.android.internal.R.styleable.View_contentDescription:
3374 mContentDescription = a.getString(attr);
3375 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003376 case com.android.internal.R.styleable.View_soundEffectsEnabled:
3377 if (!a.getBoolean(attr, true)) {
3378 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
3379 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
3380 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003381 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003382 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
3383 if (!a.getBoolean(attr, true)) {
3384 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
3385 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
3386 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003387 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003388 case R.styleable.View_scrollbars:
3389 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
3390 if (scrollbars != SCROLLBARS_NONE) {
3391 viewFlagValues |= scrollbars;
3392 viewFlagMasks |= SCROLLBARS_MASK;
3393 initializeScrollbars(a);
3394 }
3395 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07003396 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003397 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07003398 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
3399 // Ignore the attribute starting with ICS
3400 break;
3401 }
3402 // With builds < ICS, fall through and apply fading edges
3403 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003404 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
3405 if (fadingEdge != FADING_EDGE_NONE) {
3406 viewFlagValues |= fadingEdge;
3407 viewFlagMasks |= FADING_EDGE_MASK;
3408 initializeFadingEdge(a);
3409 }
3410 break;
3411 case R.styleable.View_scrollbarStyle:
3412 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3413 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3414 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3415 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3416 }
3417 break;
3418 case R.styleable.View_isScrollContainer:
3419 setScrollContainer = true;
3420 if (a.getBoolean(attr, false)) {
3421 setScrollContainer(true);
3422 }
3423 break;
3424 case com.android.internal.R.styleable.View_keepScreenOn:
3425 if (a.getBoolean(attr, false)) {
3426 viewFlagValues |= KEEP_SCREEN_ON;
3427 viewFlagMasks |= KEEP_SCREEN_ON;
3428 }
3429 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003430 case R.styleable.View_filterTouchesWhenObscured:
3431 if (a.getBoolean(attr, false)) {
3432 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3433 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3434 }
3435 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003436 case R.styleable.View_nextFocusLeft:
3437 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3438 break;
3439 case R.styleable.View_nextFocusRight:
3440 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3441 break;
3442 case R.styleable.View_nextFocusUp:
3443 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3444 break;
3445 case R.styleable.View_nextFocusDown:
3446 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3447 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003448 case R.styleable.View_nextFocusForward:
3449 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3450 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003451 case R.styleable.View_minWidth:
3452 mMinWidth = a.getDimensionPixelSize(attr, 0);
3453 break;
3454 case R.styleable.View_minHeight:
3455 mMinHeight = a.getDimensionPixelSize(attr, 0);
3456 break;
Romain Guy9a817362009-05-01 10:57:14 -07003457 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003458 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003459 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003460 + "be used within a restricted context");
3461 }
3462
Romain Guy9a817362009-05-01 10:57:14 -07003463 final String handlerName = a.getString(attr);
3464 if (handlerName != null) {
3465 setOnClickListener(new OnClickListener() {
3466 private Method mHandler;
3467
3468 public void onClick(View v) {
3469 if (mHandler == null) {
3470 try {
3471 mHandler = getContext().getClass().getMethod(handlerName,
3472 View.class);
3473 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003474 int id = getId();
3475 String idText = id == NO_ID ? "" : " with id '"
3476 + getContext().getResources().getResourceEntryName(
3477 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003478 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003479 handlerName + "(View) in the activity "
3480 + getContext().getClass() + " for onClick handler"
3481 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003482 }
3483 }
3484
3485 try {
3486 mHandler.invoke(getContext(), View.this);
3487 } catch (IllegalAccessException e) {
3488 throw new IllegalStateException("Could not execute non "
3489 + "public method of the activity", e);
3490 } catch (InvocationTargetException e) {
3491 throw new IllegalStateException("Could not execute "
3492 + "method of the activity", e);
3493 }
3494 }
3495 });
3496 }
3497 break;
Adam Powell637d3372010-08-25 14:37:03 -07003498 case R.styleable.View_overScrollMode:
3499 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3500 break;
Adam Powell20232d02010-12-08 21:08:53 -08003501 case R.styleable.View_verticalScrollbarPosition:
3502 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3503 break;
Romain Guy171c5922011-01-06 10:04:23 -08003504 case R.styleable.View_layerType:
3505 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3506 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003507 case R.styleable.View_textDirection:
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003508 // Clear any text direction flag already set
3509 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
3510 // Set the text direction flags depending on the value of the attribute
3511 final int textDirection = a.getInt(attr, -1);
3512 if (textDirection != -1) {
3513 mPrivateFlags2 |= TEXT_DIRECTION_FLAGS[textDirection];
3514 }
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003515 break;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003516 case R.styleable.View_textAlignment:
3517 // Clear any text alignment flag already set
3518 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
3519 // Set the text alignment flag depending on the value of the attribute
3520 final int textAlignment = a.getInt(attr, TEXT_ALIGNMENT_DEFAULT);
3521 mPrivateFlags2 |= TEXT_ALIGNMENT_FLAGS[textAlignment];
3522 break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07003523 case R.styleable.View_importantForAccessibility:
3524 setImportantForAccessibility(a.getInt(attr,
3525 IMPORTANT_FOR_ACCESSIBILITY_DEFAULT));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003526 }
3527 }
3528
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003529 a.recycle();
3530
Adam Powell637d3372010-08-25 14:37:03 -07003531 setOverScrollMode(overScrollMode);
3532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003533 if (background != null) {
Philip Milne6c8ea062012-04-03 17:38:43 -07003534 setBackground(background);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003535 }
3536
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003537 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3538 // layout direction). Those cached values will be used later during padding resolution.
3539 mUserPaddingStart = startPadding;
3540 mUserPaddingEnd = endPadding;
3541
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003542 updateUserPaddingRelative();
3543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003544 if (padding >= 0) {
3545 leftPadding = padding;
3546 topPadding = padding;
3547 rightPadding = padding;
3548 bottomPadding = padding;
3549 }
3550
3551 // If the user specified the padding (either with android:padding or
3552 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3553 // use the default padding or the padding from the background drawable
3554 // (stored at this point in mPadding*)
3555 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3556 topPadding >= 0 ? topPadding : mPaddingTop,
3557 rightPadding >= 0 ? rightPadding : mPaddingRight,
3558 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3559
3560 if (viewFlagMasks != 0) {
3561 setFlags(viewFlagValues, viewFlagMasks);
3562 }
3563
3564 // 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
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003590 private void updateUserPaddingRelative() {
3591 mUserPaddingRelative = (mUserPaddingStart >= 0 || mUserPaddingEnd >= 0);
3592 }
3593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003594 /**
3595 * Non-public constructor for use in testing
3596 */
3597 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003598 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003599 }
3600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003601 /**
3602 * <p>
3603 * Initializes the fading edges from a given set of styled attributes. This
3604 * method should be called by subclasses that need fading edges and when an
3605 * instance of these subclasses is created programmatically rather than
3606 * being inflated from XML. This method is automatically called when the XML
3607 * is inflated.
3608 * </p>
3609 *
3610 * @param a the styled attributes set to initialize the fading edges from
3611 */
3612 protected void initializeFadingEdge(TypedArray a) {
3613 initScrollCache();
3614
3615 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3616 R.styleable.View_fadingEdgeLength,
3617 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3618 }
3619
3620 /**
3621 * Returns the size of the vertical faded edges used to indicate that more
3622 * content in this view is visible.
3623 *
3624 * @return The size in pixels of the vertical faded edge or 0 if vertical
3625 * faded edges are not enabled for this view.
3626 * @attr ref android.R.styleable#View_fadingEdgeLength
3627 */
3628 public int getVerticalFadingEdgeLength() {
3629 if (isVerticalFadingEdgeEnabled()) {
3630 ScrollabilityCache cache = mScrollCache;
3631 if (cache != null) {
3632 return cache.fadingEdgeLength;
3633 }
3634 }
3635 return 0;
3636 }
3637
3638 /**
3639 * Set the size of the faded edge used to indicate that more content in this
3640 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003641 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3642 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3643 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003644 *
3645 * @param length The size in pixels of the faded edge used to indicate that more
3646 * content in this view is visible.
3647 */
3648 public void setFadingEdgeLength(int length) {
3649 initScrollCache();
3650 mScrollCache.fadingEdgeLength = length;
3651 }
3652
3653 /**
3654 * Returns the size of the horizontal faded edges used to indicate that more
3655 * content in this view is visible.
3656 *
3657 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3658 * faded edges are not enabled for this view.
3659 * @attr ref android.R.styleable#View_fadingEdgeLength
3660 */
3661 public int getHorizontalFadingEdgeLength() {
3662 if (isHorizontalFadingEdgeEnabled()) {
3663 ScrollabilityCache cache = mScrollCache;
3664 if (cache != null) {
3665 return cache.fadingEdgeLength;
3666 }
3667 }
3668 return 0;
3669 }
3670
3671 /**
3672 * Returns the width of the vertical scrollbar.
3673 *
3674 * @return The width in pixels of the vertical scrollbar or 0 if there
3675 * is no vertical scrollbar.
3676 */
3677 public int getVerticalScrollbarWidth() {
3678 ScrollabilityCache cache = mScrollCache;
3679 if (cache != null) {
3680 ScrollBarDrawable scrollBar = cache.scrollBar;
3681 if (scrollBar != null) {
3682 int size = scrollBar.getSize(true);
3683 if (size <= 0) {
3684 size = cache.scrollBarSize;
3685 }
3686 return size;
3687 }
3688 return 0;
3689 }
3690 return 0;
3691 }
3692
3693 /**
3694 * Returns the height of the horizontal scrollbar.
3695 *
3696 * @return The height in pixels of the horizontal scrollbar or 0 if
3697 * there is no horizontal scrollbar.
3698 */
3699 protected int getHorizontalScrollbarHeight() {
3700 ScrollabilityCache cache = mScrollCache;
3701 if (cache != null) {
3702 ScrollBarDrawable scrollBar = cache.scrollBar;
3703 if (scrollBar != null) {
3704 int size = scrollBar.getSize(false);
3705 if (size <= 0) {
3706 size = cache.scrollBarSize;
3707 }
3708 return size;
3709 }
3710 return 0;
3711 }
3712 return 0;
3713 }
3714
3715 /**
3716 * <p>
3717 * Initializes the scrollbars from a given set of styled attributes. This
3718 * method should be called by subclasses that need scrollbars and when an
3719 * instance of these subclasses is created programmatically rather than
3720 * being inflated from XML. This method is automatically called when the XML
3721 * is inflated.
3722 * </p>
3723 *
3724 * @param a the styled attributes set to initialize the scrollbars from
3725 */
3726 protected void initializeScrollbars(TypedArray a) {
3727 initScrollCache();
3728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003729 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003730
Mike Cleronf116bf82009-09-27 19:14:12 -07003731 if (scrollabilityCache.scrollBar == null) {
3732 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3733 }
Joe Malin32736f02011-01-19 16:14:20 -08003734
Romain Guy8bda2482010-03-02 11:42:11 -08003735 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003736
Mike Cleronf116bf82009-09-27 19:14:12 -07003737 if (!fadeScrollbars) {
3738 scrollabilityCache.state = ScrollabilityCache.ON;
3739 }
3740 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003741
3742
Mike Cleronf116bf82009-09-27 19:14:12 -07003743 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3744 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3745 .getScrollBarFadeDuration());
3746 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3747 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3748 ViewConfiguration.getScrollDefaultDelay());
3749
Joe Malin32736f02011-01-19 16:14:20 -08003750
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003751 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3752 com.android.internal.R.styleable.View_scrollbarSize,
3753 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3754
3755 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3756 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3757
3758 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3759 if (thumb != null) {
3760 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3761 }
3762
3763 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3764 false);
3765 if (alwaysDraw) {
3766 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3767 }
3768
3769 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3770 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3771
3772 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3773 if (thumb != null) {
3774 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3775 }
3776
3777 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3778 false);
3779 if (alwaysDraw) {
3780 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3781 }
3782
3783 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003784 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003785 }
3786
3787 /**
3788 * <p>
3789 * Initalizes the scrollability cache if necessary.
3790 * </p>
3791 */
3792 private void initScrollCache() {
3793 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003794 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003795 }
3796 }
3797
Philip Milne6c8ea062012-04-03 17:38:43 -07003798 private ScrollabilityCache getScrollCache() {
3799 initScrollCache();
3800 return mScrollCache;
3801 }
3802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003803 /**
Adam Powell20232d02010-12-08 21:08:53 -08003804 * Set the position of the vertical scroll bar. Should be one of
3805 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3806 * {@link #SCROLLBAR_POSITION_RIGHT}.
3807 *
3808 * @param position Where the vertical scroll bar should be positioned.
3809 */
3810 public void setVerticalScrollbarPosition(int position) {
3811 if (mVerticalScrollbarPosition != position) {
3812 mVerticalScrollbarPosition = position;
3813 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003814 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003815 }
3816 }
3817
3818 /**
3819 * @return The position where the vertical scroll bar will show, if applicable.
3820 * @see #setVerticalScrollbarPosition(int)
3821 */
3822 public int getVerticalScrollbarPosition() {
3823 return mVerticalScrollbarPosition;
3824 }
3825
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003826 ListenerInfo getListenerInfo() {
3827 if (mListenerInfo != null) {
3828 return mListenerInfo;
3829 }
3830 mListenerInfo = new ListenerInfo();
3831 return mListenerInfo;
3832 }
3833
Adam Powell20232d02010-12-08 21:08:53 -08003834 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003835 * Register a callback to be invoked when focus of this view changed.
3836 *
3837 * @param l The callback that will run.
3838 */
3839 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003840 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003841 }
3842
3843 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003844 * Add a listener that will be called when the bounds of the view change due to
3845 * layout processing.
3846 *
3847 * @param listener The listener that will be called when layout bounds change.
3848 */
3849 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003850 ListenerInfo li = getListenerInfo();
3851 if (li.mOnLayoutChangeListeners == null) {
3852 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003853 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003854 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3855 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003856 }
Chet Haase21cd1382010-09-01 17:42:29 -07003857 }
3858
3859 /**
3860 * Remove a listener for layout changes.
3861 *
3862 * @param listener The listener for layout bounds change.
3863 */
3864 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003865 ListenerInfo li = mListenerInfo;
3866 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003867 return;
3868 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003869 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003870 }
3871
3872 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003873 * Add a listener for attach state changes.
3874 *
3875 * This listener will be called whenever this view is attached or detached
3876 * from a window. Remove the listener using
3877 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3878 *
3879 * @param listener Listener to attach
3880 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3881 */
3882 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003883 ListenerInfo li = getListenerInfo();
3884 if (li.mOnAttachStateChangeListeners == null) {
3885 li.mOnAttachStateChangeListeners
3886 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003887 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003888 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003889 }
3890
3891 /**
3892 * Remove a listener for attach state changes. The listener will receive no further
3893 * notification of window attach/detach events.
3894 *
3895 * @param listener Listener to remove
3896 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3897 */
3898 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003899 ListenerInfo li = mListenerInfo;
3900 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003901 return;
3902 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003903 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003904 }
3905
3906 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003907 * Returns the focus-change callback registered for this view.
3908 *
3909 * @return The callback, or null if one is not registered.
3910 */
3911 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003912 ListenerInfo li = mListenerInfo;
3913 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003914 }
3915
3916 /**
3917 * Register a callback to be invoked when this view is clicked. If this view is not
3918 * clickable, it becomes clickable.
3919 *
3920 * @param l The callback that will run
3921 *
3922 * @see #setClickable(boolean)
3923 */
3924 public void setOnClickListener(OnClickListener l) {
3925 if (!isClickable()) {
3926 setClickable(true);
3927 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003928 getListenerInfo().mOnClickListener = l;
3929 }
3930
3931 /**
3932 * Return whether this view has an attached OnClickListener. Returns
3933 * true if there is a listener, false if there is none.
3934 */
3935 public boolean hasOnClickListeners() {
3936 ListenerInfo li = mListenerInfo;
3937 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003938 }
3939
3940 /**
3941 * Register a callback to be invoked when this view is clicked and held. If this view is not
3942 * long clickable, it becomes long clickable.
3943 *
3944 * @param l The callback that will run
3945 *
3946 * @see #setLongClickable(boolean)
3947 */
3948 public void setOnLongClickListener(OnLongClickListener l) {
3949 if (!isLongClickable()) {
3950 setLongClickable(true);
3951 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003952 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003953 }
3954
3955 /**
3956 * Register a callback to be invoked when the context menu for this view is
3957 * being built. If this view is not long clickable, it becomes long clickable.
3958 *
3959 * @param l The callback that will run
3960 *
3961 */
3962 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3963 if (!isLongClickable()) {
3964 setLongClickable(true);
3965 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003966 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003967 }
3968
3969 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003970 * Call this view's OnClickListener, if it is defined. Performs all normal
3971 * actions associated with clicking: reporting accessibility event, playing
3972 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003973 *
3974 * @return True there was an assigned OnClickListener that was called, false
3975 * otherwise is returned.
3976 */
3977 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003978 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3979
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003980 ListenerInfo li = mListenerInfo;
3981 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003982 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003983 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003984 return true;
3985 }
3986
3987 return false;
3988 }
3989
3990 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003991 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
3992 * this only calls the listener, and does not do any associated clicking
3993 * actions like reporting an accessibility event.
3994 *
3995 * @return True there was an assigned OnClickListener that was called, false
3996 * otherwise is returned.
3997 */
3998 public boolean callOnClick() {
3999 ListenerInfo li = mListenerInfo;
4000 if (li != null && li.mOnClickListener != null) {
4001 li.mOnClickListener.onClick(this);
4002 return true;
4003 }
4004 return false;
4005 }
4006
4007 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004008 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
4009 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004010 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004011 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004012 */
4013 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07004014 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
4015
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004016 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004017 ListenerInfo li = mListenerInfo;
4018 if (li != null && li.mOnLongClickListener != null) {
4019 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004020 }
4021 if (!handled) {
4022 handled = showContextMenu();
4023 }
4024 if (handled) {
4025 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
4026 }
4027 return handled;
4028 }
4029
4030 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004031 * Performs button-related actions during a touch down event.
4032 *
4033 * @param event The event.
4034 * @return True if the down was consumed.
4035 *
4036 * @hide
4037 */
4038 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
4039 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
4040 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
4041 return true;
4042 }
4043 }
4044 return false;
4045 }
4046
4047 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004048 * Bring up the context menu for this view.
4049 *
4050 * @return Whether a context menu was displayed.
4051 */
4052 public boolean showContextMenu() {
4053 return getParent().showContextMenuForChild(this);
4054 }
4055
4056 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004057 * Bring up the context menu for this view, referring to the item under the specified point.
4058 *
4059 * @param x The referenced x coordinate.
4060 * @param y The referenced y coordinate.
4061 * @param metaState The keyboard modifiers that were pressed.
4062 * @return Whether a context menu was displayed.
4063 *
4064 * @hide
4065 */
4066 public boolean showContextMenu(float x, float y, int metaState) {
4067 return showContextMenu();
4068 }
4069
4070 /**
Adam Powell6e346362010-07-23 10:18:23 -07004071 * Start an action mode.
4072 *
4073 * @param callback Callback that will control the lifecycle of the action mode
4074 * @return The new action mode if it is started, null otherwise
4075 *
4076 * @see ActionMode
4077 */
4078 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08004079 ViewParent parent = getParent();
4080 if (parent == null) return null;
4081 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07004082 }
4083
4084 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004085 * Register a callback to be invoked when a key is pressed in this view.
4086 * @param l the key listener to attach to this view
4087 */
4088 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004089 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004090 }
4091
4092 /**
4093 * Register a callback to be invoked when a touch event is sent to this view.
4094 * @param l the touch listener to attach to this view
4095 */
4096 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004097 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004098 }
4099
4100 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004101 * Register a callback to be invoked when a generic motion event is sent to this view.
4102 * @param l the generic motion listener to attach to this view
4103 */
4104 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004105 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08004106 }
4107
4108 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07004109 * Register a callback to be invoked when a hover event is sent to this view.
4110 * @param l the hover listener to attach to this view
4111 */
4112 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004113 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07004114 }
4115
4116 /**
Joe Malin32736f02011-01-19 16:14:20 -08004117 * Register a drag event listener callback object for this View. The parameter is
4118 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
4119 * View, the system calls the
4120 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
4121 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07004122 */
4123 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004124 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07004125 }
4126
4127 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07004128 * Give this view focus. This will cause
4129 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004130 *
4131 * Note: this does not check whether this {@link View} should get focus, it just
4132 * gives it focus no matter what. It should only be called internally by framework
4133 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
4134 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004135 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
4136 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004137 * focus moved when requestFocus() is called. It may not always
4138 * apply, in which case use the default View.FOCUS_DOWN.
4139 * @param previouslyFocusedRect The rectangle of the view that had focus
4140 * prior in this View's coordinate system.
4141 */
4142 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
4143 if (DBG) {
4144 System.out.println(this + " requestFocus()");
4145 }
4146
4147 if ((mPrivateFlags & FOCUSED) == 0) {
4148 mPrivateFlags |= FOCUSED;
4149
4150 if (mParent != null) {
4151 mParent.requestChildFocus(this, this);
4152 }
4153
4154 onFocusChanged(true, direction, previouslyFocusedRect);
4155 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004156
4157 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4158 notifyAccessibilityStateChanged();
4159 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004160 }
4161 }
4162
4163 /**
4164 * Request that a rectangle of this view be visible on the screen,
4165 * scrolling if necessary just enough.
4166 *
4167 * <p>A View should call this if it maintains some notion of which part
4168 * of its content is interesting. For example, a text editing view
4169 * should call this when its cursor moves.
4170 *
4171 * @param rectangle The rectangle.
4172 * @return Whether any parent scrolled.
4173 */
4174 public boolean requestRectangleOnScreen(Rect rectangle) {
4175 return requestRectangleOnScreen(rectangle, false);
4176 }
4177
4178 /**
4179 * Request that a rectangle of this view be visible on the screen,
4180 * scrolling if necessary just enough.
4181 *
4182 * <p>A View should call this if it maintains some notion of which part
4183 * of its content is interesting. For example, a text editing view
4184 * should call this when its cursor moves.
4185 *
4186 * <p>When <code>immediate</code> is set to true, scrolling will not be
4187 * animated.
4188 *
4189 * @param rectangle The rectangle.
4190 * @param immediate True to forbid animated scrolling, false otherwise
4191 * @return Whether any parent scrolled.
4192 */
4193 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
4194 View child = this;
4195 ViewParent parent = mParent;
4196 boolean scrolled = false;
4197 while (parent != null) {
4198 scrolled |= parent.requestChildRectangleOnScreen(child,
4199 rectangle, immediate);
4200
4201 // offset rect so next call has the rectangle in the
4202 // coordinate system of its direct child.
4203 rectangle.offset(child.getLeft(), child.getTop());
4204 rectangle.offset(-child.getScrollX(), -child.getScrollY());
4205
4206 if (!(parent instanceof View)) {
4207 break;
4208 }
Romain Guy8506ab42009-06-11 17:35:47 -07004209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004210 child = (View) parent;
4211 parent = child.getParent();
4212 }
4213 return scrolled;
4214 }
4215
4216 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004217 * Called when this view wants to give up focus. If focus is cleared
4218 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
4219 * <p>
4220 * <strong>Note:</strong> When a View clears focus the framework is trying
4221 * to give focus to the first focusable View from the top. Hence, if this
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004222 * View is the first from the top that can take focus, then all callbacks
4223 * related to clearing focus will be invoked after wich the framework will
4224 * give focus to this view.
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004225 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004226 */
4227 public void clearFocus() {
4228 if (DBG) {
4229 System.out.println(this + " clearFocus()");
4230 }
4231
4232 if ((mPrivateFlags & FOCUSED) != 0) {
4233 mPrivateFlags &= ~FOCUSED;
4234
4235 if (mParent != null) {
4236 mParent.clearChildFocus(this);
4237 }
4238
4239 onFocusChanged(false, 0, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004241 refreshDrawableState();
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004242
4243 ensureInputFocusOnFirstFocusable();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004244
4245 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4246 notifyAccessibilityStateChanged();
4247 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004248 }
4249 }
4250
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004251 void ensureInputFocusOnFirstFocusable() {
4252 View root = getRootView();
4253 if (root != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004254 root.requestFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004255 }
4256 }
4257
4258 /**
4259 * Called internally by the view system when a new view is getting focus.
4260 * This is what clears the old focus.
4261 */
4262 void unFocus() {
4263 if (DBG) {
4264 System.out.println(this + " unFocus()");
4265 }
4266
4267 if ((mPrivateFlags & FOCUSED) != 0) {
4268 mPrivateFlags &= ~FOCUSED;
4269
4270 onFocusChanged(false, 0, null);
4271 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004272
4273 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4274 notifyAccessibilityStateChanged();
4275 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004276 }
4277 }
4278
4279 /**
4280 * Returns true if this view has focus iteself, or is the ancestor of the
4281 * view that has focus.
4282 *
4283 * @return True if this view has or contains focus, false otherwise.
4284 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004285 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004286 public boolean hasFocus() {
4287 return (mPrivateFlags & FOCUSED) != 0;
4288 }
4289
4290 /**
4291 * Returns true if this view is focusable or if it contains a reachable View
4292 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
4293 * is a View whose parents do not block descendants focus.
4294 *
4295 * Only {@link #VISIBLE} views are considered focusable.
4296 *
4297 * @return True if the view is focusable or if the view contains a focusable
4298 * View, false otherwise.
4299 *
4300 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
4301 */
4302 public boolean hasFocusable() {
4303 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4304 }
4305
4306 /**
4307 * Called by the view system when the focus state of this view changes.
4308 * When the focus change event is caused by directional navigation, direction
4309 * and previouslyFocusedRect provide insight into where the focus is coming from.
4310 * When overriding, be sure to call up through to the super class so that
4311 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004312 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004313 * @param gainFocus True if the View has focus; false otherwise.
4314 * @param direction The direction focus has moved when requestFocus()
4315 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004316 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4317 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4318 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004319 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4320 * system, of the previously focused view. If applicable, this will be
4321 * passed in as finer grained information about where the focus is coming
4322 * from (in addition to direction). Will be <code>null</code> otherwise.
4323 */
4324 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004325 if (gainFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004326 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4327 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004328 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004329 }
4330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004331 InputMethodManager imm = InputMethodManager.peekInstance();
4332 if (!gainFocus) {
4333 if (isPressed()) {
4334 setPressed(false);
4335 }
4336 if (imm != null && mAttachInfo != null
4337 && mAttachInfo.mHasWindowFocus) {
4338 imm.focusOut(this);
4339 }
Romain Guya2431d02009-04-30 16:30:00 -07004340 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004341 } else if (imm != null && mAttachInfo != null
4342 && mAttachInfo.mHasWindowFocus) {
4343 imm.focusIn(this);
4344 }
Romain Guy8506ab42009-06-11 17:35:47 -07004345
Romain Guy0fd89bf2011-01-26 15:41:30 -08004346 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004347 ListenerInfo li = mListenerInfo;
4348 if (li != null && li.mOnFocusChangeListener != null) {
4349 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004350 }
Joe Malin32736f02011-01-19 16:14:20 -08004351
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004352 if (mAttachInfo != null) {
4353 mAttachInfo.mKeyDispatchState.reset(this);
4354 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004355 }
4356
4357 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004358 * Sends an accessibility event of the given type. If accessiiblity is
4359 * not enabled this method has no effect. The default implementation calls
4360 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4361 * to populate information about the event source (this View), then calls
4362 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4363 * populate the text content of the event source including its descendants,
4364 * and last calls
4365 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4366 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004367 * <p>
4368 * If an {@link AccessibilityDelegate} has been specified via calling
4369 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4370 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4371 * responsible for handling this call.
4372 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004373 *
Scott Mainb303d832011-10-12 16:45:18 -07004374 * @param eventType The type of the event to send, as defined by several types from
4375 * {@link android.view.accessibility.AccessibilityEvent}, such as
4376 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4377 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004378 *
4379 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4380 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4381 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004382 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004383 */
4384 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004385 if (mAccessibilityDelegate != null) {
4386 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4387 } else {
4388 sendAccessibilityEventInternal(eventType);
4389 }
4390 }
4391
4392 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004393 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4394 * {@link AccessibilityEvent} to make an announcement which is related to some
4395 * sort of a context change for which none of the events representing UI transitions
4396 * is a good fit. For example, announcing a new page in a book. If accessibility
4397 * is not enabled this method does nothing.
4398 *
4399 * @param text The announcement text.
4400 */
4401 public void announceForAccessibility(CharSequence text) {
4402 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4403 AccessibilityEvent event = AccessibilityEvent.obtain(
4404 AccessibilityEvent.TYPE_ANNOUNCEMENT);
4405 event.getText().add(text);
4406 sendAccessibilityEventUnchecked(event);
4407 }
4408 }
4409
4410 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004411 * @see #sendAccessibilityEvent(int)
4412 *
4413 * Note: Called from the default {@link AccessibilityDelegate}.
4414 */
4415 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004416 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4417 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4418 }
4419 }
4420
4421 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004422 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4423 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004424 * perform a check whether accessibility is enabled.
4425 * <p>
4426 * If an {@link AccessibilityDelegate} has been specified via calling
4427 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4428 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4429 * is responsible for handling this call.
4430 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004431 *
4432 * @param event The event to send.
4433 *
4434 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004435 */
4436 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004437 if (mAccessibilityDelegate != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004438 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004439 } else {
4440 sendAccessibilityEventUncheckedInternal(event);
4441 }
4442 }
4443
4444 /**
4445 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4446 *
4447 * Note: Called from the default {@link AccessibilityDelegate}.
4448 */
4449 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004450 if (!isShown()) {
4451 return;
4452 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004453 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004454 // Only a subset of accessibility events populates text content.
4455 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4456 dispatchPopulateAccessibilityEvent(event);
4457 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07004458 // Intercept accessibility focus events fired by virtual nodes to keep
4459 // track of accessibility focus position in such nodes.
4460 final int eventType = event.getEventType();
4461 switch (eventType) {
4462 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED: {
4463 final long virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
4464 event.getSourceNodeId());
4465 if (virtualNodeId != AccessibilityNodeInfo.UNDEFINED) {
4466 ViewRootImpl viewRootImpl = getViewRootImpl();
4467 if (viewRootImpl != null) {
4468 viewRootImpl.setAccessibilityFocusedHost(this);
4469 }
4470 }
4471 } break;
4472 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED: {
4473 final long virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
4474 event.getSourceNodeId());
4475 if (virtualNodeId != AccessibilityNodeInfo.UNDEFINED) {
4476 ViewRootImpl viewRootImpl = getViewRootImpl();
4477 if (viewRootImpl != null) {
4478 viewRootImpl.setAccessibilityFocusedHost(null);
4479 }
4480 }
4481 } break;
4482 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004483 // In the beginning we called #isShown(), so we know that getParent() is not null.
4484 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004485 }
4486
4487 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004488 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4489 * to its children for adding their text content to the event. Note that the
4490 * event text is populated in a separate dispatch path since we add to the
4491 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004492 * A typical implementation will call
4493 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4494 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4495 * on each child. Override this method if custom population of the event text
4496 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004497 * <p>
4498 * If an {@link AccessibilityDelegate} has been specified via calling
4499 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4500 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4501 * is responsible for handling this call.
4502 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004503 * <p>
4504 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4505 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4506 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004507 *
4508 * @param event The event.
4509 *
4510 * @return True if the event population was completed.
4511 */
4512 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004513 if (mAccessibilityDelegate != null) {
4514 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4515 } else {
4516 return dispatchPopulateAccessibilityEventInternal(event);
4517 }
4518 }
4519
4520 /**
4521 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4522 *
4523 * Note: Called from the default {@link AccessibilityDelegate}.
4524 */
4525 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004526 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004527 return false;
4528 }
4529
4530 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004531 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004532 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004533 * text content. While this method is free to modify event
4534 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004535 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4536 * <p>
4537 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004538 * to the text added by the super implementation:
4539 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004540 * super.onPopulateAccessibilityEvent(event);
4541 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4542 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4543 * mCurrentDate.getTimeInMillis(), flags);
4544 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004545 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004546 * <p>
4547 * If an {@link AccessibilityDelegate} has been specified via calling
4548 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4549 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4550 * is responsible for handling this call.
4551 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004552 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4553 * information to the event, in case the default implementation has basic information to add.
4554 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004555 *
4556 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004557 *
4558 * @see #sendAccessibilityEvent(int)
4559 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004560 */
4561 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004562 if (mAccessibilityDelegate != null) {
4563 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4564 } else {
4565 onPopulateAccessibilityEventInternal(event);
4566 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004567 }
4568
4569 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004570 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4571 *
4572 * Note: Called from the default {@link AccessibilityDelegate}.
4573 */
4574 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4575
4576 }
4577
4578 /**
4579 * Initializes an {@link AccessibilityEvent} with information about
4580 * this View which is the event source. In other words, the source of
4581 * an accessibility event is the view whose state change triggered firing
4582 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004583 * <p>
4584 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004585 * to properties set by the super implementation:
4586 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4587 * super.onInitializeAccessibilityEvent(event);
4588 * event.setPassword(true);
4589 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004590 * <p>
4591 * If an {@link AccessibilityDelegate} has been specified via calling
4592 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4593 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4594 * is responsible for handling this call.
4595 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004596 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4597 * information to the event, in case the default implementation has basic information to add.
4598 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004599 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004600 *
4601 * @see #sendAccessibilityEvent(int)
4602 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4603 */
4604 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004605 if (mAccessibilityDelegate != null) {
4606 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4607 } else {
4608 onInitializeAccessibilityEventInternal(event);
4609 }
4610 }
4611
4612 /**
4613 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4614 *
4615 * Note: Called from the default {@link AccessibilityDelegate}.
4616 */
4617 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004618 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004619 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004620 event.setPackageName(getContext().getPackageName());
4621 event.setEnabled(isEnabled());
4622 event.setContentDescription(mContentDescription);
4623
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004624 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004625 ArrayList<View> focusablesTempList = mAttachInfo.mTempArrayList;
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004626 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4627 FOCUSABLES_ALL);
4628 event.setItemCount(focusablesTempList.size());
4629 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4630 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004631 }
4632 }
4633
4634 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004635 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4636 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4637 * This method is responsible for obtaining an accessibility node info from a
4638 * pool of reusable instances and calling
4639 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4640 * initialize the former.
4641 * <p>
4642 * Note: The client is responsible for recycling the obtained instance by calling
4643 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4644 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004645 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004646 * @return A populated {@link AccessibilityNodeInfo}.
4647 *
4648 * @see AccessibilityNodeInfo
4649 */
4650 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004651 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4652 if (provider != null) {
4653 return provider.createAccessibilityNodeInfo(View.NO_ID);
4654 } else {
4655 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4656 onInitializeAccessibilityNodeInfo(info);
4657 return info;
4658 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004659 }
4660
4661 /**
4662 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4663 * The base implementation sets:
4664 * <ul>
4665 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004666 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4667 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004668 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4669 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4670 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4671 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4672 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4673 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4674 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4675 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4676 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4677 * </ul>
4678 * <p>
4679 * Subclasses should override this method, call the super implementation,
4680 * and set additional attributes.
4681 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004682 * <p>
4683 * If an {@link AccessibilityDelegate} has been specified via calling
4684 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4685 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4686 * is responsible for handling this call.
4687 * </p>
4688 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004689 * @param info The instance to initialize.
4690 */
4691 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004692 if (mAccessibilityDelegate != null) {
4693 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4694 } else {
4695 onInitializeAccessibilityNodeInfoInternal(info);
4696 }
4697 }
4698
4699 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004700 * Gets the location of this view in screen coordintates.
4701 *
4702 * @param outRect The output location
4703 */
4704 private void getBoundsOnScreen(Rect outRect) {
4705 if (mAttachInfo == null) {
4706 return;
4707 }
4708
4709 RectF position = mAttachInfo.mTmpTransformRect;
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004710 position.set(0, 0, mRight - mLeft, mBottom - mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004711
4712 if (!hasIdentityMatrix()) {
4713 getMatrix().mapRect(position);
4714 }
4715
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004716 position.offset(mLeft, mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004717
4718 ViewParent parent = mParent;
4719 while (parent instanceof View) {
4720 View parentView = (View) parent;
4721
4722 position.offset(-parentView.mScrollX, -parentView.mScrollY);
4723
4724 if (!parentView.hasIdentityMatrix()) {
4725 parentView.getMatrix().mapRect(position);
4726 }
4727
4728 position.offset(parentView.mLeft, parentView.mTop);
4729
4730 parent = parentView.mParent;
4731 }
4732
4733 if (parent instanceof ViewRootImpl) {
4734 ViewRootImpl viewRootImpl = (ViewRootImpl) parent;
4735 position.offset(0, -viewRootImpl.mCurScrollY);
4736 }
4737
4738 position.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
4739
4740 outRect.set((int) (position.left + 0.5f), (int) (position.top + 0.5f),
4741 (int) (position.right + 0.5f), (int) (position.bottom + 0.5f));
4742 }
4743
4744 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004745 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4746 *
4747 * Note: Called from the default {@link AccessibilityDelegate}.
4748 */
4749 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004750 Rect bounds = mAttachInfo.mTmpInvalRect;
4751 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004752 info.setBoundsInParent(bounds);
4753
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004754 getBoundsOnScreen(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004755 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004756
Svetoslav Ganovc406be92012-05-11 16:12:32 -07004757 ViewParent parent = getParentForAccessibility();
4758 if (parent instanceof View) {
4759 info.setParent((View) parent);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004760 }
4761
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004762 info.setVisibleToUser(isVisibleToUser());
4763
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004764 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004765 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004766 info.setContentDescription(getContentDescription());
4767
4768 info.setEnabled(isEnabled());
4769 info.setClickable(isClickable());
4770 info.setFocusable(isFocusable());
4771 info.setFocused(isFocused());
Svetoslav Ganov42138042012-03-20 11:51:39 -07004772 info.setAccessibilityFocused(isAccessibilityFocused());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004773 info.setSelected(isSelected());
4774 info.setLongClickable(isLongClickable());
4775
4776 // TODO: These make sense only if we are in an AdapterView but all
4777 // views can be selected. Maybe from accessiiblity perspective
4778 // we should report as selectable view in an AdapterView.
4779 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4780 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4781
4782 if (isFocusable()) {
4783 if (isFocused()) {
4784 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4785 } else {
4786 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4787 }
4788 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004789
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07004790 if (!isAccessibilityFocused()) {
4791 info.addAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
4792 } else {
4793 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
4794 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004795
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07004796 if (isClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004797 info.addAction(AccessibilityNodeInfo.ACTION_CLICK);
4798 }
4799
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07004800 if (isLongClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004801 info.addAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
4802 }
4803
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004804 if (mContentDescription != null && mContentDescription.length() > 0) {
Svetoslav Ganov2b435aa2012-05-04 17:16:37 -07004805 info.addAction(AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY);
4806 info.addAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY);
4807 info.setMovementGranularities(AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004808 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD
4809 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH);
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004810 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004811 }
4812
4813 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004814 * Computes whether this view is visible to the user. Such a view is
4815 * attached, visible, all its predecessors are visible, it is not clipped
4816 * entirely by its predecessors, and has an alpha greater than zero.
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004817 *
4818 * @return Whether the view is visible on the screen.
Guang Zhu0d607fb2012-05-11 19:34:56 -07004819 *
4820 * @hide
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004821 */
Guang Zhu0d607fb2012-05-11 19:34:56 -07004822 protected boolean isVisibleToUser() {
4823 return isVisibleToUser(null);
4824 }
4825
4826 /**
4827 * Computes whether the given portion of this view is visible to the user. Such a view is
4828 * attached, visible, all its predecessors are visible, has an alpha greater than zero, and
4829 * the specified portion is not clipped entirely by its predecessors.
4830 *
4831 * @param boundInView the portion of the view to test; coordinates should be relative; may be
4832 * <code>null</code>, and the entire view will be tested in this case.
4833 * When <code>true</code> is returned by the function, the actual visible
4834 * region will be stored in this parameter; that is, if boundInView is fully
4835 * contained within the view, no modification will be made, otherwise regions
4836 * outside of the visible area of the view will be clipped.
4837 *
4838 * @return Whether the specified portion of the view is visible on the screen.
4839 *
4840 * @hide
4841 */
4842 protected boolean isVisibleToUser(Rect boundInView) {
4843 Rect visibleRect = mAttachInfo.mTmpInvalRect;
4844 Point offset = mAttachInfo.mPoint;
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004845 // The first two checks are made also made by isShown() which
4846 // however traverses the tree up to the parent to catch that.
4847 // Therefore, we do some fail fast check to minimize the up
4848 // tree traversal.
Guang Zhu0d607fb2012-05-11 19:34:56 -07004849 boolean isVisible = mAttachInfo != null
4850 && mAttachInfo.mWindowVisibility == View.VISIBLE
4851 && getAlpha() > 0
4852 && isShown()
4853 && getGlobalVisibleRect(visibleRect, offset);
4854 if (isVisible && boundInView != null) {
4855 visibleRect.offset(-offset.x, -offset.y);
4856 isVisible &= boundInView.intersect(visibleRect);
4857 }
4858 return isVisible;
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004859 }
4860
4861 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004862 * Sets a delegate for implementing accessibility support via compositon as
4863 * opposed to inheritance. The delegate's primary use is for implementing
4864 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4865 *
4866 * @param delegate The delegate instance.
4867 *
4868 * @see AccessibilityDelegate
4869 */
4870 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4871 mAccessibilityDelegate = delegate;
4872 }
4873
4874 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004875 * Gets the provider for managing a virtual view hierarchy rooted at this View
4876 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4877 * that explore the window content.
4878 * <p>
4879 * If this method returns an instance, this instance is responsible for managing
4880 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4881 * View including the one representing the View itself. Similarly the returned
4882 * instance is responsible for performing accessibility actions on any virtual
4883 * view or the root view itself.
4884 * </p>
4885 * <p>
4886 * If an {@link AccessibilityDelegate} has been specified via calling
4887 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4888 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4889 * is responsible for handling this call.
4890 * </p>
4891 *
4892 * @return The provider.
4893 *
4894 * @see AccessibilityNodeProvider
4895 */
4896 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4897 if (mAccessibilityDelegate != null) {
4898 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4899 } else {
4900 return null;
4901 }
4902 }
4903
4904 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004905 * Gets the unique identifier of this view on the screen for accessibility purposes.
4906 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4907 *
4908 * @return The view accessibility id.
4909 *
4910 * @hide
4911 */
4912 public int getAccessibilityViewId() {
4913 if (mAccessibilityViewId == NO_ID) {
4914 mAccessibilityViewId = sNextAccessibilityViewId++;
4915 }
4916 return mAccessibilityViewId;
4917 }
4918
4919 /**
4920 * Gets the unique identifier of the window in which this View reseides.
4921 *
4922 * @return The window accessibility id.
4923 *
4924 * @hide
4925 */
4926 public int getAccessibilityWindowId() {
4927 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4928 }
4929
4930 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004931 * Gets the {@link View} description. It briefly describes the view and is
4932 * primarily used for accessibility support. Set this property to enable
4933 * better accessibility support for your application. This is especially
4934 * true for views that do not have textual representation (For example,
4935 * ImageButton).
4936 *
Svetoslav Ganov42138042012-03-20 11:51:39 -07004937 * @return The content description.
svetoslavganov75986cf2009-05-14 22:28:01 -07004938 *
4939 * @attr ref android.R.styleable#View_contentDescription
4940 */
Svetoslav Ganov42138042012-03-20 11:51:39 -07004941 @ViewDebug.ExportedProperty(category = "accessibility")
svetoslavganov75986cf2009-05-14 22:28:01 -07004942 public CharSequence getContentDescription() {
4943 return mContentDescription;
4944 }
4945
4946 /**
4947 * Sets the {@link View} description. It briefly describes the view and is
4948 * primarily used for accessibility support. Set this property to enable
4949 * better accessibility support for your application. This is especially
4950 * true for views that do not have textual representation (For example,
4951 * ImageButton).
4952 *
4953 * @param contentDescription The content description.
4954 *
4955 * @attr ref android.R.styleable#View_contentDescription
4956 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07004957 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07004958 public void setContentDescription(CharSequence contentDescription) {
4959 mContentDescription = contentDescription;
4960 }
4961
4962 /**
Romain Guya2431d02009-04-30 16:30:00 -07004963 * Invoked whenever this view loses focus, either by losing window focus or by losing
4964 * focus within its window. This method can be used to clear any state tied to the
4965 * focus. For instance, if a button is held pressed with the trackball and the window
4966 * loses focus, this method can be used to cancel the press.
4967 *
4968 * Subclasses of View overriding this method should always call super.onFocusLost().
4969 *
4970 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004971 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004972 *
4973 * @hide pending API council approval
4974 */
4975 protected void onFocusLost() {
4976 resetPressedState();
4977 }
4978
4979 private void resetPressedState() {
4980 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4981 return;
4982 }
4983
4984 if (isPressed()) {
4985 setPressed(false);
4986
4987 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004988 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004989 }
4990 }
4991 }
4992
4993 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004994 * Returns true if this view has focus
4995 *
4996 * @return True if this view has focus, false otherwise.
4997 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004998 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004999 public boolean isFocused() {
5000 return (mPrivateFlags & FOCUSED) != 0;
5001 }
5002
5003 /**
5004 * Find the view in the hierarchy rooted at this view that currently has
5005 * focus.
5006 *
5007 * @return The view that currently has focus, or null if no focused view can
5008 * be found.
5009 */
5010 public View findFocus() {
5011 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
5012 }
5013
5014 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07005015 * Indicates whether this view is one of the set of scrollable containers in
5016 * its window.
5017 *
5018 * @return whether this view is one of the set of scrollable containers in
5019 * its window
5020 *
5021 * @attr ref android.R.styleable#View_isScrollContainer
5022 */
5023 public boolean isScrollContainer() {
5024 return (mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0;
5025 }
5026
5027 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005028 * Change whether this view is one of the set of scrollable containers in
5029 * its window. This will be used to determine whether the window can
5030 * resize or must pan when a soft input area is open -- scrollable
5031 * containers allow the window to use resize mode since the container
5032 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07005033 *
5034 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005035 */
5036 public void setScrollContainer(boolean isScrollContainer) {
5037 if (isScrollContainer) {
5038 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
5039 mAttachInfo.mScrollContainers.add(this);
5040 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
5041 }
5042 mPrivateFlags |= SCROLL_CONTAINER;
5043 } else {
5044 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
5045 mAttachInfo.mScrollContainers.remove(this);
5046 }
5047 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
5048 }
5049 }
5050
5051 /**
5052 * Returns the quality of the drawing cache.
5053 *
5054 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5055 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5056 *
5057 * @see #setDrawingCacheQuality(int)
5058 * @see #setDrawingCacheEnabled(boolean)
5059 * @see #isDrawingCacheEnabled()
5060 *
5061 * @attr ref android.R.styleable#View_drawingCacheQuality
5062 */
5063 public int getDrawingCacheQuality() {
5064 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
5065 }
5066
5067 /**
5068 * Set the drawing cache quality of this view. This value is used only when the
5069 * drawing cache is enabled
5070 *
5071 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5072 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5073 *
5074 * @see #getDrawingCacheQuality()
5075 * @see #setDrawingCacheEnabled(boolean)
5076 * @see #isDrawingCacheEnabled()
5077 *
5078 * @attr ref android.R.styleable#View_drawingCacheQuality
5079 */
5080 public void setDrawingCacheQuality(int quality) {
5081 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
5082 }
5083
5084 /**
5085 * Returns whether the screen should remain on, corresponding to the current
5086 * value of {@link #KEEP_SCREEN_ON}.
5087 *
5088 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
5089 *
5090 * @see #setKeepScreenOn(boolean)
5091 *
5092 * @attr ref android.R.styleable#View_keepScreenOn
5093 */
5094 public boolean getKeepScreenOn() {
5095 return (mViewFlags & KEEP_SCREEN_ON) != 0;
5096 }
5097
5098 /**
5099 * Controls whether the screen should remain on, modifying the
5100 * value of {@link #KEEP_SCREEN_ON}.
5101 *
5102 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
5103 *
5104 * @see #getKeepScreenOn()
5105 *
5106 * @attr ref android.R.styleable#View_keepScreenOn
5107 */
5108 public void setKeepScreenOn(boolean keepScreenOn) {
5109 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
5110 }
5111
5112 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005113 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5114 * @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 -08005115 *
5116 * @attr ref android.R.styleable#View_nextFocusLeft
5117 */
5118 public int getNextFocusLeftId() {
5119 return mNextFocusLeftId;
5120 }
5121
5122 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005123 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5124 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
5125 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005126 *
5127 * @attr ref android.R.styleable#View_nextFocusLeft
5128 */
5129 public void setNextFocusLeftId(int nextFocusLeftId) {
5130 mNextFocusLeftId = nextFocusLeftId;
5131 }
5132
5133 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005134 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5135 * @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 -08005136 *
5137 * @attr ref android.R.styleable#View_nextFocusRight
5138 */
5139 public int getNextFocusRightId() {
5140 return mNextFocusRightId;
5141 }
5142
5143 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005144 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5145 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
5146 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005147 *
5148 * @attr ref android.R.styleable#View_nextFocusRight
5149 */
5150 public void setNextFocusRightId(int nextFocusRightId) {
5151 mNextFocusRightId = nextFocusRightId;
5152 }
5153
5154 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005155 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5156 * @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 -08005157 *
5158 * @attr ref android.R.styleable#View_nextFocusUp
5159 */
5160 public int getNextFocusUpId() {
5161 return mNextFocusUpId;
5162 }
5163
5164 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005165 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5166 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
5167 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005168 *
5169 * @attr ref android.R.styleable#View_nextFocusUp
5170 */
5171 public void setNextFocusUpId(int nextFocusUpId) {
5172 mNextFocusUpId = nextFocusUpId;
5173 }
5174
5175 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005176 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5177 * @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 -08005178 *
5179 * @attr ref android.R.styleable#View_nextFocusDown
5180 */
5181 public int getNextFocusDownId() {
5182 return mNextFocusDownId;
5183 }
5184
5185 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005186 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5187 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
5188 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005189 *
5190 * @attr ref android.R.styleable#View_nextFocusDown
5191 */
5192 public void setNextFocusDownId(int nextFocusDownId) {
5193 mNextFocusDownId = nextFocusDownId;
5194 }
5195
5196 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005197 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5198 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
5199 *
5200 * @attr ref android.R.styleable#View_nextFocusForward
5201 */
5202 public int getNextFocusForwardId() {
5203 return mNextFocusForwardId;
5204 }
5205
5206 /**
5207 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5208 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
5209 * decide automatically.
5210 *
5211 * @attr ref android.R.styleable#View_nextFocusForward
5212 */
5213 public void setNextFocusForwardId(int nextFocusForwardId) {
5214 mNextFocusForwardId = nextFocusForwardId;
5215 }
5216
5217 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005218 * Returns the visibility of this view and all of its ancestors
5219 *
5220 * @return True if this view and all of its ancestors are {@link #VISIBLE}
5221 */
5222 public boolean isShown() {
5223 View current = this;
5224 //noinspection ConstantConditions
5225 do {
5226 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5227 return false;
5228 }
5229 ViewParent parent = current.mParent;
5230 if (parent == null) {
5231 return false; // We are not attached to the view root
5232 }
5233 if (!(parent instanceof View)) {
5234 return true;
5235 }
5236 current = (View) parent;
5237 } while (current != null);
5238
5239 return false;
5240 }
5241
5242 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005243 * Called by the view hierarchy when the content insets for a window have
5244 * changed, to allow it to adjust its content to fit within those windows.
5245 * The content insets tell you the space that the status bar, input method,
5246 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005247 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005248 * <p>You do not normally need to deal with this function, since the default
5249 * window decoration given to applications takes care of applying it to the
5250 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
5251 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
5252 * and your content can be placed under those system elements. You can then
5253 * use this method within your view hierarchy if you have parts of your UI
5254 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005255 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005256 * <p>The default implementation of this method simply applies the content
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005257 * inset's to the view's padding, consuming that content (modifying the
5258 * insets to be 0), and returning true. This behavior is off by default, but can
5259 * be enabled through {@link #setFitsSystemWindows(boolean)}.
5260 *
5261 * <p>This function's traversal down the hierarchy is depth-first. The same content
5262 * insets object is propagated down the hierarchy, so any changes made to it will
5263 * be seen by all following views (including potentially ones above in
5264 * the hierarchy since this is a depth-first traversal). The first view
5265 * that returns true will abort the entire traversal.
5266 *
5267 * <p>The default implementation works well for a situation where it is
5268 * used with a container that covers the entire window, allowing it to
5269 * apply the appropriate insets to its content on all edges. If you need
5270 * a more complicated layout (such as two different views fitting system
5271 * windows, one on the top of the window, and one on the bottom),
5272 * you can override the method and handle the insets however you would like.
5273 * Note that the insets provided by the framework are always relative to the
5274 * far edges of the window, not accounting for the location of the called view
5275 * within that window. (In fact when this method is called you do not yet know
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005276 * where the layout will place the view, as it is done before layout happens.)
5277 *
5278 * <p>Note: unlike many View methods, there is no dispatch phase to this
5279 * call. If you are overriding it in a ViewGroup and want to allow the
5280 * call to continue to your children, you must be sure to call the super
5281 * implementation.
5282 *
Dianne Hackborncf675782012-05-10 15:07:24 -07005283 * <p>Here is a sample layout that makes use of fitting system windows
5284 * to have controls for a video view placed inside of the window decorations
5285 * that it hides and shows. This can be used with code like the second
5286 * sample (video player) shown in {@link #setSystemUiVisibility(int)}.
5287 *
5288 * {@sample development/samples/ApiDemos/res/layout/video_player.xml complete}
5289 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005290 * @param insets Current content insets of the window. Prior to
5291 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
5292 * the insets or else you and Android will be unhappy.
5293 *
5294 * @return Return true if this view applied the insets and it should not
5295 * continue propagating further down the hierarchy, false otherwise.
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005296 * @see #getFitsSystemWindows()
5297 * @see #setFitsSystemWindows()
5298 * @see #setSystemUiVisibility(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005299 */
5300 protected boolean fitSystemWindows(Rect insets) {
5301 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005302 mUserPaddingStart = -1;
5303 mUserPaddingEnd = -1;
5304 mUserPaddingRelative = false;
5305 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
5306 || mAttachInfo == null
5307 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
5308 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
5309 return true;
5310 } else {
5311 internalSetPadding(0, 0, 0, 0);
5312 return false;
5313 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005314 }
5315 return false;
5316 }
5317
5318 /**
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005319 * Sets whether or not this view should account for system screen decorations
5320 * such as the status bar and inset its content; that is, controlling whether
5321 * the default implementation of {@link #fitSystemWindows(Rect)} will be
5322 * executed. See that method for more details.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005323 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005324 * <p>Note that if you are providing your own implementation of
5325 * {@link #fitSystemWindows(Rect)}, then there is no need to set this
5326 * flag to true -- your implementation will be overriding the default
5327 * implementation that checks this flag.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005328 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005329 * @param fitSystemWindows If true, then the default implementation of
5330 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005331 *
5332 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005333 * @see #getFitsSystemWindows()
5334 * @see #fitSystemWindows(Rect)
5335 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005336 */
5337 public void setFitsSystemWindows(boolean fitSystemWindows) {
5338 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
5339 }
5340
5341 /**
Dianne Hackborncf675782012-05-10 15:07:24 -07005342 * Check for state of {@link #setFitsSystemWindows(boolean). If this method
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005343 * returns true, the default implementation of {@link #fitSystemWindows(Rect)}
5344 * will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005345 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005346 * @return Returns true if the default implementation of
5347 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005348 *
5349 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005350 * @see #setFitsSystemWindows()
5351 * @see #fitSystemWindows(Rect)
5352 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005353 */
Dianne Hackborncf675782012-05-10 15:07:24 -07005354 public boolean getFitsSystemWindows() {
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005355 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
5356 }
5357
Dianne Hackbornb1b55e62012-05-10 16:25:54 -07005358 /** @hide */
5359 public boolean fitsSystemWindows() {
5360 return getFitsSystemWindows();
5361 }
5362
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005363 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005364 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
5365 */
5366 public void requestFitSystemWindows() {
5367 if (mParent != null) {
5368 mParent.requestFitSystemWindows();
5369 }
5370 }
5371
5372 /**
5373 * For use by PhoneWindow to make its own system window fitting optional.
5374 * @hide
5375 */
5376 public void makeOptionalFitsSystemWindows() {
5377 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5378 }
5379
5380 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005381 * Returns the visibility status for this view.
5382 *
5383 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5384 * @attr ref android.R.styleable#View_visibility
5385 */
5386 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005387 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5388 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5389 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005390 })
5391 public int getVisibility() {
5392 return mViewFlags & VISIBILITY_MASK;
5393 }
5394
5395 /**
5396 * Set the enabled state of this view.
5397 *
5398 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5399 * @attr ref android.R.styleable#View_visibility
5400 */
5401 @RemotableViewMethod
5402 public void setVisibility(int visibility) {
5403 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005404 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005405 }
5406
5407 /**
5408 * Returns the enabled status for this view. The interpretation of the
5409 * enabled state varies by subclass.
5410 *
5411 * @return True if this view is enabled, false otherwise.
5412 */
5413 @ViewDebug.ExportedProperty
5414 public boolean isEnabled() {
5415 return (mViewFlags & ENABLED_MASK) == ENABLED;
5416 }
5417
5418 /**
5419 * Set the enabled state of this view. The interpretation of the enabled
5420 * state varies by subclass.
5421 *
5422 * @param enabled True if this view is enabled, false otherwise.
5423 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005424 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005425 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005426 if (enabled == isEnabled()) return;
5427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005428 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5429
5430 /*
5431 * The View most likely has to change its appearance, so refresh
5432 * the drawable state.
5433 */
5434 refreshDrawableState();
5435
5436 // Invalidate too, since the default behavior for views is to be
5437 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005438 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005439 }
5440
5441 /**
5442 * Set whether this view can receive the focus.
5443 *
5444 * Setting this to false will also ensure that this view is not focusable
5445 * in touch mode.
5446 *
5447 * @param focusable If true, this view can receive the focus.
5448 *
5449 * @see #setFocusableInTouchMode(boolean)
5450 * @attr ref android.R.styleable#View_focusable
5451 */
5452 public void setFocusable(boolean focusable) {
5453 if (!focusable) {
5454 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5455 }
5456 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5457 }
5458
5459 /**
5460 * Set whether this view can receive focus while in touch mode.
5461 *
5462 * Setting this to true will also ensure that this view is focusable.
5463 *
5464 * @param focusableInTouchMode If true, this view can receive the focus while
5465 * in touch mode.
5466 *
5467 * @see #setFocusable(boolean)
5468 * @attr ref android.R.styleable#View_focusableInTouchMode
5469 */
5470 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5471 // Focusable in touch mode should always be set before the focusable flag
5472 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5473 // which, in touch mode, will not successfully request focus on this view
5474 // because the focusable in touch mode flag is not set
5475 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5476 if (focusableInTouchMode) {
5477 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5478 }
5479 }
5480
5481 /**
5482 * Set whether this view should have sound effects enabled for events such as
5483 * clicking and touching.
5484 *
5485 * <p>You may wish to disable sound effects for a view if you already play sounds,
5486 * for instance, a dial key that plays dtmf tones.
5487 *
5488 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5489 * @see #isSoundEffectsEnabled()
5490 * @see #playSoundEffect(int)
5491 * @attr ref android.R.styleable#View_soundEffectsEnabled
5492 */
5493 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5494 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5495 }
5496
5497 /**
5498 * @return whether this view should have sound effects enabled for events such as
5499 * clicking and touching.
5500 *
5501 * @see #setSoundEffectsEnabled(boolean)
5502 * @see #playSoundEffect(int)
5503 * @attr ref android.R.styleable#View_soundEffectsEnabled
5504 */
5505 @ViewDebug.ExportedProperty
5506 public boolean isSoundEffectsEnabled() {
5507 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5508 }
5509
5510 /**
5511 * Set whether this view should have haptic feedback for events such as
5512 * long presses.
5513 *
5514 * <p>You may wish to disable haptic feedback if your view already controls
5515 * its own haptic feedback.
5516 *
5517 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5518 * @see #isHapticFeedbackEnabled()
5519 * @see #performHapticFeedback(int)
5520 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5521 */
5522 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5523 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5524 }
5525
5526 /**
5527 * @return whether this view should have haptic feedback enabled for events
5528 * long presses.
5529 *
5530 * @see #setHapticFeedbackEnabled(boolean)
5531 * @see #performHapticFeedback(int)
5532 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5533 */
5534 @ViewDebug.ExportedProperty
5535 public boolean isHapticFeedbackEnabled() {
5536 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5537 }
5538
5539 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005540 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005541 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005542 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5543 * {@link #LAYOUT_DIRECTION_RTL},
5544 * {@link #LAYOUT_DIRECTION_INHERIT} or
5545 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005546 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005547 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005548 * @hide
Cibu Johny7632cb92010-02-22 13:01:02 -08005549 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005550 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005551 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5552 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5553 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5554 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005555 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005556 public int getLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005557 return (mPrivateFlags2 & LAYOUT_DIRECTION_MASK) >> LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005558 }
5559
5560 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005561 * Set the layout direction for this view. This will propagate a reset of layout direction
5562 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005563 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005564 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
5565 * {@link #LAYOUT_DIRECTION_RTL},
5566 * {@link #LAYOUT_DIRECTION_INHERIT} or
5567 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005568 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005569 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005570 * @hide
Cibu Johny7632cb92010-02-22 13:01:02 -08005571 */
5572 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005573 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005574 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005575 // Reset the current layout direction and the resolved one
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005576 mPrivateFlags2 &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07005577 resetResolvedLayoutDirection();
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005578 // Set the new layout direction (filtered) and ask for a layout pass
5579 mPrivateFlags2 |=
5580 ((layoutDirection << LAYOUT_DIRECTION_MASK_SHIFT) & LAYOUT_DIRECTION_MASK);
5581 requestLayout();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005582 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005583 }
5584
5585 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005586 * Returns the resolved layout direction for this view.
5587 *
5588 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005589 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005590 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005591 */
5592 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005593 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5594 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005595 })
5596 public int getResolvedLayoutDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -07005597 // The layout diretion will be resolved only if needed
5598 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) != LAYOUT_DIRECTION_RESOLVED) {
5599 resolveLayoutDirection();
5600 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07005601 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005602 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
5603 }
5604
5605 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005606 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5607 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005608 *
5609 * @return true if the layout is right-to-left.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005610 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005611 */
5612 @ViewDebug.ExportedProperty(category = "layout")
5613 public boolean isLayoutRtl() {
5614 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
5615 }
5616
5617 /**
Adam Powell539ee872012-02-03 19:00:49 -08005618 * Indicates whether the view is currently tracking transient state that the
5619 * app should not need to concern itself with saving and restoring, but that
5620 * the framework should take special note to preserve when possible.
5621 *
Adam Powell785c4472012-05-02 21:25:39 -07005622 * <p>A view with transient state cannot be trivially rebound from an external
5623 * data source, such as an adapter binding item views in a list. This may be
5624 * because the view is performing an animation, tracking user selection
5625 * of content, or similar.</p>
5626 *
Adam Powell539ee872012-02-03 19:00:49 -08005627 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005628 */
5629 @ViewDebug.ExportedProperty(category = "layout")
5630 public boolean hasTransientState() {
5631 return (mPrivateFlags2 & HAS_TRANSIENT_STATE) == HAS_TRANSIENT_STATE;
5632 }
5633
5634 /**
5635 * Set whether this view is currently tracking transient state that the
Chet Haase563d4f22012-04-18 16:20:08 -07005636 * framework should attempt to preserve when possible. This flag is reference counted,
5637 * so every call to setHasTransientState(true) should be paired with a later call
5638 * to setHasTransientState(false).
Adam Powell539ee872012-02-03 19:00:49 -08005639 *
Adam Powell785c4472012-05-02 21:25:39 -07005640 * <p>A view with transient state cannot be trivially rebound from an external
5641 * data source, such as an adapter binding item views in a list. This may be
5642 * because the view is performing an animation, tracking user selection
5643 * of content, or similar.</p>
5644 *
Adam Powell539ee872012-02-03 19:00:49 -08005645 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005646 */
5647 public void setHasTransientState(boolean hasTransientState) {
Chet Haase563d4f22012-04-18 16:20:08 -07005648 mTransientStateCount = hasTransientState ? mTransientStateCount + 1 :
5649 mTransientStateCount - 1;
5650 if (mTransientStateCount < 0) {
5651 mTransientStateCount = 0;
5652 Log.e(VIEW_LOG_TAG, "hasTransientState decremented below 0: " +
5653 "unmatched pair of setHasTransientState calls");
5654 }
5655 if ((hasTransientState && mTransientStateCount == 1) ||
Adam Powell057a5852012-05-11 10:28:38 -07005656 (!hasTransientState && mTransientStateCount == 0)) {
Chet Haase563d4f22012-04-18 16:20:08 -07005657 // update flag if we've just incremented up from 0 or decremented down to 0
5658 mPrivateFlags2 = (mPrivateFlags2 & ~HAS_TRANSIENT_STATE) |
5659 (hasTransientState ? HAS_TRANSIENT_STATE : 0);
5660 if (mParent != null) {
5661 try {
5662 mParent.childHasTransientStateChanged(this, hasTransientState);
5663 } catch (AbstractMethodError e) {
5664 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5665 " does not fully implement ViewParent", e);
5666 }
Adam Powell539ee872012-02-03 19:00:49 -08005667 }
5668 }
5669 }
5670
5671 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005672 * If this view doesn't do any drawing on its own, set this flag to
5673 * allow further optimizations. By default, this flag is not set on
5674 * View, but could be set on some View subclasses such as ViewGroup.
5675 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005676 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5677 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005678 *
5679 * @param willNotDraw whether or not this View draw on its own
5680 */
5681 public void setWillNotDraw(boolean willNotDraw) {
5682 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5683 }
5684
5685 /**
5686 * Returns whether or not this View draws on its own.
5687 *
5688 * @return true if this view has nothing to draw, false otherwise
5689 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005690 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005691 public boolean willNotDraw() {
5692 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
5693 }
5694
5695 /**
5696 * When a View's drawing cache is enabled, drawing is redirected to an
5697 * offscreen bitmap. Some views, like an ImageView, must be able to
5698 * bypass this mechanism if they already draw a single bitmap, to avoid
5699 * unnecessary usage of the memory.
5700 *
5701 * @param willNotCacheDrawing true if this view does not cache its
5702 * drawing, false otherwise
5703 */
5704 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
5705 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
5706 }
5707
5708 /**
5709 * Returns whether or not this View can cache its drawing or not.
5710 *
5711 * @return true if this view does not cache its drawing, false otherwise
5712 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005713 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005714 public boolean willNotCacheDrawing() {
5715 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5716 }
5717
5718 /**
5719 * Indicates whether this view reacts to click events or not.
5720 *
5721 * @return true if the view is clickable, false otherwise
5722 *
5723 * @see #setClickable(boolean)
5724 * @attr ref android.R.styleable#View_clickable
5725 */
5726 @ViewDebug.ExportedProperty
5727 public boolean isClickable() {
5728 return (mViewFlags & CLICKABLE) == CLICKABLE;
5729 }
5730
5731 /**
5732 * Enables or disables click events for this view. When a view
5733 * is clickable it will change its state to "pressed" on every click.
5734 * Subclasses should set the view clickable to visually react to
5735 * user's clicks.
5736 *
5737 * @param clickable true to make the view clickable, false otherwise
5738 *
5739 * @see #isClickable()
5740 * @attr ref android.R.styleable#View_clickable
5741 */
5742 public void setClickable(boolean clickable) {
5743 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5744 }
5745
5746 /**
5747 * Indicates whether this view reacts to long click events or not.
5748 *
5749 * @return true if the view is long clickable, false otherwise
5750 *
5751 * @see #setLongClickable(boolean)
5752 * @attr ref android.R.styleable#View_longClickable
5753 */
5754 public boolean isLongClickable() {
5755 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5756 }
5757
5758 /**
5759 * Enables or disables long click events for this view. When a view is long
5760 * clickable it reacts to the user holding down the button for a longer
5761 * duration than a tap. This event can either launch the listener or a
5762 * context menu.
5763 *
5764 * @param longClickable true to make the view long clickable, false otherwise
5765 * @see #isLongClickable()
5766 * @attr ref android.R.styleable#View_longClickable
5767 */
5768 public void setLongClickable(boolean longClickable) {
5769 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5770 }
5771
5772 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005773 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005774 *
5775 * @see #isClickable()
5776 * @see #setClickable(boolean)
5777 *
5778 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5779 * the View's internal state from a previously set "pressed" state.
5780 */
5781 public void setPressed(boolean pressed) {
Adam Powell035a1fc2012-02-27 15:23:50 -08005782 final boolean needsRefresh = pressed != ((mPrivateFlags & PRESSED) == PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005783
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005784 if (pressed) {
5785 mPrivateFlags |= PRESSED;
5786 } else {
5787 mPrivateFlags &= ~PRESSED;
5788 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005789
5790 if (needsRefresh) {
5791 refreshDrawableState();
5792 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005793 dispatchSetPressed(pressed);
5794 }
5795
5796 /**
5797 * Dispatch setPressed to all of this View's children.
5798 *
5799 * @see #setPressed(boolean)
5800 *
5801 * @param pressed The new pressed state
5802 */
5803 protected void dispatchSetPressed(boolean pressed) {
5804 }
5805
5806 /**
5807 * Indicates whether the view is currently in pressed state. Unless
5808 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5809 * the pressed state.
5810 *
Philip Milne6c8ea062012-04-03 17:38:43 -07005811 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005812 * @see #isClickable()
5813 * @see #setClickable(boolean)
5814 *
5815 * @return true if the view is currently pressed, false otherwise
5816 */
5817 public boolean isPressed() {
5818 return (mPrivateFlags & PRESSED) == PRESSED;
5819 }
5820
5821 /**
5822 * Indicates whether this view will save its state (that is,
5823 * whether its {@link #onSaveInstanceState} method will be called).
5824 *
5825 * @return Returns true if the view state saving is enabled, else false.
5826 *
5827 * @see #setSaveEnabled(boolean)
5828 * @attr ref android.R.styleable#View_saveEnabled
5829 */
5830 public boolean isSaveEnabled() {
5831 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5832 }
5833
5834 /**
5835 * Controls whether the saving of this view's state is
5836 * enabled (that is, whether its {@link #onSaveInstanceState} method
5837 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005838 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005839 * for its state to be saved. This flag can only disable the
5840 * saving of this view; any child views may still have their state saved.
5841 *
5842 * @param enabled Set to false to <em>disable</em> state saving, or true
5843 * (the default) to allow it.
5844 *
5845 * @see #isSaveEnabled()
5846 * @see #setId(int)
5847 * @see #onSaveInstanceState()
5848 * @attr ref android.R.styleable#View_saveEnabled
5849 */
5850 public void setSaveEnabled(boolean enabled) {
5851 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5852 }
5853
Jeff Brown85a31762010-09-01 17:01:00 -07005854 /**
5855 * Gets whether the framework should discard touches when the view's
5856 * window is obscured by another visible window.
5857 * Refer to the {@link View} security documentation for more details.
5858 *
5859 * @return True if touch filtering is enabled.
5860 *
5861 * @see #setFilterTouchesWhenObscured(boolean)
5862 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5863 */
5864 @ViewDebug.ExportedProperty
5865 public boolean getFilterTouchesWhenObscured() {
5866 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5867 }
5868
5869 /**
5870 * Sets whether the framework should discard touches when the view's
5871 * window is obscured by another visible window.
5872 * Refer to the {@link View} security documentation for more details.
5873 *
5874 * @param enabled True if touch filtering should be enabled.
5875 *
5876 * @see #getFilterTouchesWhenObscured
5877 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5878 */
5879 public void setFilterTouchesWhenObscured(boolean enabled) {
5880 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5881 FILTER_TOUCHES_WHEN_OBSCURED);
5882 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005883
5884 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005885 * Indicates whether the entire hierarchy under this view will save its
5886 * state when a state saving traversal occurs from its parent. The default
5887 * is true; if false, these views will not be saved unless
5888 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5889 *
5890 * @return Returns true if the view state saving from parent is enabled, else false.
5891 *
5892 * @see #setSaveFromParentEnabled(boolean)
5893 */
5894 public boolean isSaveFromParentEnabled() {
5895 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5896 }
5897
5898 /**
5899 * Controls whether the entire hierarchy under this view will save its
5900 * state when a state saving traversal occurs from its parent. The default
5901 * is true; if false, these views will not be saved unless
5902 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5903 *
5904 * @param enabled Set to false to <em>disable</em> state saving, or true
5905 * (the default) to allow it.
5906 *
5907 * @see #isSaveFromParentEnabled()
5908 * @see #setId(int)
5909 * @see #onSaveInstanceState()
5910 */
5911 public void setSaveFromParentEnabled(boolean enabled) {
5912 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5913 }
5914
5915
5916 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005917 * Returns whether this View is able to take focus.
5918 *
5919 * @return True if this view can take focus, or false otherwise.
5920 * @attr ref android.R.styleable#View_focusable
5921 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005922 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005923 public final boolean isFocusable() {
5924 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
5925 }
5926
5927 /**
5928 * When a view is focusable, it may not want to take focus when in touch mode.
5929 * For example, a button would like focus when the user is navigating via a D-pad
5930 * so that the user can click on it, but once the user starts touching the screen,
5931 * the button shouldn't take focus
5932 * @return Whether the view is focusable in touch mode.
5933 * @attr ref android.R.styleable#View_focusableInTouchMode
5934 */
5935 @ViewDebug.ExportedProperty
5936 public final boolean isFocusableInTouchMode() {
5937 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5938 }
5939
5940 /**
5941 * Find the nearest view in the specified direction that can take focus.
5942 * This does not actually give focus to that view.
5943 *
5944 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5945 *
5946 * @return The nearest focusable in the specified direction, or null if none
5947 * can be found.
5948 */
5949 public View focusSearch(int direction) {
5950 if (mParent != null) {
5951 return mParent.focusSearch(this, direction);
5952 } else {
5953 return null;
5954 }
5955 }
5956
5957 /**
5958 * This method is the last chance for the focused view and its ancestors to
5959 * respond to an arrow key. This is called when the focused view did not
5960 * consume the key internally, nor could the view system find a new view in
5961 * the requested direction to give focus to.
5962 *
5963 * @param focused The currently focused view.
5964 * @param direction The direction focus wants to move. One of FOCUS_UP,
5965 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5966 * @return True if the this view consumed this unhandled move.
5967 */
5968 public boolean dispatchUnhandledMove(View focused, int direction) {
5969 return false;
5970 }
5971
5972 /**
5973 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005974 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005975 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005976 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5977 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005978 * @return The user specified next view, or null if there is none.
5979 */
5980 View findUserSetNextFocus(View root, int direction) {
5981 switch (direction) {
5982 case FOCUS_LEFT:
5983 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005984 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005985 case FOCUS_RIGHT:
5986 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005987 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005988 case FOCUS_UP:
5989 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005990 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005991 case FOCUS_DOWN:
5992 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005993 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005994 case FOCUS_FORWARD:
5995 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005996 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005997 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08005998 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08005999 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006000 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08006001 @Override
6002 public boolean apply(View t) {
6003 return t.mNextFocusForwardId == id;
6004 }
6005 });
6006 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006007 }
6008 return null;
6009 }
6010
Jeff Brown4dfbec22011-08-15 14:55:37 -07006011 private View findViewInsideOutShouldExist(View root, final int childViewId) {
6012 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
6013 @Override
6014 public boolean apply(View t) {
6015 return t.mID == childViewId;
6016 }
6017 });
6018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006019 if (result == null) {
6020 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
6021 + "by user for id " + childViewId);
6022 }
6023 return result;
6024 }
6025
6026 /**
6027 * Find and return all focusable views that are descendants of this view,
6028 * possibly including this view if it is focusable itself.
6029 *
6030 * @param direction The direction of the focus
6031 * @return A list of focusable views
6032 */
6033 public ArrayList<View> getFocusables(int direction) {
6034 ArrayList<View> result = new ArrayList<View>(24);
6035 addFocusables(result, direction);
6036 return result;
6037 }
6038
6039 /**
6040 * Add any focusable views that are descendants of this view (possibly
6041 * including this view if it is focusable itself) to views. If we are in touch mode,
6042 * only add views that are also focusable in touch mode.
6043 *
6044 * @param views Focusable views found so far
6045 * @param direction The direction of the focus
6046 */
6047 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006048 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
6049 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006050
svetoslavganov75986cf2009-05-14 22:28:01 -07006051 /**
6052 * Adds any focusable views that are descendants of this view (possibly
6053 * including this view if it is focusable itself) to views. This method
6054 * adds all focusable views regardless if we are in touch mode or
Svetoslav Ganov42138042012-03-20 11:51:39 -07006055 * only views focusable in touch mode if we are in touch mode or
6056 * only views that can take accessibility focus if accessibility is enabeld
6057 * depending on the focusable mode paramater.
svetoslavganov75986cf2009-05-14 22:28:01 -07006058 *
6059 * @param views Focusable views found so far or null if all we are interested is
6060 * the number of focusables.
6061 * @param direction The direction of the focus.
6062 * @param focusableMode The type of focusables to be added.
6063 *
6064 * @see #FOCUSABLES_ALL
6065 * @see #FOCUSABLES_TOUCH_MODE
6066 */
6067 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006068 if (views == null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006069 return;
6070 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006071 if ((focusableMode & FOCUSABLES_ACCESSIBILITY) == FOCUSABLES_ACCESSIBILITY) {
Svetoslav Ganov791fd312012-05-14 15:12:30 -07006072 if (canTakeAccessibilityFocusFromHover() || getAccessibilityNodeProvider() != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006073 views.add(this);
6074 return;
6075 }
6076 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006077 if (!isFocusable()) {
6078 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07006079 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006080 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE
6081 && isInTouchMode() && !isFocusableInTouchMode()) {
6082 return;
6083 }
6084 views.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006085 }
6086
6087 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006088 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006089 * The search is performed by either the text that the View renders or the content
6090 * description that describes the view for accessibility purposes and the view does
6091 * not render or both. Clients can specify how the search is to be performed via
6092 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
6093 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006094 *
6095 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006096 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07006097 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006098 * @see #FIND_VIEWS_WITH_TEXT
6099 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
6100 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006101 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006102 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07006103 if (getAccessibilityNodeProvider() != null) {
6104 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
6105 outViews.add(this);
6106 }
6107 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006108 && (searched != null && searched.length() > 0)
6109 && (mContentDescription != null && mContentDescription.length() > 0)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006110 String searchedLowerCase = searched.toString().toLowerCase();
6111 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
6112 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
6113 outViews.add(this);
6114 }
6115 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006116 }
6117
6118 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006119 * Find and return all touchable views that are descendants of this view,
6120 * possibly including this view if it is touchable itself.
6121 *
6122 * @return A list of touchable views
6123 */
6124 public ArrayList<View> getTouchables() {
6125 ArrayList<View> result = new ArrayList<View>();
6126 addTouchables(result);
6127 return result;
6128 }
6129
6130 /**
6131 * Add any touchable views that are descendants of this view (possibly
6132 * including this view if it is touchable itself) to views.
6133 *
6134 * @param views Touchable views found so far
6135 */
6136 public void addTouchables(ArrayList<View> views) {
6137 final int viewFlags = mViewFlags;
6138
6139 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
6140 && (viewFlags & ENABLED_MASK) == ENABLED) {
6141 views.add(this);
6142 }
6143 }
6144
6145 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006146 * Returns whether this View is accessibility focused.
6147 *
6148 * @return True if this View is accessibility focused.
6149 */
6150 boolean isAccessibilityFocused() {
6151 return (mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0;
6152 }
6153
6154 /**
6155 * Call this to try to give accessibility focus to this view.
6156 *
6157 * A view will not actually take focus if {@link AccessibilityManager#isEnabled()}
6158 * returns false or the view is no visible or the view already has accessibility
6159 * focus.
6160 *
6161 * See also {@link #focusSearch(int)}, which is what you call to say that you
6162 * have focus, and you want your parent to look for the next one.
6163 *
6164 * @return Whether this view actually took accessibility focus.
6165 *
6166 * @hide
6167 */
6168 public boolean requestAccessibilityFocus() {
Svetoslav Ganov07b726c2012-04-30 12:24:57 -07006169 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
6170 if (!manager.isEnabled() || !manager.isTouchExplorationEnabled()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006171 return false;
6172 }
6173 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6174 return false;
6175 }
6176 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) == 0) {
6177 mPrivateFlags2 |= ACCESSIBILITY_FOCUSED;
6178 ViewRootImpl viewRootImpl = getViewRootImpl();
6179 if (viewRootImpl != null) {
6180 viewRootImpl.setAccessibilityFocusedHost(this);
6181 }
6182 invalidate();
6183 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
6184 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006185 return true;
6186 }
6187 return false;
6188 }
6189
6190 /**
6191 * Call this to try to clear accessibility focus of this view.
6192 *
6193 * See also {@link #focusSearch(int)}, which is what you call to say that you
6194 * have focus, and you want your parent to look for the next one.
6195 *
6196 * @hide
6197 */
6198 public void clearAccessibilityFocus() {
Svetoslav Ganov791fd312012-05-14 15:12:30 -07006199 ViewRootImpl viewRootImpl = getViewRootImpl();
6200 if (viewRootImpl != null) {
6201 View focusHost = viewRootImpl.getAccessibilityFocusedHost();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006202 if (focusHost != null && focusHost != this
6203 && ViewRootImpl.isViewDescendantOf(focusHost, this)) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006204 viewRootImpl.setAccessibilityFocusedHost(null);
6205 }
Svetoslav Ganov791fd312012-05-14 15:12:30 -07006206 }
6207 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6208 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006209 invalidate();
6210 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
6211 notifyAccessibilityStateChanged();
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006212
6213 // Clear the text navigation state.
6214 setAccessibilityCursorPosition(-1);
Svetoslav Ganov42138042012-03-20 11:51:39 -07006215 }
6216 }
6217
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006218 private void requestAccessibilityFocusFromHover() {
6219 if (includeForAccessibility() && isActionableForAccessibility()) {
6220 requestAccessibilityFocus();
Svetoslav Ganovf76a83c2012-05-21 15:32:17 -07006221 requestFocusNoSearch(View.FOCUS_DOWN, null);
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006222 } else {
6223 if (mParent != null) {
6224 View nextFocus = mParent.findViewToTakeAccessibilityFocusFromHover(this, this);
6225 if (nextFocus != null) {
6226 nextFocus.requestAccessibilityFocus();
Svetoslav Ganovf76a83c2012-05-21 15:32:17 -07006227 nextFocus.requestFocusNoSearch(View.FOCUS_DOWN, null);
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006228 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006229 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006230 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006231 }
6232
6233 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006234 * @hide
6235 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006236 public boolean canTakeAccessibilityFocusFromHover() {
6237 if (includeForAccessibility() && isActionableForAccessibility()) {
6238 return true;
6239 }
6240 if (mParent != null) {
6241 return (mParent.findViewToTakeAccessibilityFocusFromHover(this, this) == this);
Svetoslav Ganov42138042012-03-20 11:51:39 -07006242 }
6243 return false;
6244 }
6245
6246 /**
6247 * Clears accessibility focus without calling any callback methods
6248 * normally invoked in {@link #clearAccessibilityFocus()}. This method
6249 * is used for clearing accessibility focus when giving this focus to
6250 * another view.
6251 */
6252 void clearAccessibilityFocusNoCallbacks() {
6253 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6254 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
6255 invalidate();
6256 }
6257 }
6258
6259 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006260 * Call this to try to give focus to a specific view or to one of its
6261 * descendants.
6262 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006263 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6264 * false), or if it is focusable and it is not focusable in touch mode
6265 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006266 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006267 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006268 * have focus, and you want your parent to look for the next one.
6269 *
6270 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
6271 * {@link #FOCUS_DOWN} and <code>null</code>.
6272 *
6273 * @return Whether this view or one of its descendants actually took focus.
6274 */
6275 public final boolean requestFocus() {
6276 return requestFocus(View.FOCUS_DOWN);
6277 }
6278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006279 /**
6280 * Call this to try to give focus to a specific view or to one of its
6281 * descendants and give it a hint about what direction focus is heading.
6282 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006283 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6284 * false), or if it is focusable and it is not focusable in touch mode
6285 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006286 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006287 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006288 * have focus, and you want your parent to look for the next one.
6289 *
6290 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
6291 * <code>null</code> set for the previously focused rectangle.
6292 *
6293 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6294 * @return Whether this view or one of its descendants actually took focus.
6295 */
6296 public final boolean requestFocus(int direction) {
6297 return requestFocus(direction, null);
6298 }
6299
6300 /**
6301 * Call this to try to give focus to a specific view or to one of its descendants
6302 * and give it hints about the direction and a specific rectangle that the focus
6303 * is coming from. The rectangle can help give larger views a finer grained hint
6304 * about where focus is coming from, and therefore, where to show selection, or
6305 * forward focus change internally.
6306 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006307 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6308 * false), or if it is focusable and it is not focusable in touch mode
6309 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006310 *
6311 * A View will not take focus if it is not visible.
6312 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006313 * A View will not take focus if one of its parents has
6314 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
6315 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006316 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006317 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006318 * have focus, and you want your parent to look for the next one.
6319 *
6320 * You may wish to override this method if your custom {@link View} has an internal
6321 * {@link View} that it wishes to forward the request to.
6322 *
6323 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6324 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
6325 * to give a finer grained hint about where focus is coming from. May be null
6326 * if there is no hint.
6327 * @return Whether this view or one of its descendants actually took focus.
6328 */
6329 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006330 return requestFocusNoSearch(direction, previouslyFocusedRect);
6331 }
6332
6333 private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006334 // need to be focusable
6335 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
6336 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6337 return false;
6338 }
6339
6340 // need to be focusable in touch mode if in touch mode
6341 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006342 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
6343 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006344 }
6345
6346 // need to not have any parents blocking us
6347 if (hasAncestorThatBlocksDescendantFocus()) {
6348 return false;
6349 }
6350
6351 handleFocusGainInternal(direction, previouslyFocusedRect);
6352 return true;
6353 }
6354
6355 /**
6356 * Call this to try to give focus to a specific view or to one of its descendants. This is a
6357 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
6358 * touch mode to request focus when they are touched.
6359 *
6360 * @return Whether this view or one of its descendants actually took focus.
6361 *
6362 * @see #isInTouchMode()
6363 *
6364 */
6365 public final boolean requestFocusFromTouch() {
6366 // Leave touch mode if we need to
6367 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006368 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07006369 if (viewRoot != null) {
6370 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006371 }
6372 }
6373 return requestFocus(View.FOCUS_DOWN);
6374 }
6375
6376 /**
6377 * @return Whether any ancestor of this view blocks descendant focus.
6378 */
6379 private boolean hasAncestorThatBlocksDescendantFocus() {
6380 ViewParent ancestor = mParent;
6381 while (ancestor instanceof ViewGroup) {
6382 final ViewGroup vgAncestor = (ViewGroup) ancestor;
6383 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
6384 return true;
6385 } else {
6386 ancestor = vgAncestor.getParent();
6387 }
6388 }
6389 return false;
6390 }
6391
6392 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006393 * Gets the mode for determining whether this View is important for accessibility
6394 * which is if it fires accessibility events and if it is reported to
6395 * accessibility services that query the screen.
6396 *
6397 * @return The mode for determining whether a View is important for accessibility.
6398 *
6399 * @attr ref android.R.styleable#View_importantForAccessibility
6400 *
6401 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6402 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6403 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6404 */
6405 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
6406 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_AUTO,
6407 to = "IMPORTANT_FOR_ACCESSIBILITY_AUTO"),
6408 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_YES,
6409 to = "IMPORTANT_FOR_ACCESSIBILITY_YES"),
6410 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_NO,
6411 to = "IMPORTANT_FOR_ACCESSIBILITY_NO")
6412 })
6413 public int getImportantForAccessibility() {
6414 return (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6415 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6416 }
6417
6418 /**
6419 * Sets how to determine whether this view is important for accessibility
6420 * which is if it fires accessibility events and if it is reported to
6421 * accessibility services that query the screen.
6422 *
6423 * @param mode How to determine whether this view is important for accessibility.
6424 *
6425 * @attr ref android.R.styleable#View_importantForAccessibility
6426 *
6427 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6428 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6429 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6430 */
6431 public void setImportantForAccessibility(int mode) {
6432 if (mode != getImportantForAccessibility()) {
6433 mPrivateFlags2 &= ~IMPORTANT_FOR_ACCESSIBILITY_MASK;
6434 mPrivateFlags2 |= (mode << IMPORTANT_FOR_ACCESSIBILITY_SHIFT)
6435 & IMPORTANT_FOR_ACCESSIBILITY_MASK;
6436 notifyAccessibilityStateChanged();
6437 }
6438 }
6439
6440 /**
6441 * Gets whether this view should be exposed for accessibility.
6442 *
6443 * @return Whether the view is exposed for accessibility.
6444 *
6445 * @hide
6446 */
6447 public boolean isImportantForAccessibility() {
6448 final int mode = (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6449 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6450 switch (mode) {
6451 case IMPORTANT_FOR_ACCESSIBILITY_YES:
6452 return true;
6453 case IMPORTANT_FOR_ACCESSIBILITY_NO:
6454 return false;
6455 case IMPORTANT_FOR_ACCESSIBILITY_AUTO:
6456 return isActionableForAccessibility() || hasListenersForAccessibility();
6457 default:
6458 throw new IllegalArgumentException("Unknow important for accessibility mode: "
6459 + mode);
6460 }
6461 }
6462
6463 /**
6464 * Gets the parent for accessibility purposes. Note that the parent for
6465 * accessibility is not necessary the immediate parent. It is the first
6466 * predecessor that is important for accessibility.
6467 *
6468 * @return The parent for accessibility purposes.
6469 */
6470 public ViewParent getParentForAccessibility() {
6471 if (mParent instanceof View) {
6472 View parentView = (View) mParent;
6473 if (parentView.includeForAccessibility()) {
6474 return mParent;
6475 } else {
6476 return mParent.getParentForAccessibility();
6477 }
6478 }
6479 return null;
6480 }
6481
6482 /**
6483 * Adds the children of a given View for accessibility. Since some Views are
6484 * not important for accessibility the children for accessibility are not
6485 * necessarily direct children of the riew, rather they are the first level of
6486 * descendants important for accessibility.
6487 *
6488 * @param children The list of children for accessibility.
6489 */
6490 public void addChildrenForAccessibility(ArrayList<View> children) {
6491 if (includeForAccessibility()) {
6492 children.add(this);
6493 }
6494 }
6495
6496 /**
6497 * Whether to regard this view for accessibility. A view is regarded for
6498 * accessibility if it is important for accessibility or the querying
6499 * accessibility service has explicitly requested that view not
6500 * important for accessibility are regarded.
6501 *
6502 * @return Whether to regard the view for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006503 *
6504 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006505 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006506 public boolean includeForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006507 if (mAttachInfo != null) {
6508 if (!mAttachInfo.mIncludeNotImportantViews) {
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07006509 return isImportantForAccessibility();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006510 }
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006511 return true;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006512 }
6513 return false;
6514 }
6515
6516 /**
6517 * Returns whether the View is considered actionable from
6518 * accessibility perspective. Such view are important for
6519 * accessiiblity.
6520 *
6521 * @return True if the view is actionable for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006522 *
6523 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006524 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006525 public boolean isActionableForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006526 return (isClickable() || isLongClickable() || isFocusable());
6527 }
6528
6529 /**
6530 * Returns whether the View has registered callbacks wich makes it
6531 * important for accessiiblity.
6532 *
6533 * @return True if the view is actionable for accessibility.
6534 */
6535 private boolean hasListenersForAccessibility() {
6536 ListenerInfo info = getListenerInfo();
6537 return mTouchDelegate != null || info.mOnKeyListener != null
6538 || info.mOnTouchListener != null || info.mOnGenericMotionListener != null
6539 || info.mOnHoverListener != null || info.mOnDragListener != null;
6540 }
6541
6542 /**
6543 * Notifies accessibility services that some view's important for
6544 * accessibility state has changed. Note that such notifications
6545 * are made at most once every
6546 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}
6547 * to avoid unnecessary load to the system. Also once a view has
6548 * made a notifucation this method is a NOP until the notification has
6549 * been sent to clients.
6550 *
6551 * @hide
6552 *
6553 * TODO: Makse sure this method is called for any view state change
6554 * that is interesting for accessilility purposes.
6555 */
6556 public void notifyAccessibilityStateChanged() {
Svetoslav Ganovc406be92012-05-11 16:12:32 -07006557 if (!AccessibilityManager.getInstance(mContext).isEnabled()) {
6558 return;
6559 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006560 if ((mPrivateFlags2 & ACCESSIBILITY_STATE_CHANGED) == 0) {
6561 mPrivateFlags2 |= ACCESSIBILITY_STATE_CHANGED;
6562 if (mParent != null) {
6563 mParent.childAccessibilityStateChanged(this);
6564 }
6565 }
6566 }
6567
6568 /**
6569 * Reset the state indicating the this view has requested clients
6570 * interested in its accessiblity state to be notified.
6571 *
6572 * @hide
6573 */
6574 public void resetAccessibilityStateChanged() {
6575 mPrivateFlags2 &= ~ACCESSIBILITY_STATE_CHANGED;
6576 }
6577
6578 /**
6579 * Performs the specified accessibility action on the view. For
6580 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
alanv8eeefef2012-05-07 16:57:53 -07006581 * <p>
6582 * If an {@link AccessibilityDelegate} has been specified via calling
6583 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
6584 * {@link AccessibilityDelegate#performAccessibilityAction(View, int, Bundle)}
6585 * is responsible for handling this call.
6586 * </p>
Svetoslav Ganov42138042012-03-20 11:51:39 -07006587 *
6588 * @param action The action to perform.
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006589 * @param arguments Optional action arguments.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006590 * @return Whether the action was performed.
6591 */
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006592 public boolean performAccessibilityAction(int action, Bundle arguments) {
alanv8eeefef2012-05-07 16:57:53 -07006593 if (mAccessibilityDelegate != null) {
6594 return mAccessibilityDelegate.performAccessibilityAction(this, action, arguments);
6595 } else {
6596 return performAccessibilityActionInternal(action, arguments);
6597 }
6598 }
6599
6600 /**
6601 * @see #performAccessibilityAction(int, Bundle)
6602 *
6603 * Note: Called from the default {@link AccessibilityDelegate}.
6604 */
6605 boolean performAccessibilityActionInternal(int action, Bundle arguments) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006606 switch (action) {
6607 case AccessibilityNodeInfo.ACTION_CLICK: {
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006608 if (isClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006609 return performClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006610 }
6611 } break;
6612 case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
6613 if (isLongClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006614 return performLongClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006615 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006616 } break;
6617 case AccessibilityNodeInfo.ACTION_FOCUS: {
6618 if (!hasFocus()) {
6619 // Get out of touch mode since accessibility
6620 // wants to move focus around.
6621 getViewRootImpl().ensureTouchMode(false);
6622 return requestFocus();
6623 }
6624 } break;
6625 case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS: {
6626 if (hasFocus()) {
6627 clearFocus();
6628 return !isFocused();
6629 }
6630 } break;
6631 case AccessibilityNodeInfo.ACTION_SELECT: {
6632 if (!isSelected()) {
6633 setSelected(true);
6634 return isSelected();
6635 }
6636 } break;
6637 case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
6638 if (isSelected()) {
6639 setSelected(false);
6640 return !isSelected();
6641 }
6642 } break;
6643 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
6644 if (!isAccessibilityFocused()) {
6645 return requestAccessibilityFocus();
6646 }
6647 } break;
6648 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
6649 if (isAccessibilityFocused()) {
6650 clearAccessibilityFocus();
6651 return true;
6652 }
6653 } break;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006654 case AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY: {
6655 if (arguments != null) {
6656 final int granularity = arguments.getInt(
6657 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6658 return nextAtGranularity(granularity);
6659 }
6660 } break;
6661 case AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY: {
6662 if (arguments != null) {
6663 final int granularity = arguments.getInt(
6664 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6665 return previousAtGranularity(granularity);
6666 }
6667 } break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006668 }
6669 return false;
6670 }
6671
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006672 private boolean nextAtGranularity(int granularity) {
6673 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006674 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006675 return false;
6676 }
6677 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6678 if (iterator == null) {
6679 return false;
6680 }
6681 final int current = getAccessibilityCursorPosition();
6682 final int[] range = iterator.following(current);
6683 if (range == null) {
6684 setAccessibilityCursorPosition(-1);
6685 return false;
6686 }
6687 final int start = range[0];
6688 final int end = range[1];
6689 setAccessibilityCursorPosition(start);
6690 sendViewTextTraversedAtGranularityEvent(
6691 AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY,
6692 granularity, start, end);
6693 return true;
6694 }
6695
6696 private boolean previousAtGranularity(int granularity) {
6697 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006698 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006699 return false;
6700 }
6701 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6702 if (iterator == null) {
6703 return false;
6704 }
6705 final int selectionStart = getAccessibilityCursorPosition();
6706 final int current = selectionStart >= 0 ? selectionStart : text.length() + 1;
6707 final int[] range = iterator.preceding(current);
6708 if (range == null) {
6709 setAccessibilityCursorPosition(-1);
6710 return false;
6711 }
6712 final int start = range[0];
6713 final int end = range[1];
6714 setAccessibilityCursorPosition(end);
6715 sendViewTextTraversedAtGranularityEvent(
6716 AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
6717 granularity, start, end);
6718 return true;
6719 }
6720
6721 /**
6722 * Gets the text reported for accessibility purposes.
6723 *
6724 * @return The accessibility text.
6725 *
6726 * @hide
6727 */
6728 public CharSequence getIterableTextForAccessibility() {
6729 return mContentDescription;
6730 }
6731
6732 /**
6733 * @hide
6734 */
6735 public int getAccessibilityCursorPosition() {
6736 return mAccessibilityCursorPosition;
6737 }
6738
6739 /**
6740 * @hide
6741 */
6742 public void setAccessibilityCursorPosition(int position) {
6743 mAccessibilityCursorPosition = position;
6744 }
6745
6746 private void sendViewTextTraversedAtGranularityEvent(int action, int granularity,
6747 int fromIndex, int toIndex) {
6748 if (mParent == null) {
6749 return;
6750 }
6751 AccessibilityEvent event = AccessibilityEvent.obtain(
6752 AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY);
6753 onInitializeAccessibilityEvent(event);
6754 onPopulateAccessibilityEvent(event);
6755 event.setFromIndex(fromIndex);
6756 event.setToIndex(toIndex);
6757 event.setAction(action);
6758 event.setMovementGranularity(granularity);
6759 mParent.requestSendAccessibilityEvent(this, event);
6760 }
6761
6762 /**
6763 * @hide
6764 */
6765 public TextSegmentIterator getIteratorForGranularity(int granularity) {
6766 switch (granularity) {
6767 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER: {
6768 CharSequence text = getIterableTextForAccessibility();
6769 if (text != null && text.length() > 0) {
6770 CharacterTextSegmentIterator iterator =
6771 CharacterTextSegmentIterator.getInstance(mContext);
6772 iterator.initialize(text.toString());
6773 return iterator;
6774 }
6775 } break;
6776 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD: {
6777 CharSequence text = getIterableTextForAccessibility();
6778 if (text != null && text.length() > 0) {
6779 WordTextSegmentIterator iterator =
6780 WordTextSegmentIterator.getInstance(mContext);
6781 iterator.initialize(text.toString());
6782 return iterator;
6783 }
6784 } break;
6785 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH: {
6786 CharSequence text = getIterableTextForAccessibility();
6787 if (text != null && text.length() > 0) {
6788 ParagraphTextSegmentIterator iterator =
6789 ParagraphTextSegmentIterator.getInstance();
6790 iterator.initialize(text.toString());
6791 return iterator;
6792 }
6793 } break;
6794 }
6795 return null;
6796 }
6797
Svetoslav Ganov42138042012-03-20 11:51:39 -07006798 /**
Romain Guya440b002010-02-24 15:57:54 -08006799 * @hide
6800 */
6801 public void dispatchStartTemporaryDetach() {
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07006802 clearAccessibilityFocus();
Romain Guy38c2ece2012-05-24 14:20:56 -07006803 clearDisplayList();
6804
Romain Guya440b002010-02-24 15:57:54 -08006805 onStartTemporaryDetach();
6806 }
6807
6808 /**
6809 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006810 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
6811 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08006812 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006813 */
6814 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08006815 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08006816 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08006817 }
6818
6819 /**
6820 * @hide
6821 */
6822 public void dispatchFinishTemporaryDetach() {
6823 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006824 }
Romain Guy8506ab42009-06-11 17:35:47 -07006825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006826 /**
6827 * Called after {@link #onStartTemporaryDetach} when the container is done
6828 * changing the view.
6829 */
6830 public void onFinishTemporaryDetach() {
6831 }
Romain Guy8506ab42009-06-11 17:35:47 -07006832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006833 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006834 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
6835 * for this view's window. Returns null if the view is not currently attached
6836 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07006837 * just use the standard high-level event callbacks like
6838 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006839 */
6840 public KeyEvent.DispatcherState getKeyDispatcherState() {
6841 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
6842 }
Joe Malin32736f02011-01-19 16:14:20 -08006843
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006844 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006845 * Dispatch a key event before it is processed by any input method
6846 * associated with the view hierarchy. This can be used to intercept
6847 * key events in special situations before the IME consumes them; a
6848 * typical example would be handling the BACK key to update the application's
6849 * UI instead of allowing the IME to see it and close itself.
6850 *
6851 * @param event The key event to be dispatched.
6852 * @return True if the event was handled, false otherwise.
6853 */
6854 public boolean dispatchKeyEventPreIme(KeyEvent event) {
6855 return onKeyPreIme(event.getKeyCode(), event);
6856 }
6857
6858 /**
6859 * Dispatch a key event to the next view on the focus path. This path runs
6860 * from the top of the view tree down to the currently focused view. If this
6861 * view has focus, it will dispatch to itself. Otherwise it will dispatch
6862 * the next node down the focus path. This method also fires any key
6863 * listeners.
6864 *
6865 * @param event The key event to be dispatched.
6866 * @return True if the event was handled, false otherwise.
6867 */
6868 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006869 if (mInputEventConsistencyVerifier != null) {
6870 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
6871 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006872
Jeff Brown21bc5c92011-02-28 18:27:14 -08006873 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07006874 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006875 ListenerInfo li = mListenerInfo;
6876 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6877 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006878 return true;
6879 }
6880
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006881 if (event.dispatch(this, mAttachInfo != null
6882 ? mAttachInfo.mKeyDispatchState : null, this)) {
6883 return true;
6884 }
6885
6886 if (mInputEventConsistencyVerifier != null) {
6887 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6888 }
6889 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006890 }
6891
6892 /**
6893 * Dispatches a key shortcut event.
6894 *
6895 * @param event The key event to be dispatched.
6896 * @return True if the event was handled by the view, false otherwise.
6897 */
6898 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
6899 return onKeyShortcut(event.getKeyCode(), event);
6900 }
6901
6902 /**
6903 * Pass the touch screen motion event down to the target view, or this
6904 * view if it is the target.
6905 *
6906 * @param event The motion event to be dispatched.
6907 * @return True if the event was handled by the view, false otherwise.
6908 */
6909 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006910 if (mInputEventConsistencyVerifier != null) {
6911 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
6912 }
6913
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006914 if (onFilterTouchEventForSecurity(event)) {
6915 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006916 ListenerInfo li = mListenerInfo;
6917 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6918 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006919 return true;
6920 }
6921
6922 if (onTouchEvent(event)) {
6923 return true;
6924 }
Jeff Brown85a31762010-09-01 17:01:00 -07006925 }
6926
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006927 if (mInputEventConsistencyVerifier != null) {
6928 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006929 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006930 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006931 }
6932
6933 /**
Jeff Brown85a31762010-09-01 17:01:00 -07006934 * Filter the touch event to apply security policies.
6935 *
6936 * @param event The motion event to be filtered.
6937 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08006938 *
Jeff Brown85a31762010-09-01 17:01:00 -07006939 * @see #getFilterTouchesWhenObscured
6940 */
6941 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07006942 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07006943 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
6944 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
6945 // Window is obscured, drop this touch.
6946 return false;
6947 }
6948 return true;
6949 }
6950
6951 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006952 * Pass a trackball motion event down to the focused view.
6953 *
6954 * @param event The motion event to be dispatched.
6955 * @return True if the event was handled by the view, false otherwise.
6956 */
6957 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006958 if (mInputEventConsistencyVerifier != null) {
6959 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
6960 }
6961
Romain Guy02ccac62011-06-24 13:20:23 -07006962 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006963 }
6964
6965 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08006966 * Dispatch a generic motion event.
6967 * <p>
6968 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6969 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08006970 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07006971 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006972 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08006973 *
6974 * @param event The motion event to be dispatched.
6975 * @return True if the event was handled by the view, false otherwise.
6976 */
6977 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006978 if (mInputEventConsistencyVerifier != null) {
6979 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
6980 }
6981
Jeff Browna032cc02011-03-07 16:56:21 -08006982 final int source = event.getSource();
6983 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
6984 final int action = event.getAction();
6985 if (action == MotionEvent.ACTION_HOVER_ENTER
6986 || action == MotionEvent.ACTION_HOVER_MOVE
6987 || action == MotionEvent.ACTION_HOVER_EXIT) {
6988 if (dispatchHoverEvent(event)) {
6989 return true;
6990 }
6991 } else if (dispatchGenericPointerEvent(event)) {
6992 return true;
6993 }
6994 } else if (dispatchGenericFocusedEvent(event)) {
6995 return true;
6996 }
6997
Jeff Brown10b62902011-06-20 16:40:37 -07006998 if (dispatchGenericMotionEventInternal(event)) {
6999 return true;
7000 }
7001
7002 if (mInputEventConsistencyVerifier != null) {
7003 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7004 }
7005 return false;
7006 }
7007
7008 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07007009 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007010 ListenerInfo li = mListenerInfo;
7011 if (li != null && li.mOnGenericMotionListener != null
7012 && (mViewFlags & ENABLED_MASK) == ENABLED
7013 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007014 return true;
7015 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007016
7017 if (onGenericMotionEvent(event)) {
7018 return true;
7019 }
7020
7021 if (mInputEventConsistencyVerifier != null) {
7022 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7023 }
7024 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08007025 }
7026
7027 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007028 * Dispatch a hover event.
7029 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007030 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07007031 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007032 * </p>
7033 *
7034 * @param event The motion event to be dispatched.
7035 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007036 */
7037 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07007038 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007039 ListenerInfo li = mListenerInfo;
7040 if (li != null && li.mOnHoverListener != null
7041 && (mViewFlags & ENABLED_MASK) == ENABLED
7042 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07007043 return true;
7044 }
7045
Jeff Browna032cc02011-03-07 16:56:21 -08007046 return onHoverEvent(event);
7047 }
7048
7049 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007050 * Returns true if the view has a child to which it has recently sent
7051 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
7052 * it does not have a hovered child, then it must be the innermost hovered view.
7053 * @hide
7054 */
7055 protected boolean hasHoveredChild() {
7056 return false;
7057 }
7058
7059 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007060 * Dispatch a generic motion event to the view under the first pointer.
7061 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007062 * Do not call this method directly.
7063 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007064 * </p>
7065 *
7066 * @param event The motion event to be dispatched.
7067 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007068 */
7069 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
7070 return false;
7071 }
7072
7073 /**
7074 * Dispatch a generic motion event to the currently focused view.
7075 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007076 * Do not call this method directly.
7077 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007078 * </p>
7079 *
7080 * @param event The motion event to be dispatched.
7081 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007082 */
7083 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
7084 return false;
7085 }
7086
7087 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007088 * Dispatch a pointer event.
7089 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007090 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
7091 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
7092 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08007093 * and should not be expected to handle other pointing device features.
7094 * </p>
7095 *
7096 * @param event The motion event to be dispatched.
7097 * @return True if the event was handled by the view, false otherwise.
7098 * @hide
7099 */
7100 public final boolean dispatchPointerEvent(MotionEvent event) {
7101 if (event.isTouchEvent()) {
7102 return dispatchTouchEvent(event);
7103 } else {
7104 return dispatchGenericMotionEvent(event);
7105 }
7106 }
7107
7108 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007109 * Called when the window containing this view gains or loses window focus.
7110 * ViewGroups should override to route to their children.
7111 *
7112 * @param hasFocus True if the window containing this view now has focus,
7113 * false otherwise.
7114 */
7115 public void dispatchWindowFocusChanged(boolean hasFocus) {
7116 onWindowFocusChanged(hasFocus);
7117 }
7118
7119 /**
7120 * Called when the window containing this view gains or loses focus. Note
7121 * that this is separate from view focus: to receive key events, both
7122 * your view and its window must have focus. If a window is displayed
7123 * on top of yours that takes input focus, then your own window will lose
7124 * focus but the view focus will remain unchanged.
7125 *
7126 * @param hasWindowFocus True if the window containing this view now has
7127 * focus, false otherwise.
7128 */
7129 public void onWindowFocusChanged(boolean hasWindowFocus) {
7130 InputMethodManager imm = InputMethodManager.peekInstance();
7131 if (!hasWindowFocus) {
7132 if (isPressed()) {
7133 setPressed(false);
7134 }
7135 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
7136 imm.focusOut(this);
7137 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007138 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08007139 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07007140 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007141 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
7142 imm.focusIn(this);
7143 }
7144 refreshDrawableState();
7145 }
7146
7147 /**
7148 * Returns true if this view is in a window that currently has window focus.
7149 * Note that this is not the same as the view itself having focus.
7150 *
7151 * @return True if this view is in a window that currently has window focus.
7152 */
7153 public boolean hasWindowFocus() {
7154 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
7155 }
7156
7157 /**
Adam Powell326d8082009-12-09 15:10:07 -08007158 * Dispatch a view visibility change down the view hierarchy.
7159 * ViewGroups should override to route to their children.
7160 * @param changedView The view whose visibility changed. Could be 'this' or
7161 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007162 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7163 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007164 */
7165 protected void dispatchVisibilityChanged(View changedView, int visibility) {
7166 onVisibilityChanged(changedView, visibility);
7167 }
7168
7169 /**
7170 * Called when the visibility of the view or an ancestor of the view is changed.
7171 * @param changedView The view whose visibility changed. Could be 'this' or
7172 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007173 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7174 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007175 */
7176 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007177 if (visibility == VISIBLE) {
7178 if (mAttachInfo != null) {
7179 initialAwakenScrollBars();
7180 } else {
7181 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
7182 }
7183 }
Adam Powell326d8082009-12-09 15:10:07 -08007184 }
7185
7186 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08007187 * Dispatch a hint about whether this view is displayed. For instance, when
7188 * a View moves out of the screen, it might receives a display hint indicating
7189 * the view is not displayed. Applications should not <em>rely</em> on this hint
7190 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007191 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007192 * @param hint A hint about whether or not this view is displayed:
7193 * {@link #VISIBLE} or {@link #INVISIBLE}.
7194 */
7195 public void dispatchDisplayHint(int hint) {
7196 onDisplayHint(hint);
7197 }
7198
7199 /**
7200 * Gives this view a hint about whether is displayed or not. For instance, when
7201 * a View moves out of the screen, it might receives a display hint indicating
7202 * the view is not displayed. Applications should not <em>rely</em> on this hint
7203 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007204 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007205 * @param hint A hint about whether or not this view is displayed:
7206 * {@link #VISIBLE} or {@link #INVISIBLE}.
7207 */
7208 protected void onDisplayHint(int hint) {
7209 }
7210
7211 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007212 * Dispatch a window visibility change down the view hierarchy.
7213 * ViewGroups should override to route to their children.
7214 *
7215 * @param visibility The new visibility of the window.
7216 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007217 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007218 */
7219 public void dispatchWindowVisibilityChanged(int visibility) {
7220 onWindowVisibilityChanged(visibility);
7221 }
7222
7223 /**
7224 * Called when the window containing has change its visibility
7225 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
7226 * that this tells you whether or not your window is being made visible
7227 * to the window manager; this does <em>not</em> tell you whether or not
7228 * your window is obscured by other windows on the screen, even if it
7229 * is itself visible.
7230 *
7231 * @param visibility The new visibility of the window.
7232 */
7233 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007234 if (visibility == VISIBLE) {
7235 initialAwakenScrollBars();
7236 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007237 }
7238
7239 /**
7240 * Returns the current visibility of the window this view is attached to
7241 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
7242 *
7243 * @return Returns the current visibility of the view's window.
7244 */
7245 public int getWindowVisibility() {
7246 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
7247 }
7248
7249 /**
7250 * Retrieve the overall visible display size in which the window this view is
7251 * attached to has been positioned in. This takes into account screen
7252 * decorations above the window, for both cases where the window itself
7253 * is being position inside of them or the window is being placed under
7254 * then and covered insets are used for the window to position its content
7255 * inside. In effect, this tells you the available area where content can
7256 * be placed and remain visible to users.
7257 *
7258 * <p>This function requires an IPC back to the window manager to retrieve
7259 * the requested information, so should not be used in performance critical
7260 * code like drawing.
7261 *
7262 * @param outRect Filled in with the visible display frame. If the view
7263 * is not attached to a window, this is simply the raw display size.
7264 */
7265 public void getWindowVisibleDisplayFrame(Rect outRect) {
7266 if (mAttachInfo != null) {
7267 try {
7268 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
7269 } catch (RemoteException e) {
7270 return;
7271 }
7272 // XXX This is really broken, and probably all needs to be done
7273 // in the window manager, and we need to know more about whether
7274 // we want the area behind or in front of the IME.
7275 final Rect insets = mAttachInfo.mVisibleInsets;
7276 outRect.left += insets.left;
7277 outRect.top += insets.top;
7278 outRect.right -= insets.right;
7279 outRect.bottom -= insets.bottom;
7280 return;
7281 }
7282 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07007283 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007284 }
7285
7286 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007287 * Dispatch a notification about a resource configuration change down
7288 * the view hierarchy.
7289 * ViewGroups should override to route to their children.
7290 *
7291 * @param newConfig The new resource configuration.
7292 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007293 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007294 */
7295 public void dispatchConfigurationChanged(Configuration newConfig) {
7296 onConfigurationChanged(newConfig);
7297 }
7298
7299 /**
7300 * Called when the current configuration of the resources being used
7301 * by the application have changed. You can use this to decide when
7302 * to reload resources that can changed based on orientation and other
7303 * configuration characterstics. You only need to use this if you are
7304 * not relying on the normal {@link android.app.Activity} mechanism of
7305 * recreating the activity instance upon a configuration change.
7306 *
7307 * @param newConfig The new resource configuration.
7308 */
7309 protected void onConfigurationChanged(Configuration newConfig) {
7310 }
7311
7312 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007313 * Private function to aggregate all per-view attributes in to the view
7314 * root.
7315 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007316 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7317 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007318 }
7319
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007320 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7321 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08007322 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007323 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007324 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007325 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007326 ListenerInfo li = mListenerInfo;
7327 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007328 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007329 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007330 }
7331 }
7332
7333 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08007334 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007335 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08007336 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
7337 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007338 ai.mRecomputeGlobalAttributes = true;
7339 }
7340 }
7341 }
7342
7343 /**
7344 * Returns whether the device is currently in touch mode. Touch mode is entered
7345 * once the user begins interacting with the device by touch, and affects various
7346 * things like whether focus is always visible to the user.
7347 *
7348 * @return Whether the device is in touch mode.
7349 */
7350 @ViewDebug.ExportedProperty
7351 public boolean isInTouchMode() {
7352 if (mAttachInfo != null) {
7353 return mAttachInfo.mInTouchMode;
7354 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07007355 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007356 }
7357 }
7358
7359 /**
7360 * Returns the context the view is running in, through which it can
7361 * access the current theme, resources, etc.
7362 *
7363 * @return The view's Context.
7364 */
7365 @ViewDebug.CapturedViewProperty
7366 public final Context getContext() {
7367 return mContext;
7368 }
7369
7370 /**
7371 * Handle a key event before it is processed by any input method
7372 * associated with the view hierarchy. This can be used to intercept
7373 * key events in special situations before the IME consumes them; a
7374 * typical example would be handling the BACK key to update the application's
7375 * UI instead of allowing the IME to see it and close itself.
7376 *
7377 * @param keyCode The value in event.getKeyCode().
7378 * @param event Description of the key event.
7379 * @return If you handled the event, return true. If you want to allow the
7380 * event to be handled by the next receiver, return false.
7381 */
7382 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7383 return false;
7384 }
7385
7386 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007387 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7388 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007389 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7390 * is released, if the view is enabled and clickable.
7391 *
7392 * @param keyCode A key code that represents the button pressed, from
7393 * {@link android.view.KeyEvent}.
7394 * @param event The KeyEvent object that defines the button action.
7395 */
7396 public boolean onKeyDown(int keyCode, KeyEvent event) {
7397 boolean result = false;
7398
7399 switch (keyCode) {
7400 case KeyEvent.KEYCODE_DPAD_CENTER:
7401 case KeyEvent.KEYCODE_ENTER: {
7402 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7403 return true;
7404 }
7405 // Long clickable items don't necessarily have to be clickable
7406 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7407 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7408 (event.getRepeatCount() == 0)) {
7409 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007410 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007411 return true;
7412 }
7413 break;
7414 }
7415 }
7416 return result;
7417 }
7418
7419 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007420 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7421 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7422 * the event).
7423 */
7424 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7425 return false;
7426 }
7427
7428 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007429 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7430 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007431 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7432 * {@link KeyEvent#KEYCODE_ENTER} is released.
7433 *
7434 * @param keyCode A key code that represents the button pressed, from
7435 * {@link android.view.KeyEvent}.
7436 * @param event The KeyEvent object that defines the button action.
7437 */
7438 public boolean onKeyUp(int keyCode, KeyEvent event) {
7439 boolean result = false;
7440
7441 switch (keyCode) {
7442 case KeyEvent.KEYCODE_DPAD_CENTER:
7443 case KeyEvent.KEYCODE_ENTER: {
7444 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7445 return true;
7446 }
7447 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7448 setPressed(false);
7449
7450 if (!mHasPerformedLongPress) {
7451 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007452 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007453
7454 result = performClick();
7455 }
7456 }
7457 break;
7458 }
7459 }
7460 return result;
7461 }
7462
7463 /**
7464 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7465 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7466 * the event).
7467 *
7468 * @param keyCode A key code that represents the button pressed, from
7469 * {@link android.view.KeyEvent}.
7470 * @param repeatCount The number of times the action was made.
7471 * @param event The KeyEvent object that defines the button action.
7472 */
7473 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7474 return false;
7475 }
7476
7477 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007478 * Called on the focused view when a key shortcut event is not handled.
7479 * Override this method to implement local key shortcuts for the View.
7480 * Key shortcuts can also be implemented by setting the
7481 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007482 *
7483 * @param keyCode The value in event.getKeyCode().
7484 * @param event Description of the key event.
7485 * @return If you handled the event, return true. If you want to allow the
7486 * event to be handled by the next receiver, return false.
7487 */
7488 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7489 return false;
7490 }
7491
7492 /**
7493 * Check whether the called view is a text editor, in which case it
7494 * would make sense to automatically display a soft input window for
7495 * it. Subclasses should override this if they implement
7496 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007497 * a call on that method would return a non-null InputConnection, and
7498 * they are really a first-class editor that the user would normally
7499 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007500 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007501 * <p>The default implementation always returns false. This does
7502 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7503 * will not be called or the user can not otherwise perform edits on your
7504 * view; it is just a hint to the system that this is not the primary
7505 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007506 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007507 * @return Returns true if this view is a text editor, else false.
7508 */
7509 public boolean onCheckIsTextEditor() {
7510 return false;
7511 }
Romain Guy8506ab42009-06-11 17:35:47 -07007512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007513 /**
7514 * Create a new InputConnection for an InputMethod to interact
7515 * with the view. The default implementation returns null, since it doesn't
7516 * support input methods. You can override this to implement such support.
7517 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007518 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007519 * <p>When implementing this, you probably also want to implement
7520 * {@link #onCheckIsTextEditor()} to indicate you will return a
7521 * non-null InputConnection.
7522 *
7523 * @param outAttrs Fill in with attribute information about the connection.
7524 */
7525 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7526 return null;
7527 }
7528
7529 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007530 * Called by the {@link android.view.inputmethod.InputMethodManager}
7531 * when a view who is not the current
7532 * input connection target is trying to make a call on the manager. The
7533 * default implementation returns false; you can override this to return
7534 * true for certain views if you are performing InputConnection proxying
7535 * to them.
7536 * @param view The View that is making the InputMethodManager call.
7537 * @return Return true to allow the call, false to reject.
7538 */
7539 public boolean checkInputConnectionProxy(View view) {
7540 return false;
7541 }
Romain Guy8506ab42009-06-11 17:35:47 -07007542
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007543 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007544 * Show the context menu for this view. It is not safe to hold on to the
7545 * menu after returning from this method.
7546 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007547 * You should normally not overload this method. Overload
7548 * {@link #onCreateContextMenu(ContextMenu)} or define an
7549 * {@link OnCreateContextMenuListener} to add items to the context menu.
7550 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007551 * @param menu The context menu to populate
7552 */
7553 public void createContextMenu(ContextMenu menu) {
7554 ContextMenuInfo menuInfo = getContextMenuInfo();
7555
7556 // Sets the current menu info so all items added to menu will have
7557 // my extra info set.
7558 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7559
7560 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007561 ListenerInfo li = mListenerInfo;
7562 if (li != null && li.mOnCreateContextMenuListener != null) {
7563 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007564 }
7565
7566 // Clear the extra information so subsequent items that aren't mine don't
7567 // have my extra info.
7568 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7569
7570 if (mParent != null) {
7571 mParent.createContextMenu(menu);
7572 }
7573 }
7574
7575 /**
7576 * Views should implement this if they have extra information to associate
7577 * with the context menu. The return result is supplied as a parameter to
7578 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7579 * callback.
7580 *
7581 * @return Extra information about the item for which the context menu
7582 * should be shown. This information will vary across different
7583 * subclasses of View.
7584 */
7585 protected ContextMenuInfo getContextMenuInfo() {
7586 return null;
7587 }
7588
7589 /**
7590 * Views should implement this if the view itself is going to add items to
7591 * the context menu.
7592 *
7593 * @param menu the context menu to populate
7594 */
7595 protected void onCreateContextMenu(ContextMenu menu) {
7596 }
7597
7598 /**
7599 * Implement this method to handle trackball motion events. The
7600 * <em>relative</em> movement of the trackball since the last event
7601 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
7602 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
7603 * that a movement of 1 corresponds to the user pressing one DPAD key (so
7604 * they will often be fractional values, representing the more fine-grained
7605 * movement information available from a trackball).
7606 *
7607 * @param event The motion event.
7608 * @return True if the event was handled, false otherwise.
7609 */
7610 public boolean onTrackballEvent(MotionEvent event) {
7611 return false;
7612 }
7613
7614 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08007615 * Implement this method to handle generic motion events.
7616 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08007617 * Generic motion events describe joystick movements, mouse hovers, track pad
7618 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08007619 * {@link MotionEvent#getSource() source} of the motion event specifies
7620 * the class of input that was received. Implementations of this method
7621 * must examine the bits in the source before processing the event.
7622 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007623 * </p><p>
7624 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7625 * are delivered to the view under the pointer. All other generic motion events are
7626 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08007627 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07007628 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08007629 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007630 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
7631 * // process the joystick movement...
7632 * return true;
7633 * }
7634 * }
7635 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
7636 * switch (event.getAction()) {
7637 * case MotionEvent.ACTION_HOVER_MOVE:
7638 * // process the mouse hover movement...
7639 * return true;
7640 * case MotionEvent.ACTION_SCROLL:
7641 * // process the scroll wheel movement...
7642 * return true;
7643 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08007644 * }
7645 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07007646 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08007647 *
7648 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08007649 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08007650 */
7651 public boolean onGenericMotionEvent(MotionEvent event) {
7652 return false;
7653 }
7654
7655 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007656 * Implement this method to handle hover events.
7657 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07007658 * This method is called whenever a pointer is hovering into, over, or out of the
7659 * bounds of a view and the view is not currently being touched.
7660 * Hover events are represented as pointer events with action
7661 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
7662 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
7663 * </p>
7664 * <ul>
7665 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
7666 * when the pointer enters the bounds of the view.</li>
7667 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
7668 * when the pointer has already entered the bounds of the view and has moved.</li>
7669 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
7670 * when the pointer has exited the bounds of the view or when the pointer is
7671 * about to go down due to a button click, tap, or similar user action that
7672 * causes the view to be touched.</li>
7673 * </ul>
7674 * <p>
7675 * The view should implement this method to return true to indicate that it is
7676 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08007677 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07007678 * The default implementation calls {@link #setHovered} to update the hovered state
7679 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07007680 * is enabled and is clickable. The default implementation also sends hover
7681 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08007682 * </p>
7683 *
7684 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07007685 * @return True if the view handled the hover event.
7686 *
7687 * @see #isHovered
7688 * @see #setHovered
7689 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007690 */
7691 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007692 // The root view may receive hover (or touch) events that are outside the bounds of
7693 // the window. This code ensures that we only send accessibility events for
7694 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07007695 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007696 if (!mSendingHoverAccessibilityEvents) {
7697 if ((action == MotionEvent.ACTION_HOVER_ENTER
7698 || action == MotionEvent.ACTION_HOVER_MOVE)
7699 && !hasHoveredChild()
7700 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007701 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007702 mSendingHoverAccessibilityEvents = true;
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07007703 requestAccessibilityFocusFromHover();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007704 }
7705 } else {
7706 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07007707 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007708 && !pointInView(event.getX(), event.getY()))) {
7709 mSendingHoverAccessibilityEvents = false;
7710 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007711 // If the window does not have input focus we take away accessibility
7712 // focus as soon as the user stop hovering over the view.
Jeff Brown59a422e2012-04-19 15:19:19 -07007713 if (mAttachInfo != null && !mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007714 getViewRootImpl().setAccessibilityFocusedHost(null);
7715 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007716 }
Jeff Browna1b24182011-07-28 13:38:24 -07007717 }
7718
Jeff Brown87b7f802011-06-21 18:35:45 -07007719 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007720 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07007721 case MotionEvent.ACTION_HOVER_ENTER:
7722 setHovered(true);
7723 break;
7724 case MotionEvent.ACTION_HOVER_EXIT:
7725 setHovered(false);
7726 break;
7727 }
Jeff Browna1b24182011-07-28 13:38:24 -07007728
7729 // Dispatch the event to onGenericMotionEvent before returning true.
7730 // This is to provide compatibility with existing applications that
7731 // handled HOVER_MOVE events in onGenericMotionEvent and that would
7732 // break because of the new default handling for hoverable views
7733 // in onHoverEvent.
7734 // Note that onGenericMotionEvent will be called by default when
7735 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
7736 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07007737 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08007738 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007739
Svetoslav Ganov736c2752011-04-22 18:30:36 -07007740 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08007741 }
7742
7743 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007744 * Returns true if the view should handle {@link #onHoverEvent}
7745 * by calling {@link #setHovered} to change its hovered state.
7746 *
7747 * @return True if the view is hoverable.
7748 */
7749 private boolean isHoverable() {
7750 final int viewFlags = mViewFlags;
7751 if ((viewFlags & ENABLED_MASK) == DISABLED) {
7752 return false;
7753 }
7754
7755 return (viewFlags & CLICKABLE) == CLICKABLE
7756 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
7757 }
7758
7759 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007760 * Returns true if the view is currently hovered.
7761 *
7762 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007763 *
7764 * @see #setHovered
7765 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007766 */
Jeff Brown10b62902011-06-20 16:40:37 -07007767 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08007768 public boolean isHovered() {
7769 return (mPrivateFlags & HOVERED) != 0;
7770 }
7771
7772 /**
7773 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007774 * <p>
7775 * Calling this method also changes the drawable state of the view. This
7776 * enables the view to react to hover by using different drawable resources
7777 * to change its appearance.
7778 * </p><p>
7779 * The {@link #onHoverChanged} method is called when the hovered state changes.
7780 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08007781 *
7782 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007783 *
7784 * @see #isHovered
7785 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007786 */
7787 public void setHovered(boolean hovered) {
7788 if (hovered) {
7789 if ((mPrivateFlags & HOVERED) == 0) {
7790 mPrivateFlags |= HOVERED;
7791 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007792 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08007793 }
7794 } else {
7795 if ((mPrivateFlags & HOVERED) != 0) {
7796 mPrivateFlags &= ~HOVERED;
7797 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007798 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08007799 }
7800 }
7801 }
7802
7803 /**
Jeff Brown10b62902011-06-20 16:40:37 -07007804 * Implement this method to handle hover state changes.
7805 * <p>
7806 * This method is called whenever the hover state changes as a result of a
7807 * call to {@link #setHovered}.
7808 * </p>
7809 *
7810 * @param hovered The current hover state, as returned by {@link #isHovered}.
7811 *
7812 * @see #isHovered
7813 * @see #setHovered
7814 */
7815 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07007816 }
7817
7818 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007819 * Implement this method to handle touch screen motion events.
7820 *
7821 * @param event The motion event.
7822 * @return True if the event was handled, false otherwise.
7823 */
7824 public boolean onTouchEvent(MotionEvent event) {
7825 final int viewFlags = mViewFlags;
7826
7827 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007828 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08007829 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007830 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007831 // A disabled view that is clickable still consumes the touch
7832 // events, it just doesn't respond to them.
7833 return (((viewFlags & CLICKABLE) == CLICKABLE ||
7834 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
7835 }
7836
7837 if (mTouchDelegate != null) {
7838 if (mTouchDelegate.onTouchEvent(event)) {
7839 return true;
7840 }
7841 }
7842
7843 if (((viewFlags & CLICKABLE) == CLICKABLE ||
7844 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
7845 switch (event.getAction()) {
7846 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08007847 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
7848 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007849 // take focus if we don't have it already and we should in
7850 // touch mode.
7851 boolean focusTaken = false;
7852 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
7853 focusTaken = requestFocus();
7854 }
7855
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007856 if (prepressed) {
7857 // The button is being released before we actually
7858 // showed it as pressed. Make it show the pressed
7859 // state now (before scheduling the click) to ensure
7860 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08007861 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007862 }
Joe Malin32736f02011-01-19 16:14:20 -08007863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007864 if (!mHasPerformedLongPress) {
7865 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007866 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007867
7868 // Only perform take click actions if we were in the pressed state
7869 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08007870 // Use a Runnable and post this rather than calling
7871 // performClick directly. This lets other visual state
7872 // of the view update before click actions start.
7873 if (mPerformClick == null) {
7874 mPerformClick = new PerformClick();
7875 }
7876 if (!post(mPerformClick)) {
7877 performClick();
7878 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007879 }
7880 }
7881
7882 if (mUnsetPressedState == null) {
7883 mUnsetPressedState = new UnsetPressedState();
7884 }
7885
Adam Powelle14579b2009-12-16 18:39:52 -08007886 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08007887 postDelayed(mUnsetPressedState,
7888 ViewConfiguration.getPressedStateDuration());
7889 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007890 // If the post failed, unpress right now
7891 mUnsetPressedState.run();
7892 }
Adam Powelle14579b2009-12-16 18:39:52 -08007893 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007894 }
7895 break;
7896
7897 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08007898 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007899
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07007900 if (performButtonActionOnTouchDown(event)) {
7901 break;
7902 }
7903
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007904 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07007905 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007906
7907 // For views inside a scrolling container, delay the pressed feedback for
7908 // a short period in case this is a scroll.
7909 if (isInScrollingContainer) {
7910 mPrivateFlags |= PREPRESSED;
7911 if (mPendingCheckForTap == null) {
7912 mPendingCheckForTap = new CheckForTap();
7913 }
7914 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
7915 } else {
7916 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08007917 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007918 checkForLongClick(0);
7919 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007920 break;
7921
7922 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08007923 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08007924 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007925 break;
7926
7927 case MotionEvent.ACTION_MOVE:
7928 final int x = (int) event.getX();
7929 final int y = (int) event.getY();
7930
7931 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07007932 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007933 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08007934 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007935 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08007936 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05007937 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007938
Adam Powell4d6f0662012-02-21 15:11:11 -08007939 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007940 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007941 }
7942 break;
7943 }
7944 return true;
7945 }
7946
7947 return false;
7948 }
7949
7950 /**
Adam Powell10298662011-08-14 18:26:30 -07007951 * @hide
7952 */
7953 public boolean isInScrollingContainer() {
7954 ViewParent p = getParent();
7955 while (p != null && p instanceof ViewGroup) {
7956 if (((ViewGroup) p).shouldDelayChildPressedState()) {
7957 return true;
7958 }
7959 p = p.getParent();
7960 }
7961 return false;
7962 }
7963
7964 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05007965 * Remove the longpress detection timer.
7966 */
7967 private void removeLongPressCallback() {
7968 if (mPendingCheckForLongPress != null) {
7969 removeCallbacks(mPendingCheckForLongPress);
7970 }
7971 }
Adam Powell3cb8b632011-01-21 15:34:14 -08007972
7973 /**
7974 * Remove the pending click action
7975 */
7976 private void removePerformClickCallback() {
7977 if (mPerformClick != null) {
7978 removeCallbacks(mPerformClick);
7979 }
7980 }
7981
Adam Powelle14579b2009-12-16 18:39:52 -08007982 /**
Romain Guya440b002010-02-24 15:57:54 -08007983 * Remove the prepress detection timer.
7984 */
7985 private void removeUnsetPressCallback() {
7986 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
7987 setPressed(false);
7988 removeCallbacks(mUnsetPressedState);
7989 }
7990 }
7991
7992 /**
Adam Powelle14579b2009-12-16 18:39:52 -08007993 * Remove the tap detection timer.
7994 */
7995 private void removeTapCallback() {
7996 if (mPendingCheckForTap != null) {
7997 mPrivateFlags &= ~PREPRESSED;
7998 removeCallbacks(mPendingCheckForTap);
7999 }
8000 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05008001
8002 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008003 * Cancels a pending long press. Your subclass can use this if you
8004 * want the context menu to come up if the user presses and holds
8005 * at the same place, but you don't want it to come up if they press
8006 * and then move around enough to cause scrolling.
8007 */
8008 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05008009 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08008010
8011 /*
8012 * The prepressed state handled by the tap callback is a display
8013 * construct, but the tap callback will post a long press callback
8014 * less its own timeout. Remove it here.
8015 */
8016 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008017 }
8018
8019 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008020 * Remove the pending callback for sending a
8021 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
8022 */
8023 private void removeSendViewScrolledAccessibilityEventCallback() {
8024 if (mSendViewScrolledAccessibilityEvent != null) {
8025 removeCallbacks(mSendViewScrolledAccessibilityEvent);
8026 }
8027 }
8028
8029 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008030 * Sets the TouchDelegate for this View.
8031 */
8032 public void setTouchDelegate(TouchDelegate delegate) {
8033 mTouchDelegate = delegate;
8034 }
8035
8036 /**
8037 * Gets the TouchDelegate for this View.
8038 */
8039 public TouchDelegate getTouchDelegate() {
8040 return mTouchDelegate;
8041 }
8042
8043 /**
8044 * Set flags controlling behavior of this view.
8045 *
8046 * @param flags Constant indicating the value which should be set
8047 * @param mask Constant indicating the bit range that should be changed
8048 */
8049 void setFlags(int flags, int mask) {
8050 int old = mViewFlags;
8051 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
8052
8053 int changed = mViewFlags ^ old;
8054 if (changed == 0) {
8055 return;
8056 }
8057 int privateFlags = mPrivateFlags;
8058
8059 /* Check if the FOCUSABLE bit has changed */
8060 if (((changed & FOCUSABLE_MASK) != 0) &&
8061 ((privateFlags & HAS_BOUNDS) !=0)) {
8062 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
8063 && ((privateFlags & FOCUSED) != 0)) {
8064 /* Give up focus if we are no longer focusable */
8065 clearFocus();
8066 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
8067 && ((privateFlags & FOCUSED) == 0)) {
8068 /*
8069 * Tell the view system that we are now available to take focus
8070 * if no one else already has it.
8071 */
8072 if (mParent != null) mParent.focusableViewAvailable(this);
8073 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008074 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8075 notifyAccessibilityStateChanged();
8076 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008077 }
8078
8079 if ((flags & VISIBILITY_MASK) == VISIBLE) {
8080 if ((changed & VISIBILITY_MASK) != 0) {
8081 /*
Chet Haase4324ead2011-08-24 21:31:03 -07008082 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07008083 * it was not visible. Marking it drawn ensures that the invalidation will
8084 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008085 */
Chet Haaseaceafe62011-08-26 15:44:33 -07008086 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07008087 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008088
8089 needGlobalAttributesUpdate(true);
8090
8091 // a view becoming visible is worth notifying the parent
8092 // about in case nothing has focus. even if this specific view
8093 // isn't focusable, it may contain something that is, so let
8094 // the root view try to give this focus if nothing else does.
8095 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
8096 mParent.focusableViewAvailable(this);
8097 }
8098 }
8099 }
8100
8101 /* Check if the GONE bit has changed */
8102 if ((changed & GONE) != 0) {
8103 needGlobalAttributesUpdate(false);
8104 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008105
Romain Guyecd80ee2009-12-03 17:13:02 -08008106 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
8107 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008108 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08008109 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07008110 if (mParent instanceof View) {
8111 // GONE views noop invalidation, so invalidate the parent
8112 ((View) mParent).invalidate(true);
8113 }
8114 // Mark the view drawn to ensure that it gets invalidated properly the next
8115 // time it is visible and gets invalidated
8116 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008117 }
8118 if (mAttachInfo != null) {
8119 mAttachInfo.mViewVisibilityChanged = true;
8120 }
8121 }
8122
8123 /* Check if the VISIBLE bit has changed */
8124 if ((changed & INVISIBLE) != 0) {
8125 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07008126 /*
8127 * If this view is becoming invisible, set the DRAWN flag so that
8128 * the next invalidate() will not be skipped.
8129 */
8130 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008131
8132 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008133 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008134 if (getRootView() != this) {
8135 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008136 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008137 }
8138 }
8139 if (mAttachInfo != null) {
8140 mAttachInfo.mViewVisibilityChanged = true;
8141 }
8142 }
8143
Adam Powell326d8082009-12-09 15:10:07 -08008144 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07008145 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08008146 ((ViewGroup) mParent).onChildVisibilityChanged(this,
8147 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08008148 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07008149 } else if (mParent != null) {
8150 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07008151 }
Adam Powell326d8082009-12-09 15:10:07 -08008152 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
8153 }
8154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008155 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
8156 destroyDrawingCache();
8157 }
8158
8159 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
8160 destroyDrawingCache();
8161 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008162 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008163 }
8164
8165 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
8166 destroyDrawingCache();
8167 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8168 }
8169
8170 if ((changed & DRAW_MASK) != 0) {
8171 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07008172 if (mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008173 mPrivateFlags &= ~SKIP_DRAW;
8174 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
8175 } else {
8176 mPrivateFlags |= SKIP_DRAW;
8177 }
8178 } else {
8179 mPrivateFlags &= ~SKIP_DRAW;
8180 }
8181 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08008182 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008183 }
8184
8185 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08008186 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008187 mParent.recomputeViewAttributes(this);
8188 }
8189 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008190
8191 if (AccessibilityManager.getInstance(mContext).isEnabled()
8192 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
8193 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
8194 notifyAccessibilityStateChanged();
8195 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008196 }
8197
8198 /**
8199 * Change the view's z order in the tree, so it's on top of other sibling
8200 * views
8201 */
8202 public void bringToFront() {
8203 if (mParent != null) {
8204 mParent.bringChildToFront(this);
8205 }
8206 }
8207
8208 /**
8209 * This is called in response to an internal scroll in this view (i.e., the
8210 * view scrolled its own contents). This is typically as a result of
8211 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
8212 * called.
8213 *
8214 * @param l Current horizontal scroll origin.
8215 * @param t Current vertical scroll origin.
8216 * @param oldl Previous horizontal scroll origin.
8217 * @param oldt Previous vertical scroll origin.
8218 */
8219 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07008220 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8221 postSendViewScrolledAccessibilityEventCallback();
8222 }
8223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008224 mBackgroundSizeChanged = true;
8225
8226 final AttachInfo ai = mAttachInfo;
8227 if (ai != null) {
8228 ai.mViewScrollChanged = true;
8229 }
8230 }
8231
8232 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008233 * Interface definition for a callback to be invoked when the layout bounds of a view
8234 * changes due to layout processing.
8235 */
8236 public interface OnLayoutChangeListener {
8237 /**
8238 * Called when the focus state of a view has changed.
8239 *
8240 * @param v The view whose state has changed.
8241 * @param left The new value of the view's left property.
8242 * @param top The new value of the view's top property.
8243 * @param right The new value of the view's right property.
8244 * @param bottom The new value of the view's bottom property.
8245 * @param oldLeft The previous value of the view's left property.
8246 * @param oldTop The previous value of the view's top property.
8247 * @param oldRight The previous value of the view's right property.
8248 * @param oldBottom The previous value of the view's bottom property.
8249 */
8250 void onLayoutChange(View v, int left, int top, int right, int bottom,
8251 int oldLeft, int oldTop, int oldRight, int oldBottom);
8252 }
8253
8254 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008255 * This is called during layout when the size of this view has changed. If
8256 * you were just added to the view hierarchy, you're called with the old
8257 * values of 0.
8258 *
8259 * @param w Current width of this view.
8260 * @param h Current height of this view.
8261 * @param oldw Old width of this view.
8262 * @param oldh Old height of this view.
8263 */
8264 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
8265 }
8266
8267 /**
8268 * Called by draw to draw the child views. This may be overridden
8269 * by derived classes to gain control just before its children are drawn
8270 * (but after its own view has been drawn).
8271 * @param canvas the canvas on which to draw the view
8272 */
8273 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008275 }
8276
8277 /**
8278 * Gets the parent of this view. Note that the parent is a
8279 * ViewParent and not necessarily a View.
8280 *
8281 * @return Parent of this view.
8282 */
8283 public final ViewParent getParent() {
8284 return mParent;
8285 }
8286
8287 /**
Chet Haasecca2c982011-05-20 14:34:18 -07008288 * Set the horizontal scrolled position of your view. This will cause a call to
8289 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8290 * invalidated.
8291 * @param value the x position to scroll to
8292 */
8293 public void setScrollX(int value) {
8294 scrollTo(value, mScrollY);
8295 }
8296
8297 /**
8298 * Set the vertical scrolled position of your view. This will cause a call to
8299 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8300 * invalidated.
8301 * @param value the y position to scroll to
8302 */
8303 public void setScrollY(int value) {
8304 scrollTo(mScrollX, value);
8305 }
8306
8307 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008308 * Return the scrolled left position of this view. This is the left edge of
8309 * the displayed part of your view. You do not need to draw any pixels
8310 * farther left, since those are outside of the frame of your view on
8311 * screen.
8312 *
8313 * @return The left edge of the displayed part of your view, in pixels.
8314 */
8315 public final int getScrollX() {
8316 return mScrollX;
8317 }
8318
8319 /**
8320 * Return the scrolled top position of this view. This is the top edge of
8321 * the displayed part of your view. You do not need to draw any pixels above
8322 * it, since those are outside of the frame of your view on screen.
8323 *
8324 * @return The top edge of the displayed part of your view, in pixels.
8325 */
8326 public final int getScrollY() {
8327 return mScrollY;
8328 }
8329
8330 /**
8331 * Return the width of the your view.
8332 *
8333 * @return The width of your view, in pixels.
8334 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008335 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008336 public final int getWidth() {
8337 return mRight - mLeft;
8338 }
8339
8340 /**
8341 * Return the height of your view.
8342 *
8343 * @return The height of your view, in pixels.
8344 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008345 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008346 public final int getHeight() {
8347 return mBottom - mTop;
8348 }
8349
8350 /**
8351 * Return the visible drawing bounds of your view. Fills in the output
8352 * rectangle with the values from getScrollX(), getScrollY(),
8353 * getWidth(), and getHeight().
8354 *
8355 * @param outRect The (scrolled) drawing bounds of the view.
8356 */
8357 public void getDrawingRect(Rect outRect) {
8358 outRect.left = mScrollX;
8359 outRect.top = mScrollY;
8360 outRect.right = mScrollX + (mRight - mLeft);
8361 outRect.bottom = mScrollY + (mBottom - mTop);
8362 }
8363
8364 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008365 * Like {@link #getMeasuredWidthAndState()}, but only returns the
8366 * raw width component (that is the result is masked by
8367 * {@link #MEASURED_SIZE_MASK}).
8368 *
8369 * @return The raw measured width of this view.
8370 */
8371 public final int getMeasuredWidth() {
8372 return mMeasuredWidth & MEASURED_SIZE_MASK;
8373 }
8374
8375 /**
8376 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008377 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008378 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008379 * This should be used during measurement and layout calculations only. Use
8380 * {@link #getWidth()} to see how wide a view is after layout.
8381 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008382 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008383 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008384 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008385 return mMeasuredWidth;
8386 }
8387
8388 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008389 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8390 * raw width component (that is the result is masked by
8391 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008392 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008393 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008394 */
8395 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008396 return mMeasuredHeight & MEASURED_SIZE_MASK;
8397 }
8398
8399 /**
8400 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008401 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008402 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8403 * This should be used during measurement and layout calculations only. Use
8404 * {@link #getHeight()} to see how wide a view is after layout.
8405 *
8406 * @return The measured width of this view as a bit mask.
8407 */
8408 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008409 return mMeasuredHeight;
8410 }
8411
8412 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008413 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8414 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8415 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8416 * and the height component is at the shifted bits
8417 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8418 */
8419 public final int getMeasuredState() {
8420 return (mMeasuredWidth&MEASURED_STATE_MASK)
8421 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8422 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8423 }
8424
8425 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008426 * The transform matrix of this view, which is calculated based on the current
8427 * roation, scale, and pivot properties.
8428 *
8429 * @see #getRotation()
8430 * @see #getScaleX()
8431 * @see #getScaleY()
8432 * @see #getPivotX()
8433 * @see #getPivotY()
8434 * @return The current transform matrix for the view
8435 */
8436 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008437 if (mTransformationInfo != null) {
8438 updateMatrix();
8439 return mTransformationInfo.mMatrix;
8440 }
8441 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008442 }
8443
8444 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008445 * Utility function to determine if the value is far enough away from zero to be
8446 * considered non-zero.
8447 * @param value A floating point value to check for zero-ness
8448 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8449 */
8450 private static boolean nonzero(float value) {
8451 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8452 }
8453
8454 /**
Jeff Brown86671742010-09-30 20:00:15 -07008455 * Returns true if the transform matrix is the identity matrix.
8456 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008457 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008458 * @return True if the transform matrix is the identity matrix, false otherwise.
8459 */
Jeff Brown86671742010-09-30 20:00:15 -07008460 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008461 if (mTransformationInfo != null) {
8462 updateMatrix();
8463 return mTransformationInfo.mMatrixIsIdentity;
8464 }
8465 return true;
8466 }
8467
8468 void ensureTransformationInfo() {
8469 if (mTransformationInfo == null) {
8470 mTransformationInfo = new TransformationInfo();
8471 }
Jeff Brown86671742010-09-30 20:00:15 -07008472 }
8473
8474 /**
8475 * Recomputes the transform matrix if necessary.
8476 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008477 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008478 final TransformationInfo info = mTransformationInfo;
8479 if (info == null) {
8480 return;
8481 }
8482 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008483 // transform-related properties have changed since the last time someone
8484 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008485
8486 // Figure out if we need to update the pivot point
8487 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008488 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8489 info.mPrevWidth = mRight - mLeft;
8490 info.mPrevHeight = mBottom - mTop;
8491 info.mPivotX = info.mPrevWidth / 2f;
8492 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008493 }
8494 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008495 info.mMatrix.reset();
8496 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8497 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8498 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8499 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008500 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008501 if (info.mCamera == null) {
8502 info.mCamera = new Camera();
8503 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008504 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008505 info.mCamera.save();
8506 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8507 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8508 info.mCamera.getMatrix(info.matrix3D);
8509 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8510 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8511 info.mPivotY + info.mTranslationY);
8512 info.mMatrix.postConcat(info.matrix3D);
8513 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008514 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008515 info.mMatrixDirty = false;
8516 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8517 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008518 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008519 }
8520
8521 /**
8522 * Utility method to retrieve the inverse of the current mMatrix property.
8523 * We cache the matrix to avoid recalculating it when transform properties
8524 * have not changed.
8525 *
8526 * @return The inverse of the current matrix of this view.
8527 */
Jeff Brown86671742010-09-30 20:00:15 -07008528 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008529 final TransformationInfo info = mTransformationInfo;
8530 if (info != null) {
8531 updateMatrix();
8532 if (info.mInverseMatrixDirty) {
8533 if (info.mInverseMatrix == null) {
8534 info.mInverseMatrix = new Matrix();
8535 }
8536 info.mMatrix.invert(info.mInverseMatrix);
8537 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008538 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008539 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008540 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008541 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008542 }
8543
8544 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008545 * Gets the distance along the Z axis from the camera to this view.
8546 *
8547 * @see #setCameraDistance(float)
8548 *
8549 * @return The distance along the Z axis.
8550 */
8551 public float getCameraDistance() {
8552 ensureTransformationInfo();
8553 final float dpi = mResources.getDisplayMetrics().densityDpi;
8554 final TransformationInfo info = mTransformationInfo;
8555 if (info.mCamera == null) {
8556 info.mCamera = new Camera();
8557 info.matrix3D = new Matrix();
8558 }
8559 return -(info.mCamera.getLocationZ() * dpi);
8560 }
8561
8562 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008563 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8564 * views are drawn) from the camera to this view. The camera's distance
8565 * affects 3D transformations, for instance rotations around the X and Y
8566 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008567 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008568 * use a camera distance that's greater than the height (X axis rotation) or
8569 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008570 *
Romain Guya5364ee2011-02-24 14:46:04 -08008571 * <p>The distance of the camera from the view plane can have an affect on the
8572 * perspective distortion of the view when it is rotated around the x or y axis.
8573 * For example, a large distance will result in a large viewing angle, and there
8574 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008575 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008576 * also result in some drawing artifacts if the rotated view ends up partially
8577 * behind the camera (which is why the recommendation is to use a distance at
8578 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008579 *
Romain Guya5364ee2011-02-24 14:46:04 -08008580 * <p>The distance is expressed in "depth pixels." The default distance depends
8581 * on the screen density. For instance, on a medium density display, the
8582 * default distance is 1280. On a high density display, the default distance
8583 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008584 *
Romain Guya5364ee2011-02-24 14:46:04 -08008585 * <p>If you want to specify a distance that leads to visually consistent
8586 * results across various densities, use the following formula:</p>
8587 * <pre>
8588 * float scale = context.getResources().getDisplayMetrics().density;
8589 * view.setCameraDistance(distance * scale);
8590 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07008591 *
Romain Guya5364ee2011-02-24 14:46:04 -08008592 * <p>The density scale factor of a high density display is 1.5,
8593 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008594 *
Romain Guya5364ee2011-02-24 14:46:04 -08008595 * @param distance The distance in "depth pixels", if negative the opposite
8596 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07008597 *
8598 * @see #setRotationX(float)
8599 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08008600 */
8601 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008602 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08008603
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008604 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08008605 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008606 final TransformationInfo info = mTransformationInfo;
8607 if (info.mCamera == null) {
8608 info.mCamera = new Camera();
8609 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08008610 }
8611
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008612 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
8613 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08008614
Chet Haase9d1992d2012-03-13 11:03:25 -07008615 invalidateViewProperty(false, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008616 if (mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07008617 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08008618 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008619 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8620 // View was rejected last time it was drawn by its parent; this may have changed
8621 invalidateParentIfNeeded();
8622 }
Romain Guya5364ee2011-02-24 14:46:04 -08008623 }
8624
8625 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008626 * The degrees that the view is rotated around the pivot point.
8627 *
Philip Milne6c8ea062012-04-03 17:38:43 -07008628 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07008629 * @see #getPivotX()
8630 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008631 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008632 * @return The degrees of rotation.
8633 */
Chet Haasea5531132012-02-02 13:41:44 -08008634 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008635 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008636 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008637 }
8638
8639 /**
Chet Haase897247b2010-09-09 14:54:47 -07008640 * Sets the degrees that the view is rotated around the pivot point. Increasing values
8641 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07008642 *
8643 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008644 *
8645 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07008646 * @see #getPivotX()
8647 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008648 * @see #setRotationX(float)
8649 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08008650 *
8651 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07008652 */
8653 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008654 ensureTransformationInfo();
8655 final TransformationInfo info = mTransformationInfo;
8656 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008657 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008658 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008659 info.mRotation = rotation;
8660 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008661 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008662 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008663 mDisplayList.setRotation(rotation);
8664 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008665 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8666 // View was rejected last time it was drawn by its parent; this may have changed
8667 invalidateParentIfNeeded();
8668 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008669 }
8670 }
8671
8672 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008673 * The degrees that the view is rotated around the vertical axis through the pivot point.
8674 *
8675 * @see #getPivotX()
8676 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008677 * @see #setRotationY(float)
8678 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008679 * @return The degrees of Y rotation.
8680 */
Chet Haasea5531132012-02-02 13:41:44 -08008681 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008682 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008683 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008684 }
8685
8686 /**
Chet Haase897247b2010-09-09 14:54:47 -07008687 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
8688 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
8689 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008690 *
Romain Guya5364ee2011-02-24 14:46:04 -08008691 * When rotating large views, it is recommended to adjust the camera distance
8692 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008693 *
8694 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008695 *
8696 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07008697 * @see #getPivotX()
8698 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008699 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008700 * @see #setRotationX(float)
8701 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008702 *
8703 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07008704 */
8705 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008706 ensureTransformationInfo();
8707 final TransformationInfo info = mTransformationInfo;
8708 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008709 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008710 info.mRotationY = rotationY;
8711 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008712 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008713 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008714 mDisplayList.setRotationY(rotationY);
8715 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008716 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8717 // View was rejected last time it was drawn by its parent; this may have changed
8718 invalidateParentIfNeeded();
8719 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008720 }
8721 }
8722
8723 /**
8724 * The degrees that the view is rotated around the horizontal axis through the pivot point.
8725 *
8726 * @see #getPivotX()
8727 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008728 * @see #setRotationX(float)
8729 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008730 * @return The degrees of X rotation.
8731 */
Chet Haasea5531132012-02-02 13:41:44 -08008732 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008733 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008734 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008735 }
8736
8737 /**
Chet Haase897247b2010-09-09 14:54:47 -07008738 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
8739 * Increasing values result in clockwise rotation from the viewpoint of looking down the
8740 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008741 *
Romain Guya5364ee2011-02-24 14:46:04 -08008742 * When rotating large views, it is recommended to adjust the camera distance
8743 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008744 *
8745 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008746 *
8747 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07008748 * @see #getPivotX()
8749 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008750 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008751 * @see #setRotationY(float)
8752 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008753 *
8754 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07008755 */
8756 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008757 ensureTransformationInfo();
8758 final TransformationInfo info = mTransformationInfo;
8759 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008760 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008761 info.mRotationX = rotationX;
8762 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008763 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008764 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008765 mDisplayList.setRotationX(rotationX);
8766 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008767 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8768 // View was rejected last time it was drawn by its parent; this may have changed
8769 invalidateParentIfNeeded();
8770 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008771 }
8772 }
8773
8774 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008775 * The amount that the view is scaled in x around the pivot point, as a proportion of
8776 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
8777 *
Joe Onorato93162322010-09-16 15:42:01 -04008778 * <p>By default, this is 1.0f.
8779 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008780 * @see #getPivotX()
8781 * @see #getPivotY()
8782 * @return The scaling factor.
8783 */
Chet Haasea5531132012-02-02 13:41:44 -08008784 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008785 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008786 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008787 }
8788
8789 /**
8790 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
8791 * the view's unscaled width. A value of 1 means that no scaling is applied.
8792 *
8793 * @param scaleX The scaling factor.
8794 * @see #getPivotX()
8795 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008796 *
8797 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07008798 */
8799 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008800 ensureTransformationInfo();
8801 final TransformationInfo info = mTransformationInfo;
8802 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008803 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008804 info.mScaleX = scaleX;
8805 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008806 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008807 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008808 mDisplayList.setScaleX(scaleX);
8809 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008810 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8811 // View was rejected last time it was drawn by its parent; this may have changed
8812 invalidateParentIfNeeded();
8813 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008814 }
8815 }
8816
8817 /**
8818 * The amount that the view is scaled in y around the pivot point, as a proportion of
8819 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
8820 *
Joe Onorato93162322010-09-16 15:42:01 -04008821 * <p>By default, this is 1.0f.
8822 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008823 * @see #getPivotX()
8824 * @see #getPivotY()
8825 * @return The scaling factor.
8826 */
Chet Haasea5531132012-02-02 13:41:44 -08008827 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008828 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008829 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008830 }
8831
8832 /**
8833 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
8834 * the view's unscaled width. A value of 1 means that no scaling is applied.
8835 *
8836 * @param scaleY The scaling factor.
8837 * @see #getPivotX()
8838 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008839 *
8840 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07008841 */
8842 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008843 ensureTransformationInfo();
8844 final TransformationInfo info = mTransformationInfo;
8845 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008846 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008847 info.mScaleY = scaleY;
8848 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008849 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008850 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008851 mDisplayList.setScaleY(scaleY);
8852 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008853 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8854 // View was rejected last time it was drawn by its parent; this may have changed
8855 invalidateParentIfNeeded();
8856 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008857 }
8858 }
8859
8860 /**
8861 * The x location of the point around which the view is {@link #setRotation(float) rotated}
8862 * and {@link #setScaleX(float) scaled}.
8863 *
8864 * @see #getRotation()
8865 * @see #getScaleX()
8866 * @see #getScaleY()
8867 * @see #getPivotY()
8868 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008869 *
8870 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008871 */
Chet Haasea5531132012-02-02 13:41:44 -08008872 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008873 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008874 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008875 }
8876
8877 /**
8878 * Sets the x location of the point around which the view is
8879 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07008880 * By default, the pivot point is centered on the object.
8881 * Setting this property disables this behavior and causes the view to use only the
8882 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008883 *
8884 * @param pivotX The x location of the pivot point.
8885 * @see #getRotation()
8886 * @see #getScaleX()
8887 * @see #getScaleY()
8888 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008889 *
8890 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008891 */
8892 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008893 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07008894 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008895 final TransformationInfo info = mTransformationInfo;
8896 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008897 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008898 info.mPivotX = pivotX;
8899 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008900 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008901 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008902 mDisplayList.setPivotX(pivotX);
8903 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008904 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8905 // View was rejected last time it was drawn by its parent; this may have changed
8906 invalidateParentIfNeeded();
8907 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008908 }
8909 }
8910
8911 /**
8912 * The y location of the point around which the view is {@link #setRotation(float) rotated}
8913 * and {@link #setScaleY(float) scaled}.
8914 *
8915 * @see #getRotation()
8916 * @see #getScaleX()
8917 * @see #getScaleY()
8918 * @see #getPivotY()
8919 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008920 *
8921 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07008922 */
Chet Haasea5531132012-02-02 13:41:44 -08008923 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008924 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008925 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008926 }
8927
8928 /**
8929 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07008930 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
8931 * Setting this property disables this behavior and causes the view to use only the
8932 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008933 *
8934 * @param pivotY The y location of the pivot point.
8935 * @see #getRotation()
8936 * @see #getScaleX()
8937 * @see #getScaleY()
8938 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008939 *
8940 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07008941 */
8942 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008943 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07008944 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008945 final TransformationInfo info = mTransformationInfo;
8946 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008947 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008948 info.mPivotY = pivotY;
8949 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008950 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008951 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008952 mDisplayList.setPivotY(pivotY);
8953 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008954 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8955 // View was rejected last time it was drawn by its parent; this may have changed
8956 invalidateParentIfNeeded();
8957 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008958 }
8959 }
8960
8961 /**
8962 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
8963 * completely transparent and 1 means the view is completely opaque.
8964 *
Joe Onorato93162322010-09-16 15:42:01 -04008965 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07008966 * @return The opacity of the view.
8967 */
Chet Haasea5531132012-02-02 13:41:44 -08008968 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008969 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008970 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008971 }
8972
8973 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07008974 * Returns whether this View has content which overlaps. This function, intended to be
8975 * overridden by specific View types, is an optimization when alpha is set on a view. If
8976 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
8977 * and then composited it into place, which can be expensive. If the view has no overlapping
8978 * rendering, the view can draw each primitive with the appropriate alpha value directly.
8979 * An example of overlapping rendering is a TextView with a background image, such as a
8980 * Button. An example of non-overlapping rendering is a TextView with no background, or
8981 * an ImageView with only the foreground image. The default implementation returns true;
8982 * subclasses should override if they have cases which can be optimized.
8983 *
8984 * @return true if the content in this view might overlap, false otherwise.
8985 */
8986 public boolean hasOverlappingRendering() {
8987 return true;
8988 }
8989
8990 /**
Romain Guy171c5922011-01-06 10:04:23 -08008991 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
8992 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008993 *
Romain Guy171c5922011-01-06 10:04:23 -08008994 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
8995 * responsible for applying the opacity itself. Otherwise, calling this method is
8996 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08008997 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07008998 *
Chet Haasea5531132012-02-02 13:41:44 -08008999 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
9000 * performance implications. It is generally best to use the alpha property sparingly and
9001 * transiently, as in the case of fading animations.</p>
9002 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009003 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08009004 *
Joe Malin32736f02011-01-19 16:14:20 -08009005 * @see #setLayerType(int, android.graphics.Paint)
9006 *
Chet Haase73066682010-11-29 15:55:32 -08009007 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07009008 */
9009 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009010 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009011 if (mTransformationInfo.mAlpha != alpha) {
9012 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009013 if (onSetAlpha((int) (alpha * 255))) {
9014 mPrivateFlags |= ALPHA_SET;
9015 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07009016 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009017 invalidate(true);
9018 } else {
9019 mPrivateFlags &= ~ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07009020 invalidateViewProperty(true, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07009021 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009022 mDisplayList.setAlpha(alpha);
9023 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009024 }
Chet Haaseed032702010-10-01 14:05:54 -07009025 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009026 }
9027
9028 /**
Chet Haasea00f3862011-02-22 06:34:40 -08009029 * Faster version of setAlpha() which performs the same steps except there are
9030 * no calls to invalidate(). The caller of this function should perform proper invalidation
9031 * on the parent and this object. The return value indicates whether the subclass handles
9032 * alpha (the return value for onSetAlpha()).
9033 *
9034 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009035 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
9036 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08009037 */
9038 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009039 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009040 if (mTransformationInfo.mAlpha != alpha) {
9041 mTransformationInfo.mAlpha = alpha;
9042 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
9043 if (subclassHandlesAlpha) {
9044 mPrivateFlags |= ALPHA_SET;
9045 return true;
9046 } else {
9047 mPrivateFlags &= ~ALPHA_SET;
Chet Haase1271e2c2012-04-20 09:54:27 -07009048 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009049 mDisplayList.setAlpha(alpha);
9050 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009051 }
Chet Haasea00f3862011-02-22 06:34:40 -08009052 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009053 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08009054 }
9055
9056 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009057 * Top position of this view relative to its parent.
9058 *
9059 * @return The top of this view, in pixels.
9060 */
9061 @ViewDebug.CapturedViewProperty
9062 public final int getTop() {
9063 return mTop;
9064 }
9065
9066 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009067 * Sets the top position of this view relative to its parent. This method is meant to be called
9068 * by the layout system and should not generally be called otherwise, because the property
9069 * may be changed at any time by the layout.
9070 *
9071 * @param top The top of this view, in pixels.
9072 */
9073 public final void setTop(int top) {
9074 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07009075 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009076 final boolean matrixIsIdentity = mTransformationInfo == null
9077 || mTransformationInfo.mMatrixIsIdentity;
9078 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009079 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009080 int minTop;
9081 int yLoc;
9082 if (top < mTop) {
9083 minTop = top;
9084 yLoc = top - mTop;
9085 } else {
9086 minTop = mTop;
9087 yLoc = 0;
9088 }
Chet Haasee9140a72011-02-16 16:23:29 -08009089 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009090 }
9091 } else {
9092 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009093 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009094 }
9095
Chet Haaseed032702010-10-01 14:05:54 -07009096 int width = mRight - mLeft;
9097 int oldHeight = mBottom - mTop;
9098
Chet Haase21cd1382010-09-01 17:42:29 -07009099 mTop = top;
Chet Haase1271e2c2012-04-20 09:54:27 -07009100 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009101 mDisplayList.setTop(mTop);
9102 }
Chet Haase21cd1382010-09-01 17:42:29 -07009103
Chet Haaseed032702010-10-01 14:05:54 -07009104 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9105
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009106 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009107 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9108 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009109 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009110 }
Chet Haase21cd1382010-09-01 17:42:29 -07009111 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009112 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009113 }
Chet Haase55dbb652010-12-21 20:15:08 -08009114 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009115 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009116 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9117 // View was rejected last time it was drawn by its parent; this may have changed
9118 invalidateParentIfNeeded();
9119 }
Chet Haase21cd1382010-09-01 17:42:29 -07009120 }
9121 }
9122
9123 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009124 * Bottom position of this view relative to its parent.
9125 *
9126 * @return The bottom of this view, in pixels.
9127 */
9128 @ViewDebug.CapturedViewProperty
9129 public final int getBottom() {
9130 return mBottom;
9131 }
9132
9133 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08009134 * True if this view has changed since the last time being drawn.
9135 *
9136 * @return The dirty state of this view.
9137 */
9138 public boolean isDirty() {
9139 return (mPrivateFlags & DIRTY_MASK) != 0;
9140 }
9141
9142 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009143 * Sets the bottom position of this view relative to its parent. This method is meant to be
9144 * called by the layout system and should not generally be called otherwise, because the
9145 * property may be changed at any time by the layout.
9146 *
9147 * @param bottom The bottom of this view, in pixels.
9148 */
9149 public final void setBottom(int bottom) {
9150 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07009151 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009152 final boolean matrixIsIdentity = mTransformationInfo == null
9153 || mTransformationInfo.mMatrixIsIdentity;
9154 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009155 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009156 int maxBottom;
9157 if (bottom < mBottom) {
9158 maxBottom = mBottom;
9159 } else {
9160 maxBottom = bottom;
9161 }
Chet Haasee9140a72011-02-16 16:23:29 -08009162 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009163 }
9164 } else {
9165 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009166 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009167 }
9168
Chet Haaseed032702010-10-01 14:05:54 -07009169 int width = mRight - mLeft;
9170 int oldHeight = mBottom - mTop;
9171
Chet Haase21cd1382010-09-01 17:42:29 -07009172 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -07009173 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009174 mDisplayList.setBottom(mBottom);
9175 }
Chet Haase21cd1382010-09-01 17:42:29 -07009176
Chet Haaseed032702010-10-01 14:05:54 -07009177 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9178
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009179 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009180 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9181 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009182 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009183 }
Chet Haase21cd1382010-09-01 17:42:29 -07009184 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009185 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009186 }
Chet Haase55dbb652010-12-21 20:15:08 -08009187 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009188 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009189 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9190 // View was rejected last time it was drawn by its parent; this may have changed
9191 invalidateParentIfNeeded();
9192 }
Chet Haase21cd1382010-09-01 17:42:29 -07009193 }
9194 }
9195
9196 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009197 * Left position of this view relative to its parent.
9198 *
9199 * @return The left edge of this view, in pixels.
9200 */
9201 @ViewDebug.CapturedViewProperty
9202 public final int getLeft() {
9203 return mLeft;
9204 }
9205
9206 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009207 * Sets the left position of this view relative to its parent. This method is meant to be called
9208 * by the layout system and should not generally be called otherwise, because the property
9209 * may be changed at any time by the layout.
9210 *
9211 * @param left The bottom of this view, in pixels.
9212 */
9213 public final void setLeft(int left) {
9214 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07009215 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009216 final boolean matrixIsIdentity = mTransformationInfo == null
9217 || mTransformationInfo.mMatrixIsIdentity;
9218 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009219 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009220 int minLeft;
9221 int xLoc;
9222 if (left < mLeft) {
9223 minLeft = left;
9224 xLoc = left - mLeft;
9225 } else {
9226 minLeft = mLeft;
9227 xLoc = 0;
9228 }
Chet Haasee9140a72011-02-16 16:23:29 -08009229 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009230 }
9231 } else {
9232 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009233 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009234 }
9235
Chet Haaseed032702010-10-01 14:05:54 -07009236 int oldWidth = mRight - mLeft;
9237 int height = mBottom - mTop;
9238
Chet Haase21cd1382010-09-01 17:42:29 -07009239 mLeft = left;
Chet Haase1271e2c2012-04-20 09:54:27 -07009240 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009241 mDisplayList.setLeft(left);
9242 }
Chet Haase21cd1382010-09-01 17:42:29 -07009243
Chet Haaseed032702010-10-01 14:05:54 -07009244 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9245
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009246 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009247 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9248 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009249 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009250 }
Chet Haase21cd1382010-09-01 17:42:29 -07009251 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009252 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009253 }
Chet Haase55dbb652010-12-21 20:15:08 -08009254 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009255 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009256 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9257 // View was rejected last time it was drawn by its parent; this may have changed
9258 invalidateParentIfNeeded();
9259 }
Chet Haase21cd1382010-09-01 17:42:29 -07009260 }
9261 }
9262
9263 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009264 * Right position of this view relative to its parent.
9265 *
9266 * @return The right edge of this view, in pixels.
9267 */
9268 @ViewDebug.CapturedViewProperty
9269 public final int getRight() {
9270 return mRight;
9271 }
9272
9273 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009274 * Sets the right position of this view relative to its parent. This method is meant to be called
9275 * by the layout system and should not generally be called otherwise, because the property
9276 * may be changed at any time by the layout.
9277 *
9278 * @param right The bottom of this view, in pixels.
9279 */
9280 public final void setRight(int right) {
9281 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07009282 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009283 final boolean matrixIsIdentity = mTransformationInfo == null
9284 || mTransformationInfo.mMatrixIsIdentity;
9285 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009286 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009287 int maxRight;
9288 if (right < mRight) {
9289 maxRight = mRight;
9290 } else {
9291 maxRight = right;
9292 }
Chet Haasee9140a72011-02-16 16:23:29 -08009293 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009294 }
9295 } else {
9296 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009297 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009298 }
9299
Chet Haaseed032702010-10-01 14:05:54 -07009300 int oldWidth = mRight - mLeft;
9301 int height = mBottom - mTop;
9302
Chet Haase21cd1382010-09-01 17:42:29 -07009303 mRight = right;
Chet Haase1271e2c2012-04-20 09:54:27 -07009304 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009305 mDisplayList.setRight(mRight);
9306 }
Chet Haase21cd1382010-09-01 17:42:29 -07009307
Chet Haaseed032702010-10-01 14:05:54 -07009308 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9309
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009310 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009311 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9312 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009313 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009314 }
Chet Haase21cd1382010-09-01 17:42:29 -07009315 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009316 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009317 }
Chet Haase55dbb652010-12-21 20:15:08 -08009318 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009319 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009320 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9321 // View was rejected last time it was drawn by its parent; this may have changed
9322 invalidateParentIfNeeded();
9323 }
Chet Haase21cd1382010-09-01 17:42:29 -07009324 }
9325 }
9326
9327 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009328 * The visual x position of this view, in pixels. This is equivalent to the
9329 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08009330 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07009331 *
Chet Haasedf030d22010-07-30 17:22:38 -07009332 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009333 */
Chet Haasea5531132012-02-02 13:41:44 -08009334 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009335 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009336 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009337 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009338
Chet Haasedf030d22010-07-30 17:22:38 -07009339 /**
9340 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
9341 * {@link #setTranslationX(float) translationX} property to be the difference between
9342 * the x value passed in and the current {@link #getLeft() left} property.
9343 *
9344 * @param x The visual x position of this view, in pixels.
9345 */
9346 public void setX(float x) {
9347 setTranslationX(x - mLeft);
9348 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009349
Chet Haasedf030d22010-07-30 17:22:38 -07009350 /**
9351 * The visual y position of this view, in pixels. This is equivalent to the
9352 * {@link #setTranslationY(float) translationY} property plus the current
9353 * {@link #getTop() top} property.
9354 *
9355 * @return The visual y position of this view, in pixels.
9356 */
Chet Haasea5531132012-02-02 13:41:44 -08009357 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009358 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009359 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009360 }
9361
9362 /**
9363 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
9364 * {@link #setTranslationY(float) translationY} property to be the difference between
9365 * the y value passed in and the current {@link #getTop() top} property.
9366 *
9367 * @param y The visual y position of this view, in pixels.
9368 */
9369 public void setY(float y) {
9370 setTranslationY(y - mTop);
9371 }
9372
9373
9374 /**
9375 * The horizontal location of this view relative to its {@link #getLeft() left} position.
9376 * This position is post-layout, in addition to wherever the object's
9377 * layout placed it.
9378 *
9379 * @return The horizontal position of this view relative to its left position, in pixels.
9380 */
Chet Haasea5531132012-02-02 13:41:44 -08009381 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009382 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009383 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07009384 }
9385
9386 /**
9387 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
9388 * This effectively positions the object post-layout, in addition to wherever the object's
9389 * layout placed it.
9390 *
9391 * @param translationX The horizontal position of this view relative to its left position,
9392 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009393 *
9394 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07009395 */
9396 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009397 ensureTransformationInfo();
9398 final TransformationInfo info = mTransformationInfo;
9399 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07009400 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009401 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009402 info.mTranslationX = translationX;
9403 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009404 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009405 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009406 mDisplayList.setTranslationX(translationX);
9407 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009408 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9409 // View was rejected last time it was drawn by its parent; this may have changed
9410 invalidateParentIfNeeded();
9411 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009412 }
9413 }
9414
9415 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009416 * The horizontal location of this view relative to its {@link #getTop() top} position.
9417 * This position is post-layout, in addition to wherever the object's
9418 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009419 *
Chet Haasedf030d22010-07-30 17:22:38 -07009420 * @return The vertical position of this view relative to its top position,
9421 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009422 */
Chet Haasea5531132012-02-02 13:41:44 -08009423 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009424 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009425 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009426 }
9427
9428 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009429 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9430 * This effectively positions the object post-layout, in addition to wherever the object's
9431 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009432 *
Chet Haasedf030d22010-07-30 17:22:38 -07009433 * @param translationY The vertical position of this view relative to its top position,
9434 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009435 *
9436 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009437 */
Chet Haasedf030d22010-07-30 17:22:38 -07009438 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009439 ensureTransformationInfo();
9440 final TransformationInfo info = mTransformationInfo;
9441 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009442 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009443 info.mTranslationY = translationY;
9444 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009445 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009446 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009447 mDisplayList.setTranslationY(translationY);
9448 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009449 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9450 // View was rejected last time it was drawn by its parent; this may have changed
9451 invalidateParentIfNeeded();
9452 }
Chet Haasedf030d22010-07-30 17:22:38 -07009453 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009454 }
9455
9456 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009457 * Hit rectangle in parent's coordinates
9458 *
9459 * @param outRect The hit rectangle of the view.
9460 */
9461 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009462 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009463 final TransformationInfo info = mTransformationInfo;
9464 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009465 outRect.set(mLeft, mTop, mRight, mBottom);
9466 } else {
9467 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009468 tmpRect.set(-info.mPivotX, -info.mPivotY,
9469 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9470 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009471 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9472 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009473 }
9474 }
9475
9476 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009477 * Determines whether the given point, in local coordinates is inside the view.
9478 */
9479 /*package*/ final boolean pointInView(float localX, float localY) {
9480 return localX >= 0 && localX < (mRight - mLeft)
9481 && localY >= 0 && localY < (mBottom - mTop);
9482 }
9483
9484 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009485 * Utility method to determine whether the given point, in local coordinates,
9486 * is inside the view, where the area of the view is expanded by the slop factor.
9487 * This method is called while processing touch-move events to determine if the event
9488 * is still within the view.
9489 */
9490 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009491 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009492 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009493 }
9494
9495 /**
9496 * When a view has focus and the user navigates away from it, the next view is searched for
9497 * starting from the rectangle filled in by this method.
9498 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009499 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
9500 * of the view. However, if your view maintains some idea of internal selection,
9501 * such as a cursor, or a selected row or column, you should override this method and
9502 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009503 *
9504 * @param r The rectangle to fill in, in this view's coordinates.
9505 */
9506 public void getFocusedRect(Rect r) {
9507 getDrawingRect(r);
9508 }
9509
9510 /**
9511 * If some part of this view is not clipped by any of its parents, then
9512 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009513 * coordinates (without taking possible View rotations into account), offset
9514 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9515 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009516 *
9517 * @param r If true is returned, r holds the global coordinates of the
9518 * visible portion of this view.
9519 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9520 * between this view and its root. globalOffet may be null.
9521 * @return true if r is non-empty (i.e. part of the view is visible at the
9522 * root level.
9523 */
9524 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9525 int width = mRight - mLeft;
9526 int height = mBottom - mTop;
9527 if (width > 0 && height > 0) {
9528 r.set(0, 0, width, height);
9529 if (globalOffset != null) {
9530 globalOffset.set(-mScrollX, -mScrollY);
9531 }
9532 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9533 }
9534 return false;
9535 }
9536
9537 public final boolean getGlobalVisibleRect(Rect r) {
9538 return getGlobalVisibleRect(r, null);
9539 }
9540
9541 public final boolean getLocalVisibleRect(Rect r) {
Romain Guyab4c4f4f2012-05-06 13:11:24 -07009542 final Point offset = mAttachInfo != null ? mAttachInfo.mPoint : new Point();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009543 if (getGlobalVisibleRect(r, offset)) {
9544 r.offset(-offset.x, -offset.y); // make r local
9545 return true;
9546 }
9547 return false;
9548 }
9549
9550 /**
9551 * Offset this view's vertical location by the specified number of pixels.
9552 *
9553 * @param offset the number of pixels to offset the view by
9554 */
9555 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009556 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009557 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009558 final boolean matrixIsIdentity = mTransformationInfo == null
9559 || mTransformationInfo.mMatrixIsIdentity;
9560 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009561 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009562 invalidateViewProperty(false, false);
9563 } else {
9564 final ViewParent p = mParent;
9565 if (p != null && mAttachInfo != null) {
9566 final Rect r = mAttachInfo.mTmpInvalRect;
9567 int minTop;
9568 int maxBottom;
9569 int yLoc;
9570 if (offset < 0) {
9571 minTop = mTop + offset;
9572 maxBottom = mBottom;
9573 yLoc = offset;
9574 } else {
9575 minTop = mTop;
9576 maxBottom = mBottom + offset;
9577 yLoc = 0;
9578 }
9579 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
9580 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009581 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009582 }
9583 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009584 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009585 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009586
Chet Haasec3aa3612010-06-17 08:50:37 -07009587 mTop += offset;
9588 mBottom += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009589 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009590 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009591 invalidateViewProperty(false, false);
9592 } else {
9593 if (!matrixIsIdentity) {
9594 invalidateViewProperty(false, true);
9595 }
9596 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009597 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009598 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009599 }
9600
9601 /**
9602 * Offset this view's horizontal location by the specified amount of pixels.
9603 *
9604 * @param offset the numer of pixels to offset the view by
9605 */
9606 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009607 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009608 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009609 final boolean matrixIsIdentity = mTransformationInfo == null
9610 || mTransformationInfo.mMatrixIsIdentity;
9611 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009612 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009613 invalidateViewProperty(false, false);
9614 } else {
9615 final ViewParent p = mParent;
9616 if (p != null && mAttachInfo != null) {
9617 final Rect r = mAttachInfo.mTmpInvalRect;
9618 int minLeft;
9619 int maxRight;
9620 if (offset < 0) {
9621 minLeft = mLeft + offset;
9622 maxRight = mRight;
9623 } else {
9624 minLeft = mLeft;
9625 maxRight = mRight + offset;
9626 }
9627 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
9628 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009629 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009630 }
9631 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009632 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009633 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009634
Chet Haasec3aa3612010-06-17 08:50:37 -07009635 mLeft += offset;
9636 mRight += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009637 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009638 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009639 invalidateViewProperty(false, false);
9640 } else {
9641 if (!matrixIsIdentity) {
9642 invalidateViewProperty(false, true);
9643 }
9644 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009645 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009646 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009647 }
9648
9649 /**
9650 * Get the LayoutParams associated with this view. All views should have
9651 * layout parameters. These supply parameters to the <i>parent</i> of this
9652 * view specifying how it should be arranged. There are many subclasses of
9653 * ViewGroup.LayoutParams, and these correspond to the different subclasses
9654 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08009655 *
9656 * This method may return null if this View is not attached to a parent
9657 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
9658 * was not invoked successfully. When a View is attached to a parent
9659 * ViewGroup, this method must not return null.
9660 *
9661 * @return The LayoutParams associated with this view, or null if no
9662 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009663 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07009664 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009665 public ViewGroup.LayoutParams getLayoutParams() {
9666 return mLayoutParams;
9667 }
9668
9669 /**
9670 * Set the layout parameters associated with this view. These supply
9671 * parameters to the <i>parent</i> of this view specifying how it should be
9672 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
9673 * correspond to the different subclasses of ViewGroup that are responsible
9674 * for arranging their children.
9675 *
Romain Guy01c174b2011-02-22 11:51:06 -08009676 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009677 */
9678 public void setLayoutParams(ViewGroup.LayoutParams params) {
9679 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08009680 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009681 }
9682 mLayoutParams = params;
Philip Milned7dd8902012-01-26 16:55:30 -08009683 if (mParent instanceof ViewGroup) {
9684 ((ViewGroup) mParent).onSetLayoutParams(this, params);
9685 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009686 requestLayout();
9687 }
9688
9689 /**
9690 * Set the scrolled position of your view. This will cause a call to
9691 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9692 * invalidated.
9693 * @param x the x position to scroll to
9694 * @param y the y position to scroll to
9695 */
9696 public void scrollTo(int x, int y) {
9697 if (mScrollX != x || mScrollY != y) {
9698 int oldX = mScrollX;
9699 int oldY = mScrollY;
9700 mScrollX = x;
9701 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009702 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009703 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07009704 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009705 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -07009706 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009707 }
9708 }
9709
9710 /**
9711 * Move the scrolled position of your view. This will cause a call to
9712 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9713 * invalidated.
9714 * @param x the amount of pixels to scroll by horizontally
9715 * @param y the amount of pixels to scroll by vertically
9716 */
9717 public void scrollBy(int x, int y) {
9718 scrollTo(mScrollX + x, mScrollY + y);
9719 }
9720
9721 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009722 * <p>Trigger the scrollbars to draw. When invoked this method starts an
9723 * animation to fade the scrollbars out after a default delay. If a subclass
9724 * provides animated scrolling, the start delay should equal the duration
9725 * of the scrolling animation.</p>
9726 *
9727 * <p>The animation starts only if at least one of the scrollbars is
9728 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
9729 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9730 * this method returns true, and false otherwise. If the animation is
9731 * started, this method calls {@link #invalidate()}; in that case the
9732 * caller should not call {@link #invalidate()}.</p>
9733 *
9734 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07009735 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07009736 *
9737 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
9738 * and {@link #scrollTo(int, int)}.</p>
9739 *
9740 * @return true if the animation is played, false otherwise
9741 *
9742 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07009743 * @see #scrollBy(int, int)
9744 * @see #scrollTo(int, int)
9745 * @see #isHorizontalScrollBarEnabled()
9746 * @see #isVerticalScrollBarEnabled()
9747 * @see #setHorizontalScrollBarEnabled(boolean)
9748 * @see #setVerticalScrollBarEnabled(boolean)
9749 */
9750 protected boolean awakenScrollBars() {
9751 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07009752 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07009753 }
9754
9755 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07009756 * Trigger the scrollbars to draw.
9757 * This method differs from awakenScrollBars() only in its default duration.
9758 * initialAwakenScrollBars() will show the scroll bars for longer than
9759 * usual to give the user more of a chance to notice them.
9760 *
9761 * @return true if the animation is played, false otherwise.
9762 */
9763 private boolean initialAwakenScrollBars() {
9764 return mScrollCache != null &&
9765 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
9766 }
9767
9768 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009769 * <p>
9770 * Trigger the scrollbars to draw. When invoked this method starts an
9771 * animation to fade the scrollbars out after a fixed delay. If a subclass
9772 * provides animated scrolling, the start delay should equal the duration of
9773 * the scrolling animation.
9774 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009775 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009776 * <p>
9777 * The animation starts only if at least one of the scrollbars is enabled,
9778 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9779 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9780 * this method returns true, and false otherwise. If the animation is
9781 * started, this method calls {@link #invalidate()}; in that case the caller
9782 * should not call {@link #invalidate()}.
9783 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009784 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009785 * <p>
9786 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07009787 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07009788 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009789 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009790 * @param startDelay the delay, in milliseconds, after which the animation
9791 * should start; when the delay is 0, the animation starts
9792 * immediately
9793 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009794 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009795 * @see #scrollBy(int, int)
9796 * @see #scrollTo(int, int)
9797 * @see #isHorizontalScrollBarEnabled()
9798 * @see #isVerticalScrollBarEnabled()
9799 * @see #setHorizontalScrollBarEnabled(boolean)
9800 * @see #setVerticalScrollBarEnabled(boolean)
9801 */
9802 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07009803 return awakenScrollBars(startDelay, true);
9804 }
Joe Malin32736f02011-01-19 16:14:20 -08009805
Mike Cleron290947b2009-09-29 18:34:32 -07009806 /**
9807 * <p>
9808 * Trigger the scrollbars to draw. When invoked this method starts an
9809 * animation to fade the scrollbars out after a fixed delay. If a subclass
9810 * provides animated scrolling, the start delay should equal the duration of
9811 * the scrolling animation.
9812 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009813 *
Mike Cleron290947b2009-09-29 18:34:32 -07009814 * <p>
9815 * The animation starts only if at least one of the scrollbars is enabled,
9816 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9817 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9818 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08009819 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07009820 * is set to true; in that case the caller
9821 * should not call {@link #invalidate()}.
9822 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009823 *
Mike Cleron290947b2009-09-29 18:34:32 -07009824 * <p>
9825 * This method should be invoked everytime a subclass directly updates the
9826 * scroll parameters.
9827 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009828 *
Mike Cleron290947b2009-09-29 18:34:32 -07009829 * @param startDelay the delay, in milliseconds, after which the animation
9830 * should start; when the delay is 0, the animation starts
9831 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08009832 *
Mike Cleron290947b2009-09-29 18:34:32 -07009833 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08009834 *
Mike Cleron290947b2009-09-29 18:34:32 -07009835 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009836 *
Mike Cleron290947b2009-09-29 18:34:32 -07009837 * @see #scrollBy(int, int)
9838 * @see #scrollTo(int, int)
9839 * @see #isHorizontalScrollBarEnabled()
9840 * @see #isVerticalScrollBarEnabled()
9841 * @see #setHorizontalScrollBarEnabled(boolean)
9842 * @see #setVerticalScrollBarEnabled(boolean)
9843 */
9844 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07009845 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08009846
Mike Cleronf116bf82009-09-27 19:14:12 -07009847 if (scrollCache == null || !scrollCache.fadeScrollBars) {
9848 return false;
9849 }
9850
9851 if (scrollCache.scrollBar == null) {
9852 scrollCache.scrollBar = new ScrollBarDrawable();
9853 }
9854
9855 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
9856
Mike Cleron290947b2009-09-29 18:34:32 -07009857 if (invalidate) {
9858 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009859 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -07009860 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009861
9862 if (scrollCache.state == ScrollabilityCache.OFF) {
9863 // FIXME: this is copied from WindowManagerService.
9864 // We should get this value from the system when it
9865 // is possible to do so.
9866 final int KEY_REPEAT_FIRST_DELAY = 750;
9867 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
9868 }
9869
9870 // Tell mScrollCache when we should start fading. This may
9871 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07009872 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07009873 scrollCache.fadeStartTime = fadeStartTime;
9874 scrollCache.state = ScrollabilityCache.ON;
9875
9876 // Schedule our fader to run, unscheduling any old ones first
9877 if (mAttachInfo != null) {
9878 mAttachInfo.mHandler.removeCallbacks(scrollCache);
9879 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
9880 }
9881
9882 return true;
9883 }
9884
9885 return false;
9886 }
9887
9888 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07009889 * Do not invalidate views which are not visible and which are not running an animation. They
9890 * will not get drawn and they should not set dirty flags as if they will be drawn
9891 */
9892 private boolean skipInvalidate() {
9893 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
9894 (!(mParent instanceof ViewGroup) ||
9895 !((ViewGroup) mParent).isViewTransitioning(this));
9896 }
9897 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07009898 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009899 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
9900 * in the future. This must be called from a UI thread. To call from a non-UI
9901 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009902 *
9903 * WARNING: This method is destructive to dirty.
9904 * @param dirty the rectangle representing the bounds of the dirty region
9905 */
9906 public void invalidate(Rect dirty) {
Chet Haaseaceafe62011-08-26 15:44:33 -07009907 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009908 return;
9909 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009910 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08009911 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
9912 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009913 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08009914 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07009915 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009916 final ViewParent p = mParent;
9917 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08009918 //noinspection PointlessBooleanExpression,ConstantConditions
9919 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9920 if (p != null && ai != null && ai.mHardwareAccelerated) {
9921 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009922 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009923 p.invalidateChild(this, null);
9924 return;
9925 }
Romain Guyaf636eb2010-12-09 17:47:21 -08009926 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009927 if (p != null && ai != null) {
9928 final int scrollX = mScrollX;
9929 final int scrollY = mScrollY;
9930 final Rect r = ai.mTmpInvalRect;
9931 r.set(dirty.left - scrollX, dirty.top - scrollY,
9932 dirty.right - scrollX, dirty.bottom - scrollY);
9933 mParent.invalidateChild(this, r);
9934 }
9935 }
9936 }
9937
9938 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07009939 * 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 -08009940 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009941 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
9942 * will be called at some point in the future. This must be called from
9943 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009944 * @param l the left position of the dirty region
9945 * @param t the top position of the dirty region
9946 * @param r the right position of the dirty region
9947 * @param b the bottom position of the dirty region
9948 */
9949 public void invalidate(int l, int t, int r, int b) {
Chet Haaseaceafe62011-08-26 15:44:33 -07009950 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009951 return;
9952 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009953 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08009954 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
9955 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009956 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08009957 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07009958 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009959 final ViewParent p = mParent;
9960 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08009961 //noinspection PointlessBooleanExpression,ConstantConditions
9962 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9963 if (p != null && ai != null && ai.mHardwareAccelerated) {
9964 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009965 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009966 p.invalidateChild(this, null);
9967 return;
9968 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08009969 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009970 if (p != null && ai != null && l < r && t < b) {
9971 final int scrollX = mScrollX;
9972 final int scrollY = mScrollY;
9973 final Rect tmpr = ai.mTmpInvalRect;
9974 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
9975 p.invalidateChild(this, tmpr);
9976 }
9977 }
9978 }
9979
9980 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009981 * Invalidate the whole view. If the view is visible,
9982 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
9983 * the future. This must be called from a UI thread. To call from a non-UI thread,
9984 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009985 */
9986 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07009987 invalidate(true);
9988 }
Joe Malin32736f02011-01-19 16:14:20 -08009989
Chet Haaseed032702010-10-01 14:05:54 -07009990 /**
9991 * This is where the invalidate() work actually happens. A full invalidate()
9992 * causes the drawing cache to be invalidated, but this function can be called with
9993 * invalidateCache set to false to skip that invalidation step for cases that do not
9994 * need it (for example, a component that remains at the same dimensions with the same
9995 * content).
9996 *
9997 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
9998 * well. This is usually true for a full invalidate, but may be set to false if the
9999 * View's contents or dimensions have not changed.
10000 */
Romain Guy849d0a32011-02-01 17:20:48 -080010001 void invalidate(boolean invalidateCache) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010002 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010003 return;
10004 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010005 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -080010006 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -080010007 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
10008 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -070010009 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -070010010 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -070010011 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -080010012 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -070010013 mPrivateFlags &= ~DRAWING_CACHE_VALID;
10014 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010015 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -070010016 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -080010017 //noinspection PointlessBooleanExpression,ConstantConditions
10018 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10019 if (p != null && ai != null && ai.mHardwareAccelerated) {
10020 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010021 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010022 p.invalidateChild(this, null);
10023 return;
10024 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010025 }
Michael Jurkaebefea42010-11-15 16:04:01 -080010026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010027 if (p != null && ai != null) {
10028 final Rect r = ai.mTmpInvalRect;
10029 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10030 // Don't call invalidate -- we don't want to internally scroll
10031 // our own bounds
10032 p.invalidateChild(this, r);
10033 }
10034 }
10035 }
10036
10037 /**
Chet Haase9d1992d2012-03-13 11:03:25 -070010038 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
10039 * set any flags or handle all of the cases handled by the default invalidation methods.
10040 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
10041 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
10042 * walk up the hierarchy, transforming the dirty rect as necessary.
10043 *
10044 * The method also handles normal invalidation logic if display list properties are not
10045 * being used in this view. The invalidateParent and forceRedraw flags are used by that
10046 * backup approach, to handle these cases used in the various property-setting methods.
10047 *
10048 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
10049 * are not being used in this view
10050 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
10051 * list properties are not being used in this view
10052 */
10053 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
Chet Haase1271e2c2012-04-20 09:54:27 -070010054 if (mDisplayList == null || (mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
Chet Haase9d1992d2012-03-13 11:03:25 -070010055 if (invalidateParent) {
10056 invalidateParentCaches();
10057 }
10058 if (forceRedraw) {
10059 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
10060 }
10061 invalidate(false);
10062 } else {
10063 final AttachInfo ai = mAttachInfo;
10064 final ViewParent p = mParent;
10065 if (p != null && ai != null) {
10066 final Rect r = ai.mTmpInvalRect;
10067 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10068 if (mParent instanceof ViewGroup) {
10069 ((ViewGroup) mParent).invalidateChildFast(this, r);
10070 } else {
10071 mParent.invalidateChild(this, r);
10072 }
10073 }
10074 }
10075 }
10076
10077 /**
10078 * Utility method to transform a given Rect by the current matrix of this view.
10079 */
10080 void transformRect(final Rect rect) {
10081 if (!getMatrix().isIdentity()) {
10082 RectF boundingRect = mAttachInfo.mTmpTransformRect;
10083 boundingRect.set(rect);
10084 getMatrix().mapRect(boundingRect);
10085 rect.set((int) (boundingRect.left - 0.5f),
10086 (int) (boundingRect.top - 0.5f),
10087 (int) (boundingRect.right + 0.5f),
10088 (int) (boundingRect.bottom + 0.5f));
10089 }
10090 }
10091
10092 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -080010093 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -080010094 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10095 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -080010096 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
10097 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -080010098 *
10099 * @hide
10100 */
Romain Guy0fd89bf2011-01-26 15:41:30 -080010101 protected void invalidateParentCaches() {
10102 if (mParent instanceof View) {
10103 ((View) mParent).mPrivateFlags |= INVALIDATED;
10104 }
10105 }
Joe Malin32736f02011-01-19 16:14:20 -080010106
Romain Guy0fd89bf2011-01-26 15:41:30 -080010107 /**
10108 * Used to indicate that the parent of this view should be invalidated. This functionality
10109 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10110 * which is necessary when various parent-managed properties of the view change, such as
10111 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
10112 * an invalidation event to the parent.
10113 *
10114 * @hide
10115 */
10116 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -080010117 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010118 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -080010119 }
10120 }
10121
10122 /**
Romain Guy24443ea2009-05-11 11:56:30 -070010123 * Indicates whether this View is opaque. An opaque View guarantees that it will
10124 * draw all the pixels overlapping its bounds using a fully opaque color.
10125 *
10126 * Subclasses of View should override this method whenever possible to indicate
10127 * whether an instance is opaque. Opaque Views are treated in a special way by
10128 * the View hierarchy, possibly allowing it to perform optimizations during
10129 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -070010130 *
Romain Guy24443ea2009-05-11 11:56:30 -070010131 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -070010132 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010133 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -070010134 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -070010135 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -070010136 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
10137 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -070010138 }
10139
Adam Powell20232d02010-12-08 21:08:53 -080010140 /**
10141 * @hide
10142 */
10143 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -070010144 // Opaque if:
10145 // - Has a background
10146 // - Background is opaque
10147 // - Doesn't have scrollbars or scrollbars are inside overlay
10148
Philip Milne6c8ea062012-04-03 17:38:43 -070010149 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Romain Guy8f1344f52009-05-15 16:03:59 -070010150 mPrivateFlags |= OPAQUE_BACKGROUND;
10151 } else {
10152 mPrivateFlags &= ~OPAQUE_BACKGROUND;
10153 }
10154
10155 final int flags = mViewFlags;
10156 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
10157 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
10158 mPrivateFlags |= OPAQUE_SCROLLBARS;
10159 } else {
10160 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
10161 }
10162 }
10163
10164 /**
10165 * @hide
10166 */
10167 protected boolean hasOpaqueScrollbars() {
10168 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -070010169 }
10170
10171 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010172 * @return A handler associated with the thread running the View. This
10173 * handler can be used to pump events in the UI events queue.
10174 */
10175 public Handler getHandler() {
10176 if (mAttachInfo != null) {
10177 return mAttachInfo.mHandler;
10178 }
10179 return null;
10180 }
10181
10182 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080010183 * Gets the view root associated with the View.
10184 * @return The view root, or null if none.
10185 * @hide
10186 */
10187 public ViewRootImpl getViewRootImpl() {
10188 if (mAttachInfo != null) {
10189 return mAttachInfo.mViewRootImpl;
10190 }
10191 return null;
10192 }
10193
10194 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010195 * <p>Causes the Runnable to be added to the message queue.
10196 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010197 *
Romain Guye63a4f32011-08-11 11:33:31 -070010198 * <p>This method can be invoked from outside of the UI thread
10199 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010200 *
10201 * @param action The Runnable that will be executed.
10202 *
10203 * @return Returns true if the Runnable was successfully placed in to the
10204 * message queue. Returns false on failure, usually because the
10205 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010206 *
10207 * @see #postDelayed
10208 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010209 */
10210 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010211 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010212 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010213 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010214 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010215 // Assume that post will succeed later
10216 ViewRootImpl.getRunQueue().post(action);
10217 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010218 }
10219
10220 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010221 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010222 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -070010223 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010224 *
Romain Guye63a4f32011-08-11 11:33:31 -070010225 * <p>This method can be invoked from outside of the UI thread
10226 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010227 *
10228 * @param action The Runnable that will be executed.
10229 * @param delayMillis The delay (in milliseconds) until the Runnable
10230 * will be executed.
10231 *
10232 * @return true if the Runnable was successfully placed in to the
10233 * message queue. Returns false on failure, usually because the
10234 * looper processing the message queue is exiting. Note that a
10235 * result of true does not mean the Runnable will be processed --
10236 * if the looper is quit before the delivery time of the message
10237 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010238 *
10239 * @see #post
10240 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010241 */
10242 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010243 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010244 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010245 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010246 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010247 // Assume that post will succeed later
10248 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10249 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010250 }
10251
10252 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010253 * <p>Causes the Runnable to execute on the next animation time step.
10254 * The runnable will be run on the user interface thread.</p>
10255 *
10256 * <p>This method can be invoked from outside of the UI thread
10257 * only when this View is attached to a window.</p>
10258 *
10259 * @param action The Runnable that will be executed.
10260 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010261 * @see #postOnAnimationDelayed
10262 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010263 */
10264 public void postOnAnimation(Runnable action) {
10265 final AttachInfo attachInfo = mAttachInfo;
10266 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010267 attachInfo.mViewRootImpl.mChoreographer.postCallback(
10268 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010269 } else {
10270 // Assume that post will succeed later
10271 ViewRootImpl.getRunQueue().post(action);
10272 }
10273 }
10274
10275 /**
10276 * <p>Causes the Runnable to execute on the next animation time step,
10277 * after the specified amount of time elapses.
10278 * The runnable will be run on the user interface thread.</p>
10279 *
10280 * <p>This method can be invoked from outside of the UI thread
10281 * only when this View is attached to a window.</p>
10282 *
10283 * @param action The Runnable that will be executed.
10284 * @param delayMillis The delay (in milliseconds) until the Runnable
10285 * will be executed.
10286 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010287 * @see #postOnAnimation
10288 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010289 */
10290 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
10291 final AttachInfo attachInfo = mAttachInfo;
10292 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010293 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
10294 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010295 } else {
10296 // Assume that post will succeed later
10297 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10298 }
10299 }
10300
10301 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010302 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010303 *
Romain Guye63a4f32011-08-11 11:33:31 -070010304 * <p>This method can be invoked from outside of the UI thread
10305 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010306 *
10307 * @param action The Runnable to remove from the message handling queue
10308 *
10309 * @return true if this view could ask the Handler to remove the Runnable,
10310 * false otherwise. When the returned value is true, the Runnable
10311 * may or may not have been actually removed from the message queue
10312 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010313 *
10314 * @see #post
10315 * @see #postDelayed
10316 * @see #postOnAnimation
10317 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010318 */
10319 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080010320 if (action != null) {
10321 final AttachInfo attachInfo = mAttachInfo;
10322 if (attachInfo != null) {
10323 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010324 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
10325 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -080010326 } else {
10327 // Assume that post will succeed later
10328 ViewRootImpl.getRunQueue().removeCallbacks(action);
10329 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010330 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010331 return true;
10332 }
10333
10334 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010335 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
10336 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010337 *
Romain Guye63a4f32011-08-11 11:33:31 -070010338 * <p>This method can be invoked from outside of the UI thread
10339 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010340 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010341 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010342 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010343 */
10344 public void postInvalidate() {
10345 postInvalidateDelayed(0);
10346 }
10347
10348 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010349 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10350 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010351 *
Romain Guye63a4f32011-08-11 11:33:31 -070010352 * <p>This method can be invoked from outside of the UI thread
10353 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010354 *
10355 * @param left The left coordinate of the rectangle to invalidate.
10356 * @param top The top coordinate of the rectangle to invalidate.
10357 * @param right The right coordinate of the rectangle to invalidate.
10358 * @param bottom The bottom coordinate of the rectangle to invalidate.
10359 *
10360 * @see #invalidate(int, int, int, int)
10361 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010362 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010363 */
10364 public void postInvalidate(int left, int top, int right, int bottom) {
10365 postInvalidateDelayed(0, left, top, right, bottom);
10366 }
10367
10368 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010369 * <p>Cause an invalidate to happen on a subsequent cycle through the event
10370 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010371 *
Romain Guye63a4f32011-08-11 11:33:31 -070010372 * <p>This method can be invoked from outside of the UI thread
10373 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010374 *
10375 * @param delayMilliseconds the duration in milliseconds to delay the
10376 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010377 *
10378 * @see #invalidate()
10379 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010380 */
10381 public void postInvalidateDelayed(long delayMilliseconds) {
10382 // We try only with the AttachInfo because there's no point in invalidating
10383 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010384 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010385 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010386 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010387 }
10388 }
10389
10390 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010391 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10392 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010393 *
Romain Guye63a4f32011-08-11 11:33:31 -070010394 * <p>This method can be invoked from outside of the UI thread
10395 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010396 *
10397 * @param delayMilliseconds the duration in milliseconds to delay the
10398 * invalidation by
10399 * @param left The left coordinate of the rectangle to invalidate.
10400 * @param top The top coordinate of the rectangle to invalidate.
10401 * @param right The right coordinate of the rectangle to invalidate.
10402 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010403 *
10404 * @see #invalidate(int, int, int, int)
10405 * @see #invalidate(Rect)
10406 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010407 */
10408 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
10409 int right, int bottom) {
10410
10411 // We try only with the AttachInfo because there's no point in invalidating
10412 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010413 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010414 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010415 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10416 info.target = this;
10417 info.left = left;
10418 info.top = top;
10419 info.right = right;
10420 info.bottom = bottom;
10421
Jeff Browna175a5b2012-02-15 19:18:31 -080010422 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010423 }
10424 }
10425
10426 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010427 * <p>Cause an invalidate to happen on the next animation time step, typically the
10428 * next display frame.</p>
10429 *
10430 * <p>This method can be invoked from outside of the UI thread
10431 * only when this View is attached to a window.</p>
10432 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010433 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010434 */
10435 public void postInvalidateOnAnimation() {
10436 // We try only with the AttachInfo because there's no point in invalidating
10437 // if we are not attached to our window
10438 final AttachInfo attachInfo = mAttachInfo;
10439 if (attachInfo != null) {
10440 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10441 }
10442 }
10443
10444 /**
10445 * <p>Cause an invalidate of the specified area to happen on the next animation
10446 * time step, typically the next display frame.</p>
10447 *
10448 * <p>This method can be invoked from outside of the UI thread
10449 * only when this View is attached to a window.</p>
10450 *
10451 * @param left The left coordinate of the rectangle to invalidate.
10452 * @param top The top coordinate of the rectangle to invalidate.
10453 * @param right The right coordinate of the rectangle to invalidate.
10454 * @param bottom The bottom coordinate of the rectangle to invalidate.
10455 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010456 * @see #invalidate(int, int, int, int)
10457 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010458 */
10459 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10460 // We try only with the AttachInfo because there's no point in invalidating
10461 // if we are not attached to our window
10462 final AttachInfo attachInfo = mAttachInfo;
10463 if (attachInfo != null) {
10464 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10465 info.target = this;
10466 info.left = left;
10467 info.top = top;
10468 info.right = right;
10469 info.bottom = bottom;
10470
10471 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10472 }
10473 }
10474
10475 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010476 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10477 * This event is sent at most once every
10478 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10479 */
10480 private void postSendViewScrolledAccessibilityEventCallback() {
10481 if (mSendViewScrolledAccessibilityEvent == null) {
10482 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10483 }
10484 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10485 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10486 postDelayed(mSendViewScrolledAccessibilityEvent,
10487 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10488 }
10489 }
10490
10491 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010492 * Called by a parent to request that a child update its values for mScrollX
10493 * and mScrollY if necessary. This will typically be done if the child is
10494 * animating a scroll using a {@link android.widget.Scroller Scroller}
10495 * object.
10496 */
10497 public void computeScroll() {
10498 }
10499
10500 /**
10501 * <p>Indicate whether the horizontal edges are faded when the view is
10502 * scrolled horizontally.</p>
10503 *
10504 * @return true if the horizontal edges should are faded on scroll, false
10505 * otherwise
10506 *
10507 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010508 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010509 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010510 */
10511 public boolean isHorizontalFadingEdgeEnabled() {
10512 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10513 }
10514
10515 /**
10516 * <p>Define whether the horizontal edges should be faded when this view
10517 * is scrolled horizontally.</p>
10518 *
10519 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10520 * be faded when the view is scrolled
10521 * horizontally
10522 *
10523 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010524 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010525 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010526 */
10527 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10528 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10529 if (horizontalFadingEdgeEnabled) {
10530 initScrollCache();
10531 }
10532
10533 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10534 }
10535 }
10536
10537 /**
10538 * <p>Indicate whether the vertical edges are faded when the view is
10539 * scrolled horizontally.</p>
10540 *
10541 * @return true if the vertical edges should are faded on scroll, false
10542 * otherwise
10543 *
10544 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010545 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010546 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010547 */
10548 public boolean isVerticalFadingEdgeEnabled() {
10549 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10550 }
10551
10552 /**
10553 * <p>Define whether the vertical edges should be faded when this view
10554 * is scrolled vertically.</p>
10555 *
10556 * @param verticalFadingEdgeEnabled true if the vertical edges should
10557 * be faded when the view is scrolled
10558 * vertically
10559 *
10560 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010561 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010562 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010563 */
10564 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10565 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10566 if (verticalFadingEdgeEnabled) {
10567 initScrollCache();
10568 }
10569
10570 mViewFlags ^= FADING_EDGE_VERTICAL;
10571 }
10572 }
10573
10574 /**
10575 * Returns the strength, or intensity, of the top faded edge. The strength is
10576 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10577 * returns 0.0 or 1.0 but no value in between.
10578 *
10579 * Subclasses should override this method to provide a smoother fade transition
10580 * when scrolling occurs.
10581 *
10582 * @return the intensity of the top fade as a float between 0.0f and 1.0f
10583 */
10584 protected float getTopFadingEdgeStrength() {
10585 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
10586 }
10587
10588 /**
10589 * Returns the strength, or intensity, of the bottom faded edge. The strength is
10590 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10591 * returns 0.0 or 1.0 but no value in between.
10592 *
10593 * Subclasses should override this method to provide a smoother fade transition
10594 * when scrolling occurs.
10595 *
10596 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
10597 */
10598 protected float getBottomFadingEdgeStrength() {
10599 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
10600 computeVerticalScrollRange() ? 1.0f : 0.0f;
10601 }
10602
10603 /**
10604 * Returns the strength, or intensity, of the left faded edge. The strength is
10605 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10606 * returns 0.0 or 1.0 but no value in between.
10607 *
10608 * Subclasses should override this method to provide a smoother fade transition
10609 * when scrolling occurs.
10610 *
10611 * @return the intensity of the left fade as a float between 0.0f and 1.0f
10612 */
10613 protected float getLeftFadingEdgeStrength() {
10614 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
10615 }
10616
10617 /**
10618 * Returns the strength, or intensity, of the right faded edge. The strength is
10619 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10620 * returns 0.0 or 1.0 but no value in between.
10621 *
10622 * Subclasses should override this method to provide a smoother fade transition
10623 * when scrolling occurs.
10624 *
10625 * @return the intensity of the right fade as a float between 0.0f and 1.0f
10626 */
10627 protected float getRightFadingEdgeStrength() {
10628 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
10629 computeHorizontalScrollRange() ? 1.0f : 0.0f;
10630 }
10631
10632 /**
10633 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
10634 * scrollbar is not drawn by default.</p>
10635 *
10636 * @return true if the horizontal scrollbar should be painted, false
10637 * otherwise
10638 *
10639 * @see #setHorizontalScrollBarEnabled(boolean)
10640 */
10641 public boolean isHorizontalScrollBarEnabled() {
10642 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10643 }
10644
10645 /**
10646 * <p>Define whether the horizontal scrollbar should be drawn or not. The
10647 * scrollbar is not drawn by default.</p>
10648 *
10649 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
10650 * be painted
10651 *
10652 * @see #isHorizontalScrollBarEnabled()
10653 */
10654 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
10655 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
10656 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010657 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010658 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010659 }
10660 }
10661
10662 /**
10663 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
10664 * scrollbar is not drawn by default.</p>
10665 *
10666 * @return true if the vertical scrollbar should be painted, false
10667 * otherwise
10668 *
10669 * @see #setVerticalScrollBarEnabled(boolean)
10670 */
10671 public boolean isVerticalScrollBarEnabled() {
10672 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
10673 }
10674
10675 /**
10676 * <p>Define whether the vertical scrollbar should be drawn or not. The
10677 * scrollbar is not drawn by default.</p>
10678 *
10679 * @param verticalScrollBarEnabled true if the vertical scrollbar should
10680 * be painted
10681 *
10682 * @see #isVerticalScrollBarEnabled()
10683 */
10684 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
10685 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
10686 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010687 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010688 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010689 }
10690 }
10691
Adam Powell20232d02010-12-08 21:08:53 -080010692 /**
10693 * @hide
10694 */
10695 protected void recomputePadding() {
10696 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010697 }
Joe Malin32736f02011-01-19 16:14:20 -080010698
Mike Cleronfe81d382009-09-28 14:22:16 -070010699 /**
10700 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080010701 *
Mike Cleronfe81d382009-09-28 14:22:16 -070010702 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080010703 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010704 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070010705 */
10706 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
10707 initScrollCache();
10708 final ScrollabilityCache scrollabilityCache = mScrollCache;
10709 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010710 if (fadeScrollbars) {
10711 scrollabilityCache.state = ScrollabilityCache.OFF;
10712 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070010713 scrollabilityCache.state = ScrollabilityCache.ON;
10714 }
10715 }
Joe Malin32736f02011-01-19 16:14:20 -080010716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010717 /**
Joe Malin32736f02011-01-19 16:14:20 -080010718 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010719 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080010720 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010721 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070010722 *
10723 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070010724 */
10725 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080010726 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010727 }
Joe Malin32736f02011-01-19 16:14:20 -080010728
Mike Cleron52f0a642009-09-28 18:21:37 -070010729 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070010730 *
10731 * Returns the delay before scrollbars fade.
10732 *
10733 * @return the delay before scrollbars fade
10734 *
10735 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10736 */
10737 public int getScrollBarDefaultDelayBeforeFade() {
10738 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
10739 mScrollCache.scrollBarDefaultDelayBeforeFade;
10740 }
10741
10742 /**
10743 * Define the delay before scrollbars fade.
10744 *
10745 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
10746 *
10747 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10748 */
10749 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
10750 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
10751 }
10752
10753 /**
10754 *
10755 * Returns the scrollbar fade duration.
10756 *
10757 * @return the scrollbar fade duration
10758 *
10759 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10760 */
10761 public int getScrollBarFadeDuration() {
10762 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
10763 mScrollCache.scrollBarFadeDuration;
10764 }
10765
10766 /**
10767 * Define the scrollbar fade duration.
10768 *
10769 * @param scrollBarFadeDuration - the scrollbar fade duration
10770 *
10771 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10772 */
10773 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
10774 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
10775 }
10776
10777 /**
10778 *
10779 * Returns the scrollbar size.
10780 *
10781 * @return the scrollbar size
10782 *
10783 * @attr ref android.R.styleable#View_scrollbarSize
10784 */
10785 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070010786 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070010787 mScrollCache.scrollBarSize;
10788 }
10789
10790 /**
10791 * Define the scrollbar size.
10792 *
10793 * @param scrollBarSize - the scrollbar size
10794 *
10795 * @attr ref android.R.styleable#View_scrollbarSize
10796 */
10797 public void setScrollBarSize(int scrollBarSize) {
10798 getScrollCache().scrollBarSize = scrollBarSize;
10799 }
10800
10801 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010802 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
10803 * inset. When inset, they add to the padding of the view. And the scrollbars
10804 * can be drawn inside the padding area or on the edge of the view. For example,
10805 * if a view has a background drawable and you want to draw the scrollbars
10806 * inside the padding specified by the drawable, you can use
10807 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
10808 * appear at the edge of the view, ignoring the padding, then you can use
10809 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
10810 * @param style the style of the scrollbars. Should be one of
10811 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
10812 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
10813 * @see #SCROLLBARS_INSIDE_OVERLAY
10814 * @see #SCROLLBARS_INSIDE_INSET
10815 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10816 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010817 *
10818 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010819 */
10820 public void setScrollBarStyle(int style) {
10821 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
10822 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070010823 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010824 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010825 }
10826 }
10827
10828 /**
10829 * <p>Returns the current scrollbar style.</p>
10830 * @return the current scrollbar style
10831 * @see #SCROLLBARS_INSIDE_OVERLAY
10832 * @see #SCROLLBARS_INSIDE_INSET
10833 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10834 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010835 *
10836 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010837 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070010838 @ViewDebug.ExportedProperty(mapping = {
10839 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
10840 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
10841 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
10842 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
10843 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010844 public int getScrollBarStyle() {
10845 return mViewFlags & SCROLLBARS_STYLE_MASK;
10846 }
10847
10848 /**
10849 * <p>Compute the horizontal range that the horizontal scrollbar
10850 * represents.</p>
10851 *
10852 * <p>The range is expressed in arbitrary units that must be the same as the
10853 * units used by {@link #computeHorizontalScrollExtent()} and
10854 * {@link #computeHorizontalScrollOffset()}.</p>
10855 *
10856 * <p>The default range is the drawing width of this view.</p>
10857 *
10858 * @return the total horizontal range represented by the horizontal
10859 * scrollbar
10860 *
10861 * @see #computeHorizontalScrollExtent()
10862 * @see #computeHorizontalScrollOffset()
10863 * @see android.widget.ScrollBarDrawable
10864 */
10865 protected int computeHorizontalScrollRange() {
10866 return getWidth();
10867 }
10868
10869 /**
10870 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
10871 * within the horizontal range. This value is used to compute the position
10872 * of the thumb within the scrollbar's track.</p>
10873 *
10874 * <p>The range is expressed in arbitrary units that must be the same as the
10875 * units used by {@link #computeHorizontalScrollRange()} and
10876 * {@link #computeHorizontalScrollExtent()}.</p>
10877 *
10878 * <p>The default offset is the scroll offset of this view.</p>
10879 *
10880 * @return the horizontal offset of the scrollbar's thumb
10881 *
10882 * @see #computeHorizontalScrollRange()
10883 * @see #computeHorizontalScrollExtent()
10884 * @see android.widget.ScrollBarDrawable
10885 */
10886 protected int computeHorizontalScrollOffset() {
10887 return mScrollX;
10888 }
10889
10890 /**
10891 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
10892 * within the horizontal range. This value is used to compute the length
10893 * of the thumb within the scrollbar's track.</p>
10894 *
10895 * <p>The range is expressed in arbitrary units that must be the same as the
10896 * units used by {@link #computeHorizontalScrollRange()} and
10897 * {@link #computeHorizontalScrollOffset()}.</p>
10898 *
10899 * <p>The default extent is the drawing width of this view.</p>
10900 *
10901 * @return the horizontal extent of the scrollbar's thumb
10902 *
10903 * @see #computeHorizontalScrollRange()
10904 * @see #computeHorizontalScrollOffset()
10905 * @see android.widget.ScrollBarDrawable
10906 */
10907 protected int computeHorizontalScrollExtent() {
10908 return getWidth();
10909 }
10910
10911 /**
10912 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
10913 *
10914 * <p>The range is expressed in arbitrary units that must be the same as the
10915 * units used by {@link #computeVerticalScrollExtent()} and
10916 * {@link #computeVerticalScrollOffset()}.</p>
10917 *
10918 * @return the total vertical range represented by the vertical scrollbar
10919 *
10920 * <p>The default range is the drawing height of this view.</p>
10921 *
10922 * @see #computeVerticalScrollExtent()
10923 * @see #computeVerticalScrollOffset()
10924 * @see android.widget.ScrollBarDrawable
10925 */
10926 protected int computeVerticalScrollRange() {
10927 return getHeight();
10928 }
10929
10930 /**
10931 * <p>Compute the vertical offset of the vertical scrollbar's thumb
10932 * within the horizontal range. This value is used to compute the position
10933 * of the thumb within the scrollbar's track.</p>
10934 *
10935 * <p>The range is expressed in arbitrary units that must be the same as the
10936 * units used by {@link #computeVerticalScrollRange()} and
10937 * {@link #computeVerticalScrollExtent()}.</p>
10938 *
10939 * <p>The default offset is the scroll offset of this view.</p>
10940 *
10941 * @return the vertical offset of the scrollbar's thumb
10942 *
10943 * @see #computeVerticalScrollRange()
10944 * @see #computeVerticalScrollExtent()
10945 * @see android.widget.ScrollBarDrawable
10946 */
10947 protected int computeVerticalScrollOffset() {
10948 return mScrollY;
10949 }
10950
10951 /**
10952 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
10953 * within the vertical range. This value is used to compute the length
10954 * of the thumb within the scrollbar's track.</p>
10955 *
10956 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080010957 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010958 * {@link #computeVerticalScrollOffset()}.</p>
10959 *
10960 * <p>The default extent is the drawing height of this view.</p>
10961 *
10962 * @return the vertical extent of the scrollbar's thumb
10963 *
10964 * @see #computeVerticalScrollRange()
10965 * @see #computeVerticalScrollOffset()
10966 * @see android.widget.ScrollBarDrawable
10967 */
10968 protected int computeVerticalScrollExtent() {
10969 return getHeight();
10970 }
10971
10972 /**
Adam Powell69159442011-06-13 17:53:06 -070010973 * Check if this view can be scrolled horizontally in a certain direction.
10974 *
10975 * @param direction Negative to check scrolling left, positive to check scrolling right.
10976 * @return true if this view can be scrolled in the specified direction, false otherwise.
10977 */
10978 public boolean canScrollHorizontally(int direction) {
10979 final int offset = computeHorizontalScrollOffset();
10980 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
10981 if (range == 0) return false;
10982 if (direction < 0) {
10983 return offset > 0;
10984 } else {
10985 return offset < range - 1;
10986 }
10987 }
10988
10989 /**
10990 * Check if this view can be scrolled vertically in a certain direction.
10991 *
10992 * @param direction Negative to check scrolling up, positive to check scrolling down.
10993 * @return true if this view can be scrolled in the specified direction, false otherwise.
10994 */
10995 public boolean canScrollVertically(int direction) {
10996 final int offset = computeVerticalScrollOffset();
10997 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
10998 if (range == 0) return false;
10999 if (direction < 0) {
11000 return offset > 0;
11001 } else {
11002 return offset < range - 1;
11003 }
11004 }
11005
11006 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011007 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
11008 * scrollbars are painted only if they have been awakened first.</p>
11009 *
11010 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080011011 *
Mike Cleronf116bf82009-09-27 19:14:12 -070011012 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011013 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080011014 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011015 // scrollbars are drawn only when the animation is running
11016 final ScrollabilityCache cache = mScrollCache;
11017 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080011018
Mike Cleronf116bf82009-09-27 19:14:12 -070011019 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080011020
Mike Cleronf116bf82009-09-27 19:14:12 -070011021 if (state == ScrollabilityCache.OFF) {
11022 return;
11023 }
Joe Malin32736f02011-01-19 16:14:20 -080011024
Mike Cleronf116bf82009-09-27 19:14:12 -070011025 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080011026
Mike Cleronf116bf82009-09-27 19:14:12 -070011027 if (state == ScrollabilityCache.FADING) {
11028 // We're fading -- get our fade interpolation
11029 if (cache.interpolatorValues == null) {
11030 cache.interpolatorValues = new float[1];
11031 }
Joe Malin32736f02011-01-19 16:14:20 -080011032
Mike Cleronf116bf82009-09-27 19:14:12 -070011033 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080011034
Mike Cleronf116bf82009-09-27 19:14:12 -070011035 // Stops the animation if we're done
11036 if (cache.scrollBarInterpolator.timeToValues(values) ==
11037 Interpolator.Result.FREEZE_END) {
11038 cache.state = ScrollabilityCache.OFF;
11039 } else {
11040 cache.scrollBar.setAlpha(Math.round(values[0]));
11041 }
Joe Malin32736f02011-01-19 16:14:20 -080011042
11043 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070011044 // drawing. We only want this when we're fading so that
11045 // we prevent excessive redraws
11046 invalidate = true;
11047 } else {
11048 // We're just on -- but we may have been fading before so
11049 // reset alpha
11050 cache.scrollBar.setAlpha(255);
11051 }
11052
Joe Malin32736f02011-01-19 16:14:20 -080011053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011054 final int viewFlags = mViewFlags;
11055
11056 final boolean drawHorizontalScrollBar =
11057 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
11058 final boolean drawVerticalScrollBar =
11059 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
11060 && !isVerticalScrollBarHidden();
11061
11062 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
11063 final int width = mRight - mLeft;
11064 final int height = mBottom - mTop;
11065
11066 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011067
Mike Reede8853fc2009-09-04 14:01:48 -040011068 final int scrollX = mScrollX;
11069 final int scrollY = mScrollY;
11070 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
11071
Mike Cleronf116bf82009-09-27 19:14:12 -070011072 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080011073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011074 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011075 int size = scrollBar.getSize(false);
11076 if (size <= 0) {
11077 size = cache.scrollBarSize;
11078 }
11079
Mike Cleronf116bf82009-09-27 19:14:12 -070011080 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040011081 computeHorizontalScrollOffset(),
11082 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040011083 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070011084 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080011085 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070011086 left = scrollX + (mPaddingLeft & inside);
11087 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
11088 bottom = top + size;
11089 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
11090 if (invalidate) {
11091 invalidate(left, top, right, bottom);
11092 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011093 }
11094
11095 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011096 int size = scrollBar.getSize(true);
11097 if (size <= 0) {
11098 size = cache.scrollBarSize;
11099 }
11100
Mike Reede8853fc2009-09-04 14:01:48 -040011101 scrollBar.setParameters(computeVerticalScrollRange(),
11102 computeVerticalScrollOffset(),
11103 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -080011104 switch (mVerticalScrollbarPosition) {
11105 default:
11106 case SCROLLBAR_POSITION_DEFAULT:
11107 case SCROLLBAR_POSITION_RIGHT:
11108 left = scrollX + width - size - (mUserPaddingRight & inside);
11109 break;
11110 case SCROLLBAR_POSITION_LEFT:
11111 left = scrollX + (mUserPaddingLeft & inside);
11112 break;
11113 }
Mike Cleronf116bf82009-09-27 19:14:12 -070011114 top = scrollY + (mPaddingTop & inside);
11115 right = left + size;
11116 bottom = scrollY + height - (mUserPaddingBottom & inside);
11117 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
11118 if (invalidate) {
11119 invalidate(left, top, right, bottom);
11120 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011121 }
11122 }
11123 }
11124 }
Romain Guy8506ab42009-06-11 17:35:47 -070011125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011126 /**
Romain Guy8506ab42009-06-11 17:35:47 -070011127 * 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 -080011128 * FastScroller is visible.
11129 * @return whether to temporarily hide the vertical scrollbar
11130 * @hide
11131 */
11132 protected boolean isVerticalScrollBarHidden() {
11133 return false;
11134 }
11135
11136 /**
11137 * <p>Draw the horizontal scrollbar if
11138 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
11139 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011140 * @param canvas the canvas on which to draw the scrollbar
11141 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011142 *
11143 * @see #isHorizontalScrollBarEnabled()
11144 * @see #computeHorizontalScrollRange()
11145 * @see #computeHorizontalScrollExtent()
11146 * @see #computeHorizontalScrollOffset()
11147 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070011148 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011149 */
Romain Guy8fb95422010-08-17 18:38:51 -070011150 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
11151 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011152 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011153 scrollBar.draw(canvas);
11154 }
Mike Reede8853fc2009-09-04 14:01:48 -040011155
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011156 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011157 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
11158 * returns true.</p>
11159 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011160 * @param canvas the canvas on which to draw the scrollbar
11161 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011162 *
11163 * @see #isVerticalScrollBarEnabled()
11164 * @see #computeVerticalScrollRange()
11165 * @see #computeVerticalScrollExtent()
11166 * @see #computeVerticalScrollOffset()
11167 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040011168 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011169 */
Romain Guy8fb95422010-08-17 18:38:51 -070011170 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
11171 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040011172 scrollBar.setBounds(l, t, r, b);
11173 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011174 }
11175
11176 /**
11177 * Implement this to do your drawing.
11178 *
11179 * @param canvas the canvas on which the background will be drawn
11180 */
11181 protected void onDraw(Canvas canvas) {
11182 }
11183
11184 /*
11185 * Caller is responsible for calling requestLayout if necessary.
11186 * (This allows addViewInLayout to not request a new layout.)
11187 */
11188 void assignParent(ViewParent parent) {
11189 if (mParent == null) {
11190 mParent = parent;
11191 } else if (parent == null) {
11192 mParent = null;
11193 } else {
11194 throw new RuntimeException("view " + this + " being added, but"
11195 + " it already has a parent");
11196 }
11197 }
11198
11199 /**
11200 * This is called when the view is attached to a window. At this point it
11201 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011202 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
11203 * however it may be called any time before the first onDraw -- including
11204 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011205 *
11206 * @see #onDetachedFromWindow()
11207 */
11208 protected void onAttachedToWindow() {
11209 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
11210 mParent.requestTransparentRegion(this);
11211 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011212
Adam Powell8568c3a2010-04-19 14:26:11 -070011213 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
11214 initialAwakenScrollBars();
11215 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
11216 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011217
Chet Haasea9b61ac2010-12-20 07:40:25 -080011218 jumpDrawablesToCurrentState();
Romain Guy2a0f2282012-05-08 14:43:12 -070011219
Fabrice Di Meglioa6461452011-08-19 15:42:04 -070011220 // Order is important here: LayoutDirection MUST be resolved before Padding
11221 // and TextDirection
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011222 resolveLayoutDirection();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011223 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011224 resolveTextDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011225 resolveTextAlignment();
Romain Guy2a0f2282012-05-08 14:43:12 -070011226
Svetoslav Ganov42138042012-03-20 11:51:39 -070011227 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070011228 if (isFocused()) {
11229 InputMethodManager imm = InputMethodManager.peekInstance();
11230 imm.focusIn(this);
11231 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011232
11233 if (mAttachInfo != null && mDisplayList != null) {
11234 mAttachInfo.mViewRootImpl.dequeueDisplayList(mDisplayList);
11235 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011236 }
Cibu Johny86666632010-02-22 13:01:02 -080011237
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011238 /**
Romain Guybb9908b2012-03-08 11:14:07 -080011239 * @see #onScreenStateChanged(int)
11240 */
11241 void dispatchScreenStateChanged(int screenState) {
11242 onScreenStateChanged(screenState);
11243 }
11244
11245 /**
11246 * This method is called whenever the state of the screen this view is
11247 * attached to changes. A state change will usually occurs when the screen
11248 * turns on or off (whether it happens automatically or the user does it
11249 * manually.)
11250 *
11251 * @param screenState The new state of the screen. Can be either
11252 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
11253 */
11254 public void onScreenStateChanged(int screenState) {
11255 }
11256
11257 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011258 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
11259 */
11260 private boolean hasRtlSupport() {
11261 return mContext.getApplicationInfo().hasRtlSupport();
11262 }
11263
11264 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011265 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
11266 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011267 * Will call {@link View#onResolvedLayoutDirectionChanged} when resolution is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011268 * @hide
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011269 */
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011270 public void resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011271 // Clear any previous layout direction resolution
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070011272 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011273
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011274 if (hasRtlSupport()) {
11275 // Set resolved depending on layout direction
11276 switch (getLayoutDirection()) {
11277 case LAYOUT_DIRECTION_INHERIT:
11278 // If this is root view, no need to look at parent's layout dir.
11279 if (canResolveLayoutDirection()) {
11280 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011281
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011282 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
11283 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11284 }
11285 } else {
11286 // Nothing to do, LTR by default
11287 }
11288 break;
11289 case LAYOUT_DIRECTION_RTL:
11290 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11291 break;
11292 case LAYOUT_DIRECTION_LOCALE:
11293 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011294 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11295 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011296 break;
11297 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011298 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011299 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011300 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011301
11302 // Set to resolved
11303 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011304 onResolvedLayoutDirectionChanged();
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080011305 // Resolve padding
11306 resolvePadding();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011307 }
11308
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011309 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011310 * Called when layout direction has been resolved.
11311 *
11312 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011313 * @hide
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011314 */
11315 public void onResolvedLayoutDirectionChanged() {
11316 }
11317
11318 /**
11319 * Resolve padding depending on layout direction.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011320 * @hide
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011321 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011322 public void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011323 // If the user specified the absolute padding (either with android:padding or
11324 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
11325 // use the default padding or the padding from the background drawable
11326 // (stored at this point in mPadding*)
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011327 int resolvedLayoutDirection = getResolvedLayoutDirection();
11328 switch (resolvedLayoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011329 case LAYOUT_DIRECTION_RTL:
11330 // Start user padding override Right user padding. Otherwise, if Right user
11331 // padding is not defined, use the default Right padding. If Right user padding
11332 // is defined, just use it.
11333 if (mUserPaddingStart >= 0) {
11334 mUserPaddingRight = mUserPaddingStart;
11335 } else if (mUserPaddingRight < 0) {
11336 mUserPaddingRight = mPaddingRight;
11337 }
11338 if (mUserPaddingEnd >= 0) {
11339 mUserPaddingLeft = mUserPaddingEnd;
11340 } else if (mUserPaddingLeft < 0) {
11341 mUserPaddingLeft = mPaddingLeft;
11342 }
11343 break;
11344 case LAYOUT_DIRECTION_LTR:
11345 default:
11346 // Start user padding override Left user padding. Otherwise, if Left user
11347 // padding is not defined, use the default left padding. If Left user padding
11348 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070011349 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011350 mUserPaddingLeft = mUserPaddingStart;
11351 } else if (mUserPaddingLeft < 0) {
11352 mUserPaddingLeft = mPaddingLeft;
11353 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070011354 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011355 mUserPaddingRight = mUserPaddingEnd;
11356 } else if (mUserPaddingRight < 0) {
11357 mUserPaddingRight = mPaddingRight;
11358 }
11359 }
11360
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011361 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
11362
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080011363 if(isPaddingRelative()) {
11364 setPaddingRelative(mUserPaddingStart, mPaddingTop, mUserPaddingEnd, mUserPaddingBottom);
11365 } else {
11366 recomputePadding();
11367 }
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011368 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011369 }
11370
11371 /**
11372 * Resolve padding depending on the layout direction. Subclasses that care about
11373 * padding resolution should override this method. The default implementation does
11374 * nothing.
11375 *
11376 * @param layoutDirection the direction of the layout
11377 *
Fabrice Di Meglioe8dc07d2012-03-09 17:10:19 -080011378 * @see {@link #LAYOUT_DIRECTION_LTR}
11379 * @see {@link #LAYOUT_DIRECTION_RTL}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011380 * @hide
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011381 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011382 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011383 }
11384
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011385 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011386 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011387 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011388 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011389 * @hide
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011390 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011391 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011392 switch (getLayoutDirection()) {
11393 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011394 return (mParent != null) && (mParent instanceof ViewGroup);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011395 default:
11396 return true;
11397 }
11398 }
11399
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011400 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011401 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
11402 * when reset is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011403 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011404 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011405 public void resetResolvedLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070011406 // Reset the current resolved bits
11407 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011408 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080011409 // Reset also the text direction
11410 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011411 }
11412
11413 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011414 * Called during reset of resolved layout direction.
11415 *
11416 * Subclasses need to override this method to clear cached information that depends on the
11417 * resolved layout direction, or to inform child views that inherit their layout direction.
11418 *
11419 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011420 * @hide
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011421 */
11422 public void onResolvedLayoutDirectionReset() {
11423 }
11424
11425 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011426 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011427 *
11428 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011429 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011430 * @hide
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011431 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011432 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -080011433 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011434 }
11435
11436 /**
11437 * This is called when the view is detached from a window. At this point it
11438 * no longer has a surface for drawing.
11439 *
11440 * @see #onAttachedToWindow()
11441 */
11442 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -080011443 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080011444
Romain Guya440b002010-02-24 15:57:54 -080011445 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050011446 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080011447 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070011448 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080011449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011450 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011451
Romain Guya998dff2012-03-23 18:58:36 -070011452 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011453
11454 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011455 if (mDisplayList != null) {
Romain Guy2a0f2282012-05-08 14:43:12 -070011456 mAttachInfo.mViewRootImpl.enqueueDisplayList(mDisplayList);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011457 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011458 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011459 } else {
Romain Guy38c2ece2012-05-24 14:20:56 -070011460 // Should never happen
11461 clearDisplayList();
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011462 }
11463
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011464 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011465
11466 resetResolvedLayoutDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011467 resetResolvedTextAlignment();
Svetoslav Ganov42138042012-03-20 11:51:39 -070011468 resetAccessibilityStateChanged();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011469 }
11470
11471 /**
11472 * @return The number of times this view has been attached to a window
11473 */
11474 protected int getWindowAttachCount() {
11475 return mWindowAttachCount;
11476 }
11477
11478 /**
11479 * Retrieve a unique token identifying the window this view is attached to.
11480 * @return Return the window's token for use in
11481 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11482 */
11483 public IBinder getWindowToken() {
11484 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11485 }
11486
11487 /**
11488 * Retrieve a unique token identifying the top-level "real" window of
11489 * the window that this view is attached to. That is, this is like
11490 * {@link #getWindowToken}, except if the window this view in is a panel
11491 * window (attached to another containing window), then the token of
11492 * the containing window is returned instead.
11493 *
11494 * @return Returns the associated window token, either
11495 * {@link #getWindowToken()} or the containing window's token.
11496 */
11497 public IBinder getApplicationWindowToken() {
11498 AttachInfo ai = mAttachInfo;
11499 if (ai != null) {
11500 IBinder appWindowToken = ai.mPanelParentWindowToken;
11501 if (appWindowToken == null) {
11502 appWindowToken = ai.mWindowToken;
11503 }
11504 return appWindowToken;
11505 }
11506 return null;
11507 }
11508
11509 /**
11510 * Retrieve private session object this view hierarchy is using to
11511 * communicate with the window manager.
11512 * @return the session object to communicate with the window manager
11513 */
11514 /*package*/ IWindowSession getWindowSession() {
11515 return mAttachInfo != null ? mAttachInfo.mSession : null;
11516 }
11517
11518 /**
11519 * @param info the {@link android.view.View.AttachInfo} to associated with
11520 * this view
11521 */
11522 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
11523 //System.out.println("Attached! " + this);
11524 mAttachInfo = info;
11525 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011526 // We will need to evaluate the drawable state at least once.
11527 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011528 if (mFloatingTreeObserver != null) {
11529 info.mTreeObserver.merge(mFloatingTreeObserver);
11530 mFloatingTreeObserver = null;
11531 }
11532 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
11533 mAttachInfo.mScrollContainers.add(this);
11534 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
11535 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070011536 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011537 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080011538
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011539 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011540 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011541 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011542 if (listeners != null && listeners.size() > 0) {
11543 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11544 // perform the dispatching. The iterator is a safe guard against listeners that
11545 // could mutate the list by calling the various add/remove methods. This prevents
11546 // the array from being modified while we iterate it.
11547 for (OnAttachStateChangeListener listener : listeners) {
11548 listener.onViewAttachedToWindow(this);
11549 }
11550 }
11551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011552 int vis = info.mWindowVisibility;
11553 if (vis != GONE) {
11554 onWindowVisibilityChanged(vis);
11555 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011556 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
11557 // If nobody has evaluated the drawable state yet, then do it now.
11558 refreshDrawableState();
11559 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011560 }
11561
11562 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011563 AttachInfo info = mAttachInfo;
11564 if (info != null) {
11565 int vis = info.mWindowVisibility;
11566 if (vis != GONE) {
11567 onWindowVisibilityChanged(GONE);
11568 }
11569 }
11570
11571 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080011572
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011573 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011574 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011575 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011576 if (listeners != null && listeners.size() > 0) {
11577 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11578 // perform the dispatching. The iterator is a safe guard against listeners that
11579 // could mutate the list by calling the various add/remove methods. This prevents
11580 // the array from being modified while we iterate it.
11581 for (OnAttachStateChangeListener listener : listeners) {
11582 listener.onViewDetachedFromWindow(this);
11583 }
11584 }
11585
Romain Guy01d5edc2011-01-28 11:28:53 -080011586 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011587 mAttachInfo.mScrollContainers.remove(this);
11588 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
11589 }
Romain Guy01d5edc2011-01-28 11:28:53 -080011590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011591 mAttachInfo = null;
11592 }
11593
11594 /**
11595 * Store this view hierarchy's frozen state into the given container.
11596 *
11597 * @param container The SparseArray in which to save the view's state.
11598 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011599 * @see #restoreHierarchyState(android.util.SparseArray)
11600 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11601 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011602 */
11603 public void saveHierarchyState(SparseArray<Parcelable> container) {
11604 dispatchSaveInstanceState(container);
11605 }
11606
11607 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011608 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
11609 * this view and its children. May be overridden to modify how freezing happens to a
11610 * 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 -080011611 *
11612 * @param container The SparseArray in which to save the view's state.
11613 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011614 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11615 * @see #saveHierarchyState(android.util.SparseArray)
11616 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011617 */
11618 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
11619 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
11620 mPrivateFlags &= ~SAVE_STATE_CALLED;
11621 Parcelable state = onSaveInstanceState();
11622 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11623 throw new IllegalStateException(
11624 "Derived class did not call super.onSaveInstanceState()");
11625 }
11626 if (state != null) {
11627 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
11628 // + ": " + state);
11629 container.put(mID, state);
11630 }
11631 }
11632 }
11633
11634 /**
11635 * Hook allowing a view to generate a representation of its internal state
11636 * that can later be used to create a new instance with that same state.
11637 * This state should only contain information that is not persistent or can
11638 * not be reconstructed later. For example, you will never store your
11639 * current position on screen because that will be computed again when a
11640 * new instance of the view is placed in its view hierarchy.
11641 * <p>
11642 * Some examples of things you may store here: the current cursor position
11643 * in a text view (but usually not the text itself since that is stored in a
11644 * content provider or other persistent storage), the currently selected
11645 * item in a list view.
11646 *
11647 * @return Returns a Parcelable object containing the view's current dynamic
11648 * state, or null if there is nothing interesting to save. The
11649 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070011650 * @see #onRestoreInstanceState(android.os.Parcelable)
11651 * @see #saveHierarchyState(android.util.SparseArray)
11652 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011653 * @see #setSaveEnabled(boolean)
11654 */
11655 protected Parcelable onSaveInstanceState() {
11656 mPrivateFlags |= SAVE_STATE_CALLED;
11657 return BaseSavedState.EMPTY_STATE;
11658 }
11659
11660 /**
11661 * Restore this view hierarchy's frozen state from the given container.
11662 *
11663 * @param container The SparseArray which holds previously frozen states.
11664 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011665 * @see #saveHierarchyState(android.util.SparseArray)
11666 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11667 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011668 */
11669 public void restoreHierarchyState(SparseArray<Parcelable> container) {
11670 dispatchRestoreInstanceState(container);
11671 }
11672
11673 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011674 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
11675 * state for this view and its children. May be overridden to modify how restoring
11676 * happens to a view's children; for example, some views may want to not store state
11677 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011678 *
11679 * @param container The SparseArray which holds previously saved state.
11680 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011681 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11682 * @see #restoreHierarchyState(android.util.SparseArray)
11683 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011684 */
11685 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
11686 if (mID != NO_ID) {
11687 Parcelable state = container.get(mID);
11688 if (state != null) {
11689 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
11690 // + ": " + state);
11691 mPrivateFlags &= ~SAVE_STATE_CALLED;
11692 onRestoreInstanceState(state);
11693 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11694 throw new IllegalStateException(
11695 "Derived class did not call super.onRestoreInstanceState()");
11696 }
11697 }
11698 }
11699 }
11700
11701 /**
11702 * Hook allowing a view to re-apply a representation of its internal state that had previously
11703 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
11704 * null state.
11705 *
11706 * @param state The frozen state that had previously been returned by
11707 * {@link #onSaveInstanceState}.
11708 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011709 * @see #onSaveInstanceState()
11710 * @see #restoreHierarchyState(android.util.SparseArray)
11711 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011712 */
11713 protected void onRestoreInstanceState(Parcelable state) {
11714 mPrivateFlags |= SAVE_STATE_CALLED;
11715 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080011716 throw new IllegalArgumentException("Wrong state class, expecting View State but "
11717 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080011718 + "when two views of different type have the same id in the same hierarchy. "
11719 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080011720 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011721 }
11722 }
11723
11724 /**
11725 * <p>Return the time at which the drawing of the view hierarchy started.</p>
11726 *
11727 * @return the drawing start time in milliseconds
11728 */
11729 public long getDrawingTime() {
11730 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
11731 }
11732
11733 /**
11734 * <p>Enables or disables the duplication of the parent's state into this view. When
11735 * duplication is enabled, this view gets its drawable state from its parent rather
11736 * than from its own internal properties.</p>
11737 *
11738 * <p>Note: in the current implementation, setting this property to true after the
11739 * view was added to a ViewGroup might have no effect at all. This property should
11740 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
11741 *
11742 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
11743 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011744 *
Gilles Debunnefb817032011-01-13 13:52:49 -080011745 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
11746 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011747 *
11748 * @param enabled True to enable duplication of the parent's drawable state, false
11749 * to disable it.
11750 *
11751 * @see #getDrawableState()
11752 * @see #isDuplicateParentStateEnabled()
11753 */
11754 public void setDuplicateParentStateEnabled(boolean enabled) {
11755 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
11756 }
11757
11758 /**
11759 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
11760 *
11761 * @return True if this view's drawable state is duplicated from the parent,
11762 * false otherwise
11763 *
11764 * @see #getDrawableState()
11765 * @see #setDuplicateParentStateEnabled(boolean)
11766 */
11767 public boolean isDuplicateParentStateEnabled() {
11768 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
11769 }
11770
11771 /**
Romain Guy171c5922011-01-06 10:04:23 -080011772 * <p>Specifies the type of layer backing this view. The layer can be
11773 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
11774 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011775 *
Romain Guy171c5922011-01-06 10:04:23 -080011776 * <p>A layer is associated with an optional {@link android.graphics.Paint}
11777 * instance that controls how the layer is composed on screen. The following
11778 * properties of the paint are taken into account when composing the layer:</p>
11779 * <ul>
11780 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
11781 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
11782 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
11783 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080011784 *
Romain Guy171c5922011-01-06 10:04:23 -080011785 * <p>If this view has an alpha value set to < 1.0 by calling
11786 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
11787 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
11788 * equivalent to setting a hardware layer on this view and providing a paint with
11789 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080011790 *
Romain Guy171c5922011-01-06 10:04:23 -080011791 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
11792 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
11793 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011794 *
Romain Guy171c5922011-01-06 10:04:23 -080011795 * @param layerType The ype of layer to use with this view, must be one of
11796 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11797 * {@link #LAYER_TYPE_HARDWARE}
11798 * @param paint The paint used to compose the layer. This argument is optional
11799 * and can be null. It is ignored when the layer type is
11800 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080011801 *
11802 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080011803 * @see #LAYER_TYPE_NONE
11804 * @see #LAYER_TYPE_SOFTWARE
11805 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080011806 * @see #setAlpha(float)
11807 *
Romain Guy171c5922011-01-06 10:04:23 -080011808 * @attr ref android.R.styleable#View_layerType
11809 */
11810 public void setLayerType(int layerType, Paint paint) {
11811 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080011812 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080011813 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
11814 }
Chet Haasedaf98e92011-01-10 14:10:36 -080011815
Romain Guyd6cd5722011-01-17 14:42:41 -080011816 if (layerType == mLayerType) {
11817 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
11818 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011819 invalidateParentCaches();
11820 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080011821 }
11822 return;
11823 }
Romain Guy171c5922011-01-06 10:04:23 -080011824
11825 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080011826 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070011827 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070011828 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011829 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080011830 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070011831 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011832 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080011833 default:
11834 break;
Romain Guy171c5922011-01-06 10:04:23 -080011835 }
11836
11837 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080011838 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
11839 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
11840 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080011841
Romain Guy0fd89bf2011-01-26 15:41:30 -080011842 invalidateParentCaches();
11843 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080011844 }
11845
11846 /**
Romain Guy59c7f802011-09-29 17:21:45 -070011847 * Indicates whether this view has a static layer. A view with layer type
11848 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
11849 * dynamic.
11850 */
11851 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080011852 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070011853 }
11854
11855 /**
Romain Guy171c5922011-01-06 10:04:23 -080011856 * Indicates what type of layer is currently associated with this view. By default
11857 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
11858 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
11859 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080011860 *
Romain Guy171c5922011-01-06 10:04:23 -080011861 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11862 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080011863 *
11864 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070011865 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080011866 * @see #LAYER_TYPE_NONE
11867 * @see #LAYER_TYPE_SOFTWARE
11868 * @see #LAYER_TYPE_HARDWARE
11869 */
11870 public int getLayerType() {
11871 return mLayerType;
11872 }
Joe Malin32736f02011-01-19 16:14:20 -080011873
Romain Guy6c319ca2011-01-11 14:29:25 -080011874 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080011875 * Forces this view's layer to be created and this view to be rendered
11876 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
11877 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070011878 *
Romain Guyf1ae1062011-03-02 18:16:04 -080011879 * This method can for instance be used to render a view into its layer before
11880 * starting an animation. If this view is complex, rendering into the layer
11881 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070011882 *
Romain Guyf1ae1062011-03-02 18:16:04 -080011883 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070011884 *
11885 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080011886 */
11887 public void buildLayer() {
11888 if (mLayerType == LAYER_TYPE_NONE) return;
11889
11890 if (mAttachInfo == null) {
11891 throw new IllegalStateException("This view must be attached to a window first");
11892 }
11893
11894 switch (mLayerType) {
11895 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080011896 if (mAttachInfo.mHardwareRenderer != null &&
11897 mAttachInfo.mHardwareRenderer.isEnabled() &&
11898 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080011899 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080011900 }
Romain Guyf1ae1062011-03-02 18:16:04 -080011901 break;
11902 case LAYER_TYPE_SOFTWARE:
11903 buildDrawingCache(true);
11904 break;
11905 }
11906 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011907
Romain Guy9c4b79a2011-11-10 19:23:58 -080011908 // Make sure the HardwareRenderer.validate() was invoked before calling this method
11909 void flushLayer() {
11910 if (mLayerType == LAYER_TYPE_HARDWARE && mHardwareLayer != null) {
11911 mHardwareLayer.flush();
11912 }
11913 }
Romain Guyf1ae1062011-03-02 18:16:04 -080011914
11915 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080011916 * <p>Returns a hardware layer that can be used to draw this view again
11917 * without executing its draw method.</p>
11918 *
11919 * @return A HardwareLayer ready to render, or null if an error occurred.
11920 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080011921 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070011922 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
11923 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080011924 return null;
11925 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011926
Romain Guy9c4b79a2011-11-10 19:23:58 -080011927 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080011928
11929 final int width = mRight - mLeft;
11930 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080011931
Romain Guy6c319ca2011-01-11 14:29:25 -080011932 if (width == 0 || height == 0) {
11933 return null;
11934 }
11935
11936 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
11937 if (mHardwareLayer == null) {
11938 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
11939 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070011940 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080011941 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
11942 mHardwareLayer.resize(width, height);
Michael Jurka952e02b2012-03-13 18:34:35 -070011943 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080011944 }
11945
Romain Guy5cd5c3f2011-10-17 17:10:02 -070011946 // The layer is not valid if the underlying GPU resources cannot be allocated
11947 if (!mHardwareLayer.isValid()) {
11948 return null;
11949 }
11950
Chet Haasea1cff502012-02-21 13:43:44 -080011951 mHardwareLayer.redraw(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
Michael Jurka7e52caf2012-03-06 15:57:06 -080011952 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080011953 }
11954
11955 return mHardwareLayer;
11956 }
Romain Guy171c5922011-01-06 10:04:23 -080011957
Romain Guy589b0bb2011-10-10 13:57:47 -070011958 /**
11959 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070011960 *
Romain Guy589b0bb2011-10-10 13:57:47 -070011961 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070011962 *
11963 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070011964 * @see #LAYER_TYPE_HARDWARE
11965 */
Romain Guya998dff2012-03-23 18:58:36 -070011966 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070011967 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080011968 AttachInfo info = mAttachInfo;
11969 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070011970 info.mHardwareRenderer.isEnabled() &&
11971 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080011972 mHardwareLayer.destroy();
11973 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080011974
Romain Guy9c4b79a2011-11-10 19:23:58 -080011975 invalidate(true);
11976 invalidateParentCaches();
11977 }
Romain Guy65b345f2011-07-27 18:51:50 -070011978 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070011979 }
Romain Guy65b345f2011-07-27 18:51:50 -070011980 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070011981 }
11982
Romain Guy171c5922011-01-06 10:04:23 -080011983 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080011984 * Destroys all hardware rendering resources. This method is invoked
11985 * when the system needs to reclaim resources. Upon execution of this
11986 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070011987 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080011988 * Note: you <strong>must</strong> call
11989 * <code>super.destroyHardwareResources()</code> when overriding
11990 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070011991 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080011992 * @hide
11993 */
11994 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070011995 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011996 }
11997
11998 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011999 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
12000 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
12001 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
12002 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
12003 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
12004 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012005 *
Romain Guy171c5922011-01-06 10:04:23 -080012006 * <p>Enabling the drawing cache is similar to
12007 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080012008 * acceleration is turned off. When hardware acceleration is turned on, enabling the
12009 * drawing cache has no effect on rendering because the system uses a different mechanism
12010 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
12011 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
12012 * for information on how to enable software and hardware layers.</p>
12013 *
12014 * <p>This API can be used to manually generate
12015 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
12016 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012017 *
12018 * @param enabled true to enable the drawing cache, false otherwise
12019 *
12020 * @see #isDrawingCacheEnabled()
12021 * @see #getDrawingCache()
12022 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080012023 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012024 */
12025 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012026 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012027 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
12028 }
12029
12030 /**
12031 * <p>Indicates whether the drawing cache is enabled for this view.</p>
12032 *
12033 * @return true if the drawing cache is enabled
12034 *
12035 * @see #setDrawingCacheEnabled(boolean)
12036 * @see #getDrawingCache()
12037 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012038 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012039 public boolean isDrawingCacheEnabled() {
12040 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
12041 }
12042
12043 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080012044 * Debugging utility which recursively outputs the dirty state of a view and its
12045 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080012046 *
Chet Haasedaf98e92011-01-10 14:10:36 -080012047 * @hide
12048 */
Romain Guy676b1732011-02-14 14:45:33 -080012049 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080012050 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
12051 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
12052 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
12053 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
12054 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
12055 if (clear) {
12056 mPrivateFlags &= clearMask;
12057 }
12058 if (this instanceof ViewGroup) {
12059 ViewGroup parent = (ViewGroup) this;
12060 final int count = parent.getChildCount();
12061 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080012062 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080012063 child.outputDirtyFlags(indent + " ", clear, clearMask);
12064 }
12065 }
12066 }
12067
12068 /**
12069 * This method is used by ViewGroup to cause its children to restore or recreate their
12070 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
12071 * to recreate its own display list, which would happen if it went through the normal
12072 * draw/dispatchDraw mechanisms.
12073 *
12074 * @hide
12075 */
12076 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080012077
12078 /**
12079 * A view that is not attached or hardware accelerated cannot create a display list.
12080 * This method checks these conditions and returns the appropriate result.
12081 *
12082 * @return true if view has the ability to create a display list, false otherwise.
12083 *
12084 * @hide
12085 */
12086 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080012087 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080012088 }
Joe Malin32736f02011-01-19 16:14:20 -080012089
Chet Haasedaf98e92011-01-10 14:10:36 -080012090 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080012091 * @return The HardwareRenderer associated with that view or null if hardware rendering
12092 * is not supported or this this has not been attached to a window.
12093 *
12094 * @hide
12095 */
12096 public HardwareRenderer getHardwareRenderer() {
12097 if (mAttachInfo != null) {
12098 return mAttachInfo.mHardwareRenderer;
12099 }
12100 return null;
12101 }
12102
12103 /**
Chet Haasea1cff502012-02-21 13:43:44 -080012104 * Returns a DisplayList. If the incoming displayList is null, one will be created.
12105 * Otherwise, the same display list will be returned (after having been rendered into
12106 * along the way, depending on the invalidation state of the view).
12107 *
12108 * @param displayList The previous version of this displayList, could be null.
12109 * @param isLayer Whether the requester of the display list is a layer. If so,
12110 * the view will avoid creating a layer inside the resulting display list.
12111 * @return A new or reused DisplayList object.
12112 */
12113 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
12114 if (!canHaveDisplayList()) {
12115 return null;
12116 }
12117
12118 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
12119 displayList == null || !displayList.isValid() ||
12120 (!isLayer && mRecreateDisplayList))) {
12121 // Don't need to recreate the display list, just need to tell our
12122 // children to restore/recreate theirs
12123 if (displayList != null && displayList.isValid() &&
12124 !isLayer && !mRecreateDisplayList) {
12125 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12126 mPrivateFlags &= ~DIRTY_MASK;
12127 dispatchGetDisplayList();
12128
12129 return displayList;
12130 }
12131
12132 if (!isLayer) {
12133 // If we got here, we're recreating it. Mark it as such to ensure that
12134 // we copy in child display lists into ours in drawChild()
12135 mRecreateDisplayList = true;
12136 }
12137 if (displayList == null) {
12138 final String name = getClass().getSimpleName();
12139 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
12140 // If we're creating a new display list, make sure our parent gets invalidated
12141 // since they will need to recreate their display list to account for this
12142 // new child display list.
12143 invalidateParentCaches();
12144 }
12145
12146 boolean caching = false;
12147 final HardwareCanvas canvas = displayList.start();
Chet Haasea1cff502012-02-21 13:43:44 -080012148 int width = mRight - mLeft;
12149 int height = mBottom - mTop;
12150
12151 try {
12152 canvas.setViewport(width, height);
12153 // The dirty rect should always be null for a display list
12154 canvas.onPreDraw(null);
12155 int layerType = (
12156 !(mParent instanceof ViewGroup) || ((ViewGroup)mParent).mDrawLayers) ?
12157 getLayerType() : LAYER_TYPE_NONE;
Chet Haase1271e2c2012-04-20 09:54:27 -070012158 if (!isLayer && layerType != LAYER_TYPE_NONE) {
Chet Haaseb85967b2012-03-26 14:37:51 -070012159 if (layerType == LAYER_TYPE_HARDWARE) {
12160 final HardwareLayer layer = getHardwareLayer();
12161 if (layer != null && layer.isValid()) {
12162 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
12163 } else {
12164 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
12165 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
12166 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12167 }
12168 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080012169 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070012170 buildDrawingCache(true);
12171 Bitmap cache = getDrawingCache(true);
12172 if (cache != null) {
12173 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
12174 caching = true;
12175 }
Chet Haasea1cff502012-02-21 13:43:44 -080012176 }
Chet Haasea1cff502012-02-21 13:43:44 -080012177 } else {
12178
12179 computeScroll();
12180
Chet Haasea1cff502012-02-21 13:43:44 -080012181 canvas.translate(-mScrollX, -mScrollY);
12182 if (!isLayer) {
12183 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12184 mPrivateFlags &= ~DIRTY_MASK;
12185 }
12186
12187 // Fast path for layouts with no backgrounds
12188 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12189 dispatchDraw(canvas);
12190 } else {
12191 draw(canvas);
12192 }
12193 }
12194 } finally {
Chet Haasea1cff502012-02-21 13:43:44 -080012195 canvas.onPostDraw();
12196
12197 displayList.end();
Chet Haase1271e2c2012-04-20 09:54:27 -070012198 displayList.setCaching(caching);
12199 if (isLayer) {
Chet Haasea1cff502012-02-21 13:43:44 -080012200 displayList.setLeftTopRightBottom(0, 0, width, height);
12201 } else {
12202 setDisplayListProperties(displayList);
12203 }
12204 }
12205 } else if (!isLayer) {
12206 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12207 mPrivateFlags &= ~DIRTY_MASK;
12208 }
12209
12210 return displayList;
12211 }
12212
12213 /**
12214 * Get the DisplayList for the HardwareLayer
12215 *
12216 * @param layer The HardwareLayer whose DisplayList we want
12217 * @return A DisplayList fopr the specified HardwareLayer
12218 */
12219 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
12220 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
12221 layer.setDisplayList(displayList);
12222 return displayList;
12223 }
12224
12225
12226 /**
Romain Guyb051e892010-09-28 19:09:36 -070012227 * <p>Returns a display list that can be used to draw this view again
12228 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012229 *
Romain Guyb051e892010-09-28 19:09:36 -070012230 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080012231 *
12232 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070012233 */
Chet Haasedaf98e92011-01-10 14:10:36 -080012234 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080012235 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070012236 return mDisplayList;
12237 }
12238
Romain Guy38c2ece2012-05-24 14:20:56 -070012239 private void clearDisplayList() {
12240 if (mDisplayList != null) {
12241 mDisplayList.invalidate();
12242 mDisplayList.clear();
12243 }
12244 }
12245
Romain Guyb051e892010-09-28 19:09:36 -070012246 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012247 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012248 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012249 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012250 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012251 * @see #getDrawingCache(boolean)
12252 */
12253 public Bitmap getDrawingCache() {
12254 return getDrawingCache(false);
12255 }
12256
12257 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012258 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
12259 * is null when caching is disabled. If caching is enabled and the cache is not ready,
12260 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
12261 * draw from the cache when the cache is enabled. To benefit from the cache, you must
12262 * request the drawing cache by calling this method and draw it on screen if the
12263 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012264 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012265 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12266 * this method will create a bitmap of the same size as this view. Because this bitmap
12267 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12268 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12269 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12270 * size than the view. This implies that your application must be able to handle this
12271 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012272 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012273 * @param autoScale Indicates whether the generated bitmap should be scaled based on
12274 * the current density of the screen when the application is in compatibility
12275 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012276 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012277 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012278 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012279 * @see #setDrawingCacheEnabled(boolean)
12280 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070012281 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012282 * @see #destroyDrawingCache()
12283 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012284 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012285 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
12286 return null;
12287 }
12288 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012289 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012290 }
Romain Guy02890fd2010-08-06 17:58:44 -070012291 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012292 }
12293
12294 /**
12295 * <p>Frees the resources used by the drawing cache. If you call
12296 * {@link #buildDrawingCache()} manually without calling
12297 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12298 * should cleanup the cache with this method afterwards.</p>
12299 *
12300 * @see #setDrawingCacheEnabled(boolean)
12301 * @see #buildDrawingCache()
12302 * @see #getDrawingCache()
12303 */
12304 public void destroyDrawingCache() {
12305 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012306 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012307 mDrawingCache = null;
12308 }
Romain Guyfbd8f692009-06-26 14:51:58 -070012309 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012310 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070012311 mUnscaledDrawingCache = null;
12312 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012313 }
12314
12315 /**
12316 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070012317 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012318 * view will always be drawn on top of a solid color.
12319 *
12320 * @param color The background color to use for the drawing cache's bitmap
12321 *
12322 * @see #setDrawingCacheEnabled(boolean)
12323 * @see #buildDrawingCache()
12324 * @see #getDrawingCache()
12325 */
12326 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080012327 if (color != mDrawingCacheBackgroundColor) {
12328 mDrawingCacheBackgroundColor = color;
12329 mPrivateFlags &= ~DRAWING_CACHE_VALID;
12330 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012331 }
12332
12333 /**
12334 * @see #setDrawingCacheBackgroundColor(int)
12335 *
12336 * @return The background color to used for the drawing cache's bitmap
12337 */
12338 public int getDrawingCacheBackgroundColor() {
12339 return mDrawingCacheBackgroundColor;
12340 }
12341
12342 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012343 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012344 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012345 * @see #buildDrawingCache(boolean)
12346 */
12347 public void buildDrawingCache() {
12348 buildDrawingCache(false);
12349 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080012350
Romain Guyfbd8f692009-06-26 14:51:58 -070012351 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012352 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
12353 *
12354 * <p>If you call {@link #buildDrawingCache()} manually without calling
12355 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12356 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012357 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012358 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12359 * this method will create a bitmap of the same size as this view. Because this bitmap
12360 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12361 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12362 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12363 * size than the view. This implies that your application must be able to handle this
12364 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012365 *
Romain Guy0d9275e2010-10-26 14:22:30 -070012366 * <p>You should avoid calling this method when hardware acceleration is enabled. If
12367 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080012368 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070012369 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012370 *
12371 * @see #getDrawingCache()
12372 * @see #destroyDrawingCache()
12373 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012374 public void buildDrawingCache(boolean autoScale) {
12375 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070012376 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012377 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012378
Romain Guy8506ab42009-06-11 17:35:47 -070012379 int width = mRight - mLeft;
12380 int height = mBottom - mTop;
12381
12382 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070012383 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070012384
Romain Guye1123222009-06-29 14:24:56 -070012385 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012386 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
12387 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070012388 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012389
12390 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070012391 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080012392 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012393
12394 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070012395 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080012396 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012397 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
12398 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080012399 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012400 return;
12401 }
12402
12403 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070012404 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012405
12406 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012407 Bitmap.Config quality;
12408 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080012409 // Never pick ARGB_4444 because it looks awful
12410 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012411 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
12412 case DRAWING_CACHE_QUALITY_AUTO:
12413 quality = Bitmap.Config.ARGB_8888;
12414 break;
12415 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080012416 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012417 break;
12418 case DRAWING_CACHE_QUALITY_HIGH:
12419 quality = Bitmap.Config.ARGB_8888;
12420 break;
12421 default:
12422 quality = Bitmap.Config.ARGB_8888;
12423 break;
12424 }
12425 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070012426 // Optimization for translucent windows
12427 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080012428 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012429 }
12430
12431 // Try to cleanup memory
12432 if (bitmap != null) bitmap.recycle();
12433
12434 try {
12435 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070012436 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070012437 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070012438 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012439 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070012440 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012441 }
Adam Powell26153a32010-11-08 15:22:27 -080012442 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012443 } catch (OutOfMemoryError e) {
12444 // If there is not enough memory to create the bitmap cache, just
12445 // ignore the issue as bitmap caches are not required to draw the
12446 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012447 if (autoScale) {
12448 mDrawingCache = null;
12449 } else {
12450 mUnscaledDrawingCache = null;
12451 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012452 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012453 return;
12454 }
12455
12456 clear = drawingCacheBackgroundColor != 0;
12457 }
12458
12459 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012460 if (attachInfo != null) {
12461 canvas = attachInfo.mCanvas;
12462 if (canvas == null) {
12463 canvas = new Canvas();
12464 }
12465 canvas.setBitmap(bitmap);
12466 // Temporarily clobber the cached Canvas in case one of our children
12467 // is also using a drawing cache. Without this, the children would
12468 // steal the canvas by attaching their own bitmap to it and bad, bad
12469 // thing would happen (invisible views, corrupted drawings, etc.)
12470 attachInfo.mCanvas = null;
12471 } else {
12472 // This case should hopefully never or seldom happen
12473 canvas = new Canvas(bitmap);
12474 }
12475
12476 if (clear) {
12477 bitmap.eraseColor(drawingCacheBackgroundColor);
12478 }
12479
12480 computeScroll();
12481 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080012482
Romain Guye1123222009-06-29 14:24:56 -070012483 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012484 final float scale = attachInfo.mApplicationScale;
12485 canvas.scale(scale, scale);
12486 }
Joe Malin32736f02011-01-19 16:14:20 -080012487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012488 canvas.translate(-mScrollX, -mScrollY);
12489
Romain Guy5bcdff42009-05-14 21:27:18 -070012490 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080012491 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
12492 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070012493 mPrivateFlags |= DRAWING_CACHE_VALID;
12494 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012495
12496 // Fast path for layouts with no backgrounds
12497 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Romain Guy5bcdff42009-05-14 21:27:18 -070012498 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012499 dispatchDraw(canvas);
12500 } else {
12501 draw(canvas);
12502 }
12503
12504 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012505 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012506
12507 if (attachInfo != null) {
12508 // Restore the cached Canvas for our siblings
12509 attachInfo.mCanvas = canvas;
12510 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012511 }
12512 }
12513
12514 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012515 * Create a snapshot of the view into a bitmap. We should probably make
12516 * some form of this public, but should think about the API.
12517 */
Romain Guy223ff5c2010-03-02 17:07:47 -080012518 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012519 int width = mRight - mLeft;
12520 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012521
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012522 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070012523 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012524 width = (int) ((width * scale) + 0.5f);
12525 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080012526
Romain Guy8c11e312009-09-14 15:15:30 -070012527 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012528 if (bitmap == null) {
12529 throw new OutOfMemoryError();
12530 }
12531
Romain Guyc529d8d2011-09-06 15:01:39 -070012532 Resources resources = getResources();
12533 if (resources != null) {
12534 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
12535 }
Joe Malin32736f02011-01-19 16:14:20 -080012536
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012537 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012538 if (attachInfo != null) {
12539 canvas = attachInfo.mCanvas;
12540 if (canvas == null) {
12541 canvas = new Canvas();
12542 }
12543 canvas.setBitmap(bitmap);
12544 // Temporarily clobber the cached Canvas in case one of our children
12545 // is also using a drawing cache. Without this, the children would
12546 // steal the canvas by attaching their own bitmap to it and bad, bad
12547 // things would happen (invisible views, corrupted drawings, etc.)
12548 attachInfo.mCanvas = null;
12549 } else {
12550 // This case should hopefully never or seldom happen
12551 canvas = new Canvas(bitmap);
12552 }
12553
Romain Guy5bcdff42009-05-14 21:27:18 -070012554 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012555 bitmap.eraseColor(backgroundColor);
12556 }
12557
12558 computeScroll();
12559 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012560 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012561 canvas.translate(-mScrollX, -mScrollY);
12562
Romain Guy5bcdff42009-05-14 21:27:18 -070012563 // Temporarily remove the dirty mask
12564 int flags = mPrivateFlags;
12565 mPrivateFlags &= ~DIRTY_MASK;
12566
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012567 // Fast path for layouts with no backgrounds
12568 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12569 dispatchDraw(canvas);
12570 } else {
12571 draw(canvas);
12572 }
12573
Romain Guy5bcdff42009-05-14 21:27:18 -070012574 mPrivateFlags = flags;
12575
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012576 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012577 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012578
12579 if (attachInfo != null) {
12580 // Restore the cached Canvas for our siblings
12581 attachInfo.mCanvas = canvas;
12582 }
Romain Guy8506ab42009-06-11 17:35:47 -070012583
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012584 return bitmap;
12585 }
12586
12587 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012588 * Indicates whether this View is currently in edit mode. A View is usually
12589 * in edit mode when displayed within a developer tool. For instance, if
12590 * this View is being drawn by a visual user interface builder, this method
12591 * should return true.
12592 *
12593 * Subclasses should check the return value of this method to provide
12594 * different behaviors if their normal behavior might interfere with the
12595 * host environment. For instance: the class spawns a thread in its
12596 * constructor, the drawing code relies on device-specific features, etc.
12597 *
12598 * This method is usually checked in the drawing code of custom widgets.
12599 *
12600 * @return True if this View is in edit mode, false otherwise.
12601 */
12602 public boolean isInEditMode() {
12603 return false;
12604 }
12605
12606 /**
12607 * If the View draws content inside its padding and enables fading edges,
12608 * it needs to support padding offsets. Padding offsets are added to the
12609 * fading edges to extend the length of the fade so that it covers pixels
12610 * drawn inside the padding.
12611 *
12612 * Subclasses of this class should override this method if they need
12613 * to draw content inside the padding.
12614 *
12615 * @return True if padding offset must be applied, false otherwise.
12616 *
12617 * @see #getLeftPaddingOffset()
12618 * @see #getRightPaddingOffset()
12619 * @see #getTopPaddingOffset()
12620 * @see #getBottomPaddingOffset()
12621 *
12622 * @since CURRENT
12623 */
12624 protected boolean isPaddingOffsetRequired() {
12625 return false;
12626 }
12627
12628 /**
12629 * Amount by which to extend the left fading region. Called only when
12630 * {@link #isPaddingOffsetRequired()} returns true.
12631 *
12632 * @return The left padding offset in pixels.
12633 *
12634 * @see #isPaddingOffsetRequired()
12635 *
12636 * @since CURRENT
12637 */
12638 protected int getLeftPaddingOffset() {
12639 return 0;
12640 }
12641
12642 /**
12643 * Amount by which to extend the right fading region. Called only when
12644 * {@link #isPaddingOffsetRequired()} returns true.
12645 *
12646 * @return The right padding offset in pixels.
12647 *
12648 * @see #isPaddingOffsetRequired()
12649 *
12650 * @since CURRENT
12651 */
12652 protected int getRightPaddingOffset() {
12653 return 0;
12654 }
12655
12656 /**
12657 * Amount by which to extend the top fading region. Called only when
12658 * {@link #isPaddingOffsetRequired()} returns true.
12659 *
12660 * @return The top padding offset in pixels.
12661 *
12662 * @see #isPaddingOffsetRequired()
12663 *
12664 * @since CURRENT
12665 */
12666 protected int getTopPaddingOffset() {
12667 return 0;
12668 }
12669
12670 /**
12671 * Amount by which to extend the bottom fading region. Called only when
12672 * {@link #isPaddingOffsetRequired()} returns true.
12673 *
12674 * @return The bottom padding offset in pixels.
12675 *
12676 * @see #isPaddingOffsetRequired()
12677 *
12678 * @since CURRENT
12679 */
12680 protected int getBottomPaddingOffset() {
12681 return 0;
12682 }
12683
12684 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070012685 * @hide
12686 * @param offsetRequired
12687 */
12688 protected int getFadeTop(boolean offsetRequired) {
12689 int top = mPaddingTop;
12690 if (offsetRequired) top += getTopPaddingOffset();
12691 return top;
12692 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012693
Romain Guyf2fc4602011-07-19 15:20:03 -070012694 /**
12695 * @hide
12696 * @param offsetRequired
12697 */
12698 protected int getFadeHeight(boolean offsetRequired) {
12699 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070012700 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070012701 return mBottom - mTop - mPaddingBottom - padding;
12702 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012703
Romain Guyf2fc4602011-07-19 15:20:03 -070012704 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012705 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070012706 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012707 *
Romain Guy2bffd262010-09-12 17:40:02 -070012708 * <p>Even if this method returns true, it does not mean that every call
12709 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
12710 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012711 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070012712 * window is hardware accelerated,
12713 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
12714 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012715 *
Romain Guy2bffd262010-09-12 17:40:02 -070012716 * @return True if the view is attached to a window and the window is
12717 * hardware accelerated; false in any other case.
12718 */
12719 public boolean isHardwareAccelerated() {
12720 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
12721 }
Joe Malin32736f02011-01-19 16:14:20 -080012722
Romain Guy2bffd262010-09-12 17:40:02 -070012723 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080012724 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
12725 * case of an active Animation being run on the view.
12726 */
12727 private boolean drawAnimation(ViewGroup parent, long drawingTime,
12728 Animation a, boolean scalingRequired) {
12729 Transformation invalidationTransform;
12730 final int flags = parent.mGroupFlags;
12731 final boolean initialized = a.isInitialized();
12732 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070012733 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080012734 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
Romain Guy393a52c2012-05-22 20:21:08 -070012735 if (mAttachInfo != null) a.setListenerHandler(mAttachInfo.mHandler);
Chet Haasebcca79a2012-02-14 08:45:14 -080012736 onAnimationStart();
12737 }
12738
12739 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
12740 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
12741 if (parent.mInvalidationTransformation == null) {
12742 parent.mInvalidationTransformation = new Transformation();
12743 }
12744 invalidationTransform = parent.mInvalidationTransformation;
12745 a.getTransformation(drawingTime, invalidationTransform, 1f);
12746 } else {
12747 invalidationTransform = parent.mChildTransformation;
12748 }
Romain Guy393a52c2012-05-22 20:21:08 -070012749
Chet Haasebcca79a2012-02-14 08:45:14 -080012750 if (more) {
12751 if (!a.willChangeBounds()) {
12752 if ((flags & (parent.FLAG_OPTIMIZE_INVALIDATE | parent.FLAG_ANIMATION_DONE)) ==
12753 parent.FLAG_OPTIMIZE_INVALIDATE) {
12754 parent.mGroupFlags |= parent.FLAG_INVALIDATE_REQUIRED;
12755 } else if ((flags & parent.FLAG_INVALIDATE_REQUIRED) == 0) {
12756 // The child need to draw an animation, potentially offscreen, so
12757 // make sure we do not cancel invalidate requests
12758 parent.mPrivateFlags |= DRAW_ANIMATION;
12759 parent.invalidate(mLeft, mTop, mRight, mBottom);
12760 }
12761 } else {
12762 if (parent.mInvalidateRegion == null) {
12763 parent.mInvalidateRegion = new RectF();
12764 }
12765 final RectF region = parent.mInvalidateRegion;
12766 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
12767 invalidationTransform);
12768
12769 // The child need to draw an animation, potentially offscreen, so
12770 // make sure we do not cancel invalidate requests
12771 parent.mPrivateFlags |= DRAW_ANIMATION;
12772
12773 final int left = mLeft + (int) region.left;
12774 final int top = mTop + (int) region.top;
12775 parent.invalidate(left, top, left + (int) (region.width() + .5f),
12776 top + (int) (region.height() + .5f));
12777 }
12778 }
12779 return more;
12780 }
12781
Chet Haasea1cff502012-02-21 13:43:44 -080012782 /**
12783 * This method is called by getDisplayList() when a display list is created or re-rendered.
12784 * It sets or resets the current value of all properties on that display list (resetting is
12785 * necessary when a display list is being re-created, because we need to make sure that
12786 * previously-set transform values
12787 */
12788 void setDisplayListProperties(DisplayList displayList) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012789 if (displayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012790 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070012791 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080012792 if (mParent instanceof ViewGroup) {
12793 displayList.setClipChildren(
12794 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
12795 }
Chet Haase9420abd2012-03-29 16:28:32 -070012796 float alpha = 1;
12797 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
12798 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
12799 ViewGroup parentVG = (ViewGroup) mParent;
12800 final boolean hasTransform =
12801 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
12802 if (hasTransform) {
12803 Transformation transform = parentVG.mChildTransformation;
12804 final int transformType = parentVG.mChildTransformation.getTransformationType();
12805 if (transformType != Transformation.TYPE_IDENTITY) {
12806 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
12807 alpha = transform.getAlpha();
12808 }
12809 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
12810 displayList.setStaticMatrix(transform.getMatrix());
12811 }
12812 }
12813 }
Chet Haasea1cff502012-02-21 13:43:44 -080012814 }
12815 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070012816 alpha *= mTransformationInfo.mAlpha;
12817 if (alpha < 1) {
12818 final int multipliedAlpha = (int) (255 * alpha);
12819 if (onSetAlpha(multipliedAlpha)) {
12820 alpha = 1;
12821 }
12822 }
12823 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080012824 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
12825 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
12826 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
12827 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070012828 if (mTransformationInfo.mCamera == null) {
12829 mTransformationInfo.mCamera = new Camera();
12830 mTransformationInfo.matrix3D = new Matrix();
12831 }
12832 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Chet Haasea1cff502012-02-21 13:43:44 -080012833 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == PIVOT_EXPLICITLY_SET) {
12834 displayList.setPivotX(getPivotX());
12835 displayList.setPivotY(getPivotY());
12836 }
Chet Haase9420abd2012-03-29 16:28:32 -070012837 } else if (alpha < 1) {
12838 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080012839 }
12840 }
12841 }
12842
Chet Haasebcca79a2012-02-14 08:45:14 -080012843 /**
Chet Haase64a48c12012-02-13 16:33:29 -080012844 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
12845 * This draw() method is an implementation detail and is not intended to be overridden or
12846 * to be called from anywhere else other than ViewGroup.drawChild().
12847 */
12848 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012849 boolean useDisplayListProperties = mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080012850 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080012851 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080012852 final int flags = parent.mGroupFlags;
12853
Chet Haasea1cff502012-02-21 13:43:44 -080012854 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080012855 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080012856 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012857 }
12858
12859 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080012860 boolean concatMatrix = false;
12861
12862 boolean scalingRequired = false;
12863 boolean caching;
12864 int layerType = parent.mDrawLayers ? getLayerType() : LAYER_TYPE_NONE;
12865
12866 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080012867 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
12868 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080012869 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070012870 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080012871 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
12872 } else {
12873 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
12874 }
12875
Chet Haasebcca79a2012-02-14 08:45:14 -080012876 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080012877 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012878 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080012879 concatMatrix = a.willChangeTransformationMatrix();
Chet Haaseafd5c3e2012-05-10 13:21:10 -070012880 if (concatMatrix) {
12881 mPrivateFlags2 |= VIEW_IS_ANIMATING_TRANSFORM;
12882 }
Chet Haasebcca79a2012-02-14 08:45:14 -080012883 transformToApply = parent.mChildTransformation;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070012884 } else {
12885 if ((mPrivateFlags2 & VIEW_IS_ANIMATING_TRANSFORM) == VIEW_IS_ANIMATING_TRANSFORM &&
12886 mDisplayList != null) {
12887 // No longer animating: clear out old animation matrix
12888 mDisplayList.setAnimationMatrix(null);
12889 mPrivateFlags2 &= ~VIEW_IS_ANIMATING_TRANSFORM;
12890 }
12891 if (!useDisplayListProperties &&
12892 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
12893 final boolean hasTransform =
12894 parent.getChildStaticTransformation(this, parent.mChildTransformation);
12895 if (hasTransform) {
12896 final int transformType = parent.mChildTransformation.getTransformationType();
12897 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
12898 parent.mChildTransformation : null;
12899 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
12900 }
Chet Haase64a48c12012-02-13 16:33:29 -080012901 }
12902 }
12903
12904 concatMatrix |= !childHasIdentityMatrix;
12905
12906 // Sets the flag as early as possible to allow draw() implementations
12907 // to call invalidate() successfully when doing animations
12908 mPrivateFlags |= DRAWN;
12909
Chet Haasebcca79a2012-02-14 08:45:14 -080012910 if (!concatMatrix && canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Chet Haase64a48c12012-02-13 16:33:29 -080012911 (mPrivateFlags & DRAW_ANIMATION) == 0) {
Chet Haase1a3ab172012-05-11 08:41:20 -070012912 mPrivateFlags2 |= VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080012913 return more;
12914 }
Chet Haase1a3ab172012-05-11 08:41:20 -070012915 mPrivateFlags2 &= ~VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080012916
12917 if (hardwareAccelerated) {
12918 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
12919 // retain the flag's value temporarily in the mRecreateDisplayList flag
12920 mRecreateDisplayList = (mPrivateFlags & INVALIDATED) == INVALIDATED;
12921 mPrivateFlags &= ~INVALIDATED;
12922 }
12923
12924 computeScroll();
12925
12926 final int sx = mScrollX;
12927 final int sy = mScrollY;
12928
12929 DisplayList displayList = null;
12930 Bitmap cache = null;
12931 boolean hasDisplayList = false;
12932 if (caching) {
12933 if (!hardwareAccelerated) {
12934 if (layerType != LAYER_TYPE_NONE) {
12935 layerType = LAYER_TYPE_SOFTWARE;
12936 buildDrawingCache(true);
12937 }
12938 cache = getDrawingCache(true);
12939 } else {
12940 switch (layerType) {
12941 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070012942 if (useDisplayListProperties) {
12943 hasDisplayList = canHaveDisplayList();
12944 } else {
12945 buildDrawingCache(true);
12946 cache = getDrawingCache(true);
12947 }
Chet Haase64a48c12012-02-13 16:33:29 -080012948 break;
Chet Haasea1cff502012-02-21 13:43:44 -080012949 case LAYER_TYPE_HARDWARE:
12950 if (useDisplayListProperties) {
12951 hasDisplayList = canHaveDisplayList();
12952 }
12953 break;
Chet Haase64a48c12012-02-13 16:33:29 -080012954 case LAYER_TYPE_NONE:
12955 // Delay getting the display list until animation-driven alpha values are
12956 // set up and possibly passed on to the view
12957 hasDisplayList = canHaveDisplayList();
12958 break;
12959 }
12960 }
12961 }
Chet Haasea1cff502012-02-21 13:43:44 -080012962 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070012963 if (useDisplayListProperties) {
12964 displayList = getDisplayList();
12965 if (!displayList.isValid()) {
12966 // Uncommon, but possible. If a view is removed from the hierarchy during the call
12967 // to getDisplayList(), the display list will be marked invalid and we should not
12968 // try to use it again.
12969 displayList = null;
12970 hasDisplayList = false;
12971 useDisplayListProperties = false;
12972 }
12973 }
Chet Haase64a48c12012-02-13 16:33:29 -080012974
12975 final boolean hasNoCache = cache == null || hasDisplayList;
12976 final boolean offsetForScroll = cache == null && !hasDisplayList &&
12977 layerType != LAYER_TYPE_HARDWARE;
12978
Chet Haasea1cff502012-02-21 13:43:44 -080012979 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070012980 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012981 restoreTo = canvas.save();
12982 }
Chet Haase64a48c12012-02-13 16:33:29 -080012983 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012984 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080012985 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080012986 if (!useDisplayListProperties) {
12987 canvas.translate(mLeft, mTop);
12988 }
Chet Haase64a48c12012-02-13 16:33:29 -080012989 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080012990 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070012991 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080012992 restoreTo = canvas.save();
12993 }
Chet Haase64a48c12012-02-13 16:33:29 -080012994 // mAttachInfo cannot be null, otherwise scalingRequired == false
12995 final float scale = 1.0f / mAttachInfo.mApplicationScale;
12996 canvas.scale(scale, scale);
12997 }
12998 }
12999
Chet Haasea1cff502012-02-21 13:43:44 -080013000 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070013001 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix()) {
Chet Haase64a48c12012-02-13 16:33:29 -080013002 if (transformToApply != null || !childHasIdentityMatrix) {
13003 int transX = 0;
13004 int transY = 0;
13005
13006 if (offsetForScroll) {
13007 transX = -sx;
13008 transY = -sy;
13009 }
13010
13011 if (transformToApply != null) {
13012 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070013013 if (useDisplayListProperties) {
13014 displayList.setAnimationMatrix(transformToApply.getMatrix());
13015 } else {
13016 // Undo the scroll translation, apply the transformation matrix,
13017 // then redo the scroll translate to get the correct result.
13018 canvas.translate(-transX, -transY);
13019 canvas.concat(transformToApply.getMatrix());
13020 canvas.translate(transX, transY);
13021 }
Chet Haasea1cff502012-02-21 13:43:44 -080013022 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013023 }
13024
13025 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070013026 if (transformAlpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080013027 alpha *= transformToApply.getAlpha();
Chet Haasea1cff502012-02-21 13:43:44 -080013028 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013029 }
13030 }
13031
Chet Haasea1cff502012-02-21 13:43:44 -080013032 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013033 canvas.translate(-transX, -transY);
13034 canvas.concat(getMatrix());
13035 canvas.translate(transX, transY);
13036 }
13037 }
13038
Chet Haase9420abd2012-03-29 16:28:32 -070013039 if (alpha < 1) {
Chet Haasea1cff502012-02-21 13:43:44 -080013040 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013041 if (hasNoCache) {
13042 final int multipliedAlpha = (int) (255 * alpha);
13043 if (!onSetAlpha(multipliedAlpha)) {
13044 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080013045 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080013046 layerType != LAYER_TYPE_NONE) {
13047 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
13048 }
Chet Haase9420abd2012-03-29 16:28:32 -070013049 if (useDisplayListProperties) {
13050 displayList.setAlpha(alpha * getAlpha());
13051 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070013052 final int scrollX = hasDisplayList ? 0 : sx;
13053 final int scrollY = hasDisplayList ? 0 : sy;
13054 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
13055 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080013056 }
13057 } else {
13058 // Alpha is handled by the child directly, clobber the layer's alpha
13059 mPrivateFlags |= ALPHA_SET;
13060 }
13061 }
13062 }
13063 } else if ((mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
13064 onSetAlpha(255);
13065 mPrivateFlags &= ~ALPHA_SET;
13066 }
13067
Chet Haasea1cff502012-02-21 13:43:44 -080013068 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
13069 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013070 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013071 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080013072 } else {
13073 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013074 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080013075 } else {
13076 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
13077 }
13078 }
13079 }
13080
Chet Haase9420abd2012-03-29 16:28:32 -070013081 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080013082 displayList = getDisplayList();
13083 if (!displayList.isValid()) {
13084 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13085 // to getDisplayList(), the display list will be marked invalid and we should not
13086 // try to use it again.
13087 displayList = null;
13088 hasDisplayList = false;
13089 }
13090 }
13091
13092 if (hasNoCache) {
13093 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080013094 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080013095 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080013096 if (layer != null && layer.isValid()) {
13097 mLayerPaint.setAlpha((int) (alpha * 255));
13098 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
13099 layerRendered = true;
13100 } else {
13101 final int scrollX = hasDisplayList ? 0 : sx;
13102 final int scrollY = hasDisplayList ? 0 : sy;
13103 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080013104 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080013105 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
13106 }
13107 }
13108
13109 if (!layerRendered) {
13110 if (!hasDisplayList) {
13111 // Fast path for layouts with no backgrounds
13112 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Chet Haase64a48c12012-02-13 16:33:29 -080013113 mPrivateFlags &= ~DIRTY_MASK;
13114 dispatchDraw(canvas);
13115 } else {
13116 draw(canvas);
13117 }
13118 } else {
13119 mPrivateFlags &= ~DIRTY_MASK;
Chet Haase1271e2c2012-04-20 09:54:27 -070013120 ((HardwareCanvas) canvas).drawDisplayList(displayList, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080013121 }
13122 }
13123 } else if (cache != null) {
13124 mPrivateFlags &= ~DIRTY_MASK;
13125 Paint cachePaint;
13126
13127 if (layerType == LAYER_TYPE_NONE) {
13128 cachePaint = parent.mCachePaint;
13129 if (cachePaint == null) {
13130 cachePaint = new Paint();
13131 cachePaint.setDither(false);
13132 parent.mCachePaint = cachePaint;
13133 }
Chet Haase9420abd2012-03-29 16:28:32 -070013134 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080013135 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080013136 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
13137 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013138 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080013139 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080013140 }
13141 } else {
13142 cachePaint = mLayerPaint;
13143 cachePaint.setAlpha((int) (alpha * 255));
13144 }
13145 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
13146 }
13147
Chet Haasea1cff502012-02-21 13:43:44 -080013148 if (restoreTo >= 0) {
13149 canvas.restoreToCount(restoreTo);
13150 }
Chet Haase64a48c12012-02-13 16:33:29 -080013151
13152 if (a != null && !more) {
13153 if (!hardwareAccelerated && !a.getFillAfter()) {
13154 onSetAlpha(255);
13155 }
13156 parent.finishAnimatingView(this, a);
13157 }
13158
13159 if (more && hardwareAccelerated) {
13160 // invalidation is the trigger to recreate display lists, so if we're using
13161 // display lists to render, force an invalidate to allow the animation to
13162 // continue drawing another frame
13163 parent.invalidate(true);
13164 if (a.hasAlpha() && (mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
13165 // alpha animations should cause the child to recreate its display list
13166 invalidate(true);
13167 }
13168 }
13169
13170 mRecreateDisplayList = false;
13171
13172 return more;
13173 }
13174
13175 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013176 * Manually render this view (and all of its children) to the given Canvas.
13177 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070013178 * called. When implementing a view, implement
13179 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
13180 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013181 *
13182 * @param canvas The Canvas to which the View is rendered.
13183 */
13184 public void draw(Canvas canvas) {
Romain Guy5bcdff42009-05-14 21:27:18 -070013185 final int privateFlags = mPrivateFlags;
13186 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
13187 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
13188 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070013189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013190 /*
13191 * Draw traversal performs several drawing steps which must be executed
13192 * in the appropriate order:
13193 *
13194 * 1. Draw the background
13195 * 2. If necessary, save the canvas' layers to prepare for fading
13196 * 3. Draw view's content
13197 * 4. Draw children
13198 * 5. If necessary, draw the fading edges and restore layers
13199 * 6. Draw decorations (scrollbars for instance)
13200 */
13201
13202 // Step 1, draw the background, if needed
13203 int saveCount;
13204
Romain Guy24443ea2009-05-11 11:56:30 -070013205 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013206 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070013207 if (background != null) {
13208 final int scrollX = mScrollX;
13209 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013210
Romain Guy24443ea2009-05-11 11:56:30 -070013211 if (mBackgroundSizeChanged) {
13212 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
13213 mBackgroundSizeChanged = false;
13214 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013215
Romain Guy24443ea2009-05-11 11:56:30 -070013216 if ((scrollX | scrollY) == 0) {
13217 background.draw(canvas);
13218 } else {
13219 canvas.translate(scrollX, scrollY);
13220 background.draw(canvas);
13221 canvas.translate(-scrollX, -scrollY);
13222 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013223 }
13224 }
13225
13226 // skip step 2 & 5 if possible (common case)
13227 final int viewFlags = mViewFlags;
13228 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
13229 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
13230 if (!verticalEdges && !horizontalEdges) {
13231 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013232 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013233
13234 // Step 4, draw the children
13235 dispatchDraw(canvas);
13236
13237 // Step 6, draw decorations (scrollbars)
13238 onDrawScrollBars(canvas);
13239
13240 // we're done...
13241 return;
13242 }
13243
13244 /*
13245 * Here we do the full fledged routine...
13246 * (this is an uncommon case where speed matters less,
13247 * this is why we repeat some of the tests that have been
13248 * done above)
13249 */
13250
13251 boolean drawTop = false;
13252 boolean drawBottom = false;
13253 boolean drawLeft = false;
13254 boolean drawRight = false;
13255
13256 float topFadeStrength = 0.0f;
13257 float bottomFadeStrength = 0.0f;
13258 float leftFadeStrength = 0.0f;
13259 float rightFadeStrength = 0.0f;
13260
13261 // Step 2, save the canvas' layers
13262 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013263
13264 final boolean offsetRequired = isPaddingOffsetRequired();
13265 if (offsetRequired) {
13266 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013267 }
13268
13269 int left = mScrollX + paddingLeft;
13270 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070013271 int top = mScrollY + getFadeTop(offsetRequired);
13272 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013273
13274 if (offsetRequired) {
13275 right += getRightPaddingOffset();
13276 bottom += getBottomPaddingOffset();
13277 }
13278
13279 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070013280 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013281 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013282
13283 // clip the fade length if top and bottom fades overlap
13284 // overlapping fades produce odd-looking artifacts
13285 if (verticalEdges && (top + length > bottom - length)) {
13286 length = (bottom - top) / 2;
13287 }
13288
13289 // also clip horizontal fades if necessary
13290 if (horizontalEdges && (left + length > right - length)) {
13291 length = (right - left) / 2;
13292 }
13293
13294 if (verticalEdges) {
13295 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013296 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013297 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013298 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013299 }
13300
13301 if (horizontalEdges) {
13302 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013303 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013304 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013305 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013306 }
13307
13308 saveCount = canvas.getSaveCount();
13309
13310 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070013311 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013312 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
13313
13314 if (drawTop) {
13315 canvas.saveLayer(left, top, right, top + length, null, flags);
13316 }
13317
13318 if (drawBottom) {
13319 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
13320 }
13321
13322 if (drawLeft) {
13323 canvas.saveLayer(left, top, left + length, bottom, null, flags);
13324 }
13325
13326 if (drawRight) {
13327 canvas.saveLayer(right - length, top, right, bottom, null, flags);
13328 }
13329 } else {
13330 scrollabilityCache.setFadeColor(solidColor);
13331 }
13332
13333 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013334 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013335
13336 // Step 4, draw the children
13337 dispatchDraw(canvas);
13338
13339 // Step 5, draw the fade effect and restore layers
13340 final Paint p = scrollabilityCache.paint;
13341 final Matrix matrix = scrollabilityCache.matrix;
13342 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013343
13344 if (drawTop) {
13345 matrix.setScale(1, fadeHeight * topFadeStrength);
13346 matrix.postTranslate(left, top);
13347 fade.setLocalMatrix(matrix);
13348 canvas.drawRect(left, top, right, top + length, p);
13349 }
13350
13351 if (drawBottom) {
13352 matrix.setScale(1, fadeHeight * bottomFadeStrength);
13353 matrix.postRotate(180);
13354 matrix.postTranslate(left, bottom);
13355 fade.setLocalMatrix(matrix);
13356 canvas.drawRect(left, bottom - length, right, bottom, p);
13357 }
13358
13359 if (drawLeft) {
13360 matrix.setScale(1, fadeHeight * leftFadeStrength);
13361 matrix.postRotate(-90);
13362 matrix.postTranslate(left, top);
13363 fade.setLocalMatrix(matrix);
13364 canvas.drawRect(left, top, left + length, bottom, p);
13365 }
13366
13367 if (drawRight) {
13368 matrix.setScale(1, fadeHeight * rightFadeStrength);
13369 matrix.postRotate(90);
13370 matrix.postTranslate(right, top);
13371 fade.setLocalMatrix(matrix);
13372 canvas.drawRect(right - length, top, right, bottom, p);
13373 }
13374
13375 canvas.restoreToCount(saveCount);
13376
13377 // Step 6, draw decorations (scrollbars)
13378 onDrawScrollBars(canvas);
13379 }
13380
13381 /**
13382 * Override this if your view is known to always be drawn on top of a solid color background,
13383 * and needs to draw fading edges. Returning a non-zero color enables the view system to
13384 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
13385 * should be set to 0xFF.
13386 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013387 * @see #setVerticalFadingEdgeEnabled(boolean)
13388 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013389 *
13390 * @return The known solid color background for this view, or 0 if the color may vary
13391 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013392 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013393 public int getSolidColor() {
13394 return 0;
13395 }
13396
13397 /**
13398 * Build a human readable string representation of the specified view flags.
13399 *
13400 * @param flags the view flags to convert to a string
13401 * @return a String representing the supplied flags
13402 */
13403 private static String printFlags(int flags) {
13404 String output = "";
13405 int numFlags = 0;
13406 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
13407 output += "TAKES_FOCUS";
13408 numFlags++;
13409 }
13410
13411 switch (flags & VISIBILITY_MASK) {
13412 case INVISIBLE:
13413 if (numFlags > 0) {
13414 output += " ";
13415 }
13416 output += "INVISIBLE";
13417 // USELESS HERE numFlags++;
13418 break;
13419 case GONE:
13420 if (numFlags > 0) {
13421 output += " ";
13422 }
13423 output += "GONE";
13424 // USELESS HERE numFlags++;
13425 break;
13426 default:
13427 break;
13428 }
13429 return output;
13430 }
13431
13432 /**
13433 * Build a human readable string representation of the specified private
13434 * view flags.
13435 *
13436 * @param privateFlags the private view flags to convert to a string
13437 * @return a String representing the supplied flags
13438 */
13439 private static String printPrivateFlags(int privateFlags) {
13440 String output = "";
13441 int numFlags = 0;
13442
13443 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
13444 output += "WANTS_FOCUS";
13445 numFlags++;
13446 }
13447
13448 if ((privateFlags & FOCUSED) == FOCUSED) {
13449 if (numFlags > 0) {
13450 output += " ";
13451 }
13452 output += "FOCUSED";
13453 numFlags++;
13454 }
13455
13456 if ((privateFlags & SELECTED) == SELECTED) {
13457 if (numFlags > 0) {
13458 output += " ";
13459 }
13460 output += "SELECTED";
13461 numFlags++;
13462 }
13463
13464 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
13465 if (numFlags > 0) {
13466 output += " ";
13467 }
13468 output += "IS_ROOT_NAMESPACE";
13469 numFlags++;
13470 }
13471
13472 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
13473 if (numFlags > 0) {
13474 output += " ";
13475 }
13476 output += "HAS_BOUNDS";
13477 numFlags++;
13478 }
13479
13480 if ((privateFlags & DRAWN) == DRAWN) {
13481 if (numFlags > 0) {
13482 output += " ";
13483 }
13484 output += "DRAWN";
13485 // USELESS HERE numFlags++;
13486 }
13487 return output;
13488 }
13489
13490 /**
13491 * <p>Indicates whether or not this view's layout will be requested during
13492 * the next hierarchy layout pass.</p>
13493 *
13494 * @return true if the layout will be forced during next layout pass
13495 */
13496 public boolean isLayoutRequested() {
13497 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
13498 }
13499
13500 /**
13501 * Assign a size and position to a view and all of its
13502 * descendants
13503 *
13504 * <p>This is the second phase of the layout mechanism.
13505 * (The first is measuring). In this phase, each parent calls
13506 * layout on all of its children to position them.
13507 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080013508 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013509 *
Chet Haase9c087442011-01-12 16:20:16 -080013510 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013511 * Derived classes with children should override
13512 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080013513 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013514 *
13515 * @param l Left position, relative to parent
13516 * @param t Top position, relative to parent
13517 * @param r Right position, relative to parent
13518 * @param b Bottom position, relative to parent
13519 */
Romain Guy5429e1d2010-09-07 12:38:00 -070013520 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080013521 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070013522 int oldL = mLeft;
13523 int oldT = mTop;
13524 int oldB = mBottom;
13525 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013526 boolean changed = setFrame(l, t, r, b);
13527 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013528 onLayout(changed, l, t, r, b);
13529 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070013530
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013531 ListenerInfo li = mListenerInfo;
13532 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070013533 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013534 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070013535 int numListeners = listenersCopy.size();
13536 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070013537 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070013538 }
13539 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013540 }
13541 mPrivateFlags &= ~FORCE_LAYOUT;
13542 }
13543
13544 /**
13545 * Called from layout when this view should
13546 * assign a size and position to each of its children.
13547 *
13548 * Derived classes with children should override
13549 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070013550 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013551 * @param changed This is a new size or position for this view
13552 * @param left Left position, relative to parent
13553 * @param top Top position, relative to parent
13554 * @param right Right position, relative to parent
13555 * @param bottom Bottom position, relative to parent
13556 */
13557 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
13558 }
13559
13560 /**
13561 * Assign a size and position to this view.
13562 *
13563 * This is called from layout.
13564 *
13565 * @param left Left position, relative to parent
13566 * @param top Top position, relative to parent
13567 * @param right Right position, relative to parent
13568 * @param bottom Bottom position, relative to parent
13569 * @return true if the new size and position are different than the
13570 * previous ones
13571 * {@hide}
13572 */
13573 protected boolean setFrame(int left, int top, int right, int bottom) {
13574 boolean changed = false;
13575
13576 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070013577 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013578 + right + "," + bottom + ")");
13579 }
13580
13581 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
13582 changed = true;
13583
13584 // Remember our drawn bit
13585 int drawn = mPrivateFlags & DRAWN;
13586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013587 int oldWidth = mRight - mLeft;
13588 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070013589 int newWidth = right - left;
13590 int newHeight = bottom - top;
13591 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
13592
13593 // Invalidate our old position
13594 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013595
13596 mLeft = left;
13597 mTop = top;
13598 mRight = right;
13599 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -070013600 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013601 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
13602 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013603
13604 mPrivateFlags |= HAS_BOUNDS;
13605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013606
Chet Haase75755e22011-07-18 17:48:25 -070013607 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013608 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
13609 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013610 if (mTransformationInfo != null) {
13611 mTransformationInfo.mMatrixDirty = true;
13612 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013613 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013614 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
13615 }
13616
13617 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
13618 // If we are visible, force the DRAWN bit to on so that
13619 // this invalidate will go through (at least to our parent).
13620 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013621 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013622 // the DRAWN bit.
13623 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070013624 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080013625 // parent display list may need to be recreated based on a change in the bounds
13626 // of any child
13627 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013628 }
13629
13630 // Reset drawn bit to original value (invalidate turns it off)
13631 mPrivateFlags |= drawn;
13632
13633 mBackgroundSizeChanged = true;
13634 }
13635 return changed;
13636 }
13637
13638 /**
13639 * Finalize inflating a view from XML. This is called as the last phase
13640 * of inflation, after all child views have been added.
13641 *
13642 * <p>Even if the subclass overrides onFinishInflate, they should always be
13643 * sure to call the super method, so that we get called.
13644 */
13645 protected void onFinishInflate() {
13646 }
13647
13648 /**
13649 * Returns the resources associated with this view.
13650 *
13651 * @return Resources object.
13652 */
13653 public Resources getResources() {
13654 return mResources;
13655 }
13656
13657 /**
13658 * Invalidates the specified Drawable.
13659 *
13660 * @param drawable the drawable to invalidate
13661 */
13662 public void invalidateDrawable(Drawable drawable) {
13663 if (verifyDrawable(drawable)) {
13664 final Rect dirty = drawable.getBounds();
13665 final int scrollX = mScrollX;
13666 final int scrollY = mScrollY;
13667
13668 invalidate(dirty.left + scrollX, dirty.top + scrollY,
13669 dirty.right + scrollX, dirty.bottom + scrollY);
13670 }
13671 }
13672
13673 /**
13674 * Schedules an action on a drawable to occur at a specified time.
13675 *
13676 * @param who the recipient of the action
13677 * @param what the action to run on the drawable
13678 * @param when the time at which the action must occur. Uses the
13679 * {@link SystemClock#uptimeMillis} timebase.
13680 */
13681 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080013682 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013683 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080013684 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013685 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
13686 Choreographer.CALLBACK_ANIMATION, what, who,
13687 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080013688 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013689 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080013690 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013691 }
13692 }
13693
13694 /**
13695 * Cancels a scheduled action on a drawable.
13696 *
13697 * @param who the recipient of the action
13698 * @param what the action to cancel
13699 */
13700 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080013701 if (verifyDrawable(who) && what != null) {
13702 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013703 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13704 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080013705 } else {
13706 ViewRootImpl.getRunQueue().removeCallbacks(what);
13707 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013708 }
13709 }
13710
13711 /**
13712 * Unschedule any events associated with the given Drawable. This can be
13713 * used when selecting a new Drawable into a view, so that the previous
13714 * one is completely unscheduled.
13715 *
13716 * @param who The Drawable to unschedule.
13717 *
13718 * @see #drawableStateChanged
13719 */
13720 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080013721 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013722 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13723 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013724 }
13725 }
13726
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013727 /**
13728 * Return the layout direction of a given Drawable.
13729 *
13730 * @param who the Drawable to query
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070013731 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013732 */
13733 public int getResolvedLayoutDirection(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013734 return (who == mBackground) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070013735 }
13736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013737 /**
13738 * If your view subclass is displaying its own Drawable objects, it should
13739 * override this function and return true for any Drawable it is
13740 * displaying. This allows animations for those drawables to be
13741 * scheduled.
13742 *
13743 * <p>Be sure to call through to the super class when overriding this
13744 * function.
13745 *
13746 * @param who The Drawable to verify. Return true if it is one you are
13747 * displaying, else return the result of calling through to the
13748 * super class.
13749 *
13750 * @return boolean If true than the Drawable is being displayed in the
13751 * view; else false and it is not allowed to animate.
13752 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013753 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
13754 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013755 */
13756 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013757 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013758 }
13759
13760 /**
13761 * This function is called whenever the state of the view changes in such
13762 * a way that it impacts the state of drawables being shown.
13763 *
13764 * <p>Be sure to call through to the superclass when overriding this
13765 * function.
13766 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013767 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013768 */
13769 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013770 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013771 if (d != null && d.isStateful()) {
13772 d.setState(getDrawableState());
13773 }
13774 }
13775
13776 /**
13777 * Call this to force a view to update its drawable state. This will cause
13778 * drawableStateChanged to be called on this view. Views that are interested
13779 * in the new state should call getDrawableState.
13780 *
13781 * @see #drawableStateChanged
13782 * @see #getDrawableState
13783 */
13784 public void refreshDrawableState() {
13785 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
13786 drawableStateChanged();
13787
13788 ViewParent parent = mParent;
13789 if (parent != null) {
13790 parent.childDrawableStateChanged(this);
13791 }
13792 }
13793
13794 /**
13795 * Return an array of resource IDs of the drawable states representing the
13796 * current state of the view.
13797 *
13798 * @return The current drawable state
13799 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013800 * @see Drawable#setState(int[])
13801 * @see #drawableStateChanged()
13802 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013803 */
13804 public final int[] getDrawableState() {
13805 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
13806 return mDrawableState;
13807 } else {
13808 mDrawableState = onCreateDrawableState(0);
13809 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
13810 return mDrawableState;
13811 }
13812 }
13813
13814 /**
13815 * Generate the new {@link android.graphics.drawable.Drawable} state for
13816 * this view. This is called by the view
13817 * system when the cached Drawable state is determined to be invalid. To
13818 * retrieve the current state, you should use {@link #getDrawableState}.
13819 *
13820 * @param extraSpace if non-zero, this is the number of extra entries you
13821 * would like in the returned array in which you can place your own
13822 * states.
13823 *
13824 * @return Returns an array holding the current {@link Drawable} state of
13825 * the view.
13826 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013827 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013828 */
13829 protected int[] onCreateDrawableState(int extraSpace) {
13830 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
13831 mParent instanceof View) {
13832 return ((View) mParent).onCreateDrawableState(extraSpace);
13833 }
13834
13835 int[] drawableState;
13836
13837 int privateFlags = mPrivateFlags;
13838
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013839 int viewStateIndex = 0;
13840 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
13841 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
13842 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070013843 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013844 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
13845 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070013846 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
13847 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080013848 // This is set if HW acceleration is requested, even if the current
13849 // process doesn't allow it. This is just to allow app preview
13850 // windows to better match their app.
13851 viewStateIndex |= VIEW_STATE_ACCELERATED;
13852 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070013853 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013854
Christopher Tate3d4bf172011-03-28 16:16:46 -070013855 final int privateFlags2 = mPrivateFlags2;
13856 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
13857 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
13858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013859 drawableState = VIEW_STATE_SETS[viewStateIndex];
13860
13861 //noinspection ConstantIfStatement
13862 if (false) {
13863 Log.i("View", "drawableStateIndex=" + viewStateIndex);
13864 Log.i("View", toString()
13865 + " pressed=" + ((privateFlags & PRESSED) != 0)
13866 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
13867 + " fo=" + hasFocus()
13868 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013869 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013870 + ": " + Arrays.toString(drawableState));
13871 }
13872
13873 if (extraSpace == 0) {
13874 return drawableState;
13875 }
13876
13877 final int[] fullState;
13878 if (drawableState != null) {
13879 fullState = new int[drawableState.length + extraSpace];
13880 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
13881 } else {
13882 fullState = new int[extraSpace];
13883 }
13884
13885 return fullState;
13886 }
13887
13888 /**
13889 * Merge your own state values in <var>additionalState</var> into the base
13890 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070013891 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013892 *
13893 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070013894 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013895 * own additional state values.
13896 *
13897 * @param additionalState The additional state values you would like
13898 * added to <var>baseState</var>; this array is not modified.
13899 *
13900 * @return As a convenience, the <var>baseState</var> array you originally
13901 * passed into the function is returned.
13902 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013903 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013904 */
13905 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
13906 final int N = baseState.length;
13907 int i = N - 1;
13908 while (i >= 0 && baseState[i] == 0) {
13909 i--;
13910 }
13911 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
13912 return baseState;
13913 }
13914
13915 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070013916 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
13917 * on all Drawable objects associated with this view.
13918 */
13919 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013920 if (mBackground != null) {
13921 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070013922 }
13923 }
13924
13925 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013926 * Sets the background color for this view.
13927 * @param color the color of the background
13928 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000013929 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013930 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013931 if (mBackground instanceof ColorDrawable) {
13932 ((ColorDrawable) mBackground).setColor(color);
Chet Haase70d4ba12010-10-06 09:46:45 -070013933 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070013934 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070013935 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013936 }
13937
13938 /**
13939 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070013940 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013941 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070013942 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013943 * @attr ref android.R.styleable#View_background
13944 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000013945 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013946 public void setBackgroundResource(int resid) {
13947 if (resid != 0 && resid == mBackgroundResource) {
13948 return;
13949 }
13950
13951 Drawable d= null;
13952 if (resid != 0) {
13953 d = mResources.getDrawable(resid);
13954 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013955 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013956
13957 mBackgroundResource = resid;
13958 }
13959
13960 /**
13961 * Set the background to a given Drawable, or remove the background. If the
13962 * background has padding, this View's padding is set to the background's
13963 * padding. However, when a background is removed, this View's padding isn't
13964 * touched. If setting the padding is desired, please use
13965 * {@link #setPadding(int, int, int, int)}.
13966 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013967 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013968 * background
13969 */
Philip Milne6c8ea062012-04-03 17:38:43 -070013970 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070013971 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070013972 setBackgroundDrawable(background);
13973 }
13974
13975 /**
13976 * @deprecated use {@link #setBackground(Drawable)} instead
13977 */
13978 @Deprecated
13979 public void setBackgroundDrawable(Drawable background) {
13980 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070013981 return;
13982 }
13983
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013984 boolean requestLayout = false;
13985
13986 mBackgroundResource = 0;
13987
13988 /*
13989 * Regardless of whether we're setting a new background or not, we want
13990 * to clear the previous drawable.
13991 */
Philip Milne6c8ea062012-04-03 17:38:43 -070013992 if (mBackground != null) {
13993 mBackground.setCallback(null);
13994 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013995 }
13996
Philip Milne6c8ea062012-04-03 17:38:43 -070013997 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013998 Rect padding = sThreadLocal.get();
13999 if (padding == null) {
14000 padding = new Rect();
14001 sThreadLocal.set(padding);
14002 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014003 if (background.getPadding(padding)) {
14004 switch (background.getResolvedLayoutDirectionSelf()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014005 case LAYOUT_DIRECTION_RTL:
14006 setPadding(padding.right, padding.top, padding.left, padding.bottom);
14007 break;
14008 case LAYOUT_DIRECTION_LTR:
14009 default:
14010 setPadding(padding.left, padding.top, padding.right, padding.bottom);
14011 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014012 }
14013
14014 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
14015 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070014016 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
14017 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014018 requestLayout = true;
14019 }
14020
Philip Milne6c8ea062012-04-03 17:38:43 -070014021 background.setCallback(this);
14022 if (background.isStateful()) {
14023 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014024 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014025 background.setVisible(getVisibility() == VISIBLE, false);
14026 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014027
14028 if ((mPrivateFlags & SKIP_DRAW) != 0) {
14029 mPrivateFlags &= ~SKIP_DRAW;
14030 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
14031 requestLayout = true;
14032 }
14033 } else {
14034 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070014035 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014036
14037 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
14038 /*
14039 * This view ONLY drew the background before and we're removing
14040 * the background, so now it won't draw anything
14041 * (hence we SKIP_DRAW)
14042 */
14043 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
14044 mPrivateFlags |= SKIP_DRAW;
14045 }
14046
14047 /*
14048 * When the background is set, we try to apply its padding to this
14049 * View. When the background is removed, we don't touch this View's
14050 * padding. This is noted in the Javadocs. Hence, we don't need to
14051 * requestLayout(), the invalidate() below is sufficient.
14052 */
14053
14054 // The old background's minimum size could have affected this
14055 // View's layout, so let's requestLayout
14056 requestLayout = true;
14057 }
14058
Romain Guy8f1344f52009-05-15 16:03:59 -070014059 computeOpaqueFlags();
14060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014061 if (requestLayout) {
14062 requestLayout();
14063 }
14064
14065 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014066 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014067 }
14068
14069 /**
14070 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070014071 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014072 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070014073 *
14074 * @see #setBackground(Drawable)
14075 *
14076 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014077 */
14078 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014079 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014080 }
14081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014082 /**
14083 * Sets the padding. The view may add on the space required to display
14084 * the scrollbars, depending on the style and visibility of the scrollbars.
14085 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
14086 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
14087 * from the values set in this call.
14088 *
14089 * @attr ref android.R.styleable#View_padding
14090 * @attr ref android.R.styleable#View_paddingBottom
14091 * @attr ref android.R.styleable#View_paddingLeft
14092 * @attr ref android.R.styleable#View_paddingRight
14093 * @attr ref android.R.styleable#View_paddingTop
14094 * @param left the left padding in pixels
14095 * @param top the top padding in pixels
14096 * @param right the right padding in pixels
14097 * @param bottom the bottom padding in pixels
14098 */
14099 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014100 mUserPaddingStart = -1;
14101 mUserPaddingEnd = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014102 mUserPaddingRelative = false;
14103
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014104 internalSetPadding(left, top, right, bottom);
14105 }
14106
14107 private void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080014108 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014109 mUserPaddingRight = right;
14110 mUserPaddingBottom = bottom;
14111
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014112 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014113 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070014114
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014115 // Common case is there are no scroll bars.
14116 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014117 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080014118 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014119 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080014120 switch (mVerticalScrollbarPosition) {
14121 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014122 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
14123 left += offset;
14124 } else {
14125 right += offset;
14126 }
14127 break;
Adam Powell20232d02010-12-08 21:08:53 -080014128 case SCROLLBAR_POSITION_RIGHT:
14129 right += offset;
14130 break;
14131 case SCROLLBAR_POSITION_LEFT:
14132 left += offset;
14133 break;
14134 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014135 }
Adam Powell20232d02010-12-08 21:08:53 -080014136 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014137 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
14138 ? 0 : getHorizontalScrollbarHeight();
14139 }
14140 }
Romain Guy8506ab42009-06-11 17:35:47 -070014141
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014142 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014143 changed = true;
14144 mPaddingLeft = left;
14145 }
14146 if (mPaddingTop != top) {
14147 changed = true;
14148 mPaddingTop = top;
14149 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014150 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014151 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014152 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014153 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014154 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014155 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014156 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014157 }
14158
14159 if (changed) {
14160 requestLayout();
14161 }
14162 }
14163
14164 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014165 * Sets the relative padding. The view may add on the space required to display
14166 * the scrollbars, depending on the style and visibility of the scrollbars.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014167 * from the values set in this call.
14168 *
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014169 * @param start the start padding in pixels
14170 * @param top the top padding in pixels
14171 * @param end the end padding in pixels
14172 * @param bottom the bottom padding in pixels
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014173 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014174 */
14175 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014176 mUserPaddingStart = start;
14177 mUserPaddingEnd = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014178 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014179
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014180 switch(getResolvedLayoutDirection()) {
14181 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014182 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014183 break;
14184 case LAYOUT_DIRECTION_LTR:
14185 default:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014186 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014187 }
14188 }
14189
14190 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014191 * Returns the top padding of this view.
14192 *
14193 * @return the top padding in pixels
14194 */
14195 public int getPaddingTop() {
14196 return mPaddingTop;
14197 }
14198
14199 /**
14200 * Returns the bottom padding of this view. If there are inset and enabled
14201 * scrollbars, this value may include the space required to display the
14202 * scrollbars as well.
14203 *
14204 * @return the bottom padding in pixels
14205 */
14206 public int getPaddingBottom() {
14207 return mPaddingBottom;
14208 }
14209
14210 /**
14211 * Returns the left padding of this view. If there are inset and enabled
14212 * scrollbars, this value may include the space required to display the
14213 * scrollbars as well.
14214 *
14215 * @return the left padding in pixels
14216 */
14217 public int getPaddingLeft() {
14218 return mPaddingLeft;
14219 }
14220
14221 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014222 * Returns the start padding of this view depending on its resolved layout direction.
14223 * If there are inset and enabled scrollbars, this value may include the space
14224 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014225 *
14226 * @return the start padding in pixels
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014227 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014228 */
14229 public int getPaddingStart() {
14230 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14231 mPaddingRight : mPaddingLeft;
14232 }
14233
14234 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014235 * Returns the right padding of this view. If there are inset and enabled
14236 * scrollbars, this value may include the space required to display the
14237 * scrollbars as well.
14238 *
14239 * @return the right padding in pixels
14240 */
14241 public int getPaddingRight() {
14242 return mPaddingRight;
14243 }
14244
14245 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014246 * Returns the end padding of this view depending on its resolved layout direction.
14247 * If there are inset and enabled scrollbars, this value may include the space
14248 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014249 *
14250 * @return the end padding in pixels
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014251 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014252 */
14253 public int getPaddingEnd() {
14254 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14255 mPaddingLeft : mPaddingRight;
14256 }
14257
14258 /**
14259 * Return if the padding as been set thru relative values
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014260 * {@link #setPaddingRelative(int, int, int, int)}
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014261 *
14262 * @return true if the padding is relative or false if it is not.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014263 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014264 */
14265 public boolean isPaddingRelative() {
14266 return mUserPaddingRelative;
14267 }
14268
14269 /**
Philip Milne1557fd72012-04-04 23:41:34 -070014270 * @hide
14271 */
Philip Milne7a23b492012-04-24 22:12:36 -070014272 public Insets getOpticalInsets() {
Philip Milne1557fd72012-04-04 23:41:34 -070014273 if (mLayoutInsets == null) {
Philip Milnebbd51f12012-04-18 15:09:05 -070014274 mLayoutInsets = (mBackground == null) ? Insets.NONE : mBackground.getLayoutInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070014275 }
14276 return mLayoutInsets;
14277 }
14278
14279 /**
14280 * @hide
14281 */
14282 public void setLayoutInsets(Insets layoutInsets) {
14283 mLayoutInsets = layoutInsets;
14284 }
14285
14286 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014287 * Changes the selection state of this view. A view can be selected or not.
14288 * Note that selection is not the same as focus. Views are typically
14289 * selected in the context of an AdapterView like ListView or GridView;
14290 * the selected view is the view that is highlighted.
14291 *
14292 * @param selected true if the view must be selected, false otherwise
14293 */
14294 public void setSelected(boolean selected) {
14295 if (((mPrivateFlags & SELECTED) != 0) != selected) {
14296 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070014297 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080014298 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014299 refreshDrawableState();
14300 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070014301 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
14302 notifyAccessibilityStateChanged();
14303 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014304 }
14305 }
14306
14307 /**
14308 * Dispatch setSelected to all of this View's children.
14309 *
14310 * @see #setSelected(boolean)
14311 *
14312 * @param selected The new selected state
14313 */
14314 protected void dispatchSetSelected(boolean selected) {
14315 }
14316
14317 /**
14318 * Indicates the selection state of this view.
14319 *
14320 * @return true if the view is selected, false otherwise
14321 */
14322 @ViewDebug.ExportedProperty
14323 public boolean isSelected() {
14324 return (mPrivateFlags & SELECTED) != 0;
14325 }
14326
14327 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014328 * Changes the activated state of this view. A view can be activated or not.
14329 * Note that activation is not the same as selection. Selection is
14330 * a transient property, representing the view (hierarchy) the user is
14331 * currently interacting with. Activation is a longer-term state that the
14332 * user can move views in and out of. For example, in a list view with
14333 * single or multiple selection enabled, the views in the current selection
14334 * set are activated. (Um, yeah, we are deeply sorry about the terminology
14335 * here.) The activated state is propagated down to children of the view it
14336 * is set on.
14337 *
14338 * @param activated true if the view must be activated, false otherwise
14339 */
14340 public void setActivated(boolean activated) {
14341 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
14342 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014343 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014344 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070014345 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014346 }
14347 }
14348
14349 /**
14350 * Dispatch setActivated to all of this View's children.
14351 *
14352 * @see #setActivated(boolean)
14353 *
14354 * @param activated The new activated state
14355 */
14356 protected void dispatchSetActivated(boolean activated) {
14357 }
14358
14359 /**
14360 * Indicates the activation state of this view.
14361 *
14362 * @return true if the view is activated, false otherwise
14363 */
14364 @ViewDebug.ExportedProperty
14365 public boolean isActivated() {
14366 return (mPrivateFlags & ACTIVATED) != 0;
14367 }
14368
14369 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014370 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
14371 * observer can be used to get notifications when global events, like
14372 * layout, happen.
14373 *
14374 * The returned ViewTreeObserver observer is not guaranteed to remain
14375 * valid for the lifetime of this View. If the caller of this method keeps
14376 * a long-lived reference to ViewTreeObserver, it should always check for
14377 * the return value of {@link ViewTreeObserver#isAlive()}.
14378 *
14379 * @return The ViewTreeObserver for this view's hierarchy.
14380 */
14381 public ViewTreeObserver getViewTreeObserver() {
14382 if (mAttachInfo != null) {
14383 return mAttachInfo.mTreeObserver;
14384 }
14385 if (mFloatingTreeObserver == null) {
14386 mFloatingTreeObserver = new ViewTreeObserver();
14387 }
14388 return mFloatingTreeObserver;
14389 }
14390
14391 /**
14392 * <p>Finds the topmost view in the current view hierarchy.</p>
14393 *
14394 * @return the topmost view containing this view
14395 */
14396 public View getRootView() {
14397 if (mAttachInfo != null) {
14398 final View v = mAttachInfo.mRootView;
14399 if (v != null) {
14400 return v;
14401 }
14402 }
Romain Guy8506ab42009-06-11 17:35:47 -070014403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014404 View parent = this;
14405
14406 while (parent.mParent != null && parent.mParent instanceof View) {
14407 parent = (View) parent.mParent;
14408 }
14409
14410 return parent;
14411 }
14412
14413 /**
14414 * <p>Computes the coordinates of this view on the screen. The argument
14415 * must be an array of two integers. After the method returns, the array
14416 * contains the x and y location in that order.</p>
14417 *
14418 * @param location an array of two integers in which to hold the coordinates
14419 */
14420 public void getLocationOnScreen(int[] location) {
14421 getLocationInWindow(location);
14422
14423 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070014424 if (info != null) {
14425 location[0] += info.mWindowLeft;
14426 location[1] += info.mWindowTop;
14427 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014428 }
14429
14430 /**
14431 * <p>Computes the coordinates of this view in its window. The argument
14432 * must be an array of two integers. After the method returns, the array
14433 * contains the x and y location in that order.</p>
14434 *
14435 * @param location an array of two integers in which to hold the coordinates
14436 */
14437 public void getLocationInWindow(int[] location) {
14438 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014439 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014440 }
14441
Gilles Debunne6583ce52011-12-06 18:09:02 -080014442 if (mAttachInfo == null) {
14443 // When the view is not attached to a window, this method does not make sense
14444 location[0] = location[1] = 0;
14445 return;
14446 }
14447
Gilles Debunnecea45132011-11-24 02:19:27 +010014448 float[] position = mAttachInfo.mTmpTransformLocation;
14449 position[0] = position[1] = 0.0f;
14450
14451 if (!hasIdentityMatrix()) {
14452 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014453 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014454
Gilles Debunnecea45132011-11-24 02:19:27 +010014455 position[0] += mLeft;
14456 position[1] += mTop;
14457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014458 ViewParent viewParent = mParent;
14459 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014460 final View view = (View) viewParent;
14461
14462 position[0] -= view.mScrollX;
14463 position[1] -= view.mScrollY;
14464
14465 if (!view.hasIdentityMatrix()) {
14466 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014467 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014468
14469 position[0] += view.mLeft;
14470 position[1] += view.mTop;
14471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014472 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070014473 }
Romain Guy8506ab42009-06-11 17:35:47 -070014474
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014475 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014476 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010014477 final ViewRootImpl vr = (ViewRootImpl) viewParent;
14478 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014479 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014480
14481 location[0] = (int) (position[0] + 0.5f);
14482 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014483 }
14484
14485 /**
14486 * {@hide}
14487 * @param id the id of the view to be found
14488 * @return the view of the specified id, null if cannot be found
14489 */
14490 protected View findViewTraversal(int id) {
14491 if (id == mID) {
14492 return this;
14493 }
14494 return null;
14495 }
14496
14497 /**
14498 * {@hide}
14499 * @param tag the tag of the view to be found
14500 * @return the view of specified tag, null if cannot be found
14501 */
14502 protected View findViewWithTagTraversal(Object tag) {
14503 if (tag != null && tag.equals(mTag)) {
14504 return this;
14505 }
14506 return null;
14507 }
14508
14509 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014510 * {@hide}
14511 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070014512 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080014513 * @return The first view that matches the predicate or null.
14514 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070014515 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080014516 if (predicate.apply(this)) {
14517 return this;
14518 }
14519 return null;
14520 }
14521
14522 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014523 * Look for a child view with the given id. If this view has the given
14524 * id, return this view.
14525 *
14526 * @param id The id to search for.
14527 * @return The view that has the given id in the hierarchy or null
14528 */
14529 public final View findViewById(int id) {
14530 if (id < 0) {
14531 return null;
14532 }
14533 return findViewTraversal(id);
14534 }
14535
14536 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070014537 * Finds a view by its unuque and stable accessibility id.
14538 *
14539 * @param accessibilityId The searched accessibility id.
14540 * @return The found view.
14541 */
14542 final View findViewByAccessibilityId(int accessibilityId) {
14543 if (accessibilityId < 0) {
14544 return null;
14545 }
14546 return findViewByAccessibilityIdTraversal(accessibilityId);
14547 }
14548
14549 /**
14550 * Performs the traversal to find a view by its unuque and stable accessibility id.
14551 *
14552 * <strong>Note:</strong>This method does not stop at the root namespace
14553 * boundary since the user can touch the screen at an arbitrary location
14554 * potentially crossing the root namespace bounday which will send an
14555 * accessibility event to accessibility services and they should be able
14556 * to obtain the event source. Also accessibility ids are guaranteed to be
14557 * unique in the window.
14558 *
14559 * @param accessibilityId The accessibility id.
14560 * @return The found view.
14561 */
14562 View findViewByAccessibilityIdTraversal(int accessibilityId) {
14563 if (getAccessibilityViewId() == accessibilityId) {
14564 return this;
14565 }
14566 return null;
14567 }
14568
14569 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014570 * Look for a child view with the given tag. If this view has the given
14571 * tag, return this view.
14572 *
14573 * @param tag The tag to search for, using "tag.equals(getTag())".
14574 * @return The View that has the given tag in the hierarchy or null
14575 */
14576 public final View findViewWithTag(Object tag) {
14577 if (tag == null) {
14578 return null;
14579 }
14580 return findViewWithTagTraversal(tag);
14581 }
14582
14583 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014584 * {@hide}
14585 * Look for a child view that matches the specified predicate.
14586 * If this view matches the predicate, return this view.
14587 *
14588 * @param predicate The predicate to evaluate.
14589 * @return The first view that matches the predicate or null.
14590 */
14591 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070014592 return findViewByPredicateTraversal(predicate, null);
14593 }
14594
14595 /**
14596 * {@hide}
14597 * Look for a child view that matches the specified predicate,
14598 * starting with the specified view and its descendents and then
14599 * recusively searching the ancestors and siblings of that view
14600 * until this view is reached.
14601 *
14602 * This method is useful in cases where the predicate does not match
14603 * a single unique view (perhaps multiple views use the same id)
14604 * and we are trying to find the view that is "closest" in scope to the
14605 * starting view.
14606 *
14607 * @param start The view to start from.
14608 * @param predicate The predicate to evaluate.
14609 * @return The first view that matches the predicate or null.
14610 */
14611 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
14612 View childToSkip = null;
14613 for (;;) {
14614 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
14615 if (view != null || start == this) {
14616 return view;
14617 }
14618
14619 ViewParent parent = start.getParent();
14620 if (parent == null || !(parent instanceof View)) {
14621 return null;
14622 }
14623
14624 childToSkip = start;
14625 start = (View) parent;
14626 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080014627 }
14628
14629 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014630 * Sets the identifier for this view. The identifier does not have to be
14631 * unique in this view's hierarchy. The identifier should be a positive
14632 * number.
14633 *
14634 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070014635 * @see #getId()
14636 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014637 *
14638 * @param id a number used to identify the view
14639 *
14640 * @attr ref android.R.styleable#View_id
14641 */
14642 public void setId(int id) {
14643 mID = id;
14644 }
14645
14646 /**
14647 * {@hide}
14648 *
14649 * @param isRoot true if the view belongs to the root namespace, false
14650 * otherwise
14651 */
14652 public void setIsRootNamespace(boolean isRoot) {
14653 if (isRoot) {
14654 mPrivateFlags |= IS_ROOT_NAMESPACE;
14655 } else {
14656 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
14657 }
14658 }
14659
14660 /**
14661 * {@hide}
14662 *
14663 * @return true if the view belongs to the root namespace, false otherwise
14664 */
14665 public boolean isRootNamespace() {
14666 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
14667 }
14668
14669 /**
14670 * Returns this view's identifier.
14671 *
14672 * @return a positive integer used to identify the view or {@link #NO_ID}
14673 * if the view has no ID
14674 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014675 * @see #setId(int)
14676 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014677 * @attr ref android.R.styleable#View_id
14678 */
14679 @ViewDebug.CapturedViewProperty
14680 public int getId() {
14681 return mID;
14682 }
14683
14684 /**
14685 * Returns this view's tag.
14686 *
14687 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070014688 *
14689 * @see #setTag(Object)
14690 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014691 */
14692 @ViewDebug.ExportedProperty
14693 public Object getTag() {
14694 return mTag;
14695 }
14696
14697 /**
14698 * Sets the tag associated with this view. A tag can be used to mark
14699 * a view in its hierarchy and does not have to be unique within the
14700 * hierarchy. Tags can also be used to store data within a view without
14701 * resorting to another data structure.
14702 *
14703 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070014704 *
14705 * @see #getTag()
14706 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014707 */
14708 public void setTag(final Object tag) {
14709 mTag = tag;
14710 }
14711
14712 /**
Romain Guyd90a3312009-05-06 14:54:28 -070014713 * Returns the tag associated with this view and the specified key.
14714 *
14715 * @param key The key identifying the tag
14716 *
14717 * @return the Object stored in this view as a tag
14718 *
14719 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070014720 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070014721 */
14722 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014723 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070014724 return null;
14725 }
14726
14727 /**
14728 * Sets a tag associated with this view and a key. A tag can be used
14729 * to mark a view in its hierarchy and does not have to be unique within
14730 * the hierarchy. Tags can also be used to store data within a view
14731 * without resorting to another data structure.
14732 *
14733 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070014734 * application to ensure it is unique (see the <a
14735 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
14736 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070014737 * the Android framework or not associated with any package will cause
14738 * an {@link IllegalArgumentException} to be thrown.
14739 *
14740 * @param key The key identifying the tag
14741 * @param tag An Object to tag the view with
14742 *
14743 * @throws IllegalArgumentException If they specified key is not valid
14744 *
14745 * @see #setTag(Object)
14746 * @see #getTag(int)
14747 */
14748 public void setTag(int key, final Object tag) {
14749 // If the package id is 0x00 or 0x01, it's either an undefined package
14750 // or a framework id
14751 if ((key >>> 24) < 2) {
14752 throw new IllegalArgumentException("The key must be an application-specific "
14753 + "resource id.");
14754 }
14755
Adam Powell2b2f6d62011-09-23 11:15:39 -070014756 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014757 }
14758
14759 /**
14760 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
14761 * framework id.
14762 *
14763 * @hide
14764 */
14765 public void setTagInternal(int key, Object tag) {
14766 if ((key >>> 24) != 0x1) {
14767 throw new IllegalArgumentException("The key must be a framework-specific "
14768 + "resource id.");
14769 }
14770
Adam Powell2b2f6d62011-09-23 11:15:39 -070014771 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014772 }
14773
Adam Powell2b2f6d62011-09-23 11:15:39 -070014774 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014775 if (mKeyedTags == null) {
14776 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070014777 }
14778
Adam Powell7db82ac2011-09-22 19:44:04 -070014779 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014780 }
14781
14782 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014783 * Prints information about this view in the log output, with the tag
14784 * {@link #VIEW_LOG_TAG}.
14785 *
14786 * @hide
14787 */
14788 public void debug() {
14789 debug(0);
14790 }
14791
14792 /**
14793 * Prints information about this view in the log output, with the tag
14794 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
14795 * indentation defined by the <code>depth</code>.
14796 *
14797 * @param depth the indentation level
14798 *
14799 * @hide
14800 */
14801 protected void debug(int depth) {
14802 String output = debugIndent(depth - 1);
14803
14804 output += "+ " + this;
14805 int id = getId();
14806 if (id != -1) {
14807 output += " (id=" + id + ")";
14808 }
14809 Object tag = getTag();
14810 if (tag != null) {
14811 output += " (tag=" + tag + ")";
14812 }
14813 Log.d(VIEW_LOG_TAG, output);
14814
14815 if ((mPrivateFlags & FOCUSED) != 0) {
14816 output = debugIndent(depth) + " FOCUSED";
14817 Log.d(VIEW_LOG_TAG, output);
14818 }
14819
14820 output = debugIndent(depth);
14821 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
14822 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
14823 + "} ";
14824 Log.d(VIEW_LOG_TAG, output);
14825
14826 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
14827 || mPaddingBottom != 0) {
14828 output = debugIndent(depth);
14829 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
14830 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
14831 Log.d(VIEW_LOG_TAG, output);
14832 }
14833
14834 output = debugIndent(depth);
14835 output += "mMeasureWidth=" + mMeasuredWidth +
14836 " mMeasureHeight=" + mMeasuredHeight;
14837 Log.d(VIEW_LOG_TAG, output);
14838
14839 output = debugIndent(depth);
14840 if (mLayoutParams == null) {
14841 output += "BAD! no layout params";
14842 } else {
14843 output = mLayoutParams.debug(output);
14844 }
14845 Log.d(VIEW_LOG_TAG, output);
14846
14847 output = debugIndent(depth);
14848 output += "flags={";
14849 output += View.printFlags(mViewFlags);
14850 output += "}";
14851 Log.d(VIEW_LOG_TAG, output);
14852
14853 output = debugIndent(depth);
14854 output += "privateFlags={";
14855 output += View.printPrivateFlags(mPrivateFlags);
14856 output += "}";
14857 Log.d(VIEW_LOG_TAG, output);
14858 }
14859
14860 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090014861 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014862 *
14863 * @param depth the indentation level
14864 * @return a String containing (depth * 2 + 3) * 2 white spaces
14865 *
14866 * @hide
14867 */
14868 protected static String debugIndent(int depth) {
14869 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
14870 for (int i = 0; i < (depth * 2) + 3; i++) {
14871 spaces.append(' ').append(' ');
14872 }
14873 return spaces.toString();
14874 }
14875
14876 /**
14877 * <p>Return the offset of the widget's text baseline from the widget's top
14878 * boundary. If this widget does not support baseline alignment, this
14879 * method returns -1. </p>
14880 *
14881 * @return the offset of the baseline within the widget's bounds or -1
14882 * if baseline alignment is not supported
14883 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070014884 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014885 public int getBaseline() {
14886 return -1;
14887 }
14888
14889 /**
14890 * Call this when something has changed which has invalidated the
14891 * layout of this view. This will schedule a layout pass of the view
14892 * tree.
14893 */
14894 public void requestLayout() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014895 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080014896 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014897
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070014898 if (mLayoutParams != null) {
14899 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
14900 }
14901
14902 if (mParent != null && !mParent.isLayoutRequested()) {
14903 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014904 }
14905 }
14906
14907 /**
14908 * Forces this view to be laid out during the next layout pass.
14909 * This method does not call requestLayout() or forceLayout()
14910 * on the parent.
14911 */
14912 public void forceLayout() {
14913 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080014914 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014915 }
14916
14917 /**
14918 * <p>
14919 * This is called to find out how big a view should be. The parent
14920 * supplies constraint information in the width and height parameters.
14921 * </p>
14922 *
14923 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080014924 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014925 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080014926 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014927 * </p>
14928 *
14929 *
14930 * @param widthMeasureSpec Horizontal space requirements as imposed by the
14931 * parent
14932 * @param heightMeasureSpec Vertical space requirements as imposed by the
14933 * parent
14934 *
14935 * @see #onMeasure(int, int)
14936 */
14937 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
14938 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
14939 widthMeasureSpec != mOldWidthMeasureSpec ||
14940 heightMeasureSpec != mOldHeightMeasureSpec) {
14941
14942 // first clears the measured dimension flag
14943 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
14944
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014945 // measure ourselves, this should set the measured dimension flag back
14946 onMeasure(widthMeasureSpec, heightMeasureSpec);
14947
14948 // flag not set, setMeasuredDimension() was not invoked, we raise
14949 // an exception to warn the developer
14950 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
14951 throw new IllegalStateException("onMeasure() did not set the"
14952 + " measured dimension by calling"
14953 + " setMeasuredDimension()");
14954 }
14955
14956 mPrivateFlags |= LAYOUT_REQUIRED;
14957 }
14958
14959 mOldWidthMeasureSpec = widthMeasureSpec;
14960 mOldHeightMeasureSpec = heightMeasureSpec;
14961 }
14962
14963 /**
14964 * <p>
14965 * Measure the view and its content to determine the measured width and the
14966 * measured height. This method is invoked by {@link #measure(int, int)} and
14967 * should be overriden by subclasses to provide accurate and efficient
14968 * measurement of their contents.
14969 * </p>
14970 *
14971 * <p>
14972 * <strong>CONTRACT:</strong> When overriding this method, you
14973 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
14974 * measured width and height of this view. Failure to do so will trigger an
14975 * <code>IllegalStateException</code>, thrown by
14976 * {@link #measure(int, int)}. Calling the superclass'
14977 * {@link #onMeasure(int, int)} is a valid use.
14978 * </p>
14979 *
14980 * <p>
14981 * The base class implementation of measure defaults to the background size,
14982 * unless a larger size is allowed by the MeasureSpec. Subclasses should
14983 * override {@link #onMeasure(int, int)} to provide better measurements of
14984 * their content.
14985 * </p>
14986 *
14987 * <p>
14988 * If this method is overridden, it is the subclass's responsibility to make
14989 * sure the measured height and width are at least the view's minimum height
14990 * and width ({@link #getSuggestedMinimumHeight()} and
14991 * {@link #getSuggestedMinimumWidth()}).
14992 * </p>
14993 *
14994 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
14995 * The requirements are encoded with
14996 * {@link android.view.View.MeasureSpec}.
14997 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
14998 * The requirements are encoded with
14999 * {@link android.view.View.MeasureSpec}.
15000 *
15001 * @see #getMeasuredWidth()
15002 * @see #getMeasuredHeight()
15003 * @see #setMeasuredDimension(int, int)
15004 * @see #getSuggestedMinimumHeight()
15005 * @see #getSuggestedMinimumWidth()
15006 * @see android.view.View.MeasureSpec#getMode(int)
15007 * @see android.view.View.MeasureSpec#getSize(int)
15008 */
15009 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
15010 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
15011 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
15012 }
15013
15014 /**
15015 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
15016 * measured width and measured height. Failing to do so will trigger an
15017 * exception at measurement time.</p>
15018 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080015019 * @param measuredWidth The measured width of this view. May be a complex
15020 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15021 * {@link #MEASURED_STATE_TOO_SMALL}.
15022 * @param measuredHeight The measured height of this view. May be a complex
15023 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15024 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015025 */
15026 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
15027 mMeasuredWidth = measuredWidth;
15028 mMeasuredHeight = measuredHeight;
15029
15030 mPrivateFlags |= MEASURED_DIMENSION_SET;
15031 }
15032
15033 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080015034 * Merge two states as returned by {@link #getMeasuredState()}.
15035 * @param curState The current state as returned from a view or the result
15036 * of combining multiple views.
15037 * @param newState The new view state to combine.
15038 * @return Returns a new integer reflecting the combination of the two
15039 * states.
15040 */
15041 public static int combineMeasuredStates(int curState, int newState) {
15042 return curState | newState;
15043 }
15044
15045 /**
15046 * Version of {@link #resolveSizeAndState(int, int, int)}
15047 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
15048 */
15049 public static int resolveSize(int size, int measureSpec) {
15050 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
15051 }
15052
15053 /**
15054 * Utility to reconcile a desired size and state, with constraints imposed
15055 * by a MeasureSpec. Will take the desired size, unless a different size
15056 * is imposed by the constraints. The returned value is a compound integer,
15057 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
15058 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
15059 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015060 *
15061 * @param size How big the view wants to be
15062 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080015063 * @return Size information bit mask as defined by
15064 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015065 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080015066 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015067 int result = size;
15068 int specMode = MeasureSpec.getMode(measureSpec);
15069 int specSize = MeasureSpec.getSize(measureSpec);
15070 switch (specMode) {
15071 case MeasureSpec.UNSPECIFIED:
15072 result = size;
15073 break;
15074 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080015075 if (specSize < size) {
15076 result = specSize | MEASURED_STATE_TOO_SMALL;
15077 } else {
15078 result = size;
15079 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015080 break;
15081 case MeasureSpec.EXACTLY:
15082 result = specSize;
15083 break;
15084 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080015085 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015086 }
15087
15088 /**
15089 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070015090 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015091 * by the MeasureSpec.
15092 *
15093 * @param size Default size for this view
15094 * @param measureSpec Constraints imposed by the parent
15095 * @return The size this view should be.
15096 */
15097 public static int getDefaultSize(int size, int measureSpec) {
15098 int result = size;
15099 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070015100 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015101
15102 switch (specMode) {
15103 case MeasureSpec.UNSPECIFIED:
15104 result = size;
15105 break;
15106 case MeasureSpec.AT_MOST:
15107 case MeasureSpec.EXACTLY:
15108 result = specSize;
15109 break;
15110 }
15111 return result;
15112 }
15113
15114 /**
15115 * Returns the suggested minimum height that the view should use. This
15116 * returns the maximum of the view's minimum height
15117 * and the background's minimum height
15118 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
15119 * <p>
15120 * When being used in {@link #onMeasure(int, int)}, the caller should still
15121 * ensure the returned height is within the requirements of the parent.
15122 *
15123 * @return The suggested minimum height of the view.
15124 */
15125 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015126 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015128 }
15129
15130 /**
15131 * Returns the suggested minimum width that the view should use. This
15132 * returns the maximum of the view's minimum width)
15133 * and the background's minimum width
15134 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
15135 * <p>
15136 * When being used in {@link #onMeasure(int, int)}, the caller should still
15137 * ensure the returned width is within the requirements of the parent.
15138 *
15139 * @return The suggested minimum width of the view.
15140 */
15141 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015142 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
15143 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015144
Philip Milne6c8ea062012-04-03 17:38:43 -070015145 /**
15146 * Returns the minimum height of the view.
15147 *
15148 * @return the minimum height the view will try to be.
15149 *
15150 * @see #setMinimumHeight(int)
15151 *
15152 * @attr ref android.R.styleable#View_minHeight
15153 */
15154 public int getMinimumHeight() {
15155 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015156 }
15157
15158 /**
15159 * Sets the minimum height of the view. It is not guaranteed the view will
15160 * be able to achieve this minimum height (for example, if its parent layout
15161 * constrains it with less available height).
15162 *
15163 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015164 *
15165 * @see #getMinimumHeight()
15166 *
15167 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015168 */
15169 public void setMinimumHeight(int minHeight) {
15170 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070015171 requestLayout();
15172 }
15173
15174 /**
15175 * Returns the minimum width of the view.
15176 *
15177 * @return the minimum width the view will try to be.
15178 *
15179 * @see #setMinimumWidth(int)
15180 *
15181 * @attr ref android.R.styleable#View_minWidth
15182 */
15183 public int getMinimumWidth() {
15184 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015185 }
15186
15187 /**
15188 * Sets the minimum width of the view. It is not guaranteed the view will
15189 * be able to achieve this minimum width (for example, if its parent layout
15190 * constrains it with less available width).
15191 *
15192 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015193 *
15194 * @see #getMinimumWidth()
15195 *
15196 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015197 */
15198 public void setMinimumWidth(int minWidth) {
15199 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070015200 requestLayout();
15201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015202 }
15203
15204 /**
15205 * Get the animation currently associated with this view.
15206 *
15207 * @return The animation that is currently playing or
15208 * scheduled to play for this view.
15209 */
15210 public Animation getAnimation() {
15211 return mCurrentAnimation;
15212 }
15213
15214 /**
15215 * Start the specified animation now.
15216 *
15217 * @param animation the animation to start now
15218 */
15219 public void startAnimation(Animation animation) {
15220 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
15221 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080015222 invalidateParentCaches();
15223 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015224 }
15225
15226 /**
15227 * Cancels any animations for this view.
15228 */
15229 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080015230 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080015231 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080015232 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015233 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080015234 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015235 }
15236
15237 /**
15238 * Sets the next animation to play for this view.
15239 * If you want the animation to play immediately, use
Chet Haase42428932012-05-11 15:39:07 -070015240 * {@link #startAnimation(android.view.animation.Animation)} instead.
15241 * This method provides allows fine-grained
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015242 * control over the start time and invalidation, but you
15243 * must make sure that 1) the animation has a start time set, and
Chet Haase42428932012-05-11 15:39:07 -070015244 * 2) the view's parent (which controls animations on its children)
15245 * will be invalidated when the animation is supposed to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015246 * start.
15247 *
15248 * @param animation The next animation, or null.
15249 */
15250 public void setAnimation(Animation animation) {
15251 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070015252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015253 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070015254 // If the screen is off assume the animation start time is now instead of
15255 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
15256 // would cause the animation to start when the screen turns back on
15257 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
15258 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
15259 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
15260 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015261 animation.reset();
15262 }
15263 }
15264
15265 /**
15266 * Invoked by a parent ViewGroup to notify the start of the animation
15267 * currently associated with this view. If you override this method,
15268 * always call super.onAnimationStart();
15269 *
15270 * @see #setAnimation(android.view.animation.Animation)
15271 * @see #getAnimation()
15272 */
15273 protected void onAnimationStart() {
15274 mPrivateFlags |= ANIMATION_STARTED;
15275 }
15276
15277 /**
15278 * Invoked by a parent ViewGroup to notify the end of the animation
15279 * currently associated with this view. If you override this method,
15280 * always call super.onAnimationEnd();
15281 *
15282 * @see #setAnimation(android.view.animation.Animation)
15283 * @see #getAnimation()
15284 */
15285 protected void onAnimationEnd() {
15286 mPrivateFlags &= ~ANIMATION_STARTED;
15287 }
15288
15289 /**
15290 * Invoked if there is a Transform that involves alpha. Subclass that can
15291 * draw themselves with the specified alpha should return true, and then
15292 * respect that alpha when their onDraw() is called. If this returns false
15293 * then the view may be redirected to draw into an offscreen buffer to
15294 * fulfill the request, which will look fine, but may be slower than if the
15295 * subclass handles it internally. The default implementation returns false.
15296 *
15297 * @param alpha The alpha (0..255) to apply to the view's drawing
15298 * @return true if the view can draw with the specified alpha.
15299 */
15300 protected boolean onSetAlpha(int alpha) {
15301 return false;
15302 }
15303
15304 /**
15305 * This is used by the RootView to perform an optimization when
15306 * the view hierarchy contains one or several SurfaceView.
15307 * SurfaceView is always considered transparent, but its children are not,
15308 * therefore all View objects remove themselves from the global transparent
15309 * region (passed as a parameter to this function).
15310 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015311 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015312 *
15313 * @return Returns true if the effective visibility of the view at this
15314 * point is opaque, regardless of the transparent region; returns false
15315 * if it is possible for underlying windows to be seen behind the view.
15316 *
15317 * {@hide}
15318 */
15319 public boolean gatherTransparentRegion(Region region) {
15320 final AttachInfo attachInfo = mAttachInfo;
15321 if (region != null && attachInfo != null) {
15322 final int pflags = mPrivateFlags;
15323 if ((pflags & SKIP_DRAW) == 0) {
15324 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
15325 // remove it from the transparent region.
15326 final int[] location = attachInfo.mTransparentLocation;
15327 getLocationInWindow(location);
15328 region.op(location[0], location[1], location[0] + mRight - mLeft,
15329 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Philip Milne6c8ea062012-04-03 17:38:43 -070015330 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015331 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
15332 // exists, so we remove the background drawable's non-transparent
15333 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070015334 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015335 }
15336 }
15337 return true;
15338 }
15339
15340 /**
15341 * Play a sound effect for this view.
15342 *
15343 * <p>The framework will play sound effects for some built in actions, such as
15344 * clicking, but you may wish to play these effects in your widget,
15345 * for instance, for internal navigation.
15346 *
15347 * <p>The sound effect will only be played if sound effects are enabled by the user, and
15348 * {@link #isSoundEffectsEnabled()} is true.
15349 *
15350 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
15351 */
15352 public void playSoundEffect(int soundConstant) {
15353 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
15354 return;
15355 }
15356 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
15357 }
15358
15359 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015360 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015361 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015362 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015363 *
15364 * <p>The framework will provide haptic feedback for some built in actions,
15365 * such as long presses, but you may wish to provide feedback for your
15366 * own widget.
15367 *
15368 * <p>The feedback will only be performed if
15369 * {@link #isHapticFeedbackEnabled()} is true.
15370 *
15371 * @param feedbackConstant One of the constants defined in
15372 * {@link HapticFeedbackConstants}
15373 */
15374 public boolean performHapticFeedback(int feedbackConstant) {
15375 return performHapticFeedback(feedbackConstant, 0);
15376 }
15377
15378 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015379 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015380 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015381 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015382 *
15383 * @param feedbackConstant One of the constants defined in
15384 * {@link HapticFeedbackConstants}
15385 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
15386 */
15387 public boolean performHapticFeedback(int feedbackConstant, int flags) {
15388 if (mAttachInfo == null) {
15389 return false;
15390 }
Romain Guyf607bdc2010-09-10 19:20:06 -070015391 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070015392 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015393 && !isHapticFeedbackEnabled()) {
15394 return false;
15395 }
Romain Guy812ccbe2010-06-01 14:07:24 -070015396 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
15397 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015398 }
15399
15400 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015401 * Request that the visibility of the status bar or other screen/window
15402 * decorations be changed.
15403 *
15404 * <p>This method is used to put the over device UI into temporary modes
15405 * where the user's attention is focused more on the application content,
15406 * by dimming or hiding surrounding system affordances. This is typically
15407 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
15408 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
15409 * to be placed behind the action bar (and with these flags other system
15410 * affordances) so that smooth transitions between hiding and showing them
15411 * can be done.
15412 *
15413 * <p>Two representative examples of the use of system UI visibility is
15414 * implementing a content browsing application (like a magazine reader)
15415 * and a video playing application.
15416 *
15417 * <p>The first code shows a typical implementation of a View in a content
15418 * browsing application. In this implementation, the application goes
15419 * into a content-oriented mode by hiding the status bar and action bar,
15420 * and putting the navigation elements into lights out mode. The user can
15421 * then interact with content while in this mode. Such an application should
15422 * provide an easy way for the user to toggle out of the mode (such as to
15423 * check information in the status bar or access notifications). In the
15424 * implementation here, this is done simply by tapping on the content.
15425 *
15426 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
15427 * content}
15428 *
15429 * <p>This second code sample shows a typical implementation of a View
15430 * in a video playing application. In this situation, while the video is
15431 * playing the application would like to go into a complete full-screen mode,
15432 * to use as much of the display as possible for the video. When in this state
15433 * the user can not interact with the application; the system intercepts
Dianne Hackborncf675782012-05-10 15:07:24 -070015434 * touching on the screen to pop the UI out of full screen mode. See
15435 * {@link #fitSystemWindows(Rect)} for a sample layout that goes with this code.
Dianne Hackborn98014352012-04-05 18:31:41 -070015436 *
15437 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
15438 * content}
15439 *
15440 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15441 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15442 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15443 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015444 */
15445 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040015446 if (visibility != mSystemUiVisibility) {
15447 mSystemUiVisibility = visibility;
15448 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15449 mParent.recomputeViewAttributes(this);
15450 }
Joe Onorato664644d2011-01-23 17:53:23 -080015451 }
15452 }
15453
15454 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015455 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
15456 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15457 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15458 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15459 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015460 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080015461 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080015462 return mSystemUiVisibility;
15463 }
15464
Scott Mainec6331b2011-05-24 16:55:56 -070015465 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070015466 * Returns the current system UI visibility that is currently set for
15467 * the entire window. This is the combination of the
15468 * {@link #setSystemUiVisibility(int)} values supplied by all of the
15469 * views in the window.
15470 */
15471 public int getWindowSystemUiVisibility() {
15472 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
15473 }
15474
15475 /**
15476 * Override to find out when the window's requested system UI visibility
15477 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
15478 * This is different from the callbacks recieved through
15479 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
15480 * in that this is only telling you about the local request of the window,
15481 * not the actual values applied by the system.
15482 */
15483 public void onWindowSystemUiVisibilityChanged(int visible) {
15484 }
15485
15486 /**
15487 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
15488 * the view hierarchy.
15489 */
15490 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
15491 onWindowSystemUiVisibilityChanged(visible);
15492 }
15493
15494 /**
Scott Mainec6331b2011-05-24 16:55:56 -070015495 * Set a listener to receive callbacks when the visibility of the system bar changes.
15496 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
15497 */
Joe Onorato664644d2011-01-23 17:53:23 -080015498 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015499 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080015500 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15501 mParent.recomputeViewAttributes(this);
15502 }
15503 }
15504
15505 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015506 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
15507 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080015508 */
15509 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015510 ListenerInfo li = mListenerInfo;
15511 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
15512 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080015513 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080015514 }
15515 }
15516
Dianne Hackborncf675782012-05-10 15:07:24 -070015517 boolean updateLocalSystemUiVisibility(int localValue, int localChanges) {
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015518 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
15519 if (val != mSystemUiVisibility) {
15520 setSystemUiVisibility(val);
Dianne Hackborncf675782012-05-10 15:07:24 -070015521 return true;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015522 }
Dianne Hackborncf675782012-05-10 15:07:24 -070015523 return false;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015524 }
15525
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070015526 /** @hide */
15527 public void setDisabledSystemUiVisibility(int flags) {
15528 if (mAttachInfo != null) {
15529 if (mAttachInfo.mDisabledSystemUiVisibility != flags) {
15530 mAttachInfo.mDisabledSystemUiVisibility = flags;
15531 if (mParent != null) {
15532 mParent.recomputeViewAttributes(this);
15533 }
15534 }
15535 }
15536 }
15537
Joe Onorato664644d2011-01-23 17:53:23 -080015538 /**
Joe Malin32736f02011-01-19 16:14:20 -080015539 * Creates an image that the system displays during the drag and drop
15540 * operation. This is called a &quot;drag shadow&quot;. The default implementation
15541 * for a DragShadowBuilder based on a View returns an image that has exactly the same
15542 * appearance as the given View. The default also positions the center of the drag shadow
15543 * directly under the touch point. If no View is provided (the constructor with no parameters
15544 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
15545 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
15546 * default is an invisible drag shadow.
15547 * <p>
15548 * You are not required to use the View you provide to the constructor as the basis of the
15549 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
15550 * anything you want as the drag shadow.
15551 * </p>
15552 * <p>
15553 * You pass a DragShadowBuilder object to the system when you start the drag. The system
15554 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
15555 * size and position of the drag shadow. It uses this data to construct a
15556 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
15557 * so that your application can draw the shadow image in the Canvas.
15558 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070015559 *
15560 * <div class="special reference">
15561 * <h3>Developer Guides</h3>
15562 * <p>For a guide to implementing drag and drop features, read the
15563 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
15564 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070015565 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015566 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070015567 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070015568
15569 /**
Joe Malin32736f02011-01-19 16:14:20 -080015570 * Constructs a shadow image builder based on a View. By default, the resulting drag
15571 * shadow will have the same appearance and dimensions as the View, with the touch point
15572 * over the center of the View.
15573 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070015574 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015575 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070015576 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070015577 }
15578
Christopher Tate17ed60c2011-01-18 12:50:26 -080015579 /**
15580 * Construct a shadow builder object with no associated View. This
15581 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
15582 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
15583 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080015584 * reference to any View object. If they are not overridden, then the result is an
15585 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080015586 */
15587 public DragShadowBuilder() {
15588 mView = new WeakReference<View>(null);
15589 }
15590
15591 /**
15592 * Returns the View object that had been passed to the
15593 * {@link #View.DragShadowBuilder(View)}
15594 * constructor. If that View parameter was {@code null} or if the
15595 * {@link #View.DragShadowBuilder()}
15596 * constructor was used to instantiate the builder object, this method will return
15597 * null.
15598 *
15599 * @return The View object associate with this builder object.
15600 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070015601 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070015602 final public View getView() {
15603 return mView.get();
15604 }
15605
Christopher Tate2c095f32010-10-04 14:13:40 -070015606 /**
Joe Malin32736f02011-01-19 16:14:20 -080015607 * Provides the metrics for the shadow image. These include the dimensions of
15608 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070015609 * be centered under the touch location while dragging.
15610 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015611 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080015612 * same as the dimensions of the View itself and centers the shadow under
15613 * the touch point.
15614 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070015615 *
Joe Malin32736f02011-01-19 16:14:20 -080015616 * @param shadowSize A {@link android.graphics.Point} containing the width and height
15617 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
15618 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
15619 * image.
15620 *
15621 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
15622 * shadow image that should be underneath the touch point during the drag and drop
15623 * operation. Your application must set {@link android.graphics.Point#x} to the
15624 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070015625 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015626 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070015627 final View view = mView.get();
15628 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015629 shadowSize.set(view.getWidth(), view.getHeight());
15630 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070015631 } else {
15632 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
15633 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015634 }
15635
15636 /**
Joe Malin32736f02011-01-19 16:14:20 -080015637 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
15638 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015639 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070015640 *
Joe Malin32736f02011-01-19 16:14:20 -080015641 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070015642 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015643 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070015644 final View view = mView.get();
15645 if (view != null) {
15646 view.draw(canvas);
15647 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015648 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070015649 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015650 }
15651 }
15652
15653 /**
Joe Malin32736f02011-01-19 16:14:20 -080015654 * Starts a drag and drop operation. When your application calls this method, it passes a
15655 * {@link android.view.View.DragShadowBuilder} object to the system. The
15656 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
15657 * to get metrics for the drag shadow, and then calls the object's
15658 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
15659 * <p>
15660 * Once the system has the drag shadow, it begins the drag and drop operation by sending
15661 * drag events to all the View objects in your application that are currently visible. It does
15662 * this either by calling the View object's drag listener (an implementation of
15663 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
15664 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
15665 * Both are passed a {@link android.view.DragEvent} object that has a
15666 * {@link android.view.DragEvent#getAction()} value of
15667 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
15668 * </p>
15669 * <p>
15670 * Your application can invoke startDrag() on any attached View object. The View object does not
15671 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
15672 * be related to the View the user selected for dragging.
15673 * </p>
15674 * @param data A {@link android.content.ClipData} object pointing to the data to be
15675 * transferred by the drag and drop operation.
15676 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
15677 * drag shadow.
15678 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
15679 * drop operation. This Object is put into every DragEvent object sent by the system during the
15680 * current drag.
15681 * <p>
15682 * myLocalState is a lightweight mechanism for the sending information from the dragged View
15683 * to the target Views. For example, it can contain flags that differentiate between a
15684 * a copy operation and a move operation.
15685 * </p>
15686 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
15687 * so the parameter should be set to 0.
15688 * @return {@code true} if the method completes successfully, or
15689 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
15690 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070015691 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015692 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015693 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070015694 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015695 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070015696 }
15697 boolean okay = false;
15698
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015699 Point shadowSize = new Point();
15700 Point shadowTouchPoint = new Point();
15701 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070015702
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015703 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
15704 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
15705 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070015706 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015707
Chris Tatea32dcf72010-10-14 12:13:50 -070015708 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015709 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
15710 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070015711 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015712 Surface surface = new Surface();
15713 try {
15714 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015715 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070015716 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070015717 + " surface=" + surface);
15718 if (token != null) {
15719 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070015720 try {
Chris Tate6b391282010-10-14 15:48:59 -070015721 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015722 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070015723 } finally {
15724 surface.unlockCanvasAndPost(canvas);
15725 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015726
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015727 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080015728
15729 // Cache the local state object for delivery with DragEvents
15730 root.setLocalDragState(myLocalState);
15731
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015732 // repurpose 'shadowSize' for the last touch point
15733 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070015734
Christopher Tatea53146c2010-09-07 11:57:52 -070015735 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015736 shadowSize.x, shadowSize.y,
15737 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070015738 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070015739
15740 // Off and running! Release our local surface instance; the drag
15741 // shadow surface is now managed by the system process.
15742 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070015743 }
15744 } catch (Exception e) {
15745 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
15746 surface.destroy();
15747 }
15748
15749 return okay;
15750 }
15751
Christopher Tatea53146c2010-09-07 11:57:52 -070015752 /**
Joe Malin32736f02011-01-19 16:14:20 -080015753 * Handles drag events sent by the system following a call to
15754 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
15755 *<p>
15756 * When the system calls this method, it passes a
15757 * {@link android.view.DragEvent} object. A call to
15758 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
15759 * in DragEvent. The method uses these to determine what is happening in the drag and drop
15760 * operation.
15761 * @param event The {@link android.view.DragEvent} sent by the system.
15762 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
15763 * in DragEvent, indicating the type of drag event represented by this object.
15764 * @return {@code true} if the method was successful, otherwise {@code false}.
15765 * <p>
15766 * The method should return {@code true} in response to an action type of
15767 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
15768 * operation.
15769 * </p>
15770 * <p>
15771 * The method should also return {@code true} in response to an action type of
15772 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
15773 * {@code false} if it didn't.
15774 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015775 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070015776 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070015777 return false;
15778 }
15779
15780 /**
Joe Malin32736f02011-01-19 16:14:20 -080015781 * Detects if this View is enabled and has a drag event listener.
15782 * If both are true, then it calls the drag event listener with the
15783 * {@link android.view.DragEvent} it received. If the drag event listener returns
15784 * {@code true}, then dispatchDragEvent() returns {@code true}.
15785 * <p>
15786 * For all other cases, the method calls the
15787 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
15788 * method and returns its result.
15789 * </p>
15790 * <p>
15791 * This ensures that a drag event is always consumed, even if the View does not have a drag
15792 * event listener. However, if the View has a listener and the listener returns true, then
15793 * onDragEvent() is not called.
15794 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015795 */
15796 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080015797 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015798 ListenerInfo li = mListenerInfo;
15799 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
15800 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070015801 return true;
15802 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015803 return onDragEvent(event);
15804 }
15805
Christopher Tate3d4bf172011-03-28 16:16:46 -070015806 boolean canAcceptDrag() {
15807 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
15808 }
15809
Christopher Tatea53146c2010-09-07 11:57:52 -070015810 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070015811 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
15812 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070015813 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070015814 */
15815 public void onCloseSystemDialogs(String reason) {
15816 }
Joe Malin32736f02011-01-19 16:14:20 -080015817
Dianne Hackbornffa42482009-09-23 22:20:11 -070015818 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015819 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070015820 * update a Region being computed for
15821 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015822 * that any non-transparent parts of the Drawable are removed from the
15823 * given transparent region.
15824 *
15825 * @param dr The Drawable whose transparency is to be applied to the region.
15826 * @param region A Region holding the current transparency information,
15827 * where any parts of the region that are set are considered to be
15828 * transparent. On return, this region will be modified to have the
15829 * transparency information reduced by the corresponding parts of the
15830 * Drawable that are not transparent.
15831 * {@hide}
15832 */
15833 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
15834 if (DBG) {
15835 Log.i("View", "Getting transparent region for: " + this);
15836 }
15837 final Region r = dr.getTransparentRegion();
15838 final Rect db = dr.getBounds();
15839 final AttachInfo attachInfo = mAttachInfo;
15840 if (r != null && attachInfo != null) {
15841 final int w = getRight()-getLeft();
15842 final int h = getBottom()-getTop();
15843 if (db.left > 0) {
15844 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
15845 r.op(0, 0, db.left, h, Region.Op.UNION);
15846 }
15847 if (db.right < w) {
15848 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
15849 r.op(db.right, 0, w, h, Region.Op.UNION);
15850 }
15851 if (db.top > 0) {
15852 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
15853 r.op(0, 0, w, db.top, Region.Op.UNION);
15854 }
15855 if (db.bottom < h) {
15856 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
15857 r.op(0, db.bottom, w, h, Region.Op.UNION);
15858 }
15859 final int[] location = attachInfo.mTransparentLocation;
15860 getLocationInWindow(location);
15861 r.translate(location[0], location[1]);
15862 region.op(r, Region.Op.INTERSECT);
15863 } else {
15864 region.op(db, Region.Op.DIFFERENCE);
15865 }
15866 }
15867
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015868 private void checkForLongClick(int delayOffset) {
15869 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
15870 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015871
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015872 if (mPendingCheckForLongPress == null) {
15873 mPendingCheckForLongPress = new CheckForLongPress();
15874 }
15875 mPendingCheckForLongPress.rememberWindowAttachCount();
15876 postDelayed(mPendingCheckForLongPress,
15877 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015878 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015879 }
15880
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015881 /**
15882 * Inflate a view from an XML resource. This convenience method wraps the {@link
15883 * LayoutInflater} class, which provides a full range of options for view inflation.
15884 *
15885 * @param context The Context object for your activity or application.
15886 * @param resource The resource ID to inflate
15887 * @param root A view group that will be the parent. Used to properly inflate the
15888 * layout_* parameters.
15889 * @see LayoutInflater
15890 */
15891 public static View inflate(Context context, int resource, ViewGroup root) {
15892 LayoutInflater factory = LayoutInflater.from(context);
15893 return factory.inflate(resource, root);
15894 }
Romain Guy33e72ae2010-07-17 12:40:29 -070015895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015896 /**
Adam Powell637d3372010-08-25 14:37:03 -070015897 * Scroll the view with standard behavior for scrolling beyond the normal
15898 * content boundaries. Views that call this method should override
15899 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
15900 * results of an over-scroll operation.
15901 *
15902 * Views can use this method to handle any touch or fling-based scrolling.
15903 *
15904 * @param deltaX Change in X in pixels
15905 * @param deltaY Change in Y in pixels
15906 * @param scrollX Current X scroll value in pixels before applying deltaX
15907 * @param scrollY Current Y scroll value in pixels before applying deltaY
15908 * @param scrollRangeX Maximum content scroll range along the X axis
15909 * @param scrollRangeY Maximum content scroll range along the Y axis
15910 * @param maxOverScrollX Number of pixels to overscroll by in either direction
15911 * along the X axis.
15912 * @param maxOverScrollY Number of pixels to overscroll by in either direction
15913 * along the Y axis.
15914 * @param isTouchEvent true if this scroll operation is the result of a touch event.
15915 * @return true if scrolling was clamped to an over-scroll boundary along either
15916 * axis, false otherwise.
15917 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070015918 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070015919 protected boolean overScrollBy(int deltaX, int deltaY,
15920 int scrollX, int scrollY,
15921 int scrollRangeX, int scrollRangeY,
15922 int maxOverScrollX, int maxOverScrollY,
15923 boolean isTouchEvent) {
15924 final int overScrollMode = mOverScrollMode;
15925 final boolean canScrollHorizontal =
15926 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
15927 final boolean canScrollVertical =
15928 computeVerticalScrollRange() > computeVerticalScrollExtent();
15929 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
15930 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
15931 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
15932 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
15933
15934 int newScrollX = scrollX + deltaX;
15935 if (!overScrollHorizontal) {
15936 maxOverScrollX = 0;
15937 }
15938
15939 int newScrollY = scrollY + deltaY;
15940 if (!overScrollVertical) {
15941 maxOverScrollY = 0;
15942 }
15943
15944 // Clamp values if at the limits and record
15945 final int left = -maxOverScrollX;
15946 final int right = maxOverScrollX + scrollRangeX;
15947 final int top = -maxOverScrollY;
15948 final int bottom = maxOverScrollY + scrollRangeY;
15949
15950 boolean clampedX = false;
15951 if (newScrollX > right) {
15952 newScrollX = right;
15953 clampedX = true;
15954 } else if (newScrollX < left) {
15955 newScrollX = left;
15956 clampedX = true;
15957 }
15958
15959 boolean clampedY = false;
15960 if (newScrollY > bottom) {
15961 newScrollY = bottom;
15962 clampedY = true;
15963 } else if (newScrollY < top) {
15964 newScrollY = top;
15965 clampedY = true;
15966 }
15967
15968 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
15969
15970 return clampedX || clampedY;
15971 }
15972
15973 /**
15974 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
15975 * respond to the results of an over-scroll operation.
15976 *
15977 * @param scrollX New X scroll value in pixels
15978 * @param scrollY New Y scroll value in pixels
15979 * @param clampedX True if scrollX was clamped to an over-scroll boundary
15980 * @param clampedY True if scrollY was clamped to an over-scroll boundary
15981 */
15982 protected void onOverScrolled(int scrollX, int scrollY,
15983 boolean clampedX, boolean clampedY) {
15984 // Intentionally empty.
15985 }
15986
15987 /**
15988 * Returns the over-scroll mode for this view. The result will be
15989 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
15990 * (allow over-scrolling only if the view content is larger than the container),
15991 * or {@link #OVER_SCROLL_NEVER}.
15992 *
15993 * @return This view's over-scroll mode.
15994 */
15995 public int getOverScrollMode() {
15996 return mOverScrollMode;
15997 }
15998
15999 /**
16000 * Set the over-scroll mode for this view. Valid over-scroll modes are
16001 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16002 * (allow over-scrolling only if the view content is larger than the container),
16003 * or {@link #OVER_SCROLL_NEVER}.
16004 *
16005 * Setting the over-scroll mode of a view will have an effect only if the
16006 * view is capable of scrolling.
16007 *
16008 * @param overScrollMode The new over-scroll mode for this view.
16009 */
16010 public void setOverScrollMode(int overScrollMode) {
16011 if (overScrollMode != OVER_SCROLL_ALWAYS &&
16012 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
16013 overScrollMode != OVER_SCROLL_NEVER) {
16014 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
16015 }
16016 mOverScrollMode = overScrollMode;
16017 }
16018
16019 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016020 * Gets a scale factor that determines the distance the view should scroll
16021 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
16022 * @return The vertical scroll scale factor.
16023 * @hide
16024 */
16025 protected float getVerticalScrollFactor() {
16026 if (mVerticalScrollFactor == 0) {
16027 TypedValue outValue = new TypedValue();
16028 if (!mContext.getTheme().resolveAttribute(
16029 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
16030 throw new IllegalStateException(
16031 "Expected theme to define listPreferredItemHeight.");
16032 }
16033 mVerticalScrollFactor = outValue.getDimension(
16034 mContext.getResources().getDisplayMetrics());
16035 }
16036 return mVerticalScrollFactor;
16037 }
16038
16039 /**
16040 * Gets a scale factor that determines the distance the view should scroll
16041 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
16042 * @return The horizontal scroll scale factor.
16043 * @hide
16044 */
16045 protected float getHorizontalScrollFactor() {
16046 // TODO: Should use something else.
16047 return getVerticalScrollFactor();
16048 }
16049
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016050 /**
16051 * Return the value specifying the text direction or policy that was set with
16052 * {@link #setTextDirection(int)}.
16053 *
16054 * @return the defined text direction. It can be one of:
16055 *
16056 * {@link #TEXT_DIRECTION_INHERIT},
16057 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16058 * {@link #TEXT_DIRECTION_ANY_RTL},
16059 * {@link #TEXT_DIRECTION_LTR},
16060 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016061 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016062 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016063 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016064 @ViewDebug.ExportedProperty(category = "text", mapping = {
16065 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
16066 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
16067 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
16068 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
16069 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
16070 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
16071 })
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016072 public int getTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016073 return (mPrivateFlags2 & TEXT_DIRECTION_MASK) >> TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016074 }
16075
16076 /**
16077 * Set the text direction.
16078 *
16079 * @param textDirection the direction to set. Should be one of:
16080 *
16081 * {@link #TEXT_DIRECTION_INHERIT},
16082 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16083 * {@link #TEXT_DIRECTION_ANY_RTL},
16084 * {@link #TEXT_DIRECTION_LTR},
16085 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016086 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016087 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016088 */
16089 public void setTextDirection(int textDirection) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016090 if (getTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016091 // Reset the current text direction and the resolved one
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016092 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016093 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016094 // Set the new text direction
16095 mPrivateFlags2 |= ((textDirection << TEXT_DIRECTION_MASK_SHIFT) & TEXT_DIRECTION_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016096 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016097 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016098 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016099 }
16100 }
16101
16102 /**
16103 * Return the resolved text direction.
16104 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016105 * This needs resolution if the value is TEXT_DIRECTION_INHERIT. The resolution matches
16106 * {@link #getTextDirection()}if it is not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds
16107 * up the parent chain of the view. if there is no parent, then it will return the default
16108 * {@link #TEXT_DIRECTION_FIRST_STRONG}.
16109 *
16110 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016111 *
Doug Feltcb3791202011-07-07 11:57:48 -070016112 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16113 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016114 * {@link #TEXT_DIRECTION_LTR},
16115 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016116 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016117 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016118 */
16119 public int getResolvedTextDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070016120 // The text direction will be resolved only if needed
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016121 if ((mPrivateFlags2 & TEXT_DIRECTION_RESOLVED) != TEXT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016122 resolveTextDirection();
16123 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016124 return (mPrivateFlags2 & TEXT_DIRECTION_RESOLVED_MASK) >> TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016125 }
16126
16127 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016128 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
16129 * resolution is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016130 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016131 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016132 public void resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016133 // Reset any previous text direction resolution
16134 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
16135
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016136 if (hasRtlSupport()) {
16137 // Set resolved text direction flag depending on text direction flag
16138 final int textDirection = getTextDirection();
16139 switch(textDirection) {
16140 case TEXT_DIRECTION_INHERIT:
16141 if (canResolveTextDirection()) {
16142 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016143
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016144 // Set current resolved direction to the same value as the parent's one
16145 final int parentResolvedDirection = viewGroup.getResolvedTextDirection();
16146 switch (parentResolvedDirection) {
16147 case TEXT_DIRECTION_FIRST_STRONG:
16148 case TEXT_DIRECTION_ANY_RTL:
16149 case TEXT_DIRECTION_LTR:
16150 case TEXT_DIRECTION_RTL:
16151 case TEXT_DIRECTION_LOCALE:
16152 mPrivateFlags2 |=
16153 (parentResolvedDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16154 break;
16155 default:
16156 // Default resolved direction is "first strong" heuristic
16157 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
16158 }
16159 } else {
16160 // We cannot do the resolution if there is no parent, so use the default one
16161 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016162 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016163 break;
16164 case TEXT_DIRECTION_FIRST_STRONG:
16165 case TEXT_DIRECTION_ANY_RTL:
16166 case TEXT_DIRECTION_LTR:
16167 case TEXT_DIRECTION_RTL:
16168 case TEXT_DIRECTION_LOCALE:
16169 // Resolved direction is the same as text direction
16170 mPrivateFlags2 |= (textDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16171 break;
16172 default:
16173 // Default resolved direction is "first strong" heuristic
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016174 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016175 }
16176 } else {
16177 // Default resolved direction is "first strong" heuristic
16178 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016179 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016180
16181 // Set to resolved
16182 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016183 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016184 }
16185
16186 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016187 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016188 * resolution should override this method.
16189 *
16190 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016191 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016192 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016193 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016194 }
16195
16196 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016197 * Check if text direction resolution can be done.
16198 *
16199 * @return true if text direction resolution can be done otherwise return false.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016200 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016201 */
16202 public boolean canResolveTextDirection() {
16203 switch (getTextDirection()) {
16204 case TEXT_DIRECTION_INHERIT:
16205 return (mParent != null) && (mParent instanceof ViewGroup);
16206 default:
16207 return true;
16208 }
16209 }
16210
16211 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016212 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016213 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016214 * reset is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016215 * @hide
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016216 */
16217 public void resetResolvedTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016218 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016219 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016220 }
16221
16222 /**
16223 * Called when text direction is reset. Subclasses that care about text direction reset should
16224 * override this method and do a reset of the text direction of their children. The default
16225 * implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016226 * @hide
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016227 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016228 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016229 }
16230
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016231 /**
16232 * Return the value specifying the text alignment or policy that was set with
16233 * {@link #setTextAlignment(int)}.
16234 *
16235 * @return the defined text alignment. It can be one of:
16236 *
16237 * {@link #TEXT_ALIGNMENT_INHERIT},
16238 * {@link #TEXT_ALIGNMENT_GRAVITY},
16239 * {@link #TEXT_ALIGNMENT_CENTER},
16240 * {@link #TEXT_ALIGNMENT_TEXT_START},
16241 * {@link #TEXT_ALIGNMENT_TEXT_END},
16242 * {@link #TEXT_ALIGNMENT_VIEW_START},
16243 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016244 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016245 */
16246 @ViewDebug.ExportedProperty(category = "text", mapping = {
16247 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16248 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16249 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16250 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16251 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16252 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16253 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16254 })
16255 public int getTextAlignment() {
16256 return (mPrivateFlags2 & TEXT_ALIGNMENT_MASK) >> TEXT_ALIGNMENT_MASK_SHIFT;
16257 }
16258
16259 /**
16260 * Set the text alignment.
16261 *
16262 * @param textAlignment The text alignment to set. Should be one of
16263 *
16264 * {@link #TEXT_ALIGNMENT_INHERIT},
16265 * {@link #TEXT_ALIGNMENT_GRAVITY},
16266 * {@link #TEXT_ALIGNMENT_CENTER},
16267 * {@link #TEXT_ALIGNMENT_TEXT_START},
16268 * {@link #TEXT_ALIGNMENT_TEXT_END},
16269 * {@link #TEXT_ALIGNMENT_VIEW_START},
16270 * {@link #TEXT_ALIGNMENT_VIEW_END}
16271 *
16272 * @attr ref android.R.styleable#View_textAlignment
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016273 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016274 */
16275 public void setTextAlignment(int textAlignment) {
16276 if (textAlignment != getTextAlignment()) {
16277 // Reset the current and resolved text alignment
16278 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
16279 resetResolvedTextAlignment();
16280 // Set the new text alignment
16281 mPrivateFlags2 |= ((textAlignment << TEXT_ALIGNMENT_MASK_SHIFT) & TEXT_ALIGNMENT_MASK);
16282 // Refresh
16283 requestLayout();
16284 invalidate(true);
16285 }
16286 }
16287
16288 /**
16289 * Return the resolved text alignment.
16290 *
16291 * The resolved text alignment. This needs resolution if the value is
16292 * TEXT_ALIGNMENT_INHERIT. The resolution matches {@link #setTextAlignment(int)} if it is
16293 * not TEXT_ALIGNMENT_INHERIT, otherwise resolution proceeds up the parent chain of the view.
16294 *
16295 * @return the resolved text alignment. Returns one of:
16296 *
16297 * {@link #TEXT_ALIGNMENT_GRAVITY},
16298 * {@link #TEXT_ALIGNMENT_CENTER},
16299 * {@link #TEXT_ALIGNMENT_TEXT_START},
16300 * {@link #TEXT_ALIGNMENT_TEXT_END},
16301 * {@link #TEXT_ALIGNMENT_VIEW_START},
16302 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016303 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016304 */
16305 @ViewDebug.ExportedProperty(category = "text", mapping = {
16306 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16307 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16308 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16309 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16310 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16311 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16312 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16313 })
16314 public int getResolvedTextAlignment() {
16315 // If text alignment is not resolved, then resolve it
16316 if ((mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED) != TEXT_ALIGNMENT_RESOLVED) {
16317 resolveTextAlignment();
16318 }
16319 return (mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED_MASK) >> TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
16320 }
16321
16322 /**
16323 * Resolve the text alignment. Will call {@link View#onResolvedTextAlignmentChanged} when
16324 * resolution is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016325 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016326 */
16327 public void resolveTextAlignment() {
16328 // Reset any previous text alignment resolution
16329 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16330
16331 if (hasRtlSupport()) {
16332 // Set resolved text alignment flag depending on text alignment flag
16333 final int textAlignment = getTextAlignment();
16334 switch (textAlignment) {
16335 case TEXT_ALIGNMENT_INHERIT:
16336 // Check if we can resolve the text alignment
16337 if (canResolveLayoutDirection() && mParent instanceof View) {
16338 View view = (View) mParent;
16339
16340 final int parentResolvedTextAlignment = view.getResolvedTextAlignment();
16341 switch (parentResolvedTextAlignment) {
16342 case TEXT_ALIGNMENT_GRAVITY:
16343 case TEXT_ALIGNMENT_TEXT_START:
16344 case TEXT_ALIGNMENT_TEXT_END:
16345 case TEXT_ALIGNMENT_CENTER:
16346 case TEXT_ALIGNMENT_VIEW_START:
16347 case TEXT_ALIGNMENT_VIEW_END:
16348 // Resolved text alignment is the same as the parent resolved
16349 // text alignment
16350 mPrivateFlags2 |=
16351 (parentResolvedTextAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16352 break;
16353 default:
16354 // Use default resolved text alignment
16355 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16356 }
16357 }
16358 else {
16359 // We cannot do the resolution if there is no parent so use the default
16360 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16361 }
16362 break;
16363 case TEXT_ALIGNMENT_GRAVITY:
16364 case TEXT_ALIGNMENT_TEXT_START:
16365 case TEXT_ALIGNMENT_TEXT_END:
16366 case TEXT_ALIGNMENT_CENTER:
16367 case TEXT_ALIGNMENT_VIEW_START:
16368 case TEXT_ALIGNMENT_VIEW_END:
16369 // Resolved text alignment is the same as text alignment
16370 mPrivateFlags2 |= (textAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16371 break;
16372 default:
16373 // Use default resolved text alignment
16374 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16375 }
16376 } else {
16377 // Use default resolved text alignment
16378 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16379 }
16380
16381 // Set the resolved
16382 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED;
16383 onResolvedTextAlignmentChanged();
16384 }
16385
16386 /**
16387 * Check if text alignment resolution can be done.
16388 *
16389 * @return true if text alignment resolution can be done otherwise return false.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016390 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016391 */
16392 public boolean canResolveTextAlignment() {
16393 switch (getTextAlignment()) {
16394 case TEXT_DIRECTION_INHERIT:
16395 return (mParent != null);
16396 default:
16397 return true;
16398 }
16399 }
16400
16401 /**
16402 * Called when text alignment has been resolved. Subclasses that care about text alignment
16403 * resolution should override this method.
16404 *
16405 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016406 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016407 */
16408 public void onResolvedTextAlignmentChanged() {
16409 }
16410
16411 /**
16412 * Reset resolved text alignment. Text alignment can be resolved with a call to
16413 * getResolvedTextAlignment(). Will call {@link View#onResolvedTextAlignmentReset} when
16414 * reset is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016415 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016416 */
16417 public void resetResolvedTextAlignment() {
16418 // Reset any previous text alignment resolution
16419 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16420 onResolvedTextAlignmentReset();
16421 }
16422
16423 /**
16424 * Called when text alignment is reset. Subclasses that care about text alignment reset should
16425 * override this method and do a reset of the text alignment of their children. The default
16426 * implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016427 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016428 */
16429 public void onResolvedTextAlignmentReset() {
16430 }
16431
Chet Haaseb39f0512011-05-24 14:36:40 -070016432 //
16433 // Properties
16434 //
16435 /**
16436 * A Property wrapper around the <code>alpha</code> functionality handled by the
16437 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
16438 */
Chet Haased47f1532011-12-16 11:18:52 -080016439 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016440 @Override
16441 public void setValue(View object, float value) {
16442 object.setAlpha(value);
16443 }
16444
16445 @Override
16446 public Float get(View object) {
16447 return object.getAlpha();
16448 }
16449 };
16450
16451 /**
16452 * A Property wrapper around the <code>translationX</code> functionality handled by the
16453 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
16454 */
Chet Haased47f1532011-12-16 11:18:52 -080016455 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016456 @Override
16457 public void setValue(View object, float value) {
16458 object.setTranslationX(value);
16459 }
16460
16461 @Override
16462 public Float get(View object) {
16463 return object.getTranslationX();
16464 }
16465 };
16466
16467 /**
16468 * A Property wrapper around the <code>translationY</code> functionality handled by the
16469 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
16470 */
Chet Haased47f1532011-12-16 11:18:52 -080016471 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016472 @Override
16473 public void setValue(View object, float value) {
16474 object.setTranslationY(value);
16475 }
16476
16477 @Override
16478 public Float get(View object) {
16479 return object.getTranslationY();
16480 }
16481 };
16482
16483 /**
16484 * A Property wrapper around the <code>x</code> functionality handled by the
16485 * {@link View#setX(float)} and {@link View#getX()} methods.
16486 */
Chet Haased47f1532011-12-16 11:18:52 -080016487 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016488 @Override
16489 public void setValue(View object, float value) {
16490 object.setX(value);
16491 }
16492
16493 @Override
16494 public Float get(View object) {
16495 return object.getX();
16496 }
16497 };
16498
16499 /**
16500 * A Property wrapper around the <code>y</code> functionality handled by the
16501 * {@link View#setY(float)} and {@link View#getY()} methods.
16502 */
Chet Haased47f1532011-12-16 11:18:52 -080016503 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016504 @Override
16505 public void setValue(View object, float value) {
16506 object.setY(value);
16507 }
16508
16509 @Override
16510 public Float get(View object) {
16511 return object.getY();
16512 }
16513 };
16514
16515 /**
16516 * A Property wrapper around the <code>rotation</code> functionality handled by the
16517 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
16518 */
Chet Haased47f1532011-12-16 11:18:52 -080016519 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016520 @Override
16521 public void setValue(View object, float value) {
16522 object.setRotation(value);
16523 }
16524
16525 @Override
16526 public Float get(View object) {
16527 return object.getRotation();
16528 }
16529 };
16530
16531 /**
16532 * A Property wrapper around the <code>rotationX</code> functionality handled by the
16533 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
16534 */
Chet Haased47f1532011-12-16 11:18:52 -080016535 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016536 @Override
16537 public void setValue(View object, float value) {
16538 object.setRotationX(value);
16539 }
16540
16541 @Override
16542 public Float get(View object) {
16543 return object.getRotationX();
16544 }
16545 };
16546
16547 /**
16548 * A Property wrapper around the <code>rotationY</code> functionality handled by the
16549 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
16550 */
Chet Haased47f1532011-12-16 11:18:52 -080016551 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016552 @Override
16553 public void setValue(View object, float value) {
16554 object.setRotationY(value);
16555 }
16556
16557 @Override
16558 public Float get(View object) {
16559 return object.getRotationY();
16560 }
16561 };
16562
16563 /**
16564 * A Property wrapper around the <code>scaleX</code> functionality handled by the
16565 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
16566 */
Chet Haased47f1532011-12-16 11:18:52 -080016567 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016568 @Override
16569 public void setValue(View object, float value) {
16570 object.setScaleX(value);
16571 }
16572
16573 @Override
16574 public Float get(View object) {
16575 return object.getScaleX();
16576 }
16577 };
16578
16579 /**
16580 * A Property wrapper around the <code>scaleY</code> functionality handled by the
16581 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
16582 */
Chet Haased47f1532011-12-16 11:18:52 -080016583 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016584 @Override
16585 public void setValue(View object, float value) {
16586 object.setScaleY(value);
16587 }
16588
16589 @Override
16590 public Float get(View object) {
16591 return object.getScaleY();
16592 }
16593 };
16594
Jeff Brown33bbfd22011-02-24 20:55:35 -080016595 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016596 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
16597 * Each MeasureSpec represents a requirement for either the width or the height.
16598 * A MeasureSpec is comprised of a size and a mode. There are three possible
16599 * modes:
16600 * <dl>
16601 * <dt>UNSPECIFIED</dt>
16602 * <dd>
16603 * The parent has not imposed any constraint on the child. It can be whatever size
16604 * it wants.
16605 * </dd>
16606 *
16607 * <dt>EXACTLY</dt>
16608 * <dd>
16609 * The parent has determined an exact size for the child. The child is going to be
16610 * given those bounds regardless of how big it wants to be.
16611 * </dd>
16612 *
16613 * <dt>AT_MOST</dt>
16614 * <dd>
16615 * The child can be as large as it wants up to the specified size.
16616 * </dd>
16617 * </dl>
16618 *
16619 * MeasureSpecs are implemented as ints to reduce object allocation. This class
16620 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
16621 */
16622 public static class MeasureSpec {
16623 private static final int MODE_SHIFT = 30;
16624 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
16625
16626 /**
16627 * Measure specification mode: The parent has not imposed any constraint
16628 * on the child. It can be whatever size it wants.
16629 */
16630 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
16631
16632 /**
16633 * Measure specification mode: The parent has determined an exact size
16634 * for the child. The child is going to be given those bounds regardless
16635 * of how big it wants to be.
16636 */
16637 public static final int EXACTLY = 1 << MODE_SHIFT;
16638
16639 /**
16640 * Measure specification mode: The child can be as large as it wants up
16641 * to the specified size.
16642 */
16643 public static final int AT_MOST = 2 << MODE_SHIFT;
16644
16645 /**
16646 * Creates a measure specification based on the supplied size and mode.
16647 *
16648 * The mode must always be one of the following:
16649 * <ul>
16650 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
16651 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
16652 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
16653 * </ul>
16654 *
16655 * @param size the size of the measure specification
16656 * @param mode the mode of the measure specification
16657 * @return the measure specification based on size and mode
16658 */
16659 public static int makeMeasureSpec(int size, int mode) {
16660 return size + mode;
16661 }
16662
16663 /**
16664 * Extracts the mode from the supplied measure specification.
16665 *
16666 * @param measureSpec the measure specification to extract the mode from
16667 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
16668 * {@link android.view.View.MeasureSpec#AT_MOST} or
16669 * {@link android.view.View.MeasureSpec#EXACTLY}
16670 */
16671 public static int getMode(int measureSpec) {
16672 return (measureSpec & MODE_MASK);
16673 }
16674
16675 /**
16676 * Extracts the size from the supplied measure specification.
16677 *
16678 * @param measureSpec the measure specification to extract the size from
16679 * @return the size in pixels defined in the supplied measure specification
16680 */
16681 public static int getSize(int measureSpec) {
16682 return (measureSpec & ~MODE_MASK);
16683 }
16684
16685 /**
16686 * Returns a String representation of the specified measure
16687 * specification.
16688 *
16689 * @param measureSpec the measure specification to convert to a String
16690 * @return a String with the following format: "MeasureSpec: MODE SIZE"
16691 */
16692 public static String toString(int measureSpec) {
16693 int mode = getMode(measureSpec);
16694 int size = getSize(measureSpec);
16695
16696 StringBuilder sb = new StringBuilder("MeasureSpec: ");
16697
16698 if (mode == UNSPECIFIED)
16699 sb.append("UNSPECIFIED ");
16700 else if (mode == EXACTLY)
16701 sb.append("EXACTLY ");
16702 else if (mode == AT_MOST)
16703 sb.append("AT_MOST ");
16704 else
16705 sb.append(mode).append(" ");
16706
16707 sb.append(size);
16708 return sb.toString();
16709 }
16710 }
16711
16712 class CheckForLongPress implements Runnable {
16713
16714 private int mOriginalWindowAttachCount;
16715
16716 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070016717 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016718 && mOriginalWindowAttachCount == mWindowAttachCount) {
16719 if (performLongClick()) {
16720 mHasPerformedLongPress = true;
16721 }
16722 }
16723 }
16724
16725 public void rememberWindowAttachCount() {
16726 mOriginalWindowAttachCount = mWindowAttachCount;
16727 }
16728 }
Joe Malin32736f02011-01-19 16:14:20 -080016729
Adam Powelle14579b2009-12-16 18:39:52 -080016730 private final class CheckForTap implements Runnable {
16731 public void run() {
16732 mPrivateFlags &= ~PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080016733 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016734 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080016735 }
16736 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016737
Adam Powella35d7682010-03-12 14:48:13 -080016738 private final class PerformClick implements Runnable {
16739 public void run() {
16740 performClick();
16741 }
16742 }
16743
Dianne Hackborn63042d62011-01-26 18:56:29 -080016744 /** @hide */
16745 public void hackTurnOffWindowResizeAnim(boolean off) {
16746 mAttachInfo.mTurnOffWindowResizeAnim = off;
16747 }
Joe Malin32736f02011-01-19 16:14:20 -080016748
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016749 /**
Chet Haasea00f3862011-02-22 06:34:40 -080016750 * This method returns a ViewPropertyAnimator object, which can be used to animate
16751 * specific properties on this View.
16752 *
16753 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
16754 */
16755 public ViewPropertyAnimator animate() {
16756 if (mAnimator == null) {
16757 mAnimator = new ViewPropertyAnimator(this);
16758 }
16759 return mAnimator;
16760 }
16761
16762 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016763 * Interface definition for a callback to be invoked when a key event is
16764 * dispatched to this view. The callback will be invoked before the key
16765 * event is given to the view.
16766 */
16767 public interface OnKeyListener {
16768 /**
16769 * Called when a key is dispatched to a view. This allows listeners to
16770 * get a chance to respond before the target view.
16771 *
16772 * @param v The view the key has been dispatched to.
16773 * @param keyCode The code for the physical key that was pressed
16774 * @param event The KeyEvent object containing full information about
16775 * the event.
16776 * @return True if the listener has consumed the event, false otherwise.
16777 */
16778 boolean onKey(View v, int keyCode, KeyEvent event);
16779 }
16780
16781 /**
16782 * Interface definition for a callback to be invoked when a touch event is
16783 * dispatched to this view. The callback will be invoked before the touch
16784 * event is given to the view.
16785 */
16786 public interface OnTouchListener {
16787 /**
16788 * Called when a touch event is dispatched to a view. This allows listeners to
16789 * get a chance to respond before the target view.
16790 *
16791 * @param v The view the touch event has been dispatched to.
16792 * @param event The MotionEvent object containing full information about
16793 * the event.
16794 * @return True if the listener has consumed the event, false otherwise.
16795 */
16796 boolean onTouch(View v, MotionEvent event);
16797 }
16798
16799 /**
Jeff Brown10b62902011-06-20 16:40:37 -070016800 * Interface definition for a callback to be invoked when a hover event is
16801 * dispatched to this view. The callback will be invoked before the hover
16802 * event is given to the view.
16803 */
16804 public interface OnHoverListener {
16805 /**
16806 * Called when a hover event is dispatched to a view. This allows listeners to
16807 * get a chance to respond before the target view.
16808 *
16809 * @param v The view the hover event has been dispatched to.
16810 * @param event The MotionEvent object containing full information about
16811 * the event.
16812 * @return True if the listener has consumed the event, false otherwise.
16813 */
16814 boolean onHover(View v, MotionEvent event);
16815 }
16816
16817 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016818 * Interface definition for a callback to be invoked when a generic motion event is
16819 * dispatched to this view. The callback will be invoked before the generic motion
16820 * event is given to the view.
16821 */
16822 public interface OnGenericMotionListener {
16823 /**
16824 * Called when a generic motion event is dispatched to a view. This allows listeners to
16825 * get a chance to respond before the target view.
16826 *
16827 * @param v The view the generic motion event has been dispatched to.
16828 * @param event The MotionEvent object containing full information about
16829 * the event.
16830 * @return True if the listener has consumed the event, false otherwise.
16831 */
16832 boolean onGenericMotion(View v, MotionEvent event);
16833 }
16834
16835 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016836 * Interface definition for a callback to be invoked when a view has been clicked and held.
16837 */
16838 public interface OnLongClickListener {
16839 /**
16840 * Called when a view has been clicked and held.
16841 *
16842 * @param v The view that was clicked and held.
16843 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080016844 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016845 */
16846 boolean onLongClick(View v);
16847 }
16848
16849 /**
Chris Tate32affef2010-10-18 15:29:21 -070016850 * Interface definition for a callback to be invoked when a drag is being dispatched
16851 * to this view. The callback will be invoked before the hosting view's own
16852 * onDrag(event) method. If the listener wants to fall back to the hosting view's
16853 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070016854 *
16855 * <div class="special reference">
16856 * <h3>Developer Guides</h3>
16857 * <p>For a guide to implementing drag and drop features, read the
16858 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
16859 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070016860 */
16861 public interface OnDragListener {
16862 /**
16863 * Called when a drag event is dispatched to a view. This allows listeners
16864 * to get a chance to override base View behavior.
16865 *
Joe Malin32736f02011-01-19 16:14:20 -080016866 * @param v The View that received the drag event.
16867 * @param event The {@link android.view.DragEvent} object for the drag event.
16868 * @return {@code true} if the drag event was handled successfully, or {@code false}
16869 * if the drag event was not handled. Note that {@code false} will trigger the View
16870 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070016871 */
16872 boolean onDrag(View v, DragEvent event);
16873 }
16874
16875 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016876 * Interface definition for a callback to be invoked when the focus state of
16877 * a view changed.
16878 */
16879 public interface OnFocusChangeListener {
16880 /**
16881 * Called when the focus state of a view has changed.
16882 *
16883 * @param v The view whose state has changed.
16884 * @param hasFocus The new focus state of v.
16885 */
16886 void onFocusChange(View v, boolean hasFocus);
16887 }
16888
16889 /**
16890 * Interface definition for a callback to be invoked when a view is clicked.
16891 */
16892 public interface OnClickListener {
16893 /**
16894 * Called when a view has been clicked.
16895 *
16896 * @param v The view that was clicked.
16897 */
16898 void onClick(View v);
16899 }
16900
16901 /**
16902 * Interface definition for a callback to be invoked when the context menu
16903 * for this view is being built.
16904 */
16905 public interface OnCreateContextMenuListener {
16906 /**
16907 * Called when the context menu for this view is being built. It is not
16908 * safe to hold onto the menu after this method returns.
16909 *
16910 * @param menu The context menu that is being built
16911 * @param v The view for which the context menu is being built
16912 * @param menuInfo Extra information about the item for which the
16913 * context menu should be shown. This information will vary
16914 * depending on the class of v.
16915 */
16916 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
16917 }
16918
Joe Onorato664644d2011-01-23 17:53:23 -080016919 /**
16920 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016921 * visibility. This reports <strong>global</strong> changes to the system UI
Dianne Hackborncf675782012-05-10 15:07:24 -070016922 * state, not what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080016923 *
Philip Milne6c8ea062012-04-03 17:38:43 -070016924 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080016925 */
16926 public interface OnSystemUiVisibilityChangeListener {
16927 /**
16928 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070016929 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080016930 *
Dianne Hackborncf675782012-05-10 15:07:24 -070016931 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
16932 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, and {@link #SYSTEM_UI_FLAG_FULLSCREEN}.
16933 * This tells you the <strong>global</strong> state of these UI visibility
16934 * flags, not what your app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080016935 */
16936 public void onSystemUiVisibilityChange(int visibility);
16937 }
16938
Adam Powell4afd62b2011-02-18 15:02:18 -080016939 /**
16940 * Interface definition for a callback to be invoked when this view is attached
16941 * or detached from its window.
16942 */
16943 public interface OnAttachStateChangeListener {
16944 /**
16945 * Called when the view is attached to a window.
16946 * @param v The view that was attached
16947 */
16948 public void onViewAttachedToWindow(View v);
16949 /**
16950 * Called when the view is detached from a window.
16951 * @param v The view that was detached
16952 */
16953 public void onViewDetachedFromWindow(View v);
16954 }
16955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016956 private final class UnsetPressedState implements Runnable {
16957 public void run() {
16958 setPressed(false);
16959 }
16960 }
16961
16962 /**
16963 * Base class for derived classes that want to save and restore their own
16964 * state in {@link android.view.View#onSaveInstanceState()}.
16965 */
16966 public static class BaseSavedState extends AbsSavedState {
16967 /**
16968 * Constructor used when reading from a parcel. Reads the state of the superclass.
16969 *
16970 * @param source
16971 */
16972 public BaseSavedState(Parcel source) {
16973 super(source);
16974 }
16975
16976 /**
16977 * Constructor called by derived classes when creating their SavedState objects
16978 *
16979 * @param superState The state of the superclass of this view
16980 */
16981 public BaseSavedState(Parcelable superState) {
16982 super(superState);
16983 }
16984
16985 public static final Parcelable.Creator<BaseSavedState> CREATOR =
16986 new Parcelable.Creator<BaseSavedState>() {
16987 public BaseSavedState createFromParcel(Parcel in) {
16988 return new BaseSavedState(in);
16989 }
16990
16991 public BaseSavedState[] newArray(int size) {
16992 return new BaseSavedState[size];
16993 }
16994 };
16995 }
16996
16997 /**
16998 * A set of information given to a view when it is attached to its parent
16999 * window.
17000 */
17001 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017002 interface Callbacks {
17003 void playSoundEffect(int effectId);
17004 boolean performHapticFeedback(int effectId, boolean always);
17005 }
17006
17007 /**
17008 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
17009 * to a Handler. This class contains the target (View) to invalidate and
17010 * the coordinates of the dirty rectangle.
17011 *
17012 * For performance purposes, this class also implements a pool of up to
17013 * POOL_LIMIT objects that get reused. This reduces memory allocations
17014 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017015 */
Romain Guyd928d682009-03-31 17:52:16 -070017016 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017017 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070017018 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
17019 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070017020 public InvalidateInfo newInstance() {
17021 return new InvalidateInfo();
17022 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017023
Romain Guyd928d682009-03-31 17:52:16 -070017024 public void onAcquired(InvalidateInfo element) {
17025 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017026
Romain Guyd928d682009-03-31 17:52:16 -070017027 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070017028 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070017029 }
17030 }, POOL_LIMIT)
17031 );
17032
17033 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017034 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017035
17036 View target;
17037
17038 int left;
17039 int top;
17040 int right;
17041 int bottom;
17042
Romain Guyd928d682009-03-31 17:52:16 -070017043 public void setNextPoolable(InvalidateInfo element) {
17044 mNext = element;
17045 }
17046
17047 public InvalidateInfo getNextPoolable() {
17048 return mNext;
17049 }
17050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017051 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070017052 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017053 }
17054
17055 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070017056 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017057 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017058
17059 public boolean isPooled() {
17060 return mIsPooled;
17061 }
17062
17063 public void setPooled(boolean isPooled) {
17064 mIsPooled = isPooled;
17065 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017066 }
17067
17068 final IWindowSession mSession;
17069
17070 final IWindow mWindow;
17071
17072 final IBinder mWindowToken;
17073
17074 final Callbacks mRootCallbacks;
17075
Romain Guy59a12ca2011-06-09 17:48:21 -070017076 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080017077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017078 /**
17079 * The top view of the hierarchy.
17080 */
17081 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070017082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017083 IBinder mPanelParentWindowToken;
17084 Surface mSurface;
17085
Romain Guyb051e892010-09-28 19:09:36 -070017086 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080017087 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070017088 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080017089
Romain Guy7e4e5612012-03-05 14:37:29 -080017090 boolean mScreenOn;
17091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017092 /**
Romain Guy8506ab42009-06-11 17:35:47 -070017093 * Scale factor used by the compatibility mode
17094 */
17095 float mApplicationScale;
17096
17097 /**
17098 * Indicates whether the application is in compatibility mode
17099 */
17100 boolean mScalingRequired;
17101
17102 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017103 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080017104 */
17105 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080017106
Dianne Hackborn63042d62011-01-26 18:56:29 -080017107 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017108 * Left position of this view's window
17109 */
17110 int mWindowLeft;
17111
17112 /**
17113 * Top position of this view's window
17114 */
17115 int mWindowTop;
17116
17117 /**
Adam Powell26153a32010-11-08 15:22:27 -080017118 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070017119 */
Adam Powell26153a32010-11-08 15:22:27 -080017120 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070017121
17122 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017123 * For windows that are full-screen but using insets to layout inside
17124 * of the screen decorations, these are the current insets for the
17125 * content of the window.
17126 */
17127 final Rect mContentInsets = new Rect();
17128
17129 /**
17130 * For windows that are full-screen but using insets to layout inside
17131 * of the screen decorations, these are the current insets for the
17132 * actual visible parts of the window.
17133 */
17134 final Rect mVisibleInsets = new Rect();
17135
17136 /**
17137 * The internal insets given by this window. This value is
17138 * supplied by the client (through
17139 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
17140 * be given to the window manager when changed to be used in laying
17141 * out windows behind it.
17142 */
17143 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
17144 = new ViewTreeObserver.InternalInsetsInfo();
17145
17146 /**
17147 * All views in the window's hierarchy that serve as scroll containers,
17148 * used to determine if the window can be resized or must be panned
17149 * to adjust for a soft input area.
17150 */
17151 final ArrayList<View> mScrollContainers = new ArrayList<View>();
17152
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070017153 final KeyEvent.DispatcherState mKeyDispatchState
17154 = new KeyEvent.DispatcherState();
17155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017156 /**
17157 * Indicates whether the view's window currently has the focus.
17158 */
17159 boolean mHasWindowFocus;
17160
17161 /**
17162 * The current visibility of the window.
17163 */
17164 int mWindowVisibility;
17165
17166 /**
17167 * Indicates the time at which drawing started to occur.
17168 */
17169 long mDrawingTime;
17170
17171 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070017172 * Indicates whether or not ignoring the DIRTY_MASK flags.
17173 */
17174 boolean mIgnoreDirtyState;
17175
17176 /**
Romain Guy02ccac62011-06-24 13:20:23 -070017177 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
17178 * to avoid clearing that flag prematurely.
17179 */
17180 boolean mSetIgnoreDirtyState = false;
17181
17182 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017183 * Indicates whether the view's window is currently in touch mode.
17184 */
17185 boolean mInTouchMode;
17186
17187 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017188 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017189 * the next time it performs a traversal
17190 */
17191 boolean mRecomputeGlobalAttributes;
17192
17193 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017194 * Always report new attributes at next traversal.
17195 */
17196 boolean mForceReportNewAttributes;
17197
17198 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017199 * Set during a traveral if any views want to keep the screen on.
17200 */
17201 boolean mKeepScreenOn;
17202
17203 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017204 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
17205 */
17206 int mSystemUiVisibility;
17207
17208 /**
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070017209 * Hack to force certain system UI visibility flags to be cleared.
17210 */
17211 int mDisabledSystemUiVisibility;
17212
17213 /**
Dianne Hackborncf675782012-05-10 15:07:24 -070017214 * Last global system UI visibility reported by the window manager.
17215 */
17216 int mGlobalSystemUiVisibility;
17217
17218 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017219 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
17220 * attached.
17221 */
17222 boolean mHasSystemUiListeners;
17223
17224 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017225 * Set if the visibility of any views has changed.
17226 */
17227 boolean mViewVisibilityChanged;
17228
17229 /**
17230 * Set to true if a view has been scrolled.
17231 */
17232 boolean mViewScrollChanged;
17233
17234 /**
17235 * Global to the view hierarchy used as a temporary for dealing with
17236 * x/y points in the transparent region computations.
17237 */
17238 final int[] mTransparentLocation = new int[2];
17239
17240 /**
17241 * Global to the view hierarchy used as a temporary for dealing with
17242 * x/y points in the ViewGroup.invalidateChild implementation.
17243 */
17244 final int[] mInvalidateChildLocation = new int[2];
17245
Chet Haasec3aa3612010-06-17 08:50:37 -070017246
17247 /**
17248 * Global to the view hierarchy used as a temporary for dealing with
17249 * x/y location when view is transformed.
17250 */
17251 final float[] mTmpTransformLocation = new float[2];
17252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017253 /**
17254 * The view tree observer used to dispatch global events like
17255 * layout, pre-draw, touch mode change, etc.
17256 */
17257 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
17258
17259 /**
17260 * A Canvas used by the view hierarchy to perform bitmap caching.
17261 */
17262 Canvas mCanvas;
17263
17264 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080017265 * The view root impl.
17266 */
17267 final ViewRootImpl mViewRootImpl;
17268
17269 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070017270 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017271 * handler can be used to pump events in the UI events queue.
17272 */
17273 final Handler mHandler;
17274
17275 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017276 * Temporary for use in computing invalidate rectangles while
17277 * calling up the hierarchy.
17278 */
17279 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070017280
17281 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070017282 * Temporary for use in computing hit areas with transformed views
17283 */
17284 final RectF mTmpTransformRect = new RectF();
17285
17286 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070017287 * Temporary list for use in collecting focusable descendents of a view.
17288 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070017289 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070017290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017291 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017292 * The id of the window for accessibility purposes.
17293 */
17294 int mAccessibilityWindowId = View.NO_ID;
17295
17296 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -070017297 * Whether to ingore not exposed for accessibility Views when
17298 * reporting the view tree to accessibility services.
17299 */
17300 boolean mIncludeNotImportantViews;
17301
17302 /**
17303 * The drawable for highlighting accessibility focus.
17304 */
17305 Drawable mAccessibilityFocusDrawable;
17306
17307 /**
Philip Milne10ca24a2012-04-23 15:38:27 -070017308 * Show where the margins, bounds and layout bounds are for each view.
17309 */
Dianne Hackborna53de062012-05-08 18:53:51 -070017310 boolean mDebugLayout = SystemProperties.getBoolean(DEBUG_LAYOUT_PROPERTY, false);
Philip Milne10ca24a2012-04-23 15:38:27 -070017311
17312 /**
Romain Guyab4c4f4f2012-05-06 13:11:24 -070017313 * Point used to compute visible regions.
17314 */
17315 final Point mPoint = new Point();
17316
17317 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017318 * Creates a new set of attachment information with the specified
17319 * events handler and thread.
17320 *
17321 * @param handler the events handler the view must use
17322 */
17323 AttachInfo(IWindowSession session, IWindow window,
Jeff Browna175a5b2012-02-15 19:18:31 -080017324 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017325 mSession = session;
17326 mWindow = window;
17327 mWindowToken = window.asBinder();
Jeff Browna175a5b2012-02-15 19:18:31 -080017328 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017329 mHandler = handler;
17330 mRootCallbacks = effectPlayer;
17331 }
17332 }
17333
17334 /**
17335 * <p>ScrollabilityCache holds various fields used by a View when scrolling
17336 * is supported. This avoids keeping too many unused fields in most
17337 * instances of View.</p>
17338 */
Mike Cleronf116bf82009-09-27 19:14:12 -070017339 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080017340
Mike Cleronf116bf82009-09-27 19:14:12 -070017341 /**
17342 * Scrollbars are not visible
17343 */
17344 public static final int OFF = 0;
17345
17346 /**
17347 * Scrollbars are visible
17348 */
17349 public static final int ON = 1;
17350
17351 /**
17352 * Scrollbars are fading away
17353 */
17354 public static final int FADING = 2;
17355
17356 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080017357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017358 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070017359 public int scrollBarDefaultDelayBeforeFade;
17360 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017361
17362 public int scrollBarSize;
17363 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070017364 public float[] interpolatorValues;
17365 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017366
17367 public final Paint paint;
17368 public final Matrix matrix;
17369 public Shader shader;
17370
Mike Cleronf116bf82009-09-27 19:14:12 -070017371 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
17372
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017373 private static final float[] OPAQUE = { 255 };
17374 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080017375
Mike Cleronf116bf82009-09-27 19:14:12 -070017376 /**
17377 * When fading should start. This time moves into the future every time
17378 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
17379 */
17380 public long fadeStartTime;
17381
17382
17383 /**
17384 * The current state of the scrollbars: ON, OFF, or FADING
17385 */
17386 public int state = OFF;
17387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017388 private int mLastColor;
17389
Mike Cleronf116bf82009-09-27 19:14:12 -070017390 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017391 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
17392 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070017393 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
17394 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017395
17396 paint = new Paint();
17397 matrix = new Matrix();
17398 // use use a height of 1, and then wack the matrix each time we
17399 // actually use it.
17400 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017402 paint.setShader(shader);
17403 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070017404 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017405 }
Romain Guy8506ab42009-06-11 17:35:47 -070017406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017407 public void setFadeColor(int color) {
17408 if (color != 0 && color != mLastColor) {
17409 mLastColor = color;
17410 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070017411
Romain Guye55e1a72009-08-27 10:42:26 -070017412 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
17413 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017415 paint.setShader(shader);
17416 // Restore the default transfer mode (src_over)
17417 paint.setXfermode(null);
17418 }
17419 }
Joe Malin32736f02011-01-19 16:14:20 -080017420
Mike Cleronf116bf82009-09-27 19:14:12 -070017421 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070017422 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070017423 if (now >= fadeStartTime) {
17424
17425 // the animation fades the scrollbars out by changing
17426 // the opacity (alpha) from fully opaque to fully
17427 // transparent
17428 int nextFrame = (int) now;
17429 int framesCount = 0;
17430
17431 Interpolator interpolator = scrollBarInterpolator;
17432
17433 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017434 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070017435
17436 // End transparent
17437 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017438 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070017439
17440 state = FADING;
17441
17442 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080017443 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070017444 }
17445 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070017446 }
Mike Cleronf116bf82009-09-27 19:14:12 -070017447
Svetoslav Ganova0156172011-06-26 17:55:44 -070017448 /**
17449 * Resuable callback for sending
17450 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
17451 */
17452 private class SendViewScrolledAccessibilityEvent implements Runnable {
17453 public volatile boolean mIsPending;
17454
17455 public void run() {
17456 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
17457 mIsPending = false;
17458 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017459 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017460
17461 /**
17462 * <p>
17463 * This class represents a delegate that can be registered in a {@link View}
17464 * to enhance accessibility support via composition rather via inheritance.
17465 * It is specifically targeted to widget developers that extend basic View
17466 * classes i.e. classes in package android.view, that would like their
17467 * applications to be backwards compatible.
17468 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080017469 * <div class="special reference">
17470 * <h3>Developer Guides</h3>
17471 * <p>For more information about making applications accessible, read the
17472 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
17473 * developer guide.</p>
17474 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017475 * <p>
17476 * A scenario in which a developer would like to use an accessibility delegate
17477 * is overriding a method introduced in a later API version then the minimal API
17478 * version supported by the application. For example, the method
17479 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
17480 * in API version 4 when the accessibility APIs were first introduced. If a
17481 * developer would like his application to run on API version 4 devices (assuming
17482 * all other APIs used by the application are version 4 or lower) and take advantage
17483 * of this method, instead of overriding the method which would break the application's
17484 * backwards compatibility, he can override the corresponding method in this
17485 * delegate and register the delegate in the target View if the API version of
17486 * the system is high enough i.e. the API version is same or higher to the API
17487 * version that introduced
17488 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
17489 * </p>
17490 * <p>
17491 * Here is an example implementation:
17492 * </p>
17493 * <code><pre><p>
17494 * if (Build.VERSION.SDK_INT >= 14) {
17495 * // If the API version is equal of higher than the version in
17496 * // which onInitializeAccessibilityNodeInfo was introduced we
17497 * // register a delegate with a customized implementation.
17498 * View view = findViewById(R.id.view_id);
17499 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
17500 * public void onInitializeAccessibilityNodeInfo(View host,
17501 * AccessibilityNodeInfo info) {
17502 * // Let the default implementation populate the info.
17503 * super.onInitializeAccessibilityNodeInfo(host, info);
17504 * // Set some other information.
17505 * info.setEnabled(host.isEnabled());
17506 * }
17507 * });
17508 * }
17509 * </code></pre></p>
17510 * <p>
17511 * This delegate contains methods that correspond to the accessibility methods
17512 * in View. If a delegate has been specified the implementation in View hands
17513 * off handling to the corresponding method in this delegate. The default
17514 * implementation the delegate methods behaves exactly as the corresponding
17515 * method in View for the case of no accessibility delegate been set. Hence,
17516 * to customize the behavior of a View method, clients can override only the
17517 * corresponding delegate method without altering the behavior of the rest
17518 * accessibility related methods of the host view.
17519 * </p>
17520 */
17521 public static class AccessibilityDelegate {
17522
17523 /**
17524 * Sends an accessibility event of the given type. If accessibility is not
17525 * enabled this method has no effect.
17526 * <p>
17527 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
17528 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
17529 * been set.
17530 * </p>
17531 *
17532 * @param host The View hosting the delegate.
17533 * @param eventType The type of the event to send.
17534 *
17535 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
17536 */
17537 public void sendAccessibilityEvent(View host, int eventType) {
17538 host.sendAccessibilityEventInternal(eventType);
17539 }
17540
17541 /**
alanv8eeefef2012-05-07 16:57:53 -070017542 * Performs the specified accessibility action on the view. For
17543 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
17544 * <p>
17545 * The default implementation behaves as
17546 * {@link View#performAccessibilityAction(int, Bundle)
17547 * View#performAccessibilityAction(int, Bundle)} for the case of
17548 * no accessibility delegate been set.
17549 * </p>
17550 *
17551 * @param action The action to perform.
17552 * @return Whether the action was performed.
17553 *
17554 * @see View#performAccessibilityAction(int, Bundle)
17555 * View#performAccessibilityAction(int, Bundle)
17556 */
17557 public boolean performAccessibilityAction(View host, int action, Bundle args) {
17558 return host.performAccessibilityActionInternal(action, args);
17559 }
17560
17561 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017562 * Sends an accessibility event. This method behaves exactly as
17563 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
17564 * empty {@link AccessibilityEvent} and does not perform a check whether
17565 * accessibility is enabled.
17566 * <p>
17567 * The default implementation behaves as
17568 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17569 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
17570 * the case of no accessibility delegate been set.
17571 * </p>
17572 *
17573 * @param host The View hosting the delegate.
17574 * @param event The event to send.
17575 *
17576 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17577 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17578 */
17579 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
17580 host.sendAccessibilityEventUncheckedInternal(event);
17581 }
17582
17583 /**
17584 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
17585 * to its children for adding their text content to the event.
17586 * <p>
17587 * The default implementation behaves as
17588 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17589 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
17590 * the case of no accessibility delegate been set.
17591 * </p>
17592 *
17593 * @param host The View hosting the delegate.
17594 * @param event The event.
17595 * @return True if the event population was completed.
17596 *
17597 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17598 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17599 */
17600 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17601 return host.dispatchPopulateAccessibilityEventInternal(event);
17602 }
17603
17604 /**
17605 * Gives a chance to the host View to populate the accessibility event with its
17606 * text content.
17607 * <p>
17608 * The default implementation behaves as
17609 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
17610 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
17611 * the case of no accessibility delegate been set.
17612 * </p>
17613 *
17614 * @param host The View hosting the delegate.
17615 * @param event The accessibility event which to populate.
17616 *
17617 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
17618 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
17619 */
17620 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17621 host.onPopulateAccessibilityEventInternal(event);
17622 }
17623
17624 /**
17625 * Initializes an {@link AccessibilityEvent} with information about the
17626 * the host View which is the event source.
17627 * <p>
17628 * The default implementation behaves as
17629 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
17630 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
17631 * the case of no accessibility delegate been set.
17632 * </p>
17633 *
17634 * @param host The View hosting the delegate.
17635 * @param event The event to initialize.
17636 *
17637 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
17638 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
17639 */
17640 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
17641 host.onInitializeAccessibilityEventInternal(event);
17642 }
17643
17644 /**
17645 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
17646 * <p>
17647 * The default implementation behaves as
17648 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17649 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
17650 * the case of no accessibility delegate been set.
17651 * </p>
17652 *
17653 * @param host The View hosting the delegate.
17654 * @param info The instance to initialize.
17655 *
17656 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17657 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17658 */
17659 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
17660 host.onInitializeAccessibilityNodeInfoInternal(info);
17661 }
17662
17663 /**
17664 * Called when a child of the host View has requested sending an
17665 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
17666 * to augment the event.
17667 * <p>
17668 * The default implementation behaves as
17669 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17670 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
17671 * the case of no accessibility delegate been set.
17672 * </p>
17673 *
17674 * @param host The View hosting the delegate.
17675 * @param child The child which requests sending the event.
17676 * @param event The event to be sent.
17677 * @return True if the event should be sent
17678 *
17679 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17680 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17681 */
17682 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
17683 AccessibilityEvent event) {
17684 return host.onRequestSendAccessibilityEventInternal(child, event);
17685 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070017686
17687 /**
17688 * Gets the provider for managing a virtual view hierarchy rooted at this View
17689 * and reported to {@link android.accessibilityservice.AccessibilityService}s
17690 * that explore the window content.
17691 * <p>
17692 * The default implementation behaves as
17693 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
17694 * the case of no accessibility delegate been set.
17695 * </p>
17696 *
17697 * @return The provider.
17698 *
17699 * @see AccessibilityNodeProvider
17700 */
17701 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
17702 return null;
17703 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017704 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017705}