blob: 35b6aa63b236d5e4124ec2e8eaab4e1782d8f842 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view;
18
Christopher Tatea53146c2010-09-07 11:57:52 -070019import android.content.ClipData;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.Context;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080021import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.res.Resources;
23import android.content.res.TypedArray;
24import android.graphics.Bitmap;
Adam Powell2b342f02010-08-18 18:14:13 -070025import android.graphics.Camera;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.graphics.Canvas;
Philip Milne1557fd72012-04-04 23:41:34 -070027import android.graphics.Insets;
Mike Cleronf116bf82009-09-27 19:14:12 -070028import android.graphics.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.graphics.LinearGradient;
30import android.graphics.Matrix;
31import android.graphics.Paint;
32import android.graphics.PixelFormat;
svetoslavganov75986cf2009-05-14 22:28:01 -070033import android.graphics.Point;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.graphics.PorterDuff;
35import android.graphics.PorterDuffXfermode;
36import android.graphics.Rect;
Adam Powell6e346362010-07-23 10:18:23 -070037import android.graphics.RectF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.graphics.Region;
39import android.graphics.Shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.graphics.drawable.ColorDrawable;
41import android.graphics.drawable.Drawable;
Svetoslav Ganovaa780c12012-04-19 23:01:39 -070042import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.os.Handler;
44import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.os.Parcel;
46import android.os.Parcelable;
47import android.os.RemoteException;
48import android.os.SystemClock;
Philip Milne10ca24a2012-04-23 15:38:27 -070049import android.os.SystemProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.util.AttributeSet;
Doug Feltcb3791202011-07-07 11:57:48 -070051import android.util.FloatProperty;
52import android.util.LocaleUtil;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070054import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070055import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070056import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.util.Pools;
Doug Feltcb3791202011-07-07 11:57:48 -070058import android.util.Property;
svetoslavganov75986cf2009-05-14 22:28:01 -070059import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080060import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.view.ContextMenu.ContextMenuInfo;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -070062import android.view.AccessibilityIterators.TextSegmentIterator;
63import android.view.AccessibilityIterators.CharacterTextSegmentIterator;
64import android.view.AccessibilityIterators.WordTextSegmentIterator;
65import android.view.AccessibilityIterators.ParagraphTextSegmentIterator;
svetoslavganov75986cf2009-05-14 22:28:01 -070066import android.view.accessibility.AccessibilityEvent;
67import android.view.accessibility.AccessibilityEventSource;
68import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070069import android.view.accessibility.AccessibilityNodeInfo;
Svetoslav Ganov02107852011-10-03 17:06:56 -070070import android.view.accessibility.AccessibilityNodeProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070072import android.view.animation.AnimationUtils;
Chet Haase64a48c12012-02-13 16:33:29 -080073import android.view.animation.Transformation;
svetoslavganov75986cf2009-05-14 22:28:01 -070074import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.view.inputmethod.InputConnection;
76import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.widget.ScrollBarDrawable;
78
Romain Guy1ef3fdb2011-09-09 15:30:30 -070079import static android.os.Build.VERSION_CODES.*;
Philip Milne6c8ea062012-04-03 17:38:43 -070080import static java.lang.Math.max;
Romain Guy1ef3fdb2011-09-09 15:30:30 -070081
Doug Feltcb3791202011-07-07 11:57:48 -070082import com.android.internal.R;
83import com.android.internal.util.Predicate;
84import com.android.internal.view.menu.MenuBuilder;
85
Christopher Tatea0374192010-10-05 13:06:41 -070086import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070087import java.lang.reflect.InvocationTargetException;
88import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089import java.util.ArrayList;
90import java.util.Arrays;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070091import java.util.Locale;
Adam Powell4afd62b2011-02-18 15:02:18 -080092import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093
94/**
95 * <p>
96 * This class represents the basic building block for user interface components. A View
97 * occupies a rectangular area on the screen and is responsible for drawing and
98 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070099 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
101 * are invisible containers that hold other Views (or other ViewGroups) and define
102 * their layout properties.
103 * </p>
104 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700105 * <div class="special reference">
106 * <h3>Developer Guides</h3>
107 * <p>For information about using this class to develop your application's user interface,
108 * read the <a href="{@docRoot}guide/topics/ui/index.html">User Interface</a> developer guide.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700110 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 * <a name="Using"></a>
112 * <h3>Using Views</h3>
113 * <p>
114 * All of the views in a window are arranged in a single tree. You can add views
115 * either from code or by specifying a tree of views in one or more XML layout
116 * files. There are many specialized subclasses of views that act as controls or
117 * are capable of displaying text, images, or other content.
118 * </p>
119 * <p>
120 * Once you have created a tree of views, there are typically a few types of
121 * common operations you may wish to perform:
122 * <ul>
123 * <li><strong>Set properties:</strong> for example setting the text of a
124 * {@link android.widget.TextView}. The available properties and the methods
125 * that set them will vary among the different subclasses of views. Note that
126 * properties that are known at build time can be set in the XML layout
127 * files.</li>
128 * <li><strong>Set focus:</strong> The framework will handled moving focus in
129 * response to user input. To force focus to a specific view, call
130 * {@link #requestFocus}.</li>
131 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
132 * that will be notified when something interesting happens to the view. For
133 * example, all views will let you set a listener to be notified when the view
134 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700135 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
Philip Milne6c8ea062012-04-03 17:38:43 -0700136 * Other view subclasses offer more specialized listeners. For example, a Button
Romain Guy5c22a8c2011-05-13 11:48:45 -0700137 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700139 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 * </ul>
141 * </p>
142 * <p><em>
143 * Note: The Android framework is responsible for measuring, laying out and
144 * drawing views. You should not call methods that perform these actions on
145 * views yourself unless you are actually implementing a
146 * {@link android.view.ViewGroup}.
147 * </em></p>
148 *
149 * <a name="Lifecycle"></a>
150 * <h3>Implementing a Custom View</h3>
151 *
152 * <p>
153 * To implement a custom view, you will usually begin by providing overrides for
154 * some of the standard methods that the framework calls on all views. You do
155 * not need to override all of these methods. In fact, you can start by just
156 * overriding {@link #onDraw(android.graphics.Canvas)}.
157 * <table border="2" width="85%" align="center" cellpadding="5">
158 * <thead>
159 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
160 * </thead>
161 *
162 * <tbody>
163 * <tr>
164 * <td rowspan="2">Creation</td>
165 * <td>Constructors</td>
166 * <td>There is a form of the constructor that are called when the view
167 * is created from code and a form that is called when the view is
168 * inflated from a layout file. The second form should parse and apply
169 * any attributes defined in the layout file.
170 * </td>
171 * </tr>
172 * <tr>
173 * <td><code>{@link #onFinishInflate()}</code></td>
174 * <td>Called after a view and all of its children has been inflated
175 * from XML.</td>
176 * </tr>
177 *
178 * <tr>
179 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700180 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 * <td>Called to determine the size requirements for this view and all
182 * of its children.
183 * </td>
184 * </tr>
185 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700186 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 * <td>Called when this view should assign a size and position to all
188 * of its children.
189 * </td>
190 * </tr>
191 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700192 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 * <td>Called when the size of this view has changed.
194 * </td>
195 * </tr>
196 *
197 * <tr>
198 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700199 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 * <td>Called when the view should render its content.
201 * </td>
202 * </tr>
203 *
204 * <tr>
205 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700206 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
Jean Chalard405bc512012-05-29 19:12:34 +0900207 * <td>Called when a new hardware key event occurs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 * </td>
209 * </tr>
210 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700211 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
Jean Chalard405bc512012-05-29 19:12:34 +0900212 * <td>Called when a hardware key up event occurs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 * </td>
214 * </tr>
215 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700216 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 * <td>Called when a trackball motion event occurs.
218 * </td>
219 * </tr>
220 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700221 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 * <td>Called when a touch screen motion event occurs.
223 * </td>
224 * </tr>
225 *
226 * <tr>
227 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700228 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 * <td>Called when the view gains or loses focus.
230 * </td>
231 * </tr>
232 *
233 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700234 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 * <td>Called when the window containing the view gains or loses focus.
236 * </td>
237 * </tr>
238 *
239 * <tr>
240 * <td rowspan="3">Attaching</td>
241 * <td><code>{@link #onAttachedToWindow()}</code></td>
242 * <td>Called when the view is attached to a window.
243 * </td>
244 * </tr>
245 *
246 * <tr>
247 * <td><code>{@link #onDetachedFromWindow}</code></td>
248 * <td>Called when the view is detached from its window.
249 * </td>
250 * </tr>
251 *
252 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700253 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 * <td>Called when the visibility of the window containing the view
255 * has changed.
256 * </td>
257 * </tr>
258 * </tbody>
259 *
260 * </table>
261 * </p>
262 *
263 * <a name="IDs"></a>
264 * <h3>IDs</h3>
265 * Views may have an integer id associated with them. These ids are typically
266 * assigned in the layout XML files, and are used to find specific views within
267 * the view tree. A common pattern is to:
268 * <ul>
269 * <li>Define a Button in the layout file and assign it a unique ID.
270 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700271 * &lt;Button
272 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 * android:layout_width="wrap_content"
274 * android:layout_height="wrap_content"
275 * android:text="@string/my_button_text"/&gt;
276 * </pre></li>
277 * <li>From the onCreate method of an Activity, find the Button
278 * <pre class="prettyprint">
279 * Button myButton = (Button) findViewById(R.id.my_button);
280 * </pre></li>
281 * </ul>
282 * <p>
283 * View IDs need not be unique throughout the tree, but it is good practice to
284 * ensure that they are at least unique within the part of the tree you are
285 * searching.
286 * </p>
287 *
288 * <a name="Position"></a>
289 * <h3>Position</h3>
290 * <p>
291 * The geometry of a view is that of a rectangle. A view has a location,
292 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
293 * two dimensions, expressed as a width and a height. The unit for location
294 * and dimensions is the pixel.
295 * </p>
296 *
297 * <p>
298 * It is possible to retrieve the location of a view by invoking the methods
299 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
300 * coordinate of the rectangle representing the view. The latter returns the
301 * top, or Y, coordinate of the rectangle representing the view. These methods
302 * both return the location of the view relative to its parent. For instance,
303 * when getLeft() returns 20, that means the view is located 20 pixels to the
304 * right of the left edge of its direct parent.
305 * </p>
306 *
307 * <p>
308 * In addition, several convenience methods are offered to avoid unnecessary
309 * computations, namely {@link #getRight()} and {@link #getBottom()}.
310 * These methods return the coordinates of the right and bottom edges of the
311 * rectangle representing the view. For instance, calling {@link #getRight()}
312 * is similar to the following computation: <code>getLeft() + getWidth()</code>
313 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
314 * </p>
315 *
316 * <a name="SizePaddingMargins"></a>
317 * <h3>Size, padding and margins</h3>
318 * <p>
319 * The size of a view is expressed with a width and a height. A view actually
320 * possess two pairs of width and height values.
321 * </p>
322 *
323 * <p>
324 * The first pair is known as <em>measured width</em> and
325 * <em>measured height</em>. These dimensions define how big a view wants to be
326 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
327 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
328 * and {@link #getMeasuredHeight()}.
329 * </p>
330 *
331 * <p>
332 * The second pair is simply known as <em>width</em> and <em>height</em>, or
333 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
334 * dimensions define the actual size of the view on screen, at drawing time and
335 * after layout. These values may, but do not have to, be different from the
336 * measured width and height. The width and height can be obtained by calling
337 * {@link #getWidth()} and {@link #getHeight()}.
338 * </p>
339 *
340 * <p>
341 * To measure its dimensions, a view takes into account its padding. The padding
342 * is expressed in pixels for the left, top, right and bottom parts of the view.
343 * Padding can be used to offset the content of the view by a specific amount of
344 * pixels. For instance, a left padding of 2 will push the view's content by
345 * 2 pixels to the right of the left edge. Padding can be set using the
Fabrice Di 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 /**
Jean Chalard405bc512012-05-29 19:12:34 +09004085 * Register a callback to be invoked when a hardware key is pressed in this view.
4086 * Key presses in software input methods will generally not trigger the methods of
4087 * this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004088 * @param l the key listener to attach to this view
4089 */
4090 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004091 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004092 }
4093
4094 /**
4095 * Register a callback to be invoked when a touch event is sent to this view.
4096 * @param l the touch listener to attach to this view
4097 */
4098 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004099 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004100 }
4101
4102 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004103 * Register a callback to be invoked when a generic motion event is sent to this view.
4104 * @param l the generic motion listener to attach to this view
4105 */
4106 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004107 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08004108 }
4109
4110 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07004111 * Register a callback to be invoked when a hover event is sent to this view.
4112 * @param l the hover listener to attach to this view
4113 */
4114 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004115 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07004116 }
4117
4118 /**
Joe Malin32736f02011-01-19 16:14:20 -08004119 * Register a drag event listener callback object for this View. The parameter is
4120 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
4121 * View, the system calls the
4122 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
4123 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07004124 */
4125 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004126 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07004127 }
4128
4129 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07004130 * Give this view focus. This will cause
4131 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004132 *
4133 * Note: this does not check whether this {@link View} should get focus, it just
4134 * gives it focus no matter what. It should only be called internally by framework
4135 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
4136 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004137 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
4138 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004139 * focus moved when requestFocus() is called. It may not always
4140 * apply, in which case use the default View.FOCUS_DOWN.
4141 * @param previouslyFocusedRect The rectangle of the view that had focus
4142 * prior in this View's coordinate system.
4143 */
4144 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
4145 if (DBG) {
4146 System.out.println(this + " requestFocus()");
4147 }
4148
4149 if ((mPrivateFlags & FOCUSED) == 0) {
4150 mPrivateFlags |= FOCUSED;
4151
4152 if (mParent != null) {
4153 mParent.requestChildFocus(this, this);
4154 }
4155
4156 onFocusChanged(true, direction, previouslyFocusedRect);
4157 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004158
4159 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4160 notifyAccessibilityStateChanged();
4161 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004162 }
4163 }
4164
4165 /**
4166 * Request that a rectangle of this view be visible on the screen,
4167 * scrolling if necessary just enough.
4168 *
4169 * <p>A View should call this if it maintains some notion of which part
4170 * of its content is interesting. For example, a text editing view
4171 * should call this when its cursor moves.
4172 *
4173 * @param rectangle The rectangle.
4174 * @return Whether any parent scrolled.
4175 */
4176 public boolean requestRectangleOnScreen(Rect rectangle) {
4177 return requestRectangleOnScreen(rectangle, false);
4178 }
4179
4180 /**
4181 * Request that a rectangle of this view be visible on the screen,
4182 * scrolling if necessary just enough.
4183 *
4184 * <p>A View should call this if it maintains some notion of which part
4185 * of its content is interesting. For example, a text editing view
4186 * should call this when its cursor moves.
4187 *
4188 * <p>When <code>immediate</code> is set to true, scrolling will not be
4189 * animated.
4190 *
4191 * @param rectangle The rectangle.
4192 * @param immediate True to forbid animated scrolling, false otherwise
4193 * @return Whether any parent scrolled.
4194 */
4195 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
4196 View child = this;
4197 ViewParent parent = mParent;
4198 boolean scrolled = false;
4199 while (parent != null) {
4200 scrolled |= parent.requestChildRectangleOnScreen(child,
4201 rectangle, immediate);
4202
4203 // offset rect so next call has the rectangle in the
4204 // coordinate system of its direct child.
4205 rectangle.offset(child.getLeft(), child.getTop());
4206 rectangle.offset(-child.getScrollX(), -child.getScrollY());
4207
4208 if (!(parent instanceof View)) {
4209 break;
4210 }
Romain Guy8506ab42009-06-11 17:35:47 -07004211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004212 child = (View) parent;
4213 parent = child.getParent();
4214 }
4215 return scrolled;
4216 }
4217
4218 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004219 * Called when this view wants to give up focus. If focus is cleared
4220 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
4221 * <p>
4222 * <strong>Note:</strong> When a View clears focus the framework is trying
4223 * to give focus to the first focusable View from the top. Hence, if this
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004224 * View is the first from the top that can take focus, then all callbacks
4225 * related to clearing focus will be invoked after wich the framework will
4226 * give focus to this view.
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004227 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004228 */
4229 public void clearFocus() {
4230 if (DBG) {
4231 System.out.println(this + " clearFocus()");
4232 }
4233
4234 if ((mPrivateFlags & FOCUSED) != 0) {
4235 mPrivateFlags &= ~FOCUSED;
4236
4237 if (mParent != null) {
4238 mParent.clearChildFocus(this);
4239 }
4240
4241 onFocusChanged(false, 0, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004243 refreshDrawableState();
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004244
4245 ensureInputFocusOnFirstFocusable();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004246
4247 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4248 notifyAccessibilityStateChanged();
4249 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004250 }
4251 }
4252
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004253 void ensureInputFocusOnFirstFocusable() {
4254 View root = getRootView();
4255 if (root != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004256 root.requestFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004257 }
4258 }
4259
4260 /**
4261 * Called internally by the view system when a new view is getting focus.
4262 * This is what clears the old focus.
4263 */
4264 void unFocus() {
4265 if (DBG) {
4266 System.out.println(this + " unFocus()");
4267 }
4268
4269 if ((mPrivateFlags & FOCUSED) != 0) {
4270 mPrivateFlags &= ~FOCUSED;
4271
4272 onFocusChanged(false, 0, null);
4273 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004274
4275 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4276 notifyAccessibilityStateChanged();
4277 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004278 }
4279 }
4280
4281 /**
4282 * Returns true if this view has focus iteself, or is the ancestor of the
4283 * view that has focus.
4284 *
4285 * @return True if this view has or contains focus, false otherwise.
4286 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004287 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004288 public boolean hasFocus() {
4289 return (mPrivateFlags & FOCUSED) != 0;
4290 }
4291
4292 /**
4293 * Returns true if this view is focusable or if it contains a reachable View
4294 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
4295 * is a View whose parents do not block descendants focus.
4296 *
4297 * Only {@link #VISIBLE} views are considered focusable.
4298 *
4299 * @return True if the view is focusable or if the view contains a focusable
4300 * View, false otherwise.
4301 *
4302 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
4303 */
4304 public boolean hasFocusable() {
4305 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4306 }
4307
4308 /**
4309 * Called by the view system when the focus state of this view changes.
4310 * When the focus change event is caused by directional navigation, direction
4311 * and previouslyFocusedRect provide insight into where the focus is coming from.
4312 * When overriding, be sure to call up through to the super class so that
4313 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004314 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004315 * @param gainFocus True if the View has focus; false otherwise.
4316 * @param direction The direction focus has moved when requestFocus()
4317 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004318 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4319 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4320 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004321 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4322 * system, of the previously focused view. If applicable, this will be
4323 * passed in as finer grained information about where the focus is coming
4324 * from (in addition to direction). Will be <code>null</code> otherwise.
4325 */
4326 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004327 if (gainFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004328 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4329 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004330 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004331 }
4332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004333 InputMethodManager imm = InputMethodManager.peekInstance();
4334 if (!gainFocus) {
4335 if (isPressed()) {
4336 setPressed(false);
4337 }
4338 if (imm != null && mAttachInfo != null
4339 && mAttachInfo.mHasWindowFocus) {
4340 imm.focusOut(this);
4341 }
Romain Guya2431d02009-04-30 16:30:00 -07004342 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004343 } else if (imm != null && mAttachInfo != null
4344 && mAttachInfo.mHasWindowFocus) {
4345 imm.focusIn(this);
4346 }
Romain Guy8506ab42009-06-11 17:35:47 -07004347
Romain Guy0fd89bf2011-01-26 15:41:30 -08004348 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004349 ListenerInfo li = mListenerInfo;
4350 if (li != null && li.mOnFocusChangeListener != null) {
4351 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004352 }
Joe Malin32736f02011-01-19 16:14:20 -08004353
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004354 if (mAttachInfo != null) {
4355 mAttachInfo.mKeyDispatchState.reset(this);
4356 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004357 }
4358
4359 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004360 * Sends an accessibility event of the given type. If accessiiblity is
4361 * not enabled this method has no effect. The default implementation calls
4362 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4363 * to populate information about the event source (this View), then calls
4364 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4365 * populate the text content of the event source including its descendants,
4366 * and last calls
4367 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4368 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004369 * <p>
4370 * If an {@link AccessibilityDelegate} has been specified via calling
4371 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4372 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4373 * responsible for handling this call.
4374 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004375 *
Scott Mainb303d832011-10-12 16:45:18 -07004376 * @param eventType The type of the event to send, as defined by several types from
4377 * {@link android.view.accessibility.AccessibilityEvent}, such as
4378 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4379 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004380 *
4381 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4382 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4383 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004384 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004385 */
4386 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004387 if (mAccessibilityDelegate != null) {
4388 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4389 } else {
4390 sendAccessibilityEventInternal(eventType);
4391 }
4392 }
4393
4394 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004395 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4396 * {@link AccessibilityEvent} to make an announcement which is related to some
4397 * sort of a context change for which none of the events representing UI transitions
4398 * is a good fit. For example, announcing a new page in a book. If accessibility
4399 * is not enabled this method does nothing.
4400 *
4401 * @param text The announcement text.
4402 */
4403 public void announceForAccessibility(CharSequence text) {
4404 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4405 AccessibilityEvent event = AccessibilityEvent.obtain(
4406 AccessibilityEvent.TYPE_ANNOUNCEMENT);
4407 event.getText().add(text);
4408 sendAccessibilityEventUnchecked(event);
4409 }
4410 }
4411
4412 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004413 * @see #sendAccessibilityEvent(int)
4414 *
4415 * Note: Called from the default {@link AccessibilityDelegate}.
4416 */
4417 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004418 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4419 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4420 }
4421 }
4422
4423 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004424 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4425 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004426 * perform a check whether accessibility is enabled.
4427 * <p>
4428 * If an {@link AccessibilityDelegate} has been specified via calling
4429 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4430 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4431 * is responsible for handling this call.
4432 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004433 *
4434 * @param event The event to send.
4435 *
4436 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004437 */
4438 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004439 if (mAccessibilityDelegate != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004440 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004441 } else {
4442 sendAccessibilityEventUncheckedInternal(event);
4443 }
4444 }
4445
4446 /**
4447 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4448 *
4449 * Note: Called from the default {@link AccessibilityDelegate}.
4450 */
4451 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004452 if (!isShown()) {
4453 return;
4454 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004455 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004456 // Only a subset of accessibility events populates text content.
4457 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4458 dispatchPopulateAccessibilityEvent(event);
4459 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07004460 // Intercept accessibility focus events fired by virtual nodes to keep
4461 // track of accessibility focus position in such nodes.
4462 final int eventType = event.getEventType();
4463 switch (eventType) {
4464 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED: {
4465 final long virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
4466 event.getSourceNodeId());
4467 if (virtualNodeId != AccessibilityNodeInfo.UNDEFINED) {
4468 ViewRootImpl viewRootImpl = getViewRootImpl();
4469 if (viewRootImpl != null) {
4470 viewRootImpl.setAccessibilityFocusedHost(this);
4471 }
4472 }
4473 } break;
4474 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED: {
4475 final long virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
4476 event.getSourceNodeId());
4477 if (virtualNodeId != AccessibilityNodeInfo.UNDEFINED) {
4478 ViewRootImpl viewRootImpl = getViewRootImpl();
4479 if (viewRootImpl != null) {
4480 viewRootImpl.setAccessibilityFocusedHost(null);
4481 }
4482 }
4483 } break;
4484 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004485 // In the beginning we called #isShown(), so we know that getParent() is not null.
4486 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004487 }
4488
4489 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004490 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4491 * to its children for adding their text content to the event. Note that the
4492 * event text is populated in a separate dispatch path since we add to the
4493 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004494 * A typical implementation will call
4495 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4496 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4497 * on each child. Override this method if custom population of the event text
4498 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004499 * <p>
4500 * If an {@link AccessibilityDelegate} has been specified via calling
4501 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4502 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4503 * is responsible for handling this call.
4504 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004505 * <p>
4506 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4507 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4508 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004509 *
4510 * @param event The event.
4511 *
4512 * @return True if the event population was completed.
4513 */
4514 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004515 if (mAccessibilityDelegate != null) {
4516 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4517 } else {
4518 return dispatchPopulateAccessibilityEventInternal(event);
4519 }
4520 }
4521
4522 /**
4523 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4524 *
4525 * Note: Called from the default {@link AccessibilityDelegate}.
4526 */
4527 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004528 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004529 return false;
4530 }
4531
4532 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004533 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004534 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004535 * text content. While this method is free to modify event
4536 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004537 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4538 * <p>
4539 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004540 * to the text added by the super implementation:
4541 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004542 * super.onPopulateAccessibilityEvent(event);
4543 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4544 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4545 * mCurrentDate.getTimeInMillis(), flags);
4546 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004547 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004548 * <p>
4549 * If an {@link AccessibilityDelegate} has been specified via calling
4550 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4551 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4552 * is responsible for handling this call.
4553 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004554 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4555 * information to the event, in case the default implementation has basic information to add.
4556 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004557 *
4558 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004559 *
4560 * @see #sendAccessibilityEvent(int)
4561 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004562 */
4563 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004564 if (mAccessibilityDelegate != null) {
4565 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4566 } else {
4567 onPopulateAccessibilityEventInternal(event);
4568 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004569 }
4570
4571 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004572 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4573 *
4574 * Note: Called from the default {@link AccessibilityDelegate}.
4575 */
4576 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4577
4578 }
4579
4580 /**
4581 * Initializes an {@link AccessibilityEvent} with information about
4582 * this View which is the event source. In other words, the source of
4583 * an accessibility event is the view whose state change triggered firing
4584 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004585 * <p>
4586 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004587 * to properties set by the super implementation:
4588 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4589 * super.onInitializeAccessibilityEvent(event);
4590 * event.setPassword(true);
4591 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004592 * <p>
4593 * If an {@link AccessibilityDelegate} has been specified via calling
4594 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4595 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4596 * is responsible for handling this call.
4597 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004598 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4599 * information to the event, in case the default implementation has basic information to add.
4600 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004601 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004602 *
4603 * @see #sendAccessibilityEvent(int)
4604 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4605 */
4606 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004607 if (mAccessibilityDelegate != null) {
4608 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4609 } else {
4610 onInitializeAccessibilityEventInternal(event);
4611 }
4612 }
4613
4614 /**
4615 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4616 *
4617 * Note: Called from the default {@link AccessibilityDelegate}.
4618 */
4619 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004620 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004621 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004622 event.setPackageName(getContext().getPackageName());
4623 event.setEnabled(isEnabled());
4624 event.setContentDescription(mContentDescription);
4625
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004626 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004627 ArrayList<View> focusablesTempList = mAttachInfo.mTempArrayList;
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004628 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4629 FOCUSABLES_ALL);
4630 event.setItemCount(focusablesTempList.size());
4631 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4632 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004633 }
4634 }
4635
4636 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004637 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4638 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4639 * This method is responsible for obtaining an accessibility node info from a
4640 * pool of reusable instances and calling
4641 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4642 * initialize the former.
4643 * <p>
4644 * Note: The client is responsible for recycling the obtained instance by calling
4645 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4646 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004647 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004648 * @return A populated {@link AccessibilityNodeInfo}.
4649 *
4650 * @see AccessibilityNodeInfo
4651 */
4652 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004653 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4654 if (provider != null) {
4655 return provider.createAccessibilityNodeInfo(View.NO_ID);
4656 } else {
4657 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4658 onInitializeAccessibilityNodeInfo(info);
4659 return info;
4660 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004661 }
4662
4663 /**
4664 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4665 * The base implementation sets:
4666 * <ul>
4667 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004668 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4669 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004670 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4671 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4672 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4673 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4674 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4675 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4676 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4677 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4678 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4679 * </ul>
4680 * <p>
4681 * Subclasses should override this method, call the super implementation,
4682 * and set additional attributes.
4683 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004684 * <p>
4685 * If an {@link AccessibilityDelegate} has been specified via calling
4686 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4687 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4688 * is responsible for handling this call.
4689 * </p>
4690 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004691 * @param info The instance to initialize.
4692 */
4693 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004694 if (mAccessibilityDelegate != null) {
4695 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4696 } else {
4697 onInitializeAccessibilityNodeInfoInternal(info);
4698 }
4699 }
4700
4701 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004702 * Gets the location of this view in screen coordintates.
4703 *
4704 * @param outRect The output location
4705 */
4706 private void getBoundsOnScreen(Rect outRect) {
4707 if (mAttachInfo == null) {
4708 return;
4709 }
4710
4711 RectF position = mAttachInfo.mTmpTransformRect;
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004712 position.set(0, 0, mRight - mLeft, mBottom - mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004713
4714 if (!hasIdentityMatrix()) {
4715 getMatrix().mapRect(position);
4716 }
4717
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004718 position.offset(mLeft, mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004719
4720 ViewParent parent = mParent;
4721 while (parent instanceof View) {
4722 View parentView = (View) parent;
4723
4724 position.offset(-parentView.mScrollX, -parentView.mScrollY);
4725
4726 if (!parentView.hasIdentityMatrix()) {
4727 parentView.getMatrix().mapRect(position);
4728 }
4729
4730 position.offset(parentView.mLeft, parentView.mTop);
4731
4732 parent = parentView.mParent;
4733 }
4734
4735 if (parent instanceof ViewRootImpl) {
4736 ViewRootImpl viewRootImpl = (ViewRootImpl) parent;
4737 position.offset(0, -viewRootImpl.mCurScrollY);
4738 }
4739
4740 position.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
4741
4742 outRect.set((int) (position.left + 0.5f), (int) (position.top + 0.5f),
4743 (int) (position.right + 0.5f), (int) (position.bottom + 0.5f));
4744 }
4745
4746 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004747 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4748 *
4749 * Note: Called from the default {@link AccessibilityDelegate}.
4750 */
4751 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004752 Rect bounds = mAttachInfo.mTmpInvalRect;
4753 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004754 info.setBoundsInParent(bounds);
4755
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004756 getBoundsOnScreen(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004757 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004758
Svetoslav Ganovc406be92012-05-11 16:12:32 -07004759 ViewParent parent = getParentForAccessibility();
4760 if (parent instanceof View) {
4761 info.setParent((View) parent);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004762 }
4763
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004764 info.setVisibleToUser(isVisibleToUser());
4765
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004766 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004767 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004768 info.setContentDescription(getContentDescription());
4769
4770 info.setEnabled(isEnabled());
4771 info.setClickable(isClickable());
4772 info.setFocusable(isFocusable());
4773 info.setFocused(isFocused());
Svetoslav Ganov42138042012-03-20 11:51:39 -07004774 info.setAccessibilityFocused(isAccessibilityFocused());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004775 info.setSelected(isSelected());
4776 info.setLongClickable(isLongClickable());
4777
4778 // TODO: These make sense only if we are in an AdapterView but all
4779 // views can be selected. Maybe from accessiiblity perspective
4780 // we should report as selectable view in an AdapterView.
4781 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4782 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4783
4784 if (isFocusable()) {
4785 if (isFocused()) {
4786 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4787 } else {
4788 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4789 }
4790 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004791
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07004792 if (!isAccessibilityFocused()) {
4793 info.addAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
4794 } else {
4795 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
4796 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004797
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07004798 if (isClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004799 info.addAction(AccessibilityNodeInfo.ACTION_CLICK);
4800 }
4801
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07004802 if (isLongClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004803 info.addAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
4804 }
4805
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004806 if (mContentDescription != null && mContentDescription.length() > 0) {
Svetoslav Ganov2b435aa2012-05-04 17:16:37 -07004807 info.addAction(AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY);
4808 info.addAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY);
4809 info.setMovementGranularities(AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004810 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD
4811 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH);
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004812 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004813 }
4814
4815 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004816 * Computes whether this view is visible to the user. Such a view is
4817 * attached, visible, all its predecessors are visible, it is not clipped
4818 * entirely by its predecessors, and has an alpha greater than zero.
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004819 *
4820 * @return Whether the view is visible on the screen.
Guang Zhu0d607fb2012-05-11 19:34:56 -07004821 *
4822 * @hide
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004823 */
Guang Zhu0d607fb2012-05-11 19:34:56 -07004824 protected boolean isVisibleToUser() {
4825 return isVisibleToUser(null);
4826 }
4827
4828 /**
4829 * Computes whether the given portion of this view is visible to the user. Such a view is
4830 * attached, visible, all its predecessors are visible, has an alpha greater than zero, and
4831 * the specified portion is not clipped entirely by its predecessors.
4832 *
4833 * @param boundInView the portion of the view to test; coordinates should be relative; may be
4834 * <code>null</code>, and the entire view will be tested in this case.
4835 * When <code>true</code> is returned by the function, the actual visible
4836 * region will be stored in this parameter; that is, if boundInView is fully
4837 * contained within the view, no modification will be made, otherwise regions
4838 * outside of the visible area of the view will be clipped.
4839 *
4840 * @return Whether the specified portion of the view is visible on the screen.
4841 *
4842 * @hide
4843 */
4844 protected boolean isVisibleToUser(Rect boundInView) {
4845 Rect visibleRect = mAttachInfo.mTmpInvalRect;
4846 Point offset = mAttachInfo.mPoint;
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004847 // The first two checks are made also made by isShown() which
4848 // however traverses the tree up to the parent to catch that.
4849 // Therefore, we do some fail fast check to minimize the up
4850 // tree traversal.
Guang Zhu0d607fb2012-05-11 19:34:56 -07004851 boolean isVisible = mAttachInfo != null
4852 && mAttachInfo.mWindowVisibility == View.VISIBLE
4853 && getAlpha() > 0
4854 && isShown()
4855 && getGlobalVisibleRect(visibleRect, offset);
4856 if (isVisible && boundInView != null) {
4857 visibleRect.offset(-offset.x, -offset.y);
4858 isVisible &= boundInView.intersect(visibleRect);
4859 }
4860 return isVisible;
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004861 }
4862
4863 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004864 * Sets a delegate for implementing accessibility support via compositon as
4865 * opposed to inheritance. The delegate's primary use is for implementing
4866 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4867 *
4868 * @param delegate The delegate instance.
4869 *
4870 * @see AccessibilityDelegate
4871 */
4872 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4873 mAccessibilityDelegate = delegate;
4874 }
4875
4876 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004877 * Gets the provider for managing a virtual view hierarchy rooted at this View
4878 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4879 * that explore the window content.
4880 * <p>
4881 * If this method returns an instance, this instance is responsible for managing
4882 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4883 * View including the one representing the View itself. Similarly the returned
4884 * instance is responsible for performing accessibility actions on any virtual
4885 * view or the root view itself.
4886 * </p>
4887 * <p>
4888 * If an {@link AccessibilityDelegate} has been specified via calling
4889 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4890 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4891 * is responsible for handling this call.
4892 * </p>
4893 *
4894 * @return The provider.
4895 *
4896 * @see AccessibilityNodeProvider
4897 */
4898 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4899 if (mAccessibilityDelegate != null) {
4900 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4901 } else {
4902 return null;
4903 }
4904 }
4905
4906 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004907 * Gets the unique identifier of this view on the screen for accessibility purposes.
4908 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4909 *
4910 * @return The view accessibility id.
4911 *
4912 * @hide
4913 */
4914 public int getAccessibilityViewId() {
4915 if (mAccessibilityViewId == NO_ID) {
4916 mAccessibilityViewId = sNextAccessibilityViewId++;
4917 }
4918 return mAccessibilityViewId;
4919 }
4920
4921 /**
4922 * Gets the unique identifier of the window in which this View reseides.
4923 *
4924 * @return The window accessibility id.
4925 *
4926 * @hide
4927 */
4928 public int getAccessibilityWindowId() {
4929 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4930 }
4931
4932 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004933 * Gets the {@link View} description. It briefly describes the view and is
4934 * primarily used for accessibility support. Set this property to enable
4935 * better accessibility support for your application. This is especially
4936 * true for views that do not have textual representation (For example,
4937 * ImageButton).
4938 *
Svetoslav Ganov42138042012-03-20 11:51:39 -07004939 * @return The content description.
svetoslavganov75986cf2009-05-14 22:28:01 -07004940 *
4941 * @attr ref android.R.styleable#View_contentDescription
4942 */
Svetoslav Ganov42138042012-03-20 11:51:39 -07004943 @ViewDebug.ExportedProperty(category = "accessibility")
svetoslavganov75986cf2009-05-14 22:28:01 -07004944 public CharSequence getContentDescription() {
4945 return mContentDescription;
4946 }
4947
4948 /**
4949 * Sets the {@link View} description. It briefly describes the view and is
4950 * primarily used for accessibility support. Set this property to enable
4951 * better accessibility support for your application. This is especially
4952 * true for views that do not have textual representation (For example,
4953 * ImageButton).
4954 *
4955 * @param contentDescription The content description.
4956 *
4957 * @attr ref android.R.styleable#View_contentDescription
4958 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07004959 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07004960 public void setContentDescription(CharSequence contentDescription) {
4961 mContentDescription = contentDescription;
4962 }
4963
4964 /**
Romain Guya2431d02009-04-30 16:30:00 -07004965 * Invoked whenever this view loses focus, either by losing window focus or by losing
4966 * focus within its window. This method can be used to clear any state tied to the
4967 * focus. For instance, if a button is held pressed with the trackball and the window
4968 * loses focus, this method can be used to cancel the press.
4969 *
4970 * Subclasses of View overriding this method should always call super.onFocusLost().
4971 *
4972 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004973 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004974 *
4975 * @hide pending API council approval
4976 */
4977 protected void onFocusLost() {
4978 resetPressedState();
4979 }
4980
4981 private void resetPressedState() {
4982 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4983 return;
4984 }
4985
4986 if (isPressed()) {
4987 setPressed(false);
4988
4989 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004990 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004991 }
4992 }
4993 }
4994
4995 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004996 * Returns true if this view has focus
4997 *
4998 * @return True if this view has focus, false otherwise.
4999 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005000 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005001 public boolean isFocused() {
5002 return (mPrivateFlags & FOCUSED) != 0;
5003 }
5004
5005 /**
5006 * Find the view in the hierarchy rooted at this view that currently has
5007 * focus.
5008 *
5009 * @return The view that currently has focus, or null if no focused view can
5010 * be found.
5011 */
5012 public View findFocus() {
5013 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
5014 }
5015
5016 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07005017 * Indicates whether this view is one of the set of scrollable containers in
5018 * its window.
5019 *
5020 * @return whether this view is one of the set of scrollable containers in
5021 * its window
5022 *
5023 * @attr ref android.R.styleable#View_isScrollContainer
5024 */
5025 public boolean isScrollContainer() {
5026 return (mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0;
5027 }
5028
5029 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005030 * Change whether this view is one of the set of scrollable containers in
5031 * its window. This will be used to determine whether the window can
5032 * resize or must pan when a soft input area is open -- scrollable
5033 * containers allow the window to use resize mode since the container
5034 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07005035 *
5036 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005037 */
5038 public void setScrollContainer(boolean isScrollContainer) {
5039 if (isScrollContainer) {
5040 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
5041 mAttachInfo.mScrollContainers.add(this);
5042 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
5043 }
5044 mPrivateFlags |= SCROLL_CONTAINER;
5045 } else {
5046 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
5047 mAttachInfo.mScrollContainers.remove(this);
5048 }
5049 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
5050 }
5051 }
5052
5053 /**
5054 * Returns the quality of the drawing cache.
5055 *
5056 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5057 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5058 *
5059 * @see #setDrawingCacheQuality(int)
5060 * @see #setDrawingCacheEnabled(boolean)
5061 * @see #isDrawingCacheEnabled()
5062 *
5063 * @attr ref android.R.styleable#View_drawingCacheQuality
5064 */
5065 public int getDrawingCacheQuality() {
5066 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
5067 }
5068
5069 /**
5070 * Set the drawing cache quality of this view. This value is used only when the
5071 * drawing cache is enabled
5072 *
5073 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5074 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5075 *
5076 * @see #getDrawingCacheQuality()
5077 * @see #setDrawingCacheEnabled(boolean)
5078 * @see #isDrawingCacheEnabled()
5079 *
5080 * @attr ref android.R.styleable#View_drawingCacheQuality
5081 */
5082 public void setDrawingCacheQuality(int quality) {
5083 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
5084 }
5085
5086 /**
5087 * Returns whether the screen should remain on, corresponding to the current
5088 * value of {@link #KEEP_SCREEN_ON}.
5089 *
5090 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
5091 *
5092 * @see #setKeepScreenOn(boolean)
5093 *
5094 * @attr ref android.R.styleable#View_keepScreenOn
5095 */
5096 public boolean getKeepScreenOn() {
5097 return (mViewFlags & KEEP_SCREEN_ON) != 0;
5098 }
5099
5100 /**
5101 * Controls whether the screen should remain on, modifying the
5102 * value of {@link #KEEP_SCREEN_ON}.
5103 *
5104 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
5105 *
5106 * @see #getKeepScreenOn()
5107 *
5108 * @attr ref android.R.styleable#View_keepScreenOn
5109 */
5110 public void setKeepScreenOn(boolean keepScreenOn) {
5111 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
5112 }
5113
5114 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005115 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5116 * @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 -08005117 *
5118 * @attr ref android.R.styleable#View_nextFocusLeft
5119 */
5120 public int getNextFocusLeftId() {
5121 return mNextFocusLeftId;
5122 }
5123
5124 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005125 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5126 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
5127 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005128 *
5129 * @attr ref android.R.styleable#View_nextFocusLeft
5130 */
5131 public void setNextFocusLeftId(int nextFocusLeftId) {
5132 mNextFocusLeftId = nextFocusLeftId;
5133 }
5134
5135 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005136 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5137 * @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 -08005138 *
5139 * @attr ref android.R.styleable#View_nextFocusRight
5140 */
5141 public int getNextFocusRightId() {
5142 return mNextFocusRightId;
5143 }
5144
5145 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005146 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5147 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
5148 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005149 *
5150 * @attr ref android.R.styleable#View_nextFocusRight
5151 */
5152 public void setNextFocusRightId(int nextFocusRightId) {
5153 mNextFocusRightId = nextFocusRightId;
5154 }
5155
5156 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005157 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5158 * @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 -08005159 *
5160 * @attr ref android.R.styleable#View_nextFocusUp
5161 */
5162 public int getNextFocusUpId() {
5163 return mNextFocusUpId;
5164 }
5165
5166 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005167 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5168 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
5169 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005170 *
5171 * @attr ref android.R.styleable#View_nextFocusUp
5172 */
5173 public void setNextFocusUpId(int nextFocusUpId) {
5174 mNextFocusUpId = nextFocusUpId;
5175 }
5176
5177 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005178 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5179 * @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 -08005180 *
5181 * @attr ref android.R.styleable#View_nextFocusDown
5182 */
5183 public int getNextFocusDownId() {
5184 return mNextFocusDownId;
5185 }
5186
5187 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005188 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5189 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
5190 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005191 *
5192 * @attr ref android.R.styleable#View_nextFocusDown
5193 */
5194 public void setNextFocusDownId(int nextFocusDownId) {
5195 mNextFocusDownId = nextFocusDownId;
5196 }
5197
5198 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005199 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5200 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
5201 *
5202 * @attr ref android.R.styleable#View_nextFocusForward
5203 */
5204 public int getNextFocusForwardId() {
5205 return mNextFocusForwardId;
5206 }
5207
5208 /**
5209 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5210 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
5211 * decide automatically.
5212 *
5213 * @attr ref android.R.styleable#View_nextFocusForward
5214 */
5215 public void setNextFocusForwardId(int nextFocusForwardId) {
5216 mNextFocusForwardId = nextFocusForwardId;
5217 }
5218
5219 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005220 * Returns the visibility of this view and all of its ancestors
5221 *
5222 * @return True if this view and all of its ancestors are {@link #VISIBLE}
5223 */
5224 public boolean isShown() {
5225 View current = this;
5226 //noinspection ConstantConditions
5227 do {
5228 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5229 return false;
5230 }
5231 ViewParent parent = current.mParent;
5232 if (parent == null) {
5233 return false; // We are not attached to the view root
5234 }
5235 if (!(parent instanceof View)) {
5236 return true;
5237 }
5238 current = (View) parent;
5239 } while (current != null);
5240
5241 return false;
5242 }
5243
5244 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005245 * Called by the view hierarchy when the content insets for a window have
5246 * changed, to allow it to adjust its content to fit within those windows.
5247 * The content insets tell you the space that the status bar, input method,
5248 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005249 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005250 * <p>You do not normally need to deal with this function, since the default
5251 * window decoration given to applications takes care of applying it to the
5252 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
5253 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
5254 * and your content can be placed under those system elements. You can then
5255 * use this method within your view hierarchy if you have parts of your UI
5256 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005257 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005258 * <p>The default implementation of this method simply applies the content
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005259 * inset's to the view's padding, consuming that content (modifying the
5260 * insets to be 0), and returning true. This behavior is off by default, but can
5261 * be enabled through {@link #setFitsSystemWindows(boolean)}.
5262 *
5263 * <p>This function's traversal down the hierarchy is depth-first. The same content
5264 * insets object is propagated down the hierarchy, so any changes made to it will
5265 * be seen by all following views (including potentially ones above in
5266 * the hierarchy since this is a depth-first traversal). The first view
5267 * that returns true will abort the entire traversal.
5268 *
5269 * <p>The default implementation works well for a situation where it is
5270 * used with a container that covers the entire window, allowing it to
5271 * apply the appropriate insets to its content on all edges. If you need
5272 * a more complicated layout (such as two different views fitting system
5273 * windows, one on the top of the window, and one on the bottom),
5274 * you can override the method and handle the insets however you would like.
5275 * Note that the insets provided by the framework are always relative to the
5276 * far edges of the window, not accounting for the location of the called view
5277 * within that window. (In fact when this method is called you do not yet know
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005278 * where the layout will place the view, as it is done before layout happens.)
5279 *
5280 * <p>Note: unlike many View methods, there is no dispatch phase to this
5281 * call. If you are overriding it in a ViewGroup and want to allow the
5282 * call to continue to your children, you must be sure to call the super
5283 * implementation.
5284 *
Dianne Hackborncf675782012-05-10 15:07:24 -07005285 * <p>Here is a sample layout that makes use of fitting system windows
5286 * to have controls for a video view placed inside of the window decorations
5287 * that it hides and shows. This can be used with code like the second
5288 * sample (video player) shown in {@link #setSystemUiVisibility(int)}.
5289 *
5290 * {@sample development/samples/ApiDemos/res/layout/video_player.xml complete}
5291 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005292 * @param insets Current content insets of the window. Prior to
5293 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
5294 * the insets or else you and Android will be unhappy.
5295 *
5296 * @return Return true if this view applied the insets and it should not
5297 * continue propagating further down the hierarchy, false otherwise.
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005298 * @see #getFitsSystemWindows()
5299 * @see #setFitsSystemWindows()
5300 * @see #setSystemUiVisibility(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005301 */
5302 protected boolean fitSystemWindows(Rect insets) {
5303 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005304 mUserPaddingStart = -1;
5305 mUserPaddingEnd = -1;
5306 mUserPaddingRelative = false;
5307 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
5308 || mAttachInfo == null
5309 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
5310 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
5311 return true;
5312 } else {
5313 internalSetPadding(0, 0, 0, 0);
5314 return false;
5315 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005316 }
5317 return false;
5318 }
5319
5320 /**
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005321 * Sets whether or not this view should account for system screen decorations
5322 * such as the status bar and inset its content; that is, controlling whether
5323 * the default implementation of {@link #fitSystemWindows(Rect)} will be
5324 * executed. See that method for more details.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005325 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005326 * <p>Note that if you are providing your own implementation of
5327 * {@link #fitSystemWindows(Rect)}, then there is no need to set this
5328 * flag to true -- your implementation will be overriding the default
5329 * implementation that checks this flag.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005330 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005331 * @param fitSystemWindows If true, then the default implementation of
5332 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005333 *
5334 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005335 * @see #getFitsSystemWindows()
5336 * @see #fitSystemWindows(Rect)
5337 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005338 */
5339 public void setFitsSystemWindows(boolean fitSystemWindows) {
5340 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
5341 }
5342
5343 /**
Dianne Hackborncf675782012-05-10 15:07:24 -07005344 * Check for state of {@link #setFitsSystemWindows(boolean). If this method
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005345 * returns true, the default implementation of {@link #fitSystemWindows(Rect)}
5346 * will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005347 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005348 * @return Returns true if the default implementation of
5349 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005350 *
5351 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005352 * @see #setFitsSystemWindows()
5353 * @see #fitSystemWindows(Rect)
5354 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005355 */
Dianne Hackborncf675782012-05-10 15:07:24 -07005356 public boolean getFitsSystemWindows() {
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005357 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
5358 }
5359
Dianne Hackbornb1b55e62012-05-10 16:25:54 -07005360 /** @hide */
5361 public boolean fitsSystemWindows() {
5362 return getFitsSystemWindows();
5363 }
5364
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005365 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005366 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
5367 */
5368 public void requestFitSystemWindows() {
5369 if (mParent != null) {
5370 mParent.requestFitSystemWindows();
5371 }
5372 }
5373
5374 /**
5375 * For use by PhoneWindow to make its own system window fitting optional.
5376 * @hide
5377 */
5378 public void makeOptionalFitsSystemWindows() {
5379 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5380 }
5381
5382 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005383 * Returns the visibility status for this view.
5384 *
5385 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5386 * @attr ref android.R.styleable#View_visibility
5387 */
5388 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005389 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5390 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5391 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005392 })
5393 public int getVisibility() {
5394 return mViewFlags & VISIBILITY_MASK;
5395 }
5396
5397 /**
5398 * Set the enabled state of this view.
5399 *
5400 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5401 * @attr ref android.R.styleable#View_visibility
5402 */
5403 @RemotableViewMethod
5404 public void setVisibility(int visibility) {
5405 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005406 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005407 }
5408
5409 /**
5410 * Returns the enabled status for this view. The interpretation of the
5411 * enabled state varies by subclass.
5412 *
5413 * @return True if this view is enabled, false otherwise.
5414 */
5415 @ViewDebug.ExportedProperty
5416 public boolean isEnabled() {
5417 return (mViewFlags & ENABLED_MASK) == ENABLED;
5418 }
5419
5420 /**
5421 * Set the enabled state of this view. The interpretation of the enabled
5422 * state varies by subclass.
5423 *
5424 * @param enabled True if this view is enabled, false otherwise.
5425 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005426 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005427 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005428 if (enabled == isEnabled()) return;
5429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005430 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5431
5432 /*
5433 * The View most likely has to change its appearance, so refresh
5434 * the drawable state.
5435 */
5436 refreshDrawableState();
5437
5438 // Invalidate too, since the default behavior for views is to be
5439 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005440 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005441 }
5442
5443 /**
5444 * Set whether this view can receive the focus.
5445 *
5446 * Setting this to false will also ensure that this view is not focusable
5447 * in touch mode.
5448 *
5449 * @param focusable If true, this view can receive the focus.
5450 *
5451 * @see #setFocusableInTouchMode(boolean)
5452 * @attr ref android.R.styleable#View_focusable
5453 */
5454 public void setFocusable(boolean focusable) {
5455 if (!focusable) {
5456 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5457 }
5458 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5459 }
5460
5461 /**
5462 * Set whether this view can receive focus while in touch mode.
5463 *
5464 * Setting this to true will also ensure that this view is focusable.
5465 *
5466 * @param focusableInTouchMode If true, this view can receive the focus while
5467 * in touch mode.
5468 *
5469 * @see #setFocusable(boolean)
5470 * @attr ref android.R.styleable#View_focusableInTouchMode
5471 */
5472 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5473 // Focusable in touch mode should always be set before the focusable flag
5474 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5475 // which, in touch mode, will not successfully request focus on this view
5476 // because the focusable in touch mode flag is not set
5477 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5478 if (focusableInTouchMode) {
5479 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5480 }
5481 }
5482
5483 /**
5484 * Set whether this view should have sound effects enabled for events such as
5485 * clicking and touching.
5486 *
5487 * <p>You may wish to disable sound effects for a view if you already play sounds,
5488 * for instance, a dial key that plays dtmf tones.
5489 *
5490 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5491 * @see #isSoundEffectsEnabled()
5492 * @see #playSoundEffect(int)
5493 * @attr ref android.R.styleable#View_soundEffectsEnabled
5494 */
5495 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5496 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5497 }
5498
5499 /**
5500 * @return whether this view should have sound effects enabled for events such as
5501 * clicking and touching.
5502 *
5503 * @see #setSoundEffectsEnabled(boolean)
5504 * @see #playSoundEffect(int)
5505 * @attr ref android.R.styleable#View_soundEffectsEnabled
5506 */
5507 @ViewDebug.ExportedProperty
5508 public boolean isSoundEffectsEnabled() {
5509 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5510 }
5511
5512 /**
5513 * Set whether this view should have haptic feedback for events such as
5514 * long presses.
5515 *
5516 * <p>You may wish to disable haptic feedback if your view already controls
5517 * its own haptic feedback.
5518 *
5519 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5520 * @see #isHapticFeedbackEnabled()
5521 * @see #performHapticFeedback(int)
5522 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5523 */
5524 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5525 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5526 }
5527
5528 /**
5529 * @return whether this view should have haptic feedback enabled for events
5530 * long presses.
5531 *
5532 * @see #setHapticFeedbackEnabled(boolean)
5533 * @see #performHapticFeedback(int)
5534 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5535 */
5536 @ViewDebug.ExportedProperty
5537 public boolean isHapticFeedbackEnabled() {
5538 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5539 }
5540
5541 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005542 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005543 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005544 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5545 * {@link #LAYOUT_DIRECTION_RTL},
5546 * {@link #LAYOUT_DIRECTION_INHERIT} or
5547 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005548 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005549 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005550 * @hide
Cibu Johny7632cb92010-02-22 13:01:02 -08005551 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005552 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005553 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5554 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5555 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5556 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005557 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005558 public int getLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005559 return (mPrivateFlags2 & LAYOUT_DIRECTION_MASK) >> LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005560 }
5561
5562 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005563 * Set the layout direction for this view. This will propagate a reset of layout direction
5564 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005565 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005566 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
5567 * {@link #LAYOUT_DIRECTION_RTL},
5568 * {@link #LAYOUT_DIRECTION_INHERIT} or
5569 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005570 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005571 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005572 * @hide
Cibu Johny7632cb92010-02-22 13:01:02 -08005573 */
5574 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005575 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005576 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005577 // Reset the current layout direction and the resolved one
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005578 mPrivateFlags2 &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07005579 resetResolvedLayoutDirection();
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005580 // Set the new layout direction (filtered) and ask for a layout pass
5581 mPrivateFlags2 |=
5582 ((layoutDirection << LAYOUT_DIRECTION_MASK_SHIFT) & LAYOUT_DIRECTION_MASK);
5583 requestLayout();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005584 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005585 }
5586
5587 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005588 * Returns the resolved layout direction for this view.
5589 *
5590 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005591 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005592 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005593 */
5594 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005595 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5596 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005597 })
5598 public int getResolvedLayoutDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -07005599 // The layout diretion will be resolved only if needed
5600 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) != LAYOUT_DIRECTION_RESOLVED) {
5601 resolveLayoutDirection();
5602 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07005603 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005604 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
5605 }
5606
5607 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005608 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5609 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005610 *
5611 * @return true if the layout is right-to-left.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005612 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005613 */
5614 @ViewDebug.ExportedProperty(category = "layout")
5615 public boolean isLayoutRtl() {
5616 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
5617 }
5618
5619 /**
Adam Powell539ee872012-02-03 19:00:49 -08005620 * Indicates whether the view is currently tracking transient state that the
5621 * app should not need to concern itself with saving and restoring, but that
5622 * the framework should take special note to preserve when possible.
5623 *
Adam Powell785c4472012-05-02 21:25:39 -07005624 * <p>A view with transient state cannot be trivially rebound from an external
5625 * data source, such as an adapter binding item views in a list. This may be
5626 * because the view is performing an animation, tracking user selection
5627 * of content, or similar.</p>
5628 *
Adam Powell539ee872012-02-03 19:00:49 -08005629 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005630 */
5631 @ViewDebug.ExportedProperty(category = "layout")
5632 public boolean hasTransientState() {
5633 return (mPrivateFlags2 & HAS_TRANSIENT_STATE) == HAS_TRANSIENT_STATE;
5634 }
5635
5636 /**
5637 * Set whether this view is currently tracking transient state that the
Chet Haase563d4f22012-04-18 16:20:08 -07005638 * framework should attempt to preserve when possible. This flag is reference counted,
5639 * so every call to setHasTransientState(true) should be paired with a later call
5640 * to setHasTransientState(false).
Adam Powell539ee872012-02-03 19:00:49 -08005641 *
Adam Powell785c4472012-05-02 21:25:39 -07005642 * <p>A view with transient state cannot be trivially rebound from an external
5643 * data source, such as an adapter binding item views in a list. This may be
5644 * because the view is performing an animation, tracking user selection
5645 * of content, or similar.</p>
5646 *
Adam Powell539ee872012-02-03 19:00:49 -08005647 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005648 */
5649 public void setHasTransientState(boolean hasTransientState) {
Chet Haase563d4f22012-04-18 16:20:08 -07005650 mTransientStateCount = hasTransientState ? mTransientStateCount + 1 :
5651 mTransientStateCount - 1;
5652 if (mTransientStateCount < 0) {
5653 mTransientStateCount = 0;
5654 Log.e(VIEW_LOG_TAG, "hasTransientState decremented below 0: " +
5655 "unmatched pair of setHasTransientState calls");
5656 }
5657 if ((hasTransientState && mTransientStateCount == 1) ||
Adam Powell057a5852012-05-11 10:28:38 -07005658 (!hasTransientState && mTransientStateCount == 0)) {
Chet Haase563d4f22012-04-18 16:20:08 -07005659 // update flag if we've just incremented up from 0 or decremented down to 0
5660 mPrivateFlags2 = (mPrivateFlags2 & ~HAS_TRANSIENT_STATE) |
5661 (hasTransientState ? HAS_TRANSIENT_STATE : 0);
5662 if (mParent != null) {
5663 try {
5664 mParent.childHasTransientStateChanged(this, hasTransientState);
5665 } catch (AbstractMethodError e) {
5666 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5667 " does not fully implement ViewParent", e);
5668 }
Adam Powell539ee872012-02-03 19:00:49 -08005669 }
5670 }
5671 }
5672
5673 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005674 * If this view doesn't do any drawing on its own, set this flag to
5675 * allow further optimizations. By default, this flag is not set on
5676 * View, but could be set on some View subclasses such as ViewGroup.
5677 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005678 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5679 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005680 *
5681 * @param willNotDraw whether or not this View draw on its own
5682 */
5683 public void setWillNotDraw(boolean willNotDraw) {
5684 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5685 }
5686
5687 /**
5688 * Returns whether or not this View draws on its own.
5689 *
5690 * @return true if this view has nothing to draw, false otherwise
5691 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005692 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005693 public boolean willNotDraw() {
5694 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
5695 }
5696
5697 /**
5698 * When a View's drawing cache is enabled, drawing is redirected to an
5699 * offscreen bitmap. Some views, like an ImageView, must be able to
5700 * bypass this mechanism if they already draw a single bitmap, to avoid
5701 * unnecessary usage of the memory.
5702 *
5703 * @param willNotCacheDrawing true if this view does not cache its
5704 * drawing, false otherwise
5705 */
5706 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
5707 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
5708 }
5709
5710 /**
5711 * Returns whether or not this View can cache its drawing or not.
5712 *
5713 * @return true if this view does not cache its drawing, false otherwise
5714 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005715 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005716 public boolean willNotCacheDrawing() {
5717 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5718 }
5719
5720 /**
5721 * Indicates whether this view reacts to click events or not.
5722 *
5723 * @return true if the view is clickable, false otherwise
5724 *
5725 * @see #setClickable(boolean)
5726 * @attr ref android.R.styleable#View_clickable
5727 */
5728 @ViewDebug.ExportedProperty
5729 public boolean isClickable() {
5730 return (mViewFlags & CLICKABLE) == CLICKABLE;
5731 }
5732
5733 /**
5734 * Enables or disables click events for this view. When a view
5735 * is clickable it will change its state to "pressed" on every click.
5736 * Subclasses should set the view clickable to visually react to
5737 * user's clicks.
5738 *
5739 * @param clickable true to make the view clickable, false otherwise
5740 *
5741 * @see #isClickable()
5742 * @attr ref android.R.styleable#View_clickable
5743 */
5744 public void setClickable(boolean clickable) {
5745 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5746 }
5747
5748 /**
5749 * Indicates whether this view reacts to long click events or not.
5750 *
5751 * @return true if the view is long clickable, false otherwise
5752 *
5753 * @see #setLongClickable(boolean)
5754 * @attr ref android.R.styleable#View_longClickable
5755 */
5756 public boolean isLongClickable() {
5757 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5758 }
5759
5760 /**
5761 * Enables or disables long click events for this view. When a view is long
5762 * clickable it reacts to the user holding down the button for a longer
5763 * duration than a tap. This event can either launch the listener or a
5764 * context menu.
5765 *
5766 * @param longClickable true to make the view long clickable, false otherwise
5767 * @see #isLongClickable()
5768 * @attr ref android.R.styleable#View_longClickable
5769 */
5770 public void setLongClickable(boolean longClickable) {
5771 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5772 }
5773
5774 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005775 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005776 *
5777 * @see #isClickable()
5778 * @see #setClickable(boolean)
5779 *
5780 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5781 * the View's internal state from a previously set "pressed" state.
5782 */
5783 public void setPressed(boolean pressed) {
Adam Powell035a1fc2012-02-27 15:23:50 -08005784 final boolean needsRefresh = pressed != ((mPrivateFlags & PRESSED) == PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005785
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005786 if (pressed) {
5787 mPrivateFlags |= PRESSED;
5788 } else {
5789 mPrivateFlags &= ~PRESSED;
5790 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005791
5792 if (needsRefresh) {
5793 refreshDrawableState();
5794 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005795 dispatchSetPressed(pressed);
5796 }
5797
5798 /**
5799 * Dispatch setPressed to all of this View's children.
5800 *
5801 * @see #setPressed(boolean)
5802 *
5803 * @param pressed The new pressed state
5804 */
5805 protected void dispatchSetPressed(boolean pressed) {
5806 }
5807
5808 /**
5809 * Indicates whether the view is currently in pressed state. Unless
5810 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5811 * the pressed state.
5812 *
Philip Milne6c8ea062012-04-03 17:38:43 -07005813 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005814 * @see #isClickable()
5815 * @see #setClickable(boolean)
5816 *
5817 * @return true if the view is currently pressed, false otherwise
5818 */
5819 public boolean isPressed() {
5820 return (mPrivateFlags & PRESSED) == PRESSED;
5821 }
5822
5823 /**
5824 * Indicates whether this view will save its state (that is,
5825 * whether its {@link #onSaveInstanceState} method will be called).
5826 *
5827 * @return Returns true if the view state saving is enabled, else false.
5828 *
5829 * @see #setSaveEnabled(boolean)
5830 * @attr ref android.R.styleable#View_saveEnabled
5831 */
5832 public boolean isSaveEnabled() {
5833 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5834 }
5835
5836 /**
5837 * Controls whether the saving of this view's state is
5838 * enabled (that is, whether its {@link #onSaveInstanceState} method
5839 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005840 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005841 * for its state to be saved. This flag can only disable the
5842 * saving of this view; any child views may still have their state saved.
5843 *
5844 * @param enabled Set to false to <em>disable</em> state saving, or true
5845 * (the default) to allow it.
5846 *
5847 * @see #isSaveEnabled()
5848 * @see #setId(int)
5849 * @see #onSaveInstanceState()
5850 * @attr ref android.R.styleable#View_saveEnabled
5851 */
5852 public void setSaveEnabled(boolean enabled) {
5853 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5854 }
5855
Jeff Brown85a31762010-09-01 17:01:00 -07005856 /**
5857 * Gets whether the framework should discard touches when the view's
5858 * window is obscured by another visible window.
5859 * Refer to the {@link View} security documentation for more details.
5860 *
5861 * @return True if touch filtering is enabled.
5862 *
5863 * @see #setFilterTouchesWhenObscured(boolean)
5864 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5865 */
5866 @ViewDebug.ExportedProperty
5867 public boolean getFilterTouchesWhenObscured() {
5868 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5869 }
5870
5871 /**
5872 * Sets whether the framework should discard touches when the view's
5873 * window is obscured by another visible window.
5874 * Refer to the {@link View} security documentation for more details.
5875 *
5876 * @param enabled True if touch filtering should be enabled.
5877 *
5878 * @see #getFilterTouchesWhenObscured
5879 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5880 */
5881 public void setFilterTouchesWhenObscured(boolean enabled) {
5882 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5883 FILTER_TOUCHES_WHEN_OBSCURED);
5884 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005885
5886 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005887 * Indicates whether the entire hierarchy under this view will save its
5888 * state when a state saving traversal occurs from its parent. The default
5889 * is true; if false, these views will not be saved unless
5890 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5891 *
5892 * @return Returns true if the view state saving from parent is enabled, else false.
5893 *
5894 * @see #setSaveFromParentEnabled(boolean)
5895 */
5896 public boolean isSaveFromParentEnabled() {
5897 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5898 }
5899
5900 /**
5901 * Controls whether the entire hierarchy under this view will save its
5902 * state when a state saving traversal occurs from its parent. The default
5903 * is true; if false, these views will not be saved unless
5904 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5905 *
5906 * @param enabled Set to false to <em>disable</em> state saving, or true
5907 * (the default) to allow it.
5908 *
5909 * @see #isSaveFromParentEnabled()
5910 * @see #setId(int)
5911 * @see #onSaveInstanceState()
5912 */
5913 public void setSaveFromParentEnabled(boolean enabled) {
5914 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5915 }
5916
5917
5918 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005919 * Returns whether this View is able to take focus.
5920 *
5921 * @return True if this view can take focus, or false otherwise.
5922 * @attr ref android.R.styleable#View_focusable
5923 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005924 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005925 public final boolean isFocusable() {
5926 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
5927 }
5928
5929 /**
5930 * When a view is focusable, it may not want to take focus when in touch mode.
5931 * For example, a button would like focus when the user is navigating via a D-pad
5932 * so that the user can click on it, but once the user starts touching the screen,
5933 * the button shouldn't take focus
5934 * @return Whether the view is focusable in touch mode.
5935 * @attr ref android.R.styleable#View_focusableInTouchMode
5936 */
5937 @ViewDebug.ExportedProperty
5938 public final boolean isFocusableInTouchMode() {
5939 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5940 }
5941
5942 /**
5943 * Find the nearest view in the specified direction that can take focus.
5944 * This does not actually give focus to that view.
5945 *
5946 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5947 *
5948 * @return The nearest focusable in the specified direction, or null if none
5949 * can be found.
5950 */
5951 public View focusSearch(int direction) {
5952 if (mParent != null) {
5953 return mParent.focusSearch(this, direction);
5954 } else {
5955 return null;
5956 }
5957 }
5958
5959 /**
5960 * This method is the last chance for the focused view and its ancestors to
5961 * respond to an arrow key. This is called when the focused view did not
5962 * consume the key internally, nor could the view system find a new view in
5963 * the requested direction to give focus to.
5964 *
5965 * @param focused The currently focused view.
5966 * @param direction The direction focus wants to move. One of FOCUS_UP,
5967 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5968 * @return True if the this view consumed this unhandled move.
5969 */
5970 public boolean dispatchUnhandledMove(View focused, int direction) {
5971 return false;
5972 }
5973
5974 /**
5975 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005976 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005977 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005978 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5979 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005980 * @return The user specified next view, or null if there is none.
5981 */
5982 View findUserSetNextFocus(View root, int direction) {
5983 switch (direction) {
5984 case FOCUS_LEFT:
5985 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005986 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005987 case FOCUS_RIGHT:
5988 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005989 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005990 case FOCUS_UP:
5991 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005992 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005993 case FOCUS_DOWN:
5994 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005995 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005996 case FOCUS_FORWARD:
5997 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005998 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005999 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08006000 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08006001 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006002 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08006003 @Override
6004 public boolean apply(View t) {
6005 return t.mNextFocusForwardId == id;
6006 }
6007 });
6008 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006009 }
6010 return null;
6011 }
6012
Jeff Brown4dfbec22011-08-15 14:55:37 -07006013 private View findViewInsideOutShouldExist(View root, final int childViewId) {
6014 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
6015 @Override
6016 public boolean apply(View t) {
6017 return t.mID == childViewId;
6018 }
6019 });
6020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006021 if (result == null) {
6022 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
6023 + "by user for id " + childViewId);
6024 }
6025 return result;
6026 }
6027
6028 /**
6029 * Find and return all focusable views that are descendants of this view,
6030 * possibly including this view if it is focusable itself.
6031 *
6032 * @param direction The direction of the focus
6033 * @return A list of focusable views
6034 */
6035 public ArrayList<View> getFocusables(int direction) {
6036 ArrayList<View> result = new ArrayList<View>(24);
6037 addFocusables(result, direction);
6038 return result;
6039 }
6040
6041 /**
6042 * Add any focusable views that are descendants of this view (possibly
6043 * including this view if it is focusable itself) to views. If we are in touch mode,
6044 * only add views that are also focusable in touch mode.
6045 *
6046 * @param views Focusable views found so far
6047 * @param direction The direction of the focus
6048 */
6049 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006050 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
6051 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006052
svetoslavganov75986cf2009-05-14 22:28:01 -07006053 /**
6054 * Adds any focusable views that are descendants of this view (possibly
6055 * including this view if it is focusable itself) to views. This method
6056 * adds all focusable views regardless if we are in touch mode or
Svetoslav Ganov42138042012-03-20 11:51:39 -07006057 * only views focusable in touch mode if we are in touch mode or
6058 * only views that can take accessibility focus if accessibility is enabeld
6059 * depending on the focusable mode paramater.
svetoslavganov75986cf2009-05-14 22:28:01 -07006060 *
6061 * @param views Focusable views found so far or null if all we are interested is
6062 * the number of focusables.
6063 * @param direction The direction of the focus.
6064 * @param focusableMode The type of focusables to be added.
6065 *
6066 * @see #FOCUSABLES_ALL
6067 * @see #FOCUSABLES_TOUCH_MODE
6068 */
6069 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006070 if (views == null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006071 return;
6072 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006073 if ((focusableMode & FOCUSABLES_ACCESSIBILITY) == FOCUSABLES_ACCESSIBILITY) {
Svetoslav Ganov791fd312012-05-14 15:12:30 -07006074 if (canTakeAccessibilityFocusFromHover() || getAccessibilityNodeProvider() != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006075 views.add(this);
6076 return;
6077 }
6078 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006079 if (!isFocusable()) {
6080 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07006081 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006082 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE
6083 && isInTouchMode() && !isFocusableInTouchMode()) {
6084 return;
6085 }
6086 views.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006087 }
6088
6089 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006090 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006091 * The search is performed by either the text that the View renders or the content
6092 * description that describes the view for accessibility purposes and the view does
6093 * not render or both. Clients can specify how the search is to be performed via
6094 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
6095 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006096 *
6097 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006098 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07006099 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006100 * @see #FIND_VIEWS_WITH_TEXT
6101 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
6102 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006103 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006104 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07006105 if (getAccessibilityNodeProvider() != null) {
6106 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
6107 outViews.add(this);
6108 }
6109 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006110 && (searched != null && searched.length() > 0)
6111 && (mContentDescription != null && mContentDescription.length() > 0)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006112 String searchedLowerCase = searched.toString().toLowerCase();
6113 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
6114 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
6115 outViews.add(this);
6116 }
6117 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006118 }
6119
6120 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006121 * Find and return all touchable views that are descendants of this view,
6122 * possibly including this view if it is touchable itself.
6123 *
6124 * @return A list of touchable views
6125 */
6126 public ArrayList<View> getTouchables() {
6127 ArrayList<View> result = new ArrayList<View>();
6128 addTouchables(result);
6129 return result;
6130 }
6131
6132 /**
6133 * Add any touchable views that are descendants of this view (possibly
6134 * including this view if it is touchable itself) to views.
6135 *
6136 * @param views Touchable views found so far
6137 */
6138 public void addTouchables(ArrayList<View> views) {
6139 final int viewFlags = mViewFlags;
6140
6141 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
6142 && (viewFlags & ENABLED_MASK) == ENABLED) {
6143 views.add(this);
6144 }
6145 }
6146
6147 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006148 * Returns whether this View is accessibility focused.
6149 *
6150 * @return True if this View is accessibility focused.
6151 */
6152 boolean isAccessibilityFocused() {
6153 return (mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0;
6154 }
6155
6156 /**
6157 * Call this to try to give accessibility focus to this view.
6158 *
6159 * A view will not actually take focus if {@link AccessibilityManager#isEnabled()}
6160 * returns false or the view is no visible or the view already has accessibility
6161 * focus.
6162 *
6163 * See also {@link #focusSearch(int)}, which is what you call to say that you
6164 * have focus, and you want your parent to look for the next one.
6165 *
6166 * @return Whether this view actually took accessibility focus.
6167 *
6168 * @hide
6169 */
6170 public boolean requestAccessibilityFocus() {
Svetoslav Ganov07b726c2012-04-30 12:24:57 -07006171 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
6172 if (!manager.isEnabled() || !manager.isTouchExplorationEnabled()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006173 return false;
6174 }
6175 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6176 return false;
6177 }
6178 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) == 0) {
6179 mPrivateFlags2 |= ACCESSIBILITY_FOCUSED;
6180 ViewRootImpl viewRootImpl = getViewRootImpl();
6181 if (viewRootImpl != null) {
6182 viewRootImpl.setAccessibilityFocusedHost(this);
6183 }
6184 invalidate();
6185 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
6186 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006187 return true;
6188 }
6189 return false;
6190 }
6191
6192 /**
6193 * Call this to try to clear accessibility focus of this view.
6194 *
6195 * See also {@link #focusSearch(int)}, which is what you call to say that you
6196 * have focus, and you want your parent to look for the next one.
6197 *
6198 * @hide
6199 */
6200 public void clearAccessibilityFocus() {
Svetoslav Ganov791fd312012-05-14 15:12:30 -07006201 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6202 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006203 invalidate();
6204 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
6205 notifyAccessibilityStateChanged();
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006206 // Clear the text navigation state.
6207 setAccessibilityCursorPosition(-1);
Svetoslav Ganov42138042012-03-20 11:51:39 -07006208 }
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006209 // Clear the global reference of accessibility focus if this
6210 // view or any of its descendants had accessibility focus.
6211 ViewRootImpl viewRootImpl = getViewRootImpl();
6212 if (viewRootImpl != null) {
6213 View focusHost = viewRootImpl.getAccessibilityFocusedHost();
6214 if (focusHost != null && ViewRootImpl.isViewDescendantOf(focusHost, this)) {
6215 viewRootImpl.setAccessibilityFocusedHost(null);
6216 }
6217 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006218 }
6219
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006220 private void requestAccessibilityFocusFromHover() {
6221 if (includeForAccessibility() && isActionableForAccessibility()) {
6222 requestAccessibilityFocus();
Svetoslav Ganovf76a83c2012-05-21 15:32:17 -07006223 requestFocusNoSearch(View.FOCUS_DOWN, null);
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006224 } else {
6225 if (mParent != null) {
6226 View nextFocus = mParent.findViewToTakeAccessibilityFocusFromHover(this, this);
6227 if (nextFocus != null) {
6228 nextFocus.requestAccessibilityFocus();
Svetoslav Ganovf76a83c2012-05-21 15:32:17 -07006229 nextFocus.requestFocusNoSearch(View.FOCUS_DOWN, null);
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006230 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006231 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006232 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006233 }
6234
6235 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006236 * @hide
6237 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006238 public boolean canTakeAccessibilityFocusFromHover() {
6239 if (includeForAccessibility() && isActionableForAccessibility()) {
6240 return true;
6241 }
6242 if (mParent != null) {
6243 return (mParent.findViewToTakeAccessibilityFocusFromHover(this, this) == this);
Svetoslav Ganov42138042012-03-20 11:51:39 -07006244 }
6245 return false;
6246 }
6247
6248 /**
6249 * Clears accessibility focus without calling any callback methods
6250 * normally invoked in {@link #clearAccessibilityFocus()}. This method
6251 * is used for clearing accessibility focus when giving this focus to
6252 * another view.
6253 */
6254 void clearAccessibilityFocusNoCallbacks() {
6255 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6256 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
6257 invalidate();
6258 }
6259 }
6260
6261 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006262 * Call this to try to give focus to a specific view or to one of its
6263 * descendants.
6264 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006265 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6266 * false), or if it is focusable and it is not focusable in touch mode
6267 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006268 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006269 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006270 * have focus, and you want your parent to look for the next one.
6271 *
6272 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
6273 * {@link #FOCUS_DOWN} and <code>null</code>.
6274 *
6275 * @return Whether this view or one of its descendants actually took focus.
6276 */
6277 public final boolean requestFocus() {
6278 return requestFocus(View.FOCUS_DOWN);
6279 }
6280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006281 /**
6282 * Call this to try to give focus to a specific view or to one of its
6283 * descendants and give it a hint about what direction focus is heading.
6284 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006285 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6286 * false), or if it is focusable and it is not focusable in touch mode
6287 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006288 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006289 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006290 * have focus, and you want your parent to look for the next one.
6291 *
6292 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
6293 * <code>null</code> set for the previously focused rectangle.
6294 *
6295 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6296 * @return Whether this view or one of its descendants actually took focus.
6297 */
6298 public final boolean requestFocus(int direction) {
6299 return requestFocus(direction, null);
6300 }
6301
6302 /**
6303 * Call this to try to give focus to a specific view or to one of its descendants
6304 * and give it hints about the direction and a specific rectangle that the focus
6305 * is coming from. The rectangle can help give larger views a finer grained hint
6306 * about where focus is coming from, and therefore, where to show selection, or
6307 * forward focus change internally.
6308 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006309 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6310 * false), or if it is focusable and it is not focusable in touch mode
6311 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006312 *
6313 * A View will not take focus if it is not visible.
6314 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006315 * A View will not take focus if one of its parents has
6316 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
6317 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006318 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006319 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006320 * have focus, and you want your parent to look for the next one.
6321 *
6322 * You may wish to override this method if your custom {@link View} has an internal
6323 * {@link View} that it wishes to forward the request to.
6324 *
6325 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6326 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
6327 * to give a finer grained hint about where focus is coming from. May be null
6328 * if there is no hint.
6329 * @return Whether this view or one of its descendants actually took focus.
6330 */
6331 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006332 return requestFocusNoSearch(direction, previouslyFocusedRect);
6333 }
6334
6335 private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006336 // need to be focusable
6337 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
6338 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6339 return false;
6340 }
6341
6342 // need to be focusable in touch mode if in touch mode
6343 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006344 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
6345 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006346 }
6347
6348 // need to not have any parents blocking us
6349 if (hasAncestorThatBlocksDescendantFocus()) {
6350 return false;
6351 }
6352
6353 handleFocusGainInternal(direction, previouslyFocusedRect);
6354 return true;
6355 }
6356
6357 /**
6358 * Call this to try to give focus to a specific view or to one of its descendants. This is a
6359 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
6360 * touch mode to request focus when they are touched.
6361 *
6362 * @return Whether this view or one of its descendants actually took focus.
6363 *
6364 * @see #isInTouchMode()
6365 *
6366 */
6367 public final boolean requestFocusFromTouch() {
6368 // Leave touch mode if we need to
6369 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006370 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07006371 if (viewRoot != null) {
6372 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006373 }
6374 }
6375 return requestFocus(View.FOCUS_DOWN);
6376 }
6377
6378 /**
6379 * @return Whether any ancestor of this view blocks descendant focus.
6380 */
6381 private boolean hasAncestorThatBlocksDescendantFocus() {
6382 ViewParent ancestor = mParent;
6383 while (ancestor instanceof ViewGroup) {
6384 final ViewGroup vgAncestor = (ViewGroup) ancestor;
6385 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
6386 return true;
6387 } else {
6388 ancestor = vgAncestor.getParent();
6389 }
6390 }
6391 return false;
6392 }
6393
6394 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006395 * Gets the mode for determining whether this View is important for accessibility
6396 * which is if it fires accessibility events and if it is reported to
6397 * accessibility services that query the screen.
6398 *
6399 * @return The mode for determining whether a View is important for accessibility.
6400 *
6401 * @attr ref android.R.styleable#View_importantForAccessibility
6402 *
6403 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6404 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6405 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6406 */
6407 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
6408 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_AUTO,
6409 to = "IMPORTANT_FOR_ACCESSIBILITY_AUTO"),
6410 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_YES,
6411 to = "IMPORTANT_FOR_ACCESSIBILITY_YES"),
6412 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_NO,
6413 to = "IMPORTANT_FOR_ACCESSIBILITY_NO")
6414 })
6415 public int getImportantForAccessibility() {
6416 return (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6417 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6418 }
6419
6420 /**
6421 * Sets how to determine whether this view is important for accessibility
6422 * which is if it fires accessibility events and if it is reported to
6423 * accessibility services that query the screen.
6424 *
6425 * @param mode How to determine whether this view is important for accessibility.
6426 *
6427 * @attr ref android.R.styleable#View_importantForAccessibility
6428 *
6429 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6430 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6431 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6432 */
6433 public void setImportantForAccessibility(int mode) {
6434 if (mode != getImportantForAccessibility()) {
6435 mPrivateFlags2 &= ~IMPORTANT_FOR_ACCESSIBILITY_MASK;
6436 mPrivateFlags2 |= (mode << IMPORTANT_FOR_ACCESSIBILITY_SHIFT)
6437 & IMPORTANT_FOR_ACCESSIBILITY_MASK;
6438 notifyAccessibilityStateChanged();
6439 }
6440 }
6441
6442 /**
6443 * Gets whether this view should be exposed for accessibility.
6444 *
6445 * @return Whether the view is exposed for accessibility.
6446 *
6447 * @hide
6448 */
6449 public boolean isImportantForAccessibility() {
6450 final int mode = (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6451 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6452 switch (mode) {
6453 case IMPORTANT_FOR_ACCESSIBILITY_YES:
6454 return true;
6455 case IMPORTANT_FOR_ACCESSIBILITY_NO:
6456 return false;
6457 case IMPORTANT_FOR_ACCESSIBILITY_AUTO:
6458 return isActionableForAccessibility() || hasListenersForAccessibility();
6459 default:
6460 throw new IllegalArgumentException("Unknow important for accessibility mode: "
6461 + mode);
6462 }
6463 }
6464
6465 /**
6466 * Gets the parent for accessibility purposes. Note that the parent for
6467 * accessibility is not necessary the immediate parent. It is the first
6468 * predecessor that is important for accessibility.
6469 *
6470 * @return The parent for accessibility purposes.
6471 */
6472 public ViewParent getParentForAccessibility() {
6473 if (mParent instanceof View) {
6474 View parentView = (View) mParent;
6475 if (parentView.includeForAccessibility()) {
6476 return mParent;
6477 } else {
6478 return mParent.getParentForAccessibility();
6479 }
6480 }
6481 return null;
6482 }
6483
6484 /**
6485 * Adds the children of a given View for accessibility. Since some Views are
6486 * not important for accessibility the children for accessibility are not
6487 * necessarily direct children of the riew, rather they are the first level of
6488 * descendants important for accessibility.
6489 *
6490 * @param children The list of children for accessibility.
6491 */
6492 public void addChildrenForAccessibility(ArrayList<View> children) {
6493 if (includeForAccessibility()) {
6494 children.add(this);
6495 }
6496 }
6497
6498 /**
6499 * Whether to regard this view for accessibility. A view is regarded for
6500 * accessibility if it is important for accessibility or the querying
6501 * accessibility service has explicitly requested that view not
6502 * important for accessibility are regarded.
6503 *
6504 * @return Whether to regard the view for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006505 *
6506 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006507 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006508 public boolean includeForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006509 if (mAttachInfo != null) {
6510 if (!mAttachInfo.mIncludeNotImportantViews) {
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07006511 return isImportantForAccessibility();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006512 }
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006513 return true;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006514 }
6515 return false;
6516 }
6517
6518 /**
6519 * Returns whether the View is considered actionable from
6520 * accessibility perspective. Such view are important for
6521 * accessiiblity.
6522 *
6523 * @return True if the view is actionable for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006524 *
6525 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006526 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006527 public boolean isActionableForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006528 return (isClickable() || isLongClickable() || isFocusable());
6529 }
6530
6531 /**
6532 * Returns whether the View has registered callbacks wich makes it
6533 * important for accessiiblity.
6534 *
6535 * @return True if the view is actionable for accessibility.
6536 */
6537 private boolean hasListenersForAccessibility() {
6538 ListenerInfo info = getListenerInfo();
6539 return mTouchDelegate != null || info.mOnKeyListener != null
6540 || info.mOnTouchListener != null || info.mOnGenericMotionListener != null
6541 || info.mOnHoverListener != null || info.mOnDragListener != null;
6542 }
6543
6544 /**
6545 * Notifies accessibility services that some view's important for
6546 * accessibility state has changed. Note that such notifications
6547 * are made at most once every
6548 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}
6549 * to avoid unnecessary load to the system. Also once a view has
6550 * made a notifucation this method is a NOP until the notification has
6551 * been sent to clients.
6552 *
6553 * @hide
6554 *
6555 * TODO: Makse sure this method is called for any view state change
6556 * that is interesting for accessilility purposes.
6557 */
6558 public void notifyAccessibilityStateChanged() {
Svetoslav Ganovc406be92012-05-11 16:12:32 -07006559 if (!AccessibilityManager.getInstance(mContext).isEnabled()) {
6560 return;
6561 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006562 if ((mPrivateFlags2 & ACCESSIBILITY_STATE_CHANGED) == 0) {
6563 mPrivateFlags2 |= ACCESSIBILITY_STATE_CHANGED;
6564 if (mParent != null) {
6565 mParent.childAccessibilityStateChanged(this);
6566 }
6567 }
6568 }
6569
6570 /**
6571 * Reset the state indicating the this view has requested clients
6572 * interested in its accessiblity state to be notified.
6573 *
6574 * @hide
6575 */
6576 public void resetAccessibilityStateChanged() {
6577 mPrivateFlags2 &= ~ACCESSIBILITY_STATE_CHANGED;
6578 }
6579
6580 /**
6581 * Performs the specified accessibility action on the view. For
6582 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
alanv8eeefef2012-05-07 16:57:53 -07006583 * <p>
6584 * If an {@link AccessibilityDelegate} has been specified via calling
6585 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
6586 * {@link AccessibilityDelegate#performAccessibilityAction(View, int, Bundle)}
6587 * is responsible for handling this call.
6588 * </p>
Svetoslav Ganov42138042012-03-20 11:51:39 -07006589 *
6590 * @param action The action to perform.
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006591 * @param arguments Optional action arguments.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006592 * @return Whether the action was performed.
6593 */
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006594 public boolean performAccessibilityAction(int action, Bundle arguments) {
alanv8eeefef2012-05-07 16:57:53 -07006595 if (mAccessibilityDelegate != null) {
6596 return mAccessibilityDelegate.performAccessibilityAction(this, action, arguments);
6597 } else {
6598 return performAccessibilityActionInternal(action, arguments);
6599 }
6600 }
6601
6602 /**
6603 * @see #performAccessibilityAction(int, Bundle)
6604 *
6605 * Note: Called from the default {@link AccessibilityDelegate}.
6606 */
6607 boolean performAccessibilityActionInternal(int action, Bundle arguments) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006608 switch (action) {
6609 case AccessibilityNodeInfo.ACTION_CLICK: {
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006610 if (isClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006611 return performClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006612 }
6613 } break;
6614 case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
6615 if (isLongClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006616 return performLongClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006617 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006618 } break;
6619 case AccessibilityNodeInfo.ACTION_FOCUS: {
6620 if (!hasFocus()) {
6621 // Get out of touch mode since accessibility
6622 // wants to move focus around.
6623 getViewRootImpl().ensureTouchMode(false);
6624 return requestFocus();
6625 }
6626 } break;
6627 case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS: {
6628 if (hasFocus()) {
6629 clearFocus();
6630 return !isFocused();
6631 }
6632 } break;
6633 case AccessibilityNodeInfo.ACTION_SELECT: {
6634 if (!isSelected()) {
6635 setSelected(true);
6636 return isSelected();
6637 }
6638 } break;
6639 case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
6640 if (isSelected()) {
6641 setSelected(false);
6642 return !isSelected();
6643 }
6644 } break;
6645 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
6646 if (!isAccessibilityFocused()) {
6647 return requestAccessibilityFocus();
6648 }
6649 } break;
6650 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
6651 if (isAccessibilityFocused()) {
6652 clearAccessibilityFocus();
6653 return true;
6654 }
6655 } break;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006656 case AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY: {
6657 if (arguments != null) {
6658 final int granularity = arguments.getInt(
6659 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6660 return nextAtGranularity(granularity);
6661 }
6662 } break;
6663 case AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY: {
6664 if (arguments != null) {
6665 final int granularity = arguments.getInt(
6666 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6667 return previousAtGranularity(granularity);
6668 }
6669 } break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006670 }
6671 return false;
6672 }
6673
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006674 private boolean nextAtGranularity(int granularity) {
6675 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006676 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006677 return false;
6678 }
6679 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6680 if (iterator == null) {
6681 return false;
6682 }
6683 final int current = getAccessibilityCursorPosition();
6684 final int[] range = iterator.following(current);
6685 if (range == null) {
6686 setAccessibilityCursorPosition(-1);
6687 return false;
6688 }
6689 final int start = range[0];
6690 final int end = range[1];
6691 setAccessibilityCursorPosition(start);
6692 sendViewTextTraversedAtGranularityEvent(
6693 AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY,
6694 granularity, start, end);
6695 return true;
6696 }
6697
6698 private boolean previousAtGranularity(int granularity) {
6699 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006700 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006701 return false;
6702 }
6703 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6704 if (iterator == null) {
6705 return false;
6706 }
6707 final int selectionStart = getAccessibilityCursorPosition();
6708 final int current = selectionStart >= 0 ? selectionStart : text.length() + 1;
6709 final int[] range = iterator.preceding(current);
6710 if (range == null) {
6711 setAccessibilityCursorPosition(-1);
6712 return false;
6713 }
6714 final int start = range[0];
6715 final int end = range[1];
6716 setAccessibilityCursorPosition(end);
6717 sendViewTextTraversedAtGranularityEvent(
6718 AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
6719 granularity, start, end);
6720 return true;
6721 }
6722
6723 /**
6724 * Gets the text reported for accessibility purposes.
6725 *
6726 * @return The accessibility text.
6727 *
6728 * @hide
6729 */
6730 public CharSequence getIterableTextForAccessibility() {
6731 return mContentDescription;
6732 }
6733
6734 /**
6735 * @hide
6736 */
6737 public int getAccessibilityCursorPosition() {
6738 return mAccessibilityCursorPosition;
6739 }
6740
6741 /**
6742 * @hide
6743 */
6744 public void setAccessibilityCursorPosition(int position) {
6745 mAccessibilityCursorPosition = position;
6746 }
6747
6748 private void sendViewTextTraversedAtGranularityEvent(int action, int granularity,
6749 int fromIndex, int toIndex) {
6750 if (mParent == null) {
6751 return;
6752 }
6753 AccessibilityEvent event = AccessibilityEvent.obtain(
6754 AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY);
6755 onInitializeAccessibilityEvent(event);
6756 onPopulateAccessibilityEvent(event);
6757 event.setFromIndex(fromIndex);
6758 event.setToIndex(toIndex);
6759 event.setAction(action);
6760 event.setMovementGranularity(granularity);
6761 mParent.requestSendAccessibilityEvent(this, event);
6762 }
6763
6764 /**
6765 * @hide
6766 */
6767 public TextSegmentIterator getIteratorForGranularity(int granularity) {
6768 switch (granularity) {
6769 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER: {
6770 CharSequence text = getIterableTextForAccessibility();
6771 if (text != null && text.length() > 0) {
6772 CharacterTextSegmentIterator iterator =
6773 CharacterTextSegmentIterator.getInstance(mContext);
6774 iterator.initialize(text.toString());
6775 return iterator;
6776 }
6777 } break;
6778 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD: {
6779 CharSequence text = getIterableTextForAccessibility();
6780 if (text != null && text.length() > 0) {
6781 WordTextSegmentIterator iterator =
6782 WordTextSegmentIterator.getInstance(mContext);
6783 iterator.initialize(text.toString());
6784 return iterator;
6785 }
6786 } break;
6787 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH: {
6788 CharSequence text = getIterableTextForAccessibility();
6789 if (text != null && text.length() > 0) {
6790 ParagraphTextSegmentIterator iterator =
6791 ParagraphTextSegmentIterator.getInstance();
6792 iterator.initialize(text.toString());
6793 return iterator;
6794 }
6795 } break;
6796 }
6797 return null;
6798 }
6799
Svetoslav Ganov42138042012-03-20 11:51:39 -07006800 /**
Romain Guya440b002010-02-24 15:57:54 -08006801 * @hide
6802 */
6803 public void dispatchStartTemporaryDetach() {
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07006804 clearAccessibilityFocus();
Romain Guy38c2ece2012-05-24 14:20:56 -07006805 clearDisplayList();
6806
Romain Guya440b002010-02-24 15:57:54 -08006807 onStartTemporaryDetach();
6808 }
6809
6810 /**
6811 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006812 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
6813 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08006814 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006815 */
6816 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08006817 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08006818 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08006819 }
6820
6821 /**
6822 * @hide
6823 */
6824 public void dispatchFinishTemporaryDetach() {
6825 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006826 }
Romain Guy8506ab42009-06-11 17:35:47 -07006827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006828 /**
6829 * Called after {@link #onStartTemporaryDetach} when the container is done
6830 * changing the view.
6831 */
6832 public void onFinishTemporaryDetach() {
6833 }
Romain Guy8506ab42009-06-11 17:35:47 -07006834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006835 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006836 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
6837 * for this view's window. Returns null if the view is not currently attached
6838 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07006839 * just use the standard high-level event callbacks like
6840 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006841 */
6842 public KeyEvent.DispatcherState getKeyDispatcherState() {
6843 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
6844 }
Joe Malin32736f02011-01-19 16:14:20 -08006845
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006846 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006847 * Dispatch a key event before it is processed by any input method
6848 * associated with the view hierarchy. This can be used to intercept
6849 * key events in special situations before the IME consumes them; a
6850 * typical example would be handling the BACK key to update the application's
6851 * UI instead of allowing the IME to see it and close itself.
6852 *
6853 * @param event The key event to be dispatched.
6854 * @return True if the event was handled, false otherwise.
6855 */
6856 public boolean dispatchKeyEventPreIme(KeyEvent event) {
6857 return onKeyPreIme(event.getKeyCode(), event);
6858 }
6859
6860 /**
6861 * Dispatch a key event to the next view on the focus path. This path runs
6862 * from the top of the view tree down to the currently focused view. If this
6863 * view has focus, it will dispatch to itself. Otherwise it will dispatch
6864 * the next node down the focus path. This method also fires any key
6865 * listeners.
6866 *
6867 * @param event The key event to be dispatched.
6868 * @return True if the event was handled, false otherwise.
6869 */
6870 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006871 if (mInputEventConsistencyVerifier != null) {
6872 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
6873 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006874
Jeff Brown21bc5c92011-02-28 18:27:14 -08006875 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07006876 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006877 ListenerInfo li = mListenerInfo;
6878 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6879 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006880 return true;
6881 }
6882
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006883 if (event.dispatch(this, mAttachInfo != null
6884 ? mAttachInfo.mKeyDispatchState : null, this)) {
6885 return true;
6886 }
6887
6888 if (mInputEventConsistencyVerifier != null) {
6889 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6890 }
6891 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006892 }
6893
6894 /**
6895 * Dispatches a key shortcut event.
6896 *
6897 * @param event The key event to be dispatched.
6898 * @return True if the event was handled by the view, false otherwise.
6899 */
6900 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
6901 return onKeyShortcut(event.getKeyCode(), event);
6902 }
6903
6904 /**
6905 * Pass the touch screen motion event down to the target view, or this
6906 * view if it is the target.
6907 *
6908 * @param event The motion event to be dispatched.
6909 * @return True if the event was handled by the view, false otherwise.
6910 */
6911 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006912 if (mInputEventConsistencyVerifier != null) {
6913 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
6914 }
6915
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006916 if (onFilterTouchEventForSecurity(event)) {
6917 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006918 ListenerInfo li = mListenerInfo;
6919 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6920 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006921 return true;
6922 }
6923
6924 if (onTouchEvent(event)) {
6925 return true;
6926 }
Jeff Brown85a31762010-09-01 17:01:00 -07006927 }
6928
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006929 if (mInputEventConsistencyVerifier != null) {
6930 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006931 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006932 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006933 }
6934
6935 /**
Jeff Brown85a31762010-09-01 17:01:00 -07006936 * Filter the touch event to apply security policies.
6937 *
6938 * @param event The motion event to be filtered.
6939 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08006940 *
Jeff Brown85a31762010-09-01 17:01:00 -07006941 * @see #getFilterTouchesWhenObscured
6942 */
6943 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07006944 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07006945 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
6946 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
6947 // Window is obscured, drop this touch.
6948 return false;
6949 }
6950 return true;
6951 }
6952
6953 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006954 * Pass a trackball motion event down to the focused view.
6955 *
6956 * @param event The motion event to be dispatched.
6957 * @return True if the event was handled by the view, false otherwise.
6958 */
6959 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006960 if (mInputEventConsistencyVerifier != null) {
6961 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
6962 }
6963
Romain Guy02ccac62011-06-24 13:20:23 -07006964 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006965 }
6966
6967 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08006968 * Dispatch a generic motion event.
6969 * <p>
6970 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6971 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08006972 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07006973 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006974 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08006975 *
6976 * @param event The motion event to be dispatched.
6977 * @return True if the event was handled by the view, false otherwise.
6978 */
6979 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006980 if (mInputEventConsistencyVerifier != null) {
6981 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
6982 }
6983
Jeff Browna032cc02011-03-07 16:56:21 -08006984 final int source = event.getSource();
6985 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
6986 final int action = event.getAction();
6987 if (action == MotionEvent.ACTION_HOVER_ENTER
6988 || action == MotionEvent.ACTION_HOVER_MOVE
6989 || action == MotionEvent.ACTION_HOVER_EXIT) {
6990 if (dispatchHoverEvent(event)) {
6991 return true;
6992 }
6993 } else if (dispatchGenericPointerEvent(event)) {
6994 return true;
6995 }
6996 } else if (dispatchGenericFocusedEvent(event)) {
6997 return true;
6998 }
6999
Jeff Brown10b62902011-06-20 16:40:37 -07007000 if (dispatchGenericMotionEventInternal(event)) {
7001 return true;
7002 }
7003
7004 if (mInputEventConsistencyVerifier != null) {
7005 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7006 }
7007 return false;
7008 }
7009
7010 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07007011 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007012 ListenerInfo li = mListenerInfo;
7013 if (li != null && li.mOnGenericMotionListener != null
7014 && (mViewFlags & ENABLED_MASK) == ENABLED
7015 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007016 return true;
7017 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007018
7019 if (onGenericMotionEvent(event)) {
7020 return true;
7021 }
7022
7023 if (mInputEventConsistencyVerifier != null) {
7024 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7025 }
7026 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08007027 }
7028
7029 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007030 * Dispatch a hover event.
7031 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007032 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07007033 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007034 * </p>
7035 *
7036 * @param event The motion event to be dispatched.
7037 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007038 */
7039 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07007040 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007041 ListenerInfo li = mListenerInfo;
7042 if (li != null && li.mOnHoverListener != null
7043 && (mViewFlags & ENABLED_MASK) == ENABLED
7044 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07007045 return true;
7046 }
7047
Jeff Browna032cc02011-03-07 16:56:21 -08007048 return onHoverEvent(event);
7049 }
7050
7051 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007052 * Returns true if the view has a child to which it has recently sent
7053 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
7054 * it does not have a hovered child, then it must be the innermost hovered view.
7055 * @hide
7056 */
7057 protected boolean hasHoveredChild() {
7058 return false;
7059 }
7060
7061 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007062 * Dispatch a generic motion event to the view under the first pointer.
7063 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007064 * Do not call this method directly.
7065 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007066 * </p>
7067 *
7068 * @param event The motion event to be dispatched.
7069 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007070 */
7071 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
7072 return false;
7073 }
7074
7075 /**
7076 * Dispatch a generic motion event to the currently focused view.
7077 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007078 * Do not call this method directly.
7079 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007080 * </p>
7081 *
7082 * @param event The motion event to be dispatched.
7083 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007084 */
7085 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
7086 return false;
7087 }
7088
7089 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007090 * Dispatch a pointer event.
7091 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007092 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
7093 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
7094 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08007095 * and should not be expected to handle other pointing device features.
7096 * </p>
7097 *
7098 * @param event The motion event to be dispatched.
7099 * @return True if the event was handled by the view, false otherwise.
7100 * @hide
7101 */
7102 public final boolean dispatchPointerEvent(MotionEvent event) {
7103 if (event.isTouchEvent()) {
7104 return dispatchTouchEvent(event);
7105 } else {
7106 return dispatchGenericMotionEvent(event);
7107 }
7108 }
7109
7110 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007111 * Called when the window containing this view gains or loses window focus.
7112 * ViewGroups should override to route to their children.
7113 *
7114 * @param hasFocus True if the window containing this view now has focus,
7115 * false otherwise.
7116 */
7117 public void dispatchWindowFocusChanged(boolean hasFocus) {
7118 onWindowFocusChanged(hasFocus);
7119 }
7120
7121 /**
7122 * Called when the window containing this view gains or loses focus. Note
7123 * that this is separate from view focus: to receive key events, both
7124 * your view and its window must have focus. If a window is displayed
7125 * on top of yours that takes input focus, then your own window will lose
7126 * focus but the view focus will remain unchanged.
7127 *
7128 * @param hasWindowFocus True if the window containing this view now has
7129 * focus, false otherwise.
7130 */
7131 public void onWindowFocusChanged(boolean hasWindowFocus) {
7132 InputMethodManager imm = InputMethodManager.peekInstance();
7133 if (!hasWindowFocus) {
7134 if (isPressed()) {
7135 setPressed(false);
7136 }
7137 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
7138 imm.focusOut(this);
7139 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007140 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08007141 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07007142 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007143 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
7144 imm.focusIn(this);
7145 }
7146 refreshDrawableState();
7147 }
7148
7149 /**
7150 * Returns true if this view is in a window that currently has window focus.
7151 * Note that this is not the same as the view itself having focus.
7152 *
7153 * @return True if this view is in a window that currently has window focus.
7154 */
7155 public boolean hasWindowFocus() {
7156 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
7157 }
7158
7159 /**
Adam Powell326d8082009-12-09 15:10:07 -08007160 * Dispatch a view visibility change down the view hierarchy.
7161 * ViewGroups should override to route to their children.
7162 * @param changedView The view whose visibility changed. Could be 'this' or
7163 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007164 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7165 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007166 */
7167 protected void dispatchVisibilityChanged(View changedView, int visibility) {
7168 onVisibilityChanged(changedView, visibility);
7169 }
7170
7171 /**
7172 * Called when the visibility of the view or an ancestor of the view is changed.
7173 * @param changedView The view whose visibility changed. Could be 'this' or
7174 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007175 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7176 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007177 */
7178 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007179 if (visibility == VISIBLE) {
7180 if (mAttachInfo != null) {
7181 initialAwakenScrollBars();
7182 } else {
7183 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
7184 }
7185 }
Adam Powell326d8082009-12-09 15:10:07 -08007186 }
7187
7188 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08007189 * Dispatch a hint about whether this view is displayed. For instance, when
7190 * a View moves out of the screen, it might receives a display hint indicating
7191 * the view is not displayed. Applications should not <em>rely</em> on this hint
7192 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007193 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007194 * @param hint A hint about whether or not this view is displayed:
7195 * {@link #VISIBLE} or {@link #INVISIBLE}.
7196 */
7197 public void dispatchDisplayHint(int hint) {
7198 onDisplayHint(hint);
7199 }
7200
7201 /**
7202 * Gives this view a hint about whether is displayed or not. For instance, when
7203 * a View moves out of the screen, it might receives a display hint indicating
7204 * the view is not displayed. Applications should not <em>rely</em> on this hint
7205 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007206 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007207 * @param hint A hint about whether or not this view is displayed:
7208 * {@link #VISIBLE} or {@link #INVISIBLE}.
7209 */
7210 protected void onDisplayHint(int hint) {
7211 }
7212
7213 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007214 * Dispatch a window visibility change down the view hierarchy.
7215 * ViewGroups should override to route to their children.
7216 *
7217 * @param visibility The new visibility of the window.
7218 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007219 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007220 */
7221 public void dispatchWindowVisibilityChanged(int visibility) {
7222 onWindowVisibilityChanged(visibility);
7223 }
7224
7225 /**
7226 * Called when the window containing has change its visibility
7227 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
7228 * that this tells you whether or not your window is being made visible
7229 * to the window manager; this does <em>not</em> tell you whether or not
7230 * your window is obscured by other windows on the screen, even if it
7231 * is itself visible.
7232 *
7233 * @param visibility The new visibility of the window.
7234 */
7235 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007236 if (visibility == VISIBLE) {
7237 initialAwakenScrollBars();
7238 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007239 }
7240
7241 /**
7242 * Returns the current visibility of the window this view is attached to
7243 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
7244 *
7245 * @return Returns the current visibility of the view's window.
7246 */
7247 public int getWindowVisibility() {
7248 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
7249 }
7250
7251 /**
7252 * Retrieve the overall visible display size in which the window this view is
7253 * attached to has been positioned in. This takes into account screen
7254 * decorations above the window, for both cases where the window itself
7255 * is being position inside of them or the window is being placed under
7256 * then and covered insets are used for the window to position its content
7257 * inside. In effect, this tells you the available area where content can
7258 * be placed and remain visible to users.
7259 *
7260 * <p>This function requires an IPC back to the window manager to retrieve
7261 * the requested information, so should not be used in performance critical
7262 * code like drawing.
7263 *
7264 * @param outRect Filled in with the visible display frame. If the view
7265 * is not attached to a window, this is simply the raw display size.
7266 */
7267 public void getWindowVisibleDisplayFrame(Rect outRect) {
7268 if (mAttachInfo != null) {
7269 try {
7270 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
7271 } catch (RemoteException e) {
7272 return;
7273 }
7274 // XXX This is really broken, and probably all needs to be done
7275 // in the window manager, and we need to know more about whether
7276 // we want the area behind or in front of the IME.
7277 final Rect insets = mAttachInfo.mVisibleInsets;
7278 outRect.left += insets.left;
7279 outRect.top += insets.top;
7280 outRect.right -= insets.right;
7281 outRect.bottom -= insets.bottom;
7282 return;
7283 }
7284 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07007285 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007286 }
7287
7288 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007289 * Dispatch a notification about a resource configuration change down
7290 * the view hierarchy.
7291 * ViewGroups should override to route to their children.
7292 *
7293 * @param newConfig The new resource configuration.
7294 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007295 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007296 */
7297 public void dispatchConfigurationChanged(Configuration newConfig) {
7298 onConfigurationChanged(newConfig);
7299 }
7300
7301 /**
7302 * Called when the current configuration of the resources being used
7303 * by the application have changed. You can use this to decide when
7304 * to reload resources that can changed based on orientation and other
7305 * configuration characterstics. You only need to use this if you are
7306 * not relying on the normal {@link android.app.Activity} mechanism of
7307 * recreating the activity instance upon a configuration change.
7308 *
7309 * @param newConfig The new resource configuration.
7310 */
7311 protected void onConfigurationChanged(Configuration newConfig) {
7312 }
7313
7314 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007315 * Private function to aggregate all per-view attributes in to the view
7316 * root.
7317 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007318 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7319 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007320 }
7321
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007322 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7323 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08007324 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007325 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007326 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007327 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007328 ListenerInfo li = mListenerInfo;
7329 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007330 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007331 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007332 }
7333 }
7334
7335 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08007336 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007337 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08007338 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
7339 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007340 ai.mRecomputeGlobalAttributes = true;
7341 }
7342 }
7343 }
7344
7345 /**
7346 * Returns whether the device is currently in touch mode. Touch mode is entered
7347 * once the user begins interacting with the device by touch, and affects various
7348 * things like whether focus is always visible to the user.
7349 *
7350 * @return Whether the device is in touch mode.
7351 */
7352 @ViewDebug.ExportedProperty
7353 public boolean isInTouchMode() {
7354 if (mAttachInfo != null) {
7355 return mAttachInfo.mInTouchMode;
7356 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07007357 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007358 }
7359 }
7360
7361 /**
7362 * Returns the context the view is running in, through which it can
7363 * access the current theme, resources, etc.
7364 *
7365 * @return The view's Context.
7366 */
7367 @ViewDebug.CapturedViewProperty
7368 public final Context getContext() {
7369 return mContext;
7370 }
7371
7372 /**
7373 * Handle a key event before it is processed by any input method
7374 * associated with the view hierarchy. This can be used to intercept
7375 * key events in special situations before the IME consumes them; a
7376 * typical example would be handling the BACK key to update the application's
7377 * UI instead of allowing the IME to see it and close itself.
7378 *
7379 * @param keyCode The value in event.getKeyCode().
7380 * @param event Description of the key event.
7381 * @return If you handled the event, return true. If you want to allow the
7382 * event to be handled by the next receiver, return false.
7383 */
7384 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7385 return false;
7386 }
7387
7388 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007389 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7390 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007391 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7392 * is released, if the view is enabled and clickable.
7393 *
Jean Chalard405bc512012-05-29 19:12:34 +09007394 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7395 * although some may elect to do so in some situations. Do not rely on this to
7396 * catch software key presses.
7397 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007398 * @param keyCode A key code that represents the button pressed, from
7399 * {@link android.view.KeyEvent}.
7400 * @param event The KeyEvent object that defines the button action.
7401 */
7402 public boolean onKeyDown(int keyCode, KeyEvent event) {
7403 boolean result = false;
7404
7405 switch (keyCode) {
7406 case KeyEvent.KEYCODE_DPAD_CENTER:
7407 case KeyEvent.KEYCODE_ENTER: {
7408 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7409 return true;
7410 }
7411 // Long clickable items don't necessarily have to be clickable
7412 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7413 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7414 (event.getRepeatCount() == 0)) {
7415 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007416 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007417 return true;
7418 }
7419 break;
7420 }
7421 }
7422 return result;
7423 }
7424
7425 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007426 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7427 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7428 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007429 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7430 * although some may elect to do so in some situations. Do not rely on this to
7431 * catch software key presses.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007432 */
7433 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7434 return false;
7435 }
7436
7437 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007438 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7439 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007440 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7441 * {@link KeyEvent#KEYCODE_ENTER} is released.
Jean Chalard405bc512012-05-29 19:12:34 +09007442 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7443 * although some may elect to do so in some situations. Do not rely on this to
7444 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007445 *
7446 * @param keyCode A key code that represents the button pressed, from
7447 * {@link android.view.KeyEvent}.
7448 * @param event The KeyEvent object that defines the button action.
7449 */
7450 public boolean onKeyUp(int keyCode, KeyEvent event) {
7451 boolean result = false;
7452
7453 switch (keyCode) {
7454 case KeyEvent.KEYCODE_DPAD_CENTER:
7455 case KeyEvent.KEYCODE_ENTER: {
7456 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7457 return true;
7458 }
7459 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7460 setPressed(false);
7461
7462 if (!mHasPerformedLongPress) {
7463 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007464 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007465
7466 result = performClick();
7467 }
7468 }
7469 break;
7470 }
7471 }
7472 return result;
7473 }
7474
7475 /**
7476 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7477 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7478 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007479 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7480 * although some may elect to do so in some situations. Do not rely on this to
7481 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007482 *
7483 * @param keyCode A key code that represents the button pressed, from
7484 * {@link android.view.KeyEvent}.
7485 * @param repeatCount The number of times the action was made.
7486 * @param event The KeyEvent object that defines the button action.
7487 */
7488 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7489 return false;
7490 }
7491
7492 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007493 * Called on the focused view when a key shortcut event is not handled.
7494 * Override this method to implement local key shortcuts for the View.
7495 * Key shortcuts can also be implemented by setting the
7496 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007497 *
7498 * @param keyCode The value in event.getKeyCode().
7499 * @param event Description of the key event.
7500 * @return If you handled the event, return true. If you want to allow the
7501 * event to be handled by the next receiver, return false.
7502 */
7503 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7504 return false;
7505 }
7506
7507 /**
7508 * Check whether the called view is a text editor, in which case it
7509 * would make sense to automatically display a soft input window for
7510 * it. Subclasses should override this if they implement
7511 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007512 * a call on that method would return a non-null InputConnection, and
7513 * they are really a first-class editor that the user would normally
7514 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007515 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007516 * <p>The default implementation always returns false. This does
7517 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7518 * will not be called or the user can not otherwise perform edits on your
7519 * view; it is just a hint to the system that this is not the primary
7520 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007521 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007522 * @return Returns true if this view is a text editor, else false.
7523 */
7524 public boolean onCheckIsTextEditor() {
7525 return false;
7526 }
Romain Guy8506ab42009-06-11 17:35:47 -07007527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007528 /**
7529 * Create a new InputConnection for an InputMethod to interact
7530 * with the view. The default implementation returns null, since it doesn't
7531 * support input methods. You can override this to implement such support.
7532 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007533 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007534 * <p>When implementing this, you probably also want to implement
7535 * {@link #onCheckIsTextEditor()} to indicate you will return a
7536 * non-null InputConnection.
7537 *
7538 * @param outAttrs Fill in with attribute information about the connection.
7539 */
7540 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7541 return null;
7542 }
7543
7544 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007545 * Called by the {@link android.view.inputmethod.InputMethodManager}
7546 * when a view who is not the current
7547 * input connection target is trying to make a call on the manager. The
7548 * default implementation returns false; you can override this to return
7549 * true for certain views if you are performing InputConnection proxying
7550 * to them.
7551 * @param view The View that is making the InputMethodManager call.
7552 * @return Return true to allow the call, false to reject.
7553 */
7554 public boolean checkInputConnectionProxy(View view) {
7555 return false;
7556 }
Romain Guy8506ab42009-06-11 17:35:47 -07007557
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007558 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007559 * Show the context menu for this view. It is not safe to hold on to the
7560 * menu after returning from this method.
7561 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007562 * You should normally not overload this method. Overload
7563 * {@link #onCreateContextMenu(ContextMenu)} or define an
7564 * {@link OnCreateContextMenuListener} to add items to the context menu.
7565 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007566 * @param menu The context menu to populate
7567 */
7568 public void createContextMenu(ContextMenu menu) {
7569 ContextMenuInfo menuInfo = getContextMenuInfo();
7570
7571 // Sets the current menu info so all items added to menu will have
7572 // my extra info set.
7573 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7574
7575 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007576 ListenerInfo li = mListenerInfo;
7577 if (li != null && li.mOnCreateContextMenuListener != null) {
7578 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007579 }
7580
7581 // Clear the extra information so subsequent items that aren't mine don't
7582 // have my extra info.
7583 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7584
7585 if (mParent != null) {
7586 mParent.createContextMenu(menu);
7587 }
7588 }
7589
7590 /**
7591 * Views should implement this if they have extra information to associate
7592 * with the context menu. The return result is supplied as a parameter to
7593 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7594 * callback.
7595 *
7596 * @return Extra information about the item for which the context menu
7597 * should be shown. This information will vary across different
7598 * subclasses of View.
7599 */
7600 protected ContextMenuInfo getContextMenuInfo() {
7601 return null;
7602 }
7603
7604 /**
7605 * Views should implement this if the view itself is going to add items to
7606 * the context menu.
7607 *
7608 * @param menu the context menu to populate
7609 */
7610 protected void onCreateContextMenu(ContextMenu menu) {
7611 }
7612
7613 /**
7614 * Implement this method to handle trackball motion events. The
7615 * <em>relative</em> movement of the trackball since the last event
7616 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
7617 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
7618 * that a movement of 1 corresponds to the user pressing one DPAD key (so
7619 * they will often be fractional values, representing the more fine-grained
7620 * movement information available from a trackball).
7621 *
7622 * @param event The motion event.
7623 * @return True if the event was handled, false otherwise.
7624 */
7625 public boolean onTrackballEvent(MotionEvent event) {
7626 return false;
7627 }
7628
7629 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08007630 * Implement this method to handle generic motion events.
7631 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08007632 * Generic motion events describe joystick movements, mouse hovers, track pad
7633 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08007634 * {@link MotionEvent#getSource() source} of the motion event specifies
7635 * the class of input that was received. Implementations of this method
7636 * must examine the bits in the source before processing the event.
7637 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007638 * </p><p>
7639 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7640 * are delivered to the view under the pointer. All other generic motion events are
7641 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08007642 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07007643 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08007644 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007645 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
7646 * // process the joystick movement...
7647 * return true;
7648 * }
7649 * }
7650 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
7651 * switch (event.getAction()) {
7652 * case MotionEvent.ACTION_HOVER_MOVE:
7653 * // process the mouse hover movement...
7654 * return true;
7655 * case MotionEvent.ACTION_SCROLL:
7656 * // process the scroll wheel movement...
7657 * return true;
7658 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08007659 * }
7660 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07007661 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08007662 *
7663 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08007664 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08007665 */
7666 public boolean onGenericMotionEvent(MotionEvent event) {
7667 return false;
7668 }
7669
7670 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007671 * Implement this method to handle hover events.
7672 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07007673 * This method is called whenever a pointer is hovering into, over, or out of the
7674 * bounds of a view and the view is not currently being touched.
7675 * Hover events are represented as pointer events with action
7676 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
7677 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
7678 * </p>
7679 * <ul>
7680 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
7681 * when the pointer enters the bounds of the view.</li>
7682 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
7683 * when the pointer has already entered the bounds of the view and has moved.</li>
7684 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
7685 * when the pointer has exited the bounds of the view or when the pointer is
7686 * about to go down due to a button click, tap, or similar user action that
7687 * causes the view to be touched.</li>
7688 * </ul>
7689 * <p>
7690 * The view should implement this method to return true to indicate that it is
7691 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08007692 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07007693 * The default implementation calls {@link #setHovered} to update the hovered state
7694 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07007695 * is enabled and is clickable. The default implementation also sends hover
7696 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08007697 * </p>
7698 *
7699 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07007700 * @return True if the view handled the hover event.
7701 *
7702 * @see #isHovered
7703 * @see #setHovered
7704 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007705 */
7706 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007707 // The root view may receive hover (or touch) events that are outside the bounds of
7708 // the window. This code ensures that we only send accessibility events for
7709 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07007710 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007711 if (!mSendingHoverAccessibilityEvents) {
7712 if ((action == MotionEvent.ACTION_HOVER_ENTER
7713 || action == MotionEvent.ACTION_HOVER_MOVE)
7714 && !hasHoveredChild()
7715 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007716 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007717 mSendingHoverAccessibilityEvents = true;
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07007718 requestAccessibilityFocusFromHover();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007719 }
7720 } else {
7721 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07007722 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007723 && !pointInView(event.getX(), event.getY()))) {
7724 mSendingHoverAccessibilityEvents = false;
7725 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007726 // If the window does not have input focus we take away accessibility
7727 // focus as soon as the user stop hovering over the view.
Jeff Brown59a422e2012-04-19 15:19:19 -07007728 if (mAttachInfo != null && !mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007729 getViewRootImpl().setAccessibilityFocusedHost(null);
7730 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007731 }
Jeff Browna1b24182011-07-28 13:38:24 -07007732 }
7733
Jeff Brown87b7f802011-06-21 18:35:45 -07007734 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007735 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07007736 case MotionEvent.ACTION_HOVER_ENTER:
7737 setHovered(true);
7738 break;
7739 case MotionEvent.ACTION_HOVER_EXIT:
7740 setHovered(false);
7741 break;
7742 }
Jeff Browna1b24182011-07-28 13:38:24 -07007743
7744 // Dispatch the event to onGenericMotionEvent before returning true.
7745 // This is to provide compatibility with existing applications that
7746 // handled HOVER_MOVE events in onGenericMotionEvent and that would
7747 // break because of the new default handling for hoverable views
7748 // in onHoverEvent.
7749 // Note that onGenericMotionEvent will be called by default when
7750 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
7751 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07007752 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08007753 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007754
Svetoslav Ganov736c2752011-04-22 18:30:36 -07007755 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08007756 }
7757
7758 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007759 * Returns true if the view should handle {@link #onHoverEvent}
7760 * by calling {@link #setHovered} to change its hovered state.
7761 *
7762 * @return True if the view is hoverable.
7763 */
7764 private boolean isHoverable() {
7765 final int viewFlags = mViewFlags;
7766 if ((viewFlags & ENABLED_MASK) == DISABLED) {
7767 return false;
7768 }
7769
7770 return (viewFlags & CLICKABLE) == CLICKABLE
7771 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
7772 }
7773
7774 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007775 * Returns true if the view is currently hovered.
7776 *
7777 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007778 *
7779 * @see #setHovered
7780 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007781 */
Jeff Brown10b62902011-06-20 16:40:37 -07007782 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08007783 public boolean isHovered() {
7784 return (mPrivateFlags & HOVERED) != 0;
7785 }
7786
7787 /**
7788 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007789 * <p>
7790 * Calling this method also changes the drawable state of the view. This
7791 * enables the view to react to hover by using different drawable resources
7792 * to change its appearance.
7793 * </p><p>
7794 * The {@link #onHoverChanged} method is called when the hovered state changes.
7795 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08007796 *
7797 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007798 *
7799 * @see #isHovered
7800 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007801 */
7802 public void setHovered(boolean hovered) {
7803 if (hovered) {
7804 if ((mPrivateFlags & HOVERED) == 0) {
7805 mPrivateFlags |= HOVERED;
7806 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007807 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08007808 }
7809 } else {
7810 if ((mPrivateFlags & HOVERED) != 0) {
7811 mPrivateFlags &= ~HOVERED;
7812 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007813 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08007814 }
7815 }
7816 }
7817
7818 /**
Jeff Brown10b62902011-06-20 16:40:37 -07007819 * Implement this method to handle hover state changes.
7820 * <p>
7821 * This method is called whenever the hover state changes as a result of a
7822 * call to {@link #setHovered}.
7823 * </p>
7824 *
7825 * @param hovered The current hover state, as returned by {@link #isHovered}.
7826 *
7827 * @see #isHovered
7828 * @see #setHovered
7829 */
7830 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07007831 }
7832
7833 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007834 * Implement this method to handle touch screen motion events.
7835 *
7836 * @param event The motion event.
7837 * @return True if the event was handled, false otherwise.
7838 */
7839 public boolean onTouchEvent(MotionEvent event) {
7840 final int viewFlags = mViewFlags;
7841
7842 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007843 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08007844 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007845 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007846 // A disabled view that is clickable still consumes the touch
7847 // events, it just doesn't respond to them.
7848 return (((viewFlags & CLICKABLE) == CLICKABLE ||
7849 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
7850 }
7851
7852 if (mTouchDelegate != null) {
7853 if (mTouchDelegate.onTouchEvent(event)) {
7854 return true;
7855 }
7856 }
7857
7858 if (((viewFlags & CLICKABLE) == CLICKABLE ||
7859 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
7860 switch (event.getAction()) {
7861 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08007862 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
7863 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007864 // take focus if we don't have it already and we should in
7865 // touch mode.
7866 boolean focusTaken = false;
7867 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
7868 focusTaken = requestFocus();
7869 }
7870
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007871 if (prepressed) {
7872 // The button is being released before we actually
7873 // showed it as pressed. Make it show the pressed
7874 // state now (before scheduling the click) to ensure
7875 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08007876 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007877 }
Joe Malin32736f02011-01-19 16:14:20 -08007878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007879 if (!mHasPerformedLongPress) {
7880 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007881 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007882
7883 // Only perform take click actions if we were in the pressed state
7884 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08007885 // Use a Runnable and post this rather than calling
7886 // performClick directly. This lets other visual state
7887 // of the view update before click actions start.
7888 if (mPerformClick == null) {
7889 mPerformClick = new PerformClick();
7890 }
7891 if (!post(mPerformClick)) {
7892 performClick();
7893 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007894 }
7895 }
7896
7897 if (mUnsetPressedState == null) {
7898 mUnsetPressedState = new UnsetPressedState();
7899 }
7900
Adam Powelle14579b2009-12-16 18:39:52 -08007901 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08007902 postDelayed(mUnsetPressedState,
7903 ViewConfiguration.getPressedStateDuration());
7904 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007905 // If the post failed, unpress right now
7906 mUnsetPressedState.run();
7907 }
Adam Powelle14579b2009-12-16 18:39:52 -08007908 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007909 }
7910 break;
7911
7912 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08007913 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007914
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07007915 if (performButtonActionOnTouchDown(event)) {
7916 break;
7917 }
7918
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007919 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07007920 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007921
7922 // For views inside a scrolling container, delay the pressed feedback for
7923 // a short period in case this is a scroll.
7924 if (isInScrollingContainer) {
7925 mPrivateFlags |= PREPRESSED;
7926 if (mPendingCheckForTap == null) {
7927 mPendingCheckForTap = new CheckForTap();
7928 }
7929 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
7930 } else {
7931 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08007932 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007933 checkForLongClick(0);
7934 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007935 break;
7936
7937 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08007938 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08007939 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007940 break;
7941
7942 case MotionEvent.ACTION_MOVE:
7943 final int x = (int) event.getX();
7944 final int y = (int) event.getY();
7945
7946 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07007947 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007948 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08007949 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007950 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08007951 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05007952 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007953
Adam Powell4d6f0662012-02-21 15:11:11 -08007954 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007955 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007956 }
7957 break;
7958 }
7959 return true;
7960 }
7961
7962 return false;
7963 }
7964
7965 /**
Adam Powell10298662011-08-14 18:26:30 -07007966 * @hide
7967 */
7968 public boolean isInScrollingContainer() {
7969 ViewParent p = getParent();
7970 while (p != null && p instanceof ViewGroup) {
7971 if (((ViewGroup) p).shouldDelayChildPressedState()) {
7972 return true;
7973 }
7974 p = p.getParent();
7975 }
7976 return false;
7977 }
7978
7979 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05007980 * Remove the longpress detection timer.
7981 */
7982 private void removeLongPressCallback() {
7983 if (mPendingCheckForLongPress != null) {
7984 removeCallbacks(mPendingCheckForLongPress);
7985 }
7986 }
Adam Powell3cb8b632011-01-21 15:34:14 -08007987
7988 /**
7989 * Remove the pending click action
7990 */
7991 private void removePerformClickCallback() {
7992 if (mPerformClick != null) {
7993 removeCallbacks(mPerformClick);
7994 }
7995 }
7996
Adam Powelle14579b2009-12-16 18:39:52 -08007997 /**
Romain Guya440b002010-02-24 15:57:54 -08007998 * Remove the prepress detection timer.
7999 */
8000 private void removeUnsetPressCallback() {
8001 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
8002 setPressed(false);
8003 removeCallbacks(mUnsetPressedState);
8004 }
8005 }
8006
8007 /**
Adam Powelle14579b2009-12-16 18:39:52 -08008008 * Remove the tap detection timer.
8009 */
8010 private void removeTapCallback() {
8011 if (mPendingCheckForTap != null) {
8012 mPrivateFlags &= ~PREPRESSED;
8013 removeCallbacks(mPendingCheckForTap);
8014 }
8015 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05008016
8017 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008018 * Cancels a pending long press. Your subclass can use this if you
8019 * want the context menu to come up if the user presses and holds
8020 * at the same place, but you don't want it to come up if they press
8021 * and then move around enough to cause scrolling.
8022 */
8023 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05008024 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08008025
8026 /*
8027 * The prepressed state handled by the tap callback is a display
8028 * construct, but the tap callback will post a long press callback
8029 * less its own timeout. Remove it here.
8030 */
8031 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008032 }
8033
8034 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008035 * Remove the pending callback for sending a
8036 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
8037 */
8038 private void removeSendViewScrolledAccessibilityEventCallback() {
8039 if (mSendViewScrolledAccessibilityEvent != null) {
8040 removeCallbacks(mSendViewScrolledAccessibilityEvent);
8041 }
8042 }
8043
8044 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008045 * Sets the TouchDelegate for this View.
8046 */
8047 public void setTouchDelegate(TouchDelegate delegate) {
8048 mTouchDelegate = delegate;
8049 }
8050
8051 /**
8052 * Gets the TouchDelegate for this View.
8053 */
8054 public TouchDelegate getTouchDelegate() {
8055 return mTouchDelegate;
8056 }
8057
8058 /**
8059 * Set flags controlling behavior of this view.
8060 *
8061 * @param flags Constant indicating the value which should be set
8062 * @param mask Constant indicating the bit range that should be changed
8063 */
8064 void setFlags(int flags, int mask) {
8065 int old = mViewFlags;
8066 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
8067
8068 int changed = mViewFlags ^ old;
8069 if (changed == 0) {
8070 return;
8071 }
8072 int privateFlags = mPrivateFlags;
8073
8074 /* Check if the FOCUSABLE bit has changed */
8075 if (((changed & FOCUSABLE_MASK) != 0) &&
8076 ((privateFlags & HAS_BOUNDS) !=0)) {
8077 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
8078 && ((privateFlags & FOCUSED) != 0)) {
8079 /* Give up focus if we are no longer focusable */
8080 clearFocus();
8081 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
8082 && ((privateFlags & FOCUSED) == 0)) {
8083 /*
8084 * Tell the view system that we are now available to take focus
8085 * if no one else already has it.
8086 */
8087 if (mParent != null) mParent.focusableViewAvailable(this);
8088 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008089 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8090 notifyAccessibilityStateChanged();
8091 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008092 }
8093
8094 if ((flags & VISIBILITY_MASK) == VISIBLE) {
8095 if ((changed & VISIBILITY_MASK) != 0) {
8096 /*
Chet Haase4324ead2011-08-24 21:31:03 -07008097 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07008098 * it was not visible. Marking it drawn ensures that the invalidation will
8099 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008100 */
Chet Haaseaceafe62011-08-26 15:44:33 -07008101 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07008102 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008103
8104 needGlobalAttributesUpdate(true);
8105
8106 // a view becoming visible is worth notifying the parent
8107 // about in case nothing has focus. even if this specific view
8108 // isn't focusable, it may contain something that is, so let
8109 // the root view try to give this focus if nothing else does.
8110 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
8111 mParent.focusableViewAvailable(this);
8112 }
8113 }
8114 }
8115
8116 /* Check if the GONE bit has changed */
8117 if ((changed & GONE) != 0) {
8118 needGlobalAttributesUpdate(false);
8119 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008120
Romain Guyecd80ee2009-12-03 17:13:02 -08008121 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
8122 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008123 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08008124 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07008125 if (mParent instanceof View) {
8126 // GONE views noop invalidation, so invalidate the parent
8127 ((View) mParent).invalidate(true);
8128 }
8129 // Mark the view drawn to ensure that it gets invalidated properly the next
8130 // time it is visible and gets invalidated
8131 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008132 }
8133 if (mAttachInfo != null) {
8134 mAttachInfo.mViewVisibilityChanged = true;
8135 }
8136 }
8137
8138 /* Check if the VISIBLE bit has changed */
8139 if ((changed & INVISIBLE) != 0) {
8140 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07008141 /*
8142 * If this view is becoming invisible, set the DRAWN flag so that
8143 * the next invalidate() will not be skipped.
8144 */
8145 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008146
8147 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008148 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008149 if (getRootView() != this) {
8150 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008151 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008152 }
8153 }
8154 if (mAttachInfo != null) {
8155 mAttachInfo.mViewVisibilityChanged = true;
8156 }
8157 }
8158
Adam Powell326d8082009-12-09 15:10:07 -08008159 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07008160 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08008161 ((ViewGroup) mParent).onChildVisibilityChanged(this,
8162 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08008163 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07008164 } else if (mParent != null) {
8165 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07008166 }
Adam Powell326d8082009-12-09 15:10:07 -08008167 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
8168 }
8169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008170 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
8171 destroyDrawingCache();
8172 }
8173
8174 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
8175 destroyDrawingCache();
8176 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008177 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008178 }
8179
8180 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
8181 destroyDrawingCache();
8182 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8183 }
8184
8185 if ((changed & DRAW_MASK) != 0) {
8186 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07008187 if (mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008188 mPrivateFlags &= ~SKIP_DRAW;
8189 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
8190 } else {
8191 mPrivateFlags |= SKIP_DRAW;
8192 }
8193 } else {
8194 mPrivateFlags &= ~SKIP_DRAW;
8195 }
8196 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08008197 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008198 }
8199
8200 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08008201 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008202 mParent.recomputeViewAttributes(this);
8203 }
8204 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008205
8206 if (AccessibilityManager.getInstance(mContext).isEnabled()
8207 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
8208 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
8209 notifyAccessibilityStateChanged();
8210 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008211 }
8212
8213 /**
8214 * Change the view's z order in the tree, so it's on top of other sibling
8215 * views
8216 */
8217 public void bringToFront() {
8218 if (mParent != null) {
8219 mParent.bringChildToFront(this);
8220 }
8221 }
8222
8223 /**
8224 * This is called in response to an internal scroll in this view (i.e., the
8225 * view scrolled its own contents). This is typically as a result of
8226 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
8227 * called.
8228 *
8229 * @param l Current horizontal scroll origin.
8230 * @param t Current vertical scroll origin.
8231 * @param oldl Previous horizontal scroll origin.
8232 * @param oldt Previous vertical scroll origin.
8233 */
8234 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07008235 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8236 postSendViewScrolledAccessibilityEventCallback();
8237 }
8238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008239 mBackgroundSizeChanged = true;
8240
8241 final AttachInfo ai = mAttachInfo;
8242 if (ai != null) {
8243 ai.mViewScrollChanged = true;
8244 }
8245 }
8246
8247 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008248 * Interface definition for a callback to be invoked when the layout bounds of a view
8249 * changes due to layout processing.
8250 */
8251 public interface OnLayoutChangeListener {
8252 /**
8253 * Called when the focus state of a view has changed.
8254 *
8255 * @param v The view whose state has changed.
8256 * @param left The new value of the view's left property.
8257 * @param top The new value of the view's top property.
8258 * @param right The new value of the view's right property.
8259 * @param bottom The new value of the view's bottom property.
8260 * @param oldLeft The previous value of the view's left property.
8261 * @param oldTop The previous value of the view's top property.
8262 * @param oldRight The previous value of the view's right property.
8263 * @param oldBottom The previous value of the view's bottom property.
8264 */
8265 void onLayoutChange(View v, int left, int top, int right, int bottom,
8266 int oldLeft, int oldTop, int oldRight, int oldBottom);
8267 }
8268
8269 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008270 * This is called during layout when the size of this view has changed. If
8271 * you were just added to the view hierarchy, you're called with the old
8272 * values of 0.
8273 *
8274 * @param w Current width of this view.
8275 * @param h Current height of this view.
8276 * @param oldw Old width of this view.
8277 * @param oldh Old height of this view.
8278 */
8279 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
8280 }
8281
8282 /**
8283 * Called by draw to draw the child views. This may be overridden
8284 * by derived classes to gain control just before its children are drawn
8285 * (but after its own view has been drawn).
8286 * @param canvas the canvas on which to draw the view
8287 */
8288 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008290 }
8291
8292 /**
8293 * Gets the parent of this view. Note that the parent is a
8294 * ViewParent and not necessarily a View.
8295 *
8296 * @return Parent of this view.
8297 */
8298 public final ViewParent getParent() {
8299 return mParent;
8300 }
8301
8302 /**
Chet Haasecca2c982011-05-20 14:34:18 -07008303 * Set the horizontal scrolled position of your view. This will cause a call to
8304 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8305 * invalidated.
8306 * @param value the x position to scroll to
8307 */
8308 public void setScrollX(int value) {
8309 scrollTo(value, mScrollY);
8310 }
8311
8312 /**
8313 * Set the vertical scrolled position of your view. This will cause a call to
8314 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8315 * invalidated.
8316 * @param value the y position to scroll to
8317 */
8318 public void setScrollY(int value) {
8319 scrollTo(mScrollX, value);
8320 }
8321
8322 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008323 * Return the scrolled left position of this view. This is the left edge of
8324 * the displayed part of your view. You do not need to draw any pixels
8325 * farther left, since those are outside of the frame of your view on
8326 * screen.
8327 *
8328 * @return The left edge of the displayed part of your view, in pixels.
8329 */
8330 public final int getScrollX() {
8331 return mScrollX;
8332 }
8333
8334 /**
8335 * Return the scrolled top position of this view. This is the top edge of
8336 * the displayed part of your view. You do not need to draw any pixels above
8337 * it, since those are outside of the frame of your view on screen.
8338 *
8339 * @return The top edge of the displayed part of your view, in pixels.
8340 */
8341 public final int getScrollY() {
8342 return mScrollY;
8343 }
8344
8345 /**
8346 * Return the width of the your view.
8347 *
8348 * @return The width of your view, in pixels.
8349 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008350 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008351 public final int getWidth() {
8352 return mRight - mLeft;
8353 }
8354
8355 /**
8356 * Return the height of your view.
8357 *
8358 * @return The height of your view, in pixels.
8359 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008360 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008361 public final int getHeight() {
8362 return mBottom - mTop;
8363 }
8364
8365 /**
8366 * Return the visible drawing bounds of your view. Fills in the output
8367 * rectangle with the values from getScrollX(), getScrollY(),
8368 * getWidth(), and getHeight().
8369 *
8370 * @param outRect The (scrolled) drawing bounds of the view.
8371 */
8372 public void getDrawingRect(Rect outRect) {
8373 outRect.left = mScrollX;
8374 outRect.top = mScrollY;
8375 outRect.right = mScrollX + (mRight - mLeft);
8376 outRect.bottom = mScrollY + (mBottom - mTop);
8377 }
8378
8379 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008380 * Like {@link #getMeasuredWidthAndState()}, but only returns the
8381 * raw width component (that is the result is masked by
8382 * {@link #MEASURED_SIZE_MASK}).
8383 *
8384 * @return The raw measured width of this view.
8385 */
8386 public final int getMeasuredWidth() {
8387 return mMeasuredWidth & MEASURED_SIZE_MASK;
8388 }
8389
8390 /**
8391 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008392 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008393 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008394 * This should be used during measurement and layout calculations only. Use
8395 * {@link #getWidth()} to see how wide a view is after layout.
8396 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008397 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008398 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008399 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008400 return mMeasuredWidth;
8401 }
8402
8403 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008404 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8405 * raw width component (that is the result is masked by
8406 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008407 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008408 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008409 */
8410 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008411 return mMeasuredHeight & MEASURED_SIZE_MASK;
8412 }
8413
8414 /**
8415 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008416 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008417 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8418 * This should be used during measurement and layout calculations only. Use
8419 * {@link #getHeight()} to see how wide a view is after layout.
8420 *
8421 * @return The measured width of this view as a bit mask.
8422 */
8423 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008424 return mMeasuredHeight;
8425 }
8426
8427 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008428 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8429 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8430 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8431 * and the height component is at the shifted bits
8432 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8433 */
8434 public final int getMeasuredState() {
8435 return (mMeasuredWidth&MEASURED_STATE_MASK)
8436 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8437 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8438 }
8439
8440 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008441 * The transform matrix of this view, which is calculated based on the current
8442 * roation, scale, and pivot properties.
8443 *
8444 * @see #getRotation()
8445 * @see #getScaleX()
8446 * @see #getScaleY()
8447 * @see #getPivotX()
8448 * @see #getPivotY()
8449 * @return The current transform matrix for the view
8450 */
8451 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008452 if (mTransformationInfo != null) {
8453 updateMatrix();
8454 return mTransformationInfo.mMatrix;
8455 }
8456 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008457 }
8458
8459 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008460 * Utility function to determine if the value is far enough away from zero to be
8461 * considered non-zero.
8462 * @param value A floating point value to check for zero-ness
8463 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8464 */
8465 private static boolean nonzero(float value) {
8466 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8467 }
8468
8469 /**
Jeff Brown86671742010-09-30 20:00:15 -07008470 * Returns true if the transform matrix is the identity matrix.
8471 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008472 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008473 * @return True if the transform matrix is the identity matrix, false otherwise.
8474 */
Jeff Brown86671742010-09-30 20:00:15 -07008475 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008476 if (mTransformationInfo != null) {
8477 updateMatrix();
8478 return mTransformationInfo.mMatrixIsIdentity;
8479 }
8480 return true;
8481 }
8482
8483 void ensureTransformationInfo() {
8484 if (mTransformationInfo == null) {
8485 mTransformationInfo = new TransformationInfo();
8486 }
Jeff Brown86671742010-09-30 20:00:15 -07008487 }
8488
8489 /**
8490 * Recomputes the transform matrix if necessary.
8491 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008492 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008493 final TransformationInfo info = mTransformationInfo;
8494 if (info == null) {
8495 return;
8496 }
8497 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008498 // transform-related properties have changed since the last time someone
8499 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008500
8501 // Figure out if we need to update the pivot point
8502 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008503 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8504 info.mPrevWidth = mRight - mLeft;
8505 info.mPrevHeight = mBottom - mTop;
8506 info.mPivotX = info.mPrevWidth / 2f;
8507 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008508 }
8509 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008510 info.mMatrix.reset();
8511 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8512 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8513 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8514 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008515 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008516 if (info.mCamera == null) {
8517 info.mCamera = new Camera();
8518 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008519 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008520 info.mCamera.save();
8521 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8522 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8523 info.mCamera.getMatrix(info.matrix3D);
8524 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8525 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8526 info.mPivotY + info.mTranslationY);
8527 info.mMatrix.postConcat(info.matrix3D);
8528 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008529 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008530 info.mMatrixDirty = false;
8531 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8532 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008533 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008534 }
8535
8536 /**
8537 * Utility method to retrieve the inverse of the current mMatrix property.
8538 * We cache the matrix to avoid recalculating it when transform properties
8539 * have not changed.
8540 *
8541 * @return The inverse of the current matrix of this view.
8542 */
Jeff Brown86671742010-09-30 20:00:15 -07008543 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008544 final TransformationInfo info = mTransformationInfo;
8545 if (info != null) {
8546 updateMatrix();
8547 if (info.mInverseMatrixDirty) {
8548 if (info.mInverseMatrix == null) {
8549 info.mInverseMatrix = new Matrix();
8550 }
8551 info.mMatrix.invert(info.mInverseMatrix);
8552 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008553 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008554 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008555 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008556 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008557 }
8558
8559 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008560 * Gets the distance along the Z axis from the camera to this view.
8561 *
8562 * @see #setCameraDistance(float)
8563 *
8564 * @return The distance along the Z axis.
8565 */
8566 public float getCameraDistance() {
8567 ensureTransformationInfo();
8568 final float dpi = mResources.getDisplayMetrics().densityDpi;
8569 final TransformationInfo info = mTransformationInfo;
8570 if (info.mCamera == null) {
8571 info.mCamera = new Camera();
8572 info.matrix3D = new Matrix();
8573 }
8574 return -(info.mCamera.getLocationZ() * dpi);
8575 }
8576
8577 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008578 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8579 * views are drawn) from the camera to this view. The camera's distance
8580 * affects 3D transformations, for instance rotations around the X and Y
8581 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008582 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008583 * use a camera distance that's greater than the height (X axis rotation) or
8584 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008585 *
Romain Guya5364ee2011-02-24 14:46:04 -08008586 * <p>The distance of the camera from the view plane can have an affect on the
8587 * perspective distortion of the view when it is rotated around the x or y axis.
8588 * For example, a large distance will result in a large viewing angle, and there
8589 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008590 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008591 * also result in some drawing artifacts if the rotated view ends up partially
8592 * behind the camera (which is why the recommendation is to use a distance at
8593 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008594 *
Romain Guya5364ee2011-02-24 14:46:04 -08008595 * <p>The distance is expressed in "depth pixels." The default distance depends
8596 * on the screen density. For instance, on a medium density display, the
8597 * default distance is 1280. On a high density display, the default distance
8598 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008599 *
Romain Guya5364ee2011-02-24 14:46:04 -08008600 * <p>If you want to specify a distance that leads to visually consistent
8601 * results across various densities, use the following formula:</p>
8602 * <pre>
8603 * float scale = context.getResources().getDisplayMetrics().density;
8604 * view.setCameraDistance(distance * scale);
8605 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07008606 *
Romain Guya5364ee2011-02-24 14:46:04 -08008607 * <p>The density scale factor of a high density display is 1.5,
8608 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008609 *
Romain Guya5364ee2011-02-24 14:46:04 -08008610 * @param distance The distance in "depth pixels", if negative the opposite
8611 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07008612 *
8613 * @see #setRotationX(float)
8614 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08008615 */
8616 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008617 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08008618
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008619 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08008620 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008621 final TransformationInfo info = mTransformationInfo;
8622 if (info.mCamera == null) {
8623 info.mCamera = new Camera();
8624 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08008625 }
8626
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008627 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
8628 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08008629
Chet Haase9d1992d2012-03-13 11:03:25 -07008630 invalidateViewProperty(false, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008631 if (mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07008632 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08008633 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008634 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8635 // View was rejected last time it was drawn by its parent; this may have changed
8636 invalidateParentIfNeeded();
8637 }
Romain Guya5364ee2011-02-24 14:46:04 -08008638 }
8639
8640 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008641 * The degrees that the view is rotated around the pivot point.
8642 *
Philip Milne6c8ea062012-04-03 17:38:43 -07008643 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07008644 * @see #getPivotX()
8645 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008646 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008647 * @return The degrees of rotation.
8648 */
Chet Haasea5531132012-02-02 13:41:44 -08008649 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008650 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008651 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008652 }
8653
8654 /**
Chet Haase897247b2010-09-09 14:54:47 -07008655 * Sets the degrees that the view is rotated around the pivot point. Increasing values
8656 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07008657 *
8658 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008659 *
8660 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07008661 * @see #getPivotX()
8662 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008663 * @see #setRotationX(float)
8664 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08008665 *
8666 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07008667 */
8668 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008669 ensureTransformationInfo();
8670 final TransformationInfo info = mTransformationInfo;
8671 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008672 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008673 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008674 info.mRotation = rotation;
8675 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008676 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008677 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008678 mDisplayList.setRotation(rotation);
8679 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008680 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8681 // View was rejected last time it was drawn by its parent; this may have changed
8682 invalidateParentIfNeeded();
8683 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008684 }
8685 }
8686
8687 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008688 * The degrees that the view is rotated around the vertical axis through the pivot point.
8689 *
8690 * @see #getPivotX()
8691 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008692 * @see #setRotationY(float)
8693 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008694 * @return The degrees of Y rotation.
8695 */
Chet Haasea5531132012-02-02 13:41:44 -08008696 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008697 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008698 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008699 }
8700
8701 /**
Chet Haase897247b2010-09-09 14:54:47 -07008702 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
8703 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
8704 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008705 *
Romain Guya5364ee2011-02-24 14:46:04 -08008706 * When rotating large views, it is recommended to adjust the camera distance
8707 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008708 *
8709 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008710 *
8711 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07008712 * @see #getPivotX()
8713 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008714 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008715 * @see #setRotationX(float)
8716 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008717 *
8718 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07008719 */
8720 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008721 ensureTransformationInfo();
8722 final TransformationInfo info = mTransformationInfo;
8723 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008724 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008725 info.mRotationY = rotationY;
8726 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008727 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008728 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008729 mDisplayList.setRotationY(rotationY);
8730 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008731 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8732 // View was rejected last time it was drawn by its parent; this may have changed
8733 invalidateParentIfNeeded();
8734 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008735 }
8736 }
8737
8738 /**
8739 * The degrees that the view is rotated around the horizontal axis through the pivot point.
8740 *
8741 * @see #getPivotX()
8742 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008743 * @see #setRotationX(float)
8744 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008745 * @return The degrees of X rotation.
8746 */
Chet Haasea5531132012-02-02 13:41:44 -08008747 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008748 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008749 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008750 }
8751
8752 /**
Chet Haase897247b2010-09-09 14:54:47 -07008753 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
8754 * Increasing values result in clockwise rotation from the viewpoint of looking down the
8755 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008756 *
Romain Guya5364ee2011-02-24 14:46:04 -08008757 * When rotating large views, it is recommended to adjust the camera distance
8758 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008759 *
8760 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008761 *
8762 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07008763 * @see #getPivotX()
8764 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008765 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008766 * @see #setRotationY(float)
8767 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008768 *
8769 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07008770 */
8771 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008772 ensureTransformationInfo();
8773 final TransformationInfo info = mTransformationInfo;
8774 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008775 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008776 info.mRotationX = rotationX;
8777 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008778 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008779 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008780 mDisplayList.setRotationX(rotationX);
8781 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008782 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8783 // View was rejected last time it was drawn by its parent; this may have changed
8784 invalidateParentIfNeeded();
8785 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008786 }
8787 }
8788
8789 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008790 * 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, the default, means that no scaling is applied.
8792 *
Joe Onorato93162322010-09-16 15:42:01 -04008793 * <p>By default, this is 1.0f.
8794 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008795 * @see #getPivotX()
8796 * @see #getPivotY()
8797 * @return The scaling factor.
8798 */
Chet Haasea5531132012-02-02 13:41:44 -08008799 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008800 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008801 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008802 }
8803
8804 /**
8805 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
8806 * the view's unscaled width. A value of 1 means that no scaling is applied.
8807 *
8808 * @param scaleX The scaling factor.
8809 * @see #getPivotX()
8810 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008811 *
8812 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07008813 */
8814 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008815 ensureTransformationInfo();
8816 final TransformationInfo info = mTransformationInfo;
8817 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008818 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008819 info.mScaleX = scaleX;
8820 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008821 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008822 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008823 mDisplayList.setScaleX(scaleX);
8824 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008825 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8826 // View was rejected last time it was drawn by its parent; this may have changed
8827 invalidateParentIfNeeded();
8828 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008829 }
8830 }
8831
8832 /**
8833 * The amount that the view is scaled in y around the pivot point, as a proportion of
8834 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
8835 *
Joe Onorato93162322010-09-16 15:42:01 -04008836 * <p>By default, this is 1.0f.
8837 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008838 * @see #getPivotX()
8839 * @see #getPivotY()
8840 * @return The scaling factor.
8841 */
Chet Haasea5531132012-02-02 13:41:44 -08008842 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008843 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008844 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008845 }
8846
8847 /**
8848 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
8849 * the view's unscaled width. A value of 1 means that no scaling is applied.
8850 *
8851 * @param scaleY The scaling factor.
8852 * @see #getPivotX()
8853 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008854 *
8855 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07008856 */
8857 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008858 ensureTransformationInfo();
8859 final TransformationInfo info = mTransformationInfo;
8860 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008861 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008862 info.mScaleY = scaleY;
8863 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008864 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008865 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008866 mDisplayList.setScaleY(scaleY);
8867 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008868 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8869 // View was rejected last time it was drawn by its parent; this may have changed
8870 invalidateParentIfNeeded();
8871 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008872 }
8873 }
8874
8875 /**
8876 * The x location of the point around which the view is {@link #setRotation(float) rotated}
8877 * and {@link #setScaleX(float) scaled}.
8878 *
8879 * @see #getRotation()
8880 * @see #getScaleX()
8881 * @see #getScaleY()
8882 * @see #getPivotY()
8883 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008884 *
8885 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008886 */
Chet Haasea5531132012-02-02 13:41:44 -08008887 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008888 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008889 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008890 }
8891
8892 /**
8893 * Sets the x location of the point around which the view is
8894 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07008895 * By default, the pivot point is centered on the object.
8896 * Setting this property disables this behavior and causes the view to use only the
8897 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008898 *
8899 * @param pivotX The x location of the pivot point.
8900 * @see #getRotation()
8901 * @see #getScaleX()
8902 * @see #getScaleY()
8903 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008904 *
8905 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008906 */
8907 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008908 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07008909 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008910 final TransformationInfo info = mTransformationInfo;
8911 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008912 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008913 info.mPivotX = pivotX;
8914 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008915 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008916 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008917 mDisplayList.setPivotX(pivotX);
8918 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008919 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8920 // View was rejected last time it was drawn by its parent; this may have changed
8921 invalidateParentIfNeeded();
8922 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008923 }
8924 }
8925
8926 /**
8927 * The y location of the point around which the view is {@link #setRotation(float) rotated}
8928 * and {@link #setScaleY(float) scaled}.
8929 *
8930 * @see #getRotation()
8931 * @see #getScaleX()
8932 * @see #getScaleY()
8933 * @see #getPivotY()
8934 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008935 *
8936 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07008937 */
Chet Haasea5531132012-02-02 13:41:44 -08008938 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008939 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008940 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008941 }
8942
8943 /**
8944 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07008945 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
8946 * Setting this property disables this behavior and causes the view to use only the
8947 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008948 *
8949 * @param pivotY The y location of the pivot point.
8950 * @see #getRotation()
8951 * @see #getScaleX()
8952 * @see #getScaleY()
8953 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008954 *
8955 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07008956 */
8957 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008958 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07008959 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008960 final TransformationInfo info = mTransformationInfo;
8961 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008962 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008963 info.mPivotY = pivotY;
8964 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008965 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008966 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008967 mDisplayList.setPivotY(pivotY);
8968 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008969 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8970 // View was rejected last time it was drawn by its parent; this may have changed
8971 invalidateParentIfNeeded();
8972 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008973 }
8974 }
8975
8976 /**
8977 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
8978 * completely transparent and 1 means the view is completely opaque.
8979 *
Joe Onorato93162322010-09-16 15:42:01 -04008980 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07008981 * @return The opacity of the view.
8982 */
Chet Haasea5531132012-02-02 13:41:44 -08008983 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008984 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008985 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008986 }
8987
8988 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07008989 * Returns whether this View has content which overlaps. This function, intended to be
8990 * overridden by specific View types, is an optimization when alpha is set on a view. If
8991 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
8992 * and then composited it into place, which can be expensive. If the view has no overlapping
8993 * rendering, the view can draw each primitive with the appropriate alpha value directly.
8994 * An example of overlapping rendering is a TextView with a background image, such as a
8995 * Button. An example of non-overlapping rendering is a TextView with no background, or
8996 * an ImageView with only the foreground image. The default implementation returns true;
8997 * subclasses should override if they have cases which can be optimized.
8998 *
8999 * @return true if the content in this view might overlap, false otherwise.
9000 */
9001 public boolean hasOverlappingRendering() {
9002 return true;
9003 }
9004
9005 /**
Romain Guy171c5922011-01-06 10:04:23 -08009006 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
9007 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009008 *
Romain Guy171c5922011-01-06 10:04:23 -08009009 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
9010 * responsible for applying the opacity itself. Otherwise, calling this method is
9011 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08009012 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07009013 *
Chet Haasea5531132012-02-02 13:41:44 -08009014 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
9015 * performance implications. It is generally best to use the alpha property sparingly and
9016 * transiently, as in the case of fading animations.</p>
9017 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009018 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08009019 *
Joe Malin32736f02011-01-19 16:14:20 -08009020 * @see #setLayerType(int, android.graphics.Paint)
9021 *
Chet Haase73066682010-11-29 15:55:32 -08009022 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07009023 */
9024 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009025 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009026 if (mTransformationInfo.mAlpha != alpha) {
9027 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009028 if (onSetAlpha((int) (alpha * 255))) {
9029 mPrivateFlags |= ALPHA_SET;
9030 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07009031 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009032 invalidate(true);
9033 } else {
9034 mPrivateFlags &= ~ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07009035 invalidateViewProperty(true, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07009036 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009037 mDisplayList.setAlpha(alpha);
9038 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009039 }
Chet Haaseed032702010-10-01 14:05:54 -07009040 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009041 }
9042
9043 /**
Chet Haasea00f3862011-02-22 06:34:40 -08009044 * Faster version of setAlpha() which performs the same steps except there are
9045 * no calls to invalidate(). The caller of this function should perform proper invalidation
9046 * on the parent and this object. The return value indicates whether the subclass handles
9047 * alpha (the return value for onSetAlpha()).
9048 *
9049 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009050 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
9051 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08009052 */
9053 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009054 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009055 if (mTransformationInfo.mAlpha != alpha) {
9056 mTransformationInfo.mAlpha = alpha;
9057 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
9058 if (subclassHandlesAlpha) {
9059 mPrivateFlags |= ALPHA_SET;
9060 return true;
9061 } else {
9062 mPrivateFlags &= ~ALPHA_SET;
Chet Haase1271e2c2012-04-20 09:54:27 -07009063 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009064 mDisplayList.setAlpha(alpha);
9065 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009066 }
Chet Haasea00f3862011-02-22 06:34:40 -08009067 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009068 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08009069 }
9070
9071 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009072 * Top position of this view relative to its parent.
9073 *
9074 * @return The top of this view, in pixels.
9075 */
9076 @ViewDebug.CapturedViewProperty
9077 public final int getTop() {
9078 return mTop;
9079 }
9080
9081 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009082 * Sets the top position of this view relative to its parent. This method is meant to be called
9083 * by the layout system and should not generally be called otherwise, because the property
9084 * may be changed at any time by the layout.
9085 *
9086 * @param top The top of this view, in pixels.
9087 */
9088 public final void setTop(int top) {
9089 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07009090 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009091 final boolean matrixIsIdentity = mTransformationInfo == null
9092 || mTransformationInfo.mMatrixIsIdentity;
9093 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009094 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009095 int minTop;
9096 int yLoc;
9097 if (top < mTop) {
9098 minTop = top;
9099 yLoc = top - mTop;
9100 } else {
9101 minTop = mTop;
9102 yLoc = 0;
9103 }
Chet Haasee9140a72011-02-16 16:23:29 -08009104 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009105 }
9106 } else {
9107 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009108 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009109 }
9110
Chet Haaseed032702010-10-01 14:05:54 -07009111 int width = mRight - mLeft;
9112 int oldHeight = mBottom - mTop;
9113
Chet Haase21cd1382010-09-01 17:42:29 -07009114 mTop = top;
Chet Haase1271e2c2012-04-20 09:54:27 -07009115 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009116 mDisplayList.setTop(mTop);
9117 }
Chet Haase21cd1382010-09-01 17:42:29 -07009118
Chet Haaseed032702010-10-01 14:05:54 -07009119 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9120
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009121 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009122 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9123 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009124 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009125 }
Chet Haase21cd1382010-09-01 17:42:29 -07009126 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009127 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009128 }
Chet Haase55dbb652010-12-21 20:15:08 -08009129 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009130 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009131 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9132 // View was rejected last time it was drawn by its parent; this may have changed
9133 invalidateParentIfNeeded();
9134 }
Chet Haase21cd1382010-09-01 17:42:29 -07009135 }
9136 }
9137
9138 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009139 * Bottom position of this view relative to its parent.
9140 *
9141 * @return The bottom of this view, in pixels.
9142 */
9143 @ViewDebug.CapturedViewProperty
9144 public final int getBottom() {
9145 return mBottom;
9146 }
9147
9148 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08009149 * True if this view has changed since the last time being drawn.
9150 *
9151 * @return The dirty state of this view.
9152 */
9153 public boolean isDirty() {
9154 return (mPrivateFlags & DIRTY_MASK) != 0;
9155 }
9156
9157 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009158 * Sets the bottom position of this view relative to its parent. This method is meant to be
9159 * called by the layout system and should not generally be called otherwise, because the
9160 * property may be changed at any time by the layout.
9161 *
9162 * @param bottom The bottom of this view, in pixels.
9163 */
9164 public final void setBottom(int bottom) {
9165 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07009166 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009167 final boolean matrixIsIdentity = mTransformationInfo == null
9168 || mTransformationInfo.mMatrixIsIdentity;
9169 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009170 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009171 int maxBottom;
9172 if (bottom < mBottom) {
9173 maxBottom = mBottom;
9174 } else {
9175 maxBottom = bottom;
9176 }
Chet Haasee9140a72011-02-16 16:23:29 -08009177 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009178 }
9179 } else {
9180 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009181 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009182 }
9183
Chet Haaseed032702010-10-01 14:05:54 -07009184 int width = mRight - mLeft;
9185 int oldHeight = mBottom - mTop;
9186
Chet Haase21cd1382010-09-01 17:42:29 -07009187 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -07009188 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009189 mDisplayList.setBottom(mBottom);
9190 }
Chet Haase21cd1382010-09-01 17:42:29 -07009191
Chet Haaseed032702010-10-01 14:05:54 -07009192 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9193
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009194 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009195 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9196 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009197 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009198 }
Chet Haase21cd1382010-09-01 17:42:29 -07009199 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009200 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009201 }
Chet Haase55dbb652010-12-21 20:15:08 -08009202 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009203 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009204 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9205 // View was rejected last time it was drawn by its parent; this may have changed
9206 invalidateParentIfNeeded();
9207 }
Chet Haase21cd1382010-09-01 17:42:29 -07009208 }
9209 }
9210
9211 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009212 * Left position of this view relative to its parent.
9213 *
9214 * @return The left edge of this view, in pixels.
9215 */
9216 @ViewDebug.CapturedViewProperty
9217 public final int getLeft() {
9218 return mLeft;
9219 }
9220
9221 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009222 * Sets the left position of this view relative to its parent. This method is meant to be called
9223 * by the layout system and should not generally be called otherwise, because the property
9224 * may be changed at any time by the layout.
9225 *
9226 * @param left The bottom of this view, in pixels.
9227 */
9228 public final void setLeft(int left) {
9229 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07009230 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009231 final boolean matrixIsIdentity = mTransformationInfo == null
9232 || mTransformationInfo.mMatrixIsIdentity;
9233 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009234 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009235 int minLeft;
9236 int xLoc;
9237 if (left < mLeft) {
9238 minLeft = left;
9239 xLoc = left - mLeft;
9240 } else {
9241 minLeft = mLeft;
9242 xLoc = 0;
9243 }
Chet Haasee9140a72011-02-16 16:23:29 -08009244 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009245 }
9246 } else {
9247 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009248 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009249 }
9250
Chet Haaseed032702010-10-01 14:05:54 -07009251 int oldWidth = mRight - mLeft;
9252 int height = mBottom - mTop;
9253
Chet Haase21cd1382010-09-01 17:42:29 -07009254 mLeft = left;
Chet Haase1271e2c2012-04-20 09:54:27 -07009255 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009256 mDisplayList.setLeft(left);
9257 }
Chet Haase21cd1382010-09-01 17:42:29 -07009258
Chet Haaseed032702010-10-01 14:05:54 -07009259 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9260
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009261 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009262 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9263 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009264 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009265 }
Chet Haase21cd1382010-09-01 17:42:29 -07009266 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009267 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009268 }
Chet Haase55dbb652010-12-21 20:15:08 -08009269 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009270 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009271 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9272 // View was rejected last time it was drawn by its parent; this may have changed
9273 invalidateParentIfNeeded();
9274 }
Chet Haase21cd1382010-09-01 17:42:29 -07009275 }
9276 }
9277
9278 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009279 * Right position of this view relative to its parent.
9280 *
9281 * @return The right edge of this view, in pixels.
9282 */
9283 @ViewDebug.CapturedViewProperty
9284 public final int getRight() {
9285 return mRight;
9286 }
9287
9288 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009289 * Sets the right position of this view relative to its parent. This method is meant to be called
9290 * by the layout system and should not generally be called otherwise, because the property
9291 * may be changed at any time by the layout.
9292 *
9293 * @param right The bottom of this view, in pixels.
9294 */
9295 public final void setRight(int right) {
9296 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07009297 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009298 final boolean matrixIsIdentity = mTransformationInfo == null
9299 || mTransformationInfo.mMatrixIsIdentity;
9300 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009301 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009302 int maxRight;
9303 if (right < mRight) {
9304 maxRight = mRight;
9305 } else {
9306 maxRight = right;
9307 }
Chet Haasee9140a72011-02-16 16:23:29 -08009308 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009309 }
9310 } else {
9311 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009312 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009313 }
9314
Chet Haaseed032702010-10-01 14:05:54 -07009315 int oldWidth = mRight - mLeft;
9316 int height = mBottom - mTop;
9317
Chet Haase21cd1382010-09-01 17:42:29 -07009318 mRight = right;
Chet Haase1271e2c2012-04-20 09:54:27 -07009319 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009320 mDisplayList.setRight(mRight);
9321 }
Chet Haase21cd1382010-09-01 17:42:29 -07009322
Chet Haaseed032702010-10-01 14:05:54 -07009323 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9324
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009325 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009326 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9327 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009328 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009329 }
Chet Haase21cd1382010-09-01 17:42:29 -07009330 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009331 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009332 }
Chet Haase55dbb652010-12-21 20:15:08 -08009333 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009334 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009335 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9336 // View was rejected last time it was drawn by its parent; this may have changed
9337 invalidateParentIfNeeded();
9338 }
Chet Haase21cd1382010-09-01 17:42:29 -07009339 }
9340 }
9341
9342 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009343 * The visual x position of this view, in pixels. This is equivalent to the
9344 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08009345 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07009346 *
Chet Haasedf030d22010-07-30 17:22:38 -07009347 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009348 */
Chet Haasea5531132012-02-02 13:41:44 -08009349 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009350 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009351 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009352 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009353
Chet Haasedf030d22010-07-30 17:22:38 -07009354 /**
9355 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
9356 * {@link #setTranslationX(float) translationX} property to be the difference between
9357 * the x value passed in and the current {@link #getLeft() left} property.
9358 *
9359 * @param x The visual x position of this view, in pixels.
9360 */
9361 public void setX(float x) {
9362 setTranslationX(x - mLeft);
9363 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009364
Chet Haasedf030d22010-07-30 17:22:38 -07009365 /**
9366 * The visual y position of this view, in pixels. This is equivalent to the
9367 * {@link #setTranslationY(float) translationY} property plus the current
9368 * {@link #getTop() top} property.
9369 *
9370 * @return The visual y position of this view, in pixels.
9371 */
Chet Haasea5531132012-02-02 13:41:44 -08009372 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009373 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009374 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009375 }
9376
9377 /**
9378 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
9379 * {@link #setTranslationY(float) translationY} property to be the difference between
9380 * the y value passed in and the current {@link #getTop() top} property.
9381 *
9382 * @param y The visual y position of this view, in pixels.
9383 */
9384 public void setY(float y) {
9385 setTranslationY(y - mTop);
9386 }
9387
9388
9389 /**
9390 * The horizontal location of this view relative to its {@link #getLeft() left} position.
9391 * This position is post-layout, in addition to wherever the object's
9392 * layout placed it.
9393 *
9394 * @return The horizontal position of this view relative to its left position, in pixels.
9395 */
Chet Haasea5531132012-02-02 13:41:44 -08009396 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009397 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009398 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07009399 }
9400
9401 /**
9402 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
9403 * This effectively positions the object post-layout, in addition to wherever the object's
9404 * layout placed it.
9405 *
9406 * @param translationX The horizontal position of this view relative to its left position,
9407 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009408 *
9409 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07009410 */
9411 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009412 ensureTransformationInfo();
9413 final TransformationInfo info = mTransformationInfo;
9414 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07009415 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009416 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009417 info.mTranslationX = translationX;
9418 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009419 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009420 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009421 mDisplayList.setTranslationX(translationX);
9422 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009423 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9424 // View was rejected last time it was drawn by its parent; this may have changed
9425 invalidateParentIfNeeded();
9426 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009427 }
9428 }
9429
9430 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009431 * The horizontal location of this view relative to its {@link #getTop() top} position.
9432 * This position is post-layout, in addition to wherever the object's
9433 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009434 *
Chet Haasedf030d22010-07-30 17:22:38 -07009435 * @return The vertical position of this view relative to its top position,
9436 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009437 */
Chet Haasea5531132012-02-02 13:41:44 -08009438 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009439 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009440 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009441 }
9442
9443 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009444 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9445 * This effectively positions the object post-layout, in addition to wherever the object's
9446 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009447 *
Chet Haasedf030d22010-07-30 17:22:38 -07009448 * @param translationY The vertical position of this view relative to its top position,
9449 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009450 *
9451 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009452 */
Chet Haasedf030d22010-07-30 17:22:38 -07009453 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009454 ensureTransformationInfo();
9455 final TransformationInfo info = mTransformationInfo;
9456 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009457 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009458 info.mTranslationY = translationY;
9459 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009460 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009461 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009462 mDisplayList.setTranslationY(translationY);
9463 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009464 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9465 // View was rejected last time it was drawn by its parent; this may have changed
9466 invalidateParentIfNeeded();
9467 }
Chet Haasedf030d22010-07-30 17:22:38 -07009468 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009469 }
9470
9471 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009472 * Hit rectangle in parent's coordinates
9473 *
9474 * @param outRect The hit rectangle of the view.
9475 */
9476 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009477 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009478 final TransformationInfo info = mTransformationInfo;
9479 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009480 outRect.set(mLeft, mTop, mRight, mBottom);
9481 } else {
9482 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009483 tmpRect.set(-info.mPivotX, -info.mPivotY,
9484 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9485 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009486 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9487 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009488 }
9489 }
9490
9491 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009492 * Determines whether the given point, in local coordinates is inside the view.
9493 */
9494 /*package*/ final boolean pointInView(float localX, float localY) {
9495 return localX >= 0 && localX < (mRight - mLeft)
9496 && localY >= 0 && localY < (mBottom - mTop);
9497 }
9498
9499 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009500 * Utility method to determine whether the given point, in local coordinates,
9501 * is inside the view, where the area of the view is expanded by the slop factor.
9502 * This method is called while processing touch-move events to determine if the event
9503 * is still within the view.
9504 */
9505 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009506 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009507 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009508 }
9509
9510 /**
9511 * When a view has focus and the user navigates away from it, the next view is searched for
9512 * starting from the rectangle filled in by this method.
9513 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009514 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
9515 * of the view. However, if your view maintains some idea of internal selection,
9516 * such as a cursor, or a selected row or column, you should override this method and
9517 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009518 *
9519 * @param r The rectangle to fill in, in this view's coordinates.
9520 */
9521 public void getFocusedRect(Rect r) {
9522 getDrawingRect(r);
9523 }
9524
9525 /**
9526 * If some part of this view is not clipped by any of its parents, then
9527 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009528 * coordinates (without taking possible View rotations into account), offset
9529 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9530 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009531 *
9532 * @param r If true is returned, r holds the global coordinates of the
9533 * visible portion of this view.
9534 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9535 * between this view and its root. globalOffet may be null.
9536 * @return true if r is non-empty (i.e. part of the view is visible at the
9537 * root level.
9538 */
9539 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9540 int width = mRight - mLeft;
9541 int height = mBottom - mTop;
9542 if (width > 0 && height > 0) {
9543 r.set(0, 0, width, height);
9544 if (globalOffset != null) {
9545 globalOffset.set(-mScrollX, -mScrollY);
9546 }
9547 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9548 }
9549 return false;
9550 }
9551
9552 public final boolean getGlobalVisibleRect(Rect r) {
9553 return getGlobalVisibleRect(r, null);
9554 }
9555
9556 public final boolean getLocalVisibleRect(Rect r) {
Romain Guyab4c4f4f2012-05-06 13:11:24 -07009557 final Point offset = mAttachInfo != null ? mAttachInfo.mPoint : new Point();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009558 if (getGlobalVisibleRect(r, offset)) {
9559 r.offset(-offset.x, -offset.y); // make r local
9560 return true;
9561 }
9562 return false;
9563 }
9564
9565 /**
9566 * Offset this view's vertical location by the specified number of pixels.
9567 *
9568 * @param offset the number of pixels to offset the view by
9569 */
9570 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009571 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009572 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009573 final boolean matrixIsIdentity = mTransformationInfo == null
9574 || mTransformationInfo.mMatrixIsIdentity;
9575 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009576 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009577 invalidateViewProperty(false, false);
9578 } else {
9579 final ViewParent p = mParent;
9580 if (p != null && mAttachInfo != null) {
9581 final Rect r = mAttachInfo.mTmpInvalRect;
9582 int minTop;
9583 int maxBottom;
9584 int yLoc;
9585 if (offset < 0) {
9586 minTop = mTop + offset;
9587 maxBottom = mBottom;
9588 yLoc = offset;
9589 } else {
9590 minTop = mTop;
9591 maxBottom = mBottom + offset;
9592 yLoc = 0;
9593 }
9594 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
9595 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009596 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009597 }
9598 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009599 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009600 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009601
Chet Haasec3aa3612010-06-17 08:50:37 -07009602 mTop += offset;
9603 mBottom += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009604 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009605 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009606 invalidateViewProperty(false, false);
9607 } else {
9608 if (!matrixIsIdentity) {
9609 invalidateViewProperty(false, true);
9610 }
9611 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009612 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009613 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009614 }
9615
9616 /**
9617 * Offset this view's horizontal location by the specified amount of pixels.
9618 *
9619 * @param offset the numer of pixels to offset the view by
9620 */
9621 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009622 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009623 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009624 final boolean matrixIsIdentity = mTransformationInfo == null
9625 || mTransformationInfo.mMatrixIsIdentity;
9626 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009627 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009628 invalidateViewProperty(false, false);
9629 } else {
9630 final ViewParent p = mParent;
9631 if (p != null && mAttachInfo != null) {
9632 final Rect r = mAttachInfo.mTmpInvalRect;
9633 int minLeft;
9634 int maxRight;
9635 if (offset < 0) {
9636 minLeft = mLeft + offset;
9637 maxRight = mRight;
9638 } else {
9639 minLeft = mLeft;
9640 maxRight = mRight + offset;
9641 }
9642 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
9643 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009644 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009645 }
9646 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009647 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009648 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009649
Chet Haasec3aa3612010-06-17 08:50:37 -07009650 mLeft += offset;
9651 mRight += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009652 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009653 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009654 invalidateViewProperty(false, false);
9655 } else {
9656 if (!matrixIsIdentity) {
9657 invalidateViewProperty(false, true);
9658 }
9659 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009660 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009661 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009662 }
9663
9664 /**
9665 * Get the LayoutParams associated with this view. All views should have
9666 * layout parameters. These supply parameters to the <i>parent</i> of this
9667 * view specifying how it should be arranged. There are many subclasses of
9668 * ViewGroup.LayoutParams, and these correspond to the different subclasses
9669 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08009670 *
9671 * This method may return null if this View is not attached to a parent
9672 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
9673 * was not invoked successfully. When a View is attached to a parent
9674 * ViewGroup, this method must not return null.
9675 *
9676 * @return The LayoutParams associated with this view, or null if no
9677 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009678 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07009679 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009680 public ViewGroup.LayoutParams getLayoutParams() {
9681 return mLayoutParams;
9682 }
9683
9684 /**
9685 * Set the layout parameters associated with this view. These supply
9686 * parameters to the <i>parent</i> of this view specifying how it should be
9687 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
9688 * correspond to the different subclasses of ViewGroup that are responsible
9689 * for arranging their children.
9690 *
Romain Guy01c174b2011-02-22 11:51:06 -08009691 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009692 */
9693 public void setLayoutParams(ViewGroup.LayoutParams params) {
9694 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08009695 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009696 }
9697 mLayoutParams = params;
Philip Milned7dd8902012-01-26 16:55:30 -08009698 if (mParent instanceof ViewGroup) {
9699 ((ViewGroup) mParent).onSetLayoutParams(this, params);
9700 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009701 requestLayout();
9702 }
9703
9704 /**
9705 * Set the scrolled position of your view. This will cause a call to
9706 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9707 * invalidated.
9708 * @param x the x position to scroll to
9709 * @param y the y position to scroll to
9710 */
9711 public void scrollTo(int x, int y) {
9712 if (mScrollX != x || mScrollY != y) {
9713 int oldX = mScrollX;
9714 int oldY = mScrollY;
9715 mScrollX = x;
9716 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009717 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009718 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07009719 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009720 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -07009721 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009722 }
9723 }
9724
9725 /**
9726 * Move the scrolled position of your view. This will cause a call to
9727 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9728 * invalidated.
9729 * @param x the amount of pixels to scroll by horizontally
9730 * @param y the amount of pixels to scroll by vertically
9731 */
9732 public void scrollBy(int x, int y) {
9733 scrollTo(mScrollX + x, mScrollY + y);
9734 }
9735
9736 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009737 * <p>Trigger the scrollbars to draw. When invoked this method starts an
9738 * animation to fade the scrollbars out after a default delay. If a subclass
9739 * provides animated scrolling, the start delay should equal the duration
9740 * of the scrolling animation.</p>
9741 *
9742 * <p>The animation starts only if at least one of the scrollbars is
9743 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
9744 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9745 * this method returns true, and false otherwise. If the animation is
9746 * started, this method calls {@link #invalidate()}; in that case the
9747 * caller should not call {@link #invalidate()}.</p>
9748 *
9749 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07009750 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07009751 *
9752 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
9753 * and {@link #scrollTo(int, int)}.</p>
9754 *
9755 * @return true if the animation is played, false otherwise
9756 *
9757 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07009758 * @see #scrollBy(int, int)
9759 * @see #scrollTo(int, int)
9760 * @see #isHorizontalScrollBarEnabled()
9761 * @see #isVerticalScrollBarEnabled()
9762 * @see #setHorizontalScrollBarEnabled(boolean)
9763 * @see #setVerticalScrollBarEnabled(boolean)
9764 */
9765 protected boolean awakenScrollBars() {
9766 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07009767 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07009768 }
9769
9770 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07009771 * Trigger the scrollbars to draw.
9772 * This method differs from awakenScrollBars() only in its default duration.
9773 * initialAwakenScrollBars() will show the scroll bars for longer than
9774 * usual to give the user more of a chance to notice them.
9775 *
9776 * @return true if the animation is played, false otherwise.
9777 */
9778 private boolean initialAwakenScrollBars() {
9779 return mScrollCache != null &&
9780 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
9781 }
9782
9783 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009784 * <p>
9785 * Trigger the scrollbars to draw. When invoked this method starts an
9786 * animation to fade the scrollbars out after a fixed delay. If a subclass
9787 * provides animated scrolling, the start delay should equal the duration of
9788 * the scrolling animation.
9789 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009790 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009791 * <p>
9792 * The animation starts only if at least one of the scrollbars is enabled,
9793 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9794 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9795 * this method returns true, and false otherwise. If the animation is
9796 * started, this method calls {@link #invalidate()}; in that case the caller
9797 * should not call {@link #invalidate()}.
9798 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009799 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009800 * <p>
9801 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07009802 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07009803 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009804 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009805 * @param startDelay the delay, in milliseconds, after which the animation
9806 * should start; when the delay is 0, the animation starts
9807 * immediately
9808 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009809 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009810 * @see #scrollBy(int, int)
9811 * @see #scrollTo(int, int)
9812 * @see #isHorizontalScrollBarEnabled()
9813 * @see #isVerticalScrollBarEnabled()
9814 * @see #setHorizontalScrollBarEnabled(boolean)
9815 * @see #setVerticalScrollBarEnabled(boolean)
9816 */
9817 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07009818 return awakenScrollBars(startDelay, true);
9819 }
Joe Malin32736f02011-01-19 16:14:20 -08009820
Mike Cleron290947b2009-09-29 18:34:32 -07009821 /**
9822 * <p>
9823 * Trigger the scrollbars to draw. When invoked this method starts an
9824 * animation to fade the scrollbars out after a fixed delay. If a subclass
9825 * provides animated scrolling, the start delay should equal the duration of
9826 * the scrolling animation.
9827 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009828 *
Mike Cleron290947b2009-09-29 18:34:32 -07009829 * <p>
9830 * The animation starts only if at least one of the scrollbars is enabled,
9831 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9832 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9833 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08009834 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07009835 * is set to true; in that case the caller
9836 * should not call {@link #invalidate()}.
9837 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009838 *
Mike Cleron290947b2009-09-29 18:34:32 -07009839 * <p>
9840 * This method should be invoked everytime a subclass directly updates the
9841 * scroll parameters.
9842 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009843 *
Mike Cleron290947b2009-09-29 18:34:32 -07009844 * @param startDelay the delay, in milliseconds, after which the animation
9845 * should start; when the delay is 0, the animation starts
9846 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08009847 *
Mike Cleron290947b2009-09-29 18:34:32 -07009848 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08009849 *
Mike Cleron290947b2009-09-29 18:34:32 -07009850 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009851 *
Mike Cleron290947b2009-09-29 18:34:32 -07009852 * @see #scrollBy(int, int)
9853 * @see #scrollTo(int, int)
9854 * @see #isHorizontalScrollBarEnabled()
9855 * @see #isVerticalScrollBarEnabled()
9856 * @see #setHorizontalScrollBarEnabled(boolean)
9857 * @see #setVerticalScrollBarEnabled(boolean)
9858 */
9859 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07009860 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08009861
Mike Cleronf116bf82009-09-27 19:14:12 -07009862 if (scrollCache == null || !scrollCache.fadeScrollBars) {
9863 return false;
9864 }
9865
9866 if (scrollCache.scrollBar == null) {
9867 scrollCache.scrollBar = new ScrollBarDrawable();
9868 }
9869
9870 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
9871
Mike Cleron290947b2009-09-29 18:34:32 -07009872 if (invalidate) {
9873 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009874 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -07009875 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009876
9877 if (scrollCache.state == ScrollabilityCache.OFF) {
9878 // FIXME: this is copied from WindowManagerService.
9879 // We should get this value from the system when it
9880 // is possible to do so.
9881 final int KEY_REPEAT_FIRST_DELAY = 750;
9882 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
9883 }
9884
9885 // Tell mScrollCache when we should start fading. This may
9886 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07009887 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07009888 scrollCache.fadeStartTime = fadeStartTime;
9889 scrollCache.state = ScrollabilityCache.ON;
9890
9891 // Schedule our fader to run, unscheduling any old ones first
9892 if (mAttachInfo != null) {
9893 mAttachInfo.mHandler.removeCallbacks(scrollCache);
9894 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
9895 }
9896
9897 return true;
9898 }
9899
9900 return false;
9901 }
9902
9903 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07009904 * Do not invalidate views which are not visible and which are not running an animation. They
9905 * will not get drawn and they should not set dirty flags as if they will be drawn
9906 */
9907 private boolean skipInvalidate() {
9908 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
9909 (!(mParent instanceof ViewGroup) ||
9910 !((ViewGroup) mParent).isViewTransitioning(this));
9911 }
9912 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07009913 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009914 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
9915 * in the future. This must be called from a UI thread. To call from a non-UI
9916 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009917 *
9918 * WARNING: This method is destructive to dirty.
9919 * @param dirty the rectangle representing the bounds of the dirty region
9920 */
9921 public void invalidate(Rect dirty) {
Chet Haaseaceafe62011-08-26 15:44:33 -07009922 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009923 return;
9924 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009925 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08009926 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
9927 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009928 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08009929 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07009930 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009931 final ViewParent p = mParent;
9932 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08009933 //noinspection PointlessBooleanExpression,ConstantConditions
9934 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9935 if (p != null && ai != null && ai.mHardwareAccelerated) {
9936 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009937 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009938 p.invalidateChild(this, null);
9939 return;
9940 }
Romain Guyaf636eb2010-12-09 17:47:21 -08009941 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009942 if (p != null && ai != null) {
9943 final int scrollX = mScrollX;
9944 final int scrollY = mScrollY;
9945 final Rect r = ai.mTmpInvalRect;
9946 r.set(dirty.left - scrollX, dirty.top - scrollY,
9947 dirty.right - scrollX, dirty.bottom - scrollY);
9948 mParent.invalidateChild(this, r);
9949 }
9950 }
9951 }
9952
9953 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07009954 * 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 -08009955 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009956 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
9957 * will be called at some point in the future. This must be called from
9958 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009959 * @param l the left position of the dirty region
9960 * @param t the top position of the dirty region
9961 * @param r the right position of the dirty region
9962 * @param b the bottom position of the dirty region
9963 */
9964 public void invalidate(int l, int t, int r, int b) {
Chet Haaseaceafe62011-08-26 15:44:33 -07009965 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009966 return;
9967 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009968 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08009969 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
9970 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009971 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08009972 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07009973 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009974 final ViewParent p = mParent;
9975 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08009976 //noinspection PointlessBooleanExpression,ConstantConditions
9977 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9978 if (p != null && ai != null && ai.mHardwareAccelerated) {
9979 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009980 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009981 p.invalidateChild(this, null);
9982 return;
9983 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08009984 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009985 if (p != null && ai != null && l < r && t < b) {
9986 final int scrollX = mScrollX;
9987 final int scrollY = mScrollY;
9988 final Rect tmpr = ai.mTmpInvalRect;
9989 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
9990 p.invalidateChild(this, tmpr);
9991 }
9992 }
9993 }
9994
9995 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009996 * Invalidate the whole view. If the view is visible,
9997 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
9998 * the future. This must be called from a UI thread. To call from a non-UI thread,
9999 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010000 */
10001 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -070010002 invalidate(true);
10003 }
Joe Malin32736f02011-01-19 16:14:20 -080010004
Chet Haaseed032702010-10-01 14:05:54 -070010005 /**
10006 * This is where the invalidate() work actually happens. A full invalidate()
10007 * causes the drawing cache to be invalidated, but this function can be called with
10008 * invalidateCache set to false to skip that invalidation step for cases that do not
10009 * need it (for example, a component that remains at the same dimensions with the same
10010 * content).
10011 *
10012 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
10013 * well. This is usually true for a full invalidate, but may be set to false if the
10014 * View's contents or dimensions have not changed.
10015 */
Romain Guy849d0a32011-02-01 17:20:48 -080010016 void invalidate(boolean invalidateCache) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010017 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010018 return;
10019 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010020 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -080010021 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -080010022 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
10023 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -070010024 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -070010025 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -070010026 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -080010027 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -070010028 mPrivateFlags &= ~DRAWING_CACHE_VALID;
10029 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010030 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -070010031 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -080010032 //noinspection PointlessBooleanExpression,ConstantConditions
10033 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10034 if (p != null && ai != null && ai.mHardwareAccelerated) {
10035 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010036 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010037 p.invalidateChild(this, null);
10038 return;
10039 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010040 }
Michael Jurkaebefea42010-11-15 16:04:01 -080010041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010042 if (p != null && ai != null) {
10043 final Rect r = ai.mTmpInvalRect;
10044 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10045 // Don't call invalidate -- we don't want to internally scroll
10046 // our own bounds
10047 p.invalidateChild(this, r);
10048 }
10049 }
10050 }
10051
10052 /**
Chet Haase9d1992d2012-03-13 11:03:25 -070010053 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
10054 * set any flags or handle all of the cases handled by the default invalidation methods.
10055 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
10056 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
10057 * walk up the hierarchy, transforming the dirty rect as necessary.
10058 *
10059 * The method also handles normal invalidation logic if display list properties are not
10060 * being used in this view. The invalidateParent and forceRedraw flags are used by that
10061 * backup approach, to handle these cases used in the various property-setting methods.
10062 *
10063 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
10064 * are not being used in this view
10065 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
10066 * list properties are not being used in this view
10067 */
10068 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
Chet Haase1271e2c2012-04-20 09:54:27 -070010069 if (mDisplayList == null || (mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
Chet Haase9d1992d2012-03-13 11:03:25 -070010070 if (invalidateParent) {
10071 invalidateParentCaches();
10072 }
10073 if (forceRedraw) {
10074 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
10075 }
10076 invalidate(false);
10077 } else {
10078 final AttachInfo ai = mAttachInfo;
10079 final ViewParent p = mParent;
10080 if (p != null && ai != null) {
10081 final Rect r = ai.mTmpInvalRect;
10082 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10083 if (mParent instanceof ViewGroup) {
10084 ((ViewGroup) mParent).invalidateChildFast(this, r);
10085 } else {
10086 mParent.invalidateChild(this, r);
10087 }
10088 }
10089 }
10090 }
10091
10092 /**
10093 * Utility method to transform a given Rect by the current matrix of this view.
10094 */
10095 void transformRect(final Rect rect) {
10096 if (!getMatrix().isIdentity()) {
10097 RectF boundingRect = mAttachInfo.mTmpTransformRect;
10098 boundingRect.set(rect);
10099 getMatrix().mapRect(boundingRect);
10100 rect.set((int) (boundingRect.left - 0.5f),
10101 (int) (boundingRect.top - 0.5f),
10102 (int) (boundingRect.right + 0.5f),
10103 (int) (boundingRect.bottom + 0.5f));
10104 }
10105 }
10106
10107 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -080010108 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -080010109 * 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
Romain Guy0fd89bf2011-01-26 15:41:30 -080010111 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
10112 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -080010113 *
10114 * @hide
10115 */
Romain Guy0fd89bf2011-01-26 15:41:30 -080010116 protected void invalidateParentCaches() {
10117 if (mParent instanceof View) {
10118 ((View) mParent).mPrivateFlags |= INVALIDATED;
10119 }
10120 }
Joe Malin32736f02011-01-19 16:14:20 -080010121
Romain Guy0fd89bf2011-01-26 15:41:30 -080010122 /**
10123 * Used to indicate that the parent of this view should be invalidated. This functionality
10124 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10125 * which is necessary when various parent-managed properties of the view change, such as
10126 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
10127 * an invalidation event to the parent.
10128 *
10129 * @hide
10130 */
10131 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -080010132 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010133 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -080010134 }
10135 }
10136
10137 /**
Romain Guy24443ea2009-05-11 11:56:30 -070010138 * Indicates whether this View is opaque. An opaque View guarantees that it will
10139 * draw all the pixels overlapping its bounds using a fully opaque color.
10140 *
10141 * Subclasses of View should override this method whenever possible to indicate
10142 * whether an instance is opaque. Opaque Views are treated in a special way by
10143 * the View hierarchy, possibly allowing it to perform optimizations during
10144 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -070010145 *
Romain Guy24443ea2009-05-11 11:56:30 -070010146 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -070010147 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010148 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -070010149 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -070010150 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -070010151 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
10152 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -070010153 }
10154
Adam Powell20232d02010-12-08 21:08:53 -080010155 /**
10156 * @hide
10157 */
10158 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -070010159 // Opaque if:
10160 // - Has a background
10161 // - Background is opaque
10162 // - Doesn't have scrollbars or scrollbars are inside overlay
10163
Philip Milne6c8ea062012-04-03 17:38:43 -070010164 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Romain Guy8f1344f52009-05-15 16:03:59 -070010165 mPrivateFlags |= OPAQUE_BACKGROUND;
10166 } else {
10167 mPrivateFlags &= ~OPAQUE_BACKGROUND;
10168 }
10169
10170 final int flags = mViewFlags;
10171 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
10172 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
10173 mPrivateFlags |= OPAQUE_SCROLLBARS;
10174 } else {
10175 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
10176 }
10177 }
10178
10179 /**
10180 * @hide
10181 */
10182 protected boolean hasOpaqueScrollbars() {
10183 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -070010184 }
10185
10186 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010187 * @return A handler associated with the thread running the View. This
10188 * handler can be used to pump events in the UI events queue.
10189 */
10190 public Handler getHandler() {
10191 if (mAttachInfo != null) {
10192 return mAttachInfo.mHandler;
10193 }
10194 return null;
10195 }
10196
10197 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080010198 * Gets the view root associated with the View.
10199 * @return The view root, or null if none.
10200 * @hide
10201 */
10202 public ViewRootImpl getViewRootImpl() {
10203 if (mAttachInfo != null) {
10204 return mAttachInfo.mViewRootImpl;
10205 }
10206 return null;
10207 }
10208
10209 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010210 * <p>Causes the Runnable to be added to the message queue.
10211 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010212 *
Romain Guye63a4f32011-08-11 11:33:31 -070010213 * <p>This method can be invoked from outside of the UI thread
10214 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010215 *
10216 * @param action The Runnable that will be executed.
10217 *
10218 * @return Returns true if the Runnable was successfully placed in to the
10219 * message queue. Returns false on failure, usually because the
10220 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010221 *
10222 * @see #postDelayed
10223 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010224 */
10225 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010226 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010227 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010228 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010229 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010230 // Assume that post will succeed later
10231 ViewRootImpl.getRunQueue().post(action);
10232 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010233 }
10234
10235 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010236 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010237 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -070010238 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010239 *
Romain Guye63a4f32011-08-11 11:33:31 -070010240 * <p>This method can be invoked from outside of the UI thread
10241 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010242 *
10243 * @param action The Runnable that will be executed.
10244 * @param delayMillis The delay (in milliseconds) until the Runnable
10245 * will be executed.
10246 *
10247 * @return true if the Runnable was successfully placed in to the
10248 * message queue. Returns false on failure, usually because the
10249 * looper processing the message queue is exiting. Note that a
10250 * result of true does not mean the Runnable will be processed --
10251 * if the looper is quit before the delivery time of the message
10252 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010253 *
10254 * @see #post
10255 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010256 */
10257 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010258 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010259 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010260 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010261 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010262 // Assume that post will succeed later
10263 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10264 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010265 }
10266
10267 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010268 * <p>Causes the Runnable to execute on the next animation time step.
10269 * The runnable will be run on the user interface thread.</p>
10270 *
10271 * <p>This method can be invoked from outside of the UI thread
10272 * only when this View is attached to a window.</p>
10273 *
10274 * @param action The Runnable that will be executed.
10275 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010276 * @see #postOnAnimationDelayed
10277 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010278 */
10279 public void postOnAnimation(Runnable action) {
10280 final AttachInfo attachInfo = mAttachInfo;
10281 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010282 attachInfo.mViewRootImpl.mChoreographer.postCallback(
10283 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010284 } else {
10285 // Assume that post will succeed later
10286 ViewRootImpl.getRunQueue().post(action);
10287 }
10288 }
10289
10290 /**
10291 * <p>Causes the Runnable to execute on the next animation time step,
10292 * after the specified amount of time elapses.
10293 * The runnable will be run on the user interface thread.</p>
10294 *
10295 * <p>This method can be invoked from outside of the UI thread
10296 * only when this View is attached to a window.</p>
10297 *
10298 * @param action The Runnable that will be executed.
10299 * @param delayMillis The delay (in milliseconds) until the Runnable
10300 * will be executed.
10301 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010302 * @see #postOnAnimation
10303 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010304 */
10305 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
10306 final AttachInfo attachInfo = mAttachInfo;
10307 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010308 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
10309 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010310 } else {
10311 // Assume that post will succeed later
10312 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10313 }
10314 }
10315
10316 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010317 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010318 *
Romain Guye63a4f32011-08-11 11:33:31 -070010319 * <p>This method can be invoked from outside of the UI thread
10320 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010321 *
10322 * @param action The Runnable to remove from the message handling queue
10323 *
10324 * @return true if this view could ask the Handler to remove the Runnable,
10325 * false otherwise. When the returned value is true, the Runnable
10326 * may or may not have been actually removed from the message queue
10327 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010328 *
10329 * @see #post
10330 * @see #postDelayed
10331 * @see #postOnAnimation
10332 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010333 */
10334 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080010335 if (action != null) {
10336 final AttachInfo attachInfo = mAttachInfo;
10337 if (attachInfo != null) {
10338 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010339 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
10340 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -080010341 } else {
10342 // Assume that post will succeed later
10343 ViewRootImpl.getRunQueue().removeCallbacks(action);
10344 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010345 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010346 return true;
10347 }
10348
10349 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010350 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
10351 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010352 *
Romain Guye63a4f32011-08-11 11:33:31 -070010353 * <p>This method can be invoked from outside of the UI thread
10354 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010355 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010356 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010357 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010358 */
10359 public void postInvalidate() {
10360 postInvalidateDelayed(0);
10361 }
10362
10363 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010364 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10365 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010366 *
Romain Guye63a4f32011-08-11 11:33:31 -070010367 * <p>This method can be invoked from outside of the UI thread
10368 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010369 *
10370 * @param left The left coordinate of the rectangle to invalidate.
10371 * @param top The top coordinate of the rectangle to invalidate.
10372 * @param right The right coordinate of the rectangle to invalidate.
10373 * @param bottom The bottom coordinate of the rectangle to invalidate.
10374 *
10375 * @see #invalidate(int, int, int, int)
10376 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010377 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010378 */
10379 public void postInvalidate(int left, int top, int right, int bottom) {
10380 postInvalidateDelayed(0, left, top, right, bottom);
10381 }
10382
10383 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010384 * <p>Cause an invalidate to happen on a subsequent cycle through the event
10385 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010386 *
Romain Guye63a4f32011-08-11 11:33:31 -070010387 * <p>This method can be invoked from outside of the UI thread
10388 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010389 *
10390 * @param delayMilliseconds the duration in milliseconds to delay the
10391 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010392 *
10393 * @see #invalidate()
10394 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010395 */
10396 public void postInvalidateDelayed(long delayMilliseconds) {
10397 // We try only with the AttachInfo because there's no point in invalidating
10398 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010399 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010400 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010401 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010402 }
10403 }
10404
10405 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010406 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10407 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010408 *
Romain Guye63a4f32011-08-11 11:33:31 -070010409 * <p>This method can be invoked from outside of the UI thread
10410 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010411 *
10412 * @param delayMilliseconds the duration in milliseconds to delay the
10413 * invalidation by
10414 * @param left The left coordinate of the rectangle to invalidate.
10415 * @param top The top coordinate of the rectangle to invalidate.
10416 * @param right The right coordinate of the rectangle to invalidate.
10417 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010418 *
10419 * @see #invalidate(int, int, int, int)
10420 * @see #invalidate(Rect)
10421 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010422 */
10423 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
10424 int right, int bottom) {
10425
10426 // We try only with the AttachInfo because there's no point in invalidating
10427 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010428 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010429 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010430 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10431 info.target = this;
10432 info.left = left;
10433 info.top = top;
10434 info.right = right;
10435 info.bottom = bottom;
10436
Jeff Browna175a5b2012-02-15 19:18:31 -080010437 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010438 }
10439 }
10440
10441 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010442 * <p>Cause an invalidate to happen on the next animation time step, typically the
10443 * next display frame.</p>
10444 *
10445 * <p>This method can be invoked from outside of the UI thread
10446 * only when this View is attached to a window.</p>
10447 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010448 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010449 */
10450 public void postInvalidateOnAnimation() {
10451 // We try only with the AttachInfo because there's no point in invalidating
10452 // if we are not attached to our window
10453 final AttachInfo attachInfo = mAttachInfo;
10454 if (attachInfo != null) {
10455 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10456 }
10457 }
10458
10459 /**
10460 * <p>Cause an invalidate of the specified area to happen on the next animation
10461 * time step, typically the next display frame.</p>
10462 *
10463 * <p>This method can be invoked from outside of the UI thread
10464 * only when this View is attached to a window.</p>
10465 *
10466 * @param left The left coordinate of the rectangle to invalidate.
10467 * @param top The top coordinate of the rectangle to invalidate.
10468 * @param right The right coordinate of the rectangle to invalidate.
10469 * @param bottom The bottom coordinate of the rectangle to invalidate.
10470 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010471 * @see #invalidate(int, int, int, int)
10472 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010473 */
10474 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10475 // We try only with the AttachInfo because there's no point in invalidating
10476 // if we are not attached to our window
10477 final AttachInfo attachInfo = mAttachInfo;
10478 if (attachInfo != null) {
10479 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10480 info.target = this;
10481 info.left = left;
10482 info.top = top;
10483 info.right = right;
10484 info.bottom = bottom;
10485
10486 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10487 }
10488 }
10489
10490 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010491 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10492 * This event is sent at most once every
10493 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10494 */
10495 private void postSendViewScrolledAccessibilityEventCallback() {
10496 if (mSendViewScrolledAccessibilityEvent == null) {
10497 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10498 }
10499 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10500 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10501 postDelayed(mSendViewScrolledAccessibilityEvent,
10502 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10503 }
10504 }
10505
10506 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010507 * Called by a parent to request that a child update its values for mScrollX
10508 * and mScrollY if necessary. This will typically be done if the child is
10509 * animating a scroll using a {@link android.widget.Scroller Scroller}
10510 * object.
10511 */
10512 public void computeScroll() {
10513 }
10514
10515 /**
10516 * <p>Indicate whether the horizontal edges are faded when the view is
10517 * scrolled horizontally.</p>
10518 *
10519 * @return true if the horizontal edges should are faded on scroll, false
10520 * otherwise
10521 *
10522 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010523 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010524 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010525 */
10526 public boolean isHorizontalFadingEdgeEnabled() {
10527 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10528 }
10529
10530 /**
10531 * <p>Define whether the horizontal edges should be faded when this view
10532 * is scrolled horizontally.</p>
10533 *
10534 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10535 * be faded when the view is scrolled
10536 * horizontally
10537 *
10538 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010539 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010540 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010541 */
10542 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10543 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10544 if (horizontalFadingEdgeEnabled) {
10545 initScrollCache();
10546 }
10547
10548 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10549 }
10550 }
10551
10552 /**
10553 * <p>Indicate whether the vertical edges are faded when the view is
10554 * scrolled horizontally.</p>
10555 *
10556 * @return true if the vertical edges should are faded on scroll, false
10557 * otherwise
10558 *
10559 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010560 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010561 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010562 */
10563 public boolean isVerticalFadingEdgeEnabled() {
10564 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10565 }
10566
10567 /**
10568 * <p>Define whether the vertical edges should be faded when this view
10569 * is scrolled vertically.</p>
10570 *
10571 * @param verticalFadingEdgeEnabled true if the vertical edges should
10572 * be faded when the view is scrolled
10573 * vertically
10574 *
10575 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010576 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010577 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010578 */
10579 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10580 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10581 if (verticalFadingEdgeEnabled) {
10582 initScrollCache();
10583 }
10584
10585 mViewFlags ^= FADING_EDGE_VERTICAL;
10586 }
10587 }
10588
10589 /**
10590 * Returns the strength, or intensity, of the top faded edge. The strength is
10591 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10592 * returns 0.0 or 1.0 but no value in between.
10593 *
10594 * Subclasses should override this method to provide a smoother fade transition
10595 * when scrolling occurs.
10596 *
10597 * @return the intensity of the top fade as a float between 0.0f and 1.0f
10598 */
10599 protected float getTopFadingEdgeStrength() {
10600 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
10601 }
10602
10603 /**
10604 * Returns the strength, or intensity, of the bottom 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 bottom fade as a float between 0.0f and 1.0f
10612 */
10613 protected float getBottomFadingEdgeStrength() {
10614 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
10615 computeVerticalScrollRange() ? 1.0f : 0.0f;
10616 }
10617
10618 /**
10619 * Returns the strength, or intensity, of the left faded edge. The strength is
10620 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10621 * returns 0.0 or 1.0 but no value in between.
10622 *
10623 * Subclasses should override this method to provide a smoother fade transition
10624 * when scrolling occurs.
10625 *
10626 * @return the intensity of the left fade as a float between 0.0f and 1.0f
10627 */
10628 protected float getLeftFadingEdgeStrength() {
10629 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
10630 }
10631
10632 /**
10633 * Returns the strength, or intensity, of the right faded edge. The strength is
10634 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10635 * returns 0.0 or 1.0 but no value in between.
10636 *
10637 * Subclasses should override this method to provide a smoother fade transition
10638 * when scrolling occurs.
10639 *
10640 * @return the intensity of the right fade as a float between 0.0f and 1.0f
10641 */
10642 protected float getRightFadingEdgeStrength() {
10643 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
10644 computeHorizontalScrollRange() ? 1.0f : 0.0f;
10645 }
10646
10647 /**
10648 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
10649 * scrollbar is not drawn by default.</p>
10650 *
10651 * @return true if the horizontal scrollbar should be painted, false
10652 * otherwise
10653 *
10654 * @see #setHorizontalScrollBarEnabled(boolean)
10655 */
10656 public boolean isHorizontalScrollBarEnabled() {
10657 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10658 }
10659
10660 /**
10661 * <p>Define whether the horizontal scrollbar should be drawn or not. The
10662 * scrollbar is not drawn by default.</p>
10663 *
10664 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
10665 * be painted
10666 *
10667 * @see #isHorizontalScrollBarEnabled()
10668 */
10669 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
10670 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
10671 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010672 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010673 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010674 }
10675 }
10676
10677 /**
10678 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
10679 * scrollbar is not drawn by default.</p>
10680 *
10681 * @return true if the vertical scrollbar should be painted, false
10682 * otherwise
10683 *
10684 * @see #setVerticalScrollBarEnabled(boolean)
10685 */
10686 public boolean isVerticalScrollBarEnabled() {
10687 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
10688 }
10689
10690 /**
10691 * <p>Define whether the vertical scrollbar should be drawn or not. The
10692 * scrollbar is not drawn by default.</p>
10693 *
10694 * @param verticalScrollBarEnabled true if the vertical scrollbar should
10695 * be painted
10696 *
10697 * @see #isVerticalScrollBarEnabled()
10698 */
10699 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
10700 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
10701 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010702 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010703 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010704 }
10705 }
10706
Adam Powell20232d02010-12-08 21:08:53 -080010707 /**
10708 * @hide
10709 */
10710 protected void recomputePadding() {
10711 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010712 }
Joe Malin32736f02011-01-19 16:14:20 -080010713
Mike Cleronfe81d382009-09-28 14:22:16 -070010714 /**
10715 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080010716 *
Mike Cleronfe81d382009-09-28 14:22:16 -070010717 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080010718 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010719 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070010720 */
10721 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
10722 initScrollCache();
10723 final ScrollabilityCache scrollabilityCache = mScrollCache;
10724 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010725 if (fadeScrollbars) {
10726 scrollabilityCache.state = ScrollabilityCache.OFF;
10727 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070010728 scrollabilityCache.state = ScrollabilityCache.ON;
10729 }
10730 }
Joe Malin32736f02011-01-19 16:14:20 -080010731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010732 /**
Joe Malin32736f02011-01-19 16:14:20 -080010733 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010734 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080010735 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010736 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070010737 *
10738 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070010739 */
10740 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080010741 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010742 }
Joe Malin32736f02011-01-19 16:14:20 -080010743
Mike Cleron52f0a642009-09-28 18:21:37 -070010744 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070010745 *
10746 * Returns the delay before scrollbars fade.
10747 *
10748 * @return the delay before scrollbars fade
10749 *
10750 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10751 */
10752 public int getScrollBarDefaultDelayBeforeFade() {
10753 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
10754 mScrollCache.scrollBarDefaultDelayBeforeFade;
10755 }
10756
10757 /**
10758 * Define the delay before scrollbars fade.
10759 *
10760 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
10761 *
10762 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10763 */
10764 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
10765 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
10766 }
10767
10768 /**
10769 *
10770 * Returns the scrollbar fade duration.
10771 *
10772 * @return the scrollbar fade duration
10773 *
10774 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10775 */
10776 public int getScrollBarFadeDuration() {
10777 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
10778 mScrollCache.scrollBarFadeDuration;
10779 }
10780
10781 /**
10782 * Define the scrollbar fade duration.
10783 *
10784 * @param scrollBarFadeDuration - the scrollbar fade duration
10785 *
10786 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10787 */
10788 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
10789 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
10790 }
10791
10792 /**
10793 *
10794 * Returns the scrollbar size.
10795 *
10796 * @return the scrollbar size
10797 *
10798 * @attr ref android.R.styleable#View_scrollbarSize
10799 */
10800 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070010801 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070010802 mScrollCache.scrollBarSize;
10803 }
10804
10805 /**
10806 * Define the scrollbar size.
10807 *
10808 * @param scrollBarSize - the scrollbar size
10809 *
10810 * @attr ref android.R.styleable#View_scrollbarSize
10811 */
10812 public void setScrollBarSize(int scrollBarSize) {
10813 getScrollCache().scrollBarSize = scrollBarSize;
10814 }
10815
10816 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010817 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
10818 * inset. When inset, they add to the padding of the view. And the scrollbars
10819 * can be drawn inside the padding area or on the edge of the view. For example,
10820 * if a view has a background drawable and you want to draw the scrollbars
10821 * inside the padding specified by the drawable, you can use
10822 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
10823 * appear at the edge of the view, ignoring the padding, then you can use
10824 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
10825 * @param style the style of the scrollbars. Should be one of
10826 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
10827 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
10828 * @see #SCROLLBARS_INSIDE_OVERLAY
10829 * @see #SCROLLBARS_INSIDE_INSET
10830 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10831 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010832 *
10833 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010834 */
10835 public void setScrollBarStyle(int style) {
10836 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
10837 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070010838 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010839 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010840 }
10841 }
10842
10843 /**
10844 * <p>Returns the current scrollbar style.</p>
10845 * @return the current scrollbar style
10846 * @see #SCROLLBARS_INSIDE_OVERLAY
10847 * @see #SCROLLBARS_INSIDE_INSET
10848 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10849 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010850 *
10851 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010852 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070010853 @ViewDebug.ExportedProperty(mapping = {
10854 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
10855 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
10856 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
10857 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
10858 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010859 public int getScrollBarStyle() {
10860 return mViewFlags & SCROLLBARS_STYLE_MASK;
10861 }
10862
10863 /**
10864 * <p>Compute the horizontal range that the horizontal scrollbar
10865 * represents.</p>
10866 *
10867 * <p>The range is expressed in arbitrary units that must be the same as the
10868 * units used by {@link #computeHorizontalScrollExtent()} and
10869 * {@link #computeHorizontalScrollOffset()}.</p>
10870 *
10871 * <p>The default range is the drawing width of this view.</p>
10872 *
10873 * @return the total horizontal range represented by the horizontal
10874 * scrollbar
10875 *
10876 * @see #computeHorizontalScrollExtent()
10877 * @see #computeHorizontalScrollOffset()
10878 * @see android.widget.ScrollBarDrawable
10879 */
10880 protected int computeHorizontalScrollRange() {
10881 return getWidth();
10882 }
10883
10884 /**
10885 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
10886 * within the horizontal range. This value is used to compute the position
10887 * of the thumb within the scrollbar's track.</p>
10888 *
10889 * <p>The range is expressed in arbitrary units that must be the same as the
10890 * units used by {@link #computeHorizontalScrollRange()} and
10891 * {@link #computeHorizontalScrollExtent()}.</p>
10892 *
10893 * <p>The default offset is the scroll offset of this view.</p>
10894 *
10895 * @return the horizontal offset of the scrollbar's thumb
10896 *
10897 * @see #computeHorizontalScrollRange()
10898 * @see #computeHorizontalScrollExtent()
10899 * @see android.widget.ScrollBarDrawable
10900 */
10901 protected int computeHorizontalScrollOffset() {
10902 return mScrollX;
10903 }
10904
10905 /**
10906 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
10907 * within the horizontal range. This value is used to compute the length
10908 * of the thumb within the scrollbar's track.</p>
10909 *
10910 * <p>The range is expressed in arbitrary units that must be the same as the
10911 * units used by {@link #computeHorizontalScrollRange()} and
10912 * {@link #computeHorizontalScrollOffset()}.</p>
10913 *
10914 * <p>The default extent is the drawing width of this view.</p>
10915 *
10916 * @return the horizontal extent of the scrollbar's thumb
10917 *
10918 * @see #computeHorizontalScrollRange()
10919 * @see #computeHorizontalScrollOffset()
10920 * @see android.widget.ScrollBarDrawable
10921 */
10922 protected int computeHorizontalScrollExtent() {
10923 return getWidth();
10924 }
10925
10926 /**
10927 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
10928 *
10929 * <p>The range is expressed in arbitrary units that must be the same as the
10930 * units used by {@link #computeVerticalScrollExtent()} and
10931 * {@link #computeVerticalScrollOffset()}.</p>
10932 *
10933 * @return the total vertical range represented by the vertical scrollbar
10934 *
10935 * <p>The default range is the drawing height of this view.</p>
10936 *
10937 * @see #computeVerticalScrollExtent()
10938 * @see #computeVerticalScrollOffset()
10939 * @see android.widget.ScrollBarDrawable
10940 */
10941 protected int computeVerticalScrollRange() {
10942 return getHeight();
10943 }
10944
10945 /**
10946 * <p>Compute the vertical offset of the vertical scrollbar's thumb
10947 * within the horizontal range. This value is used to compute the position
10948 * of the thumb within the scrollbar's track.</p>
10949 *
10950 * <p>The range is expressed in arbitrary units that must be the same as the
10951 * units used by {@link #computeVerticalScrollRange()} and
10952 * {@link #computeVerticalScrollExtent()}.</p>
10953 *
10954 * <p>The default offset is the scroll offset of this view.</p>
10955 *
10956 * @return the vertical offset of the scrollbar's thumb
10957 *
10958 * @see #computeVerticalScrollRange()
10959 * @see #computeVerticalScrollExtent()
10960 * @see android.widget.ScrollBarDrawable
10961 */
10962 protected int computeVerticalScrollOffset() {
10963 return mScrollY;
10964 }
10965
10966 /**
10967 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
10968 * within the vertical range. This value is used to compute the length
10969 * of the thumb within the scrollbar's track.</p>
10970 *
10971 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080010972 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010973 * {@link #computeVerticalScrollOffset()}.</p>
10974 *
10975 * <p>The default extent is the drawing height of this view.</p>
10976 *
10977 * @return the vertical extent of the scrollbar's thumb
10978 *
10979 * @see #computeVerticalScrollRange()
10980 * @see #computeVerticalScrollOffset()
10981 * @see android.widget.ScrollBarDrawable
10982 */
10983 protected int computeVerticalScrollExtent() {
10984 return getHeight();
10985 }
10986
10987 /**
Adam Powell69159442011-06-13 17:53:06 -070010988 * Check if this view can be scrolled horizontally in a certain direction.
10989 *
10990 * @param direction Negative to check scrolling left, positive to check scrolling right.
10991 * @return true if this view can be scrolled in the specified direction, false otherwise.
10992 */
10993 public boolean canScrollHorizontally(int direction) {
10994 final int offset = computeHorizontalScrollOffset();
10995 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
10996 if (range == 0) return false;
10997 if (direction < 0) {
10998 return offset > 0;
10999 } else {
11000 return offset < range - 1;
11001 }
11002 }
11003
11004 /**
11005 * Check if this view can be scrolled vertically in a certain direction.
11006 *
11007 * @param direction Negative to check scrolling up, positive to check scrolling down.
11008 * @return true if this view can be scrolled in the specified direction, false otherwise.
11009 */
11010 public boolean canScrollVertically(int direction) {
11011 final int offset = computeVerticalScrollOffset();
11012 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
11013 if (range == 0) return false;
11014 if (direction < 0) {
11015 return offset > 0;
11016 } else {
11017 return offset < range - 1;
11018 }
11019 }
11020
11021 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011022 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
11023 * scrollbars are painted only if they have been awakened first.</p>
11024 *
11025 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080011026 *
Mike Cleronf116bf82009-09-27 19:14:12 -070011027 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011028 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080011029 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011030 // scrollbars are drawn only when the animation is running
11031 final ScrollabilityCache cache = mScrollCache;
11032 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080011033
Mike Cleronf116bf82009-09-27 19:14:12 -070011034 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080011035
Mike Cleronf116bf82009-09-27 19:14:12 -070011036 if (state == ScrollabilityCache.OFF) {
11037 return;
11038 }
Joe Malin32736f02011-01-19 16:14:20 -080011039
Mike Cleronf116bf82009-09-27 19:14:12 -070011040 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080011041
Mike Cleronf116bf82009-09-27 19:14:12 -070011042 if (state == ScrollabilityCache.FADING) {
11043 // We're fading -- get our fade interpolation
11044 if (cache.interpolatorValues == null) {
11045 cache.interpolatorValues = new float[1];
11046 }
Joe Malin32736f02011-01-19 16:14:20 -080011047
Mike Cleronf116bf82009-09-27 19:14:12 -070011048 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080011049
Mike Cleronf116bf82009-09-27 19:14:12 -070011050 // Stops the animation if we're done
11051 if (cache.scrollBarInterpolator.timeToValues(values) ==
11052 Interpolator.Result.FREEZE_END) {
11053 cache.state = ScrollabilityCache.OFF;
11054 } else {
11055 cache.scrollBar.setAlpha(Math.round(values[0]));
11056 }
Joe Malin32736f02011-01-19 16:14:20 -080011057
11058 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070011059 // drawing. We only want this when we're fading so that
11060 // we prevent excessive redraws
11061 invalidate = true;
11062 } else {
11063 // We're just on -- but we may have been fading before so
11064 // reset alpha
11065 cache.scrollBar.setAlpha(255);
11066 }
11067
Joe Malin32736f02011-01-19 16:14:20 -080011068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011069 final int viewFlags = mViewFlags;
11070
11071 final boolean drawHorizontalScrollBar =
11072 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
11073 final boolean drawVerticalScrollBar =
11074 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
11075 && !isVerticalScrollBarHidden();
11076
11077 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
11078 final int width = mRight - mLeft;
11079 final int height = mBottom - mTop;
11080
11081 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011082
Mike Reede8853fc2009-09-04 14:01:48 -040011083 final int scrollX = mScrollX;
11084 final int scrollY = mScrollY;
11085 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
11086
Mike Cleronf116bf82009-09-27 19:14:12 -070011087 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080011088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011089 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011090 int size = scrollBar.getSize(false);
11091 if (size <= 0) {
11092 size = cache.scrollBarSize;
11093 }
11094
Mike Cleronf116bf82009-09-27 19:14:12 -070011095 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040011096 computeHorizontalScrollOffset(),
11097 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040011098 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070011099 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080011100 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070011101 left = scrollX + (mPaddingLeft & inside);
11102 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
11103 bottom = top + size;
11104 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
11105 if (invalidate) {
11106 invalidate(left, top, right, bottom);
11107 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011108 }
11109
11110 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011111 int size = scrollBar.getSize(true);
11112 if (size <= 0) {
11113 size = cache.scrollBarSize;
11114 }
11115
Mike Reede8853fc2009-09-04 14:01:48 -040011116 scrollBar.setParameters(computeVerticalScrollRange(),
11117 computeVerticalScrollOffset(),
11118 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -080011119 switch (mVerticalScrollbarPosition) {
11120 default:
11121 case SCROLLBAR_POSITION_DEFAULT:
11122 case SCROLLBAR_POSITION_RIGHT:
11123 left = scrollX + width - size - (mUserPaddingRight & inside);
11124 break;
11125 case SCROLLBAR_POSITION_LEFT:
11126 left = scrollX + (mUserPaddingLeft & inside);
11127 break;
11128 }
Mike Cleronf116bf82009-09-27 19:14:12 -070011129 top = scrollY + (mPaddingTop & inside);
11130 right = left + size;
11131 bottom = scrollY + height - (mUserPaddingBottom & inside);
11132 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
11133 if (invalidate) {
11134 invalidate(left, top, right, bottom);
11135 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011136 }
11137 }
11138 }
11139 }
Romain Guy8506ab42009-06-11 17:35:47 -070011140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011141 /**
Romain Guy8506ab42009-06-11 17:35:47 -070011142 * 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 -080011143 * FastScroller is visible.
11144 * @return whether to temporarily hide the vertical scrollbar
11145 * @hide
11146 */
11147 protected boolean isVerticalScrollBarHidden() {
11148 return false;
11149 }
11150
11151 /**
11152 * <p>Draw the horizontal scrollbar if
11153 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
11154 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011155 * @param canvas the canvas on which to draw the scrollbar
11156 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011157 *
11158 * @see #isHorizontalScrollBarEnabled()
11159 * @see #computeHorizontalScrollRange()
11160 * @see #computeHorizontalScrollExtent()
11161 * @see #computeHorizontalScrollOffset()
11162 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070011163 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011164 */
Romain Guy8fb95422010-08-17 18:38:51 -070011165 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
11166 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011167 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011168 scrollBar.draw(canvas);
11169 }
Mike Reede8853fc2009-09-04 14:01:48 -040011170
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011171 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011172 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
11173 * returns true.</p>
11174 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011175 * @param canvas the canvas on which to draw the scrollbar
11176 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011177 *
11178 * @see #isVerticalScrollBarEnabled()
11179 * @see #computeVerticalScrollRange()
11180 * @see #computeVerticalScrollExtent()
11181 * @see #computeVerticalScrollOffset()
11182 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040011183 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011184 */
Romain Guy8fb95422010-08-17 18:38:51 -070011185 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
11186 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040011187 scrollBar.setBounds(l, t, r, b);
11188 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011189 }
11190
11191 /**
11192 * Implement this to do your drawing.
11193 *
11194 * @param canvas the canvas on which the background will be drawn
11195 */
11196 protected void onDraw(Canvas canvas) {
11197 }
11198
11199 /*
11200 * Caller is responsible for calling requestLayout if necessary.
11201 * (This allows addViewInLayout to not request a new layout.)
11202 */
11203 void assignParent(ViewParent parent) {
11204 if (mParent == null) {
11205 mParent = parent;
11206 } else if (parent == null) {
11207 mParent = null;
11208 } else {
11209 throw new RuntimeException("view " + this + " being added, but"
11210 + " it already has a parent");
11211 }
11212 }
11213
11214 /**
11215 * This is called when the view is attached to a window. At this point it
11216 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011217 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
11218 * however it may be called any time before the first onDraw -- including
11219 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011220 *
11221 * @see #onDetachedFromWindow()
11222 */
11223 protected void onAttachedToWindow() {
11224 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
11225 mParent.requestTransparentRegion(this);
11226 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011227
Adam Powell8568c3a2010-04-19 14:26:11 -070011228 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
11229 initialAwakenScrollBars();
11230 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
11231 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011232
Chet Haasea9b61ac2010-12-20 07:40:25 -080011233 jumpDrawablesToCurrentState();
Romain Guy2a0f2282012-05-08 14:43:12 -070011234
Fabrice Di Meglioa6461452011-08-19 15:42:04 -070011235 // Order is important here: LayoutDirection MUST be resolved before Padding
11236 // and TextDirection
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011237 resolveLayoutDirection();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011238 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011239 resolveTextDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011240 resolveTextAlignment();
Romain Guy2a0f2282012-05-08 14:43:12 -070011241
Svetoslav Ganov42138042012-03-20 11:51:39 -070011242 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070011243 if (isFocused()) {
11244 InputMethodManager imm = InputMethodManager.peekInstance();
11245 imm.focusIn(this);
11246 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011247
11248 if (mAttachInfo != null && mDisplayList != null) {
11249 mAttachInfo.mViewRootImpl.dequeueDisplayList(mDisplayList);
11250 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011251 }
Cibu Johny86666632010-02-22 13:01:02 -080011252
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011253 /**
Romain Guybb9908b2012-03-08 11:14:07 -080011254 * @see #onScreenStateChanged(int)
11255 */
11256 void dispatchScreenStateChanged(int screenState) {
11257 onScreenStateChanged(screenState);
11258 }
11259
11260 /**
11261 * This method is called whenever the state of the screen this view is
11262 * attached to changes. A state change will usually occurs when the screen
11263 * turns on or off (whether it happens automatically or the user does it
11264 * manually.)
11265 *
11266 * @param screenState The new state of the screen. Can be either
11267 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
11268 */
11269 public void onScreenStateChanged(int screenState) {
11270 }
11271
11272 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011273 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
11274 */
11275 private boolean hasRtlSupport() {
11276 return mContext.getApplicationInfo().hasRtlSupport();
11277 }
11278
11279 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011280 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
11281 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011282 * Will call {@link View#onResolvedLayoutDirectionChanged} when resolution is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011283 * @hide
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011284 */
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011285 public void resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011286 // Clear any previous layout direction resolution
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070011287 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011288
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011289 if (hasRtlSupport()) {
11290 // Set resolved depending on layout direction
11291 switch (getLayoutDirection()) {
11292 case LAYOUT_DIRECTION_INHERIT:
11293 // If this is root view, no need to look at parent's layout dir.
11294 if (canResolveLayoutDirection()) {
11295 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011296
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011297 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
11298 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11299 }
11300 } else {
11301 // Nothing to do, LTR by default
11302 }
11303 break;
11304 case LAYOUT_DIRECTION_RTL:
11305 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11306 break;
11307 case LAYOUT_DIRECTION_LOCALE:
11308 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011309 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11310 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011311 break;
11312 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011313 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011314 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011315 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011316
11317 // Set to resolved
11318 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011319 onResolvedLayoutDirectionChanged();
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080011320 // Resolve padding
11321 resolvePadding();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011322 }
11323
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011324 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011325 * Called when layout direction has been resolved.
11326 *
11327 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011328 * @hide
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011329 */
11330 public void onResolvedLayoutDirectionChanged() {
11331 }
11332
11333 /**
11334 * Resolve padding depending on layout direction.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011335 * @hide
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011336 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011337 public void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011338 // If the user specified the absolute padding (either with android:padding or
11339 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
11340 // use the default padding or the padding from the background drawable
11341 // (stored at this point in mPadding*)
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011342 int resolvedLayoutDirection = getResolvedLayoutDirection();
11343 switch (resolvedLayoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011344 case LAYOUT_DIRECTION_RTL:
11345 // Start user padding override Right user padding. Otherwise, if Right user
11346 // padding is not defined, use the default Right padding. If Right user padding
11347 // is defined, just use it.
11348 if (mUserPaddingStart >= 0) {
11349 mUserPaddingRight = mUserPaddingStart;
11350 } else if (mUserPaddingRight < 0) {
11351 mUserPaddingRight = mPaddingRight;
11352 }
11353 if (mUserPaddingEnd >= 0) {
11354 mUserPaddingLeft = mUserPaddingEnd;
11355 } else if (mUserPaddingLeft < 0) {
11356 mUserPaddingLeft = mPaddingLeft;
11357 }
11358 break;
11359 case LAYOUT_DIRECTION_LTR:
11360 default:
11361 // Start user padding override Left user padding. Otherwise, if Left user
11362 // padding is not defined, use the default left padding. If Left user padding
11363 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070011364 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011365 mUserPaddingLeft = mUserPaddingStart;
11366 } else if (mUserPaddingLeft < 0) {
11367 mUserPaddingLeft = mPaddingLeft;
11368 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070011369 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011370 mUserPaddingRight = mUserPaddingEnd;
11371 } else if (mUserPaddingRight < 0) {
11372 mUserPaddingRight = mPaddingRight;
11373 }
11374 }
11375
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011376 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
11377
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080011378 if(isPaddingRelative()) {
11379 setPaddingRelative(mUserPaddingStart, mPaddingTop, mUserPaddingEnd, mUserPaddingBottom);
11380 } else {
11381 recomputePadding();
11382 }
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011383 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011384 }
11385
11386 /**
11387 * Resolve padding depending on the layout direction. Subclasses that care about
11388 * padding resolution should override this method. The default implementation does
11389 * nothing.
11390 *
11391 * @param layoutDirection the direction of the layout
11392 *
Fabrice Di Meglioe8dc07d2012-03-09 17:10:19 -080011393 * @see {@link #LAYOUT_DIRECTION_LTR}
11394 * @see {@link #LAYOUT_DIRECTION_RTL}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011395 * @hide
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011396 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011397 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011398 }
11399
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011400 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011401 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011402 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011403 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011404 * @hide
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011405 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011406 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011407 switch (getLayoutDirection()) {
11408 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011409 return (mParent != null) && (mParent instanceof ViewGroup);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011410 default:
11411 return true;
11412 }
11413 }
11414
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011415 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011416 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
11417 * when reset is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011418 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011419 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011420 public void resetResolvedLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070011421 // Reset the current resolved bits
11422 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011423 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080011424 // Reset also the text direction
11425 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011426 }
11427
11428 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011429 * Called during reset of resolved layout direction.
11430 *
11431 * Subclasses need to override this method to clear cached information that depends on the
11432 * resolved layout direction, or to inform child views that inherit their layout direction.
11433 *
11434 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011435 * @hide
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011436 */
11437 public void onResolvedLayoutDirectionReset() {
11438 }
11439
11440 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011441 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011442 *
11443 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011444 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011445 * @hide
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011446 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011447 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -080011448 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011449 }
11450
11451 /**
11452 * This is called when the view is detached from a window. At this point it
11453 * no longer has a surface for drawing.
11454 *
11455 * @see #onAttachedToWindow()
11456 */
11457 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -080011458 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080011459
Romain Guya440b002010-02-24 15:57:54 -080011460 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050011461 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080011462 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070011463 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080011464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011465 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011466
Romain Guya998dff2012-03-23 18:58:36 -070011467 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011468
11469 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011470 if (mDisplayList != null) {
Romain Guy2a0f2282012-05-08 14:43:12 -070011471 mAttachInfo.mViewRootImpl.enqueueDisplayList(mDisplayList);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011472 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011473 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011474 } else {
Romain Guy38c2ece2012-05-24 14:20:56 -070011475 // Should never happen
11476 clearDisplayList();
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011477 }
11478
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011479 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011480
11481 resetResolvedLayoutDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011482 resetResolvedTextAlignment();
Svetoslav Ganov42138042012-03-20 11:51:39 -070011483 resetAccessibilityStateChanged();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011484 }
11485
11486 /**
11487 * @return The number of times this view has been attached to a window
11488 */
11489 protected int getWindowAttachCount() {
11490 return mWindowAttachCount;
11491 }
11492
11493 /**
11494 * Retrieve a unique token identifying the window this view is attached to.
11495 * @return Return the window's token for use in
11496 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11497 */
11498 public IBinder getWindowToken() {
11499 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11500 }
11501
11502 /**
11503 * Retrieve a unique token identifying the top-level "real" window of
11504 * the window that this view is attached to. That is, this is like
11505 * {@link #getWindowToken}, except if the window this view in is a panel
11506 * window (attached to another containing window), then the token of
11507 * the containing window is returned instead.
11508 *
11509 * @return Returns the associated window token, either
11510 * {@link #getWindowToken()} or the containing window's token.
11511 */
11512 public IBinder getApplicationWindowToken() {
11513 AttachInfo ai = mAttachInfo;
11514 if (ai != null) {
11515 IBinder appWindowToken = ai.mPanelParentWindowToken;
11516 if (appWindowToken == null) {
11517 appWindowToken = ai.mWindowToken;
11518 }
11519 return appWindowToken;
11520 }
11521 return null;
11522 }
11523
11524 /**
11525 * Retrieve private session object this view hierarchy is using to
11526 * communicate with the window manager.
11527 * @return the session object to communicate with the window manager
11528 */
11529 /*package*/ IWindowSession getWindowSession() {
11530 return mAttachInfo != null ? mAttachInfo.mSession : null;
11531 }
11532
11533 /**
11534 * @param info the {@link android.view.View.AttachInfo} to associated with
11535 * this view
11536 */
11537 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
11538 //System.out.println("Attached! " + this);
11539 mAttachInfo = info;
11540 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011541 // We will need to evaluate the drawable state at least once.
11542 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011543 if (mFloatingTreeObserver != null) {
11544 info.mTreeObserver.merge(mFloatingTreeObserver);
11545 mFloatingTreeObserver = null;
11546 }
11547 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
11548 mAttachInfo.mScrollContainers.add(this);
11549 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
11550 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070011551 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011552 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080011553
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011554 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011555 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011556 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011557 if (listeners != null && listeners.size() > 0) {
11558 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11559 // perform the dispatching. The iterator is a safe guard against listeners that
11560 // could mutate the list by calling the various add/remove methods. This prevents
11561 // the array from being modified while we iterate it.
11562 for (OnAttachStateChangeListener listener : listeners) {
11563 listener.onViewAttachedToWindow(this);
11564 }
11565 }
11566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011567 int vis = info.mWindowVisibility;
11568 if (vis != GONE) {
11569 onWindowVisibilityChanged(vis);
11570 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011571 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
11572 // If nobody has evaluated the drawable state yet, then do it now.
11573 refreshDrawableState();
11574 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011575 }
11576
11577 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011578 AttachInfo info = mAttachInfo;
11579 if (info != null) {
11580 int vis = info.mWindowVisibility;
11581 if (vis != GONE) {
11582 onWindowVisibilityChanged(GONE);
11583 }
11584 }
11585
11586 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080011587
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011588 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011589 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011590 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011591 if (listeners != null && listeners.size() > 0) {
11592 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11593 // perform the dispatching. The iterator is a safe guard against listeners that
11594 // could mutate the list by calling the various add/remove methods. This prevents
11595 // the array from being modified while we iterate it.
11596 for (OnAttachStateChangeListener listener : listeners) {
11597 listener.onViewDetachedFromWindow(this);
11598 }
11599 }
11600
Romain Guy01d5edc2011-01-28 11:28:53 -080011601 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011602 mAttachInfo.mScrollContainers.remove(this);
11603 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
11604 }
Romain Guy01d5edc2011-01-28 11:28:53 -080011605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011606 mAttachInfo = null;
11607 }
11608
11609 /**
11610 * Store this view hierarchy's frozen state into the given container.
11611 *
11612 * @param container The SparseArray in which to save the view's state.
11613 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011614 * @see #restoreHierarchyState(android.util.SparseArray)
11615 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11616 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011617 */
11618 public void saveHierarchyState(SparseArray<Parcelable> container) {
11619 dispatchSaveInstanceState(container);
11620 }
11621
11622 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011623 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
11624 * this view and its children. May be overridden to modify how freezing happens to a
11625 * 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 -080011626 *
11627 * @param container The SparseArray in which to save the view's state.
11628 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011629 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11630 * @see #saveHierarchyState(android.util.SparseArray)
11631 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011632 */
11633 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
11634 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
11635 mPrivateFlags &= ~SAVE_STATE_CALLED;
11636 Parcelable state = onSaveInstanceState();
11637 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11638 throw new IllegalStateException(
11639 "Derived class did not call super.onSaveInstanceState()");
11640 }
11641 if (state != null) {
11642 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
11643 // + ": " + state);
11644 container.put(mID, state);
11645 }
11646 }
11647 }
11648
11649 /**
11650 * Hook allowing a view to generate a representation of its internal state
11651 * that can later be used to create a new instance with that same state.
11652 * This state should only contain information that is not persistent or can
11653 * not be reconstructed later. For example, you will never store your
11654 * current position on screen because that will be computed again when a
11655 * new instance of the view is placed in its view hierarchy.
11656 * <p>
11657 * Some examples of things you may store here: the current cursor position
11658 * in a text view (but usually not the text itself since that is stored in a
11659 * content provider or other persistent storage), the currently selected
11660 * item in a list view.
11661 *
11662 * @return Returns a Parcelable object containing the view's current dynamic
11663 * state, or null if there is nothing interesting to save. The
11664 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070011665 * @see #onRestoreInstanceState(android.os.Parcelable)
11666 * @see #saveHierarchyState(android.util.SparseArray)
11667 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011668 * @see #setSaveEnabled(boolean)
11669 */
11670 protected Parcelable onSaveInstanceState() {
11671 mPrivateFlags |= SAVE_STATE_CALLED;
11672 return BaseSavedState.EMPTY_STATE;
11673 }
11674
11675 /**
11676 * Restore this view hierarchy's frozen state from the given container.
11677 *
11678 * @param container The SparseArray which holds previously frozen states.
11679 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011680 * @see #saveHierarchyState(android.util.SparseArray)
11681 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11682 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011683 */
11684 public void restoreHierarchyState(SparseArray<Parcelable> container) {
11685 dispatchRestoreInstanceState(container);
11686 }
11687
11688 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011689 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
11690 * state for this view and its children. May be overridden to modify how restoring
11691 * happens to a view's children; for example, some views may want to not store state
11692 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011693 *
11694 * @param container The SparseArray which holds previously saved state.
11695 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011696 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11697 * @see #restoreHierarchyState(android.util.SparseArray)
11698 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011699 */
11700 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
11701 if (mID != NO_ID) {
11702 Parcelable state = container.get(mID);
11703 if (state != null) {
11704 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
11705 // + ": " + state);
11706 mPrivateFlags &= ~SAVE_STATE_CALLED;
11707 onRestoreInstanceState(state);
11708 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11709 throw new IllegalStateException(
11710 "Derived class did not call super.onRestoreInstanceState()");
11711 }
11712 }
11713 }
11714 }
11715
11716 /**
11717 * Hook allowing a view to re-apply a representation of its internal state that had previously
11718 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
11719 * null state.
11720 *
11721 * @param state The frozen state that had previously been returned by
11722 * {@link #onSaveInstanceState}.
11723 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011724 * @see #onSaveInstanceState()
11725 * @see #restoreHierarchyState(android.util.SparseArray)
11726 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011727 */
11728 protected void onRestoreInstanceState(Parcelable state) {
11729 mPrivateFlags |= SAVE_STATE_CALLED;
11730 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080011731 throw new IllegalArgumentException("Wrong state class, expecting View State but "
11732 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080011733 + "when two views of different type have the same id in the same hierarchy. "
11734 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080011735 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011736 }
11737 }
11738
11739 /**
11740 * <p>Return the time at which the drawing of the view hierarchy started.</p>
11741 *
11742 * @return the drawing start time in milliseconds
11743 */
11744 public long getDrawingTime() {
11745 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
11746 }
11747
11748 /**
11749 * <p>Enables or disables the duplication of the parent's state into this view. When
11750 * duplication is enabled, this view gets its drawable state from its parent rather
11751 * than from its own internal properties.</p>
11752 *
11753 * <p>Note: in the current implementation, setting this property to true after the
11754 * view was added to a ViewGroup might have no effect at all. This property should
11755 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
11756 *
11757 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
11758 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011759 *
Gilles Debunnefb817032011-01-13 13:52:49 -080011760 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
11761 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011762 *
11763 * @param enabled True to enable duplication of the parent's drawable state, false
11764 * to disable it.
11765 *
11766 * @see #getDrawableState()
11767 * @see #isDuplicateParentStateEnabled()
11768 */
11769 public void setDuplicateParentStateEnabled(boolean enabled) {
11770 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
11771 }
11772
11773 /**
11774 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
11775 *
11776 * @return True if this view's drawable state is duplicated from the parent,
11777 * false otherwise
11778 *
11779 * @see #getDrawableState()
11780 * @see #setDuplicateParentStateEnabled(boolean)
11781 */
11782 public boolean isDuplicateParentStateEnabled() {
11783 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
11784 }
11785
11786 /**
Romain Guy171c5922011-01-06 10:04:23 -080011787 * <p>Specifies the type of layer backing this view. The layer can be
11788 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
11789 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011790 *
Romain Guy171c5922011-01-06 10:04:23 -080011791 * <p>A layer is associated with an optional {@link android.graphics.Paint}
11792 * instance that controls how the layer is composed on screen. The following
11793 * properties of the paint are taken into account when composing the layer:</p>
11794 * <ul>
11795 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
11796 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
11797 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
11798 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080011799 *
Romain Guy171c5922011-01-06 10:04:23 -080011800 * <p>If this view has an alpha value set to < 1.0 by calling
11801 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
11802 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
11803 * equivalent to setting a hardware layer on this view and providing a paint with
11804 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080011805 *
Romain Guy171c5922011-01-06 10:04:23 -080011806 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
11807 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
11808 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011809 *
Romain Guy171c5922011-01-06 10:04:23 -080011810 * @param layerType The ype of layer to use with this view, must be one of
11811 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11812 * {@link #LAYER_TYPE_HARDWARE}
11813 * @param paint The paint used to compose the layer. This argument is optional
11814 * and can be null. It is ignored when the layer type is
11815 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080011816 *
11817 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080011818 * @see #LAYER_TYPE_NONE
11819 * @see #LAYER_TYPE_SOFTWARE
11820 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080011821 * @see #setAlpha(float)
11822 *
Romain Guy171c5922011-01-06 10:04:23 -080011823 * @attr ref android.R.styleable#View_layerType
11824 */
11825 public void setLayerType(int layerType, Paint paint) {
11826 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080011827 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080011828 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
11829 }
Chet Haasedaf98e92011-01-10 14:10:36 -080011830
Romain Guyd6cd5722011-01-17 14:42:41 -080011831 if (layerType == mLayerType) {
11832 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
11833 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011834 invalidateParentCaches();
11835 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080011836 }
11837 return;
11838 }
Romain Guy171c5922011-01-06 10:04:23 -080011839
11840 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080011841 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070011842 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070011843 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011844 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080011845 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070011846 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011847 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080011848 default:
11849 break;
Romain Guy171c5922011-01-06 10:04:23 -080011850 }
11851
11852 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080011853 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
11854 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
11855 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080011856
Romain Guy0fd89bf2011-01-26 15:41:30 -080011857 invalidateParentCaches();
11858 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080011859 }
11860
11861 /**
Romain Guy59c7f802011-09-29 17:21:45 -070011862 * Indicates whether this view has a static layer. A view with layer type
11863 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
11864 * dynamic.
11865 */
11866 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080011867 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070011868 }
11869
11870 /**
Romain Guy171c5922011-01-06 10:04:23 -080011871 * Indicates what type of layer is currently associated with this view. By default
11872 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
11873 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
11874 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080011875 *
Romain Guy171c5922011-01-06 10:04:23 -080011876 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11877 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080011878 *
11879 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070011880 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080011881 * @see #LAYER_TYPE_NONE
11882 * @see #LAYER_TYPE_SOFTWARE
11883 * @see #LAYER_TYPE_HARDWARE
11884 */
11885 public int getLayerType() {
11886 return mLayerType;
11887 }
Joe Malin32736f02011-01-19 16:14:20 -080011888
Romain Guy6c319ca2011-01-11 14:29:25 -080011889 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080011890 * Forces this view's layer to be created and this view to be rendered
11891 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
11892 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070011893 *
Romain Guyf1ae1062011-03-02 18:16:04 -080011894 * This method can for instance be used to render a view into its layer before
11895 * starting an animation. If this view is complex, rendering into the layer
11896 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070011897 *
Romain Guyf1ae1062011-03-02 18:16:04 -080011898 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070011899 *
11900 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080011901 */
11902 public void buildLayer() {
11903 if (mLayerType == LAYER_TYPE_NONE) return;
11904
11905 if (mAttachInfo == null) {
11906 throw new IllegalStateException("This view must be attached to a window first");
11907 }
11908
11909 switch (mLayerType) {
11910 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080011911 if (mAttachInfo.mHardwareRenderer != null &&
11912 mAttachInfo.mHardwareRenderer.isEnabled() &&
11913 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080011914 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080011915 }
Romain Guyf1ae1062011-03-02 18:16:04 -080011916 break;
11917 case LAYER_TYPE_SOFTWARE:
11918 buildDrawingCache(true);
11919 break;
11920 }
11921 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011922
Romain Guy9c4b79a2011-11-10 19:23:58 -080011923 // Make sure the HardwareRenderer.validate() was invoked before calling this method
11924 void flushLayer() {
11925 if (mLayerType == LAYER_TYPE_HARDWARE && mHardwareLayer != null) {
11926 mHardwareLayer.flush();
11927 }
11928 }
Romain Guyf1ae1062011-03-02 18:16:04 -080011929
11930 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080011931 * <p>Returns a hardware layer that can be used to draw this view again
11932 * without executing its draw method.</p>
11933 *
11934 * @return A HardwareLayer ready to render, or null if an error occurred.
11935 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080011936 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070011937 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
11938 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080011939 return null;
11940 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011941
Romain Guy9c4b79a2011-11-10 19:23:58 -080011942 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080011943
11944 final int width = mRight - mLeft;
11945 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080011946
Romain Guy6c319ca2011-01-11 14:29:25 -080011947 if (width == 0 || height == 0) {
11948 return null;
11949 }
11950
11951 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
11952 if (mHardwareLayer == null) {
11953 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
11954 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070011955 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080011956 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
11957 mHardwareLayer.resize(width, height);
Michael Jurka952e02b2012-03-13 18:34:35 -070011958 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080011959 }
11960
Romain Guy5cd5c3f2011-10-17 17:10:02 -070011961 // The layer is not valid if the underlying GPU resources cannot be allocated
11962 if (!mHardwareLayer.isValid()) {
11963 return null;
11964 }
11965
Chet Haasea1cff502012-02-21 13:43:44 -080011966 mHardwareLayer.redraw(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
Michael Jurka7e52caf2012-03-06 15:57:06 -080011967 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080011968 }
11969
11970 return mHardwareLayer;
11971 }
Romain Guy171c5922011-01-06 10:04:23 -080011972
Romain Guy589b0bb2011-10-10 13:57:47 -070011973 /**
11974 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070011975 *
Romain Guy589b0bb2011-10-10 13:57:47 -070011976 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070011977 *
11978 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070011979 * @see #LAYER_TYPE_HARDWARE
11980 */
Romain Guya998dff2012-03-23 18:58:36 -070011981 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070011982 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080011983 AttachInfo info = mAttachInfo;
11984 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070011985 info.mHardwareRenderer.isEnabled() &&
11986 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080011987 mHardwareLayer.destroy();
11988 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080011989
Romain Guy9c4b79a2011-11-10 19:23:58 -080011990 invalidate(true);
11991 invalidateParentCaches();
11992 }
Romain Guy65b345f2011-07-27 18:51:50 -070011993 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070011994 }
Romain Guy65b345f2011-07-27 18:51:50 -070011995 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070011996 }
11997
Romain Guy171c5922011-01-06 10:04:23 -080011998 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080011999 * Destroys all hardware rendering resources. This method is invoked
12000 * when the system needs to reclaim resources. Upon execution of this
12001 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070012002 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012003 * Note: you <strong>must</strong> call
12004 * <code>super.destroyHardwareResources()</code> when overriding
12005 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070012006 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012007 * @hide
12008 */
12009 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070012010 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012011 }
12012
12013 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012014 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
12015 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
12016 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
12017 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
12018 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
12019 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012020 *
Romain Guy171c5922011-01-06 10:04:23 -080012021 * <p>Enabling the drawing cache is similar to
12022 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080012023 * acceleration is turned off. When hardware acceleration is turned on, enabling the
12024 * drawing cache has no effect on rendering because the system uses a different mechanism
12025 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
12026 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
12027 * for information on how to enable software and hardware layers.</p>
12028 *
12029 * <p>This API can be used to manually generate
12030 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
12031 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012032 *
12033 * @param enabled true to enable the drawing cache, false otherwise
12034 *
12035 * @see #isDrawingCacheEnabled()
12036 * @see #getDrawingCache()
12037 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080012038 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012039 */
12040 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012041 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012042 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
12043 }
12044
12045 /**
12046 * <p>Indicates whether the drawing cache is enabled for this view.</p>
12047 *
12048 * @return true if the drawing cache is enabled
12049 *
12050 * @see #setDrawingCacheEnabled(boolean)
12051 * @see #getDrawingCache()
12052 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012053 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012054 public boolean isDrawingCacheEnabled() {
12055 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
12056 }
12057
12058 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080012059 * Debugging utility which recursively outputs the dirty state of a view and its
12060 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080012061 *
Chet Haasedaf98e92011-01-10 14:10:36 -080012062 * @hide
12063 */
Romain Guy676b1732011-02-14 14:45:33 -080012064 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080012065 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
12066 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
12067 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
12068 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
12069 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
12070 if (clear) {
12071 mPrivateFlags &= clearMask;
12072 }
12073 if (this instanceof ViewGroup) {
12074 ViewGroup parent = (ViewGroup) this;
12075 final int count = parent.getChildCount();
12076 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080012077 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080012078 child.outputDirtyFlags(indent + " ", clear, clearMask);
12079 }
12080 }
12081 }
12082
12083 /**
12084 * This method is used by ViewGroup to cause its children to restore or recreate their
12085 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
12086 * to recreate its own display list, which would happen if it went through the normal
12087 * draw/dispatchDraw mechanisms.
12088 *
12089 * @hide
12090 */
12091 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080012092
12093 /**
12094 * A view that is not attached or hardware accelerated cannot create a display list.
12095 * This method checks these conditions and returns the appropriate result.
12096 *
12097 * @return true if view has the ability to create a display list, false otherwise.
12098 *
12099 * @hide
12100 */
12101 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080012102 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080012103 }
Joe Malin32736f02011-01-19 16:14:20 -080012104
Chet Haasedaf98e92011-01-10 14:10:36 -080012105 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080012106 * @return The HardwareRenderer associated with that view or null if hardware rendering
12107 * is not supported or this this has not been attached to a window.
12108 *
12109 * @hide
12110 */
12111 public HardwareRenderer getHardwareRenderer() {
12112 if (mAttachInfo != null) {
12113 return mAttachInfo.mHardwareRenderer;
12114 }
12115 return null;
12116 }
12117
12118 /**
Chet Haasea1cff502012-02-21 13:43:44 -080012119 * Returns a DisplayList. If the incoming displayList is null, one will be created.
12120 * Otherwise, the same display list will be returned (after having been rendered into
12121 * along the way, depending on the invalidation state of the view).
12122 *
12123 * @param displayList The previous version of this displayList, could be null.
12124 * @param isLayer Whether the requester of the display list is a layer. If so,
12125 * the view will avoid creating a layer inside the resulting display list.
12126 * @return A new or reused DisplayList object.
12127 */
12128 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
12129 if (!canHaveDisplayList()) {
12130 return null;
12131 }
12132
12133 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
12134 displayList == null || !displayList.isValid() ||
12135 (!isLayer && mRecreateDisplayList))) {
12136 // Don't need to recreate the display list, just need to tell our
12137 // children to restore/recreate theirs
12138 if (displayList != null && displayList.isValid() &&
12139 !isLayer && !mRecreateDisplayList) {
12140 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12141 mPrivateFlags &= ~DIRTY_MASK;
12142 dispatchGetDisplayList();
12143
12144 return displayList;
12145 }
12146
12147 if (!isLayer) {
12148 // If we got here, we're recreating it. Mark it as such to ensure that
12149 // we copy in child display lists into ours in drawChild()
12150 mRecreateDisplayList = true;
12151 }
12152 if (displayList == null) {
12153 final String name = getClass().getSimpleName();
12154 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
12155 // If we're creating a new display list, make sure our parent gets invalidated
12156 // since they will need to recreate their display list to account for this
12157 // new child display list.
12158 invalidateParentCaches();
12159 }
12160
12161 boolean caching = false;
12162 final HardwareCanvas canvas = displayList.start();
Chet Haasea1cff502012-02-21 13:43:44 -080012163 int width = mRight - mLeft;
12164 int height = mBottom - mTop;
12165
12166 try {
12167 canvas.setViewport(width, height);
12168 // The dirty rect should always be null for a display list
12169 canvas.onPreDraw(null);
12170 int layerType = (
12171 !(mParent instanceof ViewGroup) || ((ViewGroup)mParent).mDrawLayers) ?
12172 getLayerType() : LAYER_TYPE_NONE;
Chet Haase1271e2c2012-04-20 09:54:27 -070012173 if (!isLayer && layerType != LAYER_TYPE_NONE) {
Chet Haaseb85967b2012-03-26 14:37:51 -070012174 if (layerType == LAYER_TYPE_HARDWARE) {
12175 final HardwareLayer layer = getHardwareLayer();
12176 if (layer != null && layer.isValid()) {
12177 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
12178 } else {
12179 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
12180 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
12181 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12182 }
12183 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080012184 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070012185 buildDrawingCache(true);
12186 Bitmap cache = getDrawingCache(true);
12187 if (cache != null) {
12188 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
12189 caching = true;
12190 }
Chet Haasea1cff502012-02-21 13:43:44 -080012191 }
Chet Haasea1cff502012-02-21 13:43:44 -080012192 } else {
12193
12194 computeScroll();
12195
Chet Haasea1cff502012-02-21 13:43:44 -080012196 canvas.translate(-mScrollX, -mScrollY);
12197 if (!isLayer) {
12198 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12199 mPrivateFlags &= ~DIRTY_MASK;
12200 }
12201
12202 // Fast path for layouts with no backgrounds
12203 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12204 dispatchDraw(canvas);
12205 } else {
12206 draw(canvas);
12207 }
12208 }
12209 } finally {
Chet Haasea1cff502012-02-21 13:43:44 -080012210 canvas.onPostDraw();
12211
12212 displayList.end();
Chet Haase1271e2c2012-04-20 09:54:27 -070012213 displayList.setCaching(caching);
12214 if (isLayer) {
Chet Haasea1cff502012-02-21 13:43:44 -080012215 displayList.setLeftTopRightBottom(0, 0, width, height);
12216 } else {
12217 setDisplayListProperties(displayList);
12218 }
12219 }
12220 } else if (!isLayer) {
12221 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12222 mPrivateFlags &= ~DIRTY_MASK;
12223 }
12224
12225 return displayList;
12226 }
12227
12228 /**
12229 * Get the DisplayList for the HardwareLayer
12230 *
12231 * @param layer The HardwareLayer whose DisplayList we want
12232 * @return A DisplayList fopr the specified HardwareLayer
12233 */
12234 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
12235 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
12236 layer.setDisplayList(displayList);
12237 return displayList;
12238 }
12239
12240
12241 /**
Romain Guyb051e892010-09-28 19:09:36 -070012242 * <p>Returns a display list that can be used to draw this view again
12243 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012244 *
Romain Guyb051e892010-09-28 19:09:36 -070012245 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080012246 *
12247 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070012248 */
Chet Haasedaf98e92011-01-10 14:10:36 -080012249 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080012250 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070012251 return mDisplayList;
12252 }
12253
Romain Guy38c2ece2012-05-24 14:20:56 -070012254 private void clearDisplayList() {
12255 if (mDisplayList != null) {
12256 mDisplayList.invalidate();
12257 mDisplayList.clear();
12258 }
12259 }
12260
Romain Guyb051e892010-09-28 19:09:36 -070012261 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012262 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012263 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012264 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012265 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012266 * @see #getDrawingCache(boolean)
12267 */
12268 public Bitmap getDrawingCache() {
12269 return getDrawingCache(false);
12270 }
12271
12272 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012273 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
12274 * is null when caching is disabled. If caching is enabled and the cache is not ready,
12275 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
12276 * draw from the cache when the cache is enabled. To benefit from the cache, you must
12277 * request the drawing cache by calling this method and draw it on screen if the
12278 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012279 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012280 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12281 * this method will create a bitmap of the same size as this view. Because this bitmap
12282 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12283 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12284 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12285 * size than the view. This implies that your application must be able to handle this
12286 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012287 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012288 * @param autoScale Indicates whether the generated bitmap should be scaled based on
12289 * the current density of the screen when the application is in compatibility
12290 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012291 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012292 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012293 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012294 * @see #setDrawingCacheEnabled(boolean)
12295 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070012296 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012297 * @see #destroyDrawingCache()
12298 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012299 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012300 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
12301 return null;
12302 }
12303 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012304 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012305 }
Romain Guy02890fd2010-08-06 17:58:44 -070012306 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012307 }
12308
12309 /**
12310 * <p>Frees the resources used by the drawing cache. If you call
12311 * {@link #buildDrawingCache()} manually without calling
12312 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12313 * should cleanup the cache with this method afterwards.</p>
12314 *
12315 * @see #setDrawingCacheEnabled(boolean)
12316 * @see #buildDrawingCache()
12317 * @see #getDrawingCache()
12318 */
12319 public void destroyDrawingCache() {
12320 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012321 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012322 mDrawingCache = null;
12323 }
Romain Guyfbd8f692009-06-26 14:51:58 -070012324 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012325 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070012326 mUnscaledDrawingCache = null;
12327 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012328 }
12329
12330 /**
12331 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070012332 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012333 * view will always be drawn on top of a solid color.
12334 *
12335 * @param color The background color to use for the drawing cache's bitmap
12336 *
12337 * @see #setDrawingCacheEnabled(boolean)
12338 * @see #buildDrawingCache()
12339 * @see #getDrawingCache()
12340 */
12341 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080012342 if (color != mDrawingCacheBackgroundColor) {
12343 mDrawingCacheBackgroundColor = color;
12344 mPrivateFlags &= ~DRAWING_CACHE_VALID;
12345 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012346 }
12347
12348 /**
12349 * @see #setDrawingCacheBackgroundColor(int)
12350 *
12351 * @return The background color to used for the drawing cache's bitmap
12352 */
12353 public int getDrawingCacheBackgroundColor() {
12354 return mDrawingCacheBackgroundColor;
12355 }
12356
12357 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012358 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012359 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012360 * @see #buildDrawingCache(boolean)
12361 */
12362 public void buildDrawingCache() {
12363 buildDrawingCache(false);
12364 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080012365
Romain Guyfbd8f692009-06-26 14:51:58 -070012366 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012367 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
12368 *
12369 * <p>If you call {@link #buildDrawingCache()} manually without calling
12370 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12371 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012372 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012373 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12374 * this method will create a bitmap of the same size as this view. Because this bitmap
12375 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12376 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12377 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12378 * size than the view. This implies that your application must be able to handle this
12379 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012380 *
Romain Guy0d9275e2010-10-26 14:22:30 -070012381 * <p>You should avoid calling this method when hardware acceleration is enabled. If
12382 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080012383 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070012384 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012385 *
12386 * @see #getDrawingCache()
12387 * @see #destroyDrawingCache()
12388 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012389 public void buildDrawingCache(boolean autoScale) {
12390 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070012391 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012392 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012393
Romain Guy8506ab42009-06-11 17:35:47 -070012394 int width = mRight - mLeft;
12395 int height = mBottom - mTop;
12396
12397 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070012398 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070012399
Romain Guye1123222009-06-29 14:24:56 -070012400 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012401 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
12402 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070012403 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012404
12405 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070012406 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080012407 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012408
12409 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070012410 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080012411 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012412 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
12413 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080012414 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012415 return;
12416 }
12417
12418 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070012419 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012420
12421 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012422 Bitmap.Config quality;
12423 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080012424 // Never pick ARGB_4444 because it looks awful
12425 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012426 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
12427 case DRAWING_CACHE_QUALITY_AUTO:
12428 quality = Bitmap.Config.ARGB_8888;
12429 break;
12430 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080012431 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012432 break;
12433 case DRAWING_CACHE_QUALITY_HIGH:
12434 quality = Bitmap.Config.ARGB_8888;
12435 break;
12436 default:
12437 quality = Bitmap.Config.ARGB_8888;
12438 break;
12439 }
12440 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070012441 // Optimization for translucent windows
12442 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080012443 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012444 }
12445
12446 // Try to cleanup memory
12447 if (bitmap != null) bitmap.recycle();
12448
12449 try {
12450 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070012451 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070012452 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070012453 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012454 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070012455 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012456 }
Adam Powell26153a32010-11-08 15:22:27 -080012457 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012458 } catch (OutOfMemoryError e) {
12459 // If there is not enough memory to create the bitmap cache, just
12460 // ignore the issue as bitmap caches are not required to draw the
12461 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012462 if (autoScale) {
12463 mDrawingCache = null;
12464 } else {
12465 mUnscaledDrawingCache = null;
12466 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012467 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012468 return;
12469 }
12470
12471 clear = drawingCacheBackgroundColor != 0;
12472 }
12473
12474 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012475 if (attachInfo != null) {
12476 canvas = attachInfo.mCanvas;
12477 if (canvas == null) {
12478 canvas = new Canvas();
12479 }
12480 canvas.setBitmap(bitmap);
12481 // Temporarily clobber the cached Canvas in case one of our children
12482 // is also using a drawing cache. Without this, the children would
12483 // steal the canvas by attaching their own bitmap to it and bad, bad
12484 // thing would happen (invisible views, corrupted drawings, etc.)
12485 attachInfo.mCanvas = null;
12486 } else {
12487 // This case should hopefully never or seldom happen
12488 canvas = new Canvas(bitmap);
12489 }
12490
12491 if (clear) {
12492 bitmap.eraseColor(drawingCacheBackgroundColor);
12493 }
12494
12495 computeScroll();
12496 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080012497
Romain Guye1123222009-06-29 14:24:56 -070012498 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012499 final float scale = attachInfo.mApplicationScale;
12500 canvas.scale(scale, scale);
12501 }
Joe Malin32736f02011-01-19 16:14:20 -080012502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012503 canvas.translate(-mScrollX, -mScrollY);
12504
Romain Guy5bcdff42009-05-14 21:27:18 -070012505 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080012506 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
12507 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070012508 mPrivateFlags |= DRAWING_CACHE_VALID;
12509 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012510
12511 // Fast path for layouts with no backgrounds
12512 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Romain Guy5bcdff42009-05-14 21:27:18 -070012513 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012514 dispatchDraw(canvas);
12515 } else {
12516 draw(canvas);
12517 }
12518
12519 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012520 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012521
12522 if (attachInfo != null) {
12523 // Restore the cached Canvas for our siblings
12524 attachInfo.mCanvas = canvas;
12525 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012526 }
12527 }
12528
12529 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012530 * Create a snapshot of the view into a bitmap. We should probably make
12531 * some form of this public, but should think about the API.
12532 */
Romain Guy223ff5c2010-03-02 17:07:47 -080012533 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012534 int width = mRight - mLeft;
12535 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012536
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012537 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070012538 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012539 width = (int) ((width * scale) + 0.5f);
12540 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080012541
Romain Guy8c11e312009-09-14 15:15:30 -070012542 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012543 if (bitmap == null) {
12544 throw new OutOfMemoryError();
12545 }
12546
Romain Guyc529d8d2011-09-06 15:01:39 -070012547 Resources resources = getResources();
12548 if (resources != null) {
12549 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
12550 }
Joe Malin32736f02011-01-19 16:14:20 -080012551
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012552 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012553 if (attachInfo != null) {
12554 canvas = attachInfo.mCanvas;
12555 if (canvas == null) {
12556 canvas = new Canvas();
12557 }
12558 canvas.setBitmap(bitmap);
12559 // Temporarily clobber the cached Canvas in case one of our children
12560 // is also using a drawing cache. Without this, the children would
12561 // steal the canvas by attaching their own bitmap to it and bad, bad
12562 // things would happen (invisible views, corrupted drawings, etc.)
12563 attachInfo.mCanvas = null;
12564 } else {
12565 // This case should hopefully never or seldom happen
12566 canvas = new Canvas(bitmap);
12567 }
12568
Romain Guy5bcdff42009-05-14 21:27:18 -070012569 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012570 bitmap.eraseColor(backgroundColor);
12571 }
12572
12573 computeScroll();
12574 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012575 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012576 canvas.translate(-mScrollX, -mScrollY);
12577
Romain Guy5bcdff42009-05-14 21:27:18 -070012578 // Temporarily remove the dirty mask
12579 int flags = mPrivateFlags;
12580 mPrivateFlags &= ~DIRTY_MASK;
12581
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012582 // Fast path for layouts with no backgrounds
12583 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12584 dispatchDraw(canvas);
12585 } else {
12586 draw(canvas);
12587 }
12588
Romain Guy5bcdff42009-05-14 21:27:18 -070012589 mPrivateFlags = flags;
12590
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012591 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012592 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012593
12594 if (attachInfo != null) {
12595 // Restore the cached Canvas for our siblings
12596 attachInfo.mCanvas = canvas;
12597 }
Romain Guy8506ab42009-06-11 17:35:47 -070012598
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012599 return bitmap;
12600 }
12601
12602 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012603 * Indicates whether this View is currently in edit mode. A View is usually
12604 * in edit mode when displayed within a developer tool. For instance, if
12605 * this View is being drawn by a visual user interface builder, this method
12606 * should return true.
12607 *
12608 * Subclasses should check the return value of this method to provide
12609 * different behaviors if their normal behavior might interfere with the
12610 * host environment. For instance: the class spawns a thread in its
12611 * constructor, the drawing code relies on device-specific features, etc.
12612 *
12613 * This method is usually checked in the drawing code of custom widgets.
12614 *
12615 * @return True if this View is in edit mode, false otherwise.
12616 */
12617 public boolean isInEditMode() {
12618 return false;
12619 }
12620
12621 /**
12622 * If the View draws content inside its padding and enables fading edges,
12623 * it needs to support padding offsets. Padding offsets are added to the
12624 * fading edges to extend the length of the fade so that it covers pixels
12625 * drawn inside the padding.
12626 *
12627 * Subclasses of this class should override this method if they need
12628 * to draw content inside the padding.
12629 *
12630 * @return True if padding offset must be applied, false otherwise.
12631 *
12632 * @see #getLeftPaddingOffset()
12633 * @see #getRightPaddingOffset()
12634 * @see #getTopPaddingOffset()
12635 * @see #getBottomPaddingOffset()
12636 *
12637 * @since CURRENT
12638 */
12639 protected boolean isPaddingOffsetRequired() {
12640 return false;
12641 }
12642
12643 /**
12644 * Amount by which to extend the left fading region. Called only when
12645 * {@link #isPaddingOffsetRequired()} returns true.
12646 *
12647 * @return The left padding offset in pixels.
12648 *
12649 * @see #isPaddingOffsetRequired()
12650 *
12651 * @since CURRENT
12652 */
12653 protected int getLeftPaddingOffset() {
12654 return 0;
12655 }
12656
12657 /**
12658 * Amount by which to extend the right fading region. Called only when
12659 * {@link #isPaddingOffsetRequired()} returns true.
12660 *
12661 * @return The right padding offset in pixels.
12662 *
12663 * @see #isPaddingOffsetRequired()
12664 *
12665 * @since CURRENT
12666 */
12667 protected int getRightPaddingOffset() {
12668 return 0;
12669 }
12670
12671 /**
12672 * Amount by which to extend the top fading region. Called only when
12673 * {@link #isPaddingOffsetRequired()} returns true.
12674 *
12675 * @return The top padding offset in pixels.
12676 *
12677 * @see #isPaddingOffsetRequired()
12678 *
12679 * @since CURRENT
12680 */
12681 protected int getTopPaddingOffset() {
12682 return 0;
12683 }
12684
12685 /**
12686 * Amount by which to extend the bottom fading region. Called only when
12687 * {@link #isPaddingOffsetRequired()} returns true.
12688 *
12689 * @return The bottom padding offset in pixels.
12690 *
12691 * @see #isPaddingOffsetRequired()
12692 *
12693 * @since CURRENT
12694 */
12695 protected int getBottomPaddingOffset() {
12696 return 0;
12697 }
12698
12699 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070012700 * @hide
12701 * @param offsetRequired
12702 */
12703 protected int getFadeTop(boolean offsetRequired) {
12704 int top = mPaddingTop;
12705 if (offsetRequired) top += getTopPaddingOffset();
12706 return top;
12707 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012708
Romain Guyf2fc4602011-07-19 15:20:03 -070012709 /**
12710 * @hide
12711 * @param offsetRequired
12712 */
12713 protected int getFadeHeight(boolean offsetRequired) {
12714 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070012715 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070012716 return mBottom - mTop - mPaddingBottom - padding;
12717 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012718
Romain Guyf2fc4602011-07-19 15:20:03 -070012719 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012720 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070012721 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012722 *
Romain Guy2bffd262010-09-12 17:40:02 -070012723 * <p>Even if this method returns true, it does not mean that every call
12724 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
12725 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012726 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070012727 * window is hardware accelerated,
12728 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
12729 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012730 *
Romain Guy2bffd262010-09-12 17:40:02 -070012731 * @return True if the view is attached to a window and the window is
12732 * hardware accelerated; false in any other case.
12733 */
12734 public boolean isHardwareAccelerated() {
12735 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
12736 }
Joe Malin32736f02011-01-19 16:14:20 -080012737
Romain Guy2bffd262010-09-12 17:40:02 -070012738 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080012739 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
12740 * case of an active Animation being run on the view.
12741 */
12742 private boolean drawAnimation(ViewGroup parent, long drawingTime,
12743 Animation a, boolean scalingRequired) {
12744 Transformation invalidationTransform;
12745 final int flags = parent.mGroupFlags;
12746 final boolean initialized = a.isInitialized();
12747 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070012748 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080012749 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
Romain Guy393a52c2012-05-22 20:21:08 -070012750 if (mAttachInfo != null) a.setListenerHandler(mAttachInfo.mHandler);
Chet Haasebcca79a2012-02-14 08:45:14 -080012751 onAnimationStart();
12752 }
12753
12754 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
12755 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
12756 if (parent.mInvalidationTransformation == null) {
12757 parent.mInvalidationTransformation = new Transformation();
12758 }
12759 invalidationTransform = parent.mInvalidationTransformation;
12760 a.getTransformation(drawingTime, invalidationTransform, 1f);
12761 } else {
12762 invalidationTransform = parent.mChildTransformation;
12763 }
Romain Guy393a52c2012-05-22 20:21:08 -070012764
Chet Haasebcca79a2012-02-14 08:45:14 -080012765 if (more) {
12766 if (!a.willChangeBounds()) {
12767 if ((flags & (parent.FLAG_OPTIMIZE_INVALIDATE | parent.FLAG_ANIMATION_DONE)) ==
12768 parent.FLAG_OPTIMIZE_INVALIDATE) {
12769 parent.mGroupFlags |= parent.FLAG_INVALIDATE_REQUIRED;
12770 } else if ((flags & parent.FLAG_INVALIDATE_REQUIRED) == 0) {
12771 // The child need to draw an animation, potentially offscreen, so
12772 // make sure we do not cancel invalidate requests
12773 parent.mPrivateFlags |= DRAW_ANIMATION;
12774 parent.invalidate(mLeft, mTop, mRight, mBottom);
12775 }
12776 } else {
12777 if (parent.mInvalidateRegion == null) {
12778 parent.mInvalidateRegion = new RectF();
12779 }
12780 final RectF region = parent.mInvalidateRegion;
12781 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
12782 invalidationTransform);
12783
12784 // The child need to draw an animation, potentially offscreen, so
12785 // make sure we do not cancel invalidate requests
12786 parent.mPrivateFlags |= DRAW_ANIMATION;
12787
12788 final int left = mLeft + (int) region.left;
12789 final int top = mTop + (int) region.top;
12790 parent.invalidate(left, top, left + (int) (region.width() + .5f),
12791 top + (int) (region.height() + .5f));
12792 }
12793 }
12794 return more;
12795 }
12796
Chet Haasea1cff502012-02-21 13:43:44 -080012797 /**
12798 * This method is called by getDisplayList() when a display list is created or re-rendered.
12799 * It sets or resets the current value of all properties on that display list (resetting is
12800 * necessary when a display list is being re-created, because we need to make sure that
12801 * previously-set transform values
12802 */
12803 void setDisplayListProperties(DisplayList displayList) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012804 if (displayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012805 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070012806 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080012807 if (mParent instanceof ViewGroup) {
12808 displayList.setClipChildren(
12809 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
12810 }
Chet Haase9420abd2012-03-29 16:28:32 -070012811 float alpha = 1;
12812 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
12813 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
12814 ViewGroup parentVG = (ViewGroup) mParent;
12815 final boolean hasTransform =
12816 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
12817 if (hasTransform) {
12818 Transformation transform = parentVG.mChildTransformation;
12819 final int transformType = parentVG.mChildTransformation.getTransformationType();
12820 if (transformType != Transformation.TYPE_IDENTITY) {
12821 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
12822 alpha = transform.getAlpha();
12823 }
12824 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
12825 displayList.setStaticMatrix(transform.getMatrix());
12826 }
12827 }
12828 }
Chet Haasea1cff502012-02-21 13:43:44 -080012829 }
12830 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070012831 alpha *= mTransformationInfo.mAlpha;
12832 if (alpha < 1) {
12833 final int multipliedAlpha = (int) (255 * alpha);
12834 if (onSetAlpha(multipliedAlpha)) {
12835 alpha = 1;
12836 }
12837 }
12838 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080012839 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
12840 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
12841 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
12842 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070012843 if (mTransformationInfo.mCamera == null) {
12844 mTransformationInfo.mCamera = new Camera();
12845 mTransformationInfo.matrix3D = new Matrix();
12846 }
12847 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Chet Haasea1cff502012-02-21 13:43:44 -080012848 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == PIVOT_EXPLICITLY_SET) {
12849 displayList.setPivotX(getPivotX());
12850 displayList.setPivotY(getPivotY());
12851 }
Chet Haase9420abd2012-03-29 16:28:32 -070012852 } else if (alpha < 1) {
12853 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080012854 }
12855 }
12856 }
12857
Chet Haasebcca79a2012-02-14 08:45:14 -080012858 /**
Chet Haase64a48c12012-02-13 16:33:29 -080012859 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
12860 * This draw() method is an implementation detail and is not intended to be overridden or
12861 * to be called from anywhere else other than ViewGroup.drawChild().
12862 */
12863 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012864 boolean useDisplayListProperties = mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080012865 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080012866 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080012867 final int flags = parent.mGroupFlags;
12868
Chet Haasea1cff502012-02-21 13:43:44 -080012869 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080012870 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080012871 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012872 }
12873
12874 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080012875 boolean concatMatrix = false;
12876
12877 boolean scalingRequired = false;
12878 boolean caching;
12879 int layerType = parent.mDrawLayers ? getLayerType() : LAYER_TYPE_NONE;
12880
12881 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080012882 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
12883 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080012884 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070012885 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080012886 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
12887 } else {
12888 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
12889 }
12890
Chet Haasebcca79a2012-02-14 08:45:14 -080012891 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080012892 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012893 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080012894 concatMatrix = a.willChangeTransformationMatrix();
Chet Haaseafd5c3e2012-05-10 13:21:10 -070012895 if (concatMatrix) {
12896 mPrivateFlags2 |= VIEW_IS_ANIMATING_TRANSFORM;
12897 }
Chet Haasebcca79a2012-02-14 08:45:14 -080012898 transformToApply = parent.mChildTransformation;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070012899 } else {
12900 if ((mPrivateFlags2 & VIEW_IS_ANIMATING_TRANSFORM) == VIEW_IS_ANIMATING_TRANSFORM &&
12901 mDisplayList != null) {
12902 // No longer animating: clear out old animation matrix
12903 mDisplayList.setAnimationMatrix(null);
12904 mPrivateFlags2 &= ~VIEW_IS_ANIMATING_TRANSFORM;
12905 }
12906 if (!useDisplayListProperties &&
12907 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
12908 final boolean hasTransform =
12909 parent.getChildStaticTransformation(this, parent.mChildTransformation);
12910 if (hasTransform) {
12911 final int transformType = parent.mChildTransformation.getTransformationType();
12912 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
12913 parent.mChildTransformation : null;
12914 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
12915 }
Chet Haase64a48c12012-02-13 16:33:29 -080012916 }
12917 }
12918
12919 concatMatrix |= !childHasIdentityMatrix;
12920
12921 // Sets the flag as early as possible to allow draw() implementations
12922 // to call invalidate() successfully when doing animations
12923 mPrivateFlags |= DRAWN;
12924
Chet Haasebcca79a2012-02-14 08:45:14 -080012925 if (!concatMatrix && canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Chet Haase64a48c12012-02-13 16:33:29 -080012926 (mPrivateFlags & DRAW_ANIMATION) == 0) {
Chet Haase1a3ab172012-05-11 08:41:20 -070012927 mPrivateFlags2 |= VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080012928 return more;
12929 }
Chet Haase1a3ab172012-05-11 08:41:20 -070012930 mPrivateFlags2 &= ~VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080012931
12932 if (hardwareAccelerated) {
12933 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
12934 // retain the flag's value temporarily in the mRecreateDisplayList flag
12935 mRecreateDisplayList = (mPrivateFlags & INVALIDATED) == INVALIDATED;
12936 mPrivateFlags &= ~INVALIDATED;
12937 }
12938
12939 computeScroll();
12940
12941 final int sx = mScrollX;
12942 final int sy = mScrollY;
12943
12944 DisplayList displayList = null;
12945 Bitmap cache = null;
12946 boolean hasDisplayList = false;
12947 if (caching) {
12948 if (!hardwareAccelerated) {
12949 if (layerType != LAYER_TYPE_NONE) {
12950 layerType = LAYER_TYPE_SOFTWARE;
12951 buildDrawingCache(true);
12952 }
12953 cache = getDrawingCache(true);
12954 } else {
12955 switch (layerType) {
12956 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070012957 if (useDisplayListProperties) {
12958 hasDisplayList = canHaveDisplayList();
12959 } else {
12960 buildDrawingCache(true);
12961 cache = getDrawingCache(true);
12962 }
Chet Haase64a48c12012-02-13 16:33:29 -080012963 break;
Chet Haasea1cff502012-02-21 13:43:44 -080012964 case LAYER_TYPE_HARDWARE:
12965 if (useDisplayListProperties) {
12966 hasDisplayList = canHaveDisplayList();
12967 }
12968 break;
Chet Haase64a48c12012-02-13 16:33:29 -080012969 case LAYER_TYPE_NONE:
12970 // Delay getting the display list until animation-driven alpha values are
12971 // set up and possibly passed on to the view
12972 hasDisplayList = canHaveDisplayList();
12973 break;
12974 }
12975 }
12976 }
Chet Haasea1cff502012-02-21 13:43:44 -080012977 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070012978 if (useDisplayListProperties) {
12979 displayList = getDisplayList();
12980 if (!displayList.isValid()) {
12981 // Uncommon, but possible. If a view is removed from the hierarchy during the call
12982 // to getDisplayList(), the display list will be marked invalid and we should not
12983 // try to use it again.
12984 displayList = null;
12985 hasDisplayList = false;
12986 useDisplayListProperties = false;
12987 }
12988 }
Chet Haase64a48c12012-02-13 16:33:29 -080012989
12990 final boolean hasNoCache = cache == null || hasDisplayList;
12991 final boolean offsetForScroll = cache == null && !hasDisplayList &&
12992 layerType != LAYER_TYPE_HARDWARE;
12993
Chet Haasea1cff502012-02-21 13:43:44 -080012994 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070012995 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012996 restoreTo = canvas.save();
12997 }
Chet Haase64a48c12012-02-13 16:33:29 -080012998 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012999 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080013000 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080013001 if (!useDisplayListProperties) {
13002 canvas.translate(mLeft, mTop);
13003 }
Chet Haase64a48c12012-02-13 16:33:29 -080013004 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080013005 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070013006 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080013007 restoreTo = canvas.save();
13008 }
Chet Haase64a48c12012-02-13 16:33:29 -080013009 // mAttachInfo cannot be null, otherwise scalingRequired == false
13010 final float scale = 1.0f / mAttachInfo.mApplicationScale;
13011 canvas.scale(scale, scale);
13012 }
13013 }
13014
Chet Haasea1cff502012-02-21 13:43:44 -080013015 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070013016 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix()) {
Chet Haase64a48c12012-02-13 16:33:29 -080013017 if (transformToApply != null || !childHasIdentityMatrix) {
13018 int transX = 0;
13019 int transY = 0;
13020
13021 if (offsetForScroll) {
13022 transX = -sx;
13023 transY = -sy;
13024 }
13025
13026 if (transformToApply != null) {
13027 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070013028 if (useDisplayListProperties) {
13029 displayList.setAnimationMatrix(transformToApply.getMatrix());
13030 } else {
13031 // Undo the scroll translation, apply the transformation matrix,
13032 // then redo the scroll translate to get the correct result.
13033 canvas.translate(-transX, -transY);
13034 canvas.concat(transformToApply.getMatrix());
13035 canvas.translate(transX, transY);
13036 }
Chet Haasea1cff502012-02-21 13:43:44 -080013037 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013038 }
13039
13040 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070013041 if (transformAlpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080013042 alpha *= transformToApply.getAlpha();
Chet Haasea1cff502012-02-21 13:43:44 -080013043 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013044 }
13045 }
13046
Chet Haasea1cff502012-02-21 13:43:44 -080013047 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013048 canvas.translate(-transX, -transY);
13049 canvas.concat(getMatrix());
13050 canvas.translate(transX, transY);
13051 }
13052 }
13053
Chet Haase9420abd2012-03-29 16:28:32 -070013054 if (alpha < 1) {
Chet Haasea1cff502012-02-21 13:43:44 -080013055 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013056 if (hasNoCache) {
13057 final int multipliedAlpha = (int) (255 * alpha);
13058 if (!onSetAlpha(multipliedAlpha)) {
13059 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080013060 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080013061 layerType != LAYER_TYPE_NONE) {
13062 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
13063 }
Chet Haase9420abd2012-03-29 16:28:32 -070013064 if (useDisplayListProperties) {
13065 displayList.setAlpha(alpha * getAlpha());
13066 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070013067 final int scrollX = hasDisplayList ? 0 : sx;
13068 final int scrollY = hasDisplayList ? 0 : sy;
13069 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
13070 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080013071 }
13072 } else {
13073 // Alpha is handled by the child directly, clobber the layer's alpha
13074 mPrivateFlags |= ALPHA_SET;
13075 }
13076 }
13077 }
13078 } else if ((mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
13079 onSetAlpha(255);
13080 mPrivateFlags &= ~ALPHA_SET;
13081 }
13082
Chet Haasea1cff502012-02-21 13:43:44 -080013083 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
13084 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013085 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013086 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080013087 } else {
13088 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013089 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080013090 } else {
13091 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
13092 }
13093 }
13094 }
13095
Chet Haase9420abd2012-03-29 16:28:32 -070013096 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080013097 displayList = getDisplayList();
13098 if (!displayList.isValid()) {
13099 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13100 // to getDisplayList(), the display list will be marked invalid and we should not
13101 // try to use it again.
13102 displayList = null;
13103 hasDisplayList = false;
13104 }
13105 }
13106
13107 if (hasNoCache) {
13108 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080013109 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080013110 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080013111 if (layer != null && layer.isValid()) {
13112 mLayerPaint.setAlpha((int) (alpha * 255));
13113 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
13114 layerRendered = true;
13115 } else {
13116 final int scrollX = hasDisplayList ? 0 : sx;
13117 final int scrollY = hasDisplayList ? 0 : sy;
13118 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080013119 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080013120 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
13121 }
13122 }
13123
13124 if (!layerRendered) {
13125 if (!hasDisplayList) {
13126 // Fast path for layouts with no backgrounds
13127 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Chet Haase64a48c12012-02-13 16:33:29 -080013128 mPrivateFlags &= ~DIRTY_MASK;
13129 dispatchDraw(canvas);
13130 } else {
13131 draw(canvas);
13132 }
13133 } else {
13134 mPrivateFlags &= ~DIRTY_MASK;
Chet Haase1271e2c2012-04-20 09:54:27 -070013135 ((HardwareCanvas) canvas).drawDisplayList(displayList, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080013136 }
13137 }
13138 } else if (cache != null) {
13139 mPrivateFlags &= ~DIRTY_MASK;
13140 Paint cachePaint;
13141
13142 if (layerType == LAYER_TYPE_NONE) {
13143 cachePaint = parent.mCachePaint;
13144 if (cachePaint == null) {
13145 cachePaint = new Paint();
13146 cachePaint.setDither(false);
13147 parent.mCachePaint = cachePaint;
13148 }
Chet Haase9420abd2012-03-29 16:28:32 -070013149 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080013150 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080013151 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
13152 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013153 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080013154 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080013155 }
13156 } else {
13157 cachePaint = mLayerPaint;
13158 cachePaint.setAlpha((int) (alpha * 255));
13159 }
13160 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
13161 }
13162
Chet Haasea1cff502012-02-21 13:43:44 -080013163 if (restoreTo >= 0) {
13164 canvas.restoreToCount(restoreTo);
13165 }
Chet Haase64a48c12012-02-13 16:33:29 -080013166
13167 if (a != null && !more) {
13168 if (!hardwareAccelerated && !a.getFillAfter()) {
13169 onSetAlpha(255);
13170 }
13171 parent.finishAnimatingView(this, a);
13172 }
13173
13174 if (more && hardwareAccelerated) {
13175 // invalidation is the trigger to recreate display lists, so if we're using
13176 // display lists to render, force an invalidate to allow the animation to
13177 // continue drawing another frame
13178 parent.invalidate(true);
13179 if (a.hasAlpha() && (mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
13180 // alpha animations should cause the child to recreate its display list
13181 invalidate(true);
13182 }
13183 }
13184
13185 mRecreateDisplayList = false;
13186
13187 return more;
13188 }
13189
13190 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013191 * Manually render this view (and all of its children) to the given Canvas.
13192 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070013193 * called. When implementing a view, implement
13194 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
13195 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013196 *
13197 * @param canvas The Canvas to which the View is rendered.
13198 */
13199 public void draw(Canvas canvas) {
Romain Guy5bcdff42009-05-14 21:27:18 -070013200 final int privateFlags = mPrivateFlags;
13201 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
13202 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
13203 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070013204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013205 /*
13206 * Draw traversal performs several drawing steps which must be executed
13207 * in the appropriate order:
13208 *
13209 * 1. Draw the background
13210 * 2. If necessary, save the canvas' layers to prepare for fading
13211 * 3. Draw view's content
13212 * 4. Draw children
13213 * 5. If necessary, draw the fading edges and restore layers
13214 * 6. Draw decorations (scrollbars for instance)
13215 */
13216
13217 // Step 1, draw the background, if needed
13218 int saveCount;
13219
Romain Guy24443ea2009-05-11 11:56:30 -070013220 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013221 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070013222 if (background != null) {
13223 final int scrollX = mScrollX;
13224 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013225
Romain Guy24443ea2009-05-11 11:56:30 -070013226 if (mBackgroundSizeChanged) {
13227 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
13228 mBackgroundSizeChanged = false;
13229 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013230
Romain Guy24443ea2009-05-11 11:56:30 -070013231 if ((scrollX | scrollY) == 0) {
13232 background.draw(canvas);
13233 } else {
13234 canvas.translate(scrollX, scrollY);
13235 background.draw(canvas);
13236 canvas.translate(-scrollX, -scrollY);
13237 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013238 }
13239 }
13240
13241 // skip step 2 & 5 if possible (common case)
13242 final int viewFlags = mViewFlags;
13243 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
13244 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
13245 if (!verticalEdges && !horizontalEdges) {
13246 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013247 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013248
13249 // Step 4, draw the children
13250 dispatchDraw(canvas);
13251
13252 // Step 6, draw decorations (scrollbars)
13253 onDrawScrollBars(canvas);
13254
13255 // we're done...
13256 return;
13257 }
13258
13259 /*
13260 * Here we do the full fledged routine...
13261 * (this is an uncommon case where speed matters less,
13262 * this is why we repeat some of the tests that have been
13263 * done above)
13264 */
13265
13266 boolean drawTop = false;
13267 boolean drawBottom = false;
13268 boolean drawLeft = false;
13269 boolean drawRight = false;
13270
13271 float topFadeStrength = 0.0f;
13272 float bottomFadeStrength = 0.0f;
13273 float leftFadeStrength = 0.0f;
13274 float rightFadeStrength = 0.0f;
13275
13276 // Step 2, save the canvas' layers
13277 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013278
13279 final boolean offsetRequired = isPaddingOffsetRequired();
13280 if (offsetRequired) {
13281 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013282 }
13283
13284 int left = mScrollX + paddingLeft;
13285 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070013286 int top = mScrollY + getFadeTop(offsetRequired);
13287 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013288
13289 if (offsetRequired) {
13290 right += getRightPaddingOffset();
13291 bottom += getBottomPaddingOffset();
13292 }
13293
13294 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070013295 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013296 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013297
13298 // clip the fade length if top and bottom fades overlap
13299 // overlapping fades produce odd-looking artifacts
13300 if (verticalEdges && (top + length > bottom - length)) {
13301 length = (bottom - top) / 2;
13302 }
13303
13304 // also clip horizontal fades if necessary
13305 if (horizontalEdges && (left + length > right - length)) {
13306 length = (right - left) / 2;
13307 }
13308
13309 if (verticalEdges) {
13310 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013311 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013312 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013313 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013314 }
13315
13316 if (horizontalEdges) {
13317 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013318 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013319 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013320 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013321 }
13322
13323 saveCount = canvas.getSaveCount();
13324
13325 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070013326 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013327 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
13328
13329 if (drawTop) {
13330 canvas.saveLayer(left, top, right, top + length, null, flags);
13331 }
13332
13333 if (drawBottom) {
13334 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
13335 }
13336
13337 if (drawLeft) {
13338 canvas.saveLayer(left, top, left + length, bottom, null, flags);
13339 }
13340
13341 if (drawRight) {
13342 canvas.saveLayer(right - length, top, right, bottom, null, flags);
13343 }
13344 } else {
13345 scrollabilityCache.setFadeColor(solidColor);
13346 }
13347
13348 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013349 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013350
13351 // Step 4, draw the children
13352 dispatchDraw(canvas);
13353
13354 // Step 5, draw the fade effect and restore layers
13355 final Paint p = scrollabilityCache.paint;
13356 final Matrix matrix = scrollabilityCache.matrix;
13357 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013358
13359 if (drawTop) {
13360 matrix.setScale(1, fadeHeight * topFadeStrength);
13361 matrix.postTranslate(left, top);
13362 fade.setLocalMatrix(matrix);
13363 canvas.drawRect(left, top, right, top + length, p);
13364 }
13365
13366 if (drawBottom) {
13367 matrix.setScale(1, fadeHeight * bottomFadeStrength);
13368 matrix.postRotate(180);
13369 matrix.postTranslate(left, bottom);
13370 fade.setLocalMatrix(matrix);
13371 canvas.drawRect(left, bottom - length, right, bottom, p);
13372 }
13373
13374 if (drawLeft) {
13375 matrix.setScale(1, fadeHeight * leftFadeStrength);
13376 matrix.postRotate(-90);
13377 matrix.postTranslate(left, top);
13378 fade.setLocalMatrix(matrix);
13379 canvas.drawRect(left, top, left + length, bottom, p);
13380 }
13381
13382 if (drawRight) {
13383 matrix.setScale(1, fadeHeight * rightFadeStrength);
13384 matrix.postRotate(90);
13385 matrix.postTranslate(right, top);
13386 fade.setLocalMatrix(matrix);
13387 canvas.drawRect(right - length, top, right, bottom, p);
13388 }
13389
13390 canvas.restoreToCount(saveCount);
13391
13392 // Step 6, draw decorations (scrollbars)
13393 onDrawScrollBars(canvas);
13394 }
13395
13396 /**
13397 * Override this if your view is known to always be drawn on top of a solid color background,
13398 * and needs to draw fading edges. Returning a non-zero color enables the view system to
13399 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
13400 * should be set to 0xFF.
13401 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013402 * @see #setVerticalFadingEdgeEnabled(boolean)
13403 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013404 *
13405 * @return The known solid color background for this view, or 0 if the color may vary
13406 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013407 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013408 public int getSolidColor() {
13409 return 0;
13410 }
13411
13412 /**
13413 * Build a human readable string representation of the specified view flags.
13414 *
13415 * @param flags the view flags to convert to a string
13416 * @return a String representing the supplied flags
13417 */
13418 private static String printFlags(int flags) {
13419 String output = "";
13420 int numFlags = 0;
13421 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
13422 output += "TAKES_FOCUS";
13423 numFlags++;
13424 }
13425
13426 switch (flags & VISIBILITY_MASK) {
13427 case INVISIBLE:
13428 if (numFlags > 0) {
13429 output += " ";
13430 }
13431 output += "INVISIBLE";
13432 // USELESS HERE numFlags++;
13433 break;
13434 case GONE:
13435 if (numFlags > 0) {
13436 output += " ";
13437 }
13438 output += "GONE";
13439 // USELESS HERE numFlags++;
13440 break;
13441 default:
13442 break;
13443 }
13444 return output;
13445 }
13446
13447 /**
13448 * Build a human readable string representation of the specified private
13449 * view flags.
13450 *
13451 * @param privateFlags the private view flags to convert to a string
13452 * @return a String representing the supplied flags
13453 */
13454 private static String printPrivateFlags(int privateFlags) {
13455 String output = "";
13456 int numFlags = 0;
13457
13458 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
13459 output += "WANTS_FOCUS";
13460 numFlags++;
13461 }
13462
13463 if ((privateFlags & FOCUSED) == FOCUSED) {
13464 if (numFlags > 0) {
13465 output += " ";
13466 }
13467 output += "FOCUSED";
13468 numFlags++;
13469 }
13470
13471 if ((privateFlags & SELECTED) == SELECTED) {
13472 if (numFlags > 0) {
13473 output += " ";
13474 }
13475 output += "SELECTED";
13476 numFlags++;
13477 }
13478
13479 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
13480 if (numFlags > 0) {
13481 output += " ";
13482 }
13483 output += "IS_ROOT_NAMESPACE";
13484 numFlags++;
13485 }
13486
13487 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
13488 if (numFlags > 0) {
13489 output += " ";
13490 }
13491 output += "HAS_BOUNDS";
13492 numFlags++;
13493 }
13494
13495 if ((privateFlags & DRAWN) == DRAWN) {
13496 if (numFlags > 0) {
13497 output += " ";
13498 }
13499 output += "DRAWN";
13500 // USELESS HERE numFlags++;
13501 }
13502 return output;
13503 }
13504
13505 /**
13506 * <p>Indicates whether or not this view's layout will be requested during
13507 * the next hierarchy layout pass.</p>
13508 *
13509 * @return true if the layout will be forced during next layout pass
13510 */
13511 public boolean isLayoutRequested() {
13512 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
13513 }
13514
13515 /**
13516 * Assign a size and position to a view and all of its
13517 * descendants
13518 *
13519 * <p>This is the second phase of the layout mechanism.
13520 * (The first is measuring). In this phase, each parent calls
13521 * layout on all of its children to position them.
13522 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080013523 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013524 *
Chet Haase9c087442011-01-12 16:20:16 -080013525 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013526 * Derived classes with children should override
13527 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080013528 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013529 *
13530 * @param l Left position, relative to parent
13531 * @param t Top position, relative to parent
13532 * @param r Right position, relative to parent
13533 * @param b Bottom position, relative to parent
13534 */
Romain Guy5429e1d2010-09-07 12:38:00 -070013535 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080013536 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070013537 int oldL = mLeft;
13538 int oldT = mTop;
13539 int oldB = mBottom;
13540 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013541 boolean changed = setFrame(l, t, r, b);
13542 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013543 onLayout(changed, l, t, r, b);
13544 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070013545
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013546 ListenerInfo li = mListenerInfo;
13547 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070013548 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013549 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070013550 int numListeners = listenersCopy.size();
13551 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070013552 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070013553 }
13554 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013555 }
13556 mPrivateFlags &= ~FORCE_LAYOUT;
13557 }
13558
13559 /**
13560 * Called from layout when this view should
13561 * assign a size and position to each of its children.
13562 *
13563 * Derived classes with children should override
13564 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070013565 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013566 * @param changed This is a new size or position for this view
13567 * @param left Left position, relative to parent
13568 * @param top Top position, relative to parent
13569 * @param right Right position, relative to parent
13570 * @param bottom Bottom position, relative to parent
13571 */
13572 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
13573 }
13574
13575 /**
13576 * Assign a size and position to this view.
13577 *
13578 * This is called from layout.
13579 *
13580 * @param left Left position, relative to parent
13581 * @param top Top position, relative to parent
13582 * @param right Right position, relative to parent
13583 * @param bottom Bottom position, relative to parent
13584 * @return true if the new size and position are different than the
13585 * previous ones
13586 * {@hide}
13587 */
13588 protected boolean setFrame(int left, int top, int right, int bottom) {
13589 boolean changed = false;
13590
13591 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070013592 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013593 + right + "," + bottom + ")");
13594 }
13595
13596 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
13597 changed = true;
13598
13599 // Remember our drawn bit
13600 int drawn = mPrivateFlags & DRAWN;
13601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013602 int oldWidth = mRight - mLeft;
13603 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070013604 int newWidth = right - left;
13605 int newHeight = bottom - top;
13606 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
13607
13608 // Invalidate our old position
13609 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013610
13611 mLeft = left;
13612 mTop = top;
13613 mRight = right;
13614 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -070013615 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013616 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
13617 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013618
13619 mPrivateFlags |= HAS_BOUNDS;
13620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013621
Chet Haase75755e22011-07-18 17:48:25 -070013622 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013623 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
13624 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013625 if (mTransformationInfo != null) {
13626 mTransformationInfo.mMatrixDirty = true;
13627 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013628 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013629 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
13630 }
13631
13632 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
13633 // If we are visible, force the DRAWN bit to on so that
13634 // this invalidate will go through (at least to our parent).
13635 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013636 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013637 // the DRAWN bit.
13638 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070013639 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080013640 // parent display list may need to be recreated based on a change in the bounds
13641 // of any child
13642 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013643 }
13644
13645 // Reset drawn bit to original value (invalidate turns it off)
13646 mPrivateFlags |= drawn;
13647
13648 mBackgroundSizeChanged = true;
13649 }
13650 return changed;
13651 }
13652
13653 /**
13654 * Finalize inflating a view from XML. This is called as the last phase
13655 * of inflation, after all child views have been added.
13656 *
13657 * <p>Even if the subclass overrides onFinishInflate, they should always be
13658 * sure to call the super method, so that we get called.
13659 */
13660 protected void onFinishInflate() {
13661 }
13662
13663 /**
13664 * Returns the resources associated with this view.
13665 *
13666 * @return Resources object.
13667 */
13668 public Resources getResources() {
13669 return mResources;
13670 }
13671
13672 /**
13673 * Invalidates the specified Drawable.
13674 *
13675 * @param drawable the drawable to invalidate
13676 */
13677 public void invalidateDrawable(Drawable drawable) {
13678 if (verifyDrawable(drawable)) {
13679 final Rect dirty = drawable.getBounds();
13680 final int scrollX = mScrollX;
13681 final int scrollY = mScrollY;
13682
13683 invalidate(dirty.left + scrollX, dirty.top + scrollY,
13684 dirty.right + scrollX, dirty.bottom + scrollY);
13685 }
13686 }
13687
13688 /**
13689 * Schedules an action on a drawable to occur at a specified time.
13690 *
13691 * @param who the recipient of the action
13692 * @param what the action to run on the drawable
13693 * @param when the time at which the action must occur. Uses the
13694 * {@link SystemClock#uptimeMillis} timebase.
13695 */
13696 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080013697 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013698 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080013699 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013700 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
13701 Choreographer.CALLBACK_ANIMATION, what, who,
13702 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080013703 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013704 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080013705 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013706 }
13707 }
13708
13709 /**
13710 * Cancels a scheduled action on a drawable.
13711 *
13712 * @param who the recipient of the action
13713 * @param what the action to cancel
13714 */
13715 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080013716 if (verifyDrawable(who) && what != null) {
13717 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013718 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13719 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080013720 } else {
13721 ViewRootImpl.getRunQueue().removeCallbacks(what);
13722 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013723 }
13724 }
13725
13726 /**
13727 * Unschedule any events associated with the given Drawable. This can be
13728 * used when selecting a new Drawable into a view, so that the previous
13729 * one is completely unscheduled.
13730 *
13731 * @param who The Drawable to unschedule.
13732 *
13733 * @see #drawableStateChanged
13734 */
13735 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080013736 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013737 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13738 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013739 }
13740 }
13741
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013742 /**
13743 * Return the layout direction of a given Drawable.
13744 *
13745 * @param who the Drawable to query
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070013746 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013747 */
13748 public int getResolvedLayoutDirection(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013749 return (who == mBackground) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070013750 }
13751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013752 /**
13753 * If your view subclass is displaying its own Drawable objects, it should
13754 * override this function and return true for any Drawable it is
13755 * displaying. This allows animations for those drawables to be
13756 * scheduled.
13757 *
13758 * <p>Be sure to call through to the super class when overriding this
13759 * function.
13760 *
13761 * @param who The Drawable to verify. Return true if it is one you are
13762 * displaying, else return the result of calling through to the
13763 * super class.
13764 *
13765 * @return boolean If true than the Drawable is being displayed in the
13766 * view; else false and it is not allowed to animate.
13767 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013768 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
13769 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013770 */
13771 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013772 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013773 }
13774
13775 /**
13776 * This function is called whenever the state of the view changes in such
13777 * a way that it impacts the state of drawables being shown.
13778 *
13779 * <p>Be sure to call through to the superclass when overriding this
13780 * function.
13781 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013782 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013783 */
13784 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013785 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013786 if (d != null && d.isStateful()) {
13787 d.setState(getDrawableState());
13788 }
13789 }
13790
13791 /**
13792 * Call this to force a view to update its drawable state. This will cause
13793 * drawableStateChanged to be called on this view. Views that are interested
13794 * in the new state should call getDrawableState.
13795 *
13796 * @see #drawableStateChanged
13797 * @see #getDrawableState
13798 */
13799 public void refreshDrawableState() {
13800 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
13801 drawableStateChanged();
13802
13803 ViewParent parent = mParent;
13804 if (parent != null) {
13805 parent.childDrawableStateChanged(this);
13806 }
13807 }
13808
13809 /**
13810 * Return an array of resource IDs of the drawable states representing the
13811 * current state of the view.
13812 *
13813 * @return The current drawable state
13814 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013815 * @see Drawable#setState(int[])
13816 * @see #drawableStateChanged()
13817 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013818 */
13819 public final int[] getDrawableState() {
13820 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
13821 return mDrawableState;
13822 } else {
13823 mDrawableState = onCreateDrawableState(0);
13824 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
13825 return mDrawableState;
13826 }
13827 }
13828
13829 /**
13830 * Generate the new {@link android.graphics.drawable.Drawable} state for
13831 * this view. This is called by the view
13832 * system when the cached Drawable state is determined to be invalid. To
13833 * retrieve the current state, you should use {@link #getDrawableState}.
13834 *
13835 * @param extraSpace if non-zero, this is the number of extra entries you
13836 * would like in the returned array in which you can place your own
13837 * states.
13838 *
13839 * @return Returns an array holding the current {@link Drawable} state of
13840 * the view.
13841 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013842 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013843 */
13844 protected int[] onCreateDrawableState(int extraSpace) {
13845 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
13846 mParent instanceof View) {
13847 return ((View) mParent).onCreateDrawableState(extraSpace);
13848 }
13849
13850 int[] drawableState;
13851
13852 int privateFlags = mPrivateFlags;
13853
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013854 int viewStateIndex = 0;
13855 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
13856 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
13857 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070013858 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013859 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
13860 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070013861 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
13862 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080013863 // This is set if HW acceleration is requested, even if the current
13864 // process doesn't allow it. This is just to allow app preview
13865 // windows to better match their app.
13866 viewStateIndex |= VIEW_STATE_ACCELERATED;
13867 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070013868 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013869
Christopher Tate3d4bf172011-03-28 16:16:46 -070013870 final int privateFlags2 = mPrivateFlags2;
13871 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
13872 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
13873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013874 drawableState = VIEW_STATE_SETS[viewStateIndex];
13875
13876 //noinspection ConstantIfStatement
13877 if (false) {
13878 Log.i("View", "drawableStateIndex=" + viewStateIndex);
13879 Log.i("View", toString()
13880 + " pressed=" + ((privateFlags & PRESSED) != 0)
13881 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
13882 + " fo=" + hasFocus()
13883 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013884 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013885 + ": " + Arrays.toString(drawableState));
13886 }
13887
13888 if (extraSpace == 0) {
13889 return drawableState;
13890 }
13891
13892 final int[] fullState;
13893 if (drawableState != null) {
13894 fullState = new int[drawableState.length + extraSpace];
13895 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
13896 } else {
13897 fullState = new int[extraSpace];
13898 }
13899
13900 return fullState;
13901 }
13902
13903 /**
13904 * Merge your own state values in <var>additionalState</var> into the base
13905 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070013906 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013907 *
13908 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070013909 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013910 * own additional state values.
13911 *
13912 * @param additionalState The additional state values you would like
13913 * added to <var>baseState</var>; this array is not modified.
13914 *
13915 * @return As a convenience, the <var>baseState</var> array you originally
13916 * passed into the function is returned.
13917 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013918 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013919 */
13920 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
13921 final int N = baseState.length;
13922 int i = N - 1;
13923 while (i >= 0 && baseState[i] == 0) {
13924 i--;
13925 }
13926 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
13927 return baseState;
13928 }
13929
13930 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070013931 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
13932 * on all Drawable objects associated with this view.
13933 */
13934 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013935 if (mBackground != null) {
13936 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070013937 }
13938 }
13939
13940 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013941 * Sets the background color for this view.
13942 * @param color the color of the background
13943 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000013944 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013945 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013946 if (mBackground instanceof ColorDrawable) {
13947 ((ColorDrawable) mBackground).setColor(color);
Chet Haase70d4ba12010-10-06 09:46:45 -070013948 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070013949 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070013950 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013951 }
13952
13953 /**
13954 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070013955 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013956 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070013957 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013958 * @attr ref android.R.styleable#View_background
13959 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000013960 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013961 public void setBackgroundResource(int resid) {
13962 if (resid != 0 && resid == mBackgroundResource) {
13963 return;
13964 }
13965
13966 Drawable d= null;
13967 if (resid != 0) {
13968 d = mResources.getDrawable(resid);
13969 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013970 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013971
13972 mBackgroundResource = resid;
13973 }
13974
13975 /**
13976 * Set the background to a given Drawable, or remove the background. If the
13977 * background has padding, this View's padding is set to the background's
13978 * padding. However, when a background is removed, this View's padding isn't
13979 * touched. If setting the padding is desired, please use
13980 * {@link #setPadding(int, int, int, int)}.
13981 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013982 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013983 * background
13984 */
Philip Milne6c8ea062012-04-03 17:38:43 -070013985 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070013986 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070013987 setBackgroundDrawable(background);
13988 }
13989
13990 /**
13991 * @deprecated use {@link #setBackground(Drawable)} instead
13992 */
13993 @Deprecated
13994 public void setBackgroundDrawable(Drawable background) {
13995 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070013996 return;
13997 }
13998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013999 boolean requestLayout = false;
14000
14001 mBackgroundResource = 0;
14002
14003 /*
14004 * Regardless of whether we're setting a new background or not, we want
14005 * to clear the previous drawable.
14006 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014007 if (mBackground != null) {
14008 mBackground.setCallback(null);
14009 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014010 }
14011
Philip Milne6c8ea062012-04-03 17:38:43 -070014012 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014013 Rect padding = sThreadLocal.get();
14014 if (padding == null) {
14015 padding = new Rect();
14016 sThreadLocal.set(padding);
14017 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014018 if (background.getPadding(padding)) {
14019 switch (background.getResolvedLayoutDirectionSelf()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014020 case LAYOUT_DIRECTION_RTL:
14021 setPadding(padding.right, padding.top, padding.left, padding.bottom);
14022 break;
14023 case LAYOUT_DIRECTION_LTR:
14024 default:
14025 setPadding(padding.left, padding.top, padding.right, padding.bottom);
14026 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014027 }
14028
14029 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
14030 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070014031 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
14032 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014033 requestLayout = true;
14034 }
14035
Philip Milne6c8ea062012-04-03 17:38:43 -070014036 background.setCallback(this);
14037 if (background.isStateful()) {
14038 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014039 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014040 background.setVisible(getVisibility() == VISIBLE, false);
14041 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014042
14043 if ((mPrivateFlags & SKIP_DRAW) != 0) {
14044 mPrivateFlags &= ~SKIP_DRAW;
14045 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
14046 requestLayout = true;
14047 }
14048 } else {
14049 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070014050 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014051
14052 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
14053 /*
14054 * This view ONLY drew the background before and we're removing
14055 * the background, so now it won't draw anything
14056 * (hence we SKIP_DRAW)
14057 */
14058 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
14059 mPrivateFlags |= SKIP_DRAW;
14060 }
14061
14062 /*
14063 * When the background is set, we try to apply its padding to this
14064 * View. When the background is removed, we don't touch this View's
14065 * padding. This is noted in the Javadocs. Hence, we don't need to
14066 * requestLayout(), the invalidate() below is sufficient.
14067 */
14068
14069 // The old background's minimum size could have affected this
14070 // View's layout, so let's requestLayout
14071 requestLayout = true;
14072 }
14073
Romain Guy8f1344f52009-05-15 16:03:59 -070014074 computeOpaqueFlags();
14075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014076 if (requestLayout) {
14077 requestLayout();
14078 }
14079
14080 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014081 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014082 }
14083
14084 /**
14085 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070014086 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014087 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070014088 *
14089 * @see #setBackground(Drawable)
14090 *
14091 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014092 */
14093 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014094 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014095 }
14096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014097 /**
14098 * Sets the padding. The view may add on the space required to display
14099 * the scrollbars, depending on the style and visibility of the scrollbars.
14100 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
14101 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
14102 * from the values set in this call.
14103 *
14104 * @attr ref android.R.styleable#View_padding
14105 * @attr ref android.R.styleable#View_paddingBottom
14106 * @attr ref android.R.styleable#View_paddingLeft
14107 * @attr ref android.R.styleable#View_paddingRight
14108 * @attr ref android.R.styleable#View_paddingTop
14109 * @param left the left padding in pixels
14110 * @param top the top padding in pixels
14111 * @param right the right padding in pixels
14112 * @param bottom the bottom padding in pixels
14113 */
14114 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014115 mUserPaddingStart = -1;
14116 mUserPaddingEnd = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014117 mUserPaddingRelative = false;
14118
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014119 internalSetPadding(left, top, right, bottom);
14120 }
14121
14122 private void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080014123 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014124 mUserPaddingRight = right;
14125 mUserPaddingBottom = bottom;
14126
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014127 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014128 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070014129
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014130 // Common case is there are no scroll bars.
14131 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014132 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080014133 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014134 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080014135 switch (mVerticalScrollbarPosition) {
14136 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014137 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
14138 left += offset;
14139 } else {
14140 right += offset;
14141 }
14142 break;
Adam Powell20232d02010-12-08 21:08:53 -080014143 case SCROLLBAR_POSITION_RIGHT:
14144 right += offset;
14145 break;
14146 case SCROLLBAR_POSITION_LEFT:
14147 left += offset;
14148 break;
14149 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014150 }
Adam Powell20232d02010-12-08 21:08:53 -080014151 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014152 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
14153 ? 0 : getHorizontalScrollbarHeight();
14154 }
14155 }
Romain Guy8506ab42009-06-11 17:35:47 -070014156
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014157 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014158 changed = true;
14159 mPaddingLeft = left;
14160 }
14161 if (mPaddingTop != top) {
14162 changed = true;
14163 mPaddingTop = top;
14164 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014165 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014166 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014167 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014168 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014169 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014170 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014171 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014172 }
14173
14174 if (changed) {
14175 requestLayout();
14176 }
14177 }
14178
14179 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014180 * Sets the relative padding. The view may add on the space required to display
14181 * the scrollbars, depending on the style and visibility of the scrollbars.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014182 * from the values set in this call.
14183 *
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014184 * @param start the start padding in pixels
14185 * @param top the top padding in pixels
14186 * @param end the end padding in pixels
14187 * @param bottom the bottom padding in pixels
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014188 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014189 */
14190 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014191 mUserPaddingStart = start;
14192 mUserPaddingEnd = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014193 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014194
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014195 switch(getResolvedLayoutDirection()) {
14196 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014197 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014198 break;
14199 case LAYOUT_DIRECTION_LTR:
14200 default:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014201 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014202 }
14203 }
14204
14205 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014206 * Returns the top padding of this view.
14207 *
14208 * @return the top padding in pixels
14209 */
14210 public int getPaddingTop() {
14211 return mPaddingTop;
14212 }
14213
14214 /**
14215 * Returns the bottom padding of this view. If there are inset and enabled
14216 * scrollbars, this value may include the space required to display the
14217 * scrollbars as well.
14218 *
14219 * @return the bottom padding in pixels
14220 */
14221 public int getPaddingBottom() {
14222 return mPaddingBottom;
14223 }
14224
14225 /**
14226 * Returns the left padding of this view. If there are inset and enabled
14227 * scrollbars, this value may include the space required to display the
14228 * scrollbars as well.
14229 *
14230 * @return the left padding in pixels
14231 */
14232 public int getPaddingLeft() {
14233 return mPaddingLeft;
14234 }
14235
14236 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014237 * Returns the start padding of this view depending on its resolved layout direction.
14238 * If there are inset and enabled scrollbars, this value may include the space
14239 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014240 *
14241 * @return the start padding in pixels
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014242 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014243 */
14244 public int getPaddingStart() {
14245 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14246 mPaddingRight : mPaddingLeft;
14247 }
14248
14249 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014250 * Returns the right padding of this view. If there are inset and enabled
14251 * scrollbars, this value may include the space required to display the
14252 * scrollbars as well.
14253 *
14254 * @return the right padding in pixels
14255 */
14256 public int getPaddingRight() {
14257 return mPaddingRight;
14258 }
14259
14260 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014261 * Returns the end padding of this view depending on its resolved layout direction.
14262 * If there are inset and enabled scrollbars, this value may include the space
14263 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014264 *
14265 * @return the end padding in pixels
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014266 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014267 */
14268 public int getPaddingEnd() {
14269 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14270 mPaddingLeft : mPaddingRight;
14271 }
14272
14273 /**
14274 * Return if the padding as been set thru relative values
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014275 * {@link #setPaddingRelative(int, int, int, int)}
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014276 *
14277 * @return true if the padding is relative or false if it is not.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014278 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014279 */
14280 public boolean isPaddingRelative() {
14281 return mUserPaddingRelative;
14282 }
14283
14284 /**
Philip Milne1557fd72012-04-04 23:41:34 -070014285 * @hide
14286 */
Philip Milne7a23b492012-04-24 22:12:36 -070014287 public Insets getOpticalInsets() {
Philip Milne1557fd72012-04-04 23:41:34 -070014288 if (mLayoutInsets == null) {
Philip Milnebbd51f12012-04-18 15:09:05 -070014289 mLayoutInsets = (mBackground == null) ? Insets.NONE : mBackground.getLayoutInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070014290 }
14291 return mLayoutInsets;
14292 }
14293
14294 /**
14295 * @hide
14296 */
14297 public void setLayoutInsets(Insets layoutInsets) {
14298 mLayoutInsets = layoutInsets;
14299 }
14300
14301 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014302 * Changes the selection state of this view. A view can be selected or not.
14303 * Note that selection is not the same as focus. Views are typically
14304 * selected in the context of an AdapterView like ListView or GridView;
14305 * the selected view is the view that is highlighted.
14306 *
14307 * @param selected true if the view must be selected, false otherwise
14308 */
14309 public void setSelected(boolean selected) {
14310 if (((mPrivateFlags & SELECTED) != 0) != selected) {
14311 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070014312 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080014313 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014314 refreshDrawableState();
14315 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070014316 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
14317 notifyAccessibilityStateChanged();
14318 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014319 }
14320 }
14321
14322 /**
14323 * Dispatch setSelected to all of this View's children.
14324 *
14325 * @see #setSelected(boolean)
14326 *
14327 * @param selected The new selected state
14328 */
14329 protected void dispatchSetSelected(boolean selected) {
14330 }
14331
14332 /**
14333 * Indicates the selection state of this view.
14334 *
14335 * @return true if the view is selected, false otherwise
14336 */
14337 @ViewDebug.ExportedProperty
14338 public boolean isSelected() {
14339 return (mPrivateFlags & SELECTED) != 0;
14340 }
14341
14342 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014343 * Changes the activated state of this view. A view can be activated or not.
14344 * Note that activation is not the same as selection. Selection is
14345 * a transient property, representing the view (hierarchy) the user is
14346 * currently interacting with. Activation is a longer-term state that the
14347 * user can move views in and out of. For example, in a list view with
14348 * single or multiple selection enabled, the views in the current selection
14349 * set are activated. (Um, yeah, we are deeply sorry about the terminology
14350 * here.) The activated state is propagated down to children of the view it
14351 * is set on.
14352 *
14353 * @param activated true if the view must be activated, false otherwise
14354 */
14355 public void setActivated(boolean activated) {
14356 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
14357 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014358 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014359 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070014360 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014361 }
14362 }
14363
14364 /**
14365 * Dispatch setActivated to all of this View's children.
14366 *
14367 * @see #setActivated(boolean)
14368 *
14369 * @param activated The new activated state
14370 */
14371 protected void dispatchSetActivated(boolean activated) {
14372 }
14373
14374 /**
14375 * Indicates the activation state of this view.
14376 *
14377 * @return true if the view is activated, false otherwise
14378 */
14379 @ViewDebug.ExportedProperty
14380 public boolean isActivated() {
14381 return (mPrivateFlags & ACTIVATED) != 0;
14382 }
14383
14384 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014385 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
14386 * observer can be used to get notifications when global events, like
14387 * layout, happen.
14388 *
14389 * The returned ViewTreeObserver observer is not guaranteed to remain
14390 * valid for the lifetime of this View. If the caller of this method keeps
14391 * a long-lived reference to ViewTreeObserver, it should always check for
14392 * the return value of {@link ViewTreeObserver#isAlive()}.
14393 *
14394 * @return The ViewTreeObserver for this view's hierarchy.
14395 */
14396 public ViewTreeObserver getViewTreeObserver() {
14397 if (mAttachInfo != null) {
14398 return mAttachInfo.mTreeObserver;
14399 }
14400 if (mFloatingTreeObserver == null) {
14401 mFloatingTreeObserver = new ViewTreeObserver();
14402 }
14403 return mFloatingTreeObserver;
14404 }
14405
14406 /**
14407 * <p>Finds the topmost view in the current view hierarchy.</p>
14408 *
14409 * @return the topmost view containing this view
14410 */
14411 public View getRootView() {
14412 if (mAttachInfo != null) {
14413 final View v = mAttachInfo.mRootView;
14414 if (v != null) {
14415 return v;
14416 }
14417 }
Romain Guy8506ab42009-06-11 17:35:47 -070014418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014419 View parent = this;
14420
14421 while (parent.mParent != null && parent.mParent instanceof View) {
14422 parent = (View) parent.mParent;
14423 }
14424
14425 return parent;
14426 }
14427
14428 /**
14429 * <p>Computes the coordinates of this view on the screen. The argument
14430 * must be an array of two integers. After the method returns, the array
14431 * contains the x and y location in that order.</p>
14432 *
14433 * @param location an array of two integers in which to hold the coordinates
14434 */
14435 public void getLocationOnScreen(int[] location) {
14436 getLocationInWindow(location);
14437
14438 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070014439 if (info != null) {
14440 location[0] += info.mWindowLeft;
14441 location[1] += info.mWindowTop;
14442 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014443 }
14444
14445 /**
14446 * <p>Computes the coordinates of this view in its window. The argument
14447 * must be an array of two integers. After the method returns, the array
14448 * contains the x and y location in that order.</p>
14449 *
14450 * @param location an array of two integers in which to hold the coordinates
14451 */
14452 public void getLocationInWindow(int[] location) {
14453 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014454 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014455 }
14456
Gilles Debunne6583ce52011-12-06 18:09:02 -080014457 if (mAttachInfo == null) {
14458 // When the view is not attached to a window, this method does not make sense
14459 location[0] = location[1] = 0;
14460 return;
14461 }
14462
Gilles Debunnecea45132011-11-24 02:19:27 +010014463 float[] position = mAttachInfo.mTmpTransformLocation;
14464 position[0] = position[1] = 0.0f;
14465
14466 if (!hasIdentityMatrix()) {
14467 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014468 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014469
Gilles Debunnecea45132011-11-24 02:19:27 +010014470 position[0] += mLeft;
14471 position[1] += mTop;
14472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014473 ViewParent viewParent = mParent;
14474 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014475 final View view = (View) viewParent;
14476
14477 position[0] -= view.mScrollX;
14478 position[1] -= view.mScrollY;
14479
14480 if (!view.hasIdentityMatrix()) {
14481 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014482 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014483
14484 position[0] += view.mLeft;
14485 position[1] += view.mTop;
14486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014487 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070014488 }
Romain Guy8506ab42009-06-11 17:35:47 -070014489
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014490 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014491 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010014492 final ViewRootImpl vr = (ViewRootImpl) viewParent;
14493 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014494 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014495
14496 location[0] = (int) (position[0] + 0.5f);
14497 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014498 }
14499
14500 /**
14501 * {@hide}
14502 * @param id the id of the view to be found
14503 * @return the view of the specified id, null if cannot be found
14504 */
14505 protected View findViewTraversal(int id) {
14506 if (id == mID) {
14507 return this;
14508 }
14509 return null;
14510 }
14511
14512 /**
14513 * {@hide}
14514 * @param tag the tag of the view to be found
14515 * @return the view of specified tag, null if cannot be found
14516 */
14517 protected View findViewWithTagTraversal(Object tag) {
14518 if (tag != null && tag.equals(mTag)) {
14519 return this;
14520 }
14521 return null;
14522 }
14523
14524 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014525 * {@hide}
14526 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070014527 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080014528 * @return The first view that matches the predicate or null.
14529 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070014530 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080014531 if (predicate.apply(this)) {
14532 return this;
14533 }
14534 return null;
14535 }
14536
14537 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014538 * Look for a child view with the given id. If this view has the given
14539 * id, return this view.
14540 *
14541 * @param id The id to search for.
14542 * @return The view that has the given id in the hierarchy or null
14543 */
14544 public final View findViewById(int id) {
14545 if (id < 0) {
14546 return null;
14547 }
14548 return findViewTraversal(id);
14549 }
14550
14551 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070014552 * Finds a view by its unuque and stable accessibility id.
14553 *
14554 * @param accessibilityId The searched accessibility id.
14555 * @return The found view.
14556 */
14557 final View findViewByAccessibilityId(int accessibilityId) {
14558 if (accessibilityId < 0) {
14559 return null;
14560 }
14561 return findViewByAccessibilityIdTraversal(accessibilityId);
14562 }
14563
14564 /**
14565 * Performs the traversal to find a view by its unuque and stable accessibility id.
14566 *
14567 * <strong>Note:</strong>This method does not stop at the root namespace
14568 * boundary since the user can touch the screen at an arbitrary location
14569 * potentially crossing the root namespace bounday which will send an
14570 * accessibility event to accessibility services and they should be able
14571 * to obtain the event source. Also accessibility ids are guaranteed to be
14572 * unique in the window.
14573 *
14574 * @param accessibilityId The accessibility id.
14575 * @return The found view.
14576 */
14577 View findViewByAccessibilityIdTraversal(int accessibilityId) {
14578 if (getAccessibilityViewId() == accessibilityId) {
14579 return this;
14580 }
14581 return null;
14582 }
14583
14584 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014585 * Look for a child view with the given tag. If this view has the given
14586 * tag, return this view.
14587 *
14588 * @param tag The tag to search for, using "tag.equals(getTag())".
14589 * @return The View that has the given tag in the hierarchy or null
14590 */
14591 public final View findViewWithTag(Object tag) {
14592 if (tag == null) {
14593 return null;
14594 }
14595 return findViewWithTagTraversal(tag);
14596 }
14597
14598 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014599 * {@hide}
14600 * Look for a child view that matches the specified predicate.
14601 * If this view matches the predicate, return this view.
14602 *
14603 * @param predicate The predicate to evaluate.
14604 * @return The first view that matches the predicate or null.
14605 */
14606 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070014607 return findViewByPredicateTraversal(predicate, null);
14608 }
14609
14610 /**
14611 * {@hide}
14612 * Look for a child view that matches the specified predicate,
14613 * starting with the specified view and its descendents and then
14614 * recusively searching the ancestors and siblings of that view
14615 * until this view is reached.
14616 *
14617 * This method is useful in cases where the predicate does not match
14618 * a single unique view (perhaps multiple views use the same id)
14619 * and we are trying to find the view that is "closest" in scope to the
14620 * starting view.
14621 *
14622 * @param start The view to start from.
14623 * @param predicate The predicate to evaluate.
14624 * @return The first view that matches the predicate or null.
14625 */
14626 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
14627 View childToSkip = null;
14628 for (;;) {
14629 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
14630 if (view != null || start == this) {
14631 return view;
14632 }
14633
14634 ViewParent parent = start.getParent();
14635 if (parent == null || !(parent instanceof View)) {
14636 return null;
14637 }
14638
14639 childToSkip = start;
14640 start = (View) parent;
14641 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080014642 }
14643
14644 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014645 * Sets the identifier for this view. The identifier does not have to be
14646 * unique in this view's hierarchy. The identifier should be a positive
14647 * number.
14648 *
14649 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070014650 * @see #getId()
14651 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014652 *
14653 * @param id a number used to identify the view
14654 *
14655 * @attr ref android.R.styleable#View_id
14656 */
14657 public void setId(int id) {
14658 mID = id;
14659 }
14660
14661 /**
14662 * {@hide}
14663 *
14664 * @param isRoot true if the view belongs to the root namespace, false
14665 * otherwise
14666 */
14667 public void setIsRootNamespace(boolean isRoot) {
14668 if (isRoot) {
14669 mPrivateFlags |= IS_ROOT_NAMESPACE;
14670 } else {
14671 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
14672 }
14673 }
14674
14675 /**
14676 * {@hide}
14677 *
14678 * @return true if the view belongs to the root namespace, false otherwise
14679 */
14680 public boolean isRootNamespace() {
14681 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
14682 }
14683
14684 /**
14685 * Returns this view's identifier.
14686 *
14687 * @return a positive integer used to identify the view or {@link #NO_ID}
14688 * if the view has no ID
14689 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014690 * @see #setId(int)
14691 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014692 * @attr ref android.R.styleable#View_id
14693 */
14694 @ViewDebug.CapturedViewProperty
14695 public int getId() {
14696 return mID;
14697 }
14698
14699 /**
14700 * Returns this view's tag.
14701 *
14702 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070014703 *
14704 * @see #setTag(Object)
14705 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014706 */
14707 @ViewDebug.ExportedProperty
14708 public Object getTag() {
14709 return mTag;
14710 }
14711
14712 /**
14713 * Sets the tag associated with this view. A tag can be used to mark
14714 * a view in its hierarchy and does not have to be unique within the
14715 * hierarchy. Tags can also be used to store data within a view without
14716 * resorting to another data structure.
14717 *
14718 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070014719 *
14720 * @see #getTag()
14721 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014722 */
14723 public void setTag(final Object tag) {
14724 mTag = tag;
14725 }
14726
14727 /**
Romain Guyd90a3312009-05-06 14:54:28 -070014728 * Returns the tag associated with this view and the specified key.
14729 *
14730 * @param key The key identifying the tag
14731 *
14732 * @return the Object stored in this view as a tag
14733 *
14734 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070014735 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070014736 */
14737 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014738 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070014739 return null;
14740 }
14741
14742 /**
14743 * Sets a tag associated with this view and a key. A tag can be used
14744 * to mark a view in its hierarchy and does not have to be unique within
14745 * the hierarchy. Tags can also be used to store data within a view
14746 * without resorting to another data structure.
14747 *
14748 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070014749 * application to ensure it is unique (see the <a
14750 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
14751 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070014752 * the Android framework or not associated with any package will cause
14753 * an {@link IllegalArgumentException} to be thrown.
14754 *
14755 * @param key The key identifying the tag
14756 * @param tag An Object to tag the view with
14757 *
14758 * @throws IllegalArgumentException If they specified key is not valid
14759 *
14760 * @see #setTag(Object)
14761 * @see #getTag(int)
14762 */
14763 public void setTag(int key, final Object tag) {
14764 // If the package id is 0x00 or 0x01, it's either an undefined package
14765 // or a framework id
14766 if ((key >>> 24) < 2) {
14767 throw new IllegalArgumentException("The key must be an application-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
14774 /**
14775 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
14776 * framework id.
14777 *
14778 * @hide
14779 */
14780 public void setTagInternal(int key, Object tag) {
14781 if ((key >>> 24) != 0x1) {
14782 throw new IllegalArgumentException("The key must be a framework-specific "
14783 + "resource id.");
14784 }
14785
Adam Powell2b2f6d62011-09-23 11:15:39 -070014786 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014787 }
14788
Adam Powell2b2f6d62011-09-23 11:15:39 -070014789 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014790 if (mKeyedTags == null) {
14791 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070014792 }
14793
Adam Powell7db82ac2011-09-22 19:44:04 -070014794 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014795 }
14796
14797 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014798 * Prints information about this view in the log output, with the tag
14799 * {@link #VIEW_LOG_TAG}.
14800 *
14801 * @hide
14802 */
14803 public void debug() {
14804 debug(0);
14805 }
14806
14807 /**
14808 * Prints information about this view in the log output, with the tag
14809 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
14810 * indentation defined by the <code>depth</code>.
14811 *
14812 * @param depth the indentation level
14813 *
14814 * @hide
14815 */
14816 protected void debug(int depth) {
14817 String output = debugIndent(depth - 1);
14818
14819 output += "+ " + this;
14820 int id = getId();
14821 if (id != -1) {
14822 output += " (id=" + id + ")";
14823 }
14824 Object tag = getTag();
14825 if (tag != null) {
14826 output += " (tag=" + tag + ")";
14827 }
14828 Log.d(VIEW_LOG_TAG, output);
14829
14830 if ((mPrivateFlags & FOCUSED) != 0) {
14831 output = debugIndent(depth) + " FOCUSED";
14832 Log.d(VIEW_LOG_TAG, output);
14833 }
14834
14835 output = debugIndent(depth);
14836 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
14837 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
14838 + "} ";
14839 Log.d(VIEW_LOG_TAG, output);
14840
14841 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
14842 || mPaddingBottom != 0) {
14843 output = debugIndent(depth);
14844 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
14845 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
14846 Log.d(VIEW_LOG_TAG, output);
14847 }
14848
14849 output = debugIndent(depth);
14850 output += "mMeasureWidth=" + mMeasuredWidth +
14851 " mMeasureHeight=" + mMeasuredHeight;
14852 Log.d(VIEW_LOG_TAG, output);
14853
14854 output = debugIndent(depth);
14855 if (mLayoutParams == null) {
14856 output += "BAD! no layout params";
14857 } else {
14858 output = mLayoutParams.debug(output);
14859 }
14860 Log.d(VIEW_LOG_TAG, output);
14861
14862 output = debugIndent(depth);
14863 output += "flags={";
14864 output += View.printFlags(mViewFlags);
14865 output += "}";
14866 Log.d(VIEW_LOG_TAG, output);
14867
14868 output = debugIndent(depth);
14869 output += "privateFlags={";
14870 output += View.printPrivateFlags(mPrivateFlags);
14871 output += "}";
14872 Log.d(VIEW_LOG_TAG, output);
14873 }
14874
14875 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090014876 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014877 *
14878 * @param depth the indentation level
14879 * @return a String containing (depth * 2 + 3) * 2 white spaces
14880 *
14881 * @hide
14882 */
14883 protected static String debugIndent(int depth) {
14884 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
14885 for (int i = 0; i < (depth * 2) + 3; i++) {
14886 spaces.append(' ').append(' ');
14887 }
14888 return spaces.toString();
14889 }
14890
14891 /**
14892 * <p>Return the offset of the widget's text baseline from the widget's top
14893 * boundary. If this widget does not support baseline alignment, this
14894 * method returns -1. </p>
14895 *
14896 * @return the offset of the baseline within the widget's bounds or -1
14897 * if baseline alignment is not supported
14898 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070014899 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014900 public int getBaseline() {
14901 return -1;
14902 }
14903
14904 /**
14905 * Call this when something has changed which has invalidated the
14906 * layout of this view. This will schedule a layout pass of the view
14907 * tree.
14908 */
14909 public void requestLayout() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014910 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080014911 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014912
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070014913 if (mLayoutParams != null) {
14914 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
14915 }
14916
14917 if (mParent != null && !mParent.isLayoutRequested()) {
14918 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014919 }
14920 }
14921
14922 /**
14923 * Forces this view to be laid out during the next layout pass.
14924 * This method does not call requestLayout() or forceLayout()
14925 * on the parent.
14926 */
14927 public void forceLayout() {
14928 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080014929 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014930 }
14931
14932 /**
14933 * <p>
14934 * This is called to find out how big a view should be. The parent
14935 * supplies constraint information in the width and height parameters.
14936 * </p>
14937 *
14938 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080014939 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014940 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080014941 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014942 * </p>
14943 *
14944 *
14945 * @param widthMeasureSpec Horizontal space requirements as imposed by the
14946 * parent
14947 * @param heightMeasureSpec Vertical space requirements as imposed by the
14948 * parent
14949 *
14950 * @see #onMeasure(int, int)
14951 */
14952 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
14953 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
14954 widthMeasureSpec != mOldWidthMeasureSpec ||
14955 heightMeasureSpec != mOldHeightMeasureSpec) {
14956
14957 // first clears the measured dimension flag
14958 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
14959
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014960 // measure ourselves, this should set the measured dimension flag back
14961 onMeasure(widthMeasureSpec, heightMeasureSpec);
14962
14963 // flag not set, setMeasuredDimension() was not invoked, we raise
14964 // an exception to warn the developer
14965 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
14966 throw new IllegalStateException("onMeasure() did not set the"
14967 + " measured dimension by calling"
14968 + " setMeasuredDimension()");
14969 }
14970
14971 mPrivateFlags |= LAYOUT_REQUIRED;
14972 }
14973
14974 mOldWidthMeasureSpec = widthMeasureSpec;
14975 mOldHeightMeasureSpec = heightMeasureSpec;
14976 }
14977
14978 /**
14979 * <p>
14980 * Measure the view and its content to determine the measured width and the
14981 * measured height. This method is invoked by {@link #measure(int, int)} and
14982 * should be overriden by subclasses to provide accurate and efficient
14983 * measurement of their contents.
14984 * </p>
14985 *
14986 * <p>
14987 * <strong>CONTRACT:</strong> When overriding this method, you
14988 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
14989 * measured width and height of this view. Failure to do so will trigger an
14990 * <code>IllegalStateException</code>, thrown by
14991 * {@link #measure(int, int)}. Calling the superclass'
14992 * {@link #onMeasure(int, int)} is a valid use.
14993 * </p>
14994 *
14995 * <p>
14996 * The base class implementation of measure defaults to the background size,
14997 * unless a larger size is allowed by the MeasureSpec. Subclasses should
14998 * override {@link #onMeasure(int, int)} to provide better measurements of
14999 * their content.
15000 * </p>
15001 *
15002 * <p>
15003 * If this method is overridden, it is the subclass's responsibility to make
15004 * sure the measured height and width are at least the view's minimum height
15005 * and width ({@link #getSuggestedMinimumHeight()} and
15006 * {@link #getSuggestedMinimumWidth()}).
15007 * </p>
15008 *
15009 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
15010 * The requirements are encoded with
15011 * {@link android.view.View.MeasureSpec}.
15012 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
15013 * The requirements are encoded with
15014 * {@link android.view.View.MeasureSpec}.
15015 *
15016 * @see #getMeasuredWidth()
15017 * @see #getMeasuredHeight()
15018 * @see #setMeasuredDimension(int, int)
15019 * @see #getSuggestedMinimumHeight()
15020 * @see #getSuggestedMinimumWidth()
15021 * @see android.view.View.MeasureSpec#getMode(int)
15022 * @see android.view.View.MeasureSpec#getSize(int)
15023 */
15024 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
15025 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
15026 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
15027 }
15028
15029 /**
15030 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
15031 * measured width and measured height. Failing to do so will trigger an
15032 * exception at measurement time.</p>
15033 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080015034 * @param measuredWidth The measured width of this view. May be a complex
15035 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15036 * {@link #MEASURED_STATE_TOO_SMALL}.
15037 * @param measuredHeight The measured height of this view. May be a complex
15038 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15039 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015040 */
15041 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
15042 mMeasuredWidth = measuredWidth;
15043 mMeasuredHeight = measuredHeight;
15044
15045 mPrivateFlags |= MEASURED_DIMENSION_SET;
15046 }
15047
15048 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080015049 * Merge two states as returned by {@link #getMeasuredState()}.
15050 * @param curState The current state as returned from a view or the result
15051 * of combining multiple views.
15052 * @param newState The new view state to combine.
15053 * @return Returns a new integer reflecting the combination of the two
15054 * states.
15055 */
15056 public static int combineMeasuredStates(int curState, int newState) {
15057 return curState | newState;
15058 }
15059
15060 /**
15061 * Version of {@link #resolveSizeAndState(int, int, int)}
15062 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
15063 */
15064 public static int resolveSize(int size, int measureSpec) {
15065 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
15066 }
15067
15068 /**
15069 * Utility to reconcile a desired size and state, with constraints imposed
15070 * by a MeasureSpec. Will take the desired size, unless a different size
15071 * is imposed by the constraints. The returned value is a compound integer,
15072 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
15073 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
15074 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015075 *
15076 * @param size How big the view wants to be
15077 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080015078 * @return Size information bit mask as defined by
15079 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015080 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080015081 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015082 int result = size;
15083 int specMode = MeasureSpec.getMode(measureSpec);
15084 int specSize = MeasureSpec.getSize(measureSpec);
15085 switch (specMode) {
15086 case MeasureSpec.UNSPECIFIED:
15087 result = size;
15088 break;
15089 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080015090 if (specSize < size) {
15091 result = specSize | MEASURED_STATE_TOO_SMALL;
15092 } else {
15093 result = size;
15094 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015095 break;
15096 case MeasureSpec.EXACTLY:
15097 result = specSize;
15098 break;
15099 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080015100 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015101 }
15102
15103 /**
15104 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070015105 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015106 * by the MeasureSpec.
15107 *
15108 * @param size Default size for this view
15109 * @param measureSpec Constraints imposed by the parent
15110 * @return The size this view should be.
15111 */
15112 public static int getDefaultSize(int size, int measureSpec) {
15113 int result = size;
15114 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070015115 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015116
15117 switch (specMode) {
15118 case MeasureSpec.UNSPECIFIED:
15119 result = size;
15120 break;
15121 case MeasureSpec.AT_MOST:
15122 case MeasureSpec.EXACTLY:
15123 result = specSize;
15124 break;
15125 }
15126 return result;
15127 }
15128
15129 /**
15130 * Returns the suggested minimum height that the view should use. This
15131 * returns the maximum of the view's minimum height
15132 * and the background's minimum height
15133 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
15134 * <p>
15135 * When being used in {@link #onMeasure(int, int)}, the caller should still
15136 * ensure the returned height is within the requirements of the parent.
15137 *
15138 * @return The suggested minimum height of the view.
15139 */
15140 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015141 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015143 }
15144
15145 /**
15146 * Returns the suggested minimum width that the view should use. This
15147 * returns the maximum of the view's minimum width)
15148 * and the background's minimum width
15149 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
15150 * <p>
15151 * When being used in {@link #onMeasure(int, int)}, the caller should still
15152 * ensure the returned width is within the requirements of the parent.
15153 *
15154 * @return The suggested minimum width of the view.
15155 */
15156 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015157 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
15158 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015159
Philip Milne6c8ea062012-04-03 17:38:43 -070015160 /**
15161 * Returns the minimum height of the view.
15162 *
15163 * @return the minimum height the view will try to be.
15164 *
15165 * @see #setMinimumHeight(int)
15166 *
15167 * @attr ref android.R.styleable#View_minHeight
15168 */
15169 public int getMinimumHeight() {
15170 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015171 }
15172
15173 /**
15174 * Sets the minimum height of the view. It is not guaranteed the view will
15175 * be able to achieve this minimum height (for example, if its parent layout
15176 * constrains it with less available height).
15177 *
15178 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015179 *
15180 * @see #getMinimumHeight()
15181 *
15182 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015183 */
15184 public void setMinimumHeight(int minHeight) {
15185 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070015186 requestLayout();
15187 }
15188
15189 /**
15190 * Returns the minimum width of the view.
15191 *
15192 * @return the minimum width the view will try to be.
15193 *
15194 * @see #setMinimumWidth(int)
15195 *
15196 * @attr ref android.R.styleable#View_minWidth
15197 */
15198 public int getMinimumWidth() {
15199 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015200 }
15201
15202 /**
15203 * Sets the minimum width of the view. It is not guaranteed the view will
15204 * be able to achieve this minimum width (for example, if its parent layout
15205 * constrains it with less available width).
15206 *
15207 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015208 *
15209 * @see #getMinimumWidth()
15210 *
15211 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015212 */
15213 public void setMinimumWidth(int minWidth) {
15214 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070015215 requestLayout();
15216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015217 }
15218
15219 /**
15220 * Get the animation currently associated with this view.
15221 *
15222 * @return The animation that is currently playing or
15223 * scheduled to play for this view.
15224 */
15225 public Animation getAnimation() {
15226 return mCurrentAnimation;
15227 }
15228
15229 /**
15230 * Start the specified animation now.
15231 *
15232 * @param animation the animation to start now
15233 */
15234 public void startAnimation(Animation animation) {
15235 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
15236 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080015237 invalidateParentCaches();
15238 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015239 }
15240
15241 /**
15242 * Cancels any animations for this view.
15243 */
15244 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080015245 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080015246 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080015247 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015248 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080015249 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015250 }
15251
15252 /**
15253 * Sets the next animation to play for this view.
15254 * If you want the animation to play immediately, use
Chet Haase42428932012-05-11 15:39:07 -070015255 * {@link #startAnimation(android.view.animation.Animation)} instead.
15256 * This method provides allows fine-grained
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015257 * control over the start time and invalidation, but you
15258 * must make sure that 1) the animation has a start time set, and
Chet Haase42428932012-05-11 15:39:07 -070015259 * 2) the view's parent (which controls animations on its children)
15260 * will be invalidated when the animation is supposed to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015261 * start.
15262 *
15263 * @param animation The next animation, or null.
15264 */
15265 public void setAnimation(Animation animation) {
15266 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070015267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015268 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070015269 // If the screen is off assume the animation start time is now instead of
15270 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
15271 // would cause the animation to start when the screen turns back on
15272 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
15273 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
15274 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
15275 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015276 animation.reset();
15277 }
15278 }
15279
15280 /**
15281 * Invoked by a parent ViewGroup to notify the start of the animation
15282 * currently associated with this view. If you override this method,
15283 * always call super.onAnimationStart();
15284 *
15285 * @see #setAnimation(android.view.animation.Animation)
15286 * @see #getAnimation()
15287 */
15288 protected void onAnimationStart() {
15289 mPrivateFlags |= ANIMATION_STARTED;
15290 }
15291
15292 /**
15293 * Invoked by a parent ViewGroup to notify the end of the animation
15294 * currently associated with this view. If you override this method,
15295 * always call super.onAnimationEnd();
15296 *
15297 * @see #setAnimation(android.view.animation.Animation)
15298 * @see #getAnimation()
15299 */
15300 protected void onAnimationEnd() {
15301 mPrivateFlags &= ~ANIMATION_STARTED;
15302 }
15303
15304 /**
15305 * Invoked if there is a Transform that involves alpha. Subclass that can
15306 * draw themselves with the specified alpha should return true, and then
15307 * respect that alpha when their onDraw() is called. If this returns false
15308 * then the view may be redirected to draw into an offscreen buffer to
15309 * fulfill the request, which will look fine, but may be slower than if the
15310 * subclass handles it internally. The default implementation returns false.
15311 *
15312 * @param alpha The alpha (0..255) to apply to the view's drawing
15313 * @return true if the view can draw with the specified alpha.
15314 */
15315 protected boolean onSetAlpha(int alpha) {
15316 return false;
15317 }
15318
15319 /**
15320 * This is used by the RootView to perform an optimization when
15321 * the view hierarchy contains one or several SurfaceView.
15322 * SurfaceView is always considered transparent, but its children are not,
15323 * therefore all View objects remove themselves from the global transparent
15324 * region (passed as a parameter to this function).
15325 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015326 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015327 *
15328 * @return Returns true if the effective visibility of the view at this
15329 * point is opaque, regardless of the transparent region; returns false
15330 * if it is possible for underlying windows to be seen behind the view.
15331 *
15332 * {@hide}
15333 */
15334 public boolean gatherTransparentRegion(Region region) {
15335 final AttachInfo attachInfo = mAttachInfo;
15336 if (region != null && attachInfo != null) {
15337 final int pflags = mPrivateFlags;
15338 if ((pflags & SKIP_DRAW) == 0) {
15339 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
15340 // remove it from the transparent region.
15341 final int[] location = attachInfo.mTransparentLocation;
15342 getLocationInWindow(location);
15343 region.op(location[0], location[1], location[0] + mRight - mLeft,
15344 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Philip Milne6c8ea062012-04-03 17:38:43 -070015345 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015346 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
15347 // exists, so we remove the background drawable's non-transparent
15348 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070015349 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015350 }
15351 }
15352 return true;
15353 }
15354
15355 /**
15356 * Play a sound effect for this view.
15357 *
15358 * <p>The framework will play sound effects for some built in actions, such as
15359 * clicking, but you may wish to play these effects in your widget,
15360 * for instance, for internal navigation.
15361 *
15362 * <p>The sound effect will only be played if sound effects are enabled by the user, and
15363 * {@link #isSoundEffectsEnabled()} is true.
15364 *
15365 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
15366 */
15367 public void playSoundEffect(int soundConstant) {
15368 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
15369 return;
15370 }
15371 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
15372 }
15373
15374 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015375 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015376 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015377 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015378 *
15379 * <p>The framework will provide haptic feedback for some built in actions,
15380 * such as long presses, but you may wish to provide feedback for your
15381 * own widget.
15382 *
15383 * <p>The feedback will only be performed if
15384 * {@link #isHapticFeedbackEnabled()} is true.
15385 *
15386 * @param feedbackConstant One of the constants defined in
15387 * {@link HapticFeedbackConstants}
15388 */
15389 public boolean performHapticFeedback(int feedbackConstant) {
15390 return performHapticFeedback(feedbackConstant, 0);
15391 }
15392
15393 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015394 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015395 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015396 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015397 *
15398 * @param feedbackConstant One of the constants defined in
15399 * {@link HapticFeedbackConstants}
15400 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
15401 */
15402 public boolean performHapticFeedback(int feedbackConstant, int flags) {
15403 if (mAttachInfo == null) {
15404 return false;
15405 }
Romain Guyf607bdc2010-09-10 19:20:06 -070015406 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070015407 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015408 && !isHapticFeedbackEnabled()) {
15409 return false;
15410 }
Romain Guy812ccbe2010-06-01 14:07:24 -070015411 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
15412 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015413 }
15414
15415 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015416 * Request that the visibility of the status bar or other screen/window
15417 * decorations be changed.
15418 *
15419 * <p>This method is used to put the over device UI into temporary modes
15420 * where the user's attention is focused more on the application content,
15421 * by dimming or hiding surrounding system affordances. This is typically
15422 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
15423 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
15424 * to be placed behind the action bar (and with these flags other system
15425 * affordances) so that smooth transitions between hiding and showing them
15426 * can be done.
15427 *
15428 * <p>Two representative examples of the use of system UI visibility is
15429 * implementing a content browsing application (like a magazine reader)
15430 * and a video playing application.
15431 *
15432 * <p>The first code shows a typical implementation of a View in a content
15433 * browsing application. In this implementation, the application goes
15434 * into a content-oriented mode by hiding the status bar and action bar,
15435 * and putting the navigation elements into lights out mode. The user can
15436 * then interact with content while in this mode. Such an application should
15437 * provide an easy way for the user to toggle out of the mode (such as to
15438 * check information in the status bar or access notifications). In the
15439 * implementation here, this is done simply by tapping on the content.
15440 *
15441 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
15442 * content}
15443 *
15444 * <p>This second code sample shows a typical implementation of a View
15445 * in a video playing application. In this situation, while the video is
15446 * playing the application would like to go into a complete full-screen mode,
15447 * to use as much of the display as possible for the video. When in this state
15448 * the user can not interact with the application; the system intercepts
Dianne Hackborncf675782012-05-10 15:07:24 -070015449 * touching on the screen to pop the UI out of full screen mode. See
15450 * {@link #fitSystemWindows(Rect)} for a sample layout that goes with this code.
Dianne Hackborn98014352012-04-05 18:31:41 -070015451 *
15452 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
15453 * content}
15454 *
15455 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15456 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15457 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15458 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015459 */
15460 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040015461 if (visibility != mSystemUiVisibility) {
15462 mSystemUiVisibility = visibility;
15463 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15464 mParent.recomputeViewAttributes(this);
15465 }
Joe Onorato664644d2011-01-23 17:53:23 -080015466 }
15467 }
15468
15469 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015470 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
15471 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15472 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15473 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15474 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015475 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080015476 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080015477 return mSystemUiVisibility;
15478 }
15479
Scott Mainec6331b2011-05-24 16:55:56 -070015480 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070015481 * Returns the current system UI visibility that is currently set for
15482 * the entire window. This is the combination of the
15483 * {@link #setSystemUiVisibility(int)} values supplied by all of the
15484 * views in the window.
15485 */
15486 public int getWindowSystemUiVisibility() {
15487 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
15488 }
15489
15490 /**
15491 * Override to find out when the window's requested system UI visibility
15492 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
15493 * This is different from the callbacks recieved through
15494 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
15495 * in that this is only telling you about the local request of the window,
15496 * not the actual values applied by the system.
15497 */
15498 public void onWindowSystemUiVisibilityChanged(int visible) {
15499 }
15500
15501 /**
15502 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
15503 * the view hierarchy.
15504 */
15505 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
15506 onWindowSystemUiVisibilityChanged(visible);
15507 }
15508
15509 /**
Scott Mainec6331b2011-05-24 16:55:56 -070015510 * Set a listener to receive callbacks when the visibility of the system bar changes.
15511 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
15512 */
Joe Onorato664644d2011-01-23 17:53:23 -080015513 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015514 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080015515 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15516 mParent.recomputeViewAttributes(this);
15517 }
15518 }
15519
15520 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015521 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
15522 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080015523 */
15524 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015525 ListenerInfo li = mListenerInfo;
15526 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
15527 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080015528 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080015529 }
15530 }
15531
Dianne Hackborncf675782012-05-10 15:07:24 -070015532 boolean updateLocalSystemUiVisibility(int localValue, int localChanges) {
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015533 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
15534 if (val != mSystemUiVisibility) {
15535 setSystemUiVisibility(val);
Dianne Hackborncf675782012-05-10 15:07:24 -070015536 return true;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015537 }
Dianne Hackborncf675782012-05-10 15:07:24 -070015538 return false;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015539 }
15540
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070015541 /** @hide */
15542 public void setDisabledSystemUiVisibility(int flags) {
15543 if (mAttachInfo != null) {
15544 if (mAttachInfo.mDisabledSystemUiVisibility != flags) {
15545 mAttachInfo.mDisabledSystemUiVisibility = flags;
15546 if (mParent != null) {
15547 mParent.recomputeViewAttributes(this);
15548 }
15549 }
15550 }
15551 }
15552
Joe Onorato664644d2011-01-23 17:53:23 -080015553 /**
Joe Malin32736f02011-01-19 16:14:20 -080015554 * Creates an image that the system displays during the drag and drop
15555 * operation. This is called a &quot;drag shadow&quot;. The default implementation
15556 * for a DragShadowBuilder based on a View returns an image that has exactly the same
15557 * appearance as the given View. The default also positions the center of the drag shadow
15558 * directly under the touch point. If no View is provided (the constructor with no parameters
15559 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
15560 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
15561 * default is an invisible drag shadow.
15562 * <p>
15563 * You are not required to use the View you provide to the constructor as the basis of the
15564 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
15565 * anything you want as the drag shadow.
15566 * </p>
15567 * <p>
15568 * You pass a DragShadowBuilder object to the system when you start the drag. The system
15569 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
15570 * size and position of the drag shadow. It uses this data to construct a
15571 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
15572 * so that your application can draw the shadow image in the Canvas.
15573 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070015574 *
15575 * <div class="special reference">
15576 * <h3>Developer Guides</h3>
15577 * <p>For a guide to implementing drag and drop features, read the
15578 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
15579 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070015580 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015581 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070015582 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070015583
15584 /**
Joe Malin32736f02011-01-19 16:14:20 -080015585 * Constructs a shadow image builder based on a View. By default, the resulting drag
15586 * shadow will have the same appearance and dimensions as the View, with the touch point
15587 * over the center of the View.
15588 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070015589 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015590 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070015591 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070015592 }
15593
Christopher Tate17ed60c2011-01-18 12:50:26 -080015594 /**
15595 * Construct a shadow builder object with no associated View. This
15596 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
15597 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
15598 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080015599 * reference to any View object. If they are not overridden, then the result is an
15600 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080015601 */
15602 public DragShadowBuilder() {
15603 mView = new WeakReference<View>(null);
15604 }
15605
15606 /**
15607 * Returns the View object that had been passed to the
15608 * {@link #View.DragShadowBuilder(View)}
15609 * constructor. If that View parameter was {@code null} or if the
15610 * {@link #View.DragShadowBuilder()}
15611 * constructor was used to instantiate the builder object, this method will return
15612 * null.
15613 *
15614 * @return The View object associate with this builder object.
15615 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070015616 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070015617 final public View getView() {
15618 return mView.get();
15619 }
15620
Christopher Tate2c095f32010-10-04 14:13:40 -070015621 /**
Joe Malin32736f02011-01-19 16:14:20 -080015622 * Provides the metrics for the shadow image. These include the dimensions of
15623 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070015624 * be centered under the touch location while dragging.
15625 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015626 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080015627 * same as the dimensions of the View itself and centers the shadow under
15628 * the touch point.
15629 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070015630 *
Joe Malin32736f02011-01-19 16:14:20 -080015631 * @param shadowSize A {@link android.graphics.Point} containing the width and height
15632 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
15633 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
15634 * image.
15635 *
15636 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
15637 * shadow image that should be underneath the touch point during the drag and drop
15638 * operation. Your application must set {@link android.graphics.Point#x} to the
15639 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070015640 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015641 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070015642 final View view = mView.get();
15643 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015644 shadowSize.set(view.getWidth(), view.getHeight());
15645 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070015646 } else {
15647 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
15648 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015649 }
15650
15651 /**
Joe Malin32736f02011-01-19 16:14:20 -080015652 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
15653 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015654 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070015655 *
Joe Malin32736f02011-01-19 16:14:20 -080015656 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070015657 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015658 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070015659 final View view = mView.get();
15660 if (view != null) {
15661 view.draw(canvas);
15662 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015663 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070015664 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015665 }
15666 }
15667
15668 /**
Joe Malin32736f02011-01-19 16:14:20 -080015669 * Starts a drag and drop operation. When your application calls this method, it passes a
15670 * {@link android.view.View.DragShadowBuilder} object to the system. The
15671 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
15672 * to get metrics for the drag shadow, and then calls the object's
15673 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
15674 * <p>
15675 * Once the system has the drag shadow, it begins the drag and drop operation by sending
15676 * drag events to all the View objects in your application that are currently visible. It does
15677 * this either by calling the View object's drag listener (an implementation of
15678 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
15679 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
15680 * Both are passed a {@link android.view.DragEvent} object that has a
15681 * {@link android.view.DragEvent#getAction()} value of
15682 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
15683 * </p>
15684 * <p>
15685 * Your application can invoke startDrag() on any attached View object. The View object does not
15686 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
15687 * be related to the View the user selected for dragging.
15688 * </p>
15689 * @param data A {@link android.content.ClipData} object pointing to the data to be
15690 * transferred by the drag and drop operation.
15691 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
15692 * drag shadow.
15693 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
15694 * drop operation. This Object is put into every DragEvent object sent by the system during the
15695 * current drag.
15696 * <p>
15697 * myLocalState is a lightweight mechanism for the sending information from the dragged View
15698 * to the target Views. For example, it can contain flags that differentiate between a
15699 * a copy operation and a move operation.
15700 * </p>
15701 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
15702 * so the parameter should be set to 0.
15703 * @return {@code true} if the method completes successfully, or
15704 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
15705 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070015706 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015707 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015708 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070015709 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015710 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070015711 }
15712 boolean okay = false;
15713
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015714 Point shadowSize = new Point();
15715 Point shadowTouchPoint = new Point();
15716 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070015717
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015718 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
15719 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
15720 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070015721 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015722
Chris Tatea32dcf72010-10-14 12:13:50 -070015723 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015724 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
15725 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070015726 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015727 Surface surface = new Surface();
15728 try {
15729 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015730 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070015731 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070015732 + " surface=" + surface);
15733 if (token != null) {
15734 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070015735 try {
Chris Tate6b391282010-10-14 15:48:59 -070015736 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015737 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070015738 } finally {
15739 surface.unlockCanvasAndPost(canvas);
15740 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015741
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015742 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080015743
15744 // Cache the local state object for delivery with DragEvents
15745 root.setLocalDragState(myLocalState);
15746
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015747 // repurpose 'shadowSize' for the last touch point
15748 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070015749
Christopher Tatea53146c2010-09-07 11:57:52 -070015750 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015751 shadowSize.x, shadowSize.y,
15752 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070015753 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070015754
15755 // Off and running! Release our local surface instance; the drag
15756 // shadow surface is now managed by the system process.
15757 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070015758 }
15759 } catch (Exception e) {
15760 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
15761 surface.destroy();
15762 }
15763
15764 return okay;
15765 }
15766
Christopher Tatea53146c2010-09-07 11:57:52 -070015767 /**
Joe Malin32736f02011-01-19 16:14:20 -080015768 * Handles drag events sent by the system following a call to
15769 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
15770 *<p>
15771 * When the system calls this method, it passes a
15772 * {@link android.view.DragEvent} object. A call to
15773 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
15774 * in DragEvent. The method uses these to determine what is happening in the drag and drop
15775 * operation.
15776 * @param event The {@link android.view.DragEvent} sent by the system.
15777 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
15778 * in DragEvent, indicating the type of drag event represented by this object.
15779 * @return {@code true} if the method was successful, otherwise {@code false}.
15780 * <p>
15781 * The method should return {@code true} in response to an action type of
15782 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
15783 * operation.
15784 * </p>
15785 * <p>
15786 * The method should also return {@code true} in response to an action type of
15787 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
15788 * {@code false} if it didn't.
15789 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015790 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070015791 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070015792 return false;
15793 }
15794
15795 /**
Joe Malin32736f02011-01-19 16:14:20 -080015796 * Detects if this View is enabled and has a drag event listener.
15797 * If both are true, then it calls the drag event listener with the
15798 * {@link android.view.DragEvent} it received. If the drag event listener returns
15799 * {@code true}, then dispatchDragEvent() returns {@code true}.
15800 * <p>
15801 * For all other cases, the method calls the
15802 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
15803 * method and returns its result.
15804 * </p>
15805 * <p>
15806 * This ensures that a drag event is always consumed, even if the View does not have a drag
15807 * event listener. However, if the View has a listener and the listener returns true, then
15808 * onDragEvent() is not called.
15809 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015810 */
15811 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080015812 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015813 ListenerInfo li = mListenerInfo;
15814 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
15815 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070015816 return true;
15817 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015818 return onDragEvent(event);
15819 }
15820
Christopher Tate3d4bf172011-03-28 16:16:46 -070015821 boolean canAcceptDrag() {
15822 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
15823 }
15824
Christopher Tatea53146c2010-09-07 11:57:52 -070015825 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070015826 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
15827 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070015828 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070015829 */
15830 public void onCloseSystemDialogs(String reason) {
15831 }
Joe Malin32736f02011-01-19 16:14:20 -080015832
Dianne Hackbornffa42482009-09-23 22:20:11 -070015833 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015834 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070015835 * update a Region being computed for
15836 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015837 * that any non-transparent parts of the Drawable are removed from the
15838 * given transparent region.
15839 *
15840 * @param dr The Drawable whose transparency is to be applied to the region.
15841 * @param region A Region holding the current transparency information,
15842 * where any parts of the region that are set are considered to be
15843 * transparent. On return, this region will be modified to have the
15844 * transparency information reduced by the corresponding parts of the
15845 * Drawable that are not transparent.
15846 * {@hide}
15847 */
15848 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
15849 if (DBG) {
15850 Log.i("View", "Getting transparent region for: " + this);
15851 }
15852 final Region r = dr.getTransparentRegion();
15853 final Rect db = dr.getBounds();
15854 final AttachInfo attachInfo = mAttachInfo;
15855 if (r != null && attachInfo != null) {
15856 final int w = getRight()-getLeft();
15857 final int h = getBottom()-getTop();
15858 if (db.left > 0) {
15859 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
15860 r.op(0, 0, db.left, h, Region.Op.UNION);
15861 }
15862 if (db.right < w) {
15863 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
15864 r.op(db.right, 0, w, h, Region.Op.UNION);
15865 }
15866 if (db.top > 0) {
15867 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
15868 r.op(0, 0, w, db.top, Region.Op.UNION);
15869 }
15870 if (db.bottom < h) {
15871 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
15872 r.op(0, db.bottom, w, h, Region.Op.UNION);
15873 }
15874 final int[] location = attachInfo.mTransparentLocation;
15875 getLocationInWindow(location);
15876 r.translate(location[0], location[1]);
15877 region.op(r, Region.Op.INTERSECT);
15878 } else {
15879 region.op(db, Region.Op.DIFFERENCE);
15880 }
15881 }
15882
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015883 private void checkForLongClick(int delayOffset) {
15884 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
15885 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015886
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015887 if (mPendingCheckForLongPress == null) {
15888 mPendingCheckForLongPress = new CheckForLongPress();
15889 }
15890 mPendingCheckForLongPress.rememberWindowAttachCount();
15891 postDelayed(mPendingCheckForLongPress,
15892 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015893 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015894 }
15895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015896 /**
15897 * Inflate a view from an XML resource. This convenience method wraps the {@link
15898 * LayoutInflater} class, which provides a full range of options for view inflation.
15899 *
15900 * @param context The Context object for your activity or application.
15901 * @param resource The resource ID to inflate
15902 * @param root A view group that will be the parent. Used to properly inflate the
15903 * layout_* parameters.
15904 * @see LayoutInflater
15905 */
15906 public static View inflate(Context context, int resource, ViewGroup root) {
15907 LayoutInflater factory = LayoutInflater.from(context);
15908 return factory.inflate(resource, root);
15909 }
Romain Guy33e72ae2010-07-17 12:40:29 -070015910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015911 /**
Adam Powell637d3372010-08-25 14:37:03 -070015912 * Scroll the view with standard behavior for scrolling beyond the normal
15913 * content boundaries. Views that call this method should override
15914 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
15915 * results of an over-scroll operation.
15916 *
15917 * Views can use this method to handle any touch or fling-based scrolling.
15918 *
15919 * @param deltaX Change in X in pixels
15920 * @param deltaY Change in Y in pixels
15921 * @param scrollX Current X scroll value in pixels before applying deltaX
15922 * @param scrollY Current Y scroll value in pixels before applying deltaY
15923 * @param scrollRangeX Maximum content scroll range along the X axis
15924 * @param scrollRangeY Maximum content scroll range along the Y axis
15925 * @param maxOverScrollX Number of pixels to overscroll by in either direction
15926 * along the X axis.
15927 * @param maxOverScrollY Number of pixels to overscroll by in either direction
15928 * along the Y axis.
15929 * @param isTouchEvent true if this scroll operation is the result of a touch event.
15930 * @return true if scrolling was clamped to an over-scroll boundary along either
15931 * axis, false otherwise.
15932 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070015933 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070015934 protected boolean overScrollBy(int deltaX, int deltaY,
15935 int scrollX, int scrollY,
15936 int scrollRangeX, int scrollRangeY,
15937 int maxOverScrollX, int maxOverScrollY,
15938 boolean isTouchEvent) {
15939 final int overScrollMode = mOverScrollMode;
15940 final boolean canScrollHorizontal =
15941 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
15942 final boolean canScrollVertical =
15943 computeVerticalScrollRange() > computeVerticalScrollExtent();
15944 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
15945 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
15946 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
15947 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
15948
15949 int newScrollX = scrollX + deltaX;
15950 if (!overScrollHorizontal) {
15951 maxOverScrollX = 0;
15952 }
15953
15954 int newScrollY = scrollY + deltaY;
15955 if (!overScrollVertical) {
15956 maxOverScrollY = 0;
15957 }
15958
15959 // Clamp values if at the limits and record
15960 final int left = -maxOverScrollX;
15961 final int right = maxOverScrollX + scrollRangeX;
15962 final int top = -maxOverScrollY;
15963 final int bottom = maxOverScrollY + scrollRangeY;
15964
15965 boolean clampedX = false;
15966 if (newScrollX > right) {
15967 newScrollX = right;
15968 clampedX = true;
15969 } else if (newScrollX < left) {
15970 newScrollX = left;
15971 clampedX = true;
15972 }
15973
15974 boolean clampedY = false;
15975 if (newScrollY > bottom) {
15976 newScrollY = bottom;
15977 clampedY = true;
15978 } else if (newScrollY < top) {
15979 newScrollY = top;
15980 clampedY = true;
15981 }
15982
15983 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
15984
15985 return clampedX || clampedY;
15986 }
15987
15988 /**
15989 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
15990 * respond to the results of an over-scroll operation.
15991 *
15992 * @param scrollX New X scroll value in pixels
15993 * @param scrollY New Y scroll value in pixels
15994 * @param clampedX True if scrollX was clamped to an over-scroll boundary
15995 * @param clampedY True if scrollY was clamped to an over-scroll boundary
15996 */
15997 protected void onOverScrolled(int scrollX, int scrollY,
15998 boolean clampedX, boolean clampedY) {
15999 // Intentionally empty.
16000 }
16001
16002 /**
16003 * Returns the over-scroll mode for this view. The result will be
16004 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16005 * (allow over-scrolling only if the view content is larger than the container),
16006 * or {@link #OVER_SCROLL_NEVER}.
16007 *
16008 * @return This view's over-scroll mode.
16009 */
16010 public int getOverScrollMode() {
16011 return mOverScrollMode;
16012 }
16013
16014 /**
16015 * Set the over-scroll mode for this view. Valid over-scroll modes are
16016 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16017 * (allow over-scrolling only if the view content is larger than the container),
16018 * or {@link #OVER_SCROLL_NEVER}.
16019 *
16020 * Setting the over-scroll mode of a view will have an effect only if the
16021 * view is capable of scrolling.
16022 *
16023 * @param overScrollMode The new over-scroll mode for this view.
16024 */
16025 public void setOverScrollMode(int overScrollMode) {
16026 if (overScrollMode != OVER_SCROLL_ALWAYS &&
16027 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
16028 overScrollMode != OVER_SCROLL_NEVER) {
16029 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
16030 }
16031 mOverScrollMode = overScrollMode;
16032 }
16033
16034 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016035 * Gets a scale factor that determines the distance the view should scroll
16036 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
16037 * @return The vertical scroll scale factor.
16038 * @hide
16039 */
16040 protected float getVerticalScrollFactor() {
16041 if (mVerticalScrollFactor == 0) {
16042 TypedValue outValue = new TypedValue();
16043 if (!mContext.getTheme().resolveAttribute(
16044 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
16045 throw new IllegalStateException(
16046 "Expected theme to define listPreferredItemHeight.");
16047 }
16048 mVerticalScrollFactor = outValue.getDimension(
16049 mContext.getResources().getDisplayMetrics());
16050 }
16051 return mVerticalScrollFactor;
16052 }
16053
16054 /**
16055 * Gets a scale factor that determines the distance the view should scroll
16056 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
16057 * @return The horizontal scroll scale factor.
16058 * @hide
16059 */
16060 protected float getHorizontalScrollFactor() {
16061 // TODO: Should use something else.
16062 return getVerticalScrollFactor();
16063 }
16064
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016065 /**
16066 * Return the value specifying the text direction or policy that was set with
16067 * {@link #setTextDirection(int)}.
16068 *
16069 * @return the defined text direction. It can be one of:
16070 *
16071 * {@link #TEXT_DIRECTION_INHERIT},
16072 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16073 * {@link #TEXT_DIRECTION_ANY_RTL},
16074 * {@link #TEXT_DIRECTION_LTR},
16075 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016076 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016077 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016078 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016079 @ViewDebug.ExportedProperty(category = "text", mapping = {
16080 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
16081 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
16082 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
16083 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
16084 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
16085 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
16086 })
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016087 public int getTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016088 return (mPrivateFlags2 & TEXT_DIRECTION_MASK) >> TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016089 }
16090
16091 /**
16092 * Set the text direction.
16093 *
16094 * @param textDirection the direction to set. Should be one of:
16095 *
16096 * {@link #TEXT_DIRECTION_INHERIT},
16097 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16098 * {@link #TEXT_DIRECTION_ANY_RTL},
16099 * {@link #TEXT_DIRECTION_LTR},
16100 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016101 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016102 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016103 */
16104 public void setTextDirection(int textDirection) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016105 if (getTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016106 // Reset the current text direction and the resolved one
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016107 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016108 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016109 // Set the new text direction
16110 mPrivateFlags2 |= ((textDirection << TEXT_DIRECTION_MASK_SHIFT) & TEXT_DIRECTION_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016111 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016112 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016113 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016114 }
16115 }
16116
16117 /**
16118 * Return the resolved text direction.
16119 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016120 * This needs resolution if the value is TEXT_DIRECTION_INHERIT. The resolution matches
16121 * {@link #getTextDirection()}if it is not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds
16122 * up the parent chain of the view. if there is no parent, then it will return the default
16123 * {@link #TEXT_DIRECTION_FIRST_STRONG}.
16124 *
16125 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016126 *
Doug Feltcb3791202011-07-07 11:57:48 -070016127 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16128 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016129 * {@link #TEXT_DIRECTION_LTR},
16130 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016131 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016132 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016133 */
16134 public int getResolvedTextDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070016135 // The text direction will be resolved only if needed
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016136 if ((mPrivateFlags2 & TEXT_DIRECTION_RESOLVED) != TEXT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016137 resolveTextDirection();
16138 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016139 return (mPrivateFlags2 & TEXT_DIRECTION_RESOLVED_MASK) >> TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016140 }
16141
16142 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016143 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
16144 * resolution is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016145 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016146 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016147 public void resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016148 // Reset any previous text direction resolution
16149 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
16150
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016151 if (hasRtlSupport()) {
16152 // Set resolved text direction flag depending on text direction flag
16153 final int textDirection = getTextDirection();
16154 switch(textDirection) {
16155 case TEXT_DIRECTION_INHERIT:
16156 if (canResolveTextDirection()) {
16157 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016158
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016159 // Set current resolved direction to the same value as the parent's one
16160 final int parentResolvedDirection = viewGroup.getResolvedTextDirection();
16161 switch (parentResolvedDirection) {
16162 case TEXT_DIRECTION_FIRST_STRONG:
16163 case TEXT_DIRECTION_ANY_RTL:
16164 case TEXT_DIRECTION_LTR:
16165 case TEXT_DIRECTION_RTL:
16166 case TEXT_DIRECTION_LOCALE:
16167 mPrivateFlags2 |=
16168 (parentResolvedDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16169 break;
16170 default:
16171 // Default resolved direction is "first strong" heuristic
16172 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
16173 }
16174 } else {
16175 // We cannot do the resolution if there is no parent, so use the default one
16176 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016177 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016178 break;
16179 case TEXT_DIRECTION_FIRST_STRONG:
16180 case TEXT_DIRECTION_ANY_RTL:
16181 case TEXT_DIRECTION_LTR:
16182 case TEXT_DIRECTION_RTL:
16183 case TEXT_DIRECTION_LOCALE:
16184 // Resolved direction is the same as text direction
16185 mPrivateFlags2 |= (textDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16186 break;
16187 default:
16188 // Default resolved direction is "first strong" heuristic
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016189 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016190 }
16191 } else {
16192 // Default resolved direction is "first strong" heuristic
16193 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016194 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016195
16196 // Set to resolved
16197 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016198 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016199 }
16200
16201 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016202 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016203 * resolution should override this method.
16204 *
16205 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016206 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016207 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016208 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016209 }
16210
16211 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016212 * Check if text direction resolution can be done.
16213 *
16214 * @return true if text direction resolution can be done otherwise return false.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016215 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016216 */
16217 public boolean canResolveTextDirection() {
16218 switch (getTextDirection()) {
16219 case TEXT_DIRECTION_INHERIT:
16220 return (mParent != null) && (mParent instanceof ViewGroup);
16221 default:
16222 return true;
16223 }
16224 }
16225
16226 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016227 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016228 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016229 * reset is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016230 * @hide
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016231 */
16232 public void resetResolvedTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016233 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016234 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016235 }
16236
16237 /**
16238 * Called when text direction is reset. Subclasses that care about text direction reset should
16239 * override this method and do a reset of the text direction of their children. The default
16240 * implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016241 * @hide
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016242 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016243 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016244 }
16245
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016246 /**
16247 * Return the value specifying the text alignment or policy that was set with
16248 * {@link #setTextAlignment(int)}.
16249 *
16250 * @return the defined text alignment. It can be one of:
16251 *
16252 * {@link #TEXT_ALIGNMENT_INHERIT},
16253 * {@link #TEXT_ALIGNMENT_GRAVITY},
16254 * {@link #TEXT_ALIGNMENT_CENTER},
16255 * {@link #TEXT_ALIGNMENT_TEXT_START},
16256 * {@link #TEXT_ALIGNMENT_TEXT_END},
16257 * {@link #TEXT_ALIGNMENT_VIEW_START},
16258 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016259 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016260 */
16261 @ViewDebug.ExportedProperty(category = "text", mapping = {
16262 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16263 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16264 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16265 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16266 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16267 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16268 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16269 })
16270 public int getTextAlignment() {
16271 return (mPrivateFlags2 & TEXT_ALIGNMENT_MASK) >> TEXT_ALIGNMENT_MASK_SHIFT;
16272 }
16273
16274 /**
16275 * Set the text alignment.
16276 *
16277 * @param textAlignment The text alignment to set. Should be one of
16278 *
16279 * {@link #TEXT_ALIGNMENT_INHERIT},
16280 * {@link #TEXT_ALIGNMENT_GRAVITY},
16281 * {@link #TEXT_ALIGNMENT_CENTER},
16282 * {@link #TEXT_ALIGNMENT_TEXT_START},
16283 * {@link #TEXT_ALIGNMENT_TEXT_END},
16284 * {@link #TEXT_ALIGNMENT_VIEW_START},
16285 * {@link #TEXT_ALIGNMENT_VIEW_END}
16286 *
16287 * @attr ref android.R.styleable#View_textAlignment
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016288 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016289 */
16290 public void setTextAlignment(int textAlignment) {
16291 if (textAlignment != getTextAlignment()) {
16292 // Reset the current and resolved text alignment
16293 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
16294 resetResolvedTextAlignment();
16295 // Set the new text alignment
16296 mPrivateFlags2 |= ((textAlignment << TEXT_ALIGNMENT_MASK_SHIFT) & TEXT_ALIGNMENT_MASK);
16297 // Refresh
16298 requestLayout();
16299 invalidate(true);
16300 }
16301 }
16302
16303 /**
16304 * Return the resolved text alignment.
16305 *
16306 * The resolved text alignment. This needs resolution if the value is
16307 * TEXT_ALIGNMENT_INHERIT. The resolution matches {@link #setTextAlignment(int)} if it is
16308 * not TEXT_ALIGNMENT_INHERIT, otherwise resolution proceeds up the parent chain of the view.
16309 *
16310 * @return the resolved text alignment. Returns one of:
16311 *
16312 * {@link #TEXT_ALIGNMENT_GRAVITY},
16313 * {@link #TEXT_ALIGNMENT_CENTER},
16314 * {@link #TEXT_ALIGNMENT_TEXT_START},
16315 * {@link #TEXT_ALIGNMENT_TEXT_END},
16316 * {@link #TEXT_ALIGNMENT_VIEW_START},
16317 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016318 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016319 */
16320 @ViewDebug.ExportedProperty(category = "text", mapping = {
16321 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16322 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16323 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16324 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16325 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16326 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16327 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16328 })
16329 public int getResolvedTextAlignment() {
16330 // If text alignment is not resolved, then resolve it
16331 if ((mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED) != TEXT_ALIGNMENT_RESOLVED) {
16332 resolveTextAlignment();
16333 }
16334 return (mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED_MASK) >> TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
16335 }
16336
16337 /**
16338 * Resolve the text alignment. Will call {@link View#onResolvedTextAlignmentChanged} when
16339 * resolution is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016340 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016341 */
16342 public void resolveTextAlignment() {
16343 // Reset any previous text alignment resolution
16344 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16345
16346 if (hasRtlSupport()) {
16347 // Set resolved text alignment flag depending on text alignment flag
16348 final int textAlignment = getTextAlignment();
16349 switch (textAlignment) {
16350 case TEXT_ALIGNMENT_INHERIT:
16351 // Check if we can resolve the text alignment
16352 if (canResolveLayoutDirection() && mParent instanceof View) {
16353 View view = (View) mParent;
16354
16355 final int parentResolvedTextAlignment = view.getResolvedTextAlignment();
16356 switch (parentResolvedTextAlignment) {
16357 case TEXT_ALIGNMENT_GRAVITY:
16358 case TEXT_ALIGNMENT_TEXT_START:
16359 case TEXT_ALIGNMENT_TEXT_END:
16360 case TEXT_ALIGNMENT_CENTER:
16361 case TEXT_ALIGNMENT_VIEW_START:
16362 case TEXT_ALIGNMENT_VIEW_END:
16363 // Resolved text alignment is the same as the parent resolved
16364 // text alignment
16365 mPrivateFlags2 |=
16366 (parentResolvedTextAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16367 break;
16368 default:
16369 // Use default resolved text alignment
16370 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16371 }
16372 }
16373 else {
16374 // We cannot do the resolution if there is no parent so use the default
16375 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16376 }
16377 break;
16378 case TEXT_ALIGNMENT_GRAVITY:
16379 case TEXT_ALIGNMENT_TEXT_START:
16380 case TEXT_ALIGNMENT_TEXT_END:
16381 case TEXT_ALIGNMENT_CENTER:
16382 case TEXT_ALIGNMENT_VIEW_START:
16383 case TEXT_ALIGNMENT_VIEW_END:
16384 // Resolved text alignment is the same as text alignment
16385 mPrivateFlags2 |= (textAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16386 break;
16387 default:
16388 // Use default resolved text alignment
16389 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16390 }
16391 } else {
16392 // Use default resolved text alignment
16393 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16394 }
16395
16396 // Set the resolved
16397 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED;
16398 onResolvedTextAlignmentChanged();
16399 }
16400
16401 /**
16402 * Check if text alignment resolution can be done.
16403 *
16404 * @return true if text alignment resolution can be done otherwise return false.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016405 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016406 */
16407 public boolean canResolveTextAlignment() {
16408 switch (getTextAlignment()) {
16409 case TEXT_DIRECTION_INHERIT:
16410 return (mParent != null);
16411 default:
16412 return true;
16413 }
16414 }
16415
16416 /**
16417 * Called when text alignment has been resolved. Subclasses that care about text alignment
16418 * resolution should override this method.
16419 *
16420 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016421 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016422 */
16423 public void onResolvedTextAlignmentChanged() {
16424 }
16425
16426 /**
16427 * Reset resolved text alignment. Text alignment can be resolved with a call to
16428 * getResolvedTextAlignment(). Will call {@link View#onResolvedTextAlignmentReset} when
16429 * reset is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016430 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016431 */
16432 public void resetResolvedTextAlignment() {
16433 // Reset any previous text alignment resolution
16434 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16435 onResolvedTextAlignmentReset();
16436 }
16437
16438 /**
16439 * Called when text alignment is reset. Subclasses that care about text alignment reset should
16440 * override this method and do a reset of the text alignment of their children. The default
16441 * implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016442 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016443 */
16444 public void onResolvedTextAlignmentReset() {
16445 }
16446
Chet Haaseb39f0512011-05-24 14:36:40 -070016447 //
16448 // Properties
16449 //
16450 /**
16451 * A Property wrapper around the <code>alpha</code> functionality handled by the
16452 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
16453 */
Chet Haased47f1532011-12-16 11:18:52 -080016454 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016455 @Override
16456 public void setValue(View object, float value) {
16457 object.setAlpha(value);
16458 }
16459
16460 @Override
16461 public Float get(View object) {
16462 return object.getAlpha();
16463 }
16464 };
16465
16466 /**
16467 * A Property wrapper around the <code>translationX</code> functionality handled by the
16468 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
16469 */
Chet Haased47f1532011-12-16 11:18:52 -080016470 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016471 @Override
16472 public void setValue(View object, float value) {
16473 object.setTranslationX(value);
16474 }
16475
16476 @Override
16477 public Float get(View object) {
16478 return object.getTranslationX();
16479 }
16480 };
16481
16482 /**
16483 * A Property wrapper around the <code>translationY</code> functionality handled by the
16484 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
16485 */
Chet Haased47f1532011-12-16 11:18:52 -080016486 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016487 @Override
16488 public void setValue(View object, float value) {
16489 object.setTranslationY(value);
16490 }
16491
16492 @Override
16493 public Float get(View object) {
16494 return object.getTranslationY();
16495 }
16496 };
16497
16498 /**
16499 * A Property wrapper around the <code>x</code> functionality handled by the
16500 * {@link View#setX(float)} and {@link View#getX()} methods.
16501 */
Chet Haased47f1532011-12-16 11:18:52 -080016502 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016503 @Override
16504 public void setValue(View object, float value) {
16505 object.setX(value);
16506 }
16507
16508 @Override
16509 public Float get(View object) {
16510 return object.getX();
16511 }
16512 };
16513
16514 /**
16515 * A Property wrapper around the <code>y</code> functionality handled by the
16516 * {@link View#setY(float)} and {@link View#getY()} methods.
16517 */
Chet Haased47f1532011-12-16 11:18:52 -080016518 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016519 @Override
16520 public void setValue(View object, float value) {
16521 object.setY(value);
16522 }
16523
16524 @Override
16525 public Float get(View object) {
16526 return object.getY();
16527 }
16528 };
16529
16530 /**
16531 * A Property wrapper around the <code>rotation</code> functionality handled by the
16532 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
16533 */
Chet Haased47f1532011-12-16 11:18:52 -080016534 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016535 @Override
16536 public void setValue(View object, float value) {
16537 object.setRotation(value);
16538 }
16539
16540 @Override
16541 public Float get(View object) {
16542 return object.getRotation();
16543 }
16544 };
16545
16546 /**
16547 * A Property wrapper around the <code>rotationX</code> functionality handled by the
16548 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
16549 */
Chet Haased47f1532011-12-16 11:18:52 -080016550 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016551 @Override
16552 public void setValue(View object, float value) {
16553 object.setRotationX(value);
16554 }
16555
16556 @Override
16557 public Float get(View object) {
16558 return object.getRotationX();
16559 }
16560 };
16561
16562 /**
16563 * A Property wrapper around the <code>rotationY</code> functionality handled by the
16564 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
16565 */
Chet Haased47f1532011-12-16 11:18:52 -080016566 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016567 @Override
16568 public void setValue(View object, float value) {
16569 object.setRotationY(value);
16570 }
16571
16572 @Override
16573 public Float get(View object) {
16574 return object.getRotationY();
16575 }
16576 };
16577
16578 /**
16579 * A Property wrapper around the <code>scaleX</code> functionality handled by the
16580 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
16581 */
Chet Haased47f1532011-12-16 11:18:52 -080016582 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016583 @Override
16584 public void setValue(View object, float value) {
16585 object.setScaleX(value);
16586 }
16587
16588 @Override
16589 public Float get(View object) {
16590 return object.getScaleX();
16591 }
16592 };
16593
16594 /**
16595 * A Property wrapper around the <code>scaleY</code> functionality handled by the
16596 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
16597 */
Chet Haased47f1532011-12-16 11:18:52 -080016598 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016599 @Override
16600 public void setValue(View object, float value) {
16601 object.setScaleY(value);
16602 }
16603
16604 @Override
16605 public Float get(View object) {
16606 return object.getScaleY();
16607 }
16608 };
16609
Jeff Brown33bbfd22011-02-24 20:55:35 -080016610 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016611 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
16612 * Each MeasureSpec represents a requirement for either the width or the height.
16613 * A MeasureSpec is comprised of a size and a mode. There are three possible
16614 * modes:
16615 * <dl>
16616 * <dt>UNSPECIFIED</dt>
16617 * <dd>
16618 * The parent has not imposed any constraint on the child. It can be whatever size
16619 * it wants.
16620 * </dd>
16621 *
16622 * <dt>EXACTLY</dt>
16623 * <dd>
16624 * The parent has determined an exact size for the child. The child is going to be
16625 * given those bounds regardless of how big it wants to be.
16626 * </dd>
16627 *
16628 * <dt>AT_MOST</dt>
16629 * <dd>
16630 * The child can be as large as it wants up to the specified size.
16631 * </dd>
16632 * </dl>
16633 *
16634 * MeasureSpecs are implemented as ints to reduce object allocation. This class
16635 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
16636 */
16637 public static class MeasureSpec {
16638 private static final int MODE_SHIFT = 30;
16639 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
16640
16641 /**
16642 * Measure specification mode: The parent has not imposed any constraint
16643 * on the child. It can be whatever size it wants.
16644 */
16645 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
16646
16647 /**
16648 * Measure specification mode: The parent has determined an exact size
16649 * for the child. The child is going to be given those bounds regardless
16650 * of how big it wants to be.
16651 */
16652 public static final int EXACTLY = 1 << MODE_SHIFT;
16653
16654 /**
16655 * Measure specification mode: The child can be as large as it wants up
16656 * to the specified size.
16657 */
16658 public static final int AT_MOST = 2 << MODE_SHIFT;
16659
16660 /**
16661 * Creates a measure specification based on the supplied size and mode.
16662 *
16663 * The mode must always be one of the following:
16664 * <ul>
16665 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
16666 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
16667 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
16668 * </ul>
16669 *
16670 * @param size the size of the measure specification
16671 * @param mode the mode of the measure specification
16672 * @return the measure specification based on size and mode
16673 */
16674 public static int makeMeasureSpec(int size, int mode) {
16675 return size + mode;
16676 }
16677
16678 /**
16679 * Extracts the mode from the supplied measure specification.
16680 *
16681 * @param measureSpec the measure specification to extract the mode from
16682 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
16683 * {@link android.view.View.MeasureSpec#AT_MOST} or
16684 * {@link android.view.View.MeasureSpec#EXACTLY}
16685 */
16686 public static int getMode(int measureSpec) {
16687 return (measureSpec & MODE_MASK);
16688 }
16689
16690 /**
16691 * Extracts the size from the supplied measure specification.
16692 *
16693 * @param measureSpec the measure specification to extract the size from
16694 * @return the size in pixels defined in the supplied measure specification
16695 */
16696 public static int getSize(int measureSpec) {
16697 return (measureSpec & ~MODE_MASK);
16698 }
16699
16700 /**
16701 * Returns a String representation of the specified measure
16702 * specification.
16703 *
16704 * @param measureSpec the measure specification to convert to a String
16705 * @return a String with the following format: "MeasureSpec: MODE SIZE"
16706 */
16707 public static String toString(int measureSpec) {
16708 int mode = getMode(measureSpec);
16709 int size = getSize(measureSpec);
16710
16711 StringBuilder sb = new StringBuilder("MeasureSpec: ");
16712
16713 if (mode == UNSPECIFIED)
16714 sb.append("UNSPECIFIED ");
16715 else if (mode == EXACTLY)
16716 sb.append("EXACTLY ");
16717 else if (mode == AT_MOST)
16718 sb.append("AT_MOST ");
16719 else
16720 sb.append(mode).append(" ");
16721
16722 sb.append(size);
16723 return sb.toString();
16724 }
16725 }
16726
16727 class CheckForLongPress implements Runnable {
16728
16729 private int mOriginalWindowAttachCount;
16730
16731 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070016732 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016733 && mOriginalWindowAttachCount == mWindowAttachCount) {
16734 if (performLongClick()) {
16735 mHasPerformedLongPress = true;
16736 }
16737 }
16738 }
16739
16740 public void rememberWindowAttachCount() {
16741 mOriginalWindowAttachCount = mWindowAttachCount;
16742 }
16743 }
Joe Malin32736f02011-01-19 16:14:20 -080016744
Adam Powelle14579b2009-12-16 18:39:52 -080016745 private final class CheckForTap implements Runnable {
16746 public void run() {
16747 mPrivateFlags &= ~PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080016748 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016749 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080016750 }
16751 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016752
Adam Powella35d7682010-03-12 14:48:13 -080016753 private final class PerformClick implements Runnable {
16754 public void run() {
16755 performClick();
16756 }
16757 }
16758
Dianne Hackborn63042d62011-01-26 18:56:29 -080016759 /** @hide */
16760 public void hackTurnOffWindowResizeAnim(boolean off) {
16761 mAttachInfo.mTurnOffWindowResizeAnim = off;
16762 }
Joe Malin32736f02011-01-19 16:14:20 -080016763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016764 /**
Chet Haasea00f3862011-02-22 06:34:40 -080016765 * This method returns a ViewPropertyAnimator object, which can be used to animate
16766 * specific properties on this View.
16767 *
16768 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
16769 */
16770 public ViewPropertyAnimator animate() {
16771 if (mAnimator == null) {
16772 mAnimator = new ViewPropertyAnimator(this);
16773 }
16774 return mAnimator;
16775 }
16776
16777 /**
Jean Chalard405bc512012-05-29 19:12:34 +090016778 * Interface definition for a callback to be invoked when a hardware key event is
16779 * dispatched to this view. The callback will be invoked before the key event is
16780 * given to the view. This is only useful for hardware keyboards; a software input
16781 * method has no obligation to trigger this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016782 */
16783 public interface OnKeyListener {
16784 /**
Jean Chalard405bc512012-05-29 19:12:34 +090016785 * Called when a hardware key is dispatched to a view. This allows listeners to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016786 * get a chance to respond before the target view.
Jean Chalard405bc512012-05-29 19:12:34 +090016787 * <p>Key presses in software keyboards will generally NOT trigger this method,
16788 * although some may elect to do so in some situations. Do not assume a
16789 * software input method has to be key-based; even if it is, it may use key presses
16790 * in a different way than you expect, so there is no way to reliably catch soft
16791 * input key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016792 *
16793 * @param v The view the key has been dispatched to.
16794 * @param keyCode The code for the physical key that was pressed
16795 * @param event The KeyEvent object containing full information about
16796 * the event.
16797 * @return True if the listener has consumed the event, false otherwise.
16798 */
16799 boolean onKey(View v, int keyCode, KeyEvent event);
16800 }
16801
16802 /**
16803 * Interface definition for a callback to be invoked when a touch event is
16804 * dispatched to this view. The callback will be invoked before the touch
16805 * event is given to the view.
16806 */
16807 public interface OnTouchListener {
16808 /**
16809 * Called when a touch event is dispatched to a view. This allows listeners to
16810 * get a chance to respond before the target view.
16811 *
16812 * @param v The view the touch event has been dispatched to.
16813 * @param event The MotionEvent object containing full information about
16814 * the event.
16815 * @return True if the listener has consumed the event, false otherwise.
16816 */
16817 boolean onTouch(View v, MotionEvent event);
16818 }
16819
16820 /**
Jeff Brown10b62902011-06-20 16:40:37 -070016821 * Interface definition for a callback to be invoked when a hover event is
16822 * dispatched to this view. The callback will be invoked before the hover
16823 * event is given to the view.
16824 */
16825 public interface OnHoverListener {
16826 /**
16827 * Called when a hover event is dispatched to a view. This allows listeners to
16828 * get a chance to respond before the target view.
16829 *
16830 * @param v The view the hover event has been dispatched to.
16831 * @param event The MotionEvent object containing full information about
16832 * the event.
16833 * @return True if the listener has consumed the event, false otherwise.
16834 */
16835 boolean onHover(View v, MotionEvent event);
16836 }
16837
16838 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016839 * Interface definition for a callback to be invoked when a generic motion event is
16840 * dispatched to this view. The callback will be invoked before the generic motion
16841 * event is given to the view.
16842 */
16843 public interface OnGenericMotionListener {
16844 /**
16845 * Called when a generic motion event is dispatched to a view. This allows listeners to
16846 * get a chance to respond before the target view.
16847 *
16848 * @param v The view the generic motion event has been dispatched to.
16849 * @param event The MotionEvent object containing full information about
16850 * the event.
16851 * @return True if the listener has consumed the event, false otherwise.
16852 */
16853 boolean onGenericMotion(View v, MotionEvent event);
16854 }
16855
16856 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016857 * Interface definition for a callback to be invoked when a view has been clicked and held.
16858 */
16859 public interface OnLongClickListener {
16860 /**
16861 * Called when a view has been clicked and held.
16862 *
16863 * @param v The view that was clicked and held.
16864 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080016865 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016866 */
16867 boolean onLongClick(View v);
16868 }
16869
16870 /**
Chris Tate32affef2010-10-18 15:29:21 -070016871 * Interface definition for a callback to be invoked when a drag is being dispatched
16872 * to this view. The callback will be invoked before the hosting view's own
16873 * onDrag(event) method. If the listener wants to fall back to the hosting view's
16874 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070016875 *
16876 * <div class="special reference">
16877 * <h3>Developer Guides</h3>
16878 * <p>For a guide to implementing drag and drop features, read the
16879 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
16880 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070016881 */
16882 public interface OnDragListener {
16883 /**
16884 * Called when a drag event is dispatched to a view. This allows listeners
16885 * to get a chance to override base View behavior.
16886 *
Joe Malin32736f02011-01-19 16:14:20 -080016887 * @param v The View that received the drag event.
16888 * @param event The {@link android.view.DragEvent} object for the drag event.
16889 * @return {@code true} if the drag event was handled successfully, or {@code false}
16890 * if the drag event was not handled. Note that {@code false} will trigger the View
16891 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070016892 */
16893 boolean onDrag(View v, DragEvent event);
16894 }
16895
16896 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016897 * Interface definition for a callback to be invoked when the focus state of
16898 * a view changed.
16899 */
16900 public interface OnFocusChangeListener {
16901 /**
16902 * Called when the focus state of a view has changed.
16903 *
16904 * @param v The view whose state has changed.
16905 * @param hasFocus The new focus state of v.
16906 */
16907 void onFocusChange(View v, boolean hasFocus);
16908 }
16909
16910 /**
16911 * Interface definition for a callback to be invoked when a view is clicked.
16912 */
16913 public interface OnClickListener {
16914 /**
16915 * Called when a view has been clicked.
16916 *
16917 * @param v The view that was clicked.
16918 */
16919 void onClick(View v);
16920 }
16921
16922 /**
16923 * Interface definition for a callback to be invoked when the context menu
16924 * for this view is being built.
16925 */
16926 public interface OnCreateContextMenuListener {
16927 /**
16928 * Called when the context menu for this view is being built. It is not
16929 * safe to hold onto the menu after this method returns.
16930 *
16931 * @param menu The context menu that is being built
16932 * @param v The view for which the context menu is being built
16933 * @param menuInfo Extra information about the item for which the
16934 * context menu should be shown. This information will vary
16935 * depending on the class of v.
16936 */
16937 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
16938 }
16939
Joe Onorato664644d2011-01-23 17:53:23 -080016940 /**
16941 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016942 * visibility. This reports <strong>global</strong> changes to the system UI
Dianne Hackborncf675782012-05-10 15:07:24 -070016943 * state, not what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080016944 *
Philip Milne6c8ea062012-04-03 17:38:43 -070016945 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080016946 */
16947 public interface OnSystemUiVisibilityChangeListener {
16948 /**
16949 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070016950 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080016951 *
Dianne Hackborncf675782012-05-10 15:07:24 -070016952 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
16953 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, and {@link #SYSTEM_UI_FLAG_FULLSCREEN}.
16954 * This tells you the <strong>global</strong> state of these UI visibility
16955 * flags, not what your app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080016956 */
16957 public void onSystemUiVisibilityChange(int visibility);
16958 }
16959
Adam Powell4afd62b2011-02-18 15:02:18 -080016960 /**
16961 * Interface definition for a callback to be invoked when this view is attached
16962 * or detached from its window.
16963 */
16964 public interface OnAttachStateChangeListener {
16965 /**
16966 * Called when the view is attached to a window.
16967 * @param v The view that was attached
16968 */
16969 public void onViewAttachedToWindow(View v);
16970 /**
16971 * Called when the view is detached from a window.
16972 * @param v The view that was detached
16973 */
16974 public void onViewDetachedFromWindow(View v);
16975 }
16976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016977 private final class UnsetPressedState implements Runnable {
16978 public void run() {
16979 setPressed(false);
16980 }
16981 }
16982
16983 /**
16984 * Base class for derived classes that want to save and restore their own
16985 * state in {@link android.view.View#onSaveInstanceState()}.
16986 */
16987 public static class BaseSavedState extends AbsSavedState {
16988 /**
16989 * Constructor used when reading from a parcel. Reads the state of the superclass.
16990 *
16991 * @param source
16992 */
16993 public BaseSavedState(Parcel source) {
16994 super(source);
16995 }
16996
16997 /**
16998 * Constructor called by derived classes when creating their SavedState objects
16999 *
17000 * @param superState The state of the superclass of this view
17001 */
17002 public BaseSavedState(Parcelable superState) {
17003 super(superState);
17004 }
17005
17006 public static final Parcelable.Creator<BaseSavedState> CREATOR =
17007 new Parcelable.Creator<BaseSavedState>() {
17008 public BaseSavedState createFromParcel(Parcel in) {
17009 return new BaseSavedState(in);
17010 }
17011
17012 public BaseSavedState[] newArray(int size) {
17013 return new BaseSavedState[size];
17014 }
17015 };
17016 }
17017
17018 /**
17019 * A set of information given to a view when it is attached to its parent
17020 * window.
17021 */
17022 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017023 interface Callbacks {
17024 void playSoundEffect(int effectId);
17025 boolean performHapticFeedback(int effectId, boolean always);
17026 }
17027
17028 /**
17029 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
17030 * to a Handler. This class contains the target (View) to invalidate and
17031 * the coordinates of the dirty rectangle.
17032 *
17033 * For performance purposes, this class also implements a pool of up to
17034 * POOL_LIMIT objects that get reused. This reduces memory allocations
17035 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017036 */
Romain Guyd928d682009-03-31 17:52:16 -070017037 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017038 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070017039 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
17040 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070017041 public InvalidateInfo newInstance() {
17042 return new InvalidateInfo();
17043 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017044
Romain Guyd928d682009-03-31 17:52:16 -070017045 public void onAcquired(InvalidateInfo element) {
17046 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017047
Romain Guyd928d682009-03-31 17:52:16 -070017048 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070017049 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070017050 }
17051 }, POOL_LIMIT)
17052 );
17053
17054 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017055 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017056
17057 View target;
17058
17059 int left;
17060 int top;
17061 int right;
17062 int bottom;
17063
Romain Guyd928d682009-03-31 17:52:16 -070017064 public void setNextPoolable(InvalidateInfo element) {
17065 mNext = element;
17066 }
17067
17068 public InvalidateInfo getNextPoolable() {
17069 return mNext;
17070 }
17071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017072 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070017073 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017074 }
17075
17076 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070017077 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017078 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017079
17080 public boolean isPooled() {
17081 return mIsPooled;
17082 }
17083
17084 public void setPooled(boolean isPooled) {
17085 mIsPooled = isPooled;
17086 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017087 }
17088
17089 final IWindowSession mSession;
17090
17091 final IWindow mWindow;
17092
17093 final IBinder mWindowToken;
17094
17095 final Callbacks mRootCallbacks;
17096
Romain Guy59a12ca2011-06-09 17:48:21 -070017097 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080017098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017099 /**
17100 * The top view of the hierarchy.
17101 */
17102 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070017103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017104 IBinder mPanelParentWindowToken;
17105 Surface mSurface;
17106
Romain Guyb051e892010-09-28 19:09:36 -070017107 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080017108 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070017109 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080017110
Romain Guy7e4e5612012-03-05 14:37:29 -080017111 boolean mScreenOn;
17112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017113 /**
Romain Guy8506ab42009-06-11 17:35:47 -070017114 * Scale factor used by the compatibility mode
17115 */
17116 float mApplicationScale;
17117
17118 /**
17119 * Indicates whether the application is in compatibility mode
17120 */
17121 boolean mScalingRequired;
17122
17123 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017124 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080017125 */
17126 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080017127
Dianne Hackborn63042d62011-01-26 18:56:29 -080017128 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017129 * Left position of this view's window
17130 */
17131 int mWindowLeft;
17132
17133 /**
17134 * Top position of this view's window
17135 */
17136 int mWindowTop;
17137
17138 /**
Adam Powell26153a32010-11-08 15:22:27 -080017139 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070017140 */
Adam Powell26153a32010-11-08 15:22:27 -080017141 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070017142
17143 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017144 * For windows that are full-screen but using insets to layout inside
17145 * of the screen decorations, these are the current insets for the
17146 * content of the window.
17147 */
17148 final Rect mContentInsets = new Rect();
17149
17150 /**
17151 * For windows that are full-screen but using insets to layout inside
17152 * of the screen decorations, these are the current insets for the
17153 * actual visible parts of the window.
17154 */
17155 final Rect mVisibleInsets = new Rect();
17156
17157 /**
17158 * The internal insets given by this window. This value is
17159 * supplied by the client (through
17160 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
17161 * be given to the window manager when changed to be used in laying
17162 * out windows behind it.
17163 */
17164 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
17165 = new ViewTreeObserver.InternalInsetsInfo();
17166
17167 /**
17168 * All views in the window's hierarchy that serve as scroll containers,
17169 * used to determine if the window can be resized or must be panned
17170 * to adjust for a soft input area.
17171 */
17172 final ArrayList<View> mScrollContainers = new ArrayList<View>();
17173
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070017174 final KeyEvent.DispatcherState mKeyDispatchState
17175 = new KeyEvent.DispatcherState();
17176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017177 /**
17178 * Indicates whether the view's window currently has the focus.
17179 */
17180 boolean mHasWindowFocus;
17181
17182 /**
17183 * The current visibility of the window.
17184 */
17185 int mWindowVisibility;
17186
17187 /**
17188 * Indicates the time at which drawing started to occur.
17189 */
17190 long mDrawingTime;
17191
17192 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070017193 * Indicates whether or not ignoring the DIRTY_MASK flags.
17194 */
17195 boolean mIgnoreDirtyState;
17196
17197 /**
Romain Guy02ccac62011-06-24 13:20:23 -070017198 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
17199 * to avoid clearing that flag prematurely.
17200 */
17201 boolean mSetIgnoreDirtyState = false;
17202
17203 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017204 * Indicates whether the view's window is currently in touch mode.
17205 */
17206 boolean mInTouchMode;
17207
17208 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017209 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017210 * the next time it performs a traversal
17211 */
17212 boolean mRecomputeGlobalAttributes;
17213
17214 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017215 * Always report new attributes at next traversal.
17216 */
17217 boolean mForceReportNewAttributes;
17218
17219 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017220 * Set during a traveral if any views want to keep the screen on.
17221 */
17222 boolean mKeepScreenOn;
17223
17224 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017225 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
17226 */
17227 int mSystemUiVisibility;
17228
17229 /**
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070017230 * Hack to force certain system UI visibility flags to be cleared.
17231 */
17232 int mDisabledSystemUiVisibility;
17233
17234 /**
Dianne Hackborncf675782012-05-10 15:07:24 -070017235 * Last global system UI visibility reported by the window manager.
17236 */
17237 int mGlobalSystemUiVisibility;
17238
17239 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017240 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
17241 * attached.
17242 */
17243 boolean mHasSystemUiListeners;
17244
17245 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017246 * Set if the visibility of any views has changed.
17247 */
17248 boolean mViewVisibilityChanged;
17249
17250 /**
17251 * Set to true if a view has been scrolled.
17252 */
17253 boolean mViewScrollChanged;
17254
17255 /**
17256 * Global to the view hierarchy used as a temporary for dealing with
17257 * x/y points in the transparent region computations.
17258 */
17259 final int[] mTransparentLocation = new int[2];
17260
17261 /**
17262 * Global to the view hierarchy used as a temporary for dealing with
17263 * x/y points in the ViewGroup.invalidateChild implementation.
17264 */
17265 final int[] mInvalidateChildLocation = new int[2];
17266
Chet Haasec3aa3612010-06-17 08:50:37 -070017267
17268 /**
17269 * Global to the view hierarchy used as a temporary for dealing with
17270 * x/y location when view is transformed.
17271 */
17272 final float[] mTmpTransformLocation = new float[2];
17273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017274 /**
17275 * The view tree observer used to dispatch global events like
17276 * layout, pre-draw, touch mode change, etc.
17277 */
17278 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
17279
17280 /**
17281 * A Canvas used by the view hierarchy to perform bitmap caching.
17282 */
17283 Canvas mCanvas;
17284
17285 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080017286 * The view root impl.
17287 */
17288 final ViewRootImpl mViewRootImpl;
17289
17290 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070017291 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017292 * handler can be used to pump events in the UI events queue.
17293 */
17294 final Handler mHandler;
17295
17296 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017297 * Temporary for use in computing invalidate rectangles while
17298 * calling up the hierarchy.
17299 */
17300 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070017301
17302 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070017303 * Temporary for use in computing hit areas with transformed views
17304 */
17305 final RectF mTmpTransformRect = new RectF();
17306
17307 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070017308 * Temporary list for use in collecting focusable descendents of a view.
17309 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070017310 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070017311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017312 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017313 * The id of the window for accessibility purposes.
17314 */
17315 int mAccessibilityWindowId = View.NO_ID;
17316
17317 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -070017318 * Whether to ingore not exposed for accessibility Views when
17319 * reporting the view tree to accessibility services.
17320 */
17321 boolean mIncludeNotImportantViews;
17322
17323 /**
17324 * The drawable for highlighting accessibility focus.
17325 */
17326 Drawable mAccessibilityFocusDrawable;
17327
17328 /**
Philip Milne10ca24a2012-04-23 15:38:27 -070017329 * Show where the margins, bounds and layout bounds are for each view.
17330 */
Dianne Hackborna53de062012-05-08 18:53:51 -070017331 boolean mDebugLayout = SystemProperties.getBoolean(DEBUG_LAYOUT_PROPERTY, false);
Philip Milne10ca24a2012-04-23 15:38:27 -070017332
17333 /**
Romain Guyab4c4f4f2012-05-06 13:11:24 -070017334 * Point used to compute visible regions.
17335 */
17336 final Point mPoint = new Point();
17337
17338 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017339 * Creates a new set of attachment information with the specified
17340 * events handler and thread.
17341 *
17342 * @param handler the events handler the view must use
17343 */
17344 AttachInfo(IWindowSession session, IWindow window,
Jeff Browna175a5b2012-02-15 19:18:31 -080017345 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017346 mSession = session;
17347 mWindow = window;
17348 mWindowToken = window.asBinder();
Jeff Browna175a5b2012-02-15 19:18:31 -080017349 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017350 mHandler = handler;
17351 mRootCallbacks = effectPlayer;
17352 }
17353 }
17354
17355 /**
17356 * <p>ScrollabilityCache holds various fields used by a View when scrolling
17357 * is supported. This avoids keeping too many unused fields in most
17358 * instances of View.</p>
17359 */
Mike Cleronf116bf82009-09-27 19:14:12 -070017360 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080017361
Mike Cleronf116bf82009-09-27 19:14:12 -070017362 /**
17363 * Scrollbars are not visible
17364 */
17365 public static final int OFF = 0;
17366
17367 /**
17368 * Scrollbars are visible
17369 */
17370 public static final int ON = 1;
17371
17372 /**
17373 * Scrollbars are fading away
17374 */
17375 public static final int FADING = 2;
17376
17377 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080017378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017379 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070017380 public int scrollBarDefaultDelayBeforeFade;
17381 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017382
17383 public int scrollBarSize;
17384 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070017385 public float[] interpolatorValues;
17386 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017387
17388 public final Paint paint;
17389 public final Matrix matrix;
17390 public Shader shader;
17391
Mike Cleronf116bf82009-09-27 19:14:12 -070017392 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
17393
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017394 private static final float[] OPAQUE = { 255 };
17395 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080017396
Mike Cleronf116bf82009-09-27 19:14:12 -070017397 /**
17398 * When fading should start. This time moves into the future every time
17399 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
17400 */
17401 public long fadeStartTime;
17402
17403
17404 /**
17405 * The current state of the scrollbars: ON, OFF, or FADING
17406 */
17407 public int state = OFF;
17408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017409 private int mLastColor;
17410
Mike Cleronf116bf82009-09-27 19:14:12 -070017411 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017412 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
17413 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070017414 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
17415 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017416
17417 paint = new Paint();
17418 matrix = new Matrix();
17419 // use use a height of 1, and then wack the matrix each time we
17420 // actually use it.
17421 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017423 paint.setShader(shader);
17424 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070017425 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017426 }
Romain Guy8506ab42009-06-11 17:35:47 -070017427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017428 public void setFadeColor(int color) {
17429 if (color != 0 && color != mLastColor) {
17430 mLastColor = color;
17431 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070017432
Romain Guye55e1a72009-08-27 10:42:26 -070017433 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
17434 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017436 paint.setShader(shader);
17437 // Restore the default transfer mode (src_over)
17438 paint.setXfermode(null);
17439 }
17440 }
Joe Malin32736f02011-01-19 16:14:20 -080017441
Mike Cleronf116bf82009-09-27 19:14:12 -070017442 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070017443 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070017444 if (now >= fadeStartTime) {
17445
17446 // the animation fades the scrollbars out by changing
17447 // the opacity (alpha) from fully opaque to fully
17448 // transparent
17449 int nextFrame = (int) now;
17450 int framesCount = 0;
17451
17452 Interpolator interpolator = scrollBarInterpolator;
17453
17454 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017455 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070017456
17457 // End transparent
17458 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017459 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070017460
17461 state = FADING;
17462
17463 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080017464 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070017465 }
17466 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070017467 }
Mike Cleronf116bf82009-09-27 19:14:12 -070017468
Svetoslav Ganova0156172011-06-26 17:55:44 -070017469 /**
17470 * Resuable callback for sending
17471 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
17472 */
17473 private class SendViewScrolledAccessibilityEvent implements Runnable {
17474 public volatile boolean mIsPending;
17475
17476 public void run() {
17477 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
17478 mIsPending = false;
17479 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017480 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017481
17482 /**
17483 * <p>
17484 * This class represents a delegate that can be registered in a {@link View}
17485 * to enhance accessibility support via composition rather via inheritance.
17486 * It is specifically targeted to widget developers that extend basic View
17487 * classes i.e. classes in package android.view, that would like their
17488 * applications to be backwards compatible.
17489 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080017490 * <div class="special reference">
17491 * <h3>Developer Guides</h3>
17492 * <p>For more information about making applications accessible, read the
17493 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
17494 * developer guide.</p>
17495 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017496 * <p>
17497 * A scenario in which a developer would like to use an accessibility delegate
17498 * is overriding a method introduced in a later API version then the minimal API
17499 * version supported by the application. For example, the method
17500 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
17501 * in API version 4 when the accessibility APIs were first introduced. If a
17502 * developer would like his application to run on API version 4 devices (assuming
17503 * all other APIs used by the application are version 4 or lower) and take advantage
17504 * of this method, instead of overriding the method which would break the application's
17505 * backwards compatibility, he can override the corresponding method in this
17506 * delegate and register the delegate in the target View if the API version of
17507 * the system is high enough i.e. the API version is same or higher to the API
17508 * version that introduced
17509 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
17510 * </p>
17511 * <p>
17512 * Here is an example implementation:
17513 * </p>
17514 * <code><pre><p>
17515 * if (Build.VERSION.SDK_INT >= 14) {
17516 * // If the API version is equal of higher than the version in
17517 * // which onInitializeAccessibilityNodeInfo was introduced we
17518 * // register a delegate with a customized implementation.
17519 * View view = findViewById(R.id.view_id);
17520 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
17521 * public void onInitializeAccessibilityNodeInfo(View host,
17522 * AccessibilityNodeInfo info) {
17523 * // Let the default implementation populate the info.
17524 * super.onInitializeAccessibilityNodeInfo(host, info);
17525 * // Set some other information.
17526 * info.setEnabled(host.isEnabled());
17527 * }
17528 * });
17529 * }
17530 * </code></pre></p>
17531 * <p>
17532 * This delegate contains methods that correspond to the accessibility methods
17533 * in View. If a delegate has been specified the implementation in View hands
17534 * off handling to the corresponding method in this delegate. The default
17535 * implementation the delegate methods behaves exactly as the corresponding
17536 * method in View for the case of no accessibility delegate been set. Hence,
17537 * to customize the behavior of a View method, clients can override only the
17538 * corresponding delegate method without altering the behavior of the rest
17539 * accessibility related methods of the host view.
17540 * </p>
17541 */
17542 public static class AccessibilityDelegate {
17543
17544 /**
17545 * Sends an accessibility event of the given type. If accessibility is not
17546 * enabled this method has no effect.
17547 * <p>
17548 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
17549 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
17550 * been set.
17551 * </p>
17552 *
17553 * @param host The View hosting the delegate.
17554 * @param eventType The type of the event to send.
17555 *
17556 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
17557 */
17558 public void sendAccessibilityEvent(View host, int eventType) {
17559 host.sendAccessibilityEventInternal(eventType);
17560 }
17561
17562 /**
alanv8eeefef2012-05-07 16:57:53 -070017563 * Performs the specified accessibility action on the view. For
17564 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
17565 * <p>
17566 * The default implementation behaves as
17567 * {@link View#performAccessibilityAction(int, Bundle)
17568 * View#performAccessibilityAction(int, Bundle)} for the case of
17569 * no accessibility delegate been set.
17570 * </p>
17571 *
17572 * @param action The action to perform.
17573 * @return Whether the action was performed.
17574 *
17575 * @see View#performAccessibilityAction(int, Bundle)
17576 * View#performAccessibilityAction(int, Bundle)
17577 */
17578 public boolean performAccessibilityAction(View host, int action, Bundle args) {
17579 return host.performAccessibilityActionInternal(action, args);
17580 }
17581
17582 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017583 * Sends an accessibility event. This method behaves exactly as
17584 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
17585 * empty {@link AccessibilityEvent} and does not perform a check whether
17586 * accessibility is enabled.
17587 * <p>
17588 * The default implementation behaves as
17589 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17590 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
17591 * the case of no accessibility delegate been set.
17592 * </p>
17593 *
17594 * @param host The View hosting the delegate.
17595 * @param event The event to send.
17596 *
17597 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17598 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17599 */
17600 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
17601 host.sendAccessibilityEventUncheckedInternal(event);
17602 }
17603
17604 /**
17605 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
17606 * to its children for adding their text content to the event.
17607 * <p>
17608 * The default implementation behaves as
17609 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17610 * View#dispatchPopulateAccessibilityEvent(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 event.
17616 * @return True if the event population was completed.
17617 *
17618 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17619 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17620 */
17621 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17622 return host.dispatchPopulateAccessibilityEventInternal(event);
17623 }
17624
17625 /**
17626 * Gives a chance to the host View to populate the accessibility event with its
17627 * text content.
17628 * <p>
17629 * The default implementation behaves as
17630 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
17631 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
17632 * the case of no accessibility delegate been set.
17633 * </p>
17634 *
17635 * @param host The View hosting the delegate.
17636 * @param event The accessibility event which to populate.
17637 *
17638 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
17639 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
17640 */
17641 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17642 host.onPopulateAccessibilityEventInternal(event);
17643 }
17644
17645 /**
17646 * Initializes an {@link AccessibilityEvent} with information about the
17647 * the host View which is the event source.
17648 * <p>
17649 * The default implementation behaves as
17650 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
17651 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
17652 * the case of no accessibility delegate been set.
17653 * </p>
17654 *
17655 * @param host The View hosting the delegate.
17656 * @param event The event to initialize.
17657 *
17658 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
17659 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
17660 */
17661 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
17662 host.onInitializeAccessibilityEventInternal(event);
17663 }
17664
17665 /**
17666 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
17667 * <p>
17668 * The default implementation behaves as
17669 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17670 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
17671 * the case of no accessibility delegate been set.
17672 * </p>
17673 *
17674 * @param host The View hosting the delegate.
17675 * @param info The instance to initialize.
17676 *
17677 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17678 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17679 */
17680 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
17681 host.onInitializeAccessibilityNodeInfoInternal(info);
17682 }
17683
17684 /**
17685 * Called when a child of the host View has requested sending an
17686 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
17687 * to augment the event.
17688 * <p>
17689 * The default implementation behaves as
17690 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17691 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
17692 * the case of no accessibility delegate been set.
17693 * </p>
17694 *
17695 * @param host The View hosting the delegate.
17696 * @param child The child which requests sending the event.
17697 * @param event The event to be sent.
17698 * @return True if the event should be sent
17699 *
17700 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17701 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17702 */
17703 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
17704 AccessibilityEvent event) {
17705 return host.onRequestSendAccessibilityEventInternal(child, event);
17706 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070017707
17708 /**
17709 * Gets the provider for managing a virtual view hierarchy rooted at this View
17710 * and reported to {@link android.accessibilityservice.AccessibilityService}s
17711 * that explore the window content.
17712 * <p>
17713 * The default implementation behaves as
17714 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
17715 * the case of no accessibility delegate been set.
17716 * </p>
17717 *
17718 * @return The provider.
17719 *
17720 * @see AccessibilityNodeProvider
17721 */
17722 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
17723 return null;
17724 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017725 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017726}