blob: 0dc58a27dae9c9934de947e173d7c631a9292ff2 [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 Meglio30a21e12012-03-12 13:12:19 -0700346 * {@link #setPadding(int, int, int, int)} or {@link #setPaddingRelative(int, int, int, int)}
347 * method and queried by calling {@link #getPaddingLeft()}, {@link #getPaddingTop()},
348 * {@link #getPaddingRight()}, {@link #getPaddingBottom()}, {@link #getPaddingStart()},
349 * {@link #getPaddingEnd()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 * </p>
351 *
352 * <p>
353 * Even though a view can define a padding, it does not provide any support for
354 * margins. However, view groups provide such a support. Refer to
355 * {@link android.view.ViewGroup} and
356 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
357 * </p>
358 *
359 * <a name="Layout"></a>
360 * <h3>Layout</h3>
361 * <p>
362 * Layout is a two pass process: a measure pass and a layout pass. The measuring
363 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
364 * of the view tree. Each view pushes dimension specifications down the tree
365 * during the recursion. At the end of the measure pass, every view has stored
366 * its measurements. The second pass happens in
367 * {@link #layout(int,int,int,int)} and is also top-down. During
368 * this pass each parent is responsible for positioning all of its children
369 * using the sizes computed in the measure pass.
370 * </p>
371 *
372 * <p>
373 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
374 * {@link #getMeasuredHeight()} values must be set, along with those for all of
375 * that view's descendants. A view's measured width and measured height values
376 * must respect the constraints imposed by the view's parents. This guarantees
377 * that at the end of the measure pass, all parents accept all of their
378 * children's measurements. A parent view may call measure() more than once on
379 * its children. For example, the parent may measure each child once with
380 * unspecified dimensions to find out how big they want to be, then call
381 * measure() on them again with actual numbers if the sum of all the children's
382 * unconstrained sizes is too big or too small.
383 * </p>
384 *
385 * <p>
386 * The measure pass uses two classes to communicate dimensions. The
387 * {@link MeasureSpec} class is used by views to tell their parents how they
388 * want to be measured and positioned. The base LayoutParams class just
389 * describes how big the view wants to be for both width and height. For each
390 * dimension, it can specify one of:
391 * <ul>
392 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800393 * <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 -0800394 * (minus padding)
395 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
396 * enclose its content (plus padding).
397 * </ul>
398 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
399 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
400 * an X and Y value.
401 * </p>
402 *
403 * <p>
404 * MeasureSpecs are used to push requirements down the tree from parent to
405 * child. A MeasureSpec can be in one of three modes:
406 * <ul>
407 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
408 * of a child view. For example, a LinearLayout may call measure() on its child
409 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
410 * tall the child view wants to be given a width of 240 pixels.
411 * <li>EXACTLY: This is used by the parent to impose an exact size on the
412 * child. The child must use this size, and guarantee that all of its
413 * descendants will fit within this size.
414 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
415 * child. The child must gurantee that it and all of its descendants will fit
416 * within this size.
417 * </ul>
418 * </p>
419 *
420 * <p>
421 * To intiate a layout, call {@link #requestLayout}. This method is typically
422 * called by a view on itself when it believes that is can no longer fit within
423 * its current bounds.
424 * </p>
425 *
426 * <a name="Drawing"></a>
427 * <h3>Drawing</h3>
428 * <p>
429 * Drawing is handled by walking the tree and rendering each view that
Joe Fernandez558459f2011-10-13 16:47:36 -0700430 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 * this means that parents will draw before (i.e., behind) their children, with
432 * siblings drawn in the order they appear in the tree.
433 * If you set a background drawable for a View, then the View will draw it for you
434 * before calling back to its <code>onDraw()</code> method.
435 * </p>
436 *
437 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700438 * 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 -0800439 * </p>
440 *
441 * <p>
442 * To force a view to draw, call {@link #invalidate()}.
443 * </p>
444 *
445 * <a name="EventHandlingThreading"></a>
446 * <h3>Event Handling and Threading</h3>
447 * <p>
448 * The basic cycle of a view is as follows:
449 * <ol>
450 * <li>An event comes in and is dispatched to the appropriate view. The view
451 * handles the event and notifies any listeners.</li>
452 * <li>If in the course of processing the event, the view's bounds may need
453 * to be changed, the view will call {@link #requestLayout()}.</li>
454 * <li>Similarly, if in the course of processing the event the view's appearance
455 * may need to be changed, the view will call {@link #invalidate()}.</li>
456 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
457 * the framework will take care of measuring, laying out, and drawing the tree
458 * as appropriate.</li>
459 * </ol>
460 * </p>
461 *
462 * <p><em>Note: The entire view tree is single threaded. You must always be on
463 * the UI thread when calling any method on any view.</em>
464 * If you are doing work on other threads and want to update the state of a view
465 * from that thread, you should use a {@link Handler}.
466 * </p>
467 *
468 * <a name="FocusHandling"></a>
469 * <h3>Focus Handling</h3>
470 * <p>
471 * The framework will handle routine focus movement in response to user input.
472 * This includes changing the focus as views are removed or hidden, or as new
473 * views become available. Views indicate their willingness to take focus
474 * through the {@link #isFocusable} method. To change whether a view can take
475 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
476 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
477 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
478 * </p>
479 * <p>
480 * Focus movement is based on an algorithm which finds the nearest neighbor in a
481 * given direction. In rare cases, the default algorithm may not match the
482 * intended behavior of the developer. In these situations, you can provide
483 * explicit overrides by using these XML attributes in the layout file:
484 * <pre>
485 * nextFocusDown
486 * nextFocusLeft
487 * nextFocusRight
488 * nextFocusUp
489 * </pre>
490 * </p>
491 *
492 *
493 * <p>
494 * To get a particular view to take focus, call {@link #requestFocus()}.
495 * </p>
496 *
497 * <a name="TouchMode"></a>
498 * <h3>Touch Mode</h3>
499 * <p>
500 * When a user is navigating a user interface via directional keys such as a D-pad, it is
501 * necessary to give focus to actionable items such as buttons so the user can see
502 * what will take input. If the device has touch capabilities, however, and the user
503 * begins interacting with the interface by touching it, it is no longer necessary to
504 * always highlight, or give focus to, a particular view. This motivates a mode
505 * for interaction named 'touch mode'.
506 * </p>
507 * <p>
508 * For a touch capable device, once the user touches the screen, the device
509 * will enter touch mode. From this point onward, only views for which
510 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
511 * Other views that are touchable, like buttons, will not take focus when touched; they will
512 * only fire the on click listeners.
513 * </p>
514 * <p>
515 * Any time a user hits a directional key, such as a D-pad direction, the view device will
516 * exit touch mode, and find a view to take focus, so that the user may resume interacting
517 * with the user interface without touching the screen again.
518 * </p>
519 * <p>
520 * The touch mode state is maintained across {@link android.app.Activity}s. Call
521 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
522 * </p>
523 *
524 * <a name="Scrolling"></a>
525 * <h3>Scrolling</h3>
526 * <p>
527 * The framework provides basic support for views that wish to internally
528 * scroll their content. This includes keeping track of the X and Y scroll
529 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800530 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700531 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 * </p>
533 *
534 * <a name="Tags"></a>
535 * <h3>Tags</h3>
536 * <p>
537 * Unlike IDs, tags are not used to identify views. Tags are essentially an
538 * extra piece of information that can be associated with a view. They are most
539 * often used as a convenience to store data related to views in the views
540 * themselves rather than by putting them in a separate structure.
541 * </p>
542 *
Chet Haasecb150fe2012-05-03 15:15:05 -0700543 * <a name="Properties"></a>
544 * <h3>Properties</h3>
545 * <p>
546 * The View class exposes an {@link #ALPHA} property, as well as several transform-related
547 * properties, such as {@link #TRANSLATION_X} and {@link #TRANSLATION_Y}. These properties are
548 * available both in the {@link Property} form as well as in similarly-named setter/getter
549 * methods (such as {@link #setAlpha(float)} for {@link #ALPHA}). These properties can
550 * be used to set persistent state associated with these rendering-related properties on the view.
551 * The properties and methods can also be used in conjunction with
552 * {@link android.animation.Animator Animator}-based animations, described more in the
553 * <a href="#Animation">Animation</a> section.
554 * </p>
555 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 * <a name="Animation"></a>
557 * <h3>Animation</h3>
558 * <p>
Chet Haasecb150fe2012-05-03 15:15:05 -0700559 * Starting with Android 3.0, the preferred way of animating views is to use the
560 * {@link android.animation} package APIs. These {@link android.animation.Animator Animator}-based
561 * classes change actual properties of the View object, such as {@link #setAlpha(float) alpha} and
562 * {@link #setTranslationX(float) translationX}. This behavior is contrasted to that of the pre-3.0
563 * {@link android.view.animation.Animation Animation}-based classes, which instead animate only
564 * how the view is drawn on the display. In particular, the {@link ViewPropertyAnimator} class
565 * makes animating these View properties particularly easy and efficient.
566 * </p>
567 * <p>
568 * 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 -0800569 * You can attach an {@link Animation} object to a view using
570 * {@link #setAnimation(Animation)} or
571 * {@link #startAnimation(Animation)}. The animation can alter the scale,
572 * rotation, translation and alpha of a view over time. If the animation is
573 * attached to a view that has children, the animation will affect the entire
574 * subtree rooted by that node. When an animation is started, the framework will
575 * take care of redrawing the appropriate views until the animation completes.
576 * </p>
577 *
Jeff Brown85a31762010-09-01 17:01:00 -0700578 * <a name="Security"></a>
579 * <h3>Security</h3>
580 * <p>
581 * Sometimes it is essential that an application be able to verify that an action
582 * is being performed with the full knowledge and consent of the user, such as
583 * granting a permission request, making a purchase or clicking on an advertisement.
584 * Unfortunately, a malicious application could try to spoof the user into
585 * performing these actions, unaware, by concealing the intended purpose of the view.
586 * As a remedy, the framework offers a touch filtering mechanism that can be used to
587 * improve the security of views that provide access to sensitive functionality.
588 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700589 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800590 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700591 * will discard touches that are received whenever the view's window is obscured by
592 * another visible window. As a result, the view will not receive touches whenever a
593 * toast, dialog or other window appears above the view's window.
594 * </p><p>
595 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700596 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
597 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700598 * </p>
599 *
Romain Guy171c5922011-01-06 10:04:23 -0800600 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700601 * @attr ref android.R.styleable#View_background
602 * @attr ref android.R.styleable#View_clickable
603 * @attr ref android.R.styleable#View_contentDescription
604 * @attr ref android.R.styleable#View_drawingCacheQuality
605 * @attr ref android.R.styleable#View_duplicateParentState
606 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700607 * @attr ref android.R.styleable#View_requiresFadingEdge
Philip Milne6c8ea062012-04-03 17:38:43 -0700608 * @attr ref android.R.styleable#View_fadeScrollbars
Romain Guyd6a463a2009-05-21 23:10:10 -0700609 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700610 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700612 * @attr ref android.R.styleable#View_isScrollContainer
613 * @attr ref android.R.styleable#View_focusable
614 * @attr ref android.R.styleable#View_focusableInTouchMode
615 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
616 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800617 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700618 * @attr ref android.R.styleable#View_longClickable
619 * @attr ref android.R.styleable#View_minHeight
620 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 * @attr ref android.R.styleable#View_nextFocusDown
622 * @attr ref android.R.styleable#View_nextFocusLeft
623 * @attr ref android.R.styleable#View_nextFocusRight
624 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700625 * @attr ref android.R.styleable#View_onClick
626 * @attr ref android.R.styleable#View_padding
627 * @attr ref android.R.styleable#View_paddingBottom
628 * @attr ref android.R.styleable#View_paddingLeft
629 * @attr ref android.R.styleable#View_paddingRight
630 * @attr ref android.R.styleable#View_paddingTop
Fabrice Di Meglio101d5aa2012-02-16 18:36:06 -0800631 * @attr ref android.R.styleable#View_paddingStart
632 * @attr ref android.R.styleable#View_paddingEnd
Romain Guyd6a463a2009-05-21 23:10:10 -0700633 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800634 * @attr ref android.R.styleable#View_rotation
635 * @attr ref android.R.styleable#View_rotationX
636 * @attr ref android.R.styleable#View_rotationY
637 * @attr ref android.R.styleable#View_scaleX
638 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 * @attr ref android.R.styleable#View_scrollX
640 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700641 * @attr ref android.R.styleable#View_scrollbarSize
642 * @attr ref android.R.styleable#View_scrollbarStyle
643 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700644 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
645 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
647 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 * @attr ref android.R.styleable#View_scrollbarThumbVertical
649 * @attr ref android.R.styleable#View_scrollbarTrackVertical
650 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
651 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700652 * @attr ref android.R.styleable#View_soundEffectsEnabled
653 * @attr ref android.R.styleable#View_tag
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -0700654 * @attr ref android.R.styleable#View_textAlignment
Chet Haase73066682010-11-29 15:55:32 -0800655 * @attr ref android.R.styleable#View_transformPivotX
656 * @attr ref android.R.styleable#View_transformPivotY
657 * @attr ref android.R.styleable#View_translationX
658 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700659 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 *
661 * @see android.view.ViewGroup
662 */
Fabrice Di Megliob03b4342012-06-04 12:55:30 -0700663public class View implements Drawable.Callback, KeyEvent.Callback,
Adam Powell8fc54f92011-09-07 16:40:45 -0700664 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 private static final boolean DBG = false;
666
667 /**
668 * The logging tag used by this class with android.util.Log.
669 */
670 protected static final String VIEW_LOG_TAG = "View";
671
672 /**
Guang Zhu0d607fb2012-05-11 19:34:56 -0700673 * When set to true, apps will draw debugging information about their layouts.
Romain Guy4b8c4f82012-04-27 15:48:35 -0700674 *
675 * @hide
676 */
677 public static final String DEBUG_LAYOUT_PROPERTY = "debug.layout";
678
679 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 * Used to mark a View that has no ID.
681 */
682 public static final int NO_ID = -1;
683
684 /**
685 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
686 * calling setFlags.
687 */
688 private static final int NOT_FOCUSABLE = 0x00000000;
689
690 /**
691 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
692 * setFlags.
693 */
694 private static final int FOCUSABLE = 0x00000001;
695
696 /**
697 * Mask for use with setFlags indicating bits used for focus.
698 */
699 private static final int FOCUSABLE_MASK = 0x00000001;
700
701 /**
702 * This view will adjust its padding to fit sytem windows (e.g. status bar)
703 */
704 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
705
706 /**
Scott Main812634c22011-07-27 13:22:35 -0700707 * This view is visible.
708 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
709 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 */
711 public static final int VISIBLE = 0x00000000;
712
713 /**
714 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700715 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
716 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 */
718 public static final int INVISIBLE = 0x00000004;
719
720 /**
721 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700722 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
723 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 */
725 public static final int GONE = 0x00000008;
726
727 /**
728 * Mask for use with setFlags indicating bits used for visibility.
729 * {@hide}
730 */
731 static final int VISIBILITY_MASK = 0x0000000C;
732
733 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
734
735 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700736 * This view is enabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 * Use with ENABLED_MASK when calling setFlags.
738 * {@hide}
739 */
740 static final int ENABLED = 0x00000000;
741
742 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700743 * This view is disabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 * Use with ENABLED_MASK when calling setFlags.
745 * {@hide}
746 */
747 static final int DISABLED = 0x00000020;
748
749 /**
750 * Mask for use with setFlags indicating bits used for indicating whether
751 * this view is enabled
752 * {@hide}
753 */
754 static final int ENABLED_MASK = 0x00000020;
755
756 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700757 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
758 * called and further optimizations will be performed. It is okay to have
759 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 * {@hide}
761 */
762 static final int WILL_NOT_DRAW = 0x00000080;
763
764 /**
765 * Mask for use with setFlags indicating bits used for indicating whether
766 * this view is will draw
767 * {@hide}
768 */
769 static final int DRAW_MASK = 0x00000080;
770
771 /**
772 * <p>This view doesn't show scrollbars.</p>
773 * {@hide}
774 */
775 static final int SCROLLBARS_NONE = 0x00000000;
776
777 /**
778 * <p>This view shows horizontal scrollbars.</p>
779 * {@hide}
780 */
781 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
782
783 /**
784 * <p>This view shows vertical scrollbars.</p>
785 * {@hide}
786 */
787 static final int SCROLLBARS_VERTICAL = 0x00000200;
788
789 /**
790 * <p>Mask for use with setFlags indicating bits used for indicating which
791 * scrollbars are enabled.</p>
792 * {@hide}
793 */
794 static final int SCROLLBARS_MASK = 0x00000300;
795
Jeff Brown85a31762010-09-01 17:01:00 -0700796 /**
797 * Indicates that the view should filter touches when its window is obscured.
798 * Refer to the class comments for more information about this security feature.
799 * {@hide}
800 */
801 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
802
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700803 /**
804 * Set for framework elements that use FITS_SYSTEM_WINDOWS, to indicate
805 * that they are optional and should be skipped if the window has
806 * requested system UI flags that ignore those insets for layout.
807 */
808 static final int OPTIONAL_FITS_SYSTEM_WINDOWS = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809
810 /**
811 * <p>This view doesn't show fading edges.</p>
812 * {@hide}
813 */
814 static final int FADING_EDGE_NONE = 0x00000000;
815
816 /**
817 * <p>This view shows horizontal fading edges.</p>
818 * {@hide}
819 */
820 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
821
822 /**
823 * <p>This view shows vertical fading edges.</p>
824 * {@hide}
825 */
826 static final int FADING_EDGE_VERTICAL = 0x00002000;
827
828 /**
829 * <p>Mask for use with setFlags indicating bits used for indicating which
830 * fading edges are enabled.</p>
831 * {@hide}
832 */
833 static final int FADING_EDGE_MASK = 0x00003000;
834
835 /**
836 * <p>Indicates this view can be clicked. When clickable, a View reacts
837 * to clicks by notifying the OnClickListener.<p>
838 * {@hide}
839 */
840 static final int CLICKABLE = 0x00004000;
841
842 /**
843 * <p>Indicates this view is caching its drawing into a bitmap.</p>
844 * {@hide}
845 */
846 static final int DRAWING_CACHE_ENABLED = 0x00008000;
847
848 /**
849 * <p>Indicates that no icicle should be saved for this view.<p>
850 * {@hide}
851 */
852 static final int SAVE_DISABLED = 0x000010000;
853
854 /**
855 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
856 * property.</p>
857 * {@hide}
858 */
859 static final int SAVE_DISABLED_MASK = 0x000010000;
860
861 /**
862 * <p>Indicates that no drawing cache should ever be created for this view.<p>
863 * {@hide}
864 */
865 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
866
867 /**
868 * <p>Indicates this view can take / keep focus when int touch mode.</p>
869 * {@hide}
870 */
871 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
872
873 /**
874 * <p>Enables low quality mode for the drawing cache.</p>
875 */
876 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
877
878 /**
879 * <p>Enables high quality mode for the drawing cache.</p>
880 */
881 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
882
883 /**
884 * <p>Enables automatic quality mode for the drawing cache.</p>
885 */
886 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
887
888 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
889 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
890 };
891
892 /**
893 * <p>Mask for use with setFlags indicating bits used for the cache
894 * quality property.</p>
895 * {@hide}
896 */
897 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
898
899 /**
900 * <p>
901 * Indicates this view can be long clicked. When long clickable, a View
902 * reacts to long clicks by notifying the OnLongClickListener or showing a
903 * context menu.
904 * </p>
905 * {@hide}
906 */
907 static final int LONG_CLICKABLE = 0x00200000;
908
909 /**
910 * <p>Indicates that this view gets its drawable states from its direct parent
911 * and ignores its original internal states.</p>
912 *
913 * @hide
914 */
915 static final int DUPLICATE_PARENT_STATE = 0x00400000;
916
917 /**
918 * The scrollbar style to display the scrollbars inside the content area,
919 * without increasing the padding. The scrollbars will be overlaid with
920 * translucency on the view's content.
921 */
922 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
923
924 /**
925 * The scrollbar style to display the scrollbars inside the padded area,
926 * increasing the padding of the view. The scrollbars will not overlap the
927 * content area of the view.
928 */
929 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
930
931 /**
932 * The scrollbar style to display the scrollbars at the edge of the view,
933 * without increasing the padding. The scrollbars will be overlaid with
934 * translucency.
935 */
936 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
937
938 /**
939 * The scrollbar style to display the scrollbars at the edge of the view,
940 * increasing the padding of the view. The scrollbars will only overlap the
941 * background, if any.
942 */
943 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
944
945 /**
946 * Mask to check if the scrollbar style is overlay or inset.
947 * {@hide}
948 */
949 static final int SCROLLBARS_INSET_MASK = 0x01000000;
950
951 /**
952 * Mask to check if the scrollbar style is inside or outside.
953 * {@hide}
954 */
955 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
956
957 /**
958 * Mask for scrollbar style.
959 * {@hide}
960 */
961 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
962
963 /**
964 * View flag indicating that the screen should remain on while the
965 * window containing this view is visible to the user. This effectively
966 * takes care of automatically setting the WindowManager's
967 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
968 */
969 public static final int KEEP_SCREEN_ON = 0x04000000;
970
971 /**
972 * View flag indicating whether this view should have sound effects enabled
973 * for events such as clicking and touching.
974 */
975 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
976
977 /**
978 * View flag indicating whether this view should have haptic feedback
979 * enabled for events such as long presses.
980 */
981 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
982
983 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700984 * <p>Indicates that the view hierarchy should stop saving state when
985 * it reaches this view. If state saving is initiated immediately at
986 * the view, it will be allowed.
987 * {@hide}
988 */
989 static final int PARENT_SAVE_DISABLED = 0x20000000;
990
991 /**
992 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
993 * {@hide}
994 */
995 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
996
997 /**
svetoslavganov75986cf2009-05-14 22:28:01 -0700998 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
999 * should add all focusable Views regardless if they are focusable in touch mode.
1000 */
1001 public static final int FOCUSABLES_ALL = 0x00000000;
1002
1003 /**
1004 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1005 * should add only Views focusable in touch mode.
1006 */
1007 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1008
1009 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001010 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 * item.
1012 */
1013 public static final int FOCUS_BACKWARD = 0x00000001;
1014
1015 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001016 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 * item.
1018 */
1019 public static final int FOCUS_FORWARD = 0x00000002;
1020
1021 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001022 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 */
1024 public static final int FOCUS_LEFT = 0x00000011;
1025
1026 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001027 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 */
1029 public static final int FOCUS_UP = 0x00000021;
1030
1031 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001032 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 */
1034 public static final int FOCUS_RIGHT = 0x00000042;
1035
1036 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001037 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 */
1039 public static final int FOCUS_DOWN = 0x00000082;
1040
Svetoslav Ganov42138042012-03-20 11:51:39 -07001041 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001042 * Bits of {@link #getMeasuredWidthAndState()} and
1043 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1044 */
1045 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1046
1047 /**
1048 * Bits of {@link #getMeasuredWidthAndState()} and
1049 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1050 */
1051 public static final int MEASURED_STATE_MASK = 0xff000000;
1052
1053 /**
1054 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1055 * for functions that combine both width and height into a single int,
1056 * such as {@link #getMeasuredState()} and the childState argument of
1057 * {@link #resolveSizeAndState(int, int, int)}.
1058 */
1059 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1060
1061 /**
1062 * Bit of {@link #getMeasuredWidthAndState()} and
1063 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1064 * is smaller that the space the view would like to have.
1065 */
1066 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1067
1068 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 * Base View state sets
1070 */
1071 // Singles
1072 /**
1073 * Indicates the view has no states set. States are used with
1074 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1075 * view depending on its state.
1076 *
1077 * @see android.graphics.drawable.Drawable
1078 * @see #getDrawableState()
1079 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001080 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 /**
1082 * Indicates the view is enabled. States are used with
1083 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1084 * view depending on its state.
1085 *
1086 * @see android.graphics.drawable.Drawable
1087 * @see #getDrawableState()
1088 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001089 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 /**
1091 * Indicates the view is focused. States are used with
1092 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1093 * view depending on its state.
1094 *
1095 * @see android.graphics.drawable.Drawable
1096 * @see #getDrawableState()
1097 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001098 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 /**
1100 * Indicates the view is selected. States are used with
1101 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1102 * view depending on its state.
1103 *
1104 * @see android.graphics.drawable.Drawable
1105 * @see #getDrawableState()
1106 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001107 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 /**
1109 * Indicates the view is pressed. States are used with
1110 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1111 * view depending on its state.
1112 *
1113 * @see android.graphics.drawable.Drawable
1114 * @see #getDrawableState()
1115 * @hide
1116 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001117 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001118 /**
1119 * Indicates the view's window has focus. States are used with
1120 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1121 * view depending on its state.
1122 *
1123 * @see android.graphics.drawable.Drawable
1124 * @see #getDrawableState()
1125 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001126 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 // Doubles
1128 /**
1129 * Indicates the view is enabled and has the focus.
1130 *
1131 * @see #ENABLED_STATE_SET
1132 * @see #FOCUSED_STATE_SET
1133 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001134 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 /**
1136 * Indicates the view is enabled and selected.
1137 *
1138 * @see #ENABLED_STATE_SET
1139 * @see #SELECTED_STATE_SET
1140 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001141 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 /**
1143 * Indicates the view is enabled and that its window has focus.
1144 *
1145 * @see #ENABLED_STATE_SET
1146 * @see #WINDOW_FOCUSED_STATE_SET
1147 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001148 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001149 /**
1150 * Indicates the view is focused and selected.
1151 *
1152 * @see #FOCUSED_STATE_SET
1153 * @see #SELECTED_STATE_SET
1154 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001155 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 /**
1157 * Indicates the view has the focus and that its window has the focus.
1158 *
1159 * @see #FOCUSED_STATE_SET
1160 * @see #WINDOW_FOCUSED_STATE_SET
1161 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001162 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 /**
1164 * Indicates the view is selected and that its window has the focus.
1165 *
1166 * @see #SELECTED_STATE_SET
1167 * @see #WINDOW_FOCUSED_STATE_SET
1168 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001169 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001170 // Triples
1171 /**
1172 * Indicates the view is enabled, focused and selected.
1173 *
1174 * @see #ENABLED_STATE_SET
1175 * @see #FOCUSED_STATE_SET
1176 * @see #SELECTED_STATE_SET
1177 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001178 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 /**
1180 * Indicates the view is enabled, focused and its window has the focus.
1181 *
1182 * @see #ENABLED_STATE_SET
1183 * @see #FOCUSED_STATE_SET
1184 * @see #WINDOW_FOCUSED_STATE_SET
1185 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001186 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 /**
1188 * Indicates the view is enabled, selected and its window has the focus.
1189 *
1190 * @see #ENABLED_STATE_SET
1191 * @see #SELECTED_STATE_SET
1192 * @see #WINDOW_FOCUSED_STATE_SET
1193 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001194 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 /**
1196 * Indicates the view is focused, selected and its window has the focus.
1197 *
1198 * @see #FOCUSED_STATE_SET
1199 * @see #SELECTED_STATE_SET
1200 * @see #WINDOW_FOCUSED_STATE_SET
1201 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001202 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 /**
1204 * Indicates the view is enabled, focused, selected and its window
1205 * has the focus.
1206 *
1207 * @see #ENABLED_STATE_SET
1208 * @see #FOCUSED_STATE_SET
1209 * @see #SELECTED_STATE_SET
1210 * @see #WINDOW_FOCUSED_STATE_SET
1211 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001212 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 /**
1214 * Indicates the view is pressed and its window has the focus.
1215 *
1216 * @see #PRESSED_STATE_SET
1217 * @see #WINDOW_FOCUSED_STATE_SET
1218 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001219 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 /**
1221 * Indicates the view is pressed and selected.
1222 *
1223 * @see #PRESSED_STATE_SET
1224 * @see #SELECTED_STATE_SET
1225 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001226 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 /**
1228 * Indicates the view is pressed, selected and its window has the focus.
1229 *
1230 * @see #PRESSED_STATE_SET
1231 * @see #SELECTED_STATE_SET
1232 * @see #WINDOW_FOCUSED_STATE_SET
1233 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001234 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 /**
1236 * Indicates the view is pressed and focused.
1237 *
1238 * @see #PRESSED_STATE_SET
1239 * @see #FOCUSED_STATE_SET
1240 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001241 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 /**
1243 * Indicates the view is pressed, focused and its window has the focus.
1244 *
1245 * @see #PRESSED_STATE_SET
1246 * @see #FOCUSED_STATE_SET
1247 * @see #WINDOW_FOCUSED_STATE_SET
1248 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001249 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250 /**
1251 * Indicates the view is pressed, focused and selected.
1252 *
1253 * @see #PRESSED_STATE_SET
1254 * @see #SELECTED_STATE_SET
1255 * @see #FOCUSED_STATE_SET
1256 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001257 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 /**
1259 * Indicates the view is pressed, focused, selected and its window has the focus.
1260 *
1261 * @see #PRESSED_STATE_SET
1262 * @see #FOCUSED_STATE_SET
1263 * @see #SELECTED_STATE_SET
1264 * @see #WINDOW_FOCUSED_STATE_SET
1265 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001266 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 /**
1268 * Indicates the view is pressed and enabled.
1269 *
1270 * @see #PRESSED_STATE_SET
1271 * @see #ENABLED_STATE_SET
1272 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001273 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 /**
1275 * Indicates the view is pressed, enabled and its window has the focus.
1276 *
1277 * @see #PRESSED_STATE_SET
1278 * @see #ENABLED_STATE_SET
1279 * @see #WINDOW_FOCUSED_STATE_SET
1280 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001281 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 /**
1283 * Indicates the view is pressed, enabled and selected.
1284 *
1285 * @see #PRESSED_STATE_SET
1286 * @see #ENABLED_STATE_SET
1287 * @see #SELECTED_STATE_SET
1288 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001289 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 /**
1291 * Indicates the view is pressed, enabled, selected and its window has the
1292 * focus.
1293 *
1294 * @see #PRESSED_STATE_SET
1295 * @see #ENABLED_STATE_SET
1296 * @see #SELECTED_STATE_SET
1297 * @see #WINDOW_FOCUSED_STATE_SET
1298 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001299 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 /**
1301 * Indicates the view is pressed, enabled and focused.
1302 *
1303 * @see #PRESSED_STATE_SET
1304 * @see #ENABLED_STATE_SET
1305 * @see #FOCUSED_STATE_SET
1306 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001307 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308 /**
1309 * Indicates the view is pressed, enabled, focused and its window has the
1310 * focus.
1311 *
1312 * @see #PRESSED_STATE_SET
1313 * @see #ENABLED_STATE_SET
1314 * @see #FOCUSED_STATE_SET
1315 * @see #WINDOW_FOCUSED_STATE_SET
1316 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001317 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001318 /**
1319 * Indicates the view is pressed, enabled, focused and selected.
1320 *
1321 * @see #PRESSED_STATE_SET
1322 * @see #ENABLED_STATE_SET
1323 * @see #SELECTED_STATE_SET
1324 * @see #FOCUSED_STATE_SET
1325 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001326 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 /**
1328 * Indicates the view is pressed, enabled, focused, selected and its window
1329 * has the focus.
1330 *
1331 * @see #PRESSED_STATE_SET
1332 * @see #ENABLED_STATE_SET
1333 * @see #SELECTED_STATE_SET
1334 * @see #FOCUSED_STATE_SET
1335 * @see #WINDOW_FOCUSED_STATE_SET
1336 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001337 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338
1339 /**
1340 * The order here is very important to {@link #getDrawableState()}
1341 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001342 private static final int[][] VIEW_STATE_SETS;
1343
Romain Guyb051e892010-09-28 19:09:36 -07001344 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1345 static final int VIEW_STATE_SELECTED = 1 << 1;
1346 static final int VIEW_STATE_FOCUSED = 1 << 2;
1347 static final int VIEW_STATE_ENABLED = 1 << 3;
1348 static final int VIEW_STATE_PRESSED = 1 << 4;
1349 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001350 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001351 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001352 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1353 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001354
1355 static final int[] VIEW_STATE_IDS = new int[] {
1356 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1357 R.attr.state_selected, VIEW_STATE_SELECTED,
1358 R.attr.state_focused, VIEW_STATE_FOCUSED,
1359 R.attr.state_enabled, VIEW_STATE_ENABLED,
1360 R.attr.state_pressed, VIEW_STATE_PRESSED,
1361 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001362 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001363 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001364 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
Svetoslav Ganov42138042012-03-20 11:51:39 -07001365 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 };
1367
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001368 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001369 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1370 throw new IllegalStateException(
1371 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1372 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001373 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001374 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001375 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001376 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001377 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001378 orderedIds[i * 2] = viewState;
1379 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001380 }
1381 }
1382 }
Romain Guyb051e892010-09-28 19:09:36 -07001383 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1384 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1385 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001386 int numBits = Integer.bitCount(i);
1387 int[] set = new int[numBits];
1388 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001389 for (int j = 0; j < orderedIds.length; j += 2) {
1390 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001391 set[pos++] = orderedIds[j];
1392 }
1393 }
1394 VIEW_STATE_SETS[i] = set;
1395 }
1396
1397 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1398 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1399 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1400 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1401 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1402 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1403 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1404 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1405 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1406 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1407 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1408 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1409 | VIEW_STATE_FOCUSED];
1410 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1411 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1412 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1413 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1414 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1415 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1416 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1417 | VIEW_STATE_ENABLED];
1418 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1419 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1420 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1421 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1422 | VIEW_STATE_ENABLED];
1423 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1424 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1425 | VIEW_STATE_ENABLED];
1426 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1427 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1428 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1429
1430 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1431 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1432 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1433 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1434 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1435 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1436 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1437 | VIEW_STATE_PRESSED];
1438 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1439 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1440 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1441 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1442 | VIEW_STATE_PRESSED];
1443 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1444 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1445 | VIEW_STATE_PRESSED];
1446 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1447 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1448 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1449 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1450 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1451 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1452 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1453 | VIEW_STATE_PRESSED];
1454 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1455 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1456 | VIEW_STATE_PRESSED];
1457 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1458 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1459 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1460 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1461 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1462 | VIEW_STATE_PRESSED];
1463 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1464 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1465 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1466 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1467 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1468 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1469 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1470 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1471 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1472 | VIEW_STATE_PRESSED];
1473 }
1474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001475 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001476 * Accessibility event types that are dispatched for text population.
1477 */
1478 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1479 AccessibilityEvent.TYPE_VIEW_CLICKED
1480 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1481 | AccessibilityEvent.TYPE_VIEW_SELECTED
1482 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1483 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1484 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001485 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
Svetoslav Ganov84dd52e2011-11-18 10:24:00 -08001486 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
Svetoslav Ganov42138042012-03-20 11:51:39 -07001487 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001488 | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED
1489 | AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001490
1491 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492 * Temporary Rect currently for use in setBackground(). This will probably
1493 * be extended in the future to hold our own class with more than just
1494 * a Rect. :)
1495 */
1496 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001497
1498 /**
1499 * Map used to store views' tags.
1500 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001501 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001504 * The next available accessiiblity id.
1505 */
1506 private static int sNextAccessibilityViewId;
1507
1508 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001509 * The animation currently associated with this view.
1510 * @hide
1511 */
1512 protected Animation mCurrentAnimation = null;
1513
1514 /**
1515 * Width as measured during measure pass.
1516 * {@hide}
1517 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001518 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001519 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520
1521 /**
1522 * Height as measured during measure pass.
1523 * {@hide}
1524 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001525 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001526 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527
1528 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001529 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1530 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1531 * its display list. This flag, used only when hw accelerated, allows us to clear the
1532 * flag while retaining this information until it's needed (at getDisplayList() time and
1533 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1534 *
1535 * {@hide}
1536 */
1537 boolean mRecreateDisplayList = false;
1538
1539 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 * The view's identifier.
1541 * {@hide}
1542 *
1543 * @see #setId(int)
1544 * @see #getId()
1545 */
1546 @ViewDebug.ExportedProperty(resolveId = true)
1547 int mID = NO_ID;
1548
1549 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001550 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001551 */
1552 int mAccessibilityViewId = NO_ID;
1553
1554 /**
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001555 * @hide
1556 */
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07001557 private int mAccessibilityCursorPosition = ACCESSIBILITY_CURSOR_POSITION_UNDEFINED;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001558
1559 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560 * The view's tag.
1561 * {@hide}
1562 *
1563 * @see #setTag(Object)
1564 * @see #getTag()
1565 */
1566 protected Object mTag;
1567
1568 // for mPrivateFlags:
1569 /** {@hide} */
1570 static final int WANTS_FOCUS = 0x00000001;
1571 /** {@hide} */
1572 static final int FOCUSED = 0x00000002;
1573 /** {@hide} */
1574 static final int SELECTED = 0x00000004;
1575 /** {@hide} */
1576 static final int IS_ROOT_NAMESPACE = 0x00000008;
1577 /** {@hide} */
1578 static final int HAS_BOUNDS = 0x00000010;
1579 /** {@hide} */
1580 static final int DRAWN = 0x00000020;
1581 /**
1582 * When this flag is set, this view is running an animation on behalf of its
1583 * children and should therefore not cancel invalidate requests, even if they
1584 * lie outside of this view's bounds.
1585 *
1586 * {@hide}
1587 */
1588 static final int DRAW_ANIMATION = 0x00000040;
1589 /** {@hide} */
1590 static final int SKIP_DRAW = 0x00000080;
1591 /** {@hide} */
1592 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1593 /** {@hide} */
1594 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1595 /** {@hide} */
1596 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1597 /** {@hide} */
1598 static final int MEASURED_DIMENSION_SET = 0x00000800;
1599 /** {@hide} */
1600 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001601 /** {@hide} */
1602 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001603
1604 private static final int PRESSED = 0x00004000;
1605
1606 /** {@hide} */
1607 static final int DRAWING_CACHE_VALID = 0x00008000;
1608 /**
1609 * Flag used to indicate that this view should be drawn once more (and only once
1610 * more) after its animation has completed.
1611 * {@hide}
1612 */
1613 static final int ANIMATION_STARTED = 0x00010000;
1614
1615 private static final int SAVE_STATE_CALLED = 0x00020000;
1616
1617 /**
1618 * Indicates that the View returned true when onSetAlpha() was called and that
1619 * the alpha must be restored.
1620 * {@hide}
1621 */
1622 static final int ALPHA_SET = 0x00040000;
1623
1624 /**
1625 * Set by {@link #setScrollContainer(boolean)}.
1626 */
1627 static final int SCROLL_CONTAINER = 0x00080000;
1628
1629 /**
1630 * Set by {@link #setScrollContainer(boolean)}.
1631 */
1632 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1633
1634 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001635 * View flag indicating whether this view was invalidated (fully or partially.)
1636 *
1637 * @hide
1638 */
1639 static final int DIRTY = 0x00200000;
1640
1641 /**
1642 * View flag indicating whether this view was invalidated by an opaque
1643 * invalidate request.
1644 *
1645 * @hide
1646 */
1647 static final int DIRTY_OPAQUE = 0x00400000;
1648
1649 /**
1650 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1651 *
1652 * @hide
1653 */
1654 static final int DIRTY_MASK = 0x00600000;
1655
1656 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001657 * Indicates whether the background is opaque.
1658 *
1659 * @hide
1660 */
1661 static final int OPAQUE_BACKGROUND = 0x00800000;
1662
1663 /**
1664 * Indicates whether the scrollbars are opaque.
1665 *
1666 * @hide
1667 */
1668 static final int OPAQUE_SCROLLBARS = 0x01000000;
1669
1670 /**
1671 * Indicates whether the view is opaque.
1672 *
1673 * @hide
1674 */
1675 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001676
Adam Powelle14579b2009-12-16 18:39:52 -08001677 /**
1678 * Indicates a prepressed state;
1679 * the short time between ACTION_DOWN and recognizing
1680 * a 'real' press. Prepressed is used to recognize quick taps
1681 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001682 *
Adam Powelle14579b2009-12-16 18:39:52 -08001683 * @hide
1684 */
1685 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001686
Adam Powellc9fbaab2010-02-16 17:16:19 -08001687 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001688 * Indicates whether the view is temporarily detached.
1689 *
1690 * @hide
1691 */
1692 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001693
Adam Powell8568c3a2010-04-19 14:26:11 -07001694 /**
1695 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001696 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001697 * @hide
1698 */
1699 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001700
1701 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001702 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1703 * @hide
1704 */
1705 private static final int HOVERED = 0x10000000;
1706
1707 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001708 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1709 * for transform operations
1710 *
1711 * @hide
1712 */
Adam Powellf37df072010-09-17 16:22:49 -07001713 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001714
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001715 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001716 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001717
Chet Haasefd2b0022010-08-06 13:08:56 -07001718 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001719 * Indicates that this view was specifically invalidated, not just dirtied because some
1720 * child view was invalidated. The flag is used to determine when we need to recreate
1721 * a view's display list (as opposed to just returning a reference to its existing
1722 * display list).
1723 *
1724 * @hide
1725 */
1726 static final int INVALIDATED = 0x80000000;
1727
Christopher Tate3d4bf172011-03-28 16:16:46 -07001728 /* Masks for mPrivateFlags2 */
1729
1730 /**
1731 * Indicates that this view has reported that it can accept the current drag's content.
1732 * Cleared when the drag operation concludes.
1733 * @hide
1734 */
1735 static final int DRAG_CAN_ACCEPT = 0x00000001;
1736
1737 /**
1738 * Indicates that this view is currently directly under the drag location in a
1739 * drag-and-drop operation involving content that it can accept. Cleared when
1740 * the drag exits the view, or when the drag operation concludes.
1741 * @hide
1742 */
1743 static final int DRAG_HOVERED = 0x00000002;
1744
Cibu Johny86666632010-02-22 13:01:02 -08001745 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001746 * Horizontal layout direction of this view is from Left to Right.
1747 * Use with {@link #setLayoutDirection}.
Cibu Johny86666632010-02-22 13:01:02 -08001748 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001749 public static final int LAYOUT_DIRECTION_LTR = 0;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001750
1751 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001752 * Horizontal layout direction of this view is from Right to Left.
1753 * Use with {@link #setLayoutDirection}.
1754 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001755 public static final int LAYOUT_DIRECTION_RTL = 1;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001756
1757 /**
1758 * Horizontal layout direction of this view is inherited from its parent.
1759 * Use with {@link #setLayoutDirection}.
1760 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001761 public static final int LAYOUT_DIRECTION_INHERIT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001762
1763 /**
1764 * Horizontal layout direction of this view is from deduced from the default language
1765 * script for the locale. Use with {@link #setLayoutDirection}.
1766 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001767 public static final int LAYOUT_DIRECTION_LOCALE = 3;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001768
1769 /**
1770 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001771 * @hide
1772 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001773 static final int LAYOUT_DIRECTION_MASK_SHIFT = 2;
1774
1775 /**
1776 * Mask for use with private flags indicating bits used for horizontal layout direction.
1777 * @hide
1778 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001779 static final int LAYOUT_DIRECTION_MASK = 0x00000003 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001780
1781 /**
1782 * Indicates whether the view horizontal layout direction has been resolved and drawn to the
1783 * right-to-left direction.
1784 * @hide
1785 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001786 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 4 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001787
1788 /**
1789 * Indicates whether the view horizontal layout direction has been resolved.
1790 * @hide
1791 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001792 static final int LAYOUT_DIRECTION_RESOLVED = 8 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001793
1794 /**
1795 * Mask for use with private flags indicating bits used for resolved horizontal layout direction.
1796 * @hide
1797 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001798 static final int LAYOUT_DIRECTION_RESOLVED_MASK = 0x0000000C << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001799
1800 /*
1801 * Array of horizontal layout direction flags for mapping attribute "layoutDirection" to correct
1802 * flag value.
1803 * @hide
1804 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001805 private static final int[] LAYOUT_DIRECTION_FLAGS = {
1806 LAYOUT_DIRECTION_LTR,
1807 LAYOUT_DIRECTION_RTL,
1808 LAYOUT_DIRECTION_INHERIT,
1809 LAYOUT_DIRECTION_LOCALE
1810 };
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001811
1812 /**
1813 * Default horizontal layout direction.
1814 * @hide
1815 */
1816 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001817
Adam Powell539ee872012-02-03 19:00:49 -08001818 /**
1819 * Indicates that the view is tracking some sort of transient state
1820 * that the app should not need to be aware of, but that the framework
1821 * should take special care to preserve.
1822 *
1823 * @hide
1824 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001825 static final int HAS_TRANSIENT_STATE = 0x00000100;
Adam Powell539ee872012-02-03 19:00:49 -08001826
1827
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001828 /**
1829 * Text direction is inherited thru {@link ViewGroup}
1830 */
1831 public static final int TEXT_DIRECTION_INHERIT = 0;
1832
1833 /**
1834 * Text direction is using "first strong algorithm". The first strong directional character
1835 * determines the paragraph direction. If there is no strong directional character, the
1836 * paragraph direction is the view's resolved layout direction.
1837 */
1838 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
1839
1840 /**
1841 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
1842 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
1843 * If there are neither, the paragraph direction is the view's resolved layout direction.
1844 */
1845 public static final int TEXT_DIRECTION_ANY_RTL = 2;
1846
1847 /**
1848 * Text direction is forced to LTR.
1849 */
1850 public static final int TEXT_DIRECTION_LTR = 3;
1851
1852 /**
1853 * Text direction is forced to RTL.
1854 */
1855 public static final int TEXT_DIRECTION_RTL = 4;
1856
1857 /**
1858 * Text direction is coming from the system Locale.
1859 */
1860 public static final int TEXT_DIRECTION_LOCALE = 5;
1861
1862 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001863 * Default text direction is inherited
1864 */
1865 protected static int TEXT_DIRECTION_DEFAULT = TEXT_DIRECTION_INHERIT;
1866
1867 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001868 * Bit shift to get the horizontal layout direction. (bits after LAYOUT_DIRECTION_RESOLVED)
1869 * @hide
1870 */
1871 static final int TEXT_DIRECTION_MASK_SHIFT = 6;
1872
1873 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001874 * Mask for use with private flags indicating bits used for text direction.
1875 * @hide
1876 */
1877 static final int TEXT_DIRECTION_MASK = 0x00000007 << TEXT_DIRECTION_MASK_SHIFT;
1878
1879 /**
1880 * Array of text direction flags for mapping attribute "textDirection" to correct
1881 * flag value.
1882 * @hide
1883 */
1884 private static final int[] TEXT_DIRECTION_FLAGS = {
1885 TEXT_DIRECTION_INHERIT << TEXT_DIRECTION_MASK_SHIFT,
1886 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_MASK_SHIFT,
1887 TEXT_DIRECTION_ANY_RTL << TEXT_DIRECTION_MASK_SHIFT,
1888 TEXT_DIRECTION_LTR << TEXT_DIRECTION_MASK_SHIFT,
1889 TEXT_DIRECTION_RTL << TEXT_DIRECTION_MASK_SHIFT,
1890 TEXT_DIRECTION_LOCALE << TEXT_DIRECTION_MASK_SHIFT
1891 };
1892
1893 /**
1894 * Indicates whether the view text direction has been resolved.
1895 * @hide
1896 */
1897 static final int TEXT_DIRECTION_RESOLVED = 0x00000008 << TEXT_DIRECTION_MASK_SHIFT;
1898
1899 /**
1900 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1901 * @hide
1902 */
1903 static final int TEXT_DIRECTION_RESOLVED_MASK_SHIFT = 10;
1904
1905 /**
1906 * Mask for use with private flags indicating bits used for resolved text direction.
1907 * @hide
1908 */
1909 static final int TEXT_DIRECTION_RESOLVED_MASK = 0x00000007 << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1910
1911 /**
1912 * Indicates whether the view text direction has been resolved to the "first strong" heuristic.
1913 * @hide
1914 */
1915 static final int TEXT_DIRECTION_RESOLVED_DEFAULT =
1916 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1917
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001918 /*
1919 * Default text alignment. The text alignment of this View is inherited from its parent.
1920 * Use with {@link #setTextAlignment(int)}
1921 */
1922 public static final int TEXT_ALIGNMENT_INHERIT = 0;
1923
1924 /**
1925 * Default for the root view. The gravity determines the text alignment, ALIGN_NORMAL,
1926 * ALIGN_CENTER, or ALIGN_OPPOSITE, which are relative to each paragraph’s text direction.
1927 *
1928 * Use with {@link #setTextAlignment(int)}
1929 */
1930 public static final int TEXT_ALIGNMENT_GRAVITY = 1;
1931
1932 /**
1933 * Align to the start of the paragraph, e.g. ALIGN_NORMAL.
1934 *
1935 * Use with {@link #setTextAlignment(int)}
1936 */
1937 public static final int TEXT_ALIGNMENT_TEXT_START = 2;
1938
1939 /**
1940 * Align to the end of the paragraph, e.g. ALIGN_OPPOSITE.
1941 *
1942 * Use with {@link #setTextAlignment(int)}
1943 */
1944 public static final int TEXT_ALIGNMENT_TEXT_END = 3;
1945
1946 /**
1947 * Center the paragraph, e.g. ALIGN_CENTER.
1948 *
1949 * Use with {@link #setTextAlignment(int)}
1950 */
1951 public static final int TEXT_ALIGNMENT_CENTER = 4;
1952
1953 /**
1954 * Align to the start of the view, which is ALIGN_LEFT if the view’s resolved
1955 * layoutDirection is LTR, and ALIGN_RIGHT otherwise.
1956 *
1957 * Use with {@link #setTextAlignment(int)}
1958 */
1959 public static final int TEXT_ALIGNMENT_VIEW_START = 5;
1960
1961 /**
1962 * Align to the end of the view, which is ALIGN_RIGHT if the view’s resolved
1963 * layoutDirection is LTR, and ALIGN_LEFT otherwise.
1964 *
1965 * Use with {@link #setTextAlignment(int)}
1966 */
1967 public static final int TEXT_ALIGNMENT_VIEW_END = 6;
1968
1969 /**
1970 * Default text alignment is inherited
1971 */
1972 protected static int TEXT_ALIGNMENT_DEFAULT = TEXT_ALIGNMENT_GRAVITY;
1973
1974 /**
1975 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1976 * @hide
1977 */
1978 static final int TEXT_ALIGNMENT_MASK_SHIFT = 13;
1979
1980 /**
1981 * Mask for use with private flags indicating bits used for text alignment.
1982 * @hide
1983 */
1984 static final int TEXT_ALIGNMENT_MASK = 0x00000007 << TEXT_ALIGNMENT_MASK_SHIFT;
1985
1986 /**
1987 * Array of text direction flags for mapping attribute "textAlignment" to correct
1988 * flag value.
1989 * @hide
1990 */
1991 private static final int[] TEXT_ALIGNMENT_FLAGS = {
1992 TEXT_ALIGNMENT_INHERIT << TEXT_ALIGNMENT_MASK_SHIFT,
1993 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_MASK_SHIFT,
1994 TEXT_ALIGNMENT_TEXT_START << TEXT_ALIGNMENT_MASK_SHIFT,
1995 TEXT_ALIGNMENT_TEXT_END << TEXT_ALIGNMENT_MASK_SHIFT,
1996 TEXT_ALIGNMENT_CENTER << TEXT_ALIGNMENT_MASK_SHIFT,
1997 TEXT_ALIGNMENT_VIEW_START << TEXT_ALIGNMENT_MASK_SHIFT,
1998 TEXT_ALIGNMENT_VIEW_END << TEXT_ALIGNMENT_MASK_SHIFT
1999 };
2000
2001 /**
2002 * Indicates whether the view text alignment has been resolved.
2003 * @hide
2004 */
2005 static final int TEXT_ALIGNMENT_RESOLVED = 0x00000008 << TEXT_ALIGNMENT_MASK_SHIFT;
2006
2007 /**
2008 * Bit shift to get the resolved text alignment.
2009 * @hide
2010 */
2011 static final int TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT = 17;
2012
2013 /**
2014 * Mask for use with private flags indicating bits used for text alignment.
2015 * @hide
2016 */
2017 static final int TEXT_ALIGNMENT_RESOLVED_MASK = 0x00000007 << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
2018
2019 /**
2020 * Indicates whether if the view text alignment has been resolved to gravity
2021 */
2022 public static final int TEXT_ALIGNMENT_RESOLVED_DEFAULT =
2023 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
2024
Svetoslav Ganov42138042012-03-20 11:51:39 -07002025 // Accessiblity constants for mPrivateFlags2
2026
2027 /**
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07002028 * Shift for the bits in {@link #mPrivateFlags2} related to the
2029 * "importantForAccessibility" attribute.
Svetoslav Ganov42138042012-03-20 11:51:39 -07002030 */
2031 static final int IMPORTANT_FOR_ACCESSIBILITY_SHIFT = 20;
2032
2033 /**
2034 * Automatically determine whether a view is important for accessibility.
2035 */
2036 public static final int IMPORTANT_FOR_ACCESSIBILITY_AUTO = 0x00000000;
2037
2038 /**
2039 * The view is important for accessibility.
2040 */
2041 public static final int IMPORTANT_FOR_ACCESSIBILITY_YES = 0x00000001;
2042
2043 /**
2044 * The view is not important for accessibility.
2045 */
2046 public static final int IMPORTANT_FOR_ACCESSIBILITY_NO = 0x00000002;
2047
2048 /**
2049 * The default whether the view is important for accessiblity.
2050 */
2051 static final int IMPORTANT_FOR_ACCESSIBILITY_DEFAULT = IMPORTANT_FOR_ACCESSIBILITY_AUTO;
2052
2053 /**
2054 * Mask for obtainig the bits which specify how to determine
2055 * whether a view is important for accessibility.
2056 */
2057 static final int IMPORTANT_FOR_ACCESSIBILITY_MASK = (IMPORTANT_FOR_ACCESSIBILITY_AUTO
2058 | IMPORTANT_FOR_ACCESSIBILITY_YES | IMPORTANT_FOR_ACCESSIBILITY_NO)
2059 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
2060
2061 /**
2062 * Flag indicating whether a view has accessibility focus.
2063 */
2064 static final int ACCESSIBILITY_FOCUSED = 0x00000040 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
2065
2066 /**
2067 * Flag indicating whether a view state for accessibility has changed.
2068 */
2069 static final int ACCESSIBILITY_STATE_CHANGED = 0x00000080 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07002070
Chet Haaseafd5c3e2012-05-10 13:21:10 -07002071 /**
Chet Haase1a3ab172012-05-11 08:41:20 -07002072 * Flag indicating whether a view failed the quickReject() check in draw(). This condition
2073 * is used to check whether later changes to the view's transform should invalidate the
2074 * view to force the quickReject test to run again.
2075 */
Chet Haase21433372012-06-05 07:54:09 -07002076 static final int VIEW_QUICK_REJECTED = 0x10000000;
Chet Haase1a3ab172012-05-11 08:41:20 -07002077
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07002078 // There are a couple of flags left in mPrivateFlags2
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07002079
Christopher Tate3d4bf172011-03-28 16:16:46 -07002080 /* End of masks for mPrivateFlags2 */
2081
Chet Haase21433372012-06-05 07:54:09 -07002082 /* Masks for mPrivateFlags3 */
2083
2084 /**
2085 * Flag indicating that view has a transform animation set on it. This is used to track whether
2086 * an animation is cleared between successive frames, in order to tell the associated
2087 * DisplayList to clear its animation matrix.
2088 */
2089 static final int VIEW_IS_ANIMATING_TRANSFORM = 0x1;
2090
2091 /**
2092 * Flag indicating that view has an alpha animation set on it. This is used to track whether an
2093 * animation is cleared between successive frames, in order to tell the associated
2094 * DisplayList to restore its alpha value.
2095 */
2096 static final int VIEW_IS_ANIMATING_ALPHA = 0x2;
2097
2098
2099 /* End of masks for mPrivateFlags3 */
2100
Christopher Tate3d4bf172011-03-28 16:16:46 -07002101 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
2102
Chet Haasedaf98e92011-01-10 14:10:36 -08002103 /**
Adam Powell637d3372010-08-25 14:37:03 -07002104 * Always allow a user to over-scroll this view, provided it is a
2105 * view that can scroll.
2106 *
2107 * @see #getOverScrollMode()
2108 * @see #setOverScrollMode(int)
2109 */
2110 public static final int OVER_SCROLL_ALWAYS = 0;
2111
2112 /**
2113 * Allow a user to over-scroll this view only if the content is large
2114 * enough to meaningfully scroll, provided it is a view that can scroll.
2115 *
2116 * @see #getOverScrollMode()
2117 * @see #setOverScrollMode(int)
2118 */
2119 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
2120
2121 /**
2122 * Never allow a user to over-scroll this view.
2123 *
2124 * @see #getOverScrollMode()
2125 * @see #setOverScrollMode(int)
2126 */
2127 public static final int OVER_SCROLL_NEVER = 2;
2128
2129 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002130 * Special constant for {@link #setSystemUiVisibility(int)}: View has
2131 * requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08002132 *
Joe Malin32736f02011-01-19 16:14:20 -08002133 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002134 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002135 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08002136
2137 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002138 * Flag for {@link #setSystemUiVisibility(int)}: View has requested the
2139 * system UI to enter an unobtrusive "low profile" mode.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002140 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002141 * <p>This is for use in games, book readers, video players, or any other
Philip Milne6c8ea062012-04-03 17:38:43 -07002142 * "immersive" application where the usual system chrome is deemed too distracting.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002143 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002144 * <p>In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08002145 *
Joe Malin32736f02011-01-19 16:14:20 -08002146 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002147 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002148 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
2149
2150 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002151 * Flag for {@link #setSystemUiVisibility(int)}: View has requested that the
2152 * system navigation be temporarily hidden.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002153 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002154 * <p>This is an even less obtrusive state than that called for by
Daniel Sandler60ee2562011-07-22 12:34:33 -04002155 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
2156 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
2157 * those to disappear. This is useful (in conjunction with the
Philip Milne6c8ea062012-04-03 17:38:43 -07002158 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
Daniel Sandler60ee2562011-07-22 12:34:33 -04002159 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
2160 * window flags) for displaying content using every last pixel on the display.
2161 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002162 * <p>There is a limitation: because navigation controls are so important, the least user
2163 * interaction will cause them to reappear immediately. When this happens, both
2164 * this flag and {@link #SYSTEM_UI_FLAG_FULLSCREEN} will be cleared automatically,
2165 * so that both elements reappear at the same time.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002166 *
2167 * @see #setSystemUiVisibility(int)
2168 */
2169 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
2170
2171 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002172 * Flag for {@link #setSystemUiVisibility(int)}: View has requested to go
2173 * into the normal fullscreen mode so that its content can take over the screen
2174 * while still allowing the user to interact with the application.
2175 *
2176 * <p>This has the same visual effect as
2177 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN
2178 * WindowManager.LayoutParams.FLAG_FULLSCREEN},
2179 * meaning that non-critical screen decorations (such as the status bar) will be
2180 * hidden while the user is in the View's window, focusing the experience on
2181 * that content. Unlike the window flag, if you are using ActionBar in
2182 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2183 * Window.FEATURE_ACTION_BAR_OVERLAY}, then enabling this flag will also
2184 * hide the action bar.
2185 *
2186 * <p>This approach to going fullscreen is best used over the window flag when
2187 * it is a transient state -- that is, the application does this at certain
2188 * points in its user interaction where it wants to allow the user to focus
2189 * on content, but not as a continuous state. For situations where the application
2190 * would like to simply stay full screen the entire time (such as a game that
2191 * wants to take over the screen), the
2192 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN window flag}
2193 * is usually a better approach. The state set here will be removed by the system
2194 * in various situations (such as the user moving to another application) like
2195 * the other system UI states.
2196 *
2197 * <p>When using this flag, the application should provide some easy facility
2198 * for the user to go out of it. A common example would be in an e-book
2199 * reader, where tapping on the screen brings back whatever screen and UI
2200 * decorations that had been hidden while the user was immersed in reading
2201 * the book.
2202 *
2203 * @see #setSystemUiVisibility(int)
2204 */
2205 public static final int SYSTEM_UI_FLAG_FULLSCREEN = 0x00000004;
2206
2207 /**
2208 * Flag for {@link #setSystemUiVisibility(int)}: When using other layout
2209 * flags, we would like a stable view of the content insets given to
2210 * {@link #fitSystemWindows(Rect)}. This means that the insets seen there
2211 * will always represent the worst case that the application can expect
Dianne Hackborn5b5cc4d2012-05-16 13:15:00 -07002212 * as a continuous state. In the stock Android UI this is the space for
2213 * the system bar, nav bar, and status bar, but not more transient elements
2214 * such as an input method.
2215 *
2216 * The stable layout your UI sees is based on the system UI modes you can
2217 * switch to. That is, if you specify {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
2218 * then you will get a stable layout for changes of the
2219 * {@link #SYSTEM_UI_FLAG_FULLSCREEN} mode; if you specify
2220 * {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN} and
2221 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION}, then you can transition
2222 * to {@link #SYSTEM_UI_FLAG_FULLSCREEN} and {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}
2223 * with a stable layout. (Note that you should avoid using
2224 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} by itself.)
2225 *
2226 * If you have set the window flag {@ WindowManager.LayoutParams#FLAG_FULLSCREEN}
2227 * to hide the status bar (instead of using {@link #SYSTEM_UI_FLAG_FULLSCREEN}),
2228 * then a hidden status bar will be considered a "stable" state for purposes
2229 * here. This allows your UI to continually hide the status bar, while still
2230 * using the system UI flags to hide the action bar while still retaining
2231 * a stable layout. Note that changing the window fullscreen flag will never
2232 * provide a stable layout for a clean transition.
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002233 *
2234 * <p>If you are using ActionBar in
2235 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2236 * Window.FEATURE_ACTION_BAR_OVERLAY}, this flag will also impact the
2237 * insets it adds to those given to the application.
2238 */
2239 public static final int SYSTEM_UI_FLAG_LAYOUT_STABLE = 0x00000100;
2240
2241 /**
2242 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2243 * to be layed out as if it has requested
2244 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, even if it currently hasn't. This
2245 * allows it to avoid artifacts when switching in and out of that mode, at
2246 * the expense that some of its user interface may be covered by screen
2247 * decorations when they are shown. You can perform layout of your inner
2248 * UI elements to account for the navagation system UI through the
2249 * {@link #fitSystemWindows(Rect)} method.
2250 */
2251 public static final int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 0x00000200;
2252
2253 /**
2254 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2255 * to be layed out as if it has requested
2256 * {@link #SYSTEM_UI_FLAG_FULLSCREEN}, even if it currently hasn't. This
2257 * allows it to avoid artifacts when switching in and out of that mode, at
2258 * the expense that some of its user interface may be covered by screen
2259 * decorations when they are shown. You can perform layout of your inner
2260 * UI elements to account for non-fullscreen system UI through the
2261 * {@link #fitSystemWindows(Rect)} method.
2262 */
2263 public static final int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 0x00000400;
2264
2265 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04002266 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
2267 */
2268 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
2269
2270 /**
2271 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
2272 */
2273 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08002274
2275 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002276 * @hide
2277 *
2278 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2279 * out of the public fields to keep the undefined bits out of the developer's way.
2280 *
2281 * Flag to make the status bar not expandable. Unless you also
2282 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
2283 */
2284 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
2285
2286 /**
2287 * @hide
2288 *
2289 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2290 * out of the public fields to keep the undefined bits out of the developer's way.
2291 *
2292 * Flag to hide notification icons and scrolling ticker text.
2293 */
2294 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
2295
2296 /**
2297 * @hide
2298 *
2299 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2300 * out of the public fields to keep the undefined bits out of the developer's way.
2301 *
2302 * Flag to disable incoming notification alerts. This will not block
2303 * icons, but it will block sound, vibrating and other visual or aural notifications.
2304 */
2305 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
2306
2307 /**
2308 * @hide
2309 *
2310 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2311 * out of the public fields to keep the undefined bits out of the developer's way.
2312 *
2313 * Flag to hide only the scrolling ticker. Note that
2314 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
2315 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
2316 */
2317 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
2318
2319 /**
2320 * @hide
2321 *
2322 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2323 * out of the public fields to keep the undefined bits out of the developer's way.
2324 *
2325 * Flag to hide the center system info area.
2326 */
2327 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
2328
2329 /**
2330 * @hide
2331 *
2332 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2333 * out of the public fields to keep the undefined bits out of the developer's way.
2334 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002335 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002336 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2337 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04002338 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002339
2340 /**
2341 * @hide
2342 *
2343 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2344 * out of the public fields to keep the undefined bits out of the developer's way.
2345 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002346 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08002347 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2348 */
2349 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
2350
2351 /**
2352 * @hide
2353 *
2354 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2355 * out of the public fields to keep the undefined bits out of the developer's way.
2356 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002357 * Flag to hide only the clock. You might use this if your activity has
2358 * its own clock making the status bar's clock redundant.
2359 */
Joe Onorato6478adc2011-01-27 21:15:01 -08002360 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
2361
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002362 /**
2363 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04002364 *
2365 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2366 * out of the public fields to keep the undefined bits out of the developer's way.
2367 *
2368 * Flag to hide only the recent apps button. Don't use this
2369 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2370 */
2371 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
2372
2373 /**
2374 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002375 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002376 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002377
2378 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002379 * These are the system UI flags that can be cleared by events outside
2380 * of an application. Currently this is just the ability to tap on the
2381 * screen while hiding the navigation bar to have it return.
2382 * @hide
2383 */
2384 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002385 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION
2386 | SYSTEM_UI_FLAG_FULLSCREEN;
2387
2388 /**
2389 * Flags that can impact the layout in relation to system UI.
2390 */
2391 public static final int SYSTEM_UI_LAYOUT_FLAGS =
2392 SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
2393 | SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002394
2395 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07002396 * Find views that render the specified text.
2397 *
2398 * @see #findViewsWithText(ArrayList, CharSequence, int)
2399 */
2400 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
2401
2402 /**
2403 * Find find views that contain the specified content description.
2404 *
2405 * @see #findViewsWithText(ArrayList, CharSequence, int)
2406 */
2407 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
2408
2409 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07002410 * Find views that contain {@link AccessibilityNodeProvider}. Such
2411 * a View is a root of virtual view hierarchy and may contain the searched
2412 * text. If this flag is set Views with providers are automatically
2413 * added and it is a responsibility of the client to call the APIs of
2414 * the provider to determine whether the virtual tree rooted at this View
2415 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
2416 * represeting the virtual views with this text.
2417 *
2418 * @see #findViewsWithText(ArrayList, CharSequence, int)
2419 *
2420 * @hide
2421 */
2422 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
2423
2424 /**
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07002425 * The undefined cursor position.
2426 */
2427 private static final int ACCESSIBILITY_CURSOR_POSITION_UNDEFINED = -1;
2428
2429 /**
Romain Guybb9908b2012-03-08 11:14:07 -08002430 * Indicates that the screen has changed state and is now off.
2431 *
2432 * @see #onScreenStateChanged(int)
2433 */
2434 public static final int SCREEN_STATE_OFF = 0x0;
2435
2436 /**
2437 * Indicates that the screen has changed state and is now on.
2438 *
Romain Guy1e3d3132012-03-08 15:55:56 -08002439 * @see #onScreenStateChanged(int)
Romain Guybb9908b2012-03-08 11:14:07 -08002440 */
2441 public static final int SCREEN_STATE_ON = 0x1;
2442
2443 /**
Adam Powell637d3372010-08-25 14:37:03 -07002444 * Controls the over-scroll mode for this view.
2445 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2446 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2447 * and {@link #OVER_SCROLL_NEVER}.
2448 */
2449 private int mOverScrollMode;
2450
2451 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002452 * The parent this view is attached to.
2453 * {@hide}
2454 *
2455 * @see #getParent()
2456 */
2457 protected ViewParent mParent;
2458
2459 /**
2460 * {@hide}
2461 */
2462 AttachInfo mAttachInfo;
2463
2464 /**
2465 * {@hide}
2466 */
Romain Guy809a7f62009-05-14 15:44:42 -07002467 @ViewDebug.ExportedProperty(flagMapping = {
2468 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
2469 name = "FORCE_LAYOUT"),
2470 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
2471 name = "LAYOUT_REQUIRED"),
2472 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002473 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07002474 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
2475 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
2476 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2477 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
2478 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002479 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002480 int mPrivateFlags2;
Chet Haase21433372012-06-05 07:54:09 -07002481 int mPrivateFlags3;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002482
2483 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002484 * This view's request for the visibility of the status bar.
2485 * @hide
2486 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002487 @ViewDebug.ExportedProperty(flagMapping = {
2488 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2489 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2490 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2491 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2492 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2493 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2494 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2495 equals = SYSTEM_UI_FLAG_VISIBLE,
2496 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2497 })
Joe Onorato664644d2011-01-23 17:53:23 -08002498 int mSystemUiVisibility;
2499
2500 /**
Chet Haase563d4f22012-04-18 16:20:08 -07002501 * Reference count for transient state.
2502 * @see #setHasTransientState(boolean)
2503 */
2504 int mTransientStateCount = 0;
2505
2506 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002507 * Count of how many windows this view has been attached to.
2508 */
2509 int mWindowAttachCount;
2510
2511 /**
2512 * The layout parameters associated with this view and used by the parent
2513 * {@link android.view.ViewGroup} to determine how this view should be
2514 * laid out.
2515 * {@hide}
2516 */
2517 protected ViewGroup.LayoutParams mLayoutParams;
2518
2519 /**
2520 * The view flags hold various views states.
2521 * {@hide}
2522 */
2523 @ViewDebug.ExportedProperty
2524 int mViewFlags;
2525
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002526 static class TransformationInfo {
2527 /**
2528 * The transform matrix for the View. This transform is calculated internally
2529 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2530 * is used by default. Do *not* use this variable directly; instead call
2531 * getMatrix(), which will automatically recalculate the matrix if necessary
2532 * to get the correct matrix based on the latest rotation and scale properties.
2533 */
2534 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002535
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002536 /**
2537 * The transform matrix for the View. This transform is calculated internally
2538 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2539 * is used by default. Do *not* use this variable directly; instead call
2540 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2541 * to get the correct matrix based on the latest rotation and scale properties.
2542 */
2543 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002544
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002545 /**
2546 * An internal variable that tracks whether we need to recalculate the
2547 * transform matrix, based on whether the rotation or scaleX/Y properties
2548 * have changed since the matrix was last calculated.
2549 */
2550 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002551
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002552 /**
2553 * An internal variable that tracks whether we need to recalculate the
2554 * transform matrix, based on whether the rotation or scaleX/Y properties
2555 * have changed since the matrix was last calculated.
2556 */
2557 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002558
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002559 /**
2560 * A variable that tracks whether we need to recalculate the
2561 * transform matrix, based on whether the rotation or scaleX/Y properties
2562 * have changed since the matrix was last calculated. This variable
2563 * is only valid after a call to updateMatrix() or to a function that
2564 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2565 */
2566 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002567
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002568 /**
2569 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2570 */
2571 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002572
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002573 /**
2574 * This matrix is used when computing the matrix for 3D rotations.
2575 */
2576 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002577
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002578 /**
2579 * These prev values are used to recalculate a centered pivot point when necessary. The
2580 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2581 * set), so thes values are only used then as well.
2582 */
2583 private int mPrevWidth = -1;
2584 private int mPrevHeight = -1;
Philip Milne6c8ea062012-04-03 17:38:43 -07002585
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002586 /**
2587 * The degrees rotation around the vertical axis through the pivot point.
2588 */
2589 @ViewDebug.ExportedProperty
2590 float mRotationY = 0f;
2591
2592 /**
2593 * The degrees rotation around the horizontal axis through the pivot point.
2594 */
2595 @ViewDebug.ExportedProperty
2596 float mRotationX = 0f;
2597
2598 /**
2599 * The degrees rotation around the pivot point.
2600 */
2601 @ViewDebug.ExportedProperty
2602 float mRotation = 0f;
2603
2604 /**
2605 * The amount of translation of the object away from its left property (post-layout).
2606 */
2607 @ViewDebug.ExportedProperty
2608 float mTranslationX = 0f;
2609
2610 /**
2611 * The amount of translation of the object away from its top property (post-layout).
2612 */
2613 @ViewDebug.ExportedProperty
2614 float mTranslationY = 0f;
2615
2616 /**
2617 * The amount of scale in the x direction around the pivot point. A
2618 * value of 1 means no scaling is applied.
2619 */
2620 @ViewDebug.ExportedProperty
2621 float mScaleX = 1f;
2622
2623 /**
2624 * The amount of scale in the y direction around the pivot point. A
2625 * value of 1 means no scaling is applied.
2626 */
2627 @ViewDebug.ExportedProperty
2628 float mScaleY = 1f;
2629
2630 /**
Chet Haasea33de552012-02-03 16:28:24 -08002631 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002632 */
2633 @ViewDebug.ExportedProperty
2634 float mPivotX = 0f;
2635
2636 /**
Chet Haasea33de552012-02-03 16:28:24 -08002637 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002638 */
2639 @ViewDebug.ExportedProperty
2640 float mPivotY = 0f;
2641
2642 /**
2643 * The opacity of the View. This is a value from 0 to 1, where 0 means
2644 * completely transparent and 1 means completely opaque.
2645 */
2646 @ViewDebug.ExportedProperty
2647 float mAlpha = 1f;
2648 }
2649
2650 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002651
Joe Malin32736f02011-01-19 16:14:20 -08002652 private boolean mLastIsOpaque;
2653
Chet Haasefd2b0022010-08-06 13:08:56 -07002654 /**
2655 * Convenience value to check for float values that are close enough to zero to be considered
2656 * zero.
2657 */
Romain Guy2542d192010-08-18 11:47:12 -07002658 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002659
2660 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002661 * The distance in pixels from the left edge of this view's parent
2662 * to the left edge of this view.
2663 * {@hide}
2664 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002665 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002666 protected int mLeft;
2667 /**
2668 * The distance in pixels from the left edge of this view's parent
2669 * to the right edge of this view.
2670 * {@hide}
2671 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002672 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002673 protected int mRight;
2674 /**
2675 * The distance in pixels from the top edge of this view's parent
2676 * to the top edge of this view.
2677 * {@hide}
2678 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002679 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002680 protected int mTop;
2681 /**
2682 * The distance in pixels from the top edge of this view's parent
2683 * to the bottom edge of this view.
2684 * {@hide}
2685 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002686 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002687 protected int mBottom;
2688
2689 /**
2690 * The offset, in pixels, by which the content of this view is scrolled
2691 * horizontally.
2692 * {@hide}
2693 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002694 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002695 protected int mScrollX;
2696 /**
2697 * The offset, in pixels, by which the content of this view is scrolled
2698 * vertically.
2699 * {@hide}
2700 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002701 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002702 protected int mScrollY;
2703
2704 /**
2705 * The left padding in pixels, that is the distance in pixels between the
2706 * left edge of this view and the left edge of its content.
2707 * {@hide}
2708 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002709 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002710 protected int mPaddingLeft;
2711 /**
2712 * The right padding in pixels, that is the distance in pixels between the
2713 * right edge of this view and the right edge of its content.
2714 * {@hide}
2715 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002716 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002717 protected int mPaddingRight;
2718 /**
2719 * The top padding in pixels, that is the distance in pixels between the
2720 * top edge of this view and the top edge of its content.
2721 * {@hide}
2722 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002723 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002724 protected int mPaddingTop;
2725 /**
2726 * The bottom padding in pixels, that is the distance in pixels between the
2727 * bottom edge of this view and the bottom edge of its content.
2728 * {@hide}
2729 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002730 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002731 protected int mPaddingBottom;
2732
2733 /**
Philip Milne1557fd72012-04-04 23:41:34 -07002734 * The layout insets in pixels, that is the distance in pixels between the
2735 * visible edges of this view its bounds.
2736 */
2737 private Insets mLayoutInsets;
2738
2739 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002740 * Briefly describes the view and is primarily used for accessibility support.
2741 */
2742 private CharSequence mContentDescription;
2743
2744 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002745 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002746 *
2747 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002748 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002749 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002750 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002751
2752 /**
2753 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002754 *
2755 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002757 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002758 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002759
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002760 /**
Adam Powell20232d02010-12-08 21:08:53 -08002761 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002762 *
2763 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002764 */
2765 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002766 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002767
2768 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002769 * Cache if the user padding is relative.
2770 *
2771 */
2772 @ViewDebug.ExportedProperty(category = "padding")
2773 boolean mUserPaddingRelative;
2774
2775 /**
2776 * Cache the paddingStart set by the user to append to the scrollbar's size.
2777 *
2778 */
2779 @ViewDebug.ExportedProperty(category = "padding")
2780 int mUserPaddingStart;
2781
2782 /**
2783 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2784 *
2785 */
2786 @ViewDebug.ExportedProperty(category = "padding")
2787 int mUserPaddingEnd;
2788
2789 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002790 * @hide
2791 */
2792 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2793 /**
2794 * @hide
2795 */
2796 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002797
Philip Milne6c8ea062012-04-03 17:38:43 -07002798 private Drawable mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002799
2800 private int mBackgroundResource;
2801 private boolean mBackgroundSizeChanged;
2802
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002803 static class ListenerInfo {
2804 /**
2805 * Listener used to dispatch focus change events.
2806 * This field should be made private, so it is hidden from the SDK.
2807 * {@hide}
2808 */
2809 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002810
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002811 /**
2812 * Listeners for layout change events.
2813 */
2814 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002815
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002816 /**
2817 * Listeners for attach events.
2818 */
2819 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002820
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002821 /**
2822 * Listener used to dispatch click events.
2823 * This field should be made private, so it is hidden from the SDK.
2824 * {@hide}
2825 */
2826 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002827
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002828 /**
2829 * Listener used to dispatch long click events.
2830 * This field should be made private, so it is hidden from the SDK.
2831 * {@hide}
2832 */
2833 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002834
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002835 /**
2836 * Listener used to build the context menu.
2837 * This field should be made private, so it is hidden from the SDK.
2838 * {@hide}
2839 */
2840 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002841
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002842 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002843
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002844 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002845
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002846 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002847
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002848 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002849
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002850 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002851
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002852 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2853 }
2854
2855 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002857 /**
2858 * The application environment this view lives in.
2859 * This field should be made private, so it is hidden from the SDK.
2860 * {@hide}
2861 */
2862 protected Context mContext;
2863
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002864 private final Resources mResources;
2865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002866 private ScrollabilityCache mScrollCache;
2867
2868 private int[] mDrawableState = null;
2869
Romain Guy0211a0a2011-02-14 16:34:59 -08002870 /**
2871 * Set to true when drawing cache is enabled and cannot be created.
Philip Milne6c8ea062012-04-03 17:38:43 -07002872 *
Romain Guy0211a0a2011-02-14 16:34:59 -08002873 * @hide
2874 */
2875 public boolean mCachingFailed;
2876
Romain Guy02890fd2010-08-06 17:58:44 -07002877 private Bitmap mDrawingCache;
2878 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002879 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002880 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002881
2882 /**
2883 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2884 * the user may specify which view to go to next.
2885 */
2886 private int mNextFocusLeftId = View.NO_ID;
2887
2888 /**
2889 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2890 * the user may specify which view to go to next.
2891 */
2892 private int mNextFocusRightId = View.NO_ID;
2893
2894 /**
2895 * When this view has focus and the next focus is {@link #FOCUS_UP},
2896 * the user may specify which view to go to next.
2897 */
2898 private int mNextFocusUpId = View.NO_ID;
2899
2900 /**
2901 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2902 * the user may specify which view to go to next.
2903 */
2904 private int mNextFocusDownId = View.NO_ID;
2905
Jeff Brown4e6319b2010-12-13 10:36:51 -08002906 /**
2907 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2908 * the user may specify which view to go to next.
2909 */
2910 int mNextFocusForwardId = View.NO_ID;
2911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002912 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002913 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002914 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002915 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002917 private UnsetPressedState mUnsetPressedState;
2918
2919 /**
2920 * Whether the long press's action has been invoked. The tap's action is invoked on the
2921 * up event while a long press is invoked as soon as the long press duration is reached, so
2922 * a long press could be performed before the tap is checked, in which case the tap's action
2923 * should not be invoked.
2924 */
2925 private boolean mHasPerformedLongPress;
2926
2927 /**
2928 * The minimum height of the view. We'll try our best to have the height
2929 * of this view to at least this amount.
2930 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002931 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002932 private int mMinHeight;
2933
2934 /**
2935 * The minimum width of the view. We'll try our best to have the width
2936 * of this view to at least this amount.
2937 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002938 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002939 private int mMinWidth;
2940
2941 /**
2942 * The delegate to handle touch events that are physically in this view
2943 * but should be handled by another view.
2944 */
2945 private TouchDelegate mTouchDelegate = null;
2946
2947 /**
2948 * Solid color to use as a background when creating the drawing cache. Enables
2949 * the cache to use 16 bit bitmaps instead of 32 bit.
2950 */
2951 private int mDrawingCacheBackgroundColor = 0;
2952
2953 /**
2954 * Special tree observer used when mAttachInfo is null.
2955 */
2956 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002957
Adam Powelle14579b2009-12-16 18:39:52 -08002958 /**
2959 * Cache the touch slop from the context that created the view.
2960 */
2961 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002962
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002963 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002964 * Object that handles automatic animation of view properties.
2965 */
2966 private ViewPropertyAnimator mAnimator = null;
2967
2968 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002969 * Flag indicating that a drag can cross window boundaries. When
2970 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2971 * with this flag set, all visible applications will be able to participate
2972 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002973 *
2974 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002975 */
2976 public static final int DRAG_FLAG_GLOBAL = 1;
2977
2978 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002979 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2980 */
2981 private float mVerticalScrollFactor;
2982
2983 /**
Adam Powell20232d02010-12-08 21:08:53 -08002984 * Position of the vertical scroll bar.
2985 */
2986 private int mVerticalScrollbarPosition;
2987
2988 /**
2989 * Position the scroll bar at the default position as determined by the system.
2990 */
2991 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2992
2993 /**
2994 * Position the scroll bar along the left edge.
2995 */
2996 public static final int SCROLLBAR_POSITION_LEFT = 1;
2997
2998 /**
2999 * Position the scroll bar along the right edge.
3000 */
3001 public static final int SCROLLBAR_POSITION_RIGHT = 2;
3002
3003 /**
Romain Guy171c5922011-01-06 10:04:23 -08003004 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08003005 *
3006 * @see #getLayerType()
3007 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003008 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08003009 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003010 */
3011 public static final int LAYER_TYPE_NONE = 0;
3012
3013 /**
3014 * <p>Indicates that the view has a software layer. A software layer is backed
3015 * by a bitmap and causes the view to be rendered using Android's software
3016 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003017 *
Romain Guy171c5922011-01-06 10:04:23 -08003018 * <p>Software layers have various usages:</p>
3019 * <p>When the application is not using hardware acceleration, a software layer
3020 * is useful to apply a specific color filter and/or blending mode and/or
3021 * translucency to a view and all its children.</p>
3022 * <p>When the application is using hardware acceleration, a software layer
3023 * is useful to render drawing primitives not supported by the hardware
3024 * accelerated pipeline. It can also be used to cache a complex view tree
3025 * into a texture and reduce the complexity of drawing operations. For instance,
3026 * when animating a complex view tree with a translation, a software layer can
3027 * be used to render the view tree only once.</p>
3028 * <p>Software layers should be avoided when the affected view tree updates
3029 * often. Every update will require to re-render the software layer, which can
3030 * potentially be slow (particularly when hardware acceleration is turned on
3031 * since the layer will have to be uploaded into a hardware texture after every
3032 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08003033 *
3034 * @see #getLayerType()
3035 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003036 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08003037 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003038 */
3039 public static final int LAYER_TYPE_SOFTWARE = 1;
3040
3041 /**
3042 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
3043 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
3044 * OpenGL hardware) and causes the view to be rendered using Android's hardware
3045 * rendering pipeline, but only if hardware acceleration is turned on for the
3046 * view hierarchy. When hardware acceleration is turned off, hardware layers
3047 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003048 *
Romain Guy171c5922011-01-06 10:04:23 -08003049 * <p>A hardware layer is useful to apply a specific color filter and/or
3050 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08003051 * <p>A hardware layer can be used to cache a complex view tree into a
3052 * texture and reduce the complexity of drawing operations. For instance,
3053 * when animating a complex view tree with a translation, a hardware layer can
3054 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08003055 * <p>A hardware layer can also be used to increase the rendering quality when
3056 * rotation transformations are applied on a view. It can also be used to
3057 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003058 *
3059 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08003060 * @see #setLayerType(int, android.graphics.Paint)
3061 * @see #LAYER_TYPE_NONE
3062 * @see #LAYER_TYPE_SOFTWARE
3063 */
3064 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08003065
Romain Guy3aaff3a2011-01-12 14:18:47 -08003066 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
3067 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
3068 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
3069 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
3070 })
Romain Guy171c5922011-01-06 10:04:23 -08003071 int mLayerType = LAYER_TYPE_NONE;
3072 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08003073 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08003074
3075 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07003076 * Set to true when the view is sending hover accessibility events because it
3077 * is the innermost hovered view.
3078 */
3079 private boolean mSendingHoverAccessibilityEvents;
3080
3081 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003082 * Simple constructor to use when creating a view from code.
3083 *
3084 * @param context The Context the view is running in, through which it can
3085 * access the current theme, resources, etc.
3086 */
3087 public View(Context context) {
3088 mContext = context;
3089 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003090 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003091 // Set layout and text direction defaults
3092 mPrivateFlags2 = (LAYOUT_DIRECTION_DEFAULT << LAYOUT_DIRECTION_MASK_SHIFT) |
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003093 (TEXT_DIRECTION_DEFAULT << TEXT_DIRECTION_MASK_SHIFT) |
Svetoslav Ganov42138042012-03-20 11:51:39 -07003094 (TEXT_ALIGNMENT_DEFAULT << TEXT_ALIGNMENT_MASK_SHIFT) |
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07003095 (IMPORTANT_FOR_ACCESSIBILITY_DEFAULT << IMPORTANT_FOR_ACCESSIBILITY_SHIFT);
Adam Powelle14579b2009-12-16 18:39:52 -08003096 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07003097 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003098 mUserPaddingStart = -1;
3099 mUserPaddingEnd = -1;
3100 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003101 }
3102
3103 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07003104 * Delegate for injecting accessiblity functionality.
3105 */
3106 AccessibilityDelegate mAccessibilityDelegate;
3107
3108 /**
3109 * Consistency verifier for debugging purposes.
3110 * @hide
3111 */
3112 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
3113 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
3114 new InputEventConsistencyVerifier(this, 0) : null;
3115
3116 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003117 * Constructor that is called when inflating a view from XML. This is called
3118 * when a view is being constructed from an XML file, supplying attributes
3119 * that were specified in the XML file. This version uses a default style of
3120 * 0, so the only attribute values applied are those in the Context's Theme
3121 * and the given AttributeSet.
3122 *
3123 * <p>
3124 * The method onFinishInflate() will be called after all children have been
3125 * added.
3126 *
3127 * @param context The Context the view is running in, through which it can
3128 * access the current theme, resources, etc.
3129 * @param attrs The attributes of the XML tag that is inflating the view.
3130 * @see #View(Context, AttributeSet, int)
3131 */
3132 public View(Context context, AttributeSet attrs) {
3133 this(context, attrs, 0);
3134 }
3135
3136 /**
3137 * Perform inflation from XML and apply a class-specific base style. This
3138 * constructor of View allows subclasses to use their own base style when
3139 * they are inflating. For example, a Button class's constructor would call
3140 * this version of the super class constructor and supply
3141 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
3142 * the theme's button style to modify all of the base view attributes (in
3143 * particular its background) as well as the Button class's attributes.
3144 *
3145 * @param context The Context the view is running in, through which it can
3146 * access the current theme, resources, etc.
3147 * @param attrs The attributes of the XML tag that is inflating the view.
3148 * @param defStyle The default style to apply to this view. If 0, no style
3149 * will be applied (beyond what is included in the theme). This may
3150 * either be an attribute resource, whose value will be retrieved
3151 * from the current theme, or an explicit style resource.
3152 * @see #View(Context, AttributeSet)
3153 */
3154 public View(Context context, AttributeSet attrs, int defStyle) {
3155 this(context);
3156
3157 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
3158 defStyle, 0);
3159
3160 Drawable background = null;
3161
3162 int leftPadding = -1;
3163 int topPadding = -1;
3164 int rightPadding = -1;
3165 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003166 int startPadding = -1;
3167 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003168
3169 int padding = -1;
3170
3171 int viewFlagValues = 0;
3172 int viewFlagMasks = 0;
3173
3174 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07003175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003176 int x = 0;
3177 int y = 0;
3178
Chet Haase73066682010-11-29 15:55:32 -08003179 float tx = 0;
3180 float ty = 0;
3181 float rotation = 0;
3182 float rotationX = 0;
3183 float rotationY = 0;
3184 float sx = 1f;
3185 float sy = 1f;
3186 boolean transformSet = false;
3187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003188 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
3189
Adam Powell637d3372010-08-25 14:37:03 -07003190 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003191 final int N = a.getIndexCount();
3192 for (int i = 0; i < N; i++) {
3193 int attr = a.getIndex(i);
3194 switch (attr) {
3195 case com.android.internal.R.styleable.View_background:
3196 background = a.getDrawable(attr);
3197 break;
3198 case com.android.internal.R.styleable.View_padding:
3199 padding = a.getDimensionPixelSize(attr, -1);
3200 break;
3201 case com.android.internal.R.styleable.View_paddingLeft:
3202 leftPadding = a.getDimensionPixelSize(attr, -1);
3203 break;
3204 case com.android.internal.R.styleable.View_paddingTop:
3205 topPadding = a.getDimensionPixelSize(attr, -1);
3206 break;
3207 case com.android.internal.R.styleable.View_paddingRight:
3208 rightPadding = a.getDimensionPixelSize(attr, -1);
3209 break;
3210 case com.android.internal.R.styleable.View_paddingBottom:
3211 bottomPadding = a.getDimensionPixelSize(attr, -1);
3212 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003213 case com.android.internal.R.styleable.View_paddingStart:
3214 startPadding = a.getDimensionPixelSize(attr, -1);
3215 break;
3216 case com.android.internal.R.styleable.View_paddingEnd:
3217 endPadding = a.getDimensionPixelSize(attr, -1);
3218 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003219 case com.android.internal.R.styleable.View_scrollX:
3220 x = a.getDimensionPixelOffset(attr, 0);
3221 break;
3222 case com.android.internal.R.styleable.View_scrollY:
3223 y = a.getDimensionPixelOffset(attr, 0);
3224 break;
Chet Haase73066682010-11-29 15:55:32 -08003225 case com.android.internal.R.styleable.View_alpha:
3226 setAlpha(a.getFloat(attr, 1f));
3227 break;
3228 case com.android.internal.R.styleable.View_transformPivotX:
3229 setPivotX(a.getDimensionPixelOffset(attr, 0));
3230 break;
3231 case com.android.internal.R.styleable.View_transformPivotY:
3232 setPivotY(a.getDimensionPixelOffset(attr, 0));
3233 break;
3234 case com.android.internal.R.styleable.View_translationX:
3235 tx = a.getDimensionPixelOffset(attr, 0);
3236 transformSet = true;
3237 break;
3238 case com.android.internal.R.styleable.View_translationY:
3239 ty = a.getDimensionPixelOffset(attr, 0);
3240 transformSet = true;
3241 break;
3242 case com.android.internal.R.styleable.View_rotation:
3243 rotation = a.getFloat(attr, 0);
3244 transformSet = true;
3245 break;
3246 case com.android.internal.R.styleable.View_rotationX:
3247 rotationX = a.getFloat(attr, 0);
3248 transformSet = true;
3249 break;
3250 case com.android.internal.R.styleable.View_rotationY:
3251 rotationY = a.getFloat(attr, 0);
3252 transformSet = true;
3253 break;
3254 case com.android.internal.R.styleable.View_scaleX:
3255 sx = a.getFloat(attr, 1f);
3256 transformSet = true;
3257 break;
3258 case com.android.internal.R.styleable.View_scaleY:
3259 sy = a.getFloat(attr, 1f);
3260 transformSet = true;
3261 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003262 case com.android.internal.R.styleable.View_id:
3263 mID = a.getResourceId(attr, NO_ID);
3264 break;
3265 case com.android.internal.R.styleable.View_tag:
3266 mTag = a.getText(attr);
3267 break;
3268 case com.android.internal.R.styleable.View_fitsSystemWindows:
3269 if (a.getBoolean(attr, false)) {
3270 viewFlagValues |= FITS_SYSTEM_WINDOWS;
3271 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
3272 }
3273 break;
3274 case com.android.internal.R.styleable.View_focusable:
3275 if (a.getBoolean(attr, false)) {
3276 viewFlagValues |= FOCUSABLE;
3277 viewFlagMasks |= FOCUSABLE_MASK;
3278 }
3279 break;
3280 case com.android.internal.R.styleable.View_focusableInTouchMode:
3281 if (a.getBoolean(attr, false)) {
3282 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
3283 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
3284 }
3285 break;
3286 case com.android.internal.R.styleable.View_clickable:
3287 if (a.getBoolean(attr, false)) {
3288 viewFlagValues |= CLICKABLE;
3289 viewFlagMasks |= CLICKABLE;
3290 }
3291 break;
3292 case com.android.internal.R.styleable.View_longClickable:
3293 if (a.getBoolean(attr, false)) {
3294 viewFlagValues |= LONG_CLICKABLE;
3295 viewFlagMasks |= LONG_CLICKABLE;
3296 }
3297 break;
3298 case com.android.internal.R.styleable.View_saveEnabled:
3299 if (!a.getBoolean(attr, true)) {
3300 viewFlagValues |= SAVE_DISABLED;
3301 viewFlagMasks |= SAVE_DISABLED_MASK;
3302 }
3303 break;
3304 case com.android.internal.R.styleable.View_duplicateParentState:
3305 if (a.getBoolean(attr, false)) {
3306 viewFlagValues |= DUPLICATE_PARENT_STATE;
3307 viewFlagMasks |= DUPLICATE_PARENT_STATE;
3308 }
3309 break;
3310 case com.android.internal.R.styleable.View_visibility:
3311 final int visibility = a.getInt(attr, 0);
3312 if (visibility != 0) {
3313 viewFlagValues |= VISIBILITY_FLAGS[visibility];
3314 viewFlagMasks |= VISIBILITY_MASK;
3315 }
3316 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003317 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003318 // Clear any layout direction flags (included resolved bits) already set
3319 mPrivateFlags2 &= ~(LAYOUT_DIRECTION_MASK | LAYOUT_DIRECTION_RESOLVED_MASK);
3320 // Set the layout direction flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003321 final int layoutDirection = a.getInt(attr, -1);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003322 final int value = (layoutDirection != -1) ?
3323 LAYOUT_DIRECTION_FLAGS[layoutDirection] : LAYOUT_DIRECTION_DEFAULT;
3324 mPrivateFlags2 |= (value << LAYOUT_DIRECTION_MASK_SHIFT);
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07003325 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003326 case com.android.internal.R.styleable.View_drawingCacheQuality:
3327 final int cacheQuality = a.getInt(attr, 0);
3328 if (cacheQuality != 0) {
3329 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
3330 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
3331 }
3332 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07003333 case com.android.internal.R.styleable.View_contentDescription:
Svetoslav Ganove47957a2012-06-05 14:46:50 -07003334 setContentDescription(a.getString(attr));
svetoslavganov75986cf2009-05-14 22:28:01 -07003335 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003336 case com.android.internal.R.styleable.View_soundEffectsEnabled:
3337 if (!a.getBoolean(attr, true)) {
3338 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
3339 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
3340 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003341 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003342 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
3343 if (!a.getBoolean(attr, true)) {
3344 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
3345 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
3346 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003347 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003348 case R.styleable.View_scrollbars:
3349 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
3350 if (scrollbars != SCROLLBARS_NONE) {
3351 viewFlagValues |= scrollbars;
3352 viewFlagMasks |= SCROLLBARS_MASK;
3353 initializeScrollbars(a);
3354 }
3355 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07003356 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003357 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07003358 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
3359 // Ignore the attribute starting with ICS
3360 break;
3361 }
3362 // With builds < ICS, fall through and apply fading edges
3363 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003364 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
3365 if (fadingEdge != FADING_EDGE_NONE) {
3366 viewFlagValues |= fadingEdge;
3367 viewFlagMasks |= FADING_EDGE_MASK;
3368 initializeFadingEdge(a);
3369 }
3370 break;
3371 case R.styleable.View_scrollbarStyle:
3372 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3373 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3374 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3375 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3376 }
3377 break;
3378 case R.styleable.View_isScrollContainer:
3379 setScrollContainer = true;
3380 if (a.getBoolean(attr, false)) {
3381 setScrollContainer(true);
3382 }
3383 break;
3384 case com.android.internal.R.styleable.View_keepScreenOn:
3385 if (a.getBoolean(attr, false)) {
3386 viewFlagValues |= KEEP_SCREEN_ON;
3387 viewFlagMasks |= KEEP_SCREEN_ON;
3388 }
3389 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003390 case R.styleable.View_filterTouchesWhenObscured:
3391 if (a.getBoolean(attr, false)) {
3392 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3393 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3394 }
3395 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003396 case R.styleable.View_nextFocusLeft:
3397 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3398 break;
3399 case R.styleable.View_nextFocusRight:
3400 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3401 break;
3402 case R.styleable.View_nextFocusUp:
3403 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3404 break;
3405 case R.styleable.View_nextFocusDown:
3406 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3407 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003408 case R.styleable.View_nextFocusForward:
3409 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3410 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003411 case R.styleable.View_minWidth:
3412 mMinWidth = a.getDimensionPixelSize(attr, 0);
3413 break;
3414 case R.styleable.View_minHeight:
3415 mMinHeight = a.getDimensionPixelSize(attr, 0);
3416 break;
Romain Guy9a817362009-05-01 10:57:14 -07003417 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003418 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003419 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003420 + "be used within a restricted context");
3421 }
3422
Romain Guy9a817362009-05-01 10:57:14 -07003423 final String handlerName = a.getString(attr);
3424 if (handlerName != null) {
3425 setOnClickListener(new OnClickListener() {
3426 private Method mHandler;
3427
3428 public void onClick(View v) {
3429 if (mHandler == null) {
3430 try {
3431 mHandler = getContext().getClass().getMethod(handlerName,
3432 View.class);
3433 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003434 int id = getId();
3435 String idText = id == NO_ID ? "" : " with id '"
3436 + getContext().getResources().getResourceEntryName(
3437 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003438 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003439 handlerName + "(View) in the activity "
3440 + getContext().getClass() + " for onClick handler"
3441 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003442 }
3443 }
3444
3445 try {
3446 mHandler.invoke(getContext(), View.this);
3447 } catch (IllegalAccessException e) {
3448 throw new IllegalStateException("Could not execute non "
3449 + "public method of the activity", e);
3450 } catch (InvocationTargetException e) {
3451 throw new IllegalStateException("Could not execute "
3452 + "method of the activity", e);
3453 }
3454 }
3455 });
3456 }
3457 break;
Adam Powell637d3372010-08-25 14:37:03 -07003458 case R.styleable.View_overScrollMode:
3459 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3460 break;
Adam Powell20232d02010-12-08 21:08:53 -08003461 case R.styleable.View_verticalScrollbarPosition:
3462 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3463 break;
Romain Guy171c5922011-01-06 10:04:23 -08003464 case R.styleable.View_layerType:
3465 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3466 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003467 case R.styleable.View_textDirection:
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003468 // Clear any text direction flag already set
3469 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
3470 // Set the text direction flags depending on the value of the attribute
3471 final int textDirection = a.getInt(attr, -1);
3472 if (textDirection != -1) {
3473 mPrivateFlags2 |= TEXT_DIRECTION_FLAGS[textDirection];
3474 }
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003475 break;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003476 case R.styleable.View_textAlignment:
3477 // Clear any text alignment flag already set
3478 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
3479 // Set the text alignment flag depending on the value of the attribute
3480 final int textAlignment = a.getInt(attr, TEXT_ALIGNMENT_DEFAULT);
3481 mPrivateFlags2 |= TEXT_ALIGNMENT_FLAGS[textAlignment];
3482 break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07003483 case R.styleable.View_importantForAccessibility:
3484 setImportantForAccessibility(a.getInt(attr,
3485 IMPORTANT_FOR_ACCESSIBILITY_DEFAULT));
Svetoslav Ganov86783472012-06-06 21:12:20 -07003486 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003487 }
3488 }
3489
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003490 a.recycle();
3491
Adam Powell637d3372010-08-25 14:37:03 -07003492 setOverScrollMode(overScrollMode);
3493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003494 if (background != null) {
Philip Milne6c8ea062012-04-03 17:38:43 -07003495 setBackground(background);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003496 }
3497
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003498 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3499 // layout direction). Those cached values will be used later during padding resolution.
3500 mUserPaddingStart = startPadding;
3501 mUserPaddingEnd = endPadding;
3502
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003503 updateUserPaddingRelative();
3504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003505 if (padding >= 0) {
3506 leftPadding = padding;
3507 topPadding = padding;
3508 rightPadding = padding;
3509 bottomPadding = padding;
3510 }
3511
3512 // If the user specified the padding (either with android:padding or
3513 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3514 // use the default padding or the padding from the background drawable
3515 // (stored at this point in mPadding*)
3516 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3517 topPadding >= 0 ? topPadding : mPaddingTop,
3518 rightPadding >= 0 ? rightPadding : mPaddingRight,
3519 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3520
3521 if (viewFlagMasks != 0) {
3522 setFlags(viewFlagValues, viewFlagMasks);
3523 }
3524
3525 // Needs to be called after mViewFlags is set
3526 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3527 recomputePadding();
3528 }
3529
3530 if (x != 0 || y != 0) {
3531 scrollTo(x, y);
3532 }
3533
Chet Haase73066682010-11-29 15:55:32 -08003534 if (transformSet) {
3535 setTranslationX(tx);
3536 setTranslationY(ty);
3537 setRotation(rotation);
3538 setRotationX(rotationX);
3539 setRotationY(rotationY);
3540 setScaleX(sx);
3541 setScaleY(sy);
3542 }
3543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003544 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3545 setScrollContainer(true);
3546 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003547
3548 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003549 }
3550
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003551 private void updateUserPaddingRelative() {
3552 mUserPaddingRelative = (mUserPaddingStart >= 0 || mUserPaddingEnd >= 0);
3553 }
3554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003555 /**
3556 * Non-public constructor for use in testing
3557 */
3558 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003559 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003560 }
3561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003562 /**
3563 * <p>
3564 * Initializes the fading edges from a given set of styled attributes. This
3565 * method should be called by subclasses that need fading edges and when an
3566 * instance of these subclasses is created programmatically rather than
3567 * being inflated from XML. This method is automatically called when the XML
3568 * is inflated.
3569 * </p>
3570 *
3571 * @param a the styled attributes set to initialize the fading edges from
3572 */
3573 protected void initializeFadingEdge(TypedArray a) {
3574 initScrollCache();
3575
3576 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3577 R.styleable.View_fadingEdgeLength,
3578 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3579 }
3580
3581 /**
3582 * Returns the size of the vertical faded edges used to indicate that more
3583 * content in this view is visible.
3584 *
3585 * @return The size in pixels of the vertical faded edge or 0 if vertical
3586 * faded edges are not enabled for this view.
3587 * @attr ref android.R.styleable#View_fadingEdgeLength
3588 */
3589 public int getVerticalFadingEdgeLength() {
3590 if (isVerticalFadingEdgeEnabled()) {
3591 ScrollabilityCache cache = mScrollCache;
3592 if (cache != null) {
3593 return cache.fadingEdgeLength;
3594 }
3595 }
3596 return 0;
3597 }
3598
3599 /**
3600 * Set the size of the faded edge used to indicate that more content in this
3601 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003602 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3603 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3604 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003605 *
3606 * @param length The size in pixels of the faded edge used to indicate that more
3607 * content in this view is visible.
3608 */
3609 public void setFadingEdgeLength(int length) {
3610 initScrollCache();
3611 mScrollCache.fadingEdgeLength = length;
3612 }
3613
3614 /**
3615 * Returns the size of the horizontal faded edges used to indicate that more
3616 * content in this view is visible.
3617 *
3618 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3619 * faded edges are not enabled for this view.
3620 * @attr ref android.R.styleable#View_fadingEdgeLength
3621 */
3622 public int getHorizontalFadingEdgeLength() {
3623 if (isHorizontalFadingEdgeEnabled()) {
3624 ScrollabilityCache cache = mScrollCache;
3625 if (cache != null) {
3626 return cache.fadingEdgeLength;
3627 }
3628 }
3629 return 0;
3630 }
3631
3632 /**
3633 * Returns the width of the vertical scrollbar.
3634 *
3635 * @return The width in pixels of the vertical scrollbar or 0 if there
3636 * is no vertical scrollbar.
3637 */
3638 public int getVerticalScrollbarWidth() {
3639 ScrollabilityCache cache = mScrollCache;
3640 if (cache != null) {
3641 ScrollBarDrawable scrollBar = cache.scrollBar;
3642 if (scrollBar != null) {
3643 int size = scrollBar.getSize(true);
3644 if (size <= 0) {
3645 size = cache.scrollBarSize;
3646 }
3647 return size;
3648 }
3649 return 0;
3650 }
3651 return 0;
3652 }
3653
3654 /**
3655 * Returns the height of the horizontal scrollbar.
3656 *
3657 * @return The height in pixels of the horizontal scrollbar or 0 if
3658 * there is no horizontal scrollbar.
3659 */
3660 protected int getHorizontalScrollbarHeight() {
3661 ScrollabilityCache cache = mScrollCache;
3662 if (cache != null) {
3663 ScrollBarDrawable scrollBar = cache.scrollBar;
3664 if (scrollBar != null) {
3665 int size = scrollBar.getSize(false);
3666 if (size <= 0) {
3667 size = cache.scrollBarSize;
3668 }
3669 return size;
3670 }
3671 return 0;
3672 }
3673 return 0;
3674 }
3675
3676 /**
3677 * <p>
3678 * Initializes the scrollbars from a given set of styled attributes. This
3679 * method should be called by subclasses that need scrollbars and when an
3680 * instance of these subclasses is created programmatically rather than
3681 * being inflated from XML. This method is automatically called when the XML
3682 * is inflated.
3683 * </p>
3684 *
3685 * @param a the styled attributes set to initialize the scrollbars from
3686 */
3687 protected void initializeScrollbars(TypedArray a) {
3688 initScrollCache();
3689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003690 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003691
Mike Cleronf116bf82009-09-27 19:14:12 -07003692 if (scrollabilityCache.scrollBar == null) {
3693 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3694 }
Joe Malin32736f02011-01-19 16:14:20 -08003695
Romain Guy8bda2482010-03-02 11:42:11 -08003696 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003697
Mike Cleronf116bf82009-09-27 19:14:12 -07003698 if (!fadeScrollbars) {
3699 scrollabilityCache.state = ScrollabilityCache.ON;
3700 }
3701 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003702
3703
Mike Cleronf116bf82009-09-27 19:14:12 -07003704 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3705 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3706 .getScrollBarFadeDuration());
3707 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3708 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3709 ViewConfiguration.getScrollDefaultDelay());
3710
Joe Malin32736f02011-01-19 16:14:20 -08003711
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003712 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3713 com.android.internal.R.styleable.View_scrollbarSize,
3714 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3715
3716 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3717 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3718
3719 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3720 if (thumb != null) {
3721 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3722 }
3723
3724 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3725 false);
3726 if (alwaysDraw) {
3727 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3728 }
3729
3730 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3731 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3732
3733 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3734 if (thumb != null) {
3735 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3736 }
3737
3738 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3739 false);
3740 if (alwaysDraw) {
3741 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3742 }
3743
Fabrice Di Megliob03b4342012-06-04 12:55:30 -07003744 // Apply layout direction to the new Drawables if needed
3745 final int layoutDirection = getResolvedLayoutDirection();
3746 if (track != null) {
3747 track.setLayoutDirection(layoutDirection);
3748 }
3749 if (thumb != null) {
3750 thumb.setLayoutDirection(layoutDirection);
3751 }
3752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003753 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003754 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003755 }
3756
3757 /**
3758 * <p>
3759 * Initalizes the scrollability cache if necessary.
3760 * </p>
3761 */
3762 private void initScrollCache() {
3763 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003764 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003765 }
3766 }
3767
Philip Milne6c8ea062012-04-03 17:38:43 -07003768 private ScrollabilityCache getScrollCache() {
3769 initScrollCache();
3770 return mScrollCache;
3771 }
3772
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003773 /**
Adam Powell20232d02010-12-08 21:08:53 -08003774 * Set the position of the vertical scroll bar. Should be one of
3775 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3776 * {@link #SCROLLBAR_POSITION_RIGHT}.
3777 *
3778 * @param position Where the vertical scroll bar should be positioned.
3779 */
3780 public void setVerticalScrollbarPosition(int position) {
3781 if (mVerticalScrollbarPosition != position) {
3782 mVerticalScrollbarPosition = position;
3783 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003784 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003785 }
3786 }
3787
3788 /**
3789 * @return The position where the vertical scroll bar will show, if applicable.
3790 * @see #setVerticalScrollbarPosition(int)
3791 */
3792 public int getVerticalScrollbarPosition() {
3793 return mVerticalScrollbarPosition;
3794 }
3795
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003796 ListenerInfo getListenerInfo() {
3797 if (mListenerInfo != null) {
3798 return mListenerInfo;
3799 }
3800 mListenerInfo = new ListenerInfo();
3801 return mListenerInfo;
3802 }
3803
Adam Powell20232d02010-12-08 21:08:53 -08003804 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003805 * Register a callback to be invoked when focus of this view changed.
3806 *
3807 * @param l The callback that will run.
3808 */
3809 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003810 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003811 }
3812
3813 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003814 * Add a listener that will be called when the bounds of the view change due to
3815 * layout processing.
3816 *
3817 * @param listener The listener that will be called when layout bounds change.
3818 */
3819 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003820 ListenerInfo li = getListenerInfo();
3821 if (li.mOnLayoutChangeListeners == null) {
3822 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003823 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003824 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3825 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003826 }
Chet Haase21cd1382010-09-01 17:42:29 -07003827 }
3828
3829 /**
3830 * Remove a listener for layout changes.
3831 *
3832 * @param listener The listener for layout bounds change.
3833 */
3834 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003835 ListenerInfo li = mListenerInfo;
3836 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003837 return;
3838 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003839 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003840 }
3841
3842 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003843 * Add a listener for attach state changes.
3844 *
3845 * This listener will be called whenever this view is attached or detached
3846 * from a window. Remove the listener using
3847 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3848 *
3849 * @param listener Listener to attach
3850 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3851 */
3852 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003853 ListenerInfo li = getListenerInfo();
3854 if (li.mOnAttachStateChangeListeners == null) {
3855 li.mOnAttachStateChangeListeners
3856 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003857 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003858 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003859 }
3860
3861 /**
3862 * Remove a listener for attach state changes. The listener will receive no further
3863 * notification of window attach/detach events.
3864 *
3865 * @param listener Listener to remove
3866 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3867 */
3868 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003869 ListenerInfo li = mListenerInfo;
3870 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003871 return;
3872 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003873 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003874 }
3875
3876 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003877 * Returns the focus-change callback registered for this view.
3878 *
3879 * @return The callback, or null if one is not registered.
3880 */
3881 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003882 ListenerInfo li = mListenerInfo;
3883 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003884 }
3885
3886 /**
3887 * Register a callback to be invoked when this view is clicked. If this view is not
3888 * clickable, it becomes clickable.
3889 *
3890 * @param l The callback that will run
3891 *
3892 * @see #setClickable(boolean)
3893 */
3894 public void setOnClickListener(OnClickListener l) {
3895 if (!isClickable()) {
3896 setClickable(true);
3897 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003898 getListenerInfo().mOnClickListener = l;
3899 }
3900
3901 /**
3902 * Return whether this view has an attached OnClickListener. Returns
3903 * true if there is a listener, false if there is none.
3904 */
3905 public boolean hasOnClickListeners() {
3906 ListenerInfo li = mListenerInfo;
3907 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003908 }
3909
3910 /**
3911 * Register a callback to be invoked when this view is clicked and held. If this view is not
3912 * long clickable, it becomes long clickable.
3913 *
3914 * @param l The callback that will run
3915 *
3916 * @see #setLongClickable(boolean)
3917 */
3918 public void setOnLongClickListener(OnLongClickListener l) {
3919 if (!isLongClickable()) {
3920 setLongClickable(true);
3921 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003922 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003923 }
3924
3925 /**
3926 * Register a callback to be invoked when the context menu for this view is
3927 * being built. If this view is not long clickable, it becomes long clickable.
3928 *
3929 * @param l The callback that will run
3930 *
3931 */
3932 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3933 if (!isLongClickable()) {
3934 setLongClickable(true);
3935 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003936 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003937 }
3938
3939 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003940 * Call this view's OnClickListener, if it is defined. Performs all normal
3941 * actions associated with clicking: reporting accessibility event, playing
3942 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003943 *
3944 * @return True there was an assigned OnClickListener that was called, false
3945 * otherwise is returned.
3946 */
3947 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003948 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3949
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003950 ListenerInfo li = mListenerInfo;
3951 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003952 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003953 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003954 return true;
3955 }
3956
3957 return false;
3958 }
3959
3960 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003961 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
3962 * this only calls the listener, and does not do any associated clicking
3963 * actions like reporting an accessibility event.
3964 *
3965 * @return True there was an assigned OnClickListener that was called, false
3966 * otherwise is returned.
3967 */
3968 public boolean callOnClick() {
3969 ListenerInfo li = mListenerInfo;
3970 if (li != null && li.mOnClickListener != null) {
3971 li.mOnClickListener.onClick(this);
3972 return true;
3973 }
3974 return false;
3975 }
3976
3977 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003978 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3979 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003980 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003981 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003982 */
3983 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003984 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3985
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003986 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003987 ListenerInfo li = mListenerInfo;
3988 if (li != null && li.mOnLongClickListener != null) {
3989 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003990 }
3991 if (!handled) {
3992 handled = showContextMenu();
3993 }
3994 if (handled) {
3995 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3996 }
3997 return handled;
3998 }
3999
4000 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004001 * Performs button-related actions during a touch down event.
4002 *
4003 * @param event The event.
4004 * @return True if the down was consumed.
4005 *
4006 * @hide
4007 */
4008 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
4009 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
4010 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
4011 return true;
4012 }
4013 }
4014 return false;
4015 }
4016
4017 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004018 * Bring up the context menu for this view.
4019 *
4020 * @return Whether a context menu was displayed.
4021 */
4022 public boolean showContextMenu() {
4023 return getParent().showContextMenuForChild(this);
4024 }
4025
4026 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004027 * Bring up the context menu for this view, referring to the item under the specified point.
4028 *
4029 * @param x The referenced x coordinate.
4030 * @param y The referenced y coordinate.
4031 * @param metaState The keyboard modifiers that were pressed.
4032 * @return Whether a context menu was displayed.
4033 *
4034 * @hide
4035 */
4036 public boolean showContextMenu(float x, float y, int metaState) {
4037 return showContextMenu();
4038 }
4039
4040 /**
Adam Powell6e346362010-07-23 10:18:23 -07004041 * Start an action mode.
4042 *
4043 * @param callback Callback that will control the lifecycle of the action mode
4044 * @return The new action mode if it is started, null otherwise
4045 *
4046 * @see ActionMode
4047 */
4048 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08004049 ViewParent parent = getParent();
4050 if (parent == null) return null;
4051 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07004052 }
4053
4054 /**
Jean Chalard405bc512012-05-29 19:12:34 +09004055 * Register a callback to be invoked when a hardware key is pressed in this view.
4056 * Key presses in software input methods will generally not trigger the methods of
4057 * this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004058 * @param l the key listener to attach to this view
4059 */
4060 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004061 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004062 }
4063
4064 /**
4065 * Register a callback to be invoked when a touch event is sent to this view.
4066 * @param l the touch listener to attach to this view
4067 */
4068 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004069 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004070 }
4071
4072 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004073 * Register a callback to be invoked when a generic motion event is sent to this view.
4074 * @param l the generic motion listener to attach to this view
4075 */
4076 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004077 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08004078 }
4079
4080 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07004081 * Register a callback to be invoked when a hover event is sent to this view.
4082 * @param l the hover listener to attach to this view
4083 */
4084 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004085 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07004086 }
4087
4088 /**
Joe Malin32736f02011-01-19 16:14:20 -08004089 * Register a drag event listener callback object for this View. The parameter is
4090 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
4091 * View, the system calls the
4092 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
4093 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07004094 */
4095 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004096 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07004097 }
4098
4099 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07004100 * Give this view focus. This will cause
4101 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004102 *
4103 * Note: this does not check whether this {@link View} should get focus, it just
4104 * gives it focus no matter what. It should only be called internally by framework
4105 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
4106 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004107 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
4108 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004109 * focus moved when requestFocus() is called. It may not always
4110 * apply, in which case use the default View.FOCUS_DOWN.
4111 * @param previouslyFocusedRect The rectangle of the view that had focus
4112 * prior in this View's coordinate system.
4113 */
4114 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
4115 if (DBG) {
4116 System.out.println(this + " requestFocus()");
4117 }
4118
4119 if ((mPrivateFlags & FOCUSED) == 0) {
4120 mPrivateFlags |= FOCUSED;
4121
4122 if (mParent != null) {
4123 mParent.requestChildFocus(this, this);
4124 }
4125
4126 onFocusChanged(true, direction, previouslyFocusedRect);
4127 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004128
4129 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4130 notifyAccessibilityStateChanged();
4131 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004132 }
4133 }
4134
4135 /**
4136 * Request that a rectangle of this view be visible on the screen,
4137 * scrolling if necessary just enough.
4138 *
4139 * <p>A View should call this if it maintains some notion of which part
4140 * of its content is interesting. For example, a text editing view
4141 * should call this when its cursor moves.
4142 *
4143 * @param rectangle The rectangle.
4144 * @return Whether any parent scrolled.
4145 */
4146 public boolean requestRectangleOnScreen(Rect rectangle) {
4147 return requestRectangleOnScreen(rectangle, false);
4148 }
4149
4150 /**
4151 * Request that a rectangle of this view be visible on the screen,
4152 * scrolling if necessary just enough.
4153 *
4154 * <p>A View should call this if it maintains some notion of which part
4155 * of its content is interesting. For example, a text editing view
4156 * should call this when its cursor moves.
4157 *
4158 * <p>When <code>immediate</code> is set to true, scrolling will not be
4159 * animated.
4160 *
4161 * @param rectangle The rectangle.
4162 * @param immediate True to forbid animated scrolling, false otherwise
4163 * @return Whether any parent scrolled.
4164 */
4165 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
4166 View child = this;
4167 ViewParent parent = mParent;
4168 boolean scrolled = false;
4169 while (parent != null) {
4170 scrolled |= parent.requestChildRectangleOnScreen(child,
4171 rectangle, immediate);
4172
4173 // offset rect so next call has the rectangle in the
4174 // coordinate system of its direct child.
4175 rectangle.offset(child.getLeft(), child.getTop());
4176 rectangle.offset(-child.getScrollX(), -child.getScrollY());
4177
4178 if (!(parent instanceof View)) {
4179 break;
4180 }
Romain Guy8506ab42009-06-11 17:35:47 -07004181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004182 child = (View) parent;
4183 parent = child.getParent();
4184 }
4185 return scrolled;
4186 }
4187
4188 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004189 * Called when this view wants to give up focus. If focus is cleared
4190 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
4191 * <p>
4192 * <strong>Note:</strong> When a View clears focus the framework is trying
4193 * to give focus to the first focusable View from the top. Hence, if this
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004194 * View is the first from the top that can take focus, then all callbacks
4195 * related to clearing focus will be invoked after wich the framework will
4196 * give focus to this view.
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004197 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004198 */
4199 public void clearFocus() {
4200 if (DBG) {
4201 System.out.println(this + " clearFocus()");
4202 }
4203
4204 if ((mPrivateFlags & FOCUSED) != 0) {
4205 mPrivateFlags &= ~FOCUSED;
4206
4207 if (mParent != null) {
4208 mParent.clearChildFocus(this);
4209 }
4210
4211 onFocusChanged(false, 0, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004213 refreshDrawableState();
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004214
4215 ensureInputFocusOnFirstFocusable();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004216
4217 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4218 notifyAccessibilityStateChanged();
4219 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004220 }
4221 }
4222
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004223 void ensureInputFocusOnFirstFocusable() {
4224 View root = getRootView();
4225 if (root != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004226 root.requestFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004227 }
4228 }
4229
4230 /**
4231 * Called internally by the view system when a new view is getting focus.
4232 * This is what clears the old focus.
4233 */
4234 void unFocus() {
4235 if (DBG) {
4236 System.out.println(this + " unFocus()");
4237 }
4238
4239 if ((mPrivateFlags & FOCUSED) != 0) {
4240 mPrivateFlags &= ~FOCUSED;
4241
4242 onFocusChanged(false, 0, null);
4243 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004244
4245 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4246 notifyAccessibilityStateChanged();
4247 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004248 }
4249 }
4250
4251 /**
4252 * Returns true if this view has focus iteself, or is the ancestor of the
4253 * view that has focus.
4254 *
4255 * @return True if this view has or contains focus, false otherwise.
4256 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004257 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004258 public boolean hasFocus() {
4259 return (mPrivateFlags & FOCUSED) != 0;
4260 }
4261
4262 /**
4263 * Returns true if this view is focusable or if it contains a reachable View
4264 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
4265 * is a View whose parents do not block descendants focus.
4266 *
4267 * Only {@link #VISIBLE} views are considered focusable.
4268 *
4269 * @return True if the view is focusable or if the view contains a focusable
4270 * View, false otherwise.
4271 *
4272 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
4273 */
4274 public boolean hasFocusable() {
4275 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4276 }
4277
4278 /**
4279 * Called by the view system when the focus state of this view changes.
4280 * When the focus change event is caused by directional navigation, direction
4281 * and previouslyFocusedRect provide insight into where the focus is coming from.
4282 * When overriding, be sure to call up through to the super class so that
4283 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004284 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004285 * @param gainFocus True if the View has focus; false otherwise.
4286 * @param direction The direction focus has moved when requestFocus()
4287 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004288 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4289 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4290 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004291 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4292 * system, of the previously focused view. If applicable, this will be
4293 * passed in as finer grained information about where the focus is coming
4294 * from (in addition to direction). Will be <code>null</code> otherwise.
4295 */
4296 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004297 if (gainFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004298 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4299 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004300 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004301 }
4302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004303 InputMethodManager imm = InputMethodManager.peekInstance();
4304 if (!gainFocus) {
4305 if (isPressed()) {
4306 setPressed(false);
4307 }
4308 if (imm != null && mAttachInfo != null
4309 && mAttachInfo.mHasWindowFocus) {
4310 imm.focusOut(this);
4311 }
Romain Guya2431d02009-04-30 16:30:00 -07004312 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004313 } else if (imm != null && mAttachInfo != null
4314 && mAttachInfo.mHasWindowFocus) {
4315 imm.focusIn(this);
4316 }
Romain Guy8506ab42009-06-11 17:35:47 -07004317
Romain Guy0fd89bf2011-01-26 15:41:30 -08004318 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004319 ListenerInfo li = mListenerInfo;
4320 if (li != null && li.mOnFocusChangeListener != null) {
4321 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004322 }
Joe Malin32736f02011-01-19 16:14:20 -08004323
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004324 if (mAttachInfo != null) {
4325 mAttachInfo.mKeyDispatchState.reset(this);
4326 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004327 }
4328
4329 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004330 * Sends an accessibility event of the given type. If accessiiblity is
4331 * not enabled this method has no effect. The default implementation calls
4332 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4333 * to populate information about the event source (this View), then calls
4334 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4335 * populate the text content of the event source including its descendants,
4336 * and last calls
4337 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4338 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004339 * <p>
4340 * If an {@link AccessibilityDelegate} has been specified via calling
4341 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4342 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4343 * responsible for handling this call.
4344 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004345 *
Scott Mainb303d832011-10-12 16:45:18 -07004346 * @param eventType The type of the event to send, as defined by several types from
4347 * {@link android.view.accessibility.AccessibilityEvent}, such as
4348 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4349 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004350 *
4351 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4352 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4353 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004354 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004355 */
4356 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004357 if (mAccessibilityDelegate != null) {
4358 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4359 } else {
4360 sendAccessibilityEventInternal(eventType);
4361 }
4362 }
4363
4364 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004365 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4366 * {@link AccessibilityEvent} to make an announcement which is related to some
4367 * sort of a context change for which none of the events representing UI transitions
4368 * is a good fit. For example, announcing a new page in a book. If accessibility
4369 * is not enabled this method does nothing.
4370 *
4371 * @param text The announcement text.
4372 */
4373 public void announceForAccessibility(CharSequence text) {
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004374 if (AccessibilityManager.getInstance(mContext).isEnabled() && mParent != null) {
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004375 AccessibilityEvent event = AccessibilityEvent.obtain(
4376 AccessibilityEvent.TYPE_ANNOUNCEMENT);
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004377 onInitializeAccessibilityEvent(event);
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004378 event.getText().add(text);
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004379 event.setContentDescription(null);
4380 mParent.requestSendAccessibilityEvent(this, event);
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004381 }
4382 }
4383
4384 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004385 * @see #sendAccessibilityEvent(int)
4386 *
4387 * Note: Called from the default {@link AccessibilityDelegate}.
4388 */
4389 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004390 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4391 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4392 }
4393 }
4394
4395 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004396 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4397 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004398 * perform a check whether accessibility is enabled.
4399 * <p>
4400 * If an {@link AccessibilityDelegate} has been specified via calling
4401 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4402 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4403 * is responsible for handling this call.
4404 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004405 *
4406 * @param event The event to send.
4407 *
4408 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004409 */
4410 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004411 if (mAccessibilityDelegate != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004412 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004413 } else {
4414 sendAccessibilityEventUncheckedInternal(event);
4415 }
4416 }
4417
4418 /**
4419 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4420 *
4421 * Note: Called from the default {@link AccessibilityDelegate}.
4422 */
4423 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004424 if (!isShown()) {
4425 return;
4426 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004427 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004428 // Only a subset of accessibility events populates text content.
4429 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4430 dispatchPopulateAccessibilityEvent(event);
4431 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004432 // In the beginning we called #isShown(), so we know that getParent() is not null.
4433 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004434 }
4435
4436 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004437 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4438 * to its children for adding their text content to the event. Note that the
4439 * event text is populated in a separate dispatch path since we add to the
4440 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004441 * A typical implementation will call
4442 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4443 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4444 * on each child. Override this method if custom population of the event text
4445 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004446 * <p>
4447 * If an {@link AccessibilityDelegate} has been specified via calling
4448 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4449 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4450 * is responsible for handling this call.
4451 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004452 * <p>
4453 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4454 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4455 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004456 *
4457 * @param event The event.
4458 *
4459 * @return True if the event population was completed.
4460 */
4461 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004462 if (mAccessibilityDelegate != null) {
4463 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4464 } else {
4465 return dispatchPopulateAccessibilityEventInternal(event);
4466 }
4467 }
4468
4469 /**
4470 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4471 *
4472 * Note: Called from the default {@link AccessibilityDelegate}.
4473 */
4474 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004475 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004476 return false;
4477 }
4478
4479 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004480 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004481 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004482 * text content. While this method is free to modify event
4483 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004484 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4485 * <p>
4486 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004487 * to the text added by the super implementation:
4488 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004489 * super.onPopulateAccessibilityEvent(event);
4490 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4491 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4492 * mCurrentDate.getTimeInMillis(), flags);
4493 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004494 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004495 * <p>
4496 * If an {@link AccessibilityDelegate} has been specified via calling
4497 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4498 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4499 * is responsible for handling this call.
4500 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004501 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4502 * information to the event, in case the default implementation has basic information to add.
4503 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004504 *
4505 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004506 *
4507 * @see #sendAccessibilityEvent(int)
4508 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004509 */
4510 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004511 if (mAccessibilityDelegate != null) {
4512 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4513 } else {
4514 onPopulateAccessibilityEventInternal(event);
4515 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004516 }
4517
4518 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004519 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4520 *
4521 * Note: Called from the default {@link AccessibilityDelegate}.
4522 */
4523 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4524
4525 }
4526
4527 /**
4528 * Initializes an {@link AccessibilityEvent} with information about
4529 * this View which is the event source. In other words, the source of
4530 * an accessibility event is the view whose state change triggered firing
4531 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004532 * <p>
4533 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004534 * to properties set by the super implementation:
4535 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4536 * super.onInitializeAccessibilityEvent(event);
4537 * event.setPassword(true);
4538 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004539 * <p>
4540 * If an {@link AccessibilityDelegate} has been specified via calling
4541 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4542 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4543 * is responsible for handling this call.
4544 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004545 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4546 * information to the event, in case the default implementation has basic information to add.
4547 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004548 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004549 *
4550 * @see #sendAccessibilityEvent(int)
4551 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4552 */
4553 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004554 if (mAccessibilityDelegate != null) {
4555 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4556 } else {
4557 onInitializeAccessibilityEventInternal(event);
4558 }
4559 }
4560
4561 /**
4562 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4563 *
4564 * Note: Called from the default {@link AccessibilityDelegate}.
4565 */
4566 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004567 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004568 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004569 event.setPackageName(getContext().getPackageName());
4570 event.setEnabled(isEnabled());
4571 event.setContentDescription(mContentDescription);
4572
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004573 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004574 ArrayList<View> focusablesTempList = mAttachInfo.mTempArrayList;
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004575 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4576 FOCUSABLES_ALL);
4577 event.setItemCount(focusablesTempList.size());
4578 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4579 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004580 }
4581 }
4582
4583 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004584 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4585 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4586 * This method is responsible for obtaining an accessibility node info from a
4587 * pool of reusable instances and calling
4588 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4589 * initialize the former.
4590 * <p>
4591 * Note: The client is responsible for recycling the obtained instance by calling
4592 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4593 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004594 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004595 * @return A populated {@link AccessibilityNodeInfo}.
4596 *
4597 * @see AccessibilityNodeInfo
4598 */
4599 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004600 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4601 if (provider != null) {
4602 return provider.createAccessibilityNodeInfo(View.NO_ID);
4603 } else {
4604 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4605 onInitializeAccessibilityNodeInfo(info);
4606 return info;
4607 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004608 }
4609
4610 /**
4611 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4612 * The base implementation sets:
4613 * <ul>
4614 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004615 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4616 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004617 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4618 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4619 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4620 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4621 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4622 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4623 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4624 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4625 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4626 * </ul>
4627 * <p>
4628 * Subclasses should override this method, call the super implementation,
4629 * and set additional attributes.
4630 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004631 * <p>
4632 * If an {@link AccessibilityDelegate} has been specified via calling
4633 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4634 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4635 * is responsible for handling this call.
4636 * </p>
4637 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004638 * @param info The instance to initialize.
4639 */
4640 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004641 if (mAccessibilityDelegate != null) {
4642 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4643 } else {
4644 onInitializeAccessibilityNodeInfoInternal(info);
4645 }
4646 }
4647
4648 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004649 * Gets the location of this view in screen coordintates.
4650 *
4651 * @param outRect The output location
4652 */
4653 private void getBoundsOnScreen(Rect outRect) {
4654 if (mAttachInfo == null) {
4655 return;
4656 }
4657
4658 RectF position = mAttachInfo.mTmpTransformRect;
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004659 position.set(0, 0, mRight - mLeft, mBottom - mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004660
4661 if (!hasIdentityMatrix()) {
4662 getMatrix().mapRect(position);
4663 }
4664
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004665 position.offset(mLeft, mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004666
4667 ViewParent parent = mParent;
4668 while (parent instanceof View) {
4669 View parentView = (View) parent;
4670
4671 position.offset(-parentView.mScrollX, -parentView.mScrollY);
4672
4673 if (!parentView.hasIdentityMatrix()) {
4674 parentView.getMatrix().mapRect(position);
4675 }
4676
4677 position.offset(parentView.mLeft, parentView.mTop);
4678
4679 parent = parentView.mParent;
4680 }
4681
4682 if (parent instanceof ViewRootImpl) {
4683 ViewRootImpl viewRootImpl = (ViewRootImpl) parent;
4684 position.offset(0, -viewRootImpl.mCurScrollY);
4685 }
4686
4687 position.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
4688
4689 outRect.set((int) (position.left + 0.5f), (int) (position.top + 0.5f),
4690 (int) (position.right + 0.5f), (int) (position.bottom + 0.5f));
4691 }
4692
4693 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004694 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4695 *
4696 * Note: Called from the default {@link AccessibilityDelegate}.
4697 */
4698 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004699 Rect bounds = mAttachInfo.mTmpInvalRect;
Svetoslav Ganov983119a2012-07-03 21:04:10 -07004700
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004701 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004702 info.setBoundsInParent(bounds);
4703
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004704 getBoundsOnScreen(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004705 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004706
Svetoslav Ganovc406be92012-05-11 16:12:32 -07004707 ViewParent parent = getParentForAccessibility();
4708 if (parent instanceof View) {
4709 info.setParent((View) parent);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004710 }
4711
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004712 info.setVisibleToUser(isVisibleToUser());
4713
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004714 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004715 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004716 info.setContentDescription(getContentDescription());
4717
4718 info.setEnabled(isEnabled());
4719 info.setClickable(isClickable());
4720 info.setFocusable(isFocusable());
4721 info.setFocused(isFocused());
Svetoslav Ganov42138042012-03-20 11:51:39 -07004722 info.setAccessibilityFocused(isAccessibilityFocused());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004723 info.setSelected(isSelected());
4724 info.setLongClickable(isLongClickable());
4725
4726 // TODO: These make sense only if we are in an AdapterView but all
4727 // views can be selected. Maybe from accessiiblity perspective
4728 // we should report as selectable view in an AdapterView.
4729 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4730 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4731
4732 if (isFocusable()) {
4733 if (isFocused()) {
4734 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4735 } else {
4736 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4737 }
4738 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004739
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07004740 if (!isAccessibilityFocused()) {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07004741 info.addAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07004742 } else {
4743 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
4744 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004745
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07004746 if (isClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004747 info.addAction(AccessibilityNodeInfo.ACTION_CLICK);
4748 }
4749
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07004750 if (isLongClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004751 info.addAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
4752 }
4753
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004754 if (mContentDescription != null && mContentDescription.length() > 0) {
Svetoslav Ganov2b435aa2012-05-04 17:16:37 -07004755 info.addAction(AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY);
4756 info.addAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY);
4757 info.setMovementGranularities(AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004758 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD
4759 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH);
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004760 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004761 }
4762
4763 /**
Svetoslav Ganov86783472012-06-06 21:12:20 -07004764 * Returns the delta between the actual and last reported window left.
4765 *
4766 * @hide
4767 */
4768 public int getActualAndReportedWindowLeftDelta() {
Craig Mautner5702d4d2012-06-30 14:10:16 -07004769// if (mAttachInfo != null) {
4770// return mAttachInfo.mActualWindowLeft - mAttachInfo.mWindowLeft;
4771// }
Svetoslav Ganov86783472012-06-06 21:12:20 -07004772 return 0;
4773 }
4774
4775 /**
4776 * Returns the delta between the actual and last reported window top.
4777 *
4778 * @hide
4779 */
4780 public int getActualAndReportedWindowTopDelta() {
Craig Mautner5702d4d2012-06-30 14:10:16 -07004781// if (mAttachInfo != null) {
4782// return mAttachInfo.mActualWindowTop - mAttachInfo.mWindowTop;
4783// }
Svetoslav Ganov86783472012-06-06 21:12:20 -07004784 return 0;
4785 }
4786
4787 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004788 * Computes whether this view is visible to the user. Such a view is
4789 * attached, visible, all its predecessors are visible, it is not clipped
4790 * entirely by its predecessors, and has an alpha greater than zero.
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004791 *
4792 * @return Whether the view is visible on the screen.
Guang Zhu0d607fb2012-05-11 19:34:56 -07004793 *
4794 * @hide
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004795 */
Guang Zhu0d607fb2012-05-11 19:34:56 -07004796 protected boolean isVisibleToUser() {
4797 return isVisibleToUser(null);
4798 }
4799
4800 /**
4801 * Computes whether the given portion of this view is visible to the user. Such a view is
4802 * attached, visible, all its predecessors are visible, has an alpha greater than zero, and
4803 * the specified portion is not clipped entirely by its predecessors.
4804 *
4805 * @param boundInView the portion of the view to test; coordinates should be relative; may be
4806 * <code>null</code>, and the entire view will be tested in this case.
4807 * When <code>true</code> is returned by the function, the actual visible
4808 * region will be stored in this parameter; that is, if boundInView is fully
4809 * contained within the view, no modification will be made, otherwise regions
4810 * outside of the visible area of the view will be clipped.
4811 *
4812 * @return Whether the specified portion of the view is visible on the screen.
4813 *
4814 * @hide
4815 */
4816 protected boolean isVisibleToUser(Rect boundInView) {
4817 Rect visibleRect = mAttachInfo.mTmpInvalRect;
4818 Point offset = mAttachInfo.mPoint;
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004819 // The first two checks are made also made by isShown() which
4820 // however traverses the tree up to the parent to catch that.
4821 // Therefore, we do some fail fast check to minimize the up
4822 // tree traversal.
Guang Zhu0d607fb2012-05-11 19:34:56 -07004823 boolean isVisible = mAttachInfo != null
4824 && mAttachInfo.mWindowVisibility == View.VISIBLE
4825 && getAlpha() > 0
4826 && isShown()
4827 && getGlobalVisibleRect(visibleRect, offset);
4828 if (isVisible && boundInView != null) {
4829 visibleRect.offset(-offset.x, -offset.y);
4830 isVisible &= boundInView.intersect(visibleRect);
4831 }
4832 return isVisible;
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004833 }
4834
4835 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004836 * Sets a delegate for implementing accessibility support via compositon as
4837 * opposed to inheritance. The delegate's primary use is for implementing
4838 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4839 *
4840 * @param delegate The delegate instance.
4841 *
4842 * @see AccessibilityDelegate
4843 */
4844 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4845 mAccessibilityDelegate = delegate;
4846 }
4847
4848 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004849 * Gets the provider for managing a virtual view hierarchy rooted at this View
4850 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4851 * that explore the window content.
4852 * <p>
4853 * If this method returns an instance, this instance is responsible for managing
4854 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4855 * View including the one representing the View itself. Similarly the returned
4856 * instance is responsible for performing accessibility actions on any virtual
4857 * view or the root view itself.
4858 * </p>
4859 * <p>
4860 * If an {@link AccessibilityDelegate} has been specified via calling
4861 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4862 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4863 * is responsible for handling this call.
4864 * </p>
4865 *
4866 * @return The provider.
4867 *
4868 * @see AccessibilityNodeProvider
4869 */
4870 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4871 if (mAccessibilityDelegate != null) {
4872 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4873 } else {
4874 return null;
4875 }
4876 }
4877
4878 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004879 * Gets the unique identifier of this view on the screen for accessibility purposes.
4880 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4881 *
4882 * @return The view accessibility id.
4883 *
4884 * @hide
4885 */
4886 public int getAccessibilityViewId() {
4887 if (mAccessibilityViewId == NO_ID) {
4888 mAccessibilityViewId = sNextAccessibilityViewId++;
4889 }
4890 return mAccessibilityViewId;
4891 }
4892
4893 /**
4894 * Gets the unique identifier of the window in which this View reseides.
4895 *
4896 * @return The window accessibility id.
4897 *
4898 * @hide
4899 */
4900 public int getAccessibilityWindowId() {
4901 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4902 }
4903
4904 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004905 * Gets the {@link View} description. It briefly describes the view and is
4906 * primarily used for accessibility support. Set this property to enable
4907 * better accessibility support for your application. This is especially
4908 * true for views that do not have textual representation (For example,
4909 * ImageButton).
4910 *
Svetoslav Ganov42138042012-03-20 11:51:39 -07004911 * @return The content description.
svetoslavganov75986cf2009-05-14 22:28:01 -07004912 *
4913 * @attr ref android.R.styleable#View_contentDescription
4914 */
Svetoslav Ganov42138042012-03-20 11:51:39 -07004915 @ViewDebug.ExportedProperty(category = "accessibility")
svetoslavganov75986cf2009-05-14 22:28:01 -07004916 public CharSequence getContentDescription() {
4917 return mContentDescription;
4918 }
4919
4920 /**
4921 * Sets the {@link View} description. It briefly describes the view and is
4922 * primarily used for accessibility support. Set this property to enable
4923 * better accessibility support for your application. This is especially
4924 * true for views that do not have textual representation (For example,
4925 * ImageButton).
4926 *
4927 * @param contentDescription The content description.
4928 *
4929 * @attr ref android.R.styleable#View_contentDescription
4930 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07004931 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07004932 public void setContentDescription(CharSequence contentDescription) {
4933 mContentDescription = contentDescription;
Svetoslav Ganove47957a2012-06-05 14:46:50 -07004934 final boolean nonEmptyDesc = contentDescription != null && contentDescription.length() > 0;
4935 if (nonEmptyDesc && getImportantForAccessibility() == IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
4936 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
4937 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004938 }
4939
4940 /**
Romain Guya2431d02009-04-30 16:30:00 -07004941 * Invoked whenever this view loses focus, either by losing window focus or by losing
4942 * focus within its window. This method can be used to clear any state tied to the
4943 * focus. For instance, if a button is held pressed with the trackball and the window
4944 * loses focus, this method can be used to cancel the press.
4945 *
4946 * Subclasses of View overriding this method should always call super.onFocusLost().
4947 *
4948 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004949 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004950 *
4951 * @hide pending API council approval
4952 */
4953 protected void onFocusLost() {
4954 resetPressedState();
4955 }
4956
4957 private void resetPressedState() {
4958 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4959 return;
4960 }
4961
4962 if (isPressed()) {
4963 setPressed(false);
4964
4965 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004966 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004967 }
4968 }
4969 }
4970
4971 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004972 * Returns true if this view has focus
4973 *
4974 * @return True if this view has focus, false otherwise.
4975 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004976 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004977 public boolean isFocused() {
4978 return (mPrivateFlags & FOCUSED) != 0;
4979 }
4980
4981 /**
4982 * Find the view in the hierarchy rooted at this view that currently has
4983 * focus.
4984 *
4985 * @return The view that currently has focus, or null if no focused view can
4986 * be found.
4987 */
4988 public View findFocus() {
4989 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4990 }
4991
4992 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07004993 * Indicates whether this view is one of the set of scrollable containers in
4994 * its window.
4995 *
4996 * @return whether this view is one of the set of scrollable containers in
4997 * its window
4998 *
4999 * @attr ref android.R.styleable#View_isScrollContainer
5000 */
5001 public boolean isScrollContainer() {
5002 return (mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0;
5003 }
5004
5005 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005006 * Change whether this view is one of the set of scrollable containers in
5007 * its window. This will be used to determine whether the window can
5008 * resize or must pan when a soft input area is open -- scrollable
5009 * containers allow the window to use resize mode since the container
5010 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07005011 *
5012 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005013 */
5014 public void setScrollContainer(boolean isScrollContainer) {
5015 if (isScrollContainer) {
5016 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
5017 mAttachInfo.mScrollContainers.add(this);
5018 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
5019 }
5020 mPrivateFlags |= SCROLL_CONTAINER;
5021 } else {
5022 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
5023 mAttachInfo.mScrollContainers.remove(this);
5024 }
5025 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
5026 }
5027 }
5028
5029 /**
5030 * Returns the quality of the drawing cache.
5031 *
5032 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5033 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5034 *
5035 * @see #setDrawingCacheQuality(int)
5036 * @see #setDrawingCacheEnabled(boolean)
5037 * @see #isDrawingCacheEnabled()
5038 *
5039 * @attr ref android.R.styleable#View_drawingCacheQuality
5040 */
5041 public int getDrawingCacheQuality() {
5042 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
5043 }
5044
5045 /**
5046 * Set the drawing cache quality of this view. This value is used only when the
5047 * drawing cache is enabled
5048 *
5049 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5050 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5051 *
5052 * @see #getDrawingCacheQuality()
5053 * @see #setDrawingCacheEnabled(boolean)
5054 * @see #isDrawingCacheEnabled()
5055 *
5056 * @attr ref android.R.styleable#View_drawingCacheQuality
5057 */
5058 public void setDrawingCacheQuality(int quality) {
5059 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
5060 }
5061
5062 /**
5063 * Returns whether the screen should remain on, corresponding to the current
5064 * value of {@link #KEEP_SCREEN_ON}.
5065 *
5066 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
5067 *
5068 * @see #setKeepScreenOn(boolean)
5069 *
5070 * @attr ref android.R.styleable#View_keepScreenOn
5071 */
5072 public boolean getKeepScreenOn() {
5073 return (mViewFlags & KEEP_SCREEN_ON) != 0;
5074 }
5075
5076 /**
5077 * Controls whether the screen should remain on, modifying the
5078 * value of {@link #KEEP_SCREEN_ON}.
5079 *
5080 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
5081 *
5082 * @see #getKeepScreenOn()
5083 *
5084 * @attr ref android.R.styleable#View_keepScreenOn
5085 */
5086 public void setKeepScreenOn(boolean keepScreenOn) {
5087 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
5088 }
5089
5090 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005091 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5092 * @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 -08005093 *
5094 * @attr ref android.R.styleable#View_nextFocusLeft
5095 */
5096 public int getNextFocusLeftId() {
5097 return mNextFocusLeftId;
5098 }
5099
5100 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005101 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5102 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
5103 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005104 *
5105 * @attr ref android.R.styleable#View_nextFocusLeft
5106 */
5107 public void setNextFocusLeftId(int nextFocusLeftId) {
5108 mNextFocusLeftId = nextFocusLeftId;
5109 }
5110
5111 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005112 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5113 * @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 -08005114 *
5115 * @attr ref android.R.styleable#View_nextFocusRight
5116 */
5117 public int getNextFocusRightId() {
5118 return mNextFocusRightId;
5119 }
5120
5121 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005122 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5123 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
5124 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005125 *
5126 * @attr ref android.R.styleable#View_nextFocusRight
5127 */
5128 public void setNextFocusRightId(int nextFocusRightId) {
5129 mNextFocusRightId = nextFocusRightId;
5130 }
5131
5132 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005133 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5134 * @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 -08005135 *
5136 * @attr ref android.R.styleable#View_nextFocusUp
5137 */
5138 public int getNextFocusUpId() {
5139 return mNextFocusUpId;
5140 }
5141
5142 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005143 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5144 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
5145 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005146 *
5147 * @attr ref android.R.styleable#View_nextFocusUp
5148 */
5149 public void setNextFocusUpId(int nextFocusUpId) {
5150 mNextFocusUpId = nextFocusUpId;
5151 }
5152
5153 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005154 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5155 * @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 -08005156 *
5157 * @attr ref android.R.styleable#View_nextFocusDown
5158 */
5159 public int getNextFocusDownId() {
5160 return mNextFocusDownId;
5161 }
5162
5163 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005164 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5165 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
5166 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005167 *
5168 * @attr ref android.R.styleable#View_nextFocusDown
5169 */
5170 public void setNextFocusDownId(int nextFocusDownId) {
5171 mNextFocusDownId = nextFocusDownId;
5172 }
5173
5174 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005175 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5176 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
5177 *
5178 * @attr ref android.R.styleable#View_nextFocusForward
5179 */
5180 public int getNextFocusForwardId() {
5181 return mNextFocusForwardId;
5182 }
5183
5184 /**
5185 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5186 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
5187 * decide automatically.
5188 *
5189 * @attr ref android.R.styleable#View_nextFocusForward
5190 */
5191 public void setNextFocusForwardId(int nextFocusForwardId) {
5192 mNextFocusForwardId = nextFocusForwardId;
5193 }
5194
5195 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005196 * Returns the visibility of this view and all of its ancestors
5197 *
5198 * @return True if this view and all of its ancestors are {@link #VISIBLE}
5199 */
5200 public boolean isShown() {
5201 View current = this;
5202 //noinspection ConstantConditions
5203 do {
5204 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5205 return false;
5206 }
5207 ViewParent parent = current.mParent;
5208 if (parent == null) {
5209 return false; // We are not attached to the view root
5210 }
5211 if (!(parent instanceof View)) {
5212 return true;
5213 }
5214 current = (View) parent;
5215 } while (current != null);
5216
5217 return false;
5218 }
5219
5220 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005221 * Called by the view hierarchy when the content insets for a window have
5222 * changed, to allow it to adjust its content to fit within those windows.
5223 * The content insets tell you the space that the status bar, input method,
5224 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005225 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005226 * <p>You do not normally need to deal with this function, since the default
5227 * window decoration given to applications takes care of applying it to the
5228 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
5229 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
5230 * and your content can be placed under those system elements. You can then
5231 * use this method within your view hierarchy if you have parts of your UI
5232 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005233 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005234 * <p>The default implementation of this method simply applies the content
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005235 * inset's to the view's padding, consuming that content (modifying the
5236 * insets to be 0), and returning true. This behavior is off by default, but can
5237 * be enabled through {@link #setFitsSystemWindows(boolean)}.
5238 *
5239 * <p>This function's traversal down the hierarchy is depth-first. The same content
5240 * insets object is propagated down the hierarchy, so any changes made to it will
5241 * be seen by all following views (including potentially ones above in
5242 * the hierarchy since this is a depth-first traversal). The first view
5243 * that returns true will abort the entire traversal.
5244 *
5245 * <p>The default implementation works well for a situation where it is
5246 * used with a container that covers the entire window, allowing it to
5247 * apply the appropriate insets to its content on all edges. If you need
5248 * a more complicated layout (such as two different views fitting system
5249 * windows, one on the top of the window, and one on the bottom),
5250 * you can override the method and handle the insets however you would like.
5251 * Note that the insets provided by the framework are always relative to the
5252 * far edges of the window, not accounting for the location of the called view
5253 * within that window. (In fact when this method is called you do not yet know
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005254 * where the layout will place the view, as it is done before layout happens.)
5255 *
5256 * <p>Note: unlike many View methods, there is no dispatch phase to this
5257 * call. If you are overriding it in a ViewGroup and want to allow the
5258 * call to continue to your children, you must be sure to call the super
5259 * implementation.
5260 *
Dianne Hackborncf675782012-05-10 15:07:24 -07005261 * <p>Here is a sample layout that makes use of fitting system windows
5262 * to have controls for a video view placed inside of the window decorations
5263 * that it hides and shows. This can be used with code like the second
5264 * sample (video player) shown in {@link #setSystemUiVisibility(int)}.
5265 *
5266 * {@sample development/samples/ApiDemos/res/layout/video_player.xml complete}
5267 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005268 * @param insets Current content insets of the window. Prior to
5269 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
5270 * the insets or else you and Android will be unhappy.
5271 *
5272 * @return Return true if this view applied the insets and it should not
5273 * continue propagating further down the hierarchy, false otherwise.
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005274 * @see #getFitsSystemWindows()
5275 * @see #setFitsSystemWindows()
5276 * @see #setSystemUiVisibility(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005277 */
5278 protected boolean fitSystemWindows(Rect insets) {
5279 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005280 mUserPaddingStart = -1;
5281 mUserPaddingEnd = -1;
5282 mUserPaddingRelative = false;
5283 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
5284 || mAttachInfo == null
5285 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
5286 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
5287 return true;
5288 } else {
5289 internalSetPadding(0, 0, 0, 0);
5290 return false;
5291 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005292 }
5293 return false;
5294 }
5295
5296 /**
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005297 * Sets whether or not this view should account for system screen decorations
5298 * such as the status bar and inset its content; that is, controlling whether
5299 * the default implementation of {@link #fitSystemWindows(Rect)} will be
5300 * executed. See that method for more details.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005301 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005302 * <p>Note that if you are providing your own implementation of
5303 * {@link #fitSystemWindows(Rect)}, then there is no need to set this
5304 * flag to true -- your implementation will be overriding the default
5305 * implementation that checks this flag.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005306 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005307 * @param fitSystemWindows If true, then the default implementation of
5308 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005309 *
5310 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005311 * @see #getFitsSystemWindows()
5312 * @see #fitSystemWindows(Rect)
5313 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005314 */
5315 public void setFitsSystemWindows(boolean fitSystemWindows) {
5316 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
5317 }
5318
5319 /**
Dianne Hackborncf675782012-05-10 15:07:24 -07005320 * Check for state of {@link #setFitsSystemWindows(boolean). If this method
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005321 * returns true, the default implementation of {@link #fitSystemWindows(Rect)}
5322 * will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005323 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005324 * @return Returns true if the default implementation of
5325 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005326 *
5327 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005328 * @see #setFitsSystemWindows()
5329 * @see #fitSystemWindows(Rect)
5330 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005331 */
Dianne Hackborncf675782012-05-10 15:07:24 -07005332 public boolean getFitsSystemWindows() {
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005333 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
5334 }
5335
Dianne Hackbornb1b55e62012-05-10 16:25:54 -07005336 /** @hide */
5337 public boolean fitsSystemWindows() {
5338 return getFitsSystemWindows();
5339 }
5340
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005341 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005342 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
5343 */
5344 public void requestFitSystemWindows() {
5345 if (mParent != null) {
5346 mParent.requestFitSystemWindows();
5347 }
5348 }
5349
5350 /**
5351 * For use by PhoneWindow to make its own system window fitting optional.
5352 * @hide
5353 */
5354 public void makeOptionalFitsSystemWindows() {
5355 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5356 }
5357
5358 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005359 * Returns the visibility status for this view.
5360 *
5361 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5362 * @attr ref android.R.styleable#View_visibility
5363 */
5364 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005365 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5366 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5367 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005368 })
5369 public int getVisibility() {
5370 return mViewFlags & VISIBILITY_MASK;
5371 }
5372
5373 /**
5374 * Set the enabled state of this view.
5375 *
5376 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5377 * @attr ref android.R.styleable#View_visibility
5378 */
5379 @RemotableViewMethod
5380 public void setVisibility(int visibility) {
5381 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005382 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005383 }
5384
5385 /**
5386 * Returns the enabled status for this view. The interpretation of the
5387 * enabled state varies by subclass.
5388 *
5389 * @return True if this view is enabled, false otherwise.
5390 */
5391 @ViewDebug.ExportedProperty
5392 public boolean isEnabled() {
5393 return (mViewFlags & ENABLED_MASK) == ENABLED;
5394 }
5395
5396 /**
5397 * Set the enabled state of this view. The interpretation of the enabled
5398 * state varies by subclass.
5399 *
5400 * @param enabled True if this view is enabled, false otherwise.
5401 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005402 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005403 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005404 if (enabled == isEnabled()) return;
5405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005406 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5407
5408 /*
5409 * The View most likely has to change its appearance, so refresh
5410 * the drawable state.
5411 */
5412 refreshDrawableState();
5413
5414 // Invalidate too, since the default behavior for views is to be
5415 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005416 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005417 }
5418
5419 /**
5420 * Set whether this view can receive the focus.
5421 *
5422 * Setting this to false will also ensure that this view is not focusable
5423 * in touch mode.
5424 *
5425 * @param focusable If true, this view can receive the focus.
5426 *
5427 * @see #setFocusableInTouchMode(boolean)
5428 * @attr ref android.R.styleable#View_focusable
5429 */
5430 public void setFocusable(boolean focusable) {
5431 if (!focusable) {
5432 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5433 }
5434 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5435 }
5436
5437 /**
5438 * Set whether this view can receive focus while in touch mode.
5439 *
5440 * Setting this to true will also ensure that this view is focusable.
5441 *
5442 * @param focusableInTouchMode If true, this view can receive the focus while
5443 * in touch mode.
5444 *
5445 * @see #setFocusable(boolean)
5446 * @attr ref android.R.styleable#View_focusableInTouchMode
5447 */
5448 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5449 // Focusable in touch mode should always be set before the focusable flag
5450 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5451 // which, in touch mode, will not successfully request focus on this view
5452 // because the focusable in touch mode flag is not set
5453 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5454 if (focusableInTouchMode) {
5455 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5456 }
5457 }
5458
5459 /**
5460 * Set whether this view should have sound effects enabled for events such as
5461 * clicking and touching.
5462 *
5463 * <p>You may wish to disable sound effects for a view if you already play sounds,
5464 * for instance, a dial key that plays dtmf tones.
5465 *
5466 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5467 * @see #isSoundEffectsEnabled()
5468 * @see #playSoundEffect(int)
5469 * @attr ref android.R.styleable#View_soundEffectsEnabled
5470 */
5471 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5472 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5473 }
5474
5475 /**
5476 * @return whether this view should have sound effects enabled for events such as
5477 * clicking and touching.
5478 *
5479 * @see #setSoundEffectsEnabled(boolean)
5480 * @see #playSoundEffect(int)
5481 * @attr ref android.R.styleable#View_soundEffectsEnabled
5482 */
5483 @ViewDebug.ExportedProperty
5484 public boolean isSoundEffectsEnabled() {
5485 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5486 }
5487
5488 /**
5489 * Set whether this view should have haptic feedback for events such as
5490 * long presses.
5491 *
5492 * <p>You may wish to disable haptic feedback if your view already controls
5493 * its own haptic feedback.
5494 *
5495 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5496 * @see #isHapticFeedbackEnabled()
5497 * @see #performHapticFeedback(int)
5498 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5499 */
5500 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5501 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5502 }
5503
5504 /**
5505 * @return whether this view should have haptic feedback enabled for events
5506 * long presses.
5507 *
5508 * @see #setHapticFeedbackEnabled(boolean)
5509 * @see #performHapticFeedback(int)
5510 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5511 */
5512 @ViewDebug.ExportedProperty
5513 public boolean isHapticFeedbackEnabled() {
5514 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5515 }
5516
5517 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005518 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005519 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005520 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5521 * {@link #LAYOUT_DIRECTION_RTL},
5522 * {@link #LAYOUT_DIRECTION_INHERIT} or
5523 * {@link #LAYOUT_DIRECTION_LOCALE}.
5524 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005525 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005526 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005527 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5528 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5529 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5530 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005531 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005532 public int getLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005533 return (mPrivateFlags2 & LAYOUT_DIRECTION_MASK) >> LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005534 }
5535
5536 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005537 * Set the layout direction for this view. This will propagate a reset of layout direction
5538 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005539 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005540 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
5541 * {@link #LAYOUT_DIRECTION_RTL},
5542 * {@link #LAYOUT_DIRECTION_INHERIT} or
5543 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005544 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005545 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005546 */
5547 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005548 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005549 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005550 // Reset the current layout direction and the resolved one
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005551 mPrivateFlags2 &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07005552 resetResolvedLayoutDirection();
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005553 // Set the new layout direction (filtered) and ask for a layout pass
5554 mPrivateFlags2 |=
5555 ((layoutDirection << LAYOUT_DIRECTION_MASK_SHIFT) & LAYOUT_DIRECTION_MASK);
5556 requestLayout();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005557 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005558 }
5559
5560 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005561 * Returns the resolved layout direction for this view.
5562 *
5563 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005564 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005565 */
5566 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005567 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5568 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005569 })
5570 public int getResolvedLayoutDirection() {
Fabrice Di Megliob93911f2012-06-26 19:43:15 -07005571 // The layout direction will be resolved only if needed
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -07005572 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) != LAYOUT_DIRECTION_RESOLVED) {
5573 resolveLayoutDirection();
5574 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07005575 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005576 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
5577 }
5578
5579 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005580 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5581 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005582 *
5583 * @return true if the layout is right-to-left.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005584 */
5585 @ViewDebug.ExportedProperty(category = "layout")
5586 public boolean isLayoutRtl() {
5587 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
5588 }
5589
5590 /**
Adam Powell539ee872012-02-03 19:00:49 -08005591 * Indicates whether the view is currently tracking transient state that the
5592 * app should not need to concern itself with saving and restoring, but that
5593 * the framework should take special note to preserve when possible.
5594 *
Adam Powell785c4472012-05-02 21:25:39 -07005595 * <p>A view with transient state cannot be trivially rebound from an external
5596 * data source, such as an adapter binding item views in a list. This may be
5597 * because the view is performing an animation, tracking user selection
5598 * of content, or similar.</p>
5599 *
Adam Powell539ee872012-02-03 19:00:49 -08005600 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005601 */
5602 @ViewDebug.ExportedProperty(category = "layout")
5603 public boolean hasTransientState() {
5604 return (mPrivateFlags2 & HAS_TRANSIENT_STATE) == HAS_TRANSIENT_STATE;
5605 }
5606
5607 /**
5608 * Set whether this view is currently tracking transient state that the
Chet Haase563d4f22012-04-18 16:20:08 -07005609 * framework should attempt to preserve when possible. This flag is reference counted,
5610 * so every call to setHasTransientState(true) should be paired with a later call
5611 * to setHasTransientState(false).
Adam Powell539ee872012-02-03 19:00:49 -08005612 *
Adam Powell785c4472012-05-02 21:25:39 -07005613 * <p>A view with transient state cannot be trivially rebound from an external
5614 * data source, such as an adapter binding item views in a list. This may be
5615 * because the view is performing an animation, tracking user selection
5616 * of content, or similar.</p>
5617 *
Adam Powell539ee872012-02-03 19:00:49 -08005618 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005619 */
5620 public void setHasTransientState(boolean hasTransientState) {
Chet Haase563d4f22012-04-18 16:20:08 -07005621 mTransientStateCount = hasTransientState ? mTransientStateCount + 1 :
5622 mTransientStateCount - 1;
5623 if (mTransientStateCount < 0) {
5624 mTransientStateCount = 0;
5625 Log.e(VIEW_LOG_TAG, "hasTransientState decremented below 0: " +
5626 "unmatched pair of setHasTransientState calls");
5627 }
5628 if ((hasTransientState && mTransientStateCount == 1) ||
Adam Powell057a5852012-05-11 10:28:38 -07005629 (!hasTransientState && mTransientStateCount == 0)) {
Chet Haase563d4f22012-04-18 16:20:08 -07005630 // update flag if we've just incremented up from 0 or decremented down to 0
5631 mPrivateFlags2 = (mPrivateFlags2 & ~HAS_TRANSIENT_STATE) |
5632 (hasTransientState ? HAS_TRANSIENT_STATE : 0);
5633 if (mParent != null) {
5634 try {
5635 mParent.childHasTransientStateChanged(this, hasTransientState);
5636 } catch (AbstractMethodError e) {
5637 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5638 " does not fully implement ViewParent", e);
5639 }
Adam Powell539ee872012-02-03 19:00:49 -08005640 }
5641 }
5642 }
5643
5644 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005645 * If this view doesn't do any drawing on its own, set this flag to
5646 * allow further optimizations. By default, this flag is not set on
5647 * View, but could be set on some View subclasses such as ViewGroup.
5648 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005649 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5650 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005651 *
5652 * @param willNotDraw whether or not this View draw on its own
5653 */
5654 public void setWillNotDraw(boolean willNotDraw) {
5655 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5656 }
5657
5658 /**
5659 * Returns whether or not this View draws on its own.
5660 *
5661 * @return true if this view has nothing to draw, false otherwise
5662 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005663 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005664 public boolean willNotDraw() {
5665 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
5666 }
5667
5668 /**
5669 * When a View's drawing cache is enabled, drawing is redirected to an
5670 * offscreen bitmap. Some views, like an ImageView, must be able to
5671 * bypass this mechanism if they already draw a single bitmap, to avoid
5672 * unnecessary usage of the memory.
5673 *
5674 * @param willNotCacheDrawing true if this view does not cache its
5675 * drawing, false otherwise
5676 */
5677 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
5678 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
5679 }
5680
5681 /**
5682 * Returns whether or not this View can cache its drawing or not.
5683 *
5684 * @return true if this view does not cache its drawing, false otherwise
5685 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005686 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005687 public boolean willNotCacheDrawing() {
5688 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5689 }
5690
5691 /**
5692 * Indicates whether this view reacts to click events or not.
5693 *
5694 * @return true if the view is clickable, false otherwise
5695 *
5696 * @see #setClickable(boolean)
5697 * @attr ref android.R.styleable#View_clickable
5698 */
5699 @ViewDebug.ExportedProperty
5700 public boolean isClickable() {
5701 return (mViewFlags & CLICKABLE) == CLICKABLE;
5702 }
5703
5704 /**
5705 * Enables or disables click events for this view. When a view
5706 * is clickable it will change its state to "pressed" on every click.
5707 * Subclasses should set the view clickable to visually react to
5708 * user's clicks.
5709 *
5710 * @param clickable true to make the view clickable, false otherwise
5711 *
5712 * @see #isClickable()
5713 * @attr ref android.R.styleable#View_clickable
5714 */
5715 public void setClickable(boolean clickable) {
5716 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5717 }
5718
5719 /**
5720 * Indicates whether this view reacts to long click events or not.
5721 *
5722 * @return true if the view is long clickable, false otherwise
5723 *
5724 * @see #setLongClickable(boolean)
5725 * @attr ref android.R.styleable#View_longClickable
5726 */
5727 public boolean isLongClickable() {
5728 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5729 }
5730
5731 /**
5732 * Enables or disables long click events for this view. When a view is long
5733 * clickable it reacts to the user holding down the button for a longer
5734 * duration than a tap. This event can either launch the listener or a
5735 * context menu.
5736 *
5737 * @param longClickable true to make the view long clickable, false otherwise
5738 * @see #isLongClickable()
5739 * @attr ref android.R.styleable#View_longClickable
5740 */
5741 public void setLongClickable(boolean longClickable) {
5742 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5743 }
5744
5745 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005746 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005747 *
5748 * @see #isClickable()
5749 * @see #setClickable(boolean)
5750 *
5751 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5752 * the View's internal state from a previously set "pressed" state.
5753 */
5754 public void setPressed(boolean pressed) {
Adam Powell035a1fc2012-02-27 15:23:50 -08005755 final boolean needsRefresh = pressed != ((mPrivateFlags & PRESSED) == PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005757 if (pressed) {
5758 mPrivateFlags |= PRESSED;
5759 } else {
5760 mPrivateFlags &= ~PRESSED;
5761 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005762
5763 if (needsRefresh) {
5764 refreshDrawableState();
5765 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005766 dispatchSetPressed(pressed);
5767 }
5768
5769 /**
5770 * Dispatch setPressed to all of this View's children.
5771 *
5772 * @see #setPressed(boolean)
5773 *
5774 * @param pressed The new pressed state
5775 */
5776 protected void dispatchSetPressed(boolean pressed) {
5777 }
5778
5779 /**
5780 * Indicates whether the view is currently in pressed state. Unless
5781 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5782 * the pressed state.
5783 *
Philip Milne6c8ea062012-04-03 17:38:43 -07005784 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005785 * @see #isClickable()
5786 * @see #setClickable(boolean)
5787 *
5788 * @return true if the view is currently pressed, false otherwise
5789 */
5790 public boolean isPressed() {
5791 return (mPrivateFlags & PRESSED) == PRESSED;
5792 }
5793
5794 /**
5795 * Indicates whether this view will save its state (that is,
5796 * whether its {@link #onSaveInstanceState} method will be called).
5797 *
5798 * @return Returns true if the view state saving is enabled, else false.
5799 *
5800 * @see #setSaveEnabled(boolean)
5801 * @attr ref android.R.styleable#View_saveEnabled
5802 */
5803 public boolean isSaveEnabled() {
5804 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5805 }
5806
5807 /**
5808 * Controls whether the saving of this view's state is
5809 * enabled (that is, whether its {@link #onSaveInstanceState} method
5810 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005811 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005812 * for its state to be saved. This flag can only disable the
5813 * saving of this view; any child views may still have their state saved.
5814 *
5815 * @param enabled Set to false to <em>disable</em> state saving, or true
5816 * (the default) to allow it.
5817 *
5818 * @see #isSaveEnabled()
5819 * @see #setId(int)
5820 * @see #onSaveInstanceState()
5821 * @attr ref android.R.styleable#View_saveEnabled
5822 */
5823 public void setSaveEnabled(boolean enabled) {
5824 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5825 }
5826
Jeff Brown85a31762010-09-01 17:01:00 -07005827 /**
5828 * Gets whether the framework should discard touches when the view's
5829 * window is obscured by another visible window.
5830 * Refer to the {@link View} security documentation for more details.
5831 *
5832 * @return True if touch filtering is enabled.
5833 *
5834 * @see #setFilterTouchesWhenObscured(boolean)
5835 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5836 */
5837 @ViewDebug.ExportedProperty
5838 public boolean getFilterTouchesWhenObscured() {
5839 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5840 }
5841
5842 /**
5843 * Sets whether the framework should discard touches when the view's
5844 * window is obscured by another visible window.
5845 * Refer to the {@link View} security documentation for more details.
5846 *
5847 * @param enabled True if touch filtering should be enabled.
5848 *
5849 * @see #getFilterTouchesWhenObscured
5850 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5851 */
5852 public void setFilterTouchesWhenObscured(boolean enabled) {
5853 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5854 FILTER_TOUCHES_WHEN_OBSCURED);
5855 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005856
5857 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005858 * Indicates whether the entire hierarchy under this view will save its
5859 * state when a state saving traversal occurs from its parent. The default
5860 * is true; if false, these views will not be saved unless
5861 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5862 *
5863 * @return Returns true if the view state saving from parent is enabled, else false.
5864 *
5865 * @see #setSaveFromParentEnabled(boolean)
5866 */
5867 public boolean isSaveFromParentEnabled() {
5868 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5869 }
5870
5871 /**
5872 * Controls whether the entire hierarchy under this view will save its
5873 * state when a state saving traversal occurs from its parent. The default
5874 * is true; if false, these views will not be saved unless
5875 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5876 *
5877 * @param enabled Set to false to <em>disable</em> state saving, or true
5878 * (the default) to allow it.
5879 *
5880 * @see #isSaveFromParentEnabled()
5881 * @see #setId(int)
5882 * @see #onSaveInstanceState()
5883 */
5884 public void setSaveFromParentEnabled(boolean enabled) {
5885 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5886 }
5887
5888
5889 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005890 * Returns whether this View is able to take focus.
5891 *
5892 * @return True if this view can take focus, or false otherwise.
5893 * @attr ref android.R.styleable#View_focusable
5894 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005895 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005896 public final boolean isFocusable() {
5897 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
5898 }
5899
5900 /**
5901 * When a view is focusable, it may not want to take focus when in touch mode.
5902 * For example, a button would like focus when the user is navigating via a D-pad
5903 * so that the user can click on it, but once the user starts touching the screen,
5904 * the button shouldn't take focus
5905 * @return Whether the view is focusable in touch mode.
5906 * @attr ref android.R.styleable#View_focusableInTouchMode
5907 */
5908 @ViewDebug.ExportedProperty
5909 public final boolean isFocusableInTouchMode() {
5910 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5911 }
5912
5913 /**
5914 * Find the nearest view in the specified direction that can take focus.
5915 * This does not actually give focus to that view.
5916 *
5917 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5918 *
5919 * @return The nearest focusable in the specified direction, or null if none
5920 * can be found.
5921 */
5922 public View focusSearch(int direction) {
5923 if (mParent != null) {
5924 return mParent.focusSearch(this, direction);
5925 } else {
5926 return null;
5927 }
5928 }
5929
5930 /**
5931 * This method is the last chance for the focused view and its ancestors to
5932 * respond to an arrow key. This is called when the focused view did not
5933 * consume the key internally, nor could the view system find a new view in
5934 * the requested direction to give focus to.
5935 *
5936 * @param focused The currently focused view.
5937 * @param direction The direction focus wants to move. One of FOCUS_UP,
5938 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5939 * @return True if the this view consumed this unhandled move.
5940 */
5941 public boolean dispatchUnhandledMove(View focused, int direction) {
5942 return false;
5943 }
5944
5945 /**
5946 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005947 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005948 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005949 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5950 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005951 * @return The user specified next view, or null if there is none.
5952 */
5953 View findUserSetNextFocus(View root, int direction) {
5954 switch (direction) {
5955 case FOCUS_LEFT:
5956 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005957 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005958 case FOCUS_RIGHT:
5959 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005960 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005961 case FOCUS_UP:
5962 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005963 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005964 case FOCUS_DOWN:
5965 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005966 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005967 case FOCUS_FORWARD:
5968 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005969 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005970 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08005971 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08005972 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005973 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005974 @Override
5975 public boolean apply(View t) {
5976 return t.mNextFocusForwardId == id;
5977 }
5978 });
5979 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005980 }
5981 return null;
5982 }
5983
Jeff Brown4dfbec22011-08-15 14:55:37 -07005984 private View findViewInsideOutShouldExist(View root, final int childViewId) {
5985 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
5986 @Override
5987 public boolean apply(View t) {
5988 return t.mID == childViewId;
5989 }
5990 });
5991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005992 if (result == null) {
5993 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
5994 + "by user for id " + childViewId);
5995 }
5996 return result;
5997 }
5998
5999 /**
6000 * Find and return all focusable views that are descendants of this view,
6001 * possibly including this view if it is focusable itself.
6002 *
6003 * @param direction The direction of the focus
6004 * @return A list of focusable views
6005 */
6006 public ArrayList<View> getFocusables(int direction) {
6007 ArrayList<View> result = new ArrayList<View>(24);
6008 addFocusables(result, direction);
6009 return result;
6010 }
6011
6012 /**
6013 * Add any focusable views that are descendants of this view (possibly
6014 * including this view if it is focusable itself) to views. If we are in touch mode,
6015 * only add views that are also focusable in touch mode.
6016 *
6017 * @param views Focusable views found so far
6018 * @param direction The direction of the focus
6019 */
6020 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006021 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
6022 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006023
svetoslavganov75986cf2009-05-14 22:28:01 -07006024 /**
6025 * Adds any focusable views that are descendants of this view (possibly
6026 * including this view if it is focusable itself) to views. This method
6027 * adds all focusable views regardless if we are in touch mode or
Svetoslav Ganov42138042012-03-20 11:51:39 -07006028 * only views focusable in touch mode if we are in touch mode or
6029 * only views that can take accessibility focus if accessibility is enabeld
6030 * depending on the focusable mode paramater.
svetoslavganov75986cf2009-05-14 22:28:01 -07006031 *
6032 * @param views Focusable views found so far or null if all we are interested is
6033 * the number of focusables.
6034 * @param direction The direction of the focus.
6035 * @param focusableMode The type of focusables to be added.
6036 *
6037 * @see #FOCUSABLES_ALL
6038 * @see #FOCUSABLES_TOUCH_MODE
6039 */
6040 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006041 if (views == null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006042 return;
6043 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006044 if (!isFocusable()) {
6045 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07006046 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006047 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE
6048 && isInTouchMode() && !isFocusableInTouchMode()) {
6049 return;
6050 }
6051 views.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006052 }
6053
6054 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006055 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006056 * The search is performed by either the text that the View renders or the content
6057 * description that describes the view for accessibility purposes and the view does
6058 * not render or both. Clients can specify how the search is to be performed via
6059 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
6060 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006061 *
6062 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006063 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07006064 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006065 * @see #FIND_VIEWS_WITH_TEXT
6066 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
6067 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006068 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006069 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07006070 if (getAccessibilityNodeProvider() != null) {
6071 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
6072 outViews.add(this);
6073 }
6074 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006075 && (searched != null && searched.length() > 0)
6076 && (mContentDescription != null && mContentDescription.length() > 0)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006077 String searchedLowerCase = searched.toString().toLowerCase();
6078 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
6079 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
6080 outViews.add(this);
6081 }
6082 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006083 }
6084
6085 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006086 * Find and return all touchable views that are descendants of this view,
6087 * possibly including this view if it is touchable itself.
6088 *
6089 * @return A list of touchable views
6090 */
6091 public ArrayList<View> getTouchables() {
6092 ArrayList<View> result = new ArrayList<View>();
6093 addTouchables(result);
6094 return result;
6095 }
6096
6097 /**
6098 * Add any touchable views that are descendants of this view (possibly
6099 * including this view if it is touchable itself) to views.
6100 *
6101 * @param views Touchable views found so far
6102 */
6103 public void addTouchables(ArrayList<View> views) {
6104 final int viewFlags = mViewFlags;
6105
6106 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
6107 && (viewFlags & ENABLED_MASK) == ENABLED) {
6108 views.add(this);
6109 }
6110 }
6111
6112 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006113 * Returns whether this View is accessibility focused.
6114 *
6115 * @return True if this View is accessibility focused.
6116 */
6117 boolean isAccessibilityFocused() {
6118 return (mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0;
6119 }
6120
6121 /**
6122 * Call this to try to give accessibility focus to this view.
6123 *
6124 * A view will not actually take focus if {@link AccessibilityManager#isEnabled()}
6125 * returns false or the view is no visible or the view already has accessibility
6126 * focus.
6127 *
6128 * See also {@link #focusSearch(int)}, which is what you call to say that you
6129 * have focus, and you want your parent to look for the next one.
6130 *
6131 * @return Whether this view actually took accessibility focus.
6132 *
6133 * @hide
6134 */
6135 public boolean requestAccessibilityFocus() {
Svetoslav Ganov07b726c2012-04-30 12:24:57 -07006136 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
6137 if (!manager.isEnabled() || !manager.isTouchExplorationEnabled()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006138 return false;
6139 }
6140 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6141 return false;
6142 }
6143 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) == 0) {
6144 mPrivateFlags2 |= ACCESSIBILITY_FOCUSED;
6145 ViewRootImpl viewRootImpl = getViewRootImpl();
6146 if (viewRootImpl != null) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006147 viewRootImpl.setAccessibilityFocus(this, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07006148 }
6149 invalidate();
6150 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
6151 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006152 return true;
6153 }
6154 return false;
6155 }
6156
6157 /**
6158 * Call this to try to clear accessibility focus of this view.
6159 *
6160 * See also {@link #focusSearch(int)}, which is what you call to say that you
6161 * have focus, and you want your parent to look for the next one.
6162 *
6163 * @hide
6164 */
6165 public void clearAccessibilityFocus() {
Svetoslav Ganov791fd312012-05-14 15:12:30 -07006166 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6167 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006168 invalidate();
6169 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
6170 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006171 }
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006172 // Clear the global reference of accessibility focus if this
6173 // view or any of its descendants had accessibility focus.
6174 ViewRootImpl viewRootImpl = getViewRootImpl();
6175 if (viewRootImpl != null) {
6176 View focusHost = viewRootImpl.getAccessibilityFocusedHost();
6177 if (focusHost != null && ViewRootImpl.isViewDescendantOf(focusHost, this)) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006178 viewRootImpl.setAccessibilityFocus(null, null);
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006179 }
6180 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006181 }
6182
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07006183 private void sendAccessibilityHoverEvent(int eventType) {
6184 // Since we are not delivering to a client accessibility events from not
6185 // important views (unless the clinet request that) we need to fire the
6186 // event from the deepest view exposed to the client. As a consequence if
6187 // the user crosses a not exposed view the client will see enter and exit
6188 // of the exposed predecessor followed by and enter and exit of that same
6189 // predecessor when entering and exiting the not exposed descendant. This
6190 // is fine since the client has a clear idea which view is hovered at the
6191 // price of a couple more events being sent. This is a simple and
6192 // working solution.
6193 View source = this;
6194 while (true) {
6195 if (source.includeForAccessibility()) {
6196 source.sendAccessibilityEvent(eventType);
6197 return;
6198 }
6199 ViewParent parent = source.getParent();
6200 if (parent instanceof View) {
6201 source = (View) parent;
6202 } else {
6203 return;
6204 }
6205 }
6206 }
6207
Svetoslav Ganov42138042012-03-20 11:51:39 -07006208 /**
6209 * Clears accessibility focus without calling any callback methods
6210 * normally invoked in {@link #clearAccessibilityFocus()}. This method
6211 * is used for clearing accessibility focus when giving this focus to
6212 * another view.
6213 */
6214 void clearAccessibilityFocusNoCallbacks() {
6215 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6216 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
6217 invalidate();
6218 }
6219 }
6220
6221 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006222 * Call this to try to give focus to a specific view or to one of its
6223 * descendants.
6224 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006225 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6226 * false), or if it is focusable and it is not focusable in touch mode
6227 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006228 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006229 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006230 * have focus, and you want your parent to look for the next one.
6231 *
6232 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
6233 * {@link #FOCUS_DOWN} and <code>null</code>.
6234 *
6235 * @return Whether this view or one of its descendants actually took focus.
6236 */
6237 public final boolean requestFocus() {
6238 return requestFocus(View.FOCUS_DOWN);
6239 }
6240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006241 /**
6242 * Call this to try to give focus to a specific view or to one of its
6243 * descendants and give it a hint about what direction focus is heading.
6244 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006245 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6246 * false), or if it is focusable and it is not focusable in touch mode
6247 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006248 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006249 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006250 * have focus, and you want your parent to look for the next one.
6251 *
6252 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
6253 * <code>null</code> set for the previously focused rectangle.
6254 *
6255 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6256 * @return Whether this view or one of its descendants actually took focus.
6257 */
6258 public final boolean requestFocus(int direction) {
6259 return requestFocus(direction, null);
6260 }
6261
6262 /**
6263 * Call this to try to give focus to a specific view or to one of its descendants
6264 * and give it hints about the direction and a specific rectangle that the focus
6265 * is coming from. The rectangle can help give larger views a finer grained hint
6266 * about where focus is coming from, and therefore, where to show selection, or
6267 * forward focus change internally.
6268 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006269 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6270 * false), or if it is focusable and it is not focusable in touch mode
6271 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006272 *
6273 * A View will not take focus if it is not visible.
6274 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006275 * A View will not take focus if one of its parents has
6276 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
6277 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006278 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006279 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006280 * have focus, and you want your parent to look for the next one.
6281 *
6282 * You may wish to override this method if your custom {@link View} has an internal
6283 * {@link View} that it wishes to forward the request to.
6284 *
6285 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6286 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
6287 * to give a finer grained hint about where focus is coming from. May be null
6288 * if there is no hint.
6289 * @return Whether this view or one of its descendants actually took focus.
6290 */
6291 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006292 return requestFocusNoSearch(direction, previouslyFocusedRect);
6293 }
6294
6295 private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006296 // need to be focusable
6297 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
6298 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6299 return false;
6300 }
6301
6302 // need to be focusable in touch mode if in touch mode
6303 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006304 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
6305 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006306 }
6307
6308 // need to not have any parents blocking us
6309 if (hasAncestorThatBlocksDescendantFocus()) {
6310 return false;
6311 }
6312
6313 handleFocusGainInternal(direction, previouslyFocusedRect);
6314 return true;
6315 }
6316
6317 /**
6318 * Call this to try to give focus to a specific view or to one of its descendants. This is a
6319 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
6320 * touch mode to request focus when they are touched.
6321 *
6322 * @return Whether this view or one of its descendants actually took focus.
6323 *
6324 * @see #isInTouchMode()
6325 *
6326 */
6327 public final boolean requestFocusFromTouch() {
6328 // Leave touch mode if we need to
6329 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006330 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07006331 if (viewRoot != null) {
6332 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006333 }
6334 }
6335 return requestFocus(View.FOCUS_DOWN);
6336 }
6337
6338 /**
6339 * @return Whether any ancestor of this view blocks descendant focus.
6340 */
6341 private boolean hasAncestorThatBlocksDescendantFocus() {
6342 ViewParent ancestor = mParent;
6343 while (ancestor instanceof ViewGroup) {
6344 final ViewGroup vgAncestor = (ViewGroup) ancestor;
6345 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
6346 return true;
6347 } else {
6348 ancestor = vgAncestor.getParent();
6349 }
6350 }
6351 return false;
6352 }
6353
6354 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006355 * Gets the mode for determining whether this View is important for accessibility
6356 * which is if it fires accessibility events and if it is reported to
6357 * accessibility services that query the screen.
6358 *
6359 * @return The mode for determining whether a View is important for accessibility.
6360 *
6361 * @attr ref android.R.styleable#View_importantForAccessibility
6362 *
6363 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6364 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6365 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6366 */
6367 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07006368 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_AUTO, to = "auto"),
6369 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_YES, to = "yes"),
6370 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_NO, to = "no")
Svetoslav Ganov42138042012-03-20 11:51:39 -07006371 })
6372 public int getImportantForAccessibility() {
6373 return (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6374 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6375 }
6376
6377 /**
6378 * Sets how to determine whether this view is important for accessibility
6379 * which is if it fires accessibility events and if it is reported to
6380 * accessibility services that query the screen.
6381 *
6382 * @param mode How to determine whether this view is important for accessibility.
6383 *
6384 * @attr ref android.R.styleable#View_importantForAccessibility
6385 *
6386 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6387 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6388 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6389 */
6390 public void setImportantForAccessibility(int mode) {
6391 if (mode != getImportantForAccessibility()) {
6392 mPrivateFlags2 &= ~IMPORTANT_FOR_ACCESSIBILITY_MASK;
6393 mPrivateFlags2 |= (mode << IMPORTANT_FOR_ACCESSIBILITY_SHIFT)
6394 & IMPORTANT_FOR_ACCESSIBILITY_MASK;
6395 notifyAccessibilityStateChanged();
6396 }
6397 }
6398
6399 /**
6400 * Gets whether this view should be exposed for accessibility.
6401 *
6402 * @return Whether the view is exposed for accessibility.
6403 *
6404 * @hide
6405 */
6406 public boolean isImportantForAccessibility() {
6407 final int mode = (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6408 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6409 switch (mode) {
6410 case IMPORTANT_FOR_ACCESSIBILITY_YES:
6411 return true;
6412 case IMPORTANT_FOR_ACCESSIBILITY_NO:
6413 return false;
6414 case IMPORTANT_FOR_ACCESSIBILITY_AUTO:
6415 return isActionableForAccessibility() || hasListenersForAccessibility();
6416 default:
6417 throw new IllegalArgumentException("Unknow important for accessibility mode: "
6418 + mode);
6419 }
6420 }
6421
6422 /**
6423 * Gets the parent for accessibility purposes. Note that the parent for
6424 * accessibility is not necessary the immediate parent. It is the first
6425 * predecessor that is important for accessibility.
6426 *
6427 * @return The parent for accessibility purposes.
6428 */
6429 public ViewParent getParentForAccessibility() {
6430 if (mParent instanceof View) {
6431 View parentView = (View) mParent;
6432 if (parentView.includeForAccessibility()) {
6433 return mParent;
6434 } else {
6435 return mParent.getParentForAccessibility();
6436 }
6437 }
6438 return null;
6439 }
6440
6441 /**
6442 * Adds the children of a given View for accessibility. Since some Views are
6443 * not important for accessibility the children for accessibility are not
6444 * necessarily direct children of the riew, rather they are the first level of
6445 * descendants important for accessibility.
6446 *
6447 * @param children The list of children for accessibility.
6448 */
6449 public void addChildrenForAccessibility(ArrayList<View> children) {
6450 if (includeForAccessibility()) {
6451 children.add(this);
6452 }
6453 }
6454
6455 /**
6456 * Whether to regard this view for accessibility. A view is regarded for
6457 * accessibility if it is important for accessibility or the querying
6458 * accessibility service has explicitly requested that view not
6459 * important for accessibility are regarded.
6460 *
6461 * @return Whether to regard the view for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006462 *
6463 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006464 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006465 public boolean includeForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006466 if (mAttachInfo != null) {
6467 if (!mAttachInfo.mIncludeNotImportantViews) {
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07006468 return isImportantForAccessibility();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006469 }
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006470 return true;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006471 }
6472 return false;
6473 }
6474
6475 /**
6476 * Returns whether the View is considered actionable from
6477 * accessibility perspective. Such view are important for
6478 * accessiiblity.
6479 *
6480 * @return True if the view is actionable for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006481 *
6482 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006483 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006484 public boolean isActionableForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006485 return (isClickable() || isLongClickable() || isFocusable());
6486 }
6487
6488 /**
6489 * Returns whether the View has registered callbacks wich makes it
6490 * important for accessiiblity.
6491 *
6492 * @return True if the view is actionable for accessibility.
6493 */
6494 private boolean hasListenersForAccessibility() {
6495 ListenerInfo info = getListenerInfo();
6496 return mTouchDelegate != null || info.mOnKeyListener != null
6497 || info.mOnTouchListener != null || info.mOnGenericMotionListener != null
6498 || info.mOnHoverListener != null || info.mOnDragListener != null;
6499 }
6500
6501 /**
6502 * Notifies accessibility services that some view's important for
6503 * accessibility state has changed. Note that such notifications
6504 * are made at most once every
6505 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}
6506 * to avoid unnecessary load to the system. Also once a view has
6507 * made a notifucation this method is a NOP until the notification has
6508 * been sent to clients.
6509 *
6510 * @hide
6511 *
6512 * TODO: Makse sure this method is called for any view state change
6513 * that is interesting for accessilility purposes.
6514 */
6515 public void notifyAccessibilityStateChanged() {
Svetoslav Ganovc406be92012-05-11 16:12:32 -07006516 if (!AccessibilityManager.getInstance(mContext).isEnabled()) {
6517 return;
6518 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006519 if ((mPrivateFlags2 & ACCESSIBILITY_STATE_CHANGED) == 0) {
6520 mPrivateFlags2 |= ACCESSIBILITY_STATE_CHANGED;
6521 if (mParent != null) {
6522 mParent.childAccessibilityStateChanged(this);
6523 }
6524 }
6525 }
6526
6527 /**
6528 * Reset the state indicating the this view has requested clients
6529 * interested in its accessiblity state to be notified.
6530 *
6531 * @hide
6532 */
6533 public void resetAccessibilityStateChanged() {
6534 mPrivateFlags2 &= ~ACCESSIBILITY_STATE_CHANGED;
6535 }
6536
6537 /**
6538 * Performs the specified accessibility action on the view. For
6539 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
alanv8eeefef2012-05-07 16:57:53 -07006540 * <p>
6541 * If an {@link AccessibilityDelegate} has been specified via calling
6542 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
6543 * {@link AccessibilityDelegate#performAccessibilityAction(View, int, Bundle)}
6544 * is responsible for handling this call.
6545 * </p>
Svetoslav Ganov42138042012-03-20 11:51:39 -07006546 *
6547 * @param action The action to perform.
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006548 * @param arguments Optional action arguments.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006549 * @return Whether the action was performed.
6550 */
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006551 public boolean performAccessibilityAction(int action, Bundle arguments) {
alanv8eeefef2012-05-07 16:57:53 -07006552 if (mAccessibilityDelegate != null) {
6553 return mAccessibilityDelegate.performAccessibilityAction(this, action, arguments);
6554 } else {
6555 return performAccessibilityActionInternal(action, arguments);
6556 }
6557 }
6558
6559 /**
6560 * @see #performAccessibilityAction(int, Bundle)
6561 *
6562 * Note: Called from the default {@link AccessibilityDelegate}.
6563 */
6564 boolean performAccessibilityActionInternal(int action, Bundle arguments) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006565 switch (action) {
6566 case AccessibilityNodeInfo.ACTION_CLICK: {
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006567 if (isClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006568 return performClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006569 }
6570 } break;
6571 case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
6572 if (isLongClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006573 return performLongClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006574 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006575 } break;
6576 case AccessibilityNodeInfo.ACTION_FOCUS: {
6577 if (!hasFocus()) {
6578 // Get out of touch mode since accessibility
6579 // wants to move focus around.
6580 getViewRootImpl().ensureTouchMode(false);
6581 return requestFocus();
6582 }
6583 } break;
6584 case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS: {
6585 if (hasFocus()) {
6586 clearFocus();
6587 return !isFocused();
6588 }
6589 } break;
6590 case AccessibilityNodeInfo.ACTION_SELECT: {
6591 if (!isSelected()) {
6592 setSelected(true);
6593 return isSelected();
6594 }
6595 } break;
6596 case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
6597 if (isSelected()) {
6598 setSelected(false);
6599 return !isSelected();
6600 }
6601 } break;
6602 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07006603 if (!isAccessibilityFocused()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006604 return requestAccessibilityFocus();
6605 }
6606 } break;
6607 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
6608 if (isAccessibilityFocused()) {
6609 clearAccessibilityFocus();
6610 return true;
6611 }
6612 } break;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006613 case AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY: {
6614 if (arguments != null) {
6615 final int granularity = arguments.getInt(
6616 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6617 return nextAtGranularity(granularity);
6618 }
6619 } break;
6620 case AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY: {
6621 if (arguments != null) {
6622 final int granularity = arguments.getInt(
6623 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6624 return previousAtGranularity(granularity);
6625 }
6626 } break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006627 }
6628 return false;
6629 }
6630
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006631 private boolean nextAtGranularity(int granularity) {
6632 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006633 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006634 return false;
6635 }
6636 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6637 if (iterator == null) {
6638 return false;
6639 }
6640 final int current = getAccessibilityCursorPosition();
6641 final int[] range = iterator.following(current);
6642 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006643 return false;
6644 }
6645 final int start = range[0];
6646 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006647 setAccessibilityCursorPosition(end);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006648 sendViewTextTraversedAtGranularityEvent(
6649 AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY,
6650 granularity, start, end);
6651 return true;
6652 }
6653
6654 private boolean previousAtGranularity(int granularity) {
6655 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006656 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006657 return false;
6658 }
6659 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6660 if (iterator == null) {
6661 return false;
6662 }
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006663 int current = getAccessibilityCursorPosition();
6664 if (current == ACCESSIBILITY_CURSOR_POSITION_UNDEFINED) {
6665 current = text.length();
6666 } else if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
6667 // When traversing by character we always put the cursor after the character
6668 // to ease edit and have to compensate before asking the for previous segment.
6669 current--;
6670 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006671 final int[] range = iterator.preceding(current);
6672 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006673 return false;
6674 }
6675 final int start = range[0];
6676 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006677 // Always put the cursor after the character to ease edit.
6678 if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
6679 setAccessibilityCursorPosition(end);
6680 } else {
6681 setAccessibilityCursorPosition(start);
6682 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006683 sendViewTextTraversedAtGranularityEvent(
6684 AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
6685 granularity, start, end);
6686 return true;
6687 }
6688
6689 /**
6690 * Gets the text reported for accessibility purposes.
6691 *
6692 * @return The accessibility text.
6693 *
6694 * @hide
6695 */
6696 public CharSequence getIterableTextForAccessibility() {
6697 return mContentDescription;
6698 }
6699
6700 /**
6701 * @hide
6702 */
6703 public int getAccessibilityCursorPosition() {
6704 return mAccessibilityCursorPosition;
6705 }
6706
6707 /**
6708 * @hide
6709 */
6710 public void setAccessibilityCursorPosition(int position) {
6711 mAccessibilityCursorPosition = position;
6712 }
6713
6714 private void sendViewTextTraversedAtGranularityEvent(int action, int granularity,
6715 int fromIndex, int toIndex) {
6716 if (mParent == null) {
6717 return;
6718 }
6719 AccessibilityEvent event = AccessibilityEvent.obtain(
6720 AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY);
6721 onInitializeAccessibilityEvent(event);
6722 onPopulateAccessibilityEvent(event);
6723 event.setFromIndex(fromIndex);
6724 event.setToIndex(toIndex);
6725 event.setAction(action);
6726 event.setMovementGranularity(granularity);
6727 mParent.requestSendAccessibilityEvent(this, event);
6728 }
6729
6730 /**
6731 * @hide
6732 */
6733 public TextSegmentIterator getIteratorForGranularity(int granularity) {
6734 switch (granularity) {
6735 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER: {
6736 CharSequence text = getIterableTextForAccessibility();
6737 if (text != null && text.length() > 0) {
6738 CharacterTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07006739 CharacterTextSegmentIterator.getInstance(
6740 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006741 iterator.initialize(text.toString());
6742 return iterator;
6743 }
6744 } break;
6745 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD: {
6746 CharSequence text = getIterableTextForAccessibility();
6747 if (text != null && text.length() > 0) {
6748 WordTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07006749 WordTextSegmentIterator.getInstance(
6750 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006751 iterator.initialize(text.toString());
6752 return iterator;
6753 }
6754 } break;
6755 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH: {
6756 CharSequence text = getIterableTextForAccessibility();
6757 if (text != null && text.length() > 0) {
6758 ParagraphTextSegmentIterator iterator =
6759 ParagraphTextSegmentIterator.getInstance();
6760 iterator.initialize(text.toString());
6761 return iterator;
6762 }
6763 } break;
6764 }
6765 return null;
6766 }
6767
Svetoslav Ganov42138042012-03-20 11:51:39 -07006768 /**
Romain Guya440b002010-02-24 15:57:54 -08006769 * @hide
6770 */
6771 public void dispatchStartTemporaryDetach() {
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07006772 clearAccessibilityFocus();
Romain Guy38c2ece2012-05-24 14:20:56 -07006773 clearDisplayList();
6774
Romain Guya440b002010-02-24 15:57:54 -08006775 onStartTemporaryDetach();
6776 }
6777
6778 /**
6779 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006780 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
6781 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08006782 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006783 */
6784 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08006785 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08006786 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08006787 }
6788
6789 /**
6790 * @hide
6791 */
6792 public void dispatchFinishTemporaryDetach() {
6793 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006794 }
Romain Guy8506ab42009-06-11 17:35:47 -07006795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006796 /**
6797 * Called after {@link #onStartTemporaryDetach} when the container is done
6798 * changing the view.
6799 */
6800 public void onFinishTemporaryDetach() {
6801 }
Romain Guy8506ab42009-06-11 17:35:47 -07006802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006803 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006804 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
6805 * for this view's window. Returns null if the view is not currently attached
6806 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07006807 * just use the standard high-level event callbacks like
6808 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006809 */
6810 public KeyEvent.DispatcherState getKeyDispatcherState() {
6811 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
6812 }
Joe Malin32736f02011-01-19 16:14:20 -08006813
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006814 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006815 * Dispatch a key event before it is processed by any input method
6816 * associated with the view hierarchy. This can be used to intercept
6817 * key events in special situations before the IME consumes them; a
6818 * typical example would be handling the BACK key to update the application's
6819 * UI instead of allowing the IME to see it and close itself.
6820 *
6821 * @param event The key event to be dispatched.
6822 * @return True if the event was handled, false otherwise.
6823 */
6824 public boolean dispatchKeyEventPreIme(KeyEvent event) {
6825 return onKeyPreIme(event.getKeyCode(), event);
6826 }
6827
6828 /**
6829 * Dispatch a key event to the next view on the focus path. This path runs
6830 * from the top of the view tree down to the currently focused view. If this
6831 * view has focus, it will dispatch to itself. Otherwise it will dispatch
6832 * the next node down the focus path. This method also fires any key
6833 * listeners.
6834 *
6835 * @param event The key event to be dispatched.
6836 * @return True if the event was handled, false otherwise.
6837 */
6838 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006839 if (mInputEventConsistencyVerifier != null) {
6840 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
6841 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006842
Jeff Brown21bc5c92011-02-28 18:27:14 -08006843 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07006844 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006845 ListenerInfo li = mListenerInfo;
6846 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6847 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006848 return true;
6849 }
6850
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006851 if (event.dispatch(this, mAttachInfo != null
6852 ? mAttachInfo.mKeyDispatchState : null, this)) {
6853 return true;
6854 }
6855
6856 if (mInputEventConsistencyVerifier != null) {
6857 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6858 }
6859 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006860 }
6861
6862 /**
6863 * Dispatches a key shortcut event.
6864 *
6865 * @param event The key event to be dispatched.
6866 * @return True if the event was handled by the view, false otherwise.
6867 */
6868 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
6869 return onKeyShortcut(event.getKeyCode(), event);
6870 }
6871
6872 /**
6873 * Pass the touch screen motion event down to the target view, or this
6874 * view if it is the target.
6875 *
6876 * @param event The motion event to be dispatched.
6877 * @return True if the event was handled by the view, false otherwise.
6878 */
6879 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006880 if (mInputEventConsistencyVerifier != null) {
6881 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
6882 }
6883
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006884 if (onFilterTouchEventForSecurity(event)) {
6885 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006886 ListenerInfo li = mListenerInfo;
6887 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6888 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006889 return true;
6890 }
6891
6892 if (onTouchEvent(event)) {
6893 return true;
6894 }
Jeff Brown85a31762010-09-01 17:01:00 -07006895 }
6896
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006897 if (mInputEventConsistencyVerifier != null) {
6898 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006899 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006900 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006901 }
6902
6903 /**
Jeff Brown85a31762010-09-01 17:01:00 -07006904 * Filter the touch event to apply security policies.
6905 *
6906 * @param event The motion event to be filtered.
6907 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08006908 *
Jeff Brown85a31762010-09-01 17:01:00 -07006909 * @see #getFilterTouchesWhenObscured
6910 */
6911 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07006912 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07006913 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
6914 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
6915 // Window is obscured, drop this touch.
6916 return false;
6917 }
6918 return true;
6919 }
6920
6921 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006922 * Pass a trackball motion event down to the focused view.
6923 *
6924 * @param event The motion event to be dispatched.
6925 * @return True if the event was handled by the view, false otherwise.
6926 */
6927 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006928 if (mInputEventConsistencyVerifier != null) {
6929 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
6930 }
6931
Romain Guy02ccac62011-06-24 13:20:23 -07006932 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006933 }
6934
6935 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08006936 * Dispatch a generic motion event.
6937 * <p>
6938 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6939 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08006940 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07006941 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006942 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08006943 *
6944 * @param event The motion event to be dispatched.
6945 * @return True if the event was handled by the view, false otherwise.
6946 */
6947 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006948 if (mInputEventConsistencyVerifier != null) {
6949 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
6950 }
6951
Jeff Browna032cc02011-03-07 16:56:21 -08006952 final int source = event.getSource();
6953 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
6954 final int action = event.getAction();
6955 if (action == MotionEvent.ACTION_HOVER_ENTER
6956 || action == MotionEvent.ACTION_HOVER_MOVE
6957 || action == MotionEvent.ACTION_HOVER_EXIT) {
6958 if (dispatchHoverEvent(event)) {
6959 return true;
6960 }
6961 } else if (dispatchGenericPointerEvent(event)) {
6962 return true;
6963 }
6964 } else if (dispatchGenericFocusedEvent(event)) {
6965 return true;
6966 }
6967
Jeff Brown10b62902011-06-20 16:40:37 -07006968 if (dispatchGenericMotionEventInternal(event)) {
6969 return true;
6970 }
6971
6972 if (mInputEventConsistencyVerifier != null) {
6973 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6974 }
6975 return false;
6976 }
6977
6978 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07006979 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006980 ListenerInfo li = mListenerInfo;
6981 if (li != null && li.mOnGenericMotionListener != null
6982 && (mViewFlags & ENABLED_MASK) == ENABLED
6983 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006984 return true;
6985 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006986
6987 if (onGenericMotionEvent(event)) {
6988 return true;
6989 }
6990
6991 if (mInputEventConsistencyVerifier != null) {
6992 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6993 }
6994 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08006995 }
6996
6997 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006998 * Dispatch a hover event.
6999 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007000 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07007001 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007002 * </p>
7003 *
7004 * @param event The motion event to be dispatched.
7005 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007006 */
7007 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07007008 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007009 ListenerInfo li = mListenerInfo;
7010 if (li != null && li.mOnHoverListener != null
7011 && (mViewFlags & ENABLED_MASK) == ENABLED
7012 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07007013 return true;
7014 }
7015
Jeff Browna032cc02011-03-07 16:56:21 -08007016 return onHoverEvent(event);
7017 }
7018
7019 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007020 * Returns true if the view has a child to which it has recently sent
7021 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
7022 * it does not have a hovered child, then it must be the innermost hovered view.
7023 * @hide
7024 */
7025 protected boolean hasHoveredChild() {
7026 return false;
7027 }
7028
7029 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007030 * Dispatch a generic motion event to the view under the first pointer.
7031 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007032 * Do not call this method directly.
7033 * 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 dispatchGenericPointerEvent(MotionEvent event) {
7040 return false;
7041 }
7042
7043 /**
7044 * Dispatch a generic motion event to the currently focused view.
7045 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007046 * Do not call this method directly.
7047 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007048 * </p>
7049 *
7050 * @param event The motion event to be dispatched.
7051 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007052 */
7053 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
7054 return false;
7055 }
7056
7057 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007058 * Dispatch a pointer event.
7059 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007060 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
7061 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
7062 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08007063 * and should not be expected to handle other pointing device features.
7064 * </p>
7065 *
7066 * @param event The motion event to be dispatched.
7067 * @return True if the event was handled by the view, false otherwise.
7068 * @hide
7069 */
7070 public final boolean dispatchPointerEvent(MotionEvent event) {
7071 if (event.isTouchEvent()) {
7072 return dispatchTouchEvent(event);
7073 } else {
7074 return dispatchGenericMotionEvent(event);
7075 }
7076 }
7077
7078 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007079 * Called when the window containing this view gains or loses window focus.
7080 * ViewGroups should override to route to their children.
7081 *
7082 * @param hasFocus True if the window containing this view now has focus,
7083 * false otherwise.
7084 */
7085 public void dispatchWindowFocusChanged(boolean hasFocus) {
7086 onWindowFocusChanged(hasFocus);
7087 }
7088
7089 /**
7090 * Called when the window containing this view gains or loses focus. Note
7091 * that this is separate from view focus: to receive key events, both
7092 * your view and its window must have focus. If a window is displayed
7093 * on top of yours that takes input focus, then your own window will lose
7094 * focus but the view focus will remain unchanged.
7095 *
7096 * @param hasWindowFocus True if the window containing this view now has
7097 * focus, false otherwise.
7098 */
7099 public void onWindowFocusChanged(boolean hasWindowFocus) {
7100 InputMethodManager imm = InputMethodManager.peekInstance();
7101 if (!hasWindowFocus) {
7102 if (isPressed()) {
7103 setPressed(false);
7104 }
7105 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
7106 imm.focusOut(this);
7107 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007108 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08007109 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07007110 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007111 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
7112 imm.focusIn(this);
7113 }
7114 refreshDrawableState();
7115 }
7116
7117 /**
7118 * Returns true if this view is in a window that currently has window focus.
7119 * Note that this is not the same as the view itself having focus.
7120 *
7121 * @return True if this view is in a window that currently has window focus.
7122 */
7123 public boolean hasWindowFocus() {
7124 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
7125 }
7126
7127 /**
Adam Powell326d8082009-12-09 15:10:07 -08007128 * Dispatch a view visibility change down the view hierarchy.
7129 * ViewGroups should override to route to their children.
7130 * @param changedView The view whose visibility changed. Could be 'this' or
7131 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007132 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7133 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007134 */
7135 protected void dispatchVisibilityChanged(View changedView, int visibility) {
7136 onVisibilityChanged(changedView, visibility);
7137 }
7138
7139 /**
7140 * Called when the visibility of the view or an ancestor of the view is changed.
7141 * @param changedView The view whose visibility changed. Could be 'this' or
7142 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007143 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7144 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007145 */
7146 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007147 if (visibility == VISIBLE) {
7148 if (mAttachInfo != null) {
7149 initialAwakenScrollBars();
7150 } else {
7151 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
7152 }
7153 }
Adam Powell326d8082009-12-09 15:10:07 -08007154 }
7155
7156 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08007157 * Dispatch a hint about whether this view is displayed. For instance, when
7158 * a View moves out of the screen, it might receives a display hint indicating
7159 * the view is not displayed. Applications should not <em>rely</em> on this hint
7160 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007161 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007162 * @param hint A hint about whether or not this view is displayed:
7163 * {@link #VISIBLE} or {@link #INVISIBLE}.
7164 */
7165 public void dispatchDisplayHint(int hint) {
7166 onDisplayHint(hint);
7167 }
7168
7169 /**
7170 * Gives this view a hint about whether is displayed or not. For instance, when
7171 * a View moves out of the screen, it might receives a display hint indicating
7172 * the view is not displayed. Applications should not <em>rely</em> on this hint
7173 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007174 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007175 * @param hint A hint about whether or not this view is displayed:
7176 * {@link #VISIBLE} or {@link #INVISIBLE}.
7177 */
7178 protected void onDisplayHint(int hint) {
7179 }
7180
7181 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007182 * Dispatch a window visibility change down the view hierarchy.
7183 * ViewGroups should override to route to their children.
7184 *
7185 * @param visibility The new visibility of the window.
7186 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007187 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007188 */
7189 public void dispatchWindowVisibilityChanged(int visibility) {
7190 onWindowVisibilityChanged(visibility);
7191 }
7192
7193 /**
7194 * Called when the window containing has change its visibility
7195 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
7196 * that this tells you whether or not your window is being made visible
7197 * to the window manager; this does <em>not</em> tell you whether or not
7198 * your window is obscured by other windows on the screen, even if it
7199 * is itself visible.
7200 *
7201 * @param visibility The new visibility of the window.
7202 */
7203 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007204 if (visibility == VISIBLE) {
7205 initialAwakenScrollBars();
7206 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007207 }
7208
7209 /**
7210 * Returns the current visibility of the window this view is attached to
7211 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
7212 *
7213 * @return Returns the current visibility of the view's window.
7214 */
7215 public int getWindowVisibility() {
7216 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
7217 }
7218
7219 /**
7220 * Retrieve the overall visible display size in which the window this view is
7221 * attached to has been positioned in. This takes into account screen
7222 * decorations above the window, for both cases where the window itself
7223 * is being position inside of them or the window is being placed under
7224 * then and covered insets are used for the window to position its content
7225 * inside. In effect, this tells you the available area where content can
7226 * be placed and remain visible to users.
7227 *
7228 * <p>This function requires an IPC back to the window manager to retrieve
7229 * the requested information, so should not be used in performance critical
7230 * code like drawing.
7231 *
7232 * @param outRect Filled in with the visible display frame. If the view
7233 * is not attached to a window, this is simply the raw display size.
7234 */
7235 public void getWindowVisibleDisplayFrame(Rect outRect) {
7236 if (mAttachInfo != null) {
7237 try {
7238 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
7239 } catch (RemoteException e) {
7240 return;
7241 }
7242 // XXX This is really broken, and probably all needs to be done
7243 // in the window manager, and we need to know more about whether
7244 // we want the area behind or in front of the IME.
7245 final Rect insets = mAttachInfo.mVisibleInsets;
7246 outRect.left += insets.left;
7247 outRect.top += insets.top;
7248 outRect.right -= insets.right;
7249 outRect.bottom -= insets.bottom;
7250 return;
7251 }
7252 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07007253 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007254 }
7255
7256 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007257 * Dispatch a notification about a resource configuration change down
7258 * the view hierarchy.
7259 * ViewGroups should override to route to their children.
7260 *
7261 * @param newConfig The new resource configuration.
7262 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007263 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007264 */
7265 public void dispatchConfigurationChanged(Configuration newConfig) {
7266 onConfigurationChanged(newConfig);
7267 }
7268
7269 /**
7270 * Called when the current configuration of the resources being used
7271 * by the application have changed. You can use this to decide when
7272 * to reload resources that can changed based on orientation and other
7273 * configuration characterstics. You only need to use this if you are
7274 * not relying on the normal {@link android.app.Activity} mechanism of
7275 * recreating the activity instance upon a configuration change.
7276 *
7277 * @param newConfig The new resource configuration.
7278 */
7279 protected void onConfigurationChanged(Configuration newConfig) {
7280 }
7281
7282 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007283 * Private function to aggregate all per-view attributes in to the view
7284 * root.
7285 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007286 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7287 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007288 }
7289
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007290 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7291 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08007292 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007293 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007294 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007295 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007296 ListenerInfo li = mListenerInfo;
7297 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007298 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007299 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007300 }
7301 }
7302
7303 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08007304 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007305 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08007306 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
7307 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007308 ai.mRecomputeGlobalAttributes = true;
7309 }
7310 }
7311 }
7312
7313 /**
7314 * Returns whether the device is currently in touch mode. Touch mode is entered
7315 * once the user begins interacting with the device by touch, and affects various
7316 * things like whether focus is always visible to the user.
7317 *
7318 * @return Whether the device is in touch mode.
7319 */
7320 @ViewDebug.ExportedProperty
7321 public boolean isInTouchMode() {
7322 if (mAttachInfo != null) {
7323 return mAttachInfo.mInTouchMode;
7324 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07007325 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007326 }
7327 }
7328
7329 /**
7330 * Returns the context the view is running in, through which it can
7331 * access the current theme, resources, etc.
7332 *
7333 * @return The view's Context.
7334 */
7335 @ViewDebug.CapturedViewProperty
7336 public final Context getContext() {
7337 return mContext;
7338 }
7339
7340 /**
7341 * Handle a key event before it is processed by any input method
7342 * associated with the view hierarchy. This can be used to intercept
7343 * key events in special situations before the IME consumes them; a
7344 * typical example would be handling the BACK key to update the application's
7345 * UI instead of allowing the IME to see it and close itself.
7346 *
7347 * @param keyCode The value in event.getKeyCode().
7348 * @param event Description of the key event.
7349 * @return If you handled the event, return true. If you want to allow the
7350 * event to be handled by the next receiver, return false.
7351 */
7352 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7353 return false;
7354 }
7355
7356 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007357 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7358 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007359 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7360 * is released, if the view is enabled and clickable.
7361 *
Jean Chalard405bc512012-05-29 19:12:34 +09007362 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7363 * although some may elect to do so in some situations. Do not rely on this to
7364 * catch software key presses.
7365 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007366 * @param keyCode A key code that represents the button pressed, from
7367 * {@link android.view.KeyEvent}.
7368 * @param event The KeyEvent object that defines the button action.
7369 */
7370 public boolean onKeyDown(int keyCode, KeyEvent event) {
7371 boolean result = false;
7372
7373 switch (keyCode) {
7374 case KeyEvent.KEYCODE_DPAD_CENTER:
7375 case KeyEvent.KEYCODE_ENTER: {
7376 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7377 return true;
7378 }
7379 // Long clickable items don't necessarily have to be clickable
7380 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7381 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7382 (event.getRepeatCount() == 0)) {
7383 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007384 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007385 return true;
7386 }
7387 break;
7388 }
7389 }
7390 return result;
7391 }
7392
7393 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007394 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7395 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7396 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007397 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7398 * although some may elect to do so in some situations. Do not rely on this to
7399 * catch software key presses.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007400 */
7401 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7402 return false;
7403 }
7404
7405 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007406 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7407 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007408 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7409 * {@link KeyEvent#KEYCODE_ENTER} is released.
Jean Chalard405bc512012-05-29 19:12:34 +09007410 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7411 * although some may elect to do so in some situations. Do not rely on this to
7412 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007413 *
7414 * @param keyCode A key code that represents the button pressed, from
7415 * {@link android.view.KeyEvent}.
7416 * @param event The KeyEvent object that defines the button action.
7417 */
7418 public boolean onKeyUp(int keyCode, KeyEvent event) {
7419 boolean result = false;
7420
7421 switch (keyCode) {
7422 case KeyEvent.KEYCODE_DPAD_CENTER:
7423 case KeyEvent.KEYCODE_ENTER: {
7424 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7425 return true;
7426 }
7427 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7428 setPressed(false);
7429
7430 if (!mHasPerformedLongPress) {
7431 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007432 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007433
7434 result = performClick();
7435 }
7436 }
7437 break;
7438 }
7439 }
7440 return result;
7441 }
7442
7443 /**
7444 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7445 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7446 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007447 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7448 * although some may elect to do so in some situations. Do not rely on this to
7449 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007450 *
7451 * @param keyCode A key code that represents the button pressed, from
7452 * {@link android.view.KeyEvent}.
7453 * @param repeatCount The number of times the action was made.
7454 * @param event The KeyEvent object that defines the button action.
7455 */
7456 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7457 return false;
7458 }
7459
7460 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007461 * Called on the focused view when a key shortcut event is not handled.
7462 * Override this method to implement local key shortcuts for the View.
7463 * Key shortcuts can also be implemented by setting the
7464 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007465 *
7466 * @param keyCode The value in event.getKeyCode().
7467 * @param event Description of the key event.
7468 * @return If you handled the event, return true. If you want to allow the
7469 * event to be handled by the next receiver, return false.
7470 */
7471 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7472 return false;
7473 }
7474
7475 /**
7476 * Check whether the called view is a text editor, in which case it
7477 * would make sense to automatically display a soft input window for
7478 * it. Subclasses should override this if they implement
7479 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007480 * a call on that method would return a non-null InputConnection, and
7481 * they are really a first-class editor that the user would normally
7482 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007483 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007484 * <p>The default implementation always returns false. This does
7485 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7486 * will not be called or the user can not otherwise perform edits on your
7487 * view; it is just a hint to the system that this is not the primary
7488 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007489 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007490 * @return Returns true if this view is a text editor, else false.
7491 */
7492 public boolean onCheckIsTextEditor() {
7493 return false;
7494 }
Romain Guy8506ab42009-06-11 17:35:47 -07007495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007496 /**
7497 * Create a new InputConnection for an InputMethod to interact
7498 * with the view. The default implementation returns null, since it doesn't
7499 * support input methods. You can override this to implement such support.
7500 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007501 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007502 * <p>When implementing this, you probably also want to implement
7503 * {@link #onCheckIsTextEditor()} to indicate you will return a
7504 * non-null InputConnection.
7505 *
7506 * @param outAttrs Fill in with attribute information about the connection.
7507 */
7508 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7509 return null;
7510 }
7511
7512 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007513 * Called by the {@link android.view.inputmethod.InputMethodManager}
7514 * when a view who is not the current
7515 * input connection target is trying to make a call on the manager. The
7516 * default implementation returns false; you can override this to return
7517 * true for certain views if you are performing InputConnection proxying
7518 * to them.
7519 * @param view The View that is making the InputMethodManager call.
7520 * @return Return true to allow the call, false to reject.
7521 */
7522 public boolean checkInputConnectionProxy(View view) {
7523 return false;
7524 }
Romain Guy8506ab42009-06-11 17:35:47 -07007525
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007526 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007527 * Show the context menu for this view. It is not safe to hold on to the
7528 * menu after returning from this method.
7529 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007530 * You should normally not overload this method. Overload
7531 * {@link #onCreateContextMenu(ContextMenu)} or define an
7532 * {@link OnCreateContextMenuListener} to add items to the context menu.
7533 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007534 * @param menu The context menu to populate
7535 */
7536 public void createContextMenu(ContextMenu menu) {
7537 ContextMenuInfo menuInfo = getContextMenuInfo();
7538
7539 // Sets the current menu info so all items added to menu will have
7540 // my extra info set.
7541 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7542
7543 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007544 ListenerInfo li = mListenerInfo;
7545 if (li != null && li.mOnCreateContextMenuListener != null) {
7546 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007547 }
7548
7549 // Clear the extra information so subsequent items that aren't mine don't
7550 // have my extra info.
7551 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7552
7553 if (mParent != null) {
7554 mParent.createContextMenu(menu);
7555 }
7556 }
7557
7558 /**
7559 * Views should implement this if they have extra information to associate
7560 * with the context menu. The return result is supplied as a parameter to
7561 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7562 * callback.
7563 *
7564 * @return Extra information about the item for which the context menu
7565 * should be shown. This information will vary across different
7566 * subclasses of View.
7567 */
7568 protected ContextMenuInfo getContextMenuInfo() {
7569 return null;
7570 }
7571
7572 /**
7573 * Views should implement this if the view itself is going to add items to
7574 * the context menu.
7575 *
7576 * @param menu the context menu to populate
7577 */
7578 protected void onCreateContextMenu(ContextMenu menu) {
7579 }
7580
7581 /**
7582 * Implement this method to handle trackball motion events. The
7583 * <em>relative</em> movement of the trackball since the last event
7584 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
7585 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
7586 * that a movement of 1 corresponds to the user pressing one DPAD key (so
7587 * they will often be fractional values, representing the more fine-grained
7588 * movement information available from a trackball).
7589 *
7590 * @param event The motion event.
7591 * @return True if the event was handled, false otherwise.
7592 */
7593 public boolean onTrackballEvent(MotionEvent event) {
7594 return false;
7595 }
7596
7597 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08007598 * Implement this method to handle generic motion events.
7599 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08007600 * Generic motion events describe joystick movements, mouse hovers, track pad
7601 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08007602 * {@link MotionEvent#getSource() source} of the motion event specifies
7603 * the class of input that was received. Implementations of this method
7604 * must examine the bits in the source before processing the event.
7605 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007606 * </p><p>
7607 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7608 * are delivered to the view under the pointer. All other generic motion events are
7609 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08007610 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07007611 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08007612 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007613 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
7614 * // process the joystick movement...
7615 * return true;
7616 * }
7617 * }
7618 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
7619 * switch (event.getAction()) {
7620 * case MotionEvent.ACTION_HOVER_MOVE:
7621 * // process the mouse hover movement...
7622 * return true;
7623 * case MotionEvent.ACTION_SCROLL:
7624 * // process the scroll wheel movement...
7625 * return true;
7626 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08007627 * }
7628 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07007629 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08007630 *
7631 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08007632 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08007633 */
7634 public boolean onGenericMotionEvent(MotionEvent event) {
7635 return false;
7636 }
7637
7638 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007639 * Implement this method to handle hover events.
7640 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07007641 * This method is called whenever a pointer is hovering into, over, or out of the
7642 * bounds of a view and the view is not currently being touched.
7643 * Hover events are represented as pointer events with action
7644 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
7645 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
7646 * </p>
7647 * <ul>
7648 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
7649 * when the pointer enters the bounds of the view.</li>
7650 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
7651 * when the pointer has already entered the bounds of the view and has moved.</li>
7652 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
7653 * when the pointer has exited the bounds of the view or when the pointer is
7654 * about to go down due to a button click, tap, or similar user action that
7655 * causes the view to be touched.</li>
7656 * </ul>
7657 * <p>
7658 * The view should implement this method to return true to indicate that it is
7659 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08007660 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07007661 * The default implementation calls {@link #setHovered} to update the hovered state
7662 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07007663 * is enabled and is clickable. The default implementation also sends hover
7664 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08007665 * </p>
7666 *
7667 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07007668 * @return True if the view handled the hover event.
7669 *
7670 * @see #isHovered
7671 * @see #setHovered
7672 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007673 */
7674 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007675 // The root view may receive hover (or touch) events that are outside the bounds of
7676 // the window. This code ensures that we only send accessibility events for
7677 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07007678 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007679 if (!mSendingHoverAccessibilityEvents) {
7680 if ((action == MotionEvent.ACTION_HOVER_ENTER
7681 || action == MotionEvent.ACTION_HOVER_MOVE)
7682 && !hasHoveredChild()
7683 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07007684 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007685 mSendingHoverAccessibilityEvents = true;
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007686 }
7687 } else {
7688 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07007689 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007690 && !pointInView(event.getX(), event.getY()))) {
7691 mSendingHoverAccessibilityEvents = false;
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07007692 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007693 // If the window does not have input focus we take away accessibility
7694 // focus as soon as the user stop hovering over the view.
Jeff Brown59a422e2012-04-19 15:19:19 -07007695 if (mAttachInfo != null && !mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07007696 getViewRootImpl().setAccessibilityFocus(null, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007697 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007698 }
Jeff Browna1b24182011-07-28 13:38:24 -07007699 }
7700
Jeff Brown87b7f802011-06-21 18:35:45 -07007701 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007702 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07007703 case MotionEvent.ACTION_HOVER_ENTER:
7704 setHovered(true);
7705 break;
7706 case MotionEvent.ACTION_HOVER_EXIT:
7707 setHovered(false);
7708 break;
7709 }
Jeff Browna1b24182011-07-28 13:38:24 -07007710
7711 // Dispatch the event to onGenericMotionEvent before returning true.
7712 // This is to provide compatibility with existing applications that
7713 // handled HOVER_MOVE events in onGenericMotionEvent and that would
7714 // break because of the new default handling for hoverable views
7715 // in onHoverEvent.
7716 // Note that onGenericMotionEvent will be called by default when
7717 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
7718 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07007719 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08007720 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007721
Svetoslav Ganov736c2752011-04-22 18:30:36 -07007722 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08007723 }
7724
7725 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007726 * Returns true if the view should handle {@link #onHoverEvent}
7727 * by calling {@link #setHovered} to change its hovered state.
7728 *
7729 * @return True if the view is hoverable.
7730 */
7731 private boolean isHoverable() {
7732 final int viewFlags = mViewFlags;
7733 if ((viewFlags & ENABLED_MASK) == DISABLED) {
7734 return false;
7735 }
7736
7737 return (viewFlags & CLICKABLE) == CLICKABLE
7738 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
7739 }
7740
7741 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007742 * Returns true if the view is currently hovered.
7743 *
7744 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007745 *
7746 * @see #setHovered
7747 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007748 */
Jeff Brown10b62902011-06-20 16:40:37 -07007749 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08007750 public boolean isHovered() {
7751 return (mPrivateFlags & HOVERED) != 0;
7752 }
7753
7754 /**
7755 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007756 * <p>
7757 * Calling this method also changes the drawable state of the view. This
7758 * enables the view to react to hover by using different drawable resources
7759 * to change its appearance.
7760 * </p><p>
7761 * The {@link #onHoverChanged} method is called when the hovered state changes.
7762 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08007763 *
7764 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007765 *
7766 * @see #isHovered
7767 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007768 */
7769 public void setHovered(boolean hovered) {
7770 if (hovered) {
7771 if ((mPrivateFlags & HOVERED) == 0) {
7772 mPrivateFlags |= HOVERED;
7773 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007774 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08007775 }
7776 } else {
7777 if ((mPrivateFlags & HOVERED) != 0) {
7778 mPrivateFlags &= ~HOVERED;
7779 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007780 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08007781 }
7782 }
7783 }
7784
7785 /**
Jeff Brown10b62902011-06-20 16:40:37 -07007786 * Implement this method to handle hover state changes.
7787 * <p>
7788 * This method is called whenever the hover state changes as a result of a
7789 * call to {@link #setHovered}.
7790 * </p>
7791 *
7792 * @param hovered The current hover state, as returned by {@link #isHovered}.
7793 *
7794 * @see #isHovered
7795 * @see #setHovered
7796 */
7797 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07007798 }
7799
7800 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007801 * Implement this method to handle touch screen motion events.
7802 *
7803 * @param event The motion event.
7804 * @return True if the event was handled, false otherwise.
7805 */
7806 public boolean onTouchEvent(MotionEvent event) {
7807 final int viewFlags = mViewFlags;
7808
7809 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007810 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08007811 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007812 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007813 // A disabled view that is clickable still consumes the touch
7814 // events, it just doesn't respond to them.
7815 return (((viewFlags & CLICKABLE) == CLICKABLE ||
7816 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
7817 }
7818
7819 if (mTouchDelegate != null) {
7820 if (mTouchDelegate.onTouchEvent(event)) {
7821 return true;
7822 }
7823 }
7824
7825 if (((viewFlags & CLICKABLE) == CLICKABLE ||
7826 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
7827 switch (event.getAction()) {
7828 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08007829 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
7830 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007831 // take focus if we don't have it already and we should in
7832 // touch mode.
7833 boolean focusTaken = false;
7834 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
7835 focusTaken = requestFocus();
7836 }
7837
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007838 if (prepressed) {
7839 // The button is being released before we actually
7840 // showed it as pressed. Make it show the pressed
7841 // state now (before scheduling the click) to ensure
7842 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08007843 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007844 }
Joe Malin32736f02011-01-19 16:14:20 -08007845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007846 if (!mHasPerformedLongPress) {
7847 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007848 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007849
7850 // Only perform take click actions if we were in the pressed state
7851 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08007852 // Use a Runnable and post this rather than calling
7853 // performClick directly. This lets other visual state
7854 // of the view update before click actions start.
7855 if (mPerformClick == null) {
7856 mPerformClick = new PerformClick();
7857 }
7858 if (!post(mPerformClick)) {
7859 performClick();
7860 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007861 }
7862 }
7863
7864 if (mUnsetPressedState == null) {
7865 mUnsetPressedState = new UnsetPressedState();
7866 }
7867
Adam Powelle14579b2009-12-16 18:39:52 -08007868 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08007869 postDelayed(mUnsetPressedState,
7870 ViewConfiguration.getPressedStateDuration());
7871 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007872 // If the post failed, unpress right now
7873 mUnsetPressedState.run();
7874 }
Adam Powelle14579b2009-12-16 18:39:52 -08007875 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007876 }
7877 break;
7878
7879 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08007880 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007881
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07007882 if (performButtonActionOnTouchDown(event)) {
7883 break;
7884 }
7885
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007886 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07007887 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007888
7889 // For views inside a scrolling container, delay the pressed feedback for
7890 // a short period in case this is a scroll.
7891 if (isInScrollingContainer) {
7892 mPrivateFlags |= PREPRESSED;
7893 if (mPendingCheckForTap == null) {
7894 mPendingCheckForTap = new CheckForTap();
7895 }
7896 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
7897 } else {
7898 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08007899 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007900 checkForLongClick(0);
7901 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007902 break;
7903
7904 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08007905 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08007906 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007907 break;
7908
7909 case MotionEvent.ACTION_MOVE:
7910 final int x = (int) event.getX();
7911 final int y = (int) event.getY();
7912
7913 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07007914 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007915 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08007916 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007917 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08007918 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05007919 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007920
Adam Powell4d6f0662012-02-21 15:11:11 -08007921 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007922 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007923 }
7924 break;
7925 }
7926 return true;
7927 }
7928
7929 return false;
7930 }
7931
7932 /**
Adam Powell10298662011-08-14 18:26:30 -07007933 * @hide
7934 */
7935 public boolean isInScrollingContainer() {
7936 ViewParent p = getParent();
7937 while (p != null && p instanceof ViewGroup) {
7938 if (((ViewGroup) p).shouldDelayChildPressedState()) {
7939 return true;
7940 }
7941 p = p.getParent();
7942 }
7943 return false;
7944 }
7945
7946 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05007947 * Remove the longpress detection timer.
7948 */
7949 private void removeLongPressCallback() {
7950 if (mPendingCheckForLongPress != null) {
7951 removeCallbacks(mPendingCheckForLongPress);
7952 }
7953 }
Adam Powell3cb8b632011-01-21 15:34:14 -08007954
7955 /**
7956 * Remove the pending click action
7957 */
7958 private void removePerformClickCallback() {
7959 if (mPerformClick != null) {
7960 removeCallbacks(mPerformClick);
7961 }
7962 }
7963
Adam Powelle14579b2009-12-16 18:39:52 -08007964 /**
Romain Guya440b002010-02-24 15:57:54 -08007965 * Remove the prepress detection timer.
7966 */
7967 private void removeUnsetPressCallback() {
7968 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
7969 setPressed(false);
7970 removeCallbacks(mUnsetPressedState);
7971 }
7972 }
7973
7974 /**
Adam Powelle14579b2009-12-16 18:39:52 -08007975 * Remove the tap detection timer.
7976 */
7977 private void removeTapCallback() {
7978 if (mPendingCheckForTap != null) {
7979 mPrivateFlags &= ~PREPRESSED;
7980 removeCallbacks(mPendingCheckForTap);
7981 }
7982 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007983
7984 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007985 * Cancels a pending long press. Your subclass can use this if you
7986 * want the context menu to come up if the user presses and holds
7987 * at the same place, but you don't want it to come up if they press
7988 * and then move around enough to cause scrolling.
7989 */
7990 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05007991 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08007992
7993 /*
7994 * The prepressed state handled by the tap callback is a display
7995 * construct, but the tap callback will post a long press callback
7996 * less its own timeout. Remove it here.
7997 */
7998 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007999 }
8000
8001 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008002 * Remove the pending callback for sending a
8003 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
8004 */
8005 private void removeSendViewScrolledAccessibilityEventCallback() {
8006 if (mSendViewScrolledAccessibilityEvent != null) {
8007 removeCallbacks(mSendViewScrolledAccessibilityEvent);
Svetoslav Ganov4a812ae2012-05-29 16:46:10 -07008008 mSendViewScrolledAccessibilityEvent.mIsPending = false;
Svetoslav Ganova0156172011-06-26 17:55:44 -07008009 }
8010 }
8011
8012 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008013 * Sets the TouchDelegate for this View.
8014 */
8015 public void setTouchDelegate(TouchDelegate delegate) {
8016 mTouchDelegate = delegate;
8017 }
8018
8019 /**
8020 * Gets the TouchDelegate for this View.
8021 */
8022 public TouchDelegate getTouchDelegate() {
8023 return mTouchDelegate;
8024 }
8025
8026 /**
8027 * Set flags controlling behavior of this view.
8028 *
8029 * @param flags Constant indicating the value which should be set
8030 * @param mask Constant indicating the bit range that should be changed
8031 */
8032 void setFlags(int flags, int mask) {
8033 int old = mViewFlags;
8034 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
8035
8036 int changed = mViewFlags ^ old;
8037 if (changed == 0) {
8038 return;
8039 }
8040 int privateFlags = mPrivateFlags;
8041
8042 /* Check if the FOCUSABLE bit has changed */
8043 if (((changed & FOCUSABLE_MASK) != 0) &&
8044 ((privateFlags & HAS_BOUNDS) !=0)) {
8045 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
8046 && ((privateFlags & FOCUSED) != 0)) {
8047 /* Give up focus if we are no longer focusable */
8048 clearFocus();
8049 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
8050 && ((privateFlags & FOCUSED) == 0)) {
8051 /*
8052 * Tell the view system that we are now available to take focus
8053 * if no one else already has it.
8054 */
8055 if (mParent != null) mParent.focusableViewAvailable(this);
8056 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008057 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8058 notifyAccessibilityStateChanged();
8059 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008060 }
8061
8062 if ((flags & VISIBILITY_MASK) == VISIBLE) {
8063 if ((changed & VISIBILITY_MASK) != 0) {
8064 /*
Chet Haase4324ead2011-08-24 21:31:03 -07008065 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07008066 * it was not visible. Marking it drawn ensures that the invalidation will
8067 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008068 */
Chet Haaseaceafe62011-08-26 15:44:33 -07008069 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07008070 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008071
8072 needGlobalAttributesUpdate(true);
8073
8074 // a view becoming visible is worth notifying the parent
8075 // about in case nothing has focus. even if this specific view
8076 // isn't focusable, it may contain something that is, so let
8077 // the root view try to give this focus if nothing else does.
8078 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
8079 mParent.focusableViewAvailable(this);
8080 }
8081 }
8082 }
8083
8084 /* Check if the GONE bit has changed */
8085 if ((changed & GONE) != 0) {
8086 needGlobalAttributesUpdate(false);
8087 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008088
Romain Guyecd80ee2009-12-03 17:13:02 -08008089 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
8090 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008091 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08008092 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07008093 if (mParent instanceof View) {
8094 // GONE views noop invalidation, so invalidate the parent
8095 ((View) mParent).invalidate(true);
8096 }
8097 // Mark the view drawn to ensure that it gets invalidated properly the next
8098 // time it is visible and gets invalidated
8099 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008100 }
8101 if (mAttachInfo != null) {
8102 mAttachInfo.mViewVisibilityChanged = true;
8103 }
8104 }
8105
8106 /* Check if the VISIBLE bit has changed */
8107 if ((changed & INVISIBLE) != 0) {
8108 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07008109 /*
8110 * If this view is becoming invisible, set the DRAWN flag so that
8111 * the next invalidate() will not be skipped.
8112 */
8113 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008114
8115 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008116 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008117 if (getRootView() != this) {
8118 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008119 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008120 }
8121 }
8122 if (mAttachInfo != null) {
8123 mAttachInfo.mViewVisibilityChanged = true;
8124 }
8125 }
8126
Adam Powell326d8082009-12-09 15:10:07 -08008127 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07008128 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08008129 ((ViewGroup) mParent).onChildVisibilityChanged(this,
8130 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08008131 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07008132 } else if (mParent != null) {
8133 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07008134 }
Adam Powell326d8082009-12-09 15:10:07 -08008135 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
8136 }
8137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008138 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
8139 destroyDrawingCache();
8140 }
8141
8142 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
8143 destroyDrawingCache();
8144 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008145 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008146 }
8147
8148 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
8149 destroyDrawingCache();
8150 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8151 }
8152
8153 if ((changed & DRAW_MASK) != 0) {
8154 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07008155 if (mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008156 mPrivateFlags &= ~SKIP_DRAW;
8157 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
8158 } else {
8159 mPrivateFlags |= SKIP_DRAW;
8160 }
8161 } else {
8162 mPrivateFlags &= ~SKIP_DRAW;
8163 }
8164 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08008165 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008166 }
8167
8168 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08008169 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008170 mParent.recomputeViewAttributes(this);
8171 }
8172 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008173
8174 if (AccessibilityManager.getInstance(mContext).isEnabled()
8175 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
8176 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
8177 notifyAccessibilityStateChanged();
8178 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008179 }
8180
8181 /**
8182 * Change the view's z order in the tree, so it's on top of other sibling
8183 * views
8184 */
8185 public void bringToFront() {
8186 if (mParent != null) {
8187 mParent.bringChildToFront(this);
8188 }
8189 }
8190
8191 /**
8192 * This is called in response to an internal scroll in this view (i.e., the
8193 * view scrolled its own contents). This is typically as a result of
8194 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
8195 * called.
8196 *
8197 * @param l Current horizontal scroll origin.
8198 * @param t Current vertical scroll origin.
8199 * @param oldl Previous horizontal scroll origin.
8200 * @param oldt Previous vertical scroll origin.
8201 */
8202 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07008203 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8204 postSendViewScrolledAccessibilityEventCallback();
8205 }
8206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008207 mBackgroundSizeChanged = true;
8208
8209 final AttachInfo ai = mAttachInfo;
8210 if (ai != null) {
8211 ai.mViewScrollChanged = true;
8212 }
8213 }
8214
8215 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008216 * Interface definition for a callback to be invoked when the layout bounds of a view
8217 * changes due to layout processing.
8218 */
8219 public interface OnLayoutChangeListener {
8220 /**
8221 * Called when the focus state of a view has changed.
8222 *
8223 * @param v The view whose state has changed.
8224 * @param left The new value of the view's left property.
8225 * @param top The new value of the view's top property.
8226 * @param right The new value of the view's right property.
8227 * @param bottom The new value of the view's bottom property.
8228 * @param oldLeft The previous value of the view's left property.
8229 * @param oldTop The previous value of the view's top property.
8230 * @param oldRight The previous value of the view's right property.
8231 * @param oldBottom The previous value of the view's bottom property.
8232 */
8233 void onLayoutChange(View v, int left, int top, int right, int bottom,
8234 int oldLeft, int oldTop, int oldRight, int oldBottom);
8235 }
8236
8237 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008238 * This is called during layout when the size of this view has changed. If
8239 * you were just added to the view hierarchy, you're called with the old
8240 * values of 0.
8241 *
8242 * @param w Current width of this view.
8243 * @param h Current height of this view.
8244 * @param oldw Old width of this view.
8245 * @param oldh Old height of this view.
8246 */
8247 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
8248 }
8249
8250 /**
8251 * Called by draw to draw the child views. This may be overridden
8252 * by derived classes to gain control just before its children are drawn
8253 * (but after its own view has been drawn).
8254 * @param canvas the canvas on which to draw the view
8255 */
8256 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008258 }
8259
8260 /**
8261 * Gets the parent of this view. Note that the parent is a
8262 * ViewParent and not necessarily a View.
8263 *
8264 * @return Parent of this view.
8265 */
8266 public final ViewParent getParent() {
8267 return mParent;
8268 }
8269
8270 /**
Chet Haasecca2c982011-05-20 14:34:18 -07008271 * Set the horizontal scrolled position of your view. This will cause a call to
8272 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8273 * invalidated.
8274 * @param value the x position to scroll to
8275 */
8276 public void setScrollX(int value) {
8277 scrollTo(value, mScrollY);
8278 }
8279
8280 /**
8281 * Set the vertical scrolled position of your view. This will cause a call to
8282 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8283 * invalidated.
8284 * @param value the y position to scroll to
8285 */
8286 public void setScrollY(int value) {
8287 scrollTo(mScrollX, value);
8288 }
8289
8290 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008291 * Return the scrolled left position of this view. This is the left edge of
8292 * the displayed part of your view. You do not need to draw any pixels
8293 * farther left, since those are outside of the frame of your view on
8294 * screen.
8295 *
8296 * @return The left edge of the displayed part of your view, in pixels.
8297 */
8298 public final int getScrollX() {
8299 return mScrollX;
8300 }
8301
8302 /**
8303 * Return the scrolled top position of this view. This is the top edge of
8304 * the displayed part of your view. You do not need to draw any pixels above
8305 * it, since those are outside of the frame of your view on screen.
8306 *
8307 * @return The top edge of the displayed part of your view, in pixels.
8308 */
8309 public final int getScrollY() {
8310 return mScrollY;
8311 }
8312
8313 /**
8314 * Return the width of the your view.
8315 *
8316 * @return The width of your view, in pixels.
8317 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008318 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008319 public final int getWidth() {
8320 return mRight - mLeft;
8321 }
8322
8323 /**
8324 * Return the height of your view.
8325 *
8326 * @return The height of your view, in pixels.
8327 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008328 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008329 public final int getHeight() {
8330 return mBottom - mTop;
8331 }
8332
8333 /**
8334 * Return the visible drawing bounds of your view. Fills in the output
8335 * rectangle with the values from getScrollX(), getScrollY(),
8336 * getWidth(), and getHeight().
8337 *
8338 * @param outRect The (scrolled) drawing bounds of the view.
8339 */
8340 public void getDrawingRect(Rect outRect) {
8341 outRect.left = mScrollX;
8342 outRect.top = mScrollY;
8343 outRect.right = mScrollX + (mRight - mLeft);
8344 outRect.bottom = mScrollY + (mBottom - mTop);
8345 }
8346
8347 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008348 * Like {@link #getMeasuredWidthAndState()}, but only returns the
8349 * raw width component (that is the result is masked by
8350 * {@link #MEASURED_SIZE_MASK}).
8351 *
8352 * @return The raw measured width of this view.
8353 */
8354 public final int getMeasuredWidth() {
8355 return mMeasuredWidth & MEASURED_SIZE_MASK;
8356 }
8357
8358 /**
8359 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008360 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008361 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008362 * This should be used during measurement and layout calculations only. Use
8363 * {@link #getWidth()} to see how wide a view is after layout.
8364 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008365 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008366 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008367 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008368 return mMeasuredWidth;
8369 }
8370
8371 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008372 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8373 * raw width component (that is the result is masked by
8374 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008375 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008376 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008377 */
8378 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008379 return mMeasuredHeight & MEASURED_SIZE_MASK;
8380 }
8381
8382 /**
8383 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008384 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008385 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8386 * This should be used during measurement and layout calculations only. Use
8387 * {@link #getHeight()} to see how wide a view is after layout.
8388 *
8389 * @return The measured width of this view as a bit mask.
8390 */
8391 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008392 return mMeasuredHeight;
8393 }
8394
8395 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008396 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8397 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8398 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8399 * and the height component is at the shifted bits
8400 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8401 */
8402 public final int getMeasuredState() {
8403 return (mMeasuredWidth&MEASURED_STATE_MASK)
8404 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8405 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8406 }
8407
8408 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008409 * The transform matrix of this view, which is calculated based on the current
8410 * roation, scale, and pivot properties.
8411 *
8412 * @see #getRotation()
8413 * @see #getScaleX()
8414 * @see #getScaleY()
8415 * @see #getPivotX()
8416 * @see #getPivotY()
8417 * @return The current transform matrix for the view
8418 */
8419 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008420 if (mTransformationInfo != null) {
8421 updateMatrix();
8422 return mTransformationInfo.mMatrix;
8423 }
8424 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008425 }
8426
8427 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008428 * Utility function to determine if the value is far enough away from zero to be
8429 * considered non-zero.
8430 * @param value A floating point value to check for zero-ness
8431 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8432 */
8433 private static boolean nonzero(float value) {
8434 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8435 }
8436
8437 /**
Jeff Brown86671742010-09-30 20:00:15 -07008438 * Returns true if the transform matrix is the identity matrix.
8439 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008440 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008441 * @return True if the transform matrix is the identity matrix, false otherwise.
8442 */
Jeff Brown86671742010-09-30 20:00:15 -07008443 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008444 if (mTransformationInfo != null) {
8445 updateMatrix();
8446 return mTransformationInfo.mMatrixIsIdentity;
8447 }
8448 return true;
8449 }
8450
8451 void ensureTransformationInfo() {
8452 if (mTransformationInfo == null) {
8453 mTransformationInfo = new TransformationInfo();
8454 }
Jeff Brown86671742010-09-30 20:00:15 -07008455 }
8456
8457 /**
8458 * Recomputes the transform matrix if necessary.
8459 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008460 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008461 final TransformationInfo info = mTransformationInfo;
8462 if (info == null) {
8463 return;
8464 }
8465 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008466 // transform-related properties have changed since the last time someone
8467 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008468
8469 // Figure out if we need to update the pivot point
8470 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008471 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8472 info.mPrevWidth = mRight - mLeft;
8473 info.mPrevHeight = mBottom - mTop;
8474 info.mPivotX = info.mPrevWidth / 2f;
8475 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008476 }
8477 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008478 info.mMatrix.reset();
8479 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8480 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8481 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8482 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008483 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008484 if (info.mCamera == null) {
8485 info.mCamera = new Camera();
8486 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008487 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008488 info.mCamera.save();
8489 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8490 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8491 info.mCamera.getMatrix(info.matrix3D);
8492 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8493 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8494 info.mPivotY + info.mTranslationY);
8495 info.mMatrix.postConcat(info.matrix3D);
8496 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008497 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008498 info.mMatrixDirty = false;
8499 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8500 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008501 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008502 }
8503
8504 /**
Tobias Duboisdefdb1e2010-12-15 11:35:30 +01008505 * When searching for a view to focus this rectangle is used when considering if this view is
8506 * a good candidate for receiving focus.
8507 *
8508 * By default, the rectangle is the {@link #getDrawingRect}) of the view.
8509 *
8510 * @param r The rectangle to fill in, in this view's coordinates.
8511 */
8512 public void getFocusRect(Rect r) {
8513 getDrawingRect(r);
8514 }
8515
8516 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008517 * Utility method to retrieve the inverse of the current mMatrix property.
8518 * We cache the matrix to avoid recalculating it when transform properties
8519 * have not changed.
8520 *
8521 * @return The inverse of the current matrix of this view.
8522 */
Jeff Brown86671742010-09-30 20:00:15 -07008523 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008524 final TransformationInfo info = mTransformationInfo;
8525 if (info != null) {
8526 updateMatrix();
8527 if (info.mInverseMatrixDirty) {
8528 if (info.mInverseMatrix == null) {
8529 info.mInverseMatrix = new Matrix();
8530 }
8531 info.mMatrix.invert(info.mInverseMatrix);
8532 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008533 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008534 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008535 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008536 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008537 }
8538
8539 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008540 * Gets the distance along the Z axis from the camera to this view.
8541 *
8542 * @see #setCameraDistance(float)
8543 *
8544 * @return The distance along the Z axis.
8545 */
8546 public float getCameraDistance() {
8547 ensureTransformationInfo();
8548 final float dpi = mResources.getDisplayMetrics().densityDpi;
8549 final TransformationInfo info = mTransformationInfo;
8550 if (info.mCamera == null) {
8551 info.mCamera = new Camera();
8552 info.matrix3D = new Matrix();
8553 }
8554 return -(info.mCamera.getLocationZ() * dpi);
8555 }
8556
8557 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008558 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8559 * views are drawn) from the camera to this view. The camera's distance
8560 * affects 3D transformations, for instance rotations around the X and Y
8561 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008562 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008563 * use a camera distance that's greater than the height (X axis rotation) or
8564 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008565 *
Romain Guya5364ee2011-02-24 14:46:04 -08008566 * <p>The distance of the camera from the view plane can have an affect on the
8567 * perspective distortion of the view when it is rotated around the x or y axis.
8568 * For example, a large distance will result in a large viewing angle, and there
8569 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008570 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008571 * also result in some drawing artifacts if the rotated view ends up partially
8572 * behind the camera (which is why the recommendation is to use a distance at
8573 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008574 *
Romain Guya5364ee2011-02-24 14:46:04 -08008575 * <p>The distance is expressed in "depth pixels." The default distance depends
8576 * on the screen density. For instance, on a medium density display, the
8577 * default distance is 1280. On a high density display, the default distance
8578 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008579 *
Romain Guya5364ee2011-02-24 14:46:04 -08008580 * <p>If you want to specify a distance that leads to visually consistent
8581 * results across various densities, use the following formula:</p>
8582 * <pre>
8583 * float scale = context.getResources().getDisplayMetrics().density;
8584 * view.setCameraDistance(distance * scale);
8585 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07008586 *
Romain Guya5364ee2011-02-24 14:46:04 -08008587 * <p>The density scale factor of a high density display is 1.5,
8588 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008589 *
Romain Guya5364ee2011-02-24 14:46:04 -08008590 * @param distance The distance in "depth pixels", if negative the opposite
8591 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07008592 *
8593 * @see #setRotationX(float)
8594 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08008595 */
8596 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008597 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08008598
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008599 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08008600 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008601 final TransformationInfo info = mTransformationInfo;
8602 if (info.mCamera == null) {
8603 info.mCamera = new Camera();
8604 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08008605 }
8606
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008607 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
8608 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08008609
Chet Haase9d1992d2012-03-13 11:03:25 -07008610 invalidateViewProperty(false, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008611 if (mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07008612 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08008613 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008614 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8615 // View was rejected last time it was drawn by its parent; this may have changed
8616 invalidateParentIfNeeded();
8617 }
Romain Guya5364ee2011-02-24 14:46:04 -08008618 }
8619
8620 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008621 * The degrees that the view is rotated around the pivot point.
8622 *
Philip Milne6c8ea062012-04-03 17:38:43 -07008623 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07008624 * @see #getPivotX()
8625 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008626 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008627 * @return The degrees of rotation.
8628 */
Chet Haasea5531132012-02-02 13:41:44 -08008629 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008630 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008631 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008632 }
8633
8634 /**
Chet Haase897247b2010-09-09 14:54:47 -07008635 * Sets the degrees that the view is rotated around the pivot point. Increasing values
8636 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07008637 *
8638 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008639 *
8640 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07008641 * @see #getPivotX()
8642 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008643 * @see #setRotationX(float)
8644 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08008645 *
8646 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07008647 */
8648 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008649 ensureTransformationInfo();
8650 final TransformationInfo info = mTransformationInfo;
8651 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008652 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008653 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008654 info.mRotation = rotation;
8655 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008656 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008657 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008658 mDisplayList.setRotation(rotation);
8659 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008660 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8661 // View was rejected last time it was drawn by its parent; this may have changed
8662 invalidateParentIfNeeded();
8663 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008664 }
8665 }
8666
8667 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008668 * The degrees that the view is rotated around the vertical axis through the pivot point.
8669 *
8670 * @see #getPivotX()
8671 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008672 * @see #setRotationY(float)
8673 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008674 * @return The degrees of Y rotation.
8675 */
Chet Haasea5531132012-02-02 13:41:44 -08008676 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008677 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008678 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008679 }
8680
8681 /**
Chet Haase897247b2010-09-09 14:54:47 -07008682 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
8683 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
8684 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008685 *
Romain Guya5364ee2011-02-24 14:46:04 -08008686 * When rotating large views, it is recommended to adjust the camera distance
8687 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008688 *
8689 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008690 *
8691 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07008692 * @see #getPivotX()
8693 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008694 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008695 * @see #setRotationX(float)
8696 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008697 *
8698 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07008699 */
8700 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008701 ensureTransformationInfo();
8702 final TransformationInfo info = mTransformationInfo;
8703 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008704 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008705 info.mRotationY = rotationY;
8706 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008707 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008708 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008709 mDisplayList.setRotationY(rotationY);
8710 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008711 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8712 // View was rejected last time it was drawn by its parent; this may have changed
8713 invalidateParentIfNeeded();
8714 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008715 }
8716 }
8717
8718 /**
8719 * The degrees that the view is rotated around the horizontal axis through the pivot point.
8720 *
8721 * @see #getPivotX()
8722 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008723 * @see #setRotationX(float)
8724 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008725 * @return The degrees of X rotation.
8726 */
Chet Haasea5531132012-02-02 13:41:44 -08008727 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008728 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008729 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008730 }
8731
8732 /**
Chet Haase897247b2010-09-09 14:54:47 -07008733 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
8734 * Increasing values result in clockwise rotation from the viewpoint of looking down the
8735 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008736 *
Romain Guya5364ee2011-02-24 14:46:04 -08008737 * When rotating large views, it is recommended to adjust the camera distance
8738 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008739 *
8740 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008741 *
8742 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07008743 * @see #getPivotX()
8744 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008745 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008746 * @see #setRotationY(float)
8747 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008748 *
8749 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07008750 */
8751 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008752 ensureTransformationInfo();
8753 final TransformationInfo info = mTransformationInfo;
8754 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008755 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008756 info.mRotationX = rotationX;
8757 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008758 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008759 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008760 mDisplayList.setRotationX(rotationX);
8761 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008762 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8763 // View was rejected last time it was drawn by its parent; this may have changed
8764 invalidateParentIfNeeded();
8765 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008766 }
8767 }
8768
8769 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008770 * The amount that the view is scaled in x around the pivot point, as a proportion of
8771 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
8772 *
Joe Onorato93162322010-09-16 15:42:01 -04008773 * <p>By default, this is 1.0f.
8774 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008775 * @see #getPivotX()
8776 * @see #getPivotY()
8777 * @return The scaling factor.
8778 */
Chet Haasea5531132012-02-02 13:41:44 -08008779 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008780 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008781 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008782 }
8783
8784 /**
8785 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
8786 * the view's unscaled width. A value of 1 means that no scaling is applied.
8787 *
8788 * @param scaleX The scaling factor.
8789 * @see #getPivotX()
8790 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008791 *
8792 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07008793 */
8794 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008795 ensureTransformationInfo();
8796 final TransformationInfo info = mTransformationInfo;
8797 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008798 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008799 info.mScaleX = scaleX;
8800 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008801 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008802 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008803 mDisplayList.setScaleX(scaleX);
8804 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008805 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8806 // View was rejected last time it was drawn by its parent; this may have changed
8807 invalidateParentIfNeeded();
8808 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008809 }
8810 }
8811
8812 /**
8813 * The amount that the view is scaled in y around the pivot point, as a proportion of
8814 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
8815 *
Joe Onorato93162322010-09-16 15:42:01 -04008816 * <p>By default, this is 1.0f.
8817 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008818 * @see #getPivotX()
8819 * @see #getPivotY()
8820 * @return The scaling factor.
8821 */
Chet Haasea5531132012-02-02 13:41:44 -08008822 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008823 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008824 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008825 }
8826
8827 /**
8828 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
8829 * the view's unscaled width. A value of 1 means that no scaling is applied.
8830 *
8831 * @param scaleY The scaling factor.
8832 * @see #getPivotX()
8833 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008834 *
8835 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07008836 */
8837 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008838 ensureTransformationInfo();
8839 final TransformationInfo info = mTransformationInfo;
8840 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008841 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008842 info.mScaleY = scaleY;
8843 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008844 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008845 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008846 mDisplayList.setScaleY(scaleY);
8847 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008848 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8849 // View was rejected last time it was drawn by its parent; this may have changed
8850 invalidateParentIfNeeded();
8851 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008852 }
8853 }
8854
8855 /**
8856 * The x location of the point around which the view is {@link #setRotation(float) rotated}
8857 * and {@link #setScaleX(float) scaled}.
8858 *
8859 * @see #getRotation()
8860 * @see #getScaleX()
8861 * @see #getScaleY()
8862 * @see #getPivotY()
8863 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008864 *
8865 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008866 */
Chet Haasea5531132012-02-02 13:41:44 -08008867 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008868 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008869 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008870 }
8871
8872 /**
8873 * Sets the x location of the point around which the view is
8874 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07008875 * By default, the pivot point is centered on the object.
8876 * Setting this property disables this behavior and causes the view to use only the
8877 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008878 *
8879 * @param pivotX The x location of the pivot point.
8880 * @see #getRotation()
8881 * @see #getScaleX()
8882 * @see #getScaleY()
8883 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008884 *
8885 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008886 */
8887 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008888 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07008889 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008890 final TransformationInfo info = mTransformationInfo;
8891 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008892 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008893 info.mPivotX = pivotX;
8894 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008895 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008896 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008897 mDisplayList.setPivotX(pivotX);
8898 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008899 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8900 // View was rejected last time it was drawn by its parent; this may have changed
8901 invalidateParentIfNeeded();
8902 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008903 }
8904 }
8905
8906 /**
8907 * The y location of the point around which the view is {@link #setRotation(float) rotated}
8908 * and {@link #setScaleY(float) scaled}.
8909 *
8910 * @see #getRotation()
8911 * @see #getScaleX()
8912 * @see #getScaleY()
8913 * @see #getPivotY()
8914 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008915 *
8916 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07008917 */
Chet Haasea5531132012-02-02 13:41:44 -08008918 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008919 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008920 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008921 }
8922
8923 /**
8924 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07008925 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
8926 * Setting this property disables this behavior and causes the view to use only the
8927 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008928 *
8929 * @param pivotY The y location of the pivot point.
8930 * @see #getRotation()
8931 * @see #getScaleX()
8932 * @see #getScaleY()
8933 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008934 *
8935 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07008936 */
8937 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008938 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07008939 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008940 final TransformationInfo info = mTransformationInfo;
8941 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008942 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008943 info.mPivotY = pivotY;
8944 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008945 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008946 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008947 mDisplayList.setPivotY(pivotY);
8948 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008949 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8950 // View was rejected last time it was drawn by its parent; this may have changed
8951 invalidateParentIfNeeded();
8952 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008953 }
8954 }
8955
8956 /**
8957 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
8958 * completely transparent and 1 means the view is completely opaque.
8959 *
Joe Onorato93162322010-09-16 15:42:01 -04008960 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07008961 * @return The opacity of the view.
8962 */
Chet Haasea5531132012-02-02 13:41:44 -08008963 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008964 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008965 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008966 }
8967
8968 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07008969 * Returns whether this View has content which overlaps. This function, intended to be
8970 * overridden by specific View types, is an optimization when alpha is set on a view. If
8971 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
8972 * and then composited it into place, which can be expensive. If the view has no overlapping
8973 * rendering, the view can draw each primitive with the appropriate alpha value directly.
8974 * An example of overlapping rendering is a TextView with a background image, such as a
8975 * Button. An example of non-overlapping rendering is a TextView with no background, or
8976 * an ImageView with only the foreground image. The default implementation returns true;
8977 * subclasses should override if they have cases which can be optimized.
8978 *
8979 * @return true if the content in this view might overlap, false otherwise.
8980 */
8981 public boolean hasOverlappingRendering() {
8982 return true;
8983 }
8984
8985 /**
Romain Guy171c5922011-01-06 10:04:23 -08008986 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
8987 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008988 *
Romain Guy171c5922011-01-06 10:04:23 -08008989 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
8990 * responsible for applying the opacity itself. Otherwise, calling this method is
8991 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08008992 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07008993 *
Chet Haasea5531132012-02-02 13:41:44 -08008994 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
8995 * performance implications. It is generally best to use the alpha property sparingly and
8996 * transiently, as in the case of fading animations.</p>
8997 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008998 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08008999 *
Joe Malin32736f02011-01-19 16:14:20 -08009000 * @see #setLayerType(int, android.graphics.Paint)
9001 *
Chet Haase73066682010-11-29 15:55:32 -08009002 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07009003 */
9004 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009005 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009006 if (mTransformationInfo.mAlpha != alpha) {
9007 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009008 if (onSetAlpha((int) (alpha * 255))) {
9009 mPrivateFlags |= ALPHA_SET;
9010 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07009011 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009012 invalidate(true);
9013 } else {
9014 mPrivateFlags &= ~ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07009015 invalidateViewProperty(true, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07009016 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009017 mDisplayList.setAlpha(alpha);
9018 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009019 }
Chet Haaseed032702010-10-01 14:05:54 -07009020 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009021 }
9022
9023 /**
Chet Haasea00f3862011-02-22 06:34:40 -08009024 * Faster version of setAlpha() which performs the same steps except there are
9025 * no calls to invalidate(). The caller of this function should perform proper invalidation
9026 * on the parent and this object. The return value indicates whether the subclass handles
9027 * alpha (the return value for onSetAlpha()).
9028 *
9029 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009030 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
9031 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08009032 */
9033 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009034 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009035 if (mTransformationInfo.mAlpha != alpha) {
9036 mTransformationInfo.mAlpha = alpha;
9037 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
9038 if (subclassHandlesAlpha) {
9039 mPrivateFlags |= ALPHA_SET;
9040 return true;
9041 } else {
9042 mPrivateFlags &= ~ALPHA_SET;
Chet Haase1271e2c2012-04-20 09:54:27 -07009043 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009044 mDisplayList.setAlpha(alpha);
9045 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009046 }
Chet Haasea00f3862011-02-22 06:34:40 -08009047 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009048 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08009049 }
9050
9051 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009052 * Top position of this view relative to its parent.
9053 *
9054 * @return The top of this view, in pixels.
9055 */
9056 @ViewDebug.CapturedViewProperty
9057 public final int getTop() {
9058 return mTop;
9059 }
9060
9061 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009062 * Sets the top position of this view relative to its parent. This method is meant to be called
9063 * by the layout system and should not generally be called otherwise, because the property
9064 * may be changed at any time by the layout.
9065 *
9066 * @param top The top of this view, in pixels.
9067 */
9068 public final void setTop(int top) {
9069 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07009070 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009071 final boolean matrixIsIdentity = mTransformationInfo == null
9072 || mTransformationInfo.mMatrixIsIdentity;
9073 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009074 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009075 int minTop;
9076 int yLoc;
9077 if (top < mTop) {
9078 minTop = top;
9079 yLoc = top - mTop;
9080 } else {
9081 minTop = mTop;
9082 yLoc = 0;
9083 }
Chet Haasee9140a72011-02-16 16:23:29 -08009084 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009085 }
9086 } else {
9087 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009088 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009089 }
9090
Chet Haaseed032702010-10-01 14:05:54 -07009091 int width = mRight - mLeft;
9092 int oldHeight = mBottom - mTop;
9093
Chet Haase21cd1382010-09-01 17:42:29 -07009094 mTop = top;
Chet Haase1271e2c2012-04-20 09:54:27 -07009095 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009096 mDisplayList.setTop(mTop);
9097 }
Chet Haase21cd1382010-09-01 17:42:29 -07009098
Chet Haaseed032702010-10-01 14:05:54 -07009099 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9100
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009101 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009102 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9103 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009104 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009105 }
Chet Haase21cd1382010-09-01 17:42:29 -07009106 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009107 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009108 }
Chet Haase55dbb652010-12-21 20:15:08 -08009109 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009110 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009111 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9112 // View was rejected last time it was drawn by its parent; this may have changed
9113 invalidateParentIfNeeded();
9114 }
Chet Haase21cd1382010-09-01 17:42:29 -07009115 }
9116 }
9117
9118 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009119 * Bottom position of this view relative to its parent.
9120 *
9121 * @return The bottom of this view, in pixels.
9122 */
9123 @ViewDebug.CapturedViewProperty
9124 public final int getBottom() {
9125 return mBottom;
9126 }
9127
9128 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08009129 * True if this view has changed since the last time being drawn.
9130 *
9131 * @return The dirty state of this view.
9132 */
9133 public boolean isDirty() {
9134 return (mPrivateFlags & DIRTY_MASK) != 0;
9135 }
9136
9137 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009138 * Sets the bottom position of this view relative to its parent. This method is meant to be
9139 * called by the layout system and should not generally be called otherwise, because the
9140 * property may be changed at any time by the layout.
9141 *
9142 * @param bottom The bottom of this view, in pixels.
9143 */
9144 public final void setBottom(int bottom) {
9145 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07009146 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009147 final boolean matrixIsIdentity = mTransformationInfo == null
9148 || mTransformationInfo.mMatrixIsIdentity;
9149 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009150 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009151 int maxBottom;
9152 if (bottom < mBottom) {
9153 maxBottom = mBottom;
9154 } else {
9155 maxBottom = bottom;
9156 }
Chet Haasee9140a72011-02-16 16:23:29 -08009157 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009158 }
9159 } else {
9160 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009161 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009162 }
9163
Chet Haaseed032702010-10-01 14:05:54 -07009164 int width = mRight - mLeft;
9165 int oldHeight = mBottom - mTop;
9166
Chet Haase21cd1382010-09-01 17:42:29 -07009167 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -07009168 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009169 mDisplayList.setBottom(mBottom);
9170 }
Chet Haase21cd1382010-09-01 17:42:29 -07009171
Chet Haaseed032702010-10-01 14:05:54 -07009172 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9173
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009174 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009175 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9176 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009177 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009178 }
Chet Haase21cd1382010-09-01 17:42:29 -07009179 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009180 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009181 }
Chet Haase55dbb652010-12-21 20:15:08 -08009182 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009183 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009184 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9185 // View was rejected last time it was drawn by its parent; this may have changed
9186 invalidateParentIfNeeded();
9187 }
Chet Haase21cd1382010-09-01 17:42:29 -07009188 }
9189 }
9190
9191 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009192 * Left position of this view relative to its parent.
9193 *
9194 * @return The left edge of this view, in pixels.
9195 */
9196 @ViewDebug.CapturedViewProperty
9197 public final int getLeft() {
9198 return mLeft;
9199 }
9200
9201 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009202 * Sets the left position of this view relative to its parent. This method is meant to be called
9203 * by the layout system and should not generally be called otherwise, because the property
9204 * may be changed at any time by the layout.
9205 *
9206 * @param left The bottom of this view, in pixels.
9207 */
9208 public final void setLeft(int left) {
9209 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07009210 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009211 final boolean matrixIsIdentity = mTransformationInfo == null
9212 || mTransformationInfo.mMatrixIsIdentity;
9213 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009214 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009215 int minLeft;
9216 int xLoc;
9217 if (left < mLeft) {
9218 minLeft = left;
9219 xLoc = left - mLeft;
9220 } else {
9221 minLeft = mLeft;
9222 xLoc = 0;
9223 }
Chet Haasee9140a72011-02-16 16:23:29 -08009224 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009225 }
9226 } else {
9227 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009228 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009229 }
9230
Chet Haaseed032702010-10-01 14:05:54 -07009231 int oldWidth = mRight - mLeft;
9232 int height = mBottom - mTop;
9233
Chet Haase21cd1382010-09-01 17:42:29 -07009234 mLeft = left;
Chet Haase1271e2c2012-04-20 09:54:27 -07009235 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009236 mDisplayList.setLeft(left);
9237 }
Chet Haase21cd1382010-09-01 17:42:29 -07009238
Chet Haaseed032702010-10-01 14:05:54 -07009239 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9240
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009241 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009242 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9243 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009244 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009245 }
Chet Haase21cd1382010-09-01 17:42:29 -07009246 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009247 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009248 }
Chet Haase55dbb652010-12-21 20:15:08 -08009249 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009250 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009251 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9252 // View was rejected last time it was drawn by its parent; this may have changed
9253 invalidateParentIfNeeded();
9254 }
Chet Haase21cd1382010-09-01 17:42:29 -07009255 }
9256 }
9257
9258 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009259 * Right position of this view relative to its parent.
9260 *
9261 * @return The right edge of this view, in pixels.
9262 */
9263 @ViewDebug.CapturedViewProperty
9264 public final int getRight() {
9265 return mRight;
9266 }
9267
9268 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009269 * Sets the right position of this view relative to its parent. This method is meant to be called
9270 * by the layout system and should not generally be called otherwise, because the property
9271 * may be changed at any time by the layout.
9272 *
9273 * @param right The bottom of this view, in pixels.
9274 */
9275 public final void setRight(int right) {
9276 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07009277 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009278 final boolean matrixIsIdentity = mTransformationInfo == null
9279 || mTransformationInfo.mMatrixIsIdentity;
9280 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009281 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009282 int maxRight;
9283 if (right < mRight) {
9284 maxRight = mRight;
9285 } else {
9286 maxRight = right;
9287 }
Chet Haasee9140a72011-02-16 16:23:29 -08009288 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009289 }
9290 } else {
9291 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009292 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009293 }
9294
Chet Haaseed032702010-10-01 14:05:54 -07009295 int oldWidth = mRight - mLeft;
9296 int height = mBottom - mTop;
9297
Chet Haase21cd1382010-09-01 17:42:29 -07009298 mRight = right;
Chet Haase1271e2c2012-04-20 09:54:27 -07009299 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009300 mDisplayList.setRight(mRight);
9301 }
Chet Haase21cd1382010-09-01 17:42:29 -07009302
Chet Haaseed032702010-10-01 14:05:54 -07009303 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9304
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009305 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009306 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9307 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009308 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009309 }
Chet Haase21cd1382010-09-01 17:42:29 -07009310 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009311 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009312 }
Chet Haase55dbb652010-12-21 20:15:08 -08009313 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009314 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009315 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9316 // View was rejected last time it was drawn by its parent; this may have changed
9317 invalidateParentIfNeeded();
9318 }
Chet Haase21cd1382010-09-01 17:42:29 -07009319 }
9320 }
9321
9322 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009323 * The visual x position of this view, in pixels. This is equivalent to the
9324 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08009325 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07009326 *
Chet Haasedf030d22010-07-30 17:22:38 -07009327 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009328 */
Chet Haasea5531132012-02-02 13:41:44 -08009329 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009330 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009331 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009332 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009333
Chet Haasedf030d22010-07-30 17:22:38 -07009334 /**
9335 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
9336 * {@link #setTranslationX(float) translationX} property to be the difference between
9337 * the x value passed in and the current {@link #getLeft() left} property.
9338 *
9339 * @param x The visual x position of this view, in pixels.
9340 */
9341 public void setX(float x) {
9342 setTranslationX(x - mLeft);
9343 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009344
Chet Haasedf030d22010-07-30 17:22:38 -07009345 /**
9346 * The visual y position of this view, in pixels. This is equivalent to the
9347 * {@link #setTranslationY(float) translationY} property plus the current
9348 * {@link #getTop() top} property.
9349 *
9350 * @return The visual y position of this view, in pixels.
9351 */
Chet Haasea5531132012-02-02 13:41:44 -08009352 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009353 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009354 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009355 }
9356
9357 /**
9358 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
9359 * {@link #setTranslationY(float) translationY} property to be the difference between
9360 * the y value passed in and the current {@link #getTop() top} property.
9361 *
9362 * @param y The visual y position of this view, in pixels.
9363 */
9364 public void setY(float y) {
9365 setTranslationY(y - mTop);
9366 }
9367
9368
9369 /**
9370 * The horizontal location of this view relative to its {@link #getLeft() left} position.
9371 * This position is post-layout, in addition to wherever the object's
9372 * layout placed it.
9373 *
9374 * @return The horizontal position of this view relative to its left position, in pixels.
9375 */
Chet Haasea5531132012-02-02 13:41:44 -08009376 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009377 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009378 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07009379 }
9380
9381 /**
9382 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
9383 * This effectively positions the object post-layout, in addition to wherever the object's
9384 * layout placed it.
9385 *
9386 * @param translationX The horizontal position of this view relative to its left position,
9387 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009388 *
9389 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07009390 */
9391 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009392 ensureTransformationInfo();
9393 final TransformationInfo info = mTransformationInfo;
9394 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07009395 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009396 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009397 info.mTranslationX = translationX;
9398 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009399 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009400 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009401 mDisplayList.setTranslationX(translationX);
9402 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009403 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9404 // View was rejected last time it was drawn by its parent; this may have changed
9405 invalidateParentIfNeeded();
9406 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009407 }
9408 }
9409
9410 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009411 * The horizontal location of this view relative to its {@link #getTop() top} position.
9412 * This position is post-layout, in addition to wherever the object's
9413 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009414 *
Chet Haasedf030d22010-07-30 17:22:38 -07009415 * @return The vertical position of this view relative to its top position,
9416 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009417 */
Chet Haasea5531132012-02-02 13:41:44 -08009418 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009419 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009420 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009421 }
9422
9423 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009424 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9425 * This effectively positions the object post-layout, in addition to wherever the object's
9426 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009427 *
Chet Haasedf030d22010-07-30 17:22:38 -07009428 * @param translationY The vertical position of this view relative to its top position,
9429 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009430 *
9431 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009432 */
Chet Haasedf030d22010-07-30 17:22:38 -07009433 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009434 ensureTransformationInfo();
9435 final TransformationInfo info = mTransformationInfo;
9436 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009437 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009438 info.mTranslationY = translationY;
9439 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009440 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009441 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009442 mDisplayList.setTranslationY(translationY);
9443 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009444 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9445 // View was rejected last time it was drawn by its parent; this may have changed
9446 invalidateParentIfNeeded();
9447 }
Chet Haasedf030d22010-07-30 17:22:38 -07009448 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009449 }
9450
9451 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009452 * Hit rectangle in parent's coordinates
9453 *
9454 * @param outRect The hit rectangle of the view.
9455 */
9456 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009457 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009458 final TransformationInfo info = mTransformationInfo;
9459 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009460 outRect.set(mLeft, mTop, mRight, mBottom);
9461 } else {
9462 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009463 tmpRect.set(-info.mPivotX, -info.mPivotY,
9464 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9465 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009466 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9467 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009468 }
9469 }
9470
9471 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009472 * Determines whether the given point, in local coordinates is inside the view.
9473 */
9474 /*package*/ final boolean pointInView(float localX, float localY) {
9475 return localX >= 0 && localX < (mRight - mLeft)
9476 && localY >= 0 && localY < (mBottom - mTop);
9477 }
9478
9479 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009480 * Utility method to determine whether the given point, in local coordinates,
9481 * is inside the view, where the area of the view is expanded by the slop factor.
9482 * This method is called while processing touch-move events to determine if the event
9483 * is still within the view.
9484 */
9485 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009486 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009487 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009488 }
9489
9490 /**
9491 * When a view has focus and the user navigates away from it, the next view is searched for
9492 * starting from the rectangle filled in by this method.
9493 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009494 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
9495 * of the view. However, if your view maintains some idea of internal selection,
9496 * such as a cursor, or a selected row or column, you should override this method and
9497 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009498 *
9499 * @param r The rectangle to fill in, in this view's coordinates.
9500 */
9501 public void getFocusedRect(Rect r) {
9502 getDrawingRect(r);
9503 }
9504
9505 /**
9506 * If some part of this view is not clipped by any of its parents, then
9507 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009508 * coordinates (without taking possible View rotations into account), offset
9509 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9510 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009511 *
9512 * @param r If true is returned, r holds the global coordinates of the
9513 * visible portion of this view.
9514 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9515 * between this view and its root. globalOffet may be null.
9516 * @return true if r is non-empty (i.e. part of the view is visible at the
9517 * root level.
9518 */
9519 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9520 int width = mRight - mLeft;
9521 int height = mBottom - mTop;
9522 if (width > 0 && height > 0) {
9523 r.set(0, 0, width, height);
9524 if (globalOffset != null) {
9525 globalOffset.set(-mScrollX, -mScrollY);
9526 }
9527 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9528 }
9529 return false;
9530 }
9531
9532 public final boolean getGlobalVisibleRect(Rect r) {
9533 return getGlobalVisibleRect(r, null);
9534 }
9535
9536 public final boolean getLocalVisibleRect(Rect r) {
Romain Guyab4c4f4f2012-05-06 13:11:24 -07009537 final Point offset = mAttachInfo != null ? mAttachInfo.mPoint : new Point();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009538 if (getGlobalVisibleRect(r, offset)) {
9539 r.offset(-offset.x, -offset.y); // make r local
9540 return true;
9541 }
9542 return false;
9543 }
9544
9545 /**
9546 * Offset this view's vertical location by the specified number of pixels.
9547 *
9548 * @param offset the number of pixels to offset the view by
9549 */
9550 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009551 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009552 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009553 final boolean matrixIsIdentity = mTransformationInfo == null
9554 || mTransformationInfo.mMatrixIsIdentity;
9555 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009556 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009557 invalidateViewProperty(false, false);
9558 } else {
9559 final ViewParent p = mParent;
9560 if (p != null && mAttachInfo != null) {
9561 final Rect r = mAttachInfo.mTmpInvalRect;
9562 int minTop;
9563 int maxBottom;
9564 int yLoc;
9565 if (offset < 0) {
9566 minTop = mTop + offset;
9567 maxBottom = mBottom;
9568 yLoc = offset;
9569 } else {
9570 minTop = mTop;
9571 maxBottom = mBottom + offset;
9572 yLoc = 0;
9573 }
9574 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
9575 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009576 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009577 }
9578 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009579 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009580 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009581
Chet Haasec3aa3612010-06-17 08:50:37 -07009582 mTop += offset;
9583 mBottom += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009584 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009585 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009586 invalidateViewProperty(false, false);
9587 } else {
9588 if (!matrixIsIdentity) {
9589 invalidateViewProperty(false, true);
9590 }
9591 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009592 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009593 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009594 }
9595
9596 /**
9597 * Offset this view's horizontal location by the specified amount of pixels.
9598 *
9599 * @param offset the numer of pixels to offset the view by
9600 */
9601 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009602 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009603 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009604 final boolean matrixIsIdentity = mTransformationInfo == null
9605 || mTransformationInfo.mMatrixIsIdentity;
9606 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009607 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009608 invalidateViewProperty(false, false);
9609 } else {
9610 final ViewParent p = mParent;
9611 if (p != null && mAttachInfo != null) {
9612 final Rect r = mAttachInfo.mTmpInvalRect;
9613 int minLeft;
9614 int maxRight;
9615 if (offset < 0) {
9616 minLeft = mLeft + offset;
9617 maxRight = mRight;
9618 } else {
9619 minLeft = mLeft;
9620 maxRight = mRight + offset;
9621 }
9622 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
9623 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009624 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009625 }
9626 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009627 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009628 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009629
Chet Haasec3aa3612010-06-17 08:50:37 -07009630 mLeft += offset;
9631 mRight += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009632 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009633 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009634 invalidateViewProperty(false, false);
9635 } else {
9636 if (!matrixIsIdentity) {
9637 invalidateViewProperty(false, true);
9638 }
9639 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009640 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009641 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009642 }
9643
9644 /**
9645 * Get the LayoutParams associated with this view. All views should have
9646 * layout parameters. These supply parameters to the <i>parent</i> of this
9647 * view specifying how it should be arranged. There are many subclasses of
9648 * ViewGroup.LayoutParams, and these correspond to the different subclasses
9649 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08009650 *
9651 * This method may return null if this View is not attached to a parent
9652 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
9653 * was not invoked successfully. When a View is attached to a parent
9654 * ViewGroup, this method must not return null.
9655 *
9656 * @return The LayoutParams associated with this view, or null if no
9657 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009658 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07009659 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009660 public ViewGroup.LayoutParams getLayoutParams() {
9661 return mLayoutParams;
9662 }
9663
9664 /**
9665 * Set the layout parameters associated with this view. These supply
9666 * parameters to the <i>parent</i> of this view specifying how it should be
9667 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
9668 * correspond to the different subclasses of ViewGroup that are responsible
9669 * for arranging their children.
9670 *
Romain Guy01c174b2011-02-22 11:51:06 -08009671 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009672 */
9673 public void setLayoutParams(ViewGroup.LayoutParams params) {
9674 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08009675 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009676 }
9677 mLayoutParams = params;
Philip Milned7dd8902012-01-26 16:55:30 -08009678 if (mParent instanceof ViewGroup) {
9679 ((ViewGroup) mParent).onSetLayoutParams(this, params);
9680 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009681 requestLayout();
9682 }
9683
9684 /**
9685 * Set the scrolled position of your view. This will cause a call to
9686 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9687 * invalidated.
9688 * @param x the x position to scroll to
9689 * @param y the y position to scroll to
9690 */
9691 public void scrollTo(int x, int y) {
9692 if (mScrollX != x || mScrollY != y) {
9693 int oldX = mScrollX;
9694 int oldY = mScrollY;
9695 mScrollX = x;
9696 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009697 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009698 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07009699 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009700 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -07009701 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009702 }
9703 }
9704
9705 /**
9706 * Move the scrolled position of your view. This will cause a call to
9707 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9708 * invalidated.
9709 * @param x the amount of pixels to scroll by horizontally
9710 * @param y the amount of pixels to scroll by vertically
9711 */
9712 public void scrollBy(int x, int y) {
9713 scrollTo(mScrollX + x, mScrollY + y);
9714 }
9715
9716 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009717 * <p>Trigger the scrollbars to draw. When invoked this method starts an
9718 * animation to fade the scrollbars out after a default delay. If a subclass
9719 * provides animated scrolling, the start delay should equal the duration
9720 * of the scrolling animation.</p>
9721 *
9722 * <p>The animation starts only if at least one of the scrollbars is
9723 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
9724 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9725 * this method returns true, and false otherwise. If the animation is
9726 * started, this method calls {@link #invalidate()}; in that case the
9727 * caller should not call {@link #invalidate()}.</p>
9728 *
9729 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07009730 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07009731 *
9732 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
9733 * and {@link #scrollTo(int, int)}.</p>
9734 *
9735 * @return true if the animation is played, false otherwise
9736 *
9737 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07009738 * @see #scrollBy(int, int)
9739 * @see #scrollTo(int, int)
9740 * @see #isHorizontalScrollBarEnabled()
9741 * @see #isVerticalScrollBarEnabled()
9742 * @see #setHorizontalScrollBarEnabled(boolean)
9743 * @see #setVerticalScrollBarEnabled(boolean)
9744 */
9745 protected boolean awakenScrollBars() {
9746 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07009747 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07009748 }
9749
9750 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07009751 * Trigger the scrollbars to draw.
9752 * This method differs from awakenScrollBars() only in its default duration.
9753 * initialAwakenScrollBars() will show the scroll bars for longer than
9754 * usual to give the user more of a chance to notice them.
9755 *
9756 * @return true if the animation is played, false otherwise.
9757 */
9758 private boolean initialAwakenScrollBars() {
9759 return mScrollCache != null &&
9760 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
9761 }
9762
9763 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009764 * <p>
9765 * Trigger the scrollbars to draw. When invoked this method starts an
9766 * animation to fade the scrollbars out after a fixed delay. If a subclass
9767 * provides animated scrolling, the start delay should equal the duration of
9768 * the scrolling animation.
9769 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009770 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009771 * <p>
9772 * The animation starts only if at least one of the scrollbars is enabled,
9773 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9774 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9775 * this method returns true, and false otherwise. If the animation is
9776 * started, this method calls {@link #invalidate()}; in that case the caller
9777 * should not call {@link #invalidate()}.
9778 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009779 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009780 * <p>
9781 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07009782 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07009783 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009784 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009785 * @param startDelay the delay, in milliseconds, after which the animation
9786 * should start; when the delay is 0, the animation starts
9787 * immediately
9788 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009789 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009790 * @see #scrollBy(int, int)
9791 * @see #scrollTo(int, int)
9792 * @see #isHorizontalScrollBarEnabled()
9793 * @see #isVerticalScrollBarEnabled()
9794 * @see #setHorizontalScrollBarEnabled(boolean)
9795 * @see #setVerticalScrollBarEnabled(boolean)
9796 */
9797 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07009798 return awakenScrollBars(startDelay, true);
9799 }
Joe Malin32736f02011-01-19 16:14:20 -08009800
Mike Cleron290947b2009-09-29 18:34:32 -07009801 /**
9802 * <p>
9803 * Trigger the scrollbars to draw. When invoked this method starts an
9804 * animation to fade the scrollbars out after a fixed delay. If a subclass
9805 * provides animated scrolling, the start delay should equal the duration of
9806 * the scrolling animation.
9807 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009808 *
Mike Cleron290947b2009-09-29 18:34:32 -07009809 * <p>
9810 * The animation starts only if at least one of the scrollbars is enabled,
9811 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9812 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9813 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08009814 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07009815 * is set to true; in that case the caller
9816 * should not call {@link #invalidate()}.
9817 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009818 *
Mike Cleron290947b2009-09-29 18:34:32 -07009819 * <p>
9820 * This method should be invoked everytime a subclass directly updates the
9821 * scroll parameters.
9822 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009823 *
Mike Cleron290947b2009-09-29 18:34:32 -07009824 * @param startDelay the delay, in milliseconds, after which the animation
9825 * should start; when the delay is 0, the animation starts
9826 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08009827 *
Mike Cleron290947b2009-09-29 18:34:32 -07009828 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08009829 *
Mike Cleron290947b2009-09-29 18:34:32 -07009830 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009831 *
Mike Cleron290947b2009-09-29 18:34:32 -07009832 * @see #scrollBy(int, int)
9833 * @see #scrollTo(int, int)
9834 * @see #isHorizontalScrollBarEnabled()
9835 * @see #isVerticalScrollBarEnabled()
9836 * @see #setHorizontalScrollBarEnabled(boolean)
9837 * @see #setVerticalScrollBarEnabled(boolean)
9838 */
9839 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07009840 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08009841
Mike Cleronf116bf82009-09-27 19:14:12 -07009842 if (scrollCache == null || !scrollCache.fadeScrollBars) {
9843 return false;
9844 }
9845
9846 if (scrollCache.scrollBar == null) {
9847 scrollCache.scrollBar = new ScrollBarDrawable();
9848 }
9849
9850 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
9851
Mike Cleron290947b2009-09-29 18:34:32 -07009852 if (invalidate) {
9853 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009854 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -07009855 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009856
9857 if (scrollCache.state == ScrollabilityCache.OFF) {
9858 // FIXME: this is copied from WindowManagerService.
9859 // We should get this value from the system when it
9860 // is possible to do so.
9861 final int KEY_REPEAT_FIRST_DELAY = 750;
9862 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
9863 }
9864
9865 // Tell mScrollCache when we should start fading. This may
9866 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07009867 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07009868 scrollCache.fadeStartTime = fadeStartTime;
9869 scrollCache.state = ScrollabilityCache.ON;
9870
9871 // Schedule our fader to run, unscheduling any old ones first
9872 if (mAttachInfo != null) {
9873 mAttachInfo.mHandler.removeCallbacks(scrollCache);
9874 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
9875 }
9876
9877 return true;
9878 }
9879
9880 return false;
9881 }
9882
9883 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07009884 * Do not invalidate views which are not visible and which are not running an animation. They
9885 * will not get drawn and they should not set dirty flags as if they will be drawn
9886 */
9887 private boolean skipInvalidate() {
9888 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
9889 (!(mParent instanceof ViewGroup) ||
9890 !((ViewGroup) mParent).isViewTransitioning(this));
9891 }
9892 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07009893 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009894 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
9895 * in the future. This must be called from a UI thread. To call from a non-UI
9896 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009897 *
9898 * WARNING: This method is destructive to dirty.
9899 * @param dirty the rectangle representing the bounds of the dirty region
9900 */
9901 public void invalidate(Rect dirty) {
Chet Haaseaceafe62011-08-26 15:44:33 -07009902 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009903 return;
9904 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009905 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08009906 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
9907 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009908 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08009909 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07009910 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009911 final ViewParent p = mParent;
9912 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08009913 //noinspection PointlessBooleanExpression,ConstantConditions
9914 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9915 if (p != null && ai != null && ai.mHardwareAccelerated) {
9916 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009917 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009918 p.invalidateChild(this, null);
9919 return;
9920 }
Romain Guyaf636eb2010-12-09 17:47:21 -08009921 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009922 if (p != null && ai != null) {
9923 final int scrollX = mScrollX;
9924 final int scrollY = mScrollY;
9925 final Rect r = ai.mTmpInvalRect;
9926 r.set(dirty.left - scrollX, dirty.top - scrollY,
9927 dirty.right - scrollX, dirty.bottom - scrollY);
9928 mParent.invalidateChild(this, r);
9929 }
9930 }
9931 }
9932
9933 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07009934 * 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 -08009935 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009936 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
9937 * will be called at some point in the future. This must be called from
9938 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009939 * @param l the left position of the dirty region
9940 * @param t the top position of the dirty region
9941 * @param r the right position of the dirty region
9942 * @param b the bottom position of the dirty region
9943 */
9944 public void invalidate(int l, int t, int r, int b) {
Chet Haaseaceafe62011-08-26 15:44:33 -07009945 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009946 return;
9947 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009948 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08009949 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
9950 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009951 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08009952 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07009953 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009954 final ViewParent p = mParent;
9955 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08009956 //noinspection PointlessBooleanExpression,ConstantConditions
9957 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9958 if (p != null && ai != null && ai.mHardwareAccelerated) {
9959 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009960 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009961 p.invalidateChild(this, null);
9962 return;
9963 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08009964 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009965 if (p != null && ai != null && l < r && t < b) {
9966 final int scrollX = mScrollX;
9967 final int scrollY = mScrollY;
9968 final Rect tmpr = ai.mTmpInvalRect;
9969 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
9970 p.invalidateChild(this, tmpr);
9971 }
9972 }
9973 }
9974
9975 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009976 * Invalidate the whole view. If the view is visible,
9977 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
9978 * the future. This must be called from a UI thread. To call from a non-UI thread,
9979 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009980 */
9981 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07009982 invalidate(true);
9983 }
Joe Malin32736f02011-01-19 16:14:20 -08009984
Chet Haaseed032702010-10-01 14:05:54 -07009985 /**
9986 * This is where the invalidate() work actually happens. A full invalidate()
9987 * causes the drawing cache to be invalidated, but this function can be called with
9988 * invalidateCache set to false to skip that invalidation step for cases that do not
9989 * need it (for example, a component that remains at the same dimensions with the same
9990 * content).
9991 *
9992 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
9993 * well. This is usually true for a full invalidate, but may be set to false if the
9994 * View's contents or dimensions have not changed.
9995 */
Romain Guy849d0a32011-02-01 17:20:48 -08009996 void invalidate(boolean invalidateCache) {
Chet Haaseaceafe62011-08-26 15:44:33 -07009997 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009998 return;
9999 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010000 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -080010001 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -080010002 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
10003 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -070010004 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -070010005 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -070010006 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -080010007 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -070010008 mPrivateFlags &= ~DRAWING_CACHE_VALID;
10009 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010010 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -070010011 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -080010012 //noinspection PointlessBooleanExpression,ConstantConditions
10013 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10014 if (p != null && ai != null && ai.mHardwareAccelerated) {
10015 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010016 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010017 p.invalidateChild(this, null);
10018 return;
10019 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010020 }
Michael Jurkaebefea42010-11-15 16:04:01 -080010021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010022 if (p != null && ai != null) {
10023 final Rect r = ai.mTmpInvalRect;
10024 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10025 // Don't call invalidate -- we don't want to internally scroll
10026 // our own bounds
10027 p.invalidateChild(this, r);
10028 }
10029 }
10030 }
10031
10032 /**
Chet Haase9d1992d2012-03-13 11:03:25 -070010033 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
10034 * set any flags or handle all of the cases handled by the default invalidation methods.
10035 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
10036 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
10037 * walk up the hierarchy, transforming the dirty rect as necessary.
10038 *
10039 * The method also handles normal invalidation logic if display list properties are not
10040 * being used in this view. The invalidateParent and forceRedraw flags are used by that
10041 * backup approach, to handle these cases used in the various property-setting methods.
10042 *
10043 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
10044 * are not being used in this view
10045 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
10046 * list properties are not being used in this view
10047 */
10048 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
Chet Haase1271e2c2012-04-20 09:54:27 -070010049 if (mDisplayList == null || (mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
Chet Haase9d1992d2012-03-13 11:03:25 -070010050 if (invalidateParent) {
10051 invalidateParentCaches();
10052 }
10053 if (forceRedraw) {
10054 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
10055 }
10056 invalidate(false);
10057 } else {
10058 final AttachInfo ai = mAttachInfo;
10059 final ViewParent p = mParent;
10060 if (p != null && ai != null) {
10061 final Rect r = ai.mTmpInvalRect;
10062 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10063 if (mParent instanceof ViewGroup) {
10064 ((ViewGroup) mParent).invalidateChildFast(this, r);
10065 } else {
10066 mParent.invalidateChild(this, r);
10067 }
10068 }
10069 }
10070 }
10071
10072 /**
10073 * Utility method to transform a given Rect by the current matrix of this view.
10074 */
10075 void transformRect(final Rect rect) {
10076 if (!getMatrix().isIdentity()) {
10077 RectF boundingRect = mAttachInfo.mTmpTransformRect;
10078 boundingRect.set(rect);
10079 getMatrix().mapRect(boundingRect);
10080 rect.set((int) (boundingRect.left - 0.5f),
10081 (int) (boundingRect.top - 0.5f),
10082 (int) (boundingRect.right + 0.5f),
10083 (int) (boundingRect.bottom + 0.5f));
10084 }
10085 }
10086
10087 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -080010088 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -080010089 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10090 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -080010091 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
10092 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -080010093 *
10094 * @hide
10095 */
Romain Guy0fd89bf2011-01-26 15:41:30 -080010096 protected void invalidateParentCaches() {
10097 if (mParent instanceof View) {
10098 ((View) mParent).mPrivateFlags |= INVALIDATED;
10099 }
10100 }
Joe Malin32736f02011-01-19 16:14:20 -080010101
Romain Guy0fd89bf2011-01-26 15:41:30 -080010102 /**
10103 * Used to indicate that the parent of this view should be invalidated. This functionality
10104 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10105 * which is necessary when various parent-managed properties of the view change, such as
10106 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
10107 * an invalidation event to the parent.
10108 *
10109 * @hide
10110 */
10111 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -080010112 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010113 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -080010114 }
10115 }
10116
10117 /**
Romain Guy24443ea2009-05-11 11:56:30 -070010118 * Indicates whether this View is opaque. An opaque View guarantees that it will
10119 * draw all the pixels overlapping its bounds using a fully opaque color.
10120 *
10121 * Subclasses of View should override this method whenever possible to indicate
10122 * whether an instance is opaque. Opaque Views are treated in a special way by
10123 * the View hierarchy, possibly allowing it to perform optimizations during
10124 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -070010125 *
Romain Guy24443ea2009-05-11 11:56:30 -070010126 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -070010127 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010128 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -070010129 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -070010130 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Romain Guyf8773082012-07-12 18:01:00 -070010131 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1.0f) >= 1.0f);
Romain Guy8f1344f52009-05-15 16:03:59 -070010132 }
10133
Adam Powell20232d02010-12-08 21:08:53 -080010134 /**
10135 * @hide
10136 */
10137 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -070010138 // Opaque if:
10139 // - Has a background
10140 // - Background is opaque
10141 // - Doesn't have scrollbars or scrollbars are inside overlay
10142
Philip Milne6c8ea062012-04-03 17:38:43 -070010143 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Romain Guy8f1344f52009-05-15 16:03:59 -070010144 mPrivateFlags |= OPAQUE_BACKGROUND;
10145 } else {
10146 mPrivateFlags &= ~OPAQUE_BACKGROUND;
10147 }
10148
10149 final int flags = mViewFlags;
10150 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
10151 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
10152 mPrivateFlags |= OPAQUE_SCROLLBARS;
10153 } else {
10154 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
10155 }
10156 }
10157
10158 /**
10159 * @hide
10160 */
10161 protected boolean hasOpaqueScrollbars() {
10162 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -070010163 }
10164
10165 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010166 * @return A handler associated with the thread running the View. This
10167 * handler can be used to pump events in the UI events queue.
10168 */
10169 public Handler getHandler() {
10170 if (mAttachInfo != null) {
10171 return mAttachInfo.mHandler;
10172 }
10173 return null;
10174 }
10175
10176 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080010177 * Gets the view root associated with the View.
10178 * @return The view root, or null if none.
10179 * @hide
10180 */
10181 public ViewRootImpl getViewRootImpl() {
10182 if (mAttachInfo != null) {
10183 return mAttachInfo.mViewRootImpl;
10184 }
10185 return null;
10186 }
10187
10188 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010189 * <p>Causes the Runnable to be added to the message queue.
10190 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010191 *
Romain Guye63a4f32011-08-11 11:33:31 -070010192 * <p>This method can be invoked from outside of the UI thread
10193 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010194 *
10195 * @param action The Runnable that will be executed.
10196 *
10197 * @return Returns true if the Runnable was successfully placed in to the
10198 * message queue. Returns false on failure, usually because the
10199 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010200 *
10201 * @see #postDelayed
10202 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010203 */
10204 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010205 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010206 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010207 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010208 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010209 // Assume that post will succeed later
10210 ViewRootImpl.getRunQueue().post(action);
10211 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010212 }
10213
10214 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010215 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010216 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -070010217 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010218 *
Romain Guye63a4f32011-08-11 11:33:31 -070010219 * <p>This method can be invoked from outside of the UI thread
10220 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010221 *
10222 * @param action The Runnable that will be executed.
10223 * @param delayMillis The delay (in milliseconds) until the Runnable
10224 * will be executed.
10225 *
10226 * @return true if the Runnable was successfully placed in to the
10227 * message queue. Returns false on failure, usually because the
10228 * looper processing the message queue is exiting. Note that a
10229 * result of true does not mean the Runnable will be processed --
10230 * if the looper is quit before the delivery time of the message
10231 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010232 *
10233 * @see #post
10234 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010235 */
10236 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010237 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010238 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010239 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010240 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010241 // Assume that post will succeed later
10242 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10243 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010244 }
10245
10246 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010247 * <p>Causes the Runnable to execute on the next animation time step.
10248 * The runnable will be run on the user interface thread.</p>
10249 *
10250 * <p>This method can be invoked from outside of the UI thread
10251 * only when this View is attached to a window.</p>
10252 *
10253 * @param action The Runnable that will be executed.
10254 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010255 * @see #postOnAnimationDelayed
10256 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010257 */
10258 public void postOnAnimation(Runnable action) {
10259 final AttachInfo attachInfo = mAttachInfo;
10260 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010261 attachInfo.mViewRootImpl.mChoreographer.postCallback(
10262 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010263 } else {
10264 // Assume that post will succeed later
10265 ViewRootImpl.getRunQueue().post(action);
10266 }
10267 }
10268
10269 /**
10270 * <p>Causes the Runnable to execute on the next animation time step,
10271 * after the specified amount of time elapses.
10272 * The runnable will be run on the user interface thread.</p>
10273 *
10274 * <p>This method can be invoked from outside of the UI thread
10275 * only when this View is attached to a window.</p>
10276 *
10277 * @param action The Runnable that will be executed.
10278 * @param delayMillis The delay (in milliseconds) until the Runnable
10279 * will be executed.
10280 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010281 * @see #postOnAnimation
10282 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010283 */
10284 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
10285 final AttachInfo attachInfo = mAttachInfo;
10286 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010287 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
10288 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010289 } else {
10290 // Assume that post will succeed later
10291 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10292 }
10293 }
10294
10295 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010296 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010297 *
Romain Guye63a4f32011-08-11 11:33:31 -070010298 * <p>This method can be invoked from outside of the UI thread
10299 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010300 *
10301 * @param action The Runnable to remove from the message handling queue
10302 *
10303 * @return true if this view could ask the Handler to remove the Runnable,
10304 * false otherwise. When the returned value is true, the Runnable
10305 * may or may not have been actually removed from the message queue
10306 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010307 *
10308 * @see #post
10309 * @see #postDelayed
10310 * @see #postOnAnimation
10311 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010312 */
10313 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080010314 if (action != null) {
10315 final AttachInfo attachInfo = mAttachInfo;
10316 if (attachInfo != null) {
10317 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010318 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
10319 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -080010320 } else {
10321 // Assume that post will succeed later
10322 ViewRootImpl.getRunQueue().removeCallbacks(action);
10323 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010324 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010325 return true;
10326 }
10327
10328 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010329 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
10330 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010331 *
Romain Guye63a4f32011-08-11 11:33:31 -070010332 * <p>This method can be invoked from outside of the UI thread
10333 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010334 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010335 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010336 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010337 */
10338 public void postInvalidate() {
10339 postInvalidateDelayed(0);
10340 }
10341
10342 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010343 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10344 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010345 *
Romain Guye63a4f32011-08-11 11:33:31 -070010346 * <p>This method can be invoked from outside of the UI thread
10347 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010348 *
10349 * @param left The left coordinate of the rectangle to invalidate.
10350 * @param top The top coordinate of the rectangle to invalidate.
10351 * @param right The right coordinate of the rectangle to invalidate.
10352 * @param bottom The bottom coordinate of the rectangle to invalidate.
10353 *
10354 * @see #invalidate(int, int, int, int)
10355 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010356 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010357 */
10358 public void postInvalidate(int left, int top, int right, int bottom) {
10359 postInvalidateDelayed(0, left, top, right, bottom);
10360 }
10361
10362 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010363 * <p>Cause an invalidate to happen on a subsequent cycle through the event
10364 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010365 *
Romain Guye63a4f32011-08-11 11:33:31 -070010366 * <p>This method can be invoked from outside of the UI thread
10367 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010368 *
10369 * @param delayMilliseconds the duration in milliseconds to delay the
10370 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010371 *
10372 * @see #invalidate()
10373 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010374 */
10375 public void postInvalidateDelayed(long delayMilliseconds) {
10376 // We try only with the AttachInfo because there's no point in invalidating
10377 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010378 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010379 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010380 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010381 }
10382 }
10383
10384 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010385 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10386 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010387 *
Romain Guye63a4f32011-08-11 11:33:31 -070010388 * <p>This method can be invoked from outside of the UI thread
10389 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010390 *
10391 * @param delayMilliseconds the duration in milliseconds to delay the
10392 * invalidation by
10393 * @param left The left coordinate of the rectangle to invalidate.
10394 * @param top The top coordinate of the rectangle to invalidate.
10395 * @param right The right coordinate of the rectangle to invalidate.
10396 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010397 *
10398 * @see #invalidate(int, int, int, int)
10399 * @see #invalidate(Rect)
10400 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010401 */
10402 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
10403 int right, int bottom) {
10404
10405 // We try only with the AttachInfo because there's no point in invalidating
10406 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010407 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010408 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010409 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10410 info.target = this;
10411 info.left = left;
10412 info.top = top;
10413 info.right = right;
10414 info.bottom = bottom;
10415
Jeff Browna175a5b2012-02-15 19:18:31 -080010416 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010417 }
10418 }
10419
10420 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010421 * <p>Cause an invalidate to happen on the next animation time step, typically the
10422 * next display frame.</p>
10423 *
10424 * <p>This method can be invoked from outside of the UI thread
10425 * only when this View is attached to a window.</p>
10426 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010427 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010428 */
10429 public void postInvalidateOnAnimation() {
10430 // We try only with the AttachInfo because there's no point in invalidating
10431 // if we are not attached to our window
10432 final AttachInfo attachInfo = mAttachInfo;
10433 if (attachInfo != null) {
10434 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10435 }
10436 }
10437
10438 /**
10439 * <p>Cause an invalidate of the specified area to happen on the next animation
10440 * time step, typically the next display frame.</p>
10441 *
10442 * <p>This method can be invoked from outside of the UI thread
10443 * only when this View is attached to a window.</p>
10444 *
10445 * @param left The left coordinate of the rectangle to invalidate.
10446 * @param top The top coordinate of the rectangle to invalidate.
10447 * @param right The right coordinate of the rectangle to invalidate.
10448 * @param bottom The bottom coordinate of the rectangle to invalidate.
10449 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010450 * @see #invalidate(int, int, int, int)
10451 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010452 */
10453 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10454 // We try only with the AttachInfo because there's no point in invalidating
10455 // if we are not attached to our window
10456 final AttachInfo attachInfo = mAttachInfo;
10457 if (attachInfo != null) {
10458 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10459 info.target = this;
10460 info.left = left;
10461 info.top = top;
10462 info.right = right;
10463 info.bottom = bottom;
10464
10465 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10466 }
10467 }
10468
10469 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010470 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10471 * This event is sent at most once every
10472 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10473 */
10474 private void postSendViewScrolledAccessibilityEventCallback() {
10475 if (mSendViewScrolledAccessibilityEvent == null) {
10476 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10477 }
10478 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10479 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10480 postDelayed(mSendViewScrolledAccessibilityEvent,
10481 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10482 }
10483 }
10484
10485 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010486 * Called by a parent to request that a child update its values for mScrollX
10487 * and mScrollY if necessary. This will typically be done if the child is
10488 * animating a scroll using a {@link android.widget.Scroller Scroller}
10489 * object.
10490 */
10491 public void computeScroll() {
10492 }
10493
10494 /**
10495 * <p>Indicate whether the horizontal edges are faded when the view is
10496 * scrolled horizontally.</p>
10497 *
10498 * @return true if the horizontal edges should are faded on scroll, false
10499 * otherwise
10500 *
10501 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010502 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010503 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010504 */
10505 public boolean isHorizontalFadingEdgeEnabled() {
10506 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10507 }
10508
10509 /**
10510 * <p>Define whether the horizontal edges should be faded when this view
10511 * is scrolled horizontally.</p>
10512 *
10513 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10514 * be faded when the view is scrolled
10515 * horizontally
10516 *
10517 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010518 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010519 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010520 */
10521 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10522 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10523 if (horizontalFadingEdgeEnabled) {
10524 initScrollCache();
10525 }
10526
10527 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10528 }
10529 }
10530
10531 /**
10532 * <p>Indicate whether the vertical edges are faded when the view is
10533 * scrolled horizontally.</p>
10534 *
10535 * @return true if the vertical edges should are faded on scroll, false
10536 * otherwise
10537 *
10538 * @see #setVerticalFadingEdgeEnabled(boolean)
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 boolean isVerticalFadingEdgeEnabled() {
10543 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10544 }
10545
10546 /**
10547 * <p>Define whether the vertical edges should be faded when this view
10548 * is scrolled vertically.</p>
10549 *
10550 * @param verticalFadingEdgeEnabled true if the vertical edges should
10551 * be faded when the view is scrolled
10552 * vertically
10553 *
10554 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010555 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010556 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010557 */
10558 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10559 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10560 if (verticalFadingEdgeEnabled) {
10561 initScrollCache();
10562 }
10563
10564 mViewFlags ^= FADING_EDGE_VERTICAL;
10565 }
10566 }
10567
10568 /**
10569 * Returns the strength, or intensity, of the top faded edge. The strength is
10570 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10571 * returns 0.0 or 1.0 but no value in between.
10572 *
10573 * Subclasses should override this method to provide a smoother fade transition
10574 * when scrolling occurs.
10575 *
10576 * @return the intensity of the top fade as a float between 0.0f and 1.0f
10577 */
10578 protected float getTopFadingEdgeStrength() {
10579 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
10580 }
10581
10582 /**
10583 * Returns the strength, or intensity, of the bottom faded edge. The strength is
10584 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10585 * returns 0.0 or 1.0 but no value in between.
10586 *
10587 * Subclasses should override this method to provide a smoother fade transition
10588 * when scrolling occurs.
10589 *
10590 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
10591 */
10592 protected float getBottomFadingEdgeStrength() {
10593 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
10594 computeVerticalScrollRange() ? 1.0f : 0.0f;
10595 }
10596
10597 /**
10598 * Returns the strength, or intensity, of the left faded edge. The strength is
10599 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10600 * returns 0.0 or 1.0 but no value in between.
10601 *
10602 * Subclasses should override this method to provide a smoother fade transition
10603 * when scrolling occurs.
10604 *
10605 * @return the intensity of the left fade as a float between 0.0f and 1.0f
10606 */
10607 protected float getLeftFadingEdgeStrength() {
10608 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
10609 }
10610
10611 /**
10612 * Returns the strength, or intensity, of the right faded edge. The strength is
10613 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10614 * returns 0.0 or 1.0 but no value in between.
10615 *
10616 * Subclasses should override this method to provide a smoother fade transition
10617 * when scrolling occurs.
10618 *
10619 * @return the intensity of the right fade as a float between 0.0f and 1.0f
10620 */
10621 protected float getRightFadingEdgeStrength() {
10622 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
10623 computeHorizontalScrollRange() ? 1.0f : 0.0f;
10624 }
10625
10626 /**
10627 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
10628 * scrollbar is not drawn by default.</p>
10629 *
10630 * @return true if the horizontal scrollbar should be painted, false
10631 * otherwise
10632 *
10633 * @see #setHorizontalScrollBarEnabled(boolean)
10634 */
10635 public boolean isHorizontalScrollBarEnabled() {
10636 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10637 }
10638
10639 /**
10640 * <p>Define whether the horizontal scrollbar should be drawn or not. The
10641 * scrollbar is not drawn by default.</p>
10642 *
10643 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
10644 * be painted
10645 *
10646 * @see #isHorizontalScrollBarEnabled()
10647 */
10648 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
10649 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
10650 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010651 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010652 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010653 }
10654 }
10655
10656 /**
10657 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
10658 * scrollbar is not drawn by default.</p>
10659 *
10660 * @return true if the vertical scrollbar should be painted, false
10661 * otherwise
10662 *
10663 * @see #setVerticalScrollBarEnabled(boolean)
10664 */
10665 public boolean isVerticalScrollBarEnabled() {
10666 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
10667 }
10668
10669 /**
10670 * <p>Define whether the vertical scrollbar should be drawn or not. The
10671 * scrollbar is not drawn by default.</p>
10672 *
10673 * @param verticalScrollBarEnabled true if the vertical scrollbar should
10674 * be painted
10675 *
10676 * @see #isVerticalScrollBarEnabled()
10677 */
10678 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
10679 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
10680 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010681 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010682 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010683 }
10684 }
10685
Adam Powell20232d02010-12-08 21:08:53 -080010686 /**
10687 * @hide
10688 */
10689 protected void recomputePadding() {
10690 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010691 }
Joe Malin32736f02011-01-19 16:14:20 -080010692
Mike Cleronfe81d382009-09-28 14:22:16 -070010693 /**
10694 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080010695 *
Mike Cleronfe81d382009-09-28 14:22:16 -070010696 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080010697 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010698 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070010699 */
10700 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
10701 initScrollCache();
10702 final ScrollabilityCache scrollabilityCache = mScrollCache;
10703 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010704 if (fadeScrollbars) {
10705 scrollabilityCache.state = ScrollabilityCache.OFF;
10706 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070010707 scrollabilityCache.state = ScrollabilityCache.ON;
10708 }
10709 }
Joe Malin32736f02011-01-19 16:14:20 -080010710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010711 /**
Joe Malin32736f02011-01-19 16:14:20 -080010712 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010713 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080010714 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010715 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070010716 *
10717 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070010718 */
10719 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080010720 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010721 }
Joe Malin32736f02011-01-19 16:14:20 -080010722
Mike Cleron52f0a642009-09-28 18:21:37 -070010723 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070010724 *
10725 * Returns the delay before scrollbars fade.
10726 *
10727 * @return the delay before scrollbars fade
10728 *
10729 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10730 */
10731 public int getScrollBarDefaultDelayBeforeFade() {
10732 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
10733 mScrollCache.scrollBarDefaultDelayBeforeFade;
10734 }
10735
10736 /**
10737 * Define the delay before scrollbars fade.
10738 *
10739 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
10740 *
10741 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10742 */
10743 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
10744 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
10745 }
10746
10747 /**
10748 *
10749 * Returns the scrollbar fade duration.
10750 *
10751 * @return the scrollbar fade duration
10752 *
10753 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10754 */
10755 public int getScrollBarFadeDuration() {
10756 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
10757 mScrollCache.scrollBarFadeDuration;
10758 }
10759
10760 /**
10761 * Define the scrollbar fade duration.
10762 *
10763 * @param scrollBarFadeDuration - the scrollbar fade duration
10764 *
10765 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10766 */
10767 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
10768 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
10769 }
10770
10771 /**
10772 *
10773 * Returns the scrollbar size.
10774 *
10775 * @return the scrollbar size
10776 *
10777 * @attr ref android.R.styleable#View_scrollbarSize
10778 */
10779 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070010780 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070010781 mScrollCache.scrollBarSize;
10782 }
10783
10784 /**
10785 * Define the scrollbar size.
10786 *
10787 * @param scrollBarSize - the scrollbar size
10788 *
10789 * @attr ref android.R.styleable#View_scrollbarSize
10790 */
10791 public void setScrollBarSize(int scrollBarSize) {
10792 getScrollCache().scrollBarSize = scrollBarSize;
10793 }
10794
10795 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010796 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
10797 * inset. When inset, they add to the padding of the view. And the scrollbars
10798 * can be drawn inside the padding area or on the edge of the view. For example,
10799 * if a view has a background drawable and you want to draw the scrollbars
10800 * inside the padding specified by the drawable, you can use
10801 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
10802 * appear at the edge of the view, ignoring the padding, then you can use
10803 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
10804 * @param style the style of the scrollbars. Should be one of
10805 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
10806 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
10807 * @see #SCROLLBARS_INSIDE_OVERLAY
10808 * @see #SCROLLBARS_INSIDE_INSET
10809 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10810 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010811 *
10812 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010813 */
10814 public void setScrollBarStyle(int style) {
10815 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
10816 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070010817 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010818 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010819 }
10820 }
10821
10822 /**
10823 * <p>Returns the current scrollbar style.</p>
10824 * @return the current scrollbar style
10825 * @see #SCROLLBARS_INSIDE_OVERLAY
10826 * @see #SCROLLBARS_INSIDE_INSET
10827 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10828 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010829 *
10830 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010831 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070010832 @ViewDebug.ExportedProperty(mapping = {
10833 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
10834 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
10835 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
10836 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
10837 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010838 public int getScrollBarStyle() {
10839 return mViewFlags & SCROLLBARS_STYLE_MASK;
10840 }
10841
10842 /**
10843 * <p>Compute the horizontal range that the horizontal scrollbar
10844 * represents.</p>
10845 *
10846 * <p>The range is expressed in arbitrary units that must be the same as the
10847 * units used by {@link #computeHorizontalScrollExtent()} and
10848 * {@link #computeHorizontalScrollOffset()}.</p>
10849 *
10850 * <p>The default range is the drawing width of this view.</p>
10851 *
10852 * @return the total horizontal range represented by the horizontal
10853 * scrollbar
10854 *
10855 * @see #computeHorizontalScrollExtent()
10856 * @see #computeHorizontalScrollOffset()
10857 * @see android.widget.ScrollBarDrawable
10858 */
10859 protected int computeHorizontalScrollRange() {
10860 return getWidth();
10861 }
10862
10863 /**
10864 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
10865 * within the horizontal range. This value is used to compute the position
10866 * of the thumb within the scrollbar's track.</p>
10867 *
10868 * <p>The range is expressed in arbitrary units that must be the same as the
10869 * units used by {@link #computeHorizontalScrollRange()} and
10870 * {@link #computeHorizontalScrollExtent()}.</p>
10871 *
10872 * <p>The default offset is the scroll offset of this view.</p>
10873 *
10874 * @return the horizontal offset of the scrollbar's thumb
10875 *
10876 * @see #computeHorizontalScrollRange()
10877 * @see #computeHorizontalScrollExtent()
10878 * @see android.widget.ScrollBarDrawable
10879 */
10880 protected int computeHorizontalScrollOffset() {
10881 return mScrollX;
10882 }
10883
10884 /**
10885 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
10886 * within the horizontal range. This value is used to compute the length
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 #computeHorizontalScrollOffset()}.</p>
10892 *
10893 * <p>The default extent is the drawing width of this view.</p>
10894 *
10895 * @return the horizontal extent of the scrollbar's thumb
10896 *
10897 * @see #computeHorizontalScrollRange()
10898 * @see #computeHorizontalScrollOffset()
10899 * @see android.widget.ScrollBarDrawable
10900 */
10901 protected int computeHorizontalScrollExtent() {
10902 return getWidth();
10903 }
10904
10905 /**
10906 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
10907 *
10908 * <p>The range is expressed in arbitrary units that must be the same as the
10909 * units used by {@link #computeVerticalScrollExtent()} and
10910 * {@link #computeVerticalScrollOffset()}.</p>
10911 *
10912 * @return the total vertical range represented by the vertical scrollbar
10913 *
10914 * <p>The default range is the drawing height of this view.</p>
10915 *
10916 * @see #computeVerticalScrollExtent()
10917 * @see #computeVerticalScrollOffset()
10918 * @see android.widget.ScrollBarDrawable
10919 */
10920 protected int computeVerticalScrollRange() {
10921 return getHeight();
10922 }
10923
10924 /**
10925 * <p>Compute the vertical offset of the vertical scrollbar's thumb
10926 * within the horizontal range. This value is used to compute the position
10927 * of the thumb within the scrollbar's track.</p>
10928 *
10929 * <p>The range is expressed in arbitrary units that must be the same as the
10930 * units used by {@link #computeVerticalScrollRange()} and
10931 * {@link #computeVerticalScrollExtent()}.</p>
10932 *
10933 * <p>The default offset is the scroll offset of this view.</p>
10934 *
10935 * @return the vertical offset of the scrollbar's thumb
10936 *
10937 * @see #computeVerticalScrollRange()
10938 * @see #computeVerticalScrollExtent()
10939 * @see android.widget.ScrollBarDrawable
10940 */
10941 protected int computeVerticalScrollOffset() {
10942 return mScrollY;
10943 }
10944
10945 /**
10946 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
10947 * within the vertical range. This value is used to compute the length
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
Gilles Debunne52964242010-02-24 11:05:19 -080010951 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010952 * {@link #computeVerticalScrollOffset()}.</p>
10953 *
10954 * <p>The default extent is the drawing height of this view.</p>
10955 *
10956 * @return the vertical extent of the scrollbar's thumb
10957 *
10958 * @see #computeVerticalScrollRange()
10959 * @see #computeVerticalScrollOffset()
10960 * @see android.widget.ScrollBarDrawable
10961 */
10962 protected int computeVerticalScrollExtent() {
10963 return getHeight();
10964 }
10965
10966 /**
Adam Powell69159442011-06-13 17:53:06 -070010967 * Check if this view can be scrolled horizontally in a certain direction.
10968 *
10969 * @param direction Negative to check scrolling left, positive to check scrolling right.
10970 * @return true if this view can be scrolled in the specified direction, false otherwise.
10971 */
10972 public boolean canScrollHorizontally(int direction) {
10973 final int offset = computeHorizontalScrollOffset();
10974 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
10975 if (range == 0) return false;
10976 if (direction < 0) {
10977 return offset > 0;
10978 } else {
10979 return offset < range - 1;
10980 }
10981 }
10982
10983 /**
10984 * Check if this view can be scrolled vertically in a certain direction.
10985 *
10986 * @param direction Negative to check scrolling up, positive to check scrolling down.
10987 * @return true if this view can be scrolled in the specified direction, false otherwise.
10988 */
10989 public boolean canScrollVertically(int direction) {
10990 final int offset = computeVerticalScrollOffset();
10991 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
10992 if (range == 0) return false;
10993 if (direction < 0) {
10994 return offset > 0;
10995 } else {
10996 return offset < range - 1;
10997 }
10998 }
10999
11000 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011001 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
11002 * scrollbars are painted only if they have been awakened first.</p>
11003 *
11004 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080011005 *
Mike Cleronf116bf82009-09-27 19:14:12 -070011006 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011007 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080011008 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011009 // scrollbars are drawn only when the animation is running
11010 final ScrollabilityCache cache = mScrollCache;
11011 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080011012
Mike Cleronf116bf82009-09-27 19:14:12 -070011013 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080011014
Mike Cleronf116bf82009-09-27 19:14:12 -070011015 if (state == ScrollabilityCache.OFF) {
11016 return;
11017 }
Joe Malin32736f02011-01-19 16:14:20 -080011018
Mike Cleronf116bf82009-09-27 19:14:12 -070011019 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080011020
Mike Cleronf116bf82009-09-27 19:14:12 -070011021 if (state == ScrollabilityCache.FADING) {
11022 // We're fading -- get our fade interpolation
11023 if (cache.interpolatorValues == null) {
11024 cache.interpolatorValues = new float[1];
11025 }
Joe Malin32736f02011-01-19 16:14:20 -080011026
Mike Cleronf116bf82009-09-27 19:14:12 -070011027 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080011028
Mike Cleronf116bf82009-09-27 19:14:12 -070011029 // Stops the animation if we're done
11030 if (cache.scrollBarInterpolator.timeToValues(values) ==
11031 Interpolator.Result.FREEZE_END) {
11032 cache.state = ScrollabilityCache.OFF;
11033 } else {
11034 cache.scrollBar.setAlpha(Math.round(values[0]));
11035 }
Joe Malin32736f02011-01-19 16:14:20 -080011036
11037 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070011038 // drawing. We only want this when we're fading so that
11039 // we prevent excessive redraws
11040 invalidate = true;
11041 } else {
11042 // We're just on -- but we may have been fading before so
11043 // reset alpha
11044 cache.scrollBar.setAlpha(255);
11045 }
11046
Joe Malin32736f02011-01-19 16:14:20 -080011047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011048 final int viewFlags = mViewFlags;
11049
11050 final boolean drawHorizontalScrollBar =
11051 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
11052 final boolean drawVerticalScrollBar =
11053 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
11054 && !isVerticalScrollBarHidden();
11055
11056 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
11057 final int width = mRight - mLeft;
11058 final int height = mBottom - mTop;
11059
11060 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011061
Mike Reede8853fc2009-09-04 14:01:48 -040011062 final int scrollX = mScrollX;
11063 final int scrollY = mScrollY;
11064 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
11065
Mike Cleronf116bf82009-09-27 19:14:12 -070011066 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080011067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011068 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011069 int size = scrollBar.getSize(false);
11070 if (size <= 0) {
11071 size = cache.scrollBarSize;
11072 }
11073
Mike Cleronf116bf82009-09-27 19:14:12 -070011074 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040011075 computeHorizontalScrollOffset(),
11076 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040011077 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070011078 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080011079 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070011080 left = scrollX + (mPaddingLeft & inside);
11081 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
11082 bottom = top + size;
11083 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
11084 if (invalidate) {
11085 invalidate(left, top, right, bottom);
11086 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011087 }
11088
11089 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011090 int size = scrollBar.getSize(true);
11091 if (size <= 0) {
11092 size = cache.scrollBarSize;
11093 }
11094
Mike Reede8853fc2009-09-04 14:01:48 -040011095 scrollBar.setParameters(computeVerticalScrollRange(),
11096 computeVerticalScrollOffset(),
11097 computeVerticalScrollExtent(), true);
Fabrice Di Meglioc91b6ca2012-06-22 14:51:15 -070011098 int verticalScrollbarPosition = mVerticalScrollbarPosition;
11099 if (verticalScrollbarPosition == SCROLLBAR_POSITION_DEFAULT) {
11100 verticalScrollbarPosition = isLayoutRtl() ?
11101 SCROLLBAR_POSITION_LEFT : SCROLLBAR_POSITION_RIGHT;
11102 }
11103 switch (verticalScrollbarPosition) {
Adam Powell20232d02010-12-08 21:08:53 -080011104 default:
Adam Powell20232d02010-12-08 21:08:53 -080011105 case SCROLLBAR_POSITION_RIGHT:
11106 left = scrollX + width - size - (mUserPaddingRight & inside);
11107 break;
11108 case SCROLLBAR_POSITION_LEFT:
11109 left = scrollX + (mUserPaddingLeft & inside);
11110 break;
11111 }
Mike Cleronf116bf82009-09-27 19:14:12 -070011112 top = scrollY + (mPaddingTop & inside);
11113 right = left + size;
11114 bottom = scrollY + height - (mUserPaddingBottom & inside);
11115 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
11116 if (invalidate) {
11117 invalidate(left, top, right, bottom);
11118 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011119 }
11120 }
11121 }
11122 }
Romain Guy8506ab42009-06-11 17:35:47 -070011123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011124 /**
Romain Guy8506ab42009-06-11 17:35:47 -070011125 * 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 -080011126 * FastScroller is visible.
11127 * @return whether to temporarily hide the vertical scrollbar
11128 * @hide
11129 */
11130 protected boolean isVerticalScrollBarHidden() {
11131 return false;
11132 }
11133
11134 /**
11135 * <p>Draw the horizontal scrollbar if
11136 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
11137 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011138 * @param canvas the canvas on which to draw the scrollbar
11139 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011140 *
11141 * @see #isHorizontalScrollBarEnabled()
11142 * @see #computeHorizontalScrollRange()
11143 * @see #computeHorizontalScrollExtent()
11144 * @see #computeHorizontalScrollOffset()
11145 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070011146 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011147 */
Romain Guy8fb95422010-08-17 18:38:51 -070011148 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
11149 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011150 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011151 scrollBar.draw(canvas);
11152 }
Mike Reede8853fc2009-09-04 14:01:48 -040011153
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011154 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011155 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
11156 * returns true.</p>
11157 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011158 * @param canvas the canvas on which to draw the scrollbar
11159 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011160 *
11161 * @see #isVerticalScrollBarEnabled()
11162 * @see #computeVerticalScrollRange()
11163 * @see #computeVerticalScrollExtent()
11164 * @see #computeVerticalScrollOffset()
11165 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040011166 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011167 */
Romain Guy8fb95422010-08-17 18:38:51 -070011168 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
11169 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040011170 scrollBar.setBounds(l, t, r, b);
11171 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011172 }
11173
11174 /**
11175 * Implement this to do your drawing.
11176 *
11177 * @param canvas the canvas on which the background will be drawn
11178 */
11179 protected void onDraw(Canvas canvas) {
11180 }
11181
11182 /*
11183 * Caller is responsible for calling requestLayout if necessary.
11184 * (This allows addViewInLayout to not request a new layout.)
11185 */
11186 void assignParent(ViewParent parent) {
11187 if (mParent == null) {
11188 mParent = parent;
11189 } else if (parent == null) {
11190 mParent = null;
11191 } else {
11192 throw new RuntimeException("view " + this + " being added, but"
11193 + " it already has a parent");
11194 }
11195 }
11196
11197 /**
11198 * This is called when the view is attached to a window. At this point it
11199 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011200 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
11201 * however it may be called any time before the first onDraw -- including
11202 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011203 *
11204 * @see #onDetachedFromWindow()
11205 */
11206 protected void onAttachedToWindow() {
11207 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
11208 mParent.requestTransparentRegion(this);
11209 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011210
Adam Powell8568c3a2010-04-19 14:26:11 -070011211 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
11212 initialAwakenScrollBars();
11213 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
11214 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011215
Chet Haasea9b61ac2010-12-20 07:40:25 -080011216 jumpDrawablesToCurrentState();
Romain Guy2a0f2282012-05-08 14:43:12 -070011217
Fabrice Di Meglioa6461452011-08-19 15:42:04 -070011218 // Order is important here: LayoutDirection MUST be resolved before Padding
11219 // and TextDirection
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011220 resolveLayoutDirection();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011221 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011222 resolveTextDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011223 resolveTextAlignment();
Romain Guy2a0f2282012-05-08 14:43:12 -070011224
Svetoslav Ganov42138042012-03-20 11:51:39 -070011225 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070011226 if (isFocused()) {
11227 InputMethodManager imm = InputMethodManager.peekInstance();
11228 imm.focusIn(this);
11229 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011230
11231 if (mAttachInfo != null && mDisplayList != null) {
11232 mAttachInfo.mViewRootImpl.dequeueDisplayList(mDisplayList);
11233 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011234 }
Cibu Johny86666632010-02-22 13:01:02 -080011235
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011236 /**
Romain Guybb9908b2012-03-08 11:14:07 -080011237 * @see #onScreenStateChanged(int)
11238 */
11239 void dispatchScreenStateChanged(int screenState) {
11240 onScreenStateChanged(screenState);
11241 }
11242
11243 /**
11244 * This method is called whenever the state of the screen this view is
11245 * attached to changes. A state change will usually occurs when the screen
11246 * turns on or off (whether it happens automatically or the user does it
11247 * manually.)
11248 *
11249 * @param screenState The new state of the screen. Can be either
11250 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
11251 */
11252 public void onScreenStateChanged(int screenState) {
11253 }
11254
11255 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011256 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
11257 */
11258 private boolean hasRtlSupport() {
11259 return mContext.getApplicationInfo().hasRtlSupport();
11260 }
11261
11262 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011263 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
11264 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011265 * Will call {@link View#onResolvedLayoutDirectionChanged} when resolution is done.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011266 */
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011267 public void resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011268 // Clear any previous layout direction resolution
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070011269 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011270
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011271 if (hasRtlSupport()) {
11272 // Set resolved depending on layout direction
11273 switch (getLayoutDirection()) {
11274 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliob93911f2012-06-26 19:43:15 -070011275 // We cannot resolve yet. LTR is by default and let the resolution happen again
11276 // later to get the correct resolved value
11277 if (!canResolveLayoutDirection()) return;
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011278
Fabrice Di Megliob93911f2012-06-26 19:43:15 -070011279 ViewGroup viewGroup = ((ViewGroup) mParent);
11280
11281 // We cannot resolve yet on the parent too. LTR is by default and let the
11282 // resolution happen again later
11283 if (!viewGroup.canResolveLayoutDirection()) return;
11284
11285 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
11286 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011287 }
11288 break;
11289 case LAYOUT_DIRECTION_RTL:
11290 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11291 break;
11292 case LAYOUT_DIRECTION_LOCALE:
11293 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011294 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11295 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011296 break;
11297 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011298 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011299 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011300 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011301
11302 // Set to resolved
11303 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011304 onResolvedLayoutDirectionChanged();
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080011305 // Resolve padding
11306 resolvePadding();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011307 }
11308
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011309 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011310 * Called when layout direction has been resolved.
11311 *
11312 * The default implementation does nothing.
11313 */
11314 public void onResolvedLayoutDirectionChanged() {
11315 }
11316
11317 /**
11318 * Resolve padding depending on layout direction.
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011319 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011320 public void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011321 // If the user specified the absolute padding (either with android:padding or
11322 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
11323 // use the default padding or the padding from the background drawable
11324 // (stored at this point in mPadding*)
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011325 int resolvedLayoutDirection = getResolvedLayoutDirection();
11326 switch (resolvedLayoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011327 case LAYOUT_DIRECTION_RTL:
11328 // Start user padding override Right user padding. Otherwise, if Right user
11329 // padding is not defined, use the default Right padding. If Right user padding
11330 // is defined, just use it.
11331 if (mUserPaddingStart >= 0) {
11332 mUserPaddingRight = mUserPaddingStart;
11333 } else if (mUserPaddingRight < 0) {
11334 mUserPaddingRight = mPaddingRight;
11335 }
11336 if (mUserPaddingEnd >= 0) {
11337 mUserPaddingLeft = mUserPaddingEnd;
11338 } else if (mUserPaddingLeft < 0) {
11339 mUserPaddingLeft = mPaddingLeft;
11340 }
11341 break;
11342 case LAYOUT_DIRECTION_LTR:
11343 default:
11344 // Start user padding override Left user padding. Otherwise, if Left user
11345 // padding is not defined, use the default left padding. If Left user padding
11346 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070011347 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011348 mUserPaddingLeft = mUserPaddingStart;
11349 } else if (mUserPaddingLeft < 0) {
11350 mUserPaddingLeft = mPaddingLeft;
11351 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070011352 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011353 mUserPaddingRight = mUserPaddingEnd;
11354 } else if (mUserPaddingRight < 0) {
11355 mUserPaddingRight = mPaddingRight;
11356 }
11357 }
11358
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011359 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
11360
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080011361 if(isPaddingRelative()) {
11362 setPaddingRelative(mUserPaddingStart, mPaddingTop, mUserPaddingEnd, mUserPaddingBottom);
11363 } else {
11364 recomputePadding();
11365 }
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011366 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011367 }
11368
11369 /**
11370 * Resolve padding depending on the layout direction. Subclasses that care about
11371 * padding resolution should override this method. The default implementation does
11372 * nothing.
11373 *
11374 * @param layoutDirection the direction of the layout
11375 *
Fabrice Di Meglioe8dc07d2012-03-09 17:10:19 -080011376 * @see {@link #LAYOUT_DIRECTION_LTR}
11377 * @see {@link #LAYOUT_DIRECTION_RTL}
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011378 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011379 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011380 }
11381
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011382 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011383 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011384 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011385 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011386 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011387 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011388 switch (getLayoutDirection()) {
11389 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011390 return (mParent != null) && (mParent instanceof ViewGroup);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011391 default:
11392 return true;
11393 }
11394 }
11395
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011396 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011397 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
11398 * when reset is done.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011399 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011400 public void resetResolvedLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070011401 // Reset the current resolved bits
11402 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011403 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080011404 // Reset also the text direction
11405 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011406 }
11407
11408 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011409 * Called during reset of resolved layout direction.
11410 *
11411 * Subclasses need to override this method to clear cached information that depends on the
11412 * resolved layout direction, or to inform child views that inherit their layout direction.
11413 *
11414 * The default implementation does nothing.
11415 */
11416 public void onResolvedLayoutDirectionReset() {
11417 }
11418
11419 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011420 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011421 *
11422 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011423 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011424 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011425 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -080011426 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011427 }
11428
11429 /**
11430 * This is called when the view is detached from a window. At this point it
11431 * no longer has a surface for drawing.
11432 *
11433 * @see #onAttachedToWindow()
11434 */
11435 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -080011436 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080011437
Romain Guya440b002010-02-24 15:57:54 -080011438 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050011439 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080011440 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070011441 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080011442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011443 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011444
Romain Guya998dff2012-03-23 18:58:36 -070011445 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011446
11447 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011448 if (mDisplayList != null) {
Romain Guy2a0f2282012-05-08 14:43:12 -070011449 mAttachInfo.mViewRootImpl.enqueueDisplayList(mDisplayList);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011450 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011451 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011452 } else {
Romain Guy38c2ece2012-05-24 14:20:56 -070011453 // Should never happen
11454 clearDisplayList();
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011455 }
11456
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011457 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011458
11459 resetResolvedLayoutDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011460 resetResolvedTextAlignment();
Svetoslav Ganov42138042012-03-20 11:51:39 -070011461 resetAccessibilityStateChanged();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011462 }
11463
11464 /**
11465 * @return The number of times this view has been attached to a window
11466 */
11467 protected int getWindowAttachCount() {
11468 return mWindowAttachCount;
11469 }
11470
11471 /**
11472 * Retrieve a unique token identifying the window this view is attached to.
11473 * @return Return the window's token for use in
11474 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11475 */
11476 public IBinder getWindowToken() {
11477 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11478 }
11479
11480 /**
11481 * Retrieve a unique token identifying the top-level "real" window of
11482 * the window that this view is attached to. That is, this is like
11483 * {@link #getWindowToken}, except if the window this view in is a panel
11484 * window (attached to another containing window), then the token of
11485 * the containing window is returned instead.
11486 *
11487 * @return Returns the associated window token, either
11488 * {@link #getWindowToken()} or the containing window's token.
11489 */
11490 public IBinder getApplicationWindowToken() {
11491 AttachInfo ai = mAttachInfo;
11492 if (ai != null) {
11493 IBinder appWindowToken = ai.mPanelParentWindowToken;
11494 if (appWindowToken == null) {
11495 appWindowToken = ai.mWindowToken;
11496 }
11497 return appWindowToken;
11498 }
11499 return null;
11500 }
11501
11502 /**
11503 * Retrieve private session object this view hierarchy is using to
11504 * communicate with the window manager.
11505 * @return the session object to communicate with the window manager
11506 */
11507 /*package*/ IWindowSession getWindowSession() {
11508 return mAttachInfo != null ? mAttachInfo.mSession : null;
11509 }
11510
11511 /**
11512 * @param info the {@link android.view.View.AttachInfo} to associated with
11513 * this view
11514 */
11515 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
11516 //System.out.println("Attached! " + this);
11517 mAttachInfo = info;
11518 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011519 // We will need to evaluate the drawable state at least once.
11520 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011521 if (mFloatingTreeObserver != null) {
11522 info.mTreeObserver.merge(mFloatingTreeObserver);
11523 mFloatingTreeObserver = null;
11524 }
11525 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
11526 mAttachInfo.mScrollContainers.add(this);
11527 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
11528 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070011529 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011530 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080011531
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011532 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011533 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011534 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011535 if (listeners != null && listeners.size() > 0) {
11536 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11537 // perform the dispatching. The iterator is a safe guard against listeners that
11538 // could mutate the list by calling the various add/remove methods. This prevents
11539 // the array from being modified while we iterate it.
11540 for (OnAttachStateChangeListener listener : listeners) {
11541 listener.onViewAttachedToWindow(this);
11542 }
11543 }
11544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011545 int vis = info.mWindowVisibility;
11546 if (vis != GONE) {
11547 onWindowVisibilityChanged(vis);
11548 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011549 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
11550 // If nobody has evaluated the drawable state yet, then do it now.
11551 refreshDrawableState();
11552 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011553 }
11554
11555 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011556 AttachInfo info = mAttachInfo;
11557 if (info != null) {
11558 int vis = info.mWindowVisibility;
11559 if (vis != GONE) {
11560 onWindowVisibilityChanged(GONE);
11561 }
11562 }
11563
11564 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080011565
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011566 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011567 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011568 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011569 if (listeners != null && listeners.size() > 0) {
11570 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11571 // perform the dispatching. The iterator is a safe guard against listeners that
11572 // could mutate the list by calling the various add/remove methods. This prevents
11573 // the array from being modified while we iterate it.
11574 for (OnAttachStateChangeListener listener : listeners) {
11575 listener.onViewDetachedFromWindow(this);
11576 }
11577 }
11578
Romain Guy01d5edc2011-01-28 11:28:53 -080011579 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011580 mAttachInfo.mScrollContainers.remove(this);
11581 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
11582 }
Romain Guy01d5edc2011-01-28 11:28:53 -080011583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011584 mAttachInfo = null;
11585 }
11586
11587 /**
11588 * Store this view hierarchy's frozen state into the given container.
11589 *
11590 * @param container The SparseArray in which to save the view's state.
11591 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011592 * @see #restoreHierarchyState(android.util.SparseArray)
11593 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11594 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011595 */
11596 public void saveHierarchyState(SparseArray<Parcelable> container) {
11597 dispatchSaveInstanceState(container);
11598 }
11599
11600 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011601 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
11602 * this view and its children. May be overridden to modify how freezing happens to a
11603 * 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 -080011604 *
11605 * @param container The SparseArray in which to save the view's state.
11606 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011607 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11608 * @see #saveHierarchyState(android.util.SparseArray)
11609 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011610 */
11611 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
11612 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
11613 mPrivateFlags &= ~SAVE_STATE_CALLED;
11614 Parcelable state = onSaveInstanceState();
11615 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11616 throw new IllegalStateException(
11617 "Derived class did not call super.onSaveInstanceState()");
11618 }
11619 if (state != null) {
11620 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
11621 // + ": " + state);
11622 container.put(mID, state);
11623 }
11624 }
11625 }
11626
11627 /**
11628 * Hook allowing a view to generate a representation of its internal state
11629 * that can later be used to create a new instance with that same state.
11630 * This state should only contain information that is not persistent or can
11631 * not be reconstructed later. For example, you will never store your
11632 * current position on screen because that will be computed again when a
11633 * new instance of the view is placed in its view hierarchy.
11634 * <p>
11635 * Some examples of things you may store here: the current cursor position
11636 * in a text view (but usually not the text itself since that is stored in a
11637 * content provider or other persistent storage), the currently selected
11638 * item in a list view.
11639 *
11640 * @return Returns a Parcelable object containing the view's current dynamic
11641 * state, or null if there is nothing interesting to save. The
11642 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070011643 * @see #onRestoreInstanceState(android.os.Parcelable)
11644 * @see #saveHierarchyState(android.util.SparseArray)
11645 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011646 * @see #setSaveEnabled(boolean)
11647 */
11648 protected Parcelable onSaveInstanceState() {
11649 mPrivateFlags |= SAVE_STATE_CALLED;
11650 return BaseSavedState.EMPTY_STATE;
11651 }
11652
11653 /**
11654 * Restore this view hierarchy's frozen state from the given container.
11655 *
11656 * @param container The SparseArray which holds previously frozen states.
11657 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011658 * @see #saveHierarchyState(android.util.SparseArray)
11659 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11660 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011661 */
11662 public void restoreHierarchyState(SparseArray<Parcelable> container) {
11663 dispatchRestoreInstanceState(container);
11664 }
11665
11666 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011667 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
11668 * state for this view and its children. May be overridden to modify how restoring
11669 * happens to a view's children; for example, some views may want to not store state
11670 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011671 *
11672 * @param container The SparseArray which holds previously saved state.
11673 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011674 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11675 * @see #restoreHierarchyState(android.util.SparseArray)
11676 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011677 */
11678 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
11679 if (mID != NO_ID) {
11680 Parcelable state = container.get(mID);
11681 if (state != null) {
11682 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
11683 // + ": " + state);
11684 mPrivateFlags &= ~SAVE_STATE_CALLED;
11685 onRestoreInstanceState(state);
11686 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11687 throw new IllegalStateException(
11688 "Derived class did not call super.onRestoreInstanceState()");
11689 }
11690 }
11691 }
11692 }
11693
11694 /**
11695 * Hook allowing a view to re-apply a representation of its internal state that had previously
11696 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
11697 * null state.
11698 *
11699 * @param state The frozen state that had previously been returned by
11700 * {@link #onSaveInstanceState}.
11701 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011702 * @see #onSaveInstanceState()
11703 * @see #restoreHierarchyState(android.util.SparseArray)
11704 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011705 */
11706 protected void onRestoreInstanceState(Parcelable state) {
11707 mPrivateFlags |= SAVE_STATE_CALLED;
11708 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080011709 throw new IllegalArgumentException("Wrong state class, expecting View State but "
11710 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080011711 + "when two views of different type have the same id in the same hierarchy. "
11712 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080011713 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011714 }
11715 }
11716
11717 /**
11718 * <p>Return the time at which the drawing of the view hierarchy started.</p>
11719 *
11720 * @return the drawing start time in milliseconds
11721 */
11722 public long getDrawingTime() {
11723 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
11724 }
11725
11726 /**
11727 * <p>Enables or disables the duplication of the parent's state into this view. When
11728 * duplication is enabled, this view gets its drawable state from its parent rather
11729 * than from its own internal properties.</p>
11730 *
11731 * <p>Note: in the current implementation, setting this property to true after the
11732 * view was added to a ViewGroup might have no effect at all. This property should
11733 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
11734 *
11735 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
11736 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011737 *
Gilles Debunnefb817032011-01-13 13:52:49 -080011738 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
11739 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011740 *
11741 * @param enabled True to enable duplication of the parent's drawable state, false
11742 * to disable it.
11743 *
11744 * @see #getDrawableState()
11745 * @see #isDuplicateParentStateEnabled()
11746 */
11747 public void setDuplicateParentStateEnabled(boolean enabled) {
11748 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
11749 }
11750
11751 /**
11752 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
11753 *
11754 * @return True if this view's drawable state is duplicated from the parent,
11755 * false otherwise
11756 *
11757 * @see #getDrawableState()
11758 * @see #setDuplicateParentStateEnabled(boolean)
11759 */
11760 public boolean isDuplicateParentStateEnabled() {
11761 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
11762 }
11763
11764 /**
Romain Guy171c5922011-01-06 10:04:23 -080011765 * <p>Specifies the type of layer backing this view. The layer can be
11766 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
11767 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011768 *
Romain Guy171c5922011-01-06 10:04:23 -080011769 * <p>A layer is associated with an optional {@link android.graphics.Paint}
11770 * instance that controls how the layer is composed on screen. The following
11771 * properties of the paint are taken into account when composing the layer:</p>
11772 * <ul>
11773 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
11774 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
11775 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
11776 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080011777 *
Romain Guy171c5922011-01-06 10:04:23 -080011778 * <p>If this view has an alpha value set to < 1.0 by calling
11779 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
11780 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
11781 * equivalent to setting a hardware layer on this view and providing a paint with
11782 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080011783 *
Romain Guy171c5922011-01-06 10:04:23 -080011784 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
11785 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
11786 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011787 *
Romain Guy171c5922011-01-06 10:04:23 -080011788 * @param layerType The ype of layer to use with this view, must be one of
11789 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11790 * {@link #LAYER_TYPE_HARDWARE}
11791 * @param paint The paint used to compose the layer. This argument is optional
11792 * and can be null. It is ignored when the layer type is
11793 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080011794 *
11795 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080011796 * @see #LAYER_TYPE_NONE
11797 * @see #LAYER_TYPE_SOFTWARE
11798 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080011799 * @see #setAlpha(float)
11800 *
Romain Guy171c5922011-01-06 10:04:23 -080011801 * @attr ref android.R.styleable#View_layerType
11802 */
11803 public void setLayerType(int layerType, Paint paint) {
11804 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080011805 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080011806 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
11807 }
Chet Haasedaf98e92011-01-10 14:10:36 -080011808
Romain Guyd6cd5722011-01-17 14:42:41 -080011809 if (layerType == mLayerType) {
11810 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
11811 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011812 invalidateParentCaches();
11813 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080011814 }
11815 return;
11816 }
Romain Guy171c5922011-01-06 10:04:23 -080011817
11818 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080011819 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070011820 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070011821 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011822 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080011823 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070011824 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011825 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080011826 default:
11827 break;
Romain Guy171c5922011-01-06 10:04:23 -080011828 }
11829
11830 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080011831 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
11832 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
11833 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080011834
Romain Guy0fd89bf2011-01-26 15:41:30 -080011835 invalidateParentCaches();
11836 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080011837 }
11838
11839 /**
Romain Guy59c7f802011-09-29 17:21:45 -070011840 * Indicates whether this view has a static layer. A view with layer type
11841 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
11842 * dynamic.
11843 */
11844 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080011845 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070011846 }
11847
11848 /**
Romain Guy171c5922011-01-06 10:04:23 -080011849 * Indicates what type of layer is currently associated with this view. By default
11850 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
11851 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
11852 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080011853 *
Romain Guy171c5922011-01-06 10:04:23 -080011854 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11855 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080011856 *
11857 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070011858 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080011859 * @see #LAYER_TYPE_NONE
11860 * @see #LAYER_TYPE_SOFTWARE
11861 * @see #LAYER_TYPE_HARDWARE
11862 */
11863 public int getLayerType() {
11864 return mLayerType;
11865 }
Joe Malin32736f02011-01-19 16:14:20 -080011866
Romain Guy6c319ca2011-01-11 14:29:25 -080011867 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080011868 * Forces this view's layer to be created and this view to be rendered
11869 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
11870 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070011871 *
Romain Guyf1ae1062011-03-02 18:16:04 -080011872 * This method can for instance be used to render a view into its layer before
11873 * starting an animation. If this view is complex, rendering into the layer
11874 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070011875 *
Romain Guyf1ae1062011-03-02 18:16:04 -080011876 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070011877 *
11878 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080011879 */
11880 public void buildLayer() {
11881 if (mLayerType == LAYER_TYPE_NONE) return;
11882
11883 if (mAttachInfo == null) {
11884 throw new IllegalStateException("This view must be attached to a window first");
11885 }
11886
11887 switch (mLayerType) {
11888 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080011889 if (mAttachInfo.mHardwareRenderer != null &&
11890 mAttachInfo.mHardwareRenderer.isEnabled() &&
11891 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080011892 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080011893 }
Romain Guyf1ae1062011-03-02 18:16:04 -080011894 break;
11895 case LAYER_TYPE_SOFTWARE:
11896 buildDrawingCache(true);
11897 break;
11898 }
11899 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011900
Romain Guy9c4b79a2011-11-10 19:23:58 -080011901 // Make sure the HardwareRenderer.validate() was invoked before calling this method
11902 void flushLayer() {
11903 if (mLayerType == LAYER_TYPE_HARDWARE && mHardwareLayer != null) {
11904 mHardwareLayer.flush();
11905 }
11906 }
Romain Guyf1ae1062011-03-02 18:16:04 -080011907
11908 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080011909 * <p>Returns a hardware layer that can be used to draw this view again
11910 * without executing its draw method.</p>
11911 *
11912 * @return A HardwareLayer ready to render, or null if an error occurred.
11913 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080011914 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070011915 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
11916 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080011917 return null;
11918 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011919
Romain Guy9c4b79a2011-11-10 19:23:58 -080011920 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080011921
11922 final int width = mRight - mLeft;
11923 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080011924
Romain Guy6c319ca2011-01-11 14:29:25 -080011925 if (width == 0 || height == 0) {
11926 return null;
11927 }
11928
11929 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
11930 if (mHardwareLayer == null) {
11931 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
11932 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070011933 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy846a5332012-07-11 17:44:57 -070011934 } else {
11935 if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
11936 mHardwareLayer.resize(width, height);
11937 mLocalDirtyRect.set(0, 0, width, height);
11938 }
11939
11940 // This should not be necessary but applications that change
11941 // the parameters of their background drawable without calling
11942 // this.setBackground(Drawable) can leave the view in a bad state
11943 // (for instance isOpaque() returns true, but the background is
11944 // not opaque.)
11945 computeOpaqueFlags();
11946
11947 final boolean opaque = isOpaque();
11948 if (mHardwareLayer.isOpaque() != opaque) {
11949 mHardwareLayer.setOpaque(opaque);
11950 mLocalDirtyRect.set(0, 0, width, height);
11951 }
Romain Guy6c319ca2011-01-11 14:29:25 -080011952 }
11953
Romain Guy5cd5c3f2011-10-17 17:10:02 -070011954 // The layer is not valid if the underlying GPU resources cannot be allocated
11955 if (!mHardwareLayer.isValid()) {
11956 return null;
11957 }
11958
Chet Haasea1cff502012-02-21 13:43:44 -080011959 mHardwareLayer.redraw(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
Michael Jurka7e52caf2012-03-06 15:57:06 -080011960 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080011961 }
11962
11963 return mHardwareLayer;
11964 }
Romain Guy171c5922011-01-06 10:04:23 -080011965
Romain Guy589b0bb2011-10-10 13:57:47 -070011966 /**
11967 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070011968 *
Romain Guy589b0bb2011-10-10 13:57:47 -070011969 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070011970 *
11971 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070011972 * @see #LAYER_TYPE_HARDWARE
11973 */
Romain Guya998dff2012-03-23 18:58:36 -070011974 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070011975 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080011976 AttachInfo info = mAttachInfo;
11977 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070011978 info.mHardwareRenderer.isEnabled() &&
11979 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080011980 mHardwareLayer.destroy();
11981 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080011982
Romain Guy9c4b79a2011-11-10 19:23:58 -080011983 invalidate(true);
11984 invalidateParentCaches();
11985 }
Romain Guy65b345f2011-07-27 18:51:50 -070011986 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070011987 }
Romain Guy65b345f2011-07-27 18:51:50 -070011988 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070011989 }
11990
Romain Guy171c5922011-01-06 10:04:23 -080011991 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080011992 * Destroys all hardware rendering resources. This method is invoked
11993 * when the system needs to reclaim resources. Upon execution of this
11994 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070011995 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080011996 * Note: you <strong>must</strong> call
11997 * <code>super.destroyHardwareResources()</code> when overriding
11998 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070011999 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012000 * @hide
12001 */
12002 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070012003 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012004 }
12005
12006 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012007 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
12008 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
12009 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
12010 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
12011 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
12012 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012013 *
Romain Guy171c5922011-01-06 10:04:23 -080012014 * <p>Enabling the drawing cache is similar to
12015 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080012016 * acceleration is turned off. When hardware acceleration is turned on, enabling the
12017 * drawing cache has no effect on rendering because the system uses a different mechanism
12018 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
12019 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
12020 * for information on how to enable software and hardware layers.</p>
12021 *
12022 * <p>This API can be used to manually generate
12023 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
12024 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012025 *
12026 * @param enabled true to enable the drawing cache, false otherwise
12027 *
12028 * @see #isDrawingCacheEnabled()
12029 * @see #getDrawingCache()
12030 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080012031 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012032 */
12033 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012034 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012035 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
12036 }
12037
12038 /**
12039 * <p>Indicates whether the drawing cache is enabled for this view.</p>
12040 *
12041 * @return true if the drawing cache is enabled
12042 *
12043 * @see #setDrawingCacheEnabled(boolean)
12044 * @see #getDrawingCache()
12045 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012046 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012047 public boolean isDrawingCacheEnabled() {
12048 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
12049 }
12050
12051 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080012052 * Debugging utility which recursively outputs the dirty state of a view and its
12053 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080012054 *
Chet Haasedaf98e92011-01-10 14:10:36 -080012055 * @hide
12056 */
Romain Guy676b1732011-02-14 14:45:33 -080012057 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080012058 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
12059 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
12060 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
12061 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
12062 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
12063 if (clear) {
12064 mPrivateFlags &= clearMask;
12065 }
12066 if (this instanceof ViewGroup) {
12067 ViewGroup parent = (ViewGroup) this;
12068 final int count = parent.getChildCount();
12069 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080012070 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080012071 child.outputDirtyFlags(indent + " ", clear, clearMask);
12072 }
12073 }
12074 }
12075
12076 /**
12077 * This method is used by ViewGroup to cause its children to restore or recreate their
12078 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
12079 * to recreate its own display list, which would happen if it went through the normal
12080 * draw/dispatchDraw mechanisms.
12081 *
12082 * @hide
12083 */
12084 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080012085
12086 /**
12087 * A view that is not attached or hardware accelerated cannot create a display list.
12088 * This method checks these conditions and returns the appropriate result.
12089 *
12090 * @return true if view has the ability to create a display list, false otherwise.
12091 *
12092 * @hide
12093 */
12094 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080012095 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080012096 }
Joe Malin32736f02011-01-19 16:14:20 -080012097
Chet Haasedaf98e92011-01-10 14:10:36 -080012098 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080012099 * @return The HardwareRenderer associated with that view or null if hardware rendering
12100 * is not supported or this this has not been attached to a window.
12101 *
12102 * @hide
12103 */
12104 public HardwareRenderer getHardwareRenderer() {
12105 if (mAttachInfo != null) {
12106 return mAttachInfo.mHardwareRenderer;
12107 }
12108 return null;
12109 }
12110
12111 /**
Chet Haasea1cff502012-02-21 13:43:44 -080012112 * Returns a DisplayList. If the incoming displayList is null, one will be created.
12113 * Otherwise, the same display list will be returned (after having been rendered into
12114 * along the way, depending on the invalidation state of the view).
12115 *
12116 * @param displayList The previous version of this displayList, could be null.
12117 * @param isLayer Whether the requester of the display list is a layer. If so,
12118 * the view will avoid creating a layer inside the resulting display list.
12119 * @return A new or reused DisplayList object.
12120 */
12121 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
12122 if (!canHaveDisplayList()) {
12123 return null;
12124 }
12125
12126 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
12127 displayList == null || !displayList.isValid() ||
12128 (!isLayer && mRecreateDisplayList))) {
12129 // Don't need to recreate the display list, just need to tell our
12130 // children to restore/recreate theirs
12131 if (displayList != null && displayList.isValid() &&
12132 !isLayer && !mRecreateDisplayList) {
12133 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12134 mPrivateFlags &= ~DIRTY_MASK;
12135 dispatchGetDisplayList();
12136
12137 return displayList;
12138 }
12139
12140 if (!isLayer) {
12141 // If we got here, we're recreating it. Mark it as such to ensure that
12142 // we copy in child display lists into ours in drawChild()
12143 mRecreateDisplayList = true;
12144 }
12145 if (displayList == null) {
12146 final String name = getClass().getSimpleName();
12147 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
12148 // If we're creating a new display list, make sure our parent gets invalidated
12149 // since they will need to recreate their display list to account for this
12150 // new child display list.
12151 invalidateParentCaches();
12152 }
12153
12154 boolean caching = false;
12155 final HardwareCanvas canvas = displayList.start();
Chet Haasea1cff502012-02-21 13:43:44 -080012156 int width = mRight - mLeft;
12157 int height = mBottom - mTop;
12158
12159 try {
12160 canvas.setViewport(width, height);
12161 // The dirty rect should always be null for a display list
12162 canvas.onPreDraw(null);
Michael Jurkaba649742012-06-28 19:12:58 -070012163 int layerType = getLayerType();
Chet Haase1271e2c2012-04-20 09:54:27 -070012164 if (!isLayer && layerType != LAYER_TYPE_NONE) {
Chet Haaseb85967b2012-03-26 14:37:51 -070012165 if (layerType == LAYER_TYPE_HARDWARE) {
12166 final HardwareLayer layer = getHardwareLayer();
12167 if (layer != null && layer.isValid()) {
12168 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
12169 } else {
12170 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
12171 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
12172 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12173 }
12174 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080012175 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070012176 buildDrawingCache(true);
12177 Bitmap cache = getDrawingCache(true);
12178 if (cache != null) {
12179 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
12180 caching = true;
12181 }
Chet Haasea1cff502012-02-21 13:43:44 -080012182 }
Chet Haasea1cff502012-02-21 13:43:44 -080012183 } else {
12184
12185 computeScroll();
12186
Chet Haasea1cff502012-02-21 13:43:44 -080012187 canvas.translate(-mScrollX, -mScrollY);
12188 if (!isLayer) {
12189 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12190 mPrivateFlags &= ~DIRTY_MASK;
12191 }
12192
12193 // Fast path for layouts with no backgrounds
12194 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12195 dispatchDraw(canvas);
12196 } else {
12197 draw(canvas);
12198 }
12199 }
12200 } finally {
Chet Haasea1cff502012-02-21 13:43:44 -080012201 canvas.onPostDraw();
12202
12203 displayList.end();
Chet Haase1271e2c2012-04-20 09:54:27 -070012204 displayList.setCaching(caching);
12205 if (isLayer) {
Chet Haasea1cff502012-02-21 13:43:44 -080012206 displayList.setLeftTopRightBottom(0, 0, width, height);
12207 } else {
12208 setDisplayListProperties(displayList);
12209 }
12210 }
12211 } else if (!isLayer) {
12212 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12213 mPrivateFlags &= ~DIRTY_MASK;
12214 }
12215
12216 return displayList;
12217 }
12218
12219 /**
12220 * Get the DisplayList for the HardwareLayer
12221 *
12222 * @param layer The HardwareLayer whose DisplayList we want
12223 * @return A DisplayList fopr the specified HardwareLayer
12224 */
12225 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
12226 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
12227 layer.setDisplayList(displayList);
12228 return displayList;
12229 }
12230
12231
12232 /**
Romain Guyb051e892010-09-28 19:09:36 -070012233 * <p>Returns a display list that can be used to draw this view again
12234 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012235 *
Romain Guyb051e892010-09-28 19:09:36 -070012236 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080012237 *
12238 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070012239 */
Chet Haasedaf98e92011-01-10 14:10:36 -080012240 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080012241 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070012242 return mDisplayList;
12243 }
12244
Romain Guy38c2ece2012-05-24 14:20:56 -070012245 private void clearDisplayList() {
12246 if (mDisplayList != null) {
12247 mDisplayList.invalidate();
12248 mDisplayList.clear();
12249 }
12250 }
12251
Romain Guyb051e892010-09-28 19:09:36 -070012252 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012253 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012254 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012255 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012256 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012257 * @see #getDrawingCache(boolean)
12258 */
12259 public Bitmap getDrawingCache() {
12260 return getDrawingCache(false);
12261 }
12262
12263 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012264 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
12265 * is null when caching is disabled. If caching is enabled and the cache is not ready,
12266 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
12267 * draw from the cache when the cache is enabled. To benefit from the cache, you must
12268 * request the drawing cache by calling this method and draw it on screen if the
12269 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012270 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012271 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12272 * this method will create a bitmap of the same size as this view. Because this bitmap
12273 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12274 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12275 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12276 * size than the view. This implies that your application must be able to handle this
12277 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012278 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012279 * @param autoScale Indicates whether the generated bitmap should be scaled based on
12280 * the current density of the screen when the application is in compatibility
12281 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012282 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012283 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012284 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012285 * @see #setDrawingCacheEnabled(boolean)
12286 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070012287 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012288 * @see #destroyDrawingCache()
12289 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012290 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012291 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
12292 return null;
12293 }
12294 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012295 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012296 }
Romain Guy02890fd2010-08-06 17:58:44 -070012297 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012298 }
12299
12300 /**
12301 * <p>Frees the resources used by the drawing cache. If you call
12302 * {@link #buildDrawingCache()} manually without calling
12303 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12304 * should cleanup the cache with this method afterwards.</p>
12305 *
12306 * @see #setDrawingCacheEnabled(boolean)
12307 * @see #buildDrawingCache()
12308 * @see #getDrawingCache()
12309 */
12310 public void destroyDrawingCache() {
12311 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012312 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012313 mDrawingCache = null;
12314 }
Romain Guyfbd8f692009-06-26 14:51:58 -070012315 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012316 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070012317 mUnscaledDrawingCache = null;
12318 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012319 }
12320
12321 /**
12322 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070012323 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012324 * view will always be drawn on top of a solid color.
12325 *
12326 * @param color The background color to use for the drawing cache's bitmap
12327 *
12328 * @see #setDrawingCacheEnabled(boolean)
12329 * @see #buildDrawingCache()
12330 * @see #getDrawingCache()
12331 */
12332 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080012333 if (color != mDrawingCacheBackgroundColor) {
12334 mDrawingCacheBackgroundColor = color;
12335 mPrivateFlags &= ~DRAWING_CACHE_VALID;
12336 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012337 }
12338
12339 /**
12340 * @see #setDrawingCacheBackgroundColor(int)
12341 *
12342 * @return The background color to used for the drawing cache's bitmap
12343 */
12344 public int getDrawingCacheBackgroundColor() {
12345 return mDrawingCacheBackgroundColor;
12346 }
12347
12348 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012349 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012350 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012351 * @see #buildDrawingCache(boolean)
12352 */
12353 public void buildDrawingCache() {
12354 buildDrawingCache(false);
12355 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080012356
Romain Guyfbd8f692009-06-26 14:51:58 -070012357 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012358 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
12359 *
12360 * <p>If you call {@link #buildDrawingCache()} manually without calling
12361 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12362 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012363 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012364 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12365 * this method will create a bitmap of the same size as this view. Because this bitmap
12366 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12367 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12368 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12369 * size than the view. This implies that your application must be able to handle this
12370 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012371 *
Romain Guy0d9275e2010-10-26 14:22:30 -070012372 * <p>You should avoid calling this method when hardware acceleration is enabled. If
12373 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080012374 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070012375 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012376 *
12377 * @see #getDrawingCache()
12378 * @see #destroyDrawingCache()
12379 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012380 public void buildDrawingCache(boolean autoScale) {
12381 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070012382 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012383 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012384
Romain Guy8506ab42009-06-11 17:35:47 -070012385 int width = mRight - mLeft;
12386 int height = mBottom - mTop;
12387
12388 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070012389 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070012390
Romain Guye1123222009-06-29 14:24:56 -070012391 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012392 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
12393 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070012394 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012395
12396 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070012397 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080012398 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012399
12400 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070012401 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080012402 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012403 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
12404 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080012405 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012406 return;
12407 }
12408
12409 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070012410 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012411
12412 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012413 Bitmap.Config quality;
12414 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080012415 // Never pick ARGB_4444 because it looks awful
12416 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012417 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
12418 case DRAWING_CACHE_QUALITY_AUTO:
12419 quality = Bitmap.Config.ARGB_8888;
12420 break;
12421 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080012422 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012423 break;
12424 case DRAWING_CACHE_QUALITY_HIGH:
12425 quality = Bitmap.Config.ARGB_8888;
12426 break;
12427 default:
12428 quality = Bitmap.Config.ARGB_8888;
12429 break;
12430 }
12431 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070012432 // Optimization for translucent windows
12433 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080012434 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012435 }
12436
12437 // Try to cleanup memory
12438 if (bitmap != null) bitmap.recycle();
12439
12440 try {
12441 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070012442 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070012443 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070012444 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012445 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070012446 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012447 }
Adam Powell26153a32010-11-08 15:22:27 -080012448 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012449 } catch (OutOfMemoryError e) {
12450 // If there is not enough memory to create the bitmap cache, just
12451 // ignore the issue as bitmap caches are not required to draw the
12452 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012453 if (autoScale) {
12454 mDrawingCache = null;
12455 } else {
12456 mUnscaledDrawingCache = null;
12457 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012458 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012459 return;
12460 }
12461
12462 clear = drawingCacheBackgroundColor != 0;
12463 }
12464
12465 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012466 if (attachInfo != null) {
12467 canvas = attachInfo.mCanvas;
12468 if (canvas == null) {
12469 canvas = new Canvas();
12470 }
12471 canvas.setBitmap(bitmap);
12472 // Temporarily clobber the cached Canvas in case one of our children
12473 // is also using a drawing cache. Without this, the children would
12474 // steal the canvas by attaching their own bitmap to it and bad, bad
12475 // thing would happen (invisible views, corrupted drawings, etc.)
12476 attachInfo.mCanvas = null;
12477 } else {
12478 // This case should hopefully never or seldom happen
12479 canvas = new Canvas(bitmap);
12480 }
12481
12482 if (clear) {
12483 bitmap.eraseColor(drawingCacheBackgroundColor);
12484 }
12485
12486 computeScroll();
12487 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080012488
Romain Guye1123222009-06-29 14:24:56 -070012489 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012490 final float scale = attachInfo.mApplicationScale;
12491 canvas.scale(scale, scale);
12492 }
Joe Malin32736f02011-01-19 16:14:20 -080012493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012494 canvas.translate(-mScrollX, -mScrollY);
12495
Romain Guy5bcdff42009-05-14 21:27:18 -070012496 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080012497 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
12498 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070012499 mPrivateFlags |= DRAWING_CACHE_VALID;
12500 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012501
12502 // Fast path for layouts with no backgrounds
12503 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Romain Guy5bcdff42009-05-14 21:27:18 -070012504 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012505 dispatchDraw(canvas);
12506 } else {
12507 draw(canvas);
12508 }
12509
12510 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012511 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012512
12513 if (attachInfo != null) {
12514 // Restore the cached Canvas for our siblings
12515 attachInfo.mCanvas = canvas;
12516 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012517 }
12518 }
12519
12520 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012521 * Create a snapshot of the view into a bitmap. We should probably make
12522 * some form of this public, but should think about the API.
12523 */
Romain Guy223ff5c2010-03-02 17:07:47 -080012524 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012525 int width = mRight - mLeft;
12526 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012527
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012528 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070012529 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012530 width = (int) ((width * scale) + 0.5f);
12531 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080012532
Romain Guy8c11e312009-09-14 15:15:30 -070012533 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012534 if (bitmap == null) {
12535 throw new OutOfMemoryError();
12536 }
12537
Romain Guyc529d8d2011-09-06 15:01:39 -070012538 Resources resources = getResources();
12539 if (resources != null) {
12540 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
12541 }
Joe Malin32736f02011-01-19 16:14:20 -080012542
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012543 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012544 if (attachInfo != null) {
12545 canvas = attachInfo.mCanvas;
12546 if (canvas == null) {
12547 canvas = new Canvas();
12548 }
12549 canvas.setBitmap(bitmap);
12550 // Temporarily clobber the cached Canvas in case one of our children
12551 // is also using a drawing cache. Without this, the children would
12552 // steal the canvas by attaching their own bitmap to it and bad, bad
12553 // things would happen (invisible views, corrupted drawings, etc.)
12554 attachInfo.mCanvas = null;
12555 } else {
12556 // This case should hopefully never or seldom happen
12557 canvas = new Canvas(bitmap);
12558 }
12559
Romain Guy5bcdff42009-05-14 21:27:18 -070012560 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012561 bitmap.eraseColor(backgroundColor);
12562 }
12563
12564 computeScroll();
12565 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012566 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012567 canvas.translate(-mScrollX, -mScrollY);
12568
Romain Guy5bcdff42009-05-14 21:27:18 -070012569 // Temporarily remove the dirty mask
12570 int flags = mPrivateFlags;
12571 mPrivateFlags &= ~DIRTY_MASK;
12572
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012573 // Fast path for layouts with no backgrounds
12574 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12575 dispatchDraw(canvas);
12576 } else {
12577 draw(canvas);
12578 }
12579
Romain Guy5bcdff42009-05-14 21:27:18 -070012580 mPrivateFlags = flags;
12581
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012582 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012583 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012584
12585 if (attachInfo != null) {
12586 // Restore the cached Canvas for our siblings
12587 attachInfo.mCanvas = canvas;
12588 }
Romain Guy8506ab42009-06-11 17:35:47 -070012589
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012590 return bitmap;
12591 }
12592
12593 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012594 * Indicates whether this View is currently in edit mode. A View is usually
12595 * in edit mode when displayed within a developer tool. For instance, if
12596 * this View is being drawn by a visual user interface builder, this method
12597 * should return true.
12598 *
12599 * Subclasses should check the return value of this method to provide
12600 * different behaviors if their normal behavior might interfere with the
12601 * host environment. For instance: the class spawns a thread in its
12602 * constructor, the drawing code relies on device-specific features, etc.
12603 *
12604 * This method is usually checked in the drawing code of custom widgets.
12605 *
12606 * @return True if this View is in edit mode, false otherwise.
12607 */
12608 public boolean isInEditMode() {
12609 return false;
12610 }
12611
12612 /**
12613 * If the View draws content inside its padding and enables fading edges,
12614 * it needs to support padding offsets. Padding offsets are added to the
12615 * fading edges to extend the length of the fade so that it covers pixels
12616 * drawn inside the padding.
12617 *
12618 * Subclasses of this class should override this method if they need
12619 * to draw content inside the padding.
12620 *
12621 * @return True if padding offset must be applied, false otherwise.
12622 *
12623 * @see #getLeftPaddingOffset()
12624 * @see #getRightPaddingOffset()
12625 * @see #getTopPaddingOffset()
12626 * @see #getBottomPaddingOffset()
12627 *
12628 * @since CURRENT
12629 */
12630 protected boolean isPaddingOffsetRequired() {
12631 return false;
12632 }
12633
12634 /**
12635 * Amount by which to extend the left fading region. Called only when
12636 * {@link #isPaddingOffsetRequired()} returns true.
12637 *
12638 * @return The left padding offset in pixels.
12639 *
12640 * @see #isPaddingOffsetRequired()
12641 *
12642 * @since CURRENT
12643 */
12644 protected int getLeftPaddingOffset() {
12645 return 0;
12646 }
12647
12648 /**
12649 * Amount by which to extend the right fading region. Called only when
12650 * {@link #isPaddingOffsetRequired()} returns true.
12651 *
12652 * @return The right padding offset in pixels.
12653 *
12654 * @see #isPaddingOffsetRequired()
12655 *
12656 * @since CURRENT
12657 */
12658 protected int getRightPaddingOffset() {
12659 return 0;
12660 }
12661
12662 /**
12663 * Amount by which to extend the top fading region. Called only when
12664 * {@link #isPaddingOffsetRequired()} returns true.
12665 *
12666 * @return The top padding offset in pixels.
12667 *
12668 * @see #isPaddingOffsetRequired()
12669 *
12670 * @since CURRENT
12671 */
12672 protected int getTopPaddingOffset() {
12673 return 0;
12674 }
12675
12676 /**
12677 * Amount by which to extend the bottom fading region. Called only when
12678 * {@link #isPaddingOffsetRequired()} returns true.
12679 *
12680 * @return The bottom padding offset in pixels.
12681 *
12682 * @see #isPaddingOffsetRequired()
12683 *
12684 * @since CURRENT
12685 */
12686 protected int getBottomPaddingOffset() {
12687 return 0;
12688 }
12689
12690 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070012691 * @hide
12692 * @param offsetRequired
12693 */
12694 protected int getFadeTop(boolean offsetRequired) {
12695 int top = mPaddingTop;
12696 if (offsetRequired) top += getTopPaddingOffset();
12697 return top;
12698 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012699
Romain Guyf2fc4602011-07-19 15:20:03 -070012700 /**
12701 * @hide
12702 * @param offsetRequired
12703 */
12704 protected int getFadeHeight(boolean offsetRequired) {
12705 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070012706 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070012707 return mBottom - mTop - mPaddingBottom - padding;
12708 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012709
Romain Guyf2fc4602011-07-19 15:20:03 -070012710 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012711 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070012712 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012713 *
Romain Guy2bffd262010-09-12 17:40:02 -070012714 * <p>Even if this method returns true, it does not mean that every call
12715 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
12716 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012717 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070012718 * window is hardware accelerated,
12719 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
12720 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012721 *
Romain Guy2bffd262010-09-12 17:40:02 -070012722 * @return True if the view is attached to a window and the window is
12723 * hardware accelerated; false in any other case.
12724 */
12725 public boolean isHardwareAccelerated() {
12726 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
12727 }
Joe Malin32736f02011-01-19 16:14:20 -080012728
Romain Guy2bffd262010-09-12 17:40:02 -070012729 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080012730 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
12731 * case of an active Animation being run on the view.
12732 */
12733 private boolean drawAnimation(ViewGroup parent, long drawingTime,
12734 Animation a, boolean scalingRequired) {
12735 Transformation invalidationTransform;
12736 final int flags = parent.mGroupFlags;
12737 final boolean initialized = a.isInitialized();
12738 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070012739 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080012740 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
Romain Guy393a52c2012-05-22 20:21:08 -070012741 if (mAttachInfo != null) a.setListenerHandler(mAttachInfo.mHandler);
Chet Haasebcca79a2012-02-14 08:45:14 -080012742 onAnimationStart();
12743 }
12744
12745 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
12746 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
12747 if (parent.mInvalidationTransformation == null) {
12748 parent.mInvalidationTransformation = new Transformation();
12749 }
12750 invalidationTransform = parent.mInvalidationTransformation;
12751 a.getTransformation(drawingTime, invalidationTransform, 1f);
12752 } else {
12753 invalidationTransform = parent.mChildTransformation;
12754 }
Romain Guy393a52c2012-05-22 20:21:08 -070012755
Chet Haasebcca79a2012-02-14 08:45:14 -080012756 if (more) {
12757 if (!a.willChangeBounds()) {
12758 if ((flags & (parent.FLAG_OPTIMIZE_INVALIDATE | parent.FLAG_ANIMATION_DONE)) ==
12759 parent.FLAG_OPTIMIZE_INVALIDATE) {
12760 parent.mGroupFlags |= parent.FLAG_INVALIDATE_REQUIRED;
12761 } else if ((flags & parent.FLAG_INVALIDATE_REQUIRED) == 0) {
12762 // The child need to draw an animation, potentially offscreen, so
12763 // make sure we do not cancel invalidate requests
12764 parent.mPrivateFlags |= DRAW_ANIMATION;
12765 parent.invalidate(mLeft, mTop, mRight, mBottom);
12766 }
12767 } else {
12768 if (parent.mInvalidateRegion == null) {
12769 parent.mInvalidateRegion = new RectF();
12770 }
12771 final RectF region = parent.mInvalidateRegion;
12772 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
12773 invalidationTransform);
12774
12775 // The child need to draw an animation, potentially offscreen, so
12776 // make sure we do not cancel invalidate requests
12777 parent.mPrivateFlags |= DRAW_ANIMATION;
12778
12779 final int left = mLeft + (int) region.left;
12780 final int top = mTop + (int) region.top;
12781 parent.invalidate(left, top, left + (int) (region.width() + .5f),
12782 top + (int) (region.height() + .5f));
12783 }
12784 }
12785 return more;
12786 }
12787
Chet Haasea1cff502012-02-21 13:43:44 -080012788 /**
12789 * This method is called by getDisplayList() when a display list is created or re-rendered.
12790 * It sets or resets the current value of all properties on that display list (resetting is
12791 * necessary when a display list is being re-created, because we need to make sure that
12792 * previously-set transform values
12793 */
12794 void setDisplayListProperties(DisplayList displayList) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012795 if (displayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012796 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070012797 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080012798 if (mParent instanceof ViewGroup) {
12799 displayList.setClipChildren(
12800 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
12801 }
Chet Haase9420abd2012-03-29 16:28:32 -070012802 float alpha = 1;
12803 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
12804 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
12805 ViewGroup parentVG = (ViewGroup) mParent;
12806 final boolean hasTransform =
12807 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
12808 if (hasTransform) {
12809 Transformation transform = parentVG.mChildTransformation;
12810 final int transformType = parentVG.mChildTransformation.getTransformationType();
12811 if (transformType != Transformation.TYPE_IDENTITY) {
12812 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
12813 alpha = transform.getAlpha();
12814 }
12815 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
12816 displayList.setStaticMatrix(transform.getMatrix());
12817 }
12818 }
12819 }
Chet Haasea1cff502012-02-21 13:43:44 -080012820 }
12821 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070012822 alpha *= mTransformationInfo.mAlpha;
12823 if (alpha < 1) {
12824 final int multipliedAlpha = (int) (255 * alpha);
12825 if (onSetAlpha(multipliedAlpha)) {
12826 alpha = 1;
12827 }
12828 }
12829 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080012830 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
12831 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
12832 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
12833 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070012834 if (mTransformationInfo.mCamera == null) {
12835 mTransformationInfo.mCamera = new Camera();
12836 mTransformationInfo.matrix3D = new Matrix();
12837 }
12838 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Chet Haasea1cff502012-02-21 13:43:44 -080012839 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == PIVOT_EXPLICITLY_SET) {
12840 displayList.setPivotX(getPivotX());
12841 displayList.setPivotY(getPivotY());
12842 }
Chet Haase9420abd2012-03-29 16:28:32 -070012843 } else if (alpha < 1) {
12844 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080012845 }
12846 }
12847 }
12848
Chet Haasebcca79a2012-02-14 08:45:14 -080012849 /**
Chet Haase64a48c12012-02-13 16:33:29 -080012850 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
12851 * This draw() method is an implementation detail and is not intended to be overridden or
12852 * to be called from anywhere else other than ViewGroup.drawChild().
12853 */
12854 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012855 boolean useDisplayListProperties = mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080012856 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080012857 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080012858 final int flags = parent.mGroupFlags;
12859
Chet Haasea1cff502012-02-21 13:43:44 -080012860 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080012861 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080012862 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012863 }
12864
12865 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080012866 boolean concatMatrix = false;
12867
12868 boolean scalingRequired = false;
12869 boolean caching;
Michael Jurkaba649742012-06-28 19:12:58 -070012870 int layerType = getLayerType();
Chet Haase64a48c12012-02-13 16:33:29 -080012871
12872 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080012873 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
12874 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080012875 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070012876 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080012877 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
12878 } else {
12879 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
12880 }
12881
Chet Haasebcca79a2012-02-14 08:45:14 -080012882 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080012883 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012884 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080012885 concatMatrix = a.willChangeTransformationMatrix();
Chet Haaseafd5c3e2012-05-10 13:21:10 -070012886 if (concatMatrix) {
Chet Haase21433372012-06-05 07:54:09 -070012887 mPrivateFlags3 |= VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070012888 }
Chet Haasebcca79a2012-02-14 08:45:14 -080012889 transformToApply = parent.mChildTransformation;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070012890 } else {
Chet Haase21433372012-06-05 07:54:09 -070012891 if ((mPrivateFlags3 & VIEW_IS_ANIMATING_TRANSFORM) == VIEW_IS_ANIMATING_TRANSFORM &&
Chet Haaseafd5c3e2012-05-10 13:21:10 -070012892 mDisplayList != null) {
12893 // No longer animating: clear out old animation matrix
12894 mDisplayList.setAnimationMatrix(null);
Chet Haase21433372012-06-05 07:54:09 -070012895 mPrivateFlags3 &= ~VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070012896 }
12897 if (!useDisplayListProperties &&
12898 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
12899 final boolean hasTransform =
12900 parent.getChildStaticTransformation(this, parent.mChildTransformation);
12901 if (hasTransform) {
12902 final int transformType = parent.mChildTransformation.getTransformationType();
12903 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
12904 parent.mChildTransformation : null;
12905 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
12906 }
Chet Haase64a48c12012-02-13 16:33:29 -080012907 }
12908 }
12909
12910 concatMatrix |= !childHasIdentityMatrix;
12911
12912 // Sets the flag as early as possible to allow draw() implementations
12913 // to call invalidate() successfully when doing animations
12914 mPrivateFlags |= DRAWN;
12915
Chet Haasebcca79a2012-02-14 08:45:14 -080012916 if (!concatMatrix && canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Chet Haase64a48c12012-02-13 16:33:29 -080012917 (mPrivateFlags & DRAW_ANIMATION) == 0) {
Chet Haase1a3ab172012-05-11 08:41:20 -070012918 mPrivateFlags2 |= VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080012919 return more;
12920 }
Chet Haase1a3ab172012-05-11 08:41:20 -070012921 mPrivateFlags2 &= ~VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080012922
12923 if (hardwareAccelerated) {
12924 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
12925 // retain the flag's value temporarily in the mRecreateDisplayList flag
12926 mRecreateDisplayList = (mPrivateFlags & INVALIDATED) == INVALIDATED;
12927 mPrivateFlags &= ~INVALIDATED;
12928 }
12929
12930 computeScroll();
12931
12932 final int sx = mScrollX;
12933 final int sy = mScrollY;
12934
12935 DisplayList displayList = null;
12936 Bitmap cache = null;
12937 boolean hasDisplayList = false;
12938 if (caching) {
12939 if (!hardwareAccelerated) {
12940 if (layerType != LAYER_TYPE_NONE) {
12941 layerType = LAYER_TYPE_SOFTWARE;
12942 buildDrawingCache(true);
12943 }
12944 cache = getDrawingCache(true);
12945 } else {
12946 switch (layerType) {
12947 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070012948 if (useDisplayListProperties) {
12949 hasDisplayList = canHaveDisplayList();
12950 } else {
12951 buildDrawingCache(true);
12952 cache = getDrawingCache(true);
12953 }
Chet Haase64a48c12012-02-13 16:33:29 -080012954 break;
Chet Haasea1cff502012-02-21 13:43:44 -080012955 case LAYER_TYPE_HARDWARE:
12956 if (useDisplayListProperties) {
12957 hasDisplayList = canHaveDisplayList();
12958 }
12959 break;
Chet Haase64a48c12012-02-13 16:33:29 -080012960 case LAYER_TYPE_NONE:
12961 // Delay getting the display list until animation-driven alpha values are
12962 // set up and possibly passed on to the view
12963 hasDisplayList = canHaveDisplayList();
12964 break;
12965 }
12966 }
12967 }
Chet Haasea1cff502012-02-21 13:43:44 -080012968 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070012969 if (useDisplayListProperties) {
12970 displayList = getDisplayList();
12971 if (!displayList.isValid()) {
12972 // Uncommon, but possible. If a view is removed from the hierarchy during the call
12973 // to getDisplayList(), the display list will be marked invalid and we should not
12974 // try to use it again.
12975 displayList = null;
12976 hasDisplayList = false;
12977 useDisplayListProperties = false;
12978 }
12979 }
Chet Haase64a48c12012-02-13 16:33:29 -080012980
12981 final boolean hasNoCache = cache == null || hasDisplayList;
12982 final boolean offsetForScroll = cache == null && !hasDisplayList &&
12983 layerType != LAYER_TYPE_HARDWARE;
12984
Chet Haasea1cff502012-02-21 13:43:44 -080012985 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070012986 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012987 restoreTo = canvas.save();
12988 }
Chet Haase64a48c12012-02-13 16:33:29 -080012989 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012990 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080012991 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080012992 if (!useDisplayListProperties) {
12993 canvas.translate(mLeft, mTop);
12994 }
Chet Haase64a48c12012-02-13 16:33:29 -080012995 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080012996 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070012997 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080012998 restoreTo = canvas.save();
12999 }
Chet Haase64a48c12012-02-13 16:33:29 -080013000 // mAttachInfo cannot be null, otherwise scalingRequired == false
13001 final float scale = 1.0f / mAttachInfo.mApplicationScale;
13002 canvas.scale(scale, scale);
13003 }
13004 }
13005
Chet Haasea1cff502012-02-21 13:43:44 -080013006 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase21433372012-06-05 07:54:09 -070013007 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix() ||
13008 (mPrivateFlags3 & VIEW_IS_ANIMATING_ALPHA) == VIEW_IS_ANIMATING_ALPHA) {
Chet Haase64a48c12012-02-13 16:33:29 -080013009 if (transformToApply != null || !childHasIdentityMatrix) {
13010 int transX = 0;
13011 int transY = 0;
13012
13013 if (offsetForScroll) {
13014 transX = -sx;
13015 transY = -sy;
13016 }
13017
13018 if (transformToApply != null) {
13019 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070013020 if (useDisplayListProperties) {
13021 displayList.setAnimationMatrix(transformToApply.getMatrix());
13022 } else {
13023 // Undo the scroll translation, apply the transformation matrix,
13024 // then redo the scroll translate to get the correct result.
13025 canvas.translate(-transX, -transY);
13026 canvas.concat(transformToApply.getMatrix());
13027 canvas.translate(transX, transY);
13028 }
Chet Haasea1cff502012-02-21 13:43:44 -080013029 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013030 }
13031
13032 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070013033 if (transformAlpha < 1) {
Chet Haase21433372012-06-05 07:54:09 -070013034 alpha *= transformAlpha;
Chet Haasea1cff502012-02-21 13:43:44 -080013035 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013036 }
13037 }
13038
Chet Haasea1cff502012-02-21 13:43:44 -080013039 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013040 canvas.translate(-transX, -transY);
13041 canvas.concat(getMatrix());
13042 canvas.translate(transX, transY);
13043 }
13044 }
13045
Chet Haase21433372012-06-05 07:54:09 -070013046 // Deal with alpha if it is or used to be <1
13047 if (alpha < 1 ||
13048 (mPrivateFlags3 & VIEW_IS_ANIMATING_ALPHA) == VIEW_IS_ANIMATING_ALPHA) {
13049 if (alpha < 1) {
13050 mPrivateFlags3 |= VIEW_IS_ANIMATING_ALPHA;
13051 } else {
13052 mPrivateFlags3 &= ~VIEW_IS_ANIMATING_ALPHA;
13053 }
Chet Haasea1cff502012-02-21 13:43:44 -080013054 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013055 if (hasNoCache) {
13056 final int multipliedAlpha = (int) (255 * alpha);
13057 if (!onSetAlpha(multipliedAlpha)) {
13058 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080013059 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080013060 layerType != LAYER_TYPE_NONE) {
13061 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
13062 }
Chet Haase9420abd2012-03-29 16:28:32 -070013063 if (useDisplayListProperties) {
13064 displayList.setAlpha(alpha * getAlpha());
13065 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070013066 final int scrollX = hasDisplayList ? 0 : sx;
13067 final int scrollY = hasDisplayList ? 0 : sy;
13068 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
13069 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080013070 }
13071 } else {
13072 // Alpha is handled by the child directly, clobber the layer's alpha
13073 mPrivateFlags |= ALPHA_SET;
13074 }
13075 }
13076 }
13077 } else if ((mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
13078 onSetAlpha(255);
13079 mPrivateFlags &= ~ALPHA_SET;
13080 }
13081
Chet Haasea1cff502012-02-21 13:43:44 -080013082 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
13083 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013084 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013085 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080013086 } else {
13087 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013088 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080013089 } else {
13090 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
13091 }
13092 }
13093 }
13094
Chet Haase9420abd2012-03-29 16:28:32 -070013095 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080013096 displayList = getDisplayList();
13097 if (!displayList.isValid()) {
13098 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13099 // to getDisplayList(), the display list will be marked invalid and we should not
13100 // try to use it again.
13101 displayList = null;
13102 hasDisplayList = false;
13103 }
13104 }
13105
13106 if (hasNoCache) {
13107 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080013108 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080013109 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080013110 if (layer != null && layer.isValid()) {
13111 mLayerPaint.setAlpha((int) (alpha * 255));
13112 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
13113 layerRendered = true;
13114 } else {
13115 final int scrollX = hasDisplayList ? 0 : sx;
13116 final int scrollY = hasDisplayList ? 0 : sy;
13117 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080013118 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080013119 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
13120 }
13121 }
13122
13123 if (!layerRendered) {
13124 if (!hasDisplayList) {
13125 // Fast path for layouts with no backgrounds
13126 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Chet Haase64a48c12012-02-13 16:33:29 -080013127 mPrivateFlags &= ~DIRTY_MASK;
13128 dispatchDraw(canvas);
13129 } else {
13130 draw(canvas);
13131 }
13132 } else {
13133 mPrivateFlags &= ~DIRTY_MASK;
Chet Haase1271e2c2012-04-20 09:54:27 -070013134 ((HardwareCanvas) canvas).drawDisplayList(displayList, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080013135 }
13136 }
13137 } else if (cache != null) {
13138 mPrivateFlags &= ~DIRTY_MASK;
13139 Paint cachePaint;
13140
13141 if (layerType == LAYER_TYPE_NONE) {
13142 cachePaint = parent.mCachePaint;
13143 if (cachePaint == null) {
13144 cachePaint = new Paint();
13145 cachePaint.setDither(false);
13146 parent.mCachePaint = cachePaint;
13147 }
Chet Haase9420abd2012-03-29 16:28:32 -070013148 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080013149 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080013150 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
13151 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013152 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080013153 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080013154 }
13155 } else {
13156 cachePaint = mLayerPaint;
13157 cachePaint.setAlpha((int) (alpha * 255));
13158 }
13159 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
13160 }
13161
Chet Haasea1cff502012-02-21 13:43:44 -080013162 if (restoreTo >= 0) {
13163 canvas.restoreToCount(restoreTo);
13164 }
Chet Haase64a48c12012-02-13 16:33:29 -080013165
13166 if (a != null && !more) {
13167 if (!hardwareAccelerated && !a.getFillAfter()) {
13168 onSetAlpha(255);
13169 }
13170 parent.finishAnimatingView(this, a);
13171 }
13172
13173 if (more && hardwareAccelerated) {
13174 // invalidation is the trigger to recreate display lists, so if we're using
13175 // display lists to render, force an invalidate to allow the animation to
13176 // continue drawing another frame
13177 parent.invalidate(true);
13178 if (a.hasAlpha() && (mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
13179 // alpha animations should cause the child to recreate its display list
13180 invalidate(true);
13181 }
13182 }
13183
13184 mRecreateDisplayList = false;
13185
13186 return more;
13187 }
13188
13189 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013190 * Manually render this view (and all of its children) to the given Canvas.
13191 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070013192 * called. When implementing a view, implement
13193 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
13194 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013195 *
13196 * @param canvas The Canvas to which the View is rendered.
13197 */
13198 public void draw(Canvas canvas) {
Romain Guy5bcdff42009-05-14 21:27:18 -070013199 final int privateFlags = mPrivateFlags;
13200 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
13201 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
13202 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070013203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013204 /*
13205 * Draw traversal performs several drawing steps which must be executed
13206 * in the appropriate order:
13207 *
13208 * 1. Draw the background
13209 * 2. If necessary, save the canvas' layers to prepare for fading
13210 * 3. Draw view's content
13211 * 4. Draw children
13212 * 5. If necessary, draw the fading edges and restore layers
13213 * 6. Draw decorations (scrollbars for instance)
13214 */
13215
13216 // Step 1, draw the background, if needed
13217 int saveCount;
13218
Romain Guy24443ea2009-05-11 11:56:30 -070013219 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013220 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070013221 if (background != null) {
13222 final int scrollX = mScrollX;
13223 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013224
Romain Guy24443ea2009-05-11 11:56:30 -070013225 if (mBackgroundSizeChanged) {
13226 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
13227 mBackgroundSizeChanged = false;
13228 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013229
Romain Guy24443ea2009-05-11 11:56:30 -070013230 if ((scrollX | scrollY) == 0) {
13231 background.draw(canvas);
13232 } else {
13233 canvas.translate(scrollX, scrollY);
13234 background.draw(canvas);
13235 canvas.translate(-scrollX, -scrollY);
13236 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013237 }
13238 }
13239
13240 // skip step 2 & 5 if possible (common case)
13241 final int viewFlags = mViewFlags;
13242 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
13243 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
13244 if (!verticalEdges && !horizontalEdges) {
13245 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013246 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013247
13248 // Step 4, draw the children
13249 dispatchDraw(canvas);
13250
13251 // Step 6, draw decorations (scrollbars)
13252 onDrawScrollBars(canvas);
13253
13254 // we're done...
13255 return;
13256 }
13257
13258 /*
13259 * Here we do the full fledged routine...
13260 * (this is an uncommon case where speed matters less,
13261 * this is why we repeat some of the tests that have been
13262 * done above)
13263 */
13264
13265 boolean drawTop = false;
13266 boolean drawBottom = false;
13267 boolean drawLeft = false;
13268 boolean drawRight = false;
13269
13270 float topFadeStrength = 0.0f;
13271 float bottomFadeStrength = 0.0f;
13272 float leftFadeStrength = 0.0f;
13273 float rightFadeStrength = 0.0f;
13274
13275 // Step 2, save the canvas' layers
13276 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013277
13278 final boolean offsetRequired = isPaddingOffsetRequired();
13279 if (offsetRequired) {
13280 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013281 }
13282
13283 int left = mScrollX + paddingLeft;
13284 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070013285 int top = mScrollY + getFadeTop(offsetRequired);
13286 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013287
13288 if (offsetRequired) {
13289 right += getRightPaddingOffset();
13290 bottom += getBottomPaddingOffset();
13291 }
13292
13293 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070013294 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013295 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013296
13297 // clip the fade length if top and bottom fades overlap
13298 // overlapping fades produce odd-looking artifacts
13299 if (verticalEdges && (top + length > bottom - length)) {
13300 length = (bottom - top) / 2;
13301 }
13302
13303 // also clip horizontal fades if necessary
13304 if (horizontalEdges && (left + length > right - length)) {
13305 length = (right - left) / 2;
13306 }
13307
13308 if (verticalEdges) {
13309 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013310 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013311 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013312 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013313 }
13314
13315 if (horizontalEdges) {
13316 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013317 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013318 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013319 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013320 }
13321
13322 saveCount = canvas.getSaveCount();
13323
13324 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070013325 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013326 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
13327
13328 if (drawTop) {
13329 canvas.saveLayer(left, top, right, top + length, null, flags);
13330 }
13331
13332 if (drawBottom) {
13333 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
13334 }
13335
13336 if (drawLeft) {
13337 canvas.saveLayer(left, top, left + length, bottom, null, flags);
13338 }
13339
13340 if (drawRight) {
13341 canvas.saveLayer(right - length, top, right, bottom, null, flags);
13342 }
13343 } else {
13344 scrollabilityCache.setFadeColor(solidColor);
13345 }
13346
13347 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013348 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013349
13350 // Step 4, draw the children
13351 dispatchDraw(canvas);
13352
13353 // Step 5, draw the fade effect and restore layers
13354 final Paint p = scrollabilityCache.paint;
13355 final Matrix matrix = scrollabilityCache.matrix;
13356 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013357
13358 if (drawTop) {
13359 matrix.setScale(1, fadeHeight * topFadeStrength);
13360 matrix.postTranslate(left, top);
13361 fade.setLocalMatrix(matrix);
13362 canvas.drawRect(left, top, right, top + length, p);
13363 }
13364
13365 if (drawBottom) {
13366 matrix.setScale(1, fadeHeight * bottomFadeStrength);
13367 matrix.postRotate(180);
13368 matrix.postTranslate(left, bottom);
13369 fade.setLocalMatrix(matrix);
13370 canvas.drawRect(left, bottom - length, right, bottom, p);
13371 }
13372
13373 if (drawLeft) {
13374 matrix.setScale(1, fadeHeight * leftFadeStrength);
13375 matrix.postRotate(-90);
13376 matrix.postTranslate(left, top);
13377 fade.setLocalMatrix(matrix);
13378 canvas.drawRect(left, top, left + length, bottom, p);
13379 }
13380
13381 if (drawRight) {
13382 matrix.setScale(1, fadeHeight * rightFadeStrength);
13383 matrix.postRotate(90);
13384 matrix.postTranslate(right, top);
13385 fade.setLocalMatrix(matrix);
13386 canvas.drawRect(right - length, top, right, bottom, p);
13387 }
13388
13389 canvas.restoreToCount(saveCount);
13390
13391 // Step 6, draw decorations (scrollbars)
13392 onDrawScrollBars(canvas);
13393 }
13394
13395 /**
13396 * Override this if your view is known to always be drawn on top of a solid color background,
13397 * and needs to draw fading edges. Returning a non-zero color enables the view system to
13398 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
13399 * should be set to 0xFF.
13400 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013401 * @see #setVerticalFadingEdgeEnabled(boolean)
13402 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013403 *
13404 * @return The known solid color background for this view, or 0 if the color may vary
13405 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013406 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013407 public int getSolidColor() {
13408 return 0;
13409 }
13410
13411 /**
13412 * Build a human readable string representation of the specified view flags.
13413 *
13414 * @param flags the view flags to convert to a string
13415 * @return a String representing the supplied flags
13416 */
13417 private static String printFlags(int flags) {
13418 String output = "";
13419 int numFlags = 0;
13420 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
13421 output += "TAKES_FOCUS";
13422 numFlags++;
13423 }
13424
13425 switch (flags & VISIBILITY_MASK) {
13426 case INVISIBLE:
13427 if (numFlags > 0) {
13428 output += " ";
13429 }
13430 output += "INVISIBLE";
13431 // USELESS HERE numFlags++;
13432 break;
13433 case GONE:
13434 if (numFlags > 0) {
13435 output += " ";
13436 }
13437 output += "GONE";
13438 // USELESS HERE numFlags++;
13439 break;
13440 default:
13441 break;
13442 }
13443 return output;
13444 }
13445
13446 /**
13447 * Build a human readable string representation of the specified private
13448 * view flags.
13449 *
13450 * @param privateFlags the private view flags to convert to a string
13451 * @return a String representing the supplied flags
13452 */
13453 private static String printPrivateFlags(int privateFlags) {
13454 String output = "";
13455 int numFlags = 0;
13456
13457 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
13458 output += "WANTS_FOCUS";
13459 numFlags++;
13460 }
13461
13462 if ((privateFlags & FOCUSED) == FOCUSED) {
13463 if (numFlags > 0) {
13464 output += " ";
13465 }
13466 output += "FOCUSED";
13467 numFlags++;
13468 }
13469
13470 if ((privateFlags & SELECTED) == SELECTED) {
13471 if (numFlags > 0) {
13472 output += " ";
13473 }
13474 output += "SELECTED";
13475 numFlags++;
13476 }
13477
13478 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
13479 if (numFlags > 0) {
13480 output += " ";
13481 }
13482 output += "IS_ROOT_NAMESPACE";
13483 numFlags++;
13484 }
13485
13486 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
13487 if (numFlags > 0) {
13488 output += " ";
13489 }
13490 output += "HAS_BOUNDS";
13491 numFlags++;
13492 }
13493
13494 if ((privateFlags & DRAWN) == DRAWN) {
13495 if (numFlags > 0) {
13496 output += " ";
13497 }
13498 output += "DRAWN";
13499 // USELESS HERE numFlags++;
13500 }
13501 return output;
13502 }
13503
13504 /**
13505 * <p>Indicates whether or not this view's layout will be requested during
13506 * the next hierarchy layout pass.</p>
13507 *
13508 * @return true if the layout will be forced during next layout pass
13509 */
13510 public boolean isLayoutRequested() {
13511 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
13512 }
13513
13514 /**
13515 * Assign a size and position to a view and all of its
13516 * descendants
13517 *
13518 * <p>This is the second phase of the layout mechanism.
13519 * (The first is measuring). In this phase, each parent calls
13520 * layout on all of its children to position them.
13521 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080013522 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013523 *
Chet Haase9c087442011-01-12 16:20:16 -080013524 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013525 * Derived classes with children should override
13526 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080013527 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013528 *
13529 * @param l Left position, relative to parent
13530 * @param t Top position, relative to parent
13531 * @param r Right position, relative to parent
13532 * @param b Bottom position, relative to parent
13533 */
Romain Guy5429e1d2010-09-07 12:38:00 -070013534 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080013535 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070013536 int oldL = mLeft;
13537 int oldT = mTop;
13538 int oldB = mBottom;
13539 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013540 boolean changed = setFrame(l, t, r, b);
13541 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013542 onLayout(changed, l, t, r, b);
13543 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070013544
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013545 ListenerInfo li = mListenerInfo;
13546 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070013547 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013548 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070013549 int numListeners = listenersCopy.size();
13550 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070013551 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070013552 }
13553 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013554 }
13555 mPrivateFlags &= ~FORCE_LAYOUT;
13556 }
13557
13558 /**
13559 * Called from layout when this view should
13560 * assign a size and position to each of its children.
13561 *
13562 * Derived classes with children should override
13563 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070013564 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013565 * @param changed This is a new size or position for this view
13566 * @param left Left position, relative to parent
13567 * @param top Top position, relative to parent
13568 * @param right Right position, relative to parent
13569 * @param bottom Bottom position, relative to parent
13570 */
13571 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
13572 }
13573
13574 /**
13575 * Assign a size and position to this view.
13576 *
13577 * This is called from layout.
13578 *
13579 * @param left Left position, relative to parent
13580 * @param top Top position, relative to parent
13581 * @param right Right position, relative to parent
13582 * @param bottom Bottom position, relative to parent
13583 * @return true if the new size and position are different than the
13584 * previous ones
13585 * {@hide}
13586 */
13587 protected boolean setFrame(int left, int top, int right, int bottom) {
13588 boolean changed = false;
13589
13590 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070013591 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013592 + right + "," + bottom + ")");
13593 }
13594
13595 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
13596 changed = true;
13597
13598 // Remember our drawn bit
13599 int drawn = mPrivateFlags & DRAWN;
13600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013601 int oldWidth = mRight - mLeft;
13602 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070013603 int newWidth = right - left;
13604 int newHeight = bottom - top;
13605 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
13606
13607 // Invalidate our old position
13608 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013609
13610 mLeft = left;
13611 mTop = top;
13612 mRight = right;
13613 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -070013614 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013615 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
13616 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013617
13618 mPrivateFlags |= HAS_BOUNDS;
13619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013620
Chet Haase75755e22011-07-18 17:48:25 -070013621 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013622 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
13623 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013624 if (mTransformationInfo != null) {
13625 mTransformationInfo.mMatrixDirty = true;
13626 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013627 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013628 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
13629 }
13630
13631 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
13632 // If we are visible, force the DRAWN bit to on so that
13633 // this invalidate will go through (at least to our parent).
13634 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013635 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013636 // the DRAWN bit.
13637 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070013638 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080013639 // parent display list may need to be recreated based on a change in the bounds
13640 // of any child
13641 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013642 }
13643
13644 // Reset drawn bit to original value (invalidate turns it off)
13645 mPrivateFlags |= drawn;
13646
13647 mBackgroundSizeChanged = true;
13648 }
13649 return changed;
13650 }
13651
13652 /**
13653 * Finalize inflating a view from XML. This is called as the last phase
13654 * of inflation, after all child views have been added.
13655 *
13656 * <p>Even if the subclass overrides onFinishInflate, they should always be
13657 * sure to call the super method, so that we get called.
13658 */
13659 protected void onFinishInflate() {
13660 }
13661
13662 /**
13663 * Returns the resources associated with this view.
13664 *
13665 * @return Resources object.
13666 */
13667 public Resources getResources() {
13668 return mResources;
13669 }
13670
13671 /**
13672 * Invalidates the specified Drawable.
13673 *
13674 * @param drawable the drawable to invalidate
13675 */
13676 public void invalidateDrawable(Drawable drawable) {
13677 if (verifyDrawable(drawable)) {
13678 final Rect dirty = drawable.getBounds();
13679 final int scrollX = mScrollX;
13680 final int scrollY = mScrollY;
13681
13682 invalidate(dirty.left + scrollX, dirty.top + scrollY,
13683 dirty.right + scrollX, dirty.bottom + scrollY);
13684 }
13685 }
13686
13687 /**
13688 * Schedules an action on a drawable to occur at a specified time.
13689 *
13690 * @param who the recipient of the action
13691 * @param what the action to run on the drawable
13692 * @param when the time at which the action must occur. Uses the
13693 * {@link SystemClock#uptimeMillis} timebase.
13694 */
13695 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080013696 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013697 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080013698 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013699 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
13700 Choreographer.CALLBACK_ANIMATION, what, who,
13701 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080013702 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013703 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080013704 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013705 }
13706 }
13707
13708 /**
13709 * Cancels a scheduled action on a drawable.
13710 *
13711 * @param who the recipient of the action
13712 * @param what the action to cancel
13713 */
13714 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080013715 if (verifyDrawable(who) && what != null) {
13716 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013717 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13718 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080013719 } else {
13720 ViewRootImpl.getRunQueue().removeCallbacks(what);
13721 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013722 }
13723 }
13724
13725 /**
13726 * Unschedule any events associated with the given Drawable. This can be
13727 * used when selecting a new Drawable into a view, so that the previous
13728 * one is completely unscheduled.
13729 *
13730 * @param who The Drawable to unschedule.
13731 *
13732 * @see #drawableStateChanged
13733 */
13734 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080013735 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013736 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13737 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013738 }
13739 }
13740
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013741 /**
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070013742 * Resolve the Drawables depending on the layout direction. This is implicitly supposing
13743 * that the View directionality can and will be resolved before its Drawables.
13744 *
13745 * Will call {@link View#onResolveDrawables} when resolution is done.
13746 */
13747 public void resolveDrawables() {
13748 if (mBackground != null) {
13749 mBackground.setLayoutDirection(getResolvedLayoutDirection());
13750 }
13751 onResolveDrawables(getResolvedLayoutDirection());
13752 }
13753
13754 /**
13755 * Called when layout direction has been resolved.
13756 *
13757 * The default implementation does nothing.
13758 *
13759 * @param layoutDirection The resolved layout direction.
13760 *
13761 * @see {@link #LAYOUT_DIRECTION_LTR}
13762 * @see {@link #LAYOUT_DIRECTION_RTL}
13763 */
13764 public void onResolveDrawables(int layoutDirection) {
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070013765 }
13766
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013767 /**
13768 * If your view subclass is displaying its own Drawable objects, it should
13769 * override this function and return true for any Drawable it is
13770 * displaying. This allows animations for those drawables to be
13771 * scheduled.
13772 *
13773 * <p>Be sure to call through to the super class when overriding this
13774 * function.
13775 *
13776 * @param who The Drawable to verify. Return true if it is one you are
13777 * displaying, else return the result of calling through to the
13778 * super class.
13779 *
13780 * @return boolean If true than the Drawable is being displayed in the
13781 * view; else false and it is not allowed to animate.
13782 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013783 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
13784 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013785 */
13786 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013787 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013788 }
13789
13790 /**
13791 * This function is called whenever the state of the view changes in such
13792 * a way that it impacts the state of drawables being shown.
13793 *
13794 * <p>Be sure to call through to the superclass when overriding this
13795 * function.
13796 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013797 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013798 */
13799 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013800 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013801 if (d != null && d.isStateful()) {
13802 d.setState(getDrawableState());
13803 }
13804 }
13805
13806 /**
13807 * Call this to force a view to update its drawable state. This will cause
13808 * drawableStateChanged to be called on this view. Views that are interested
13809 * in the new state should call getDrawableState.
13810 *
13811 * @see #drawableStateChanged
13812 * @see #getDrawableState
13813 */
13814 public void refreshDrawableState() {
13815 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
13816 drawableStateChanged();
13817
13818 ViewParent parent = mParent;
13819 if (parent != null) {
13820 parent.childDrawableStateChanged(this);
13821 }
13822 }
13823
13824 /**
13825 * Return an array of resource IDs of the drawable states representing the
13826 * current state of the view.
13827 *
13828 * @return The current drawable state
13829 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013830 * @see Drawable#setState(int[])
13831 * @see #drawableStateChanged()
13832 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013833 */
13834 public final int[] getDrawableState() {
13835 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
13836 return mDrawableState;
13837 } else {
13838 mDrawableState = onCreateDrawableState(0);
13839 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
13840 return mDrawableState;
13841 }
13842 }
13843
13844 /**
13845 * Generate the new {@link android.graphics.drawable.Drawable} state for
13846 * this view. This is called by the view
13847 * system when the cached Drawable state is determined to be invalid. To
13848 * retrieve the current state, you should use {@link #getDrawableState}.
13849 *
13850 * @param extraSpace if non-zero, this is the number of extra entries you
13851 * would like in the returned array in which you can place your own
13852 * states.
13853 *
13854 * @return Returns an array holding the current {@link Drawable} state of
13855 * the view.
13856 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013857 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013858 */
13859 protected int[] onCreateDrawableState(int extraSpace) {
13860 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
13861 mParent instanceof View) {
13862 return ((View) mParent).onCreateDrawableState(extraSpace);
13863 }
13864
13865 int[] drawableState;
13866
13867 int privateFlags = mPrivateFlags;
13868
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013869 int viewStateIndex = 0;
13870 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
13871 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
13872 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070013873 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013874 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
13875 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070013876 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
13877 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080013878 // This is set if HW acceleration is requested, even if the current
13879 // process doesn't allow it. This is just to allow app preview
13880 // windows to better match their app.
13881 viewStateIndex |= VIEW_STATE_ACCELERATED;
13882 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070013883 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013884
Christopher Tate3d4bf172011-03-28 16:16:46 -070013885 final int privateFlags2 = mPrivateFlags2;
13886 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
13887 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
13888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013889 drawableState = VIEW_STATE_SETS[viewStateIndex];
13890
13891 //noinspection ConstantIfStatement
13892 if (false) {
13893 Log.i("View", "drawableStateIndex=" + viewStateIndex);
13894 Log.i("View", toString()
13895 + " pressed=" + ((privateFlags & PRESSED) != 0)
13896 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
13897 + " fo=" + hasFocus()
13898 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013899 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013900 + ": " + Arrays.toString(drawableState));
13901 }
13902
13903 if (extraSpace == 0) {
13904 return drawableState;
13905 }
13906
13907 final int[] fullState;
13908 if (drawableState != null) {
13909 fullState = new int[drawableState.length + extraSpace];
13910 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
13911 } else {
13912 fullState = new int[extraSpace];
13913 }
13914
13915 return fullState;
13916 }
13917
13918 /**
13919 * Merge your own state values in <var>additionalState</var> into the base
13920 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070013921 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013922 *
13923 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070013924 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013925 * own additional state values.
13926 *
13927 * @param additionalState The additional state values you would like
13928 * added to <var>baseState</var>; this array is not modified.
13929 *
13930 * @return As a convenience, the <var>baseState</var> array you originally
13931 * passed into the function is returned.
13932 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013933 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013934 */
13935 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
13936 final int N = baseState.length;
13937 int i = N - 1;
13938 while (i >= 0 && baseState[i] == 0) {
13939 i--;
13940 }
13941 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
13942 return baseState;
13943 }
13944
13945 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070013946 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
13947 * on all Drawable objects associated with this view.
13948 */
13949 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013950 if (mBackground != null) {
13951 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070013952 }
13953 }
13954
13955 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013956 * Sets the background color for this view.
13957 * @param color the color of the background
13958 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000013959 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013960 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013961 if (mBackground instanceof ColorDrawable) {
13962 ((ColorDrawable) mBackground).setColor(color);
Romain Guy7d3082a2012-07-11 17:52:54 -070013963 computeOpaqueFlags();
Chet Haase70d4ba12010-10-06 09:46:45 -070013964 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070013965 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070013966 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013967 }
13968
13969 /**
13970 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070013971 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013972 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070013973 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013974 * @attr ref android.R.styleable#View_background
13975 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000013976 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013977 public void setBackgroundResource(int resid) {
13978 if (resid != 0 && resid == mBackgroundResource) {
13979 return;
13980 }
13981
13982 Drawable d= null;
13983 if (resid != 0) {
13984 d = mResources.getDrawable(resid);
13985 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013986 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013987
13988 mBackgroundResource = resid;
13989 }
13990
13991 /**
13992 * Set the background to a given Drawable, or remove the background. If the
13993 * background has padding, this View's padding is set to the background's
13994 * padding. However, when a background is removed, this View's padding isn't
13995 * touched. If setting the padding is desired, please use
13996 * {@link #setPadding(int, int, int, int)}.
13997 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013998 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013999 * background
14000 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014001 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070014002 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070014003 setBackgroundDrawable(background);
14004 }
14005
14006 /**
14007 * @deprecated use {@link #setBackground(Drawable)} instead
14008 */
14009 @Deprecated
14010 public void setBackgroundDrawable(Drawable background) {
Romain Guy846a5332012-07-11 17:44:57 -070014011 computeOpaqueFlags();
14012
Philip Milne6c8ea062012-04-03 17:38:43 -070014013 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070014014 return;
14015 }
14016
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014017 boolean requestLayout = false;
14018
14019 mBackgroundResource = 0;
14020
14021 /*
14022 * Regardless of whether we're setting a new background or not, we want
14023 * to clear the previous drawable.
14024 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014025 if (mBackground != null) {
14026 mBackground.setCallback(null);
14027 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014028 }
14029
Philip Milne6c8ea062012-04-03 17:38:43 -070014030 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014031 Rect padding = sThreadLocal.get();
14032 if (padding == null) {
14033 padding = new Rect();
14034 sThreadLocal.set(padding);
14035 }
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014036 background.setLayoutDirection(getResolvedLayoutDirection());
Philip Milne6c8ea062012-04-03 17:38:43 -070014037 if (background.getPadding(padding)) {
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014038 switch (background.getLayoutDirection()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014039 case LAYOUT_DIRECTION_RTL:
14040 setPadding(padding.right, padding.top, padding.left, padding.bottom);
14041 break;
14042 case LAYOUT_DIRECTION_LTR:
14043 default:
14044 setPadding(padding.left, padding.top, padding.right, padding.bottom);
14045 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014046 }
14047
14048 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
14049 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070014050 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
14051 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014052 requestLayout = true;
14053 }
14054
Philip Milne6c8ea062012-04-03 17:38:43 -070014055 background.setCallback(this);
14056 if (background.isStateful()) {
14057 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014058 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014059 background.setVisible(getVisibility() == VISIBLE, false);
14060 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014061
14062 if ((mPrivateFlags & SKIP_DRAW) != 0) {
14063 mPrivateFlags &= ~SKIP_DRAW;
14064 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
14065 requestLayout = true;
14066 }
14067 } else {
14068 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070014069 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014070
14071 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
14072 /*
14073 * This view ONLY drew the background before and we're removing
14074 * the background, so now it won't draw anything
14075 * (hence we SKIP_DRAW)
14076 */
14077 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
14078 mPrivateFlags |= SKIP_DRAW;
14079 }
14080
14081 /*
14082 * When the background is set, we try to apply its padding to this
14083 * View. When the background is removed, we don't touch this View's
14084 * padding. This is noted in the Javadocs. Hence, we don't need to
14085 * requestLayout(), the invalidate() below is sufficient.
14086 */
14087
14088 // The old background's minimum size could have affected this
14089 // View's layout, so let's requestLayout
14090 requestLayout = true;
14091 }
14092
Romain Guy8f1344f52009-05-15 16:03:59 -070014093 computeOpaqueFlags();
14094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014095 if (requestLayout) {
14096 requestLayout();
14097 }
14098
14099 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014100 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014101 }
14102
14103 /**
14104 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070014105 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014106 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070014107 *
14108 * @see #setBackground(Drawable)
14109 *
14110 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014111 */
14112 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014113 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014114 }
14115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014116 /**
14117 * Sets the padding. The view may add on the space required to display
14118 * the scrollbars, depending on the style and visibility of the scrollbars.
14119 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
14120 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
14121 * from the values set in this call.
14122 *
14123 * @attr ref android.R.styleable#View_padding
14124 * @attr ref android.R.styleable#View_paddingBottom
14125 * @attr ref android.R.styleable#View_paddingLeft
14126 * @attr ref android.R.styleable#View_paddingRight
14127 * @attr ref android.R.styleable#View_paddingTop
14128 * @param left the left padding in pixels
14129 * @param top the top padding in pixels
14130 * @param right the right padding in pixels
14131 * @param bottom the bottom padding in pixels
14132 */
14133 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014134 mUserPaddingStart = -1;
14135 mUserPaddingEnd = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014136 mUserPaddingRelative = false;
14137
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014138 internalSetPadding(left, top, right, bottom);
14139 }
14140
14141 private void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080014142 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014143 mUserPaddingRight = right;
14144 mUserPaddingBottom = bottom;
14145
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014146 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014147 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070014148
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014149 // Common case is there are no scroll bars.
14150 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014151 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080014152 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014153 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080014154 switch (mVerticalScrollbarPosition) {
14155 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Meglioc91b6ca2012-06-22 14:51:15 -070014156 if (isLayoutRtl()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014157 left += offset;
14158 } else {
14159 right += offset;
14160 }
14161 break;
Adam Powell20232d02010-12-08 21:08:53 -080014162 case SCROLLBAR_POSITION_RIGHT:
14163 right += offset;
14164 break;
14165 case SCROLLBAR_POSITION_LEFT:
14166 left += offset;
14167 break;
14168 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014169 }
Adam Powell20232d02010-12-08 21:08:53 -080014170 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014171 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
14172 ? 0 : getHorizontalScrollbarHeight();
14173 }
14174 }
Romain Guy8506ab42009-06-11 17:35:47 -070014175
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014176 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014177 changed = true;
14178 mPaddingLeft = left;
14179 }
14180 if (mPaddingTop != top) {
14181 changed = true;
14182 mPaddingTop = top;
14183 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014184 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014185 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014186 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014187 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014188 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014189 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014190 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014191 }
14192
14193 if (changed) {
14194 requestLayout();
14195 }
14196 }
14197
14198 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014199 * Sets the relative padding. The view may add on the space required to display
14200 * the scrollbars, depending on the style and visibility of the scrollbars.
14201 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
14202 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
14203 * from the values set in this call.
14204 *
14205 * @attr ref android.R.styleable#View_padding
14206 * @attr ref android.R.styleable#View_paddingBottom
14207 * @attr ref android.R.styleable#View_paddingStart
14208 * @attr ref android.R.styleable#View_paddingEnd
14209 * @attr ref android.R.styleable#View_paddingTop
14210 * @param start the start padding in pixels
14211 * @param top the top padding in pixels
14212 * @param end the end padding in pixels
14213 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014214 */
14215 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014216 mUserPaddingStart = start;
14217 mUserPaddingEnd = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014218 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014219
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014220 switch(getResolvedLayoutDirection()) {
14221 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014222 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014223 break;
14224 case LAYOUT_DIRECTION_LTR:
14225 default:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014226 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014227 }
14228 }
14229
14230 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014231 * Returns the top padding of this view.
14232 *
14233 * @return the top padding in pixels
14234 */
14235 public int getPaddingTop() {
14236 return mPaddingTop;
14237 }
14238
14239 /**
14240 * Returns the bottom padding of this view. If there are inset and enabled
14241 * scrollbars, this value may include the space required to display the
14242 * scrollbars as well.
14243 *
14244 * @return the bottom padding in pixels
14245 */
14246 public int getPaddingBottom() {
14247 return mPaddingBottom;
14248 }
14249
14250 /**
14251 * Returns the left padding of this view. If there are inset and enabled
14252 * scrollbars, this value may include the space required to display the
14253 * scrollbars as well.
14254 *
14255 * @return the left padding in pixels
14256 */
14257 public int getPaddingLeft() {
14258 return mPaddingLeft;
14259 }
14260
14261 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014262 * Returns the start padding of this view depending on its resolved layout direction.
14263 * If there are inset and enabled scrollbars, this value may include the space
14264 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014265 *
14266 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014267 */
14268 public int getPaddingStart() {
14269 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14270 mPaddingRight : mPaddingLeft;
14271 }
14272
14273 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014274 * Returns the right padding of this view. If there are inset and enabled
14275 * scrollbars, this value may include the space required to display the
14276 * scrollbars as well.
14277 *
14278 * @return the right padding in pixels
14279 */
14280 public int getPaddingRight() {
14281 return mPaddingRight;
14282 }
14283
14284 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014285 * Returns the end padding of this view depending on its resolved layout direction.
14286 * If there are inset and enabled scrollbars, this value may include the space
14287 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014288 *
14289 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014290 */
14291 public int getPaddingEnd() {
14292 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14293 mPaddingLeft : mPaddingRight;
14294 }
14295
14296 /**
14297 * Return if the padding as been set thru relative values
14298 * {@link #setPaddingRelative(int, int, int, int)} or thru
14299 * @attr ref android.R.styleable#View_paddingStart or
14300 * @attr ref android.R.styleable#View_paddingEnd
14301 *
14302 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014303 */
14304 public boolean isPaddingRelative() {
14305 return mUserPaddingRelative;
14306 }
14307
14308 /**
Philip Milne1557fd72012-04-04 23:41:34 -070014309 * @hide
14310 */
Philip Milne7a23b492012-04-24 22:12:36 -070014311 public Insets getOpticalInsets() {
Philip Milne1557fd72012-04-04 23:41:34 -070014312 if (mLayoutInsets == null) {
Philip Milnebbd51f12012-04-18 15:09:05 -070014313 mLayoutInsets = (mBackground == null) ? Insets.NONE : mBackground.getLayoutInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070014314 }
14315 return mLayoutInsets;
14316 }
14317
14318 /**
14319 * @hide
14320 */
14321 public void setLayoutInsets(Insets layoutInsets) {
14322 mLayoutInsets = layoutInsets;
14323 }
14324
14325 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014326 * Changes the selection state of this view. A view can be selected or not.
14327 * Note that selection is not the same as focus. Views are typically
14328 * selected in the context of an AdapterView like ListView or GridView;
14329 * the selected view is the view that is highlighted.
14330 *
14331 * @param selected true if the view must be selected, false otherwise
14332 */
14333 public void setSelected(boolean selected) {
14334 if (((mPrivateFlags & SELECTED) != 0) != selected) {
14335 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070014336 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080014337 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014338 refreshDrawableState();
14339 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070014340 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
14341 notifyAccessibilityStateChanged();
14342 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014343 }
14344 }
14345
14346 /**
14347 * Dispatch setSelected to all of this View's children.
14348 *
14349 * @see #setSelected(boolean)
14350 *
14351 * @param selected The new selected state
14352 */
14353 protected void dispatchSetSelected(boolean selected) {
14354 }
14355
14356 /**
14357 * Indicates the selection state of this view.
14358 *
14359 * @return true if the view is selected, false otherwise
14360 */
14361 @ViewDebug.ExportedProperty
14362 public boolean isSelected() {
14363 return (mPrivateFlags & SELECTED) != 0;
14364 }
14365
14366 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014367 * Changes the activated state of this view. A view can be activated or not.
14368 * Note that activation is not the same as selection. Selection is
14369 * a transient property, representing the view (hierarchy) the user is
14370 * currently interacting with. Activation is a longer-term state that the
14371 * user can move views in and out of. For example, in a list view with
14372 * single or multiple selection enabled, the views in the current selection
14373 * set are activated. (Um, yeah, we are deeply sorry about the terminology
14374 * here.) The activated state is propagated down to children of the view it
14375 * is set on.
14376 *
14377 * @param activated true if the view must be activated, false otherwise
14378 */
14379 public void setActivated(boolean activated) {
14380 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
14381 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014382 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014383 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070014384 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014385 }
14386 }
14387
14388 /**
14389 * Dispatch setActivated to all of this View's children.
14390 *
14391 * @see #setActivated(boolean)
14392 *
14393 * @param activated The new activated state
14394 */
14395 protected void dispatchSetActivated(boolean activated) {
14396 }
14397
14398 /**
14399 * Indicates the activation state of this view.
14400 *
14401 * @return true if the view is activated, false otherwise
14402 */
14403 @ViewDebug.ExportedProperty
14404 public boolean isActivated() {
14405 return (mPrivateFlags & ACTIVATED) != 0;
14406 }
14407
14408 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014409 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
14410 * observer can be used to get notifications when global events, like
14411 * layout, happen.
14412 *
14413 * The returned ViewTreeObserver observer is not guaranteed to remain
14414 * valid for the lifetime of this View. If the caller of this method keeps
14415 * a long-lived reference to ViewTreeObserver, it should always check for
14416 * the return value of {@link ViewTreeObserver#isAlive()}.
14417 *
14418 * @return The ViewTreeObserver for this view's hierarchy.
14419 */
14420 public ViewTreeObserver getViewTreeObserver() {
14421 if (mAttachInfo != null) {
14422 return mAttachInfo.mTreeObserver;
14423 }
14424 if (mFloatingTreeObserver == null) {
14425 mFloatingTreeObserver = new ViewTreeObserver();
14426 }
14427 return mFloatingTreeObserver;
14428 }
14429
14430 /**
14431 * <p>Finds the topmost view in the current view hierarchy.</p>
14432 *
14433 * @return the topmost view containing this view
14434 */
14435 public View getRootView() {
14436 if (mAttachInfo != null) {
14437 final View v = mAttachInfo.mRootView;
14438 if (v != null) {
14439 return v;
14440 }
14441 }
Romain Guy8506ab42009-06-11 17:35:47 -070014442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014443 View parent = this;
14444
14445 while (parent.mParent != null && parent.mParent instanceof View) {
14446 parent = (View) parent.mParent;
14447 }
14448
14449 return parent;
14450 }
14451
14452 /**
14453 * <p>Computes the coordinates of this view on the screen. The argument
14454 * must be an array of two integers. After the method returns, the array
14455 * contains the x and y location in that order.</p>
14456 *
14457 * @param location an array of two integers in which to hold the coordinates
14458 */
14459 public void getLocationOnScreen(int[] location) {
14460 getLocationInWindow(location);
14461
14462 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070014463 if (info != null) {
14464 location[0] += info.mWindowLeft;
14465 location[1] += info.mWindowTop;
14466 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014467 }
14468
14469 /**
14470 * <p>Computes the coordinates of this view in its window. The argument
14471 * must be an array of two integers. After the method returns, the array
14472 * contains the x and y location in that order.</p>
14473 *
14474 * @param location an array of two integers in which to hold the coordinates
14475 */
14476 public void getLocationInWindow(int[] location) {
14477 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014478 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014479 }
14480
Gilles Debunne6583ce52011-12-06 18:09:02 -080014481 if (mAttachInfo == null) {
14482 // When the view is not attached to a window, this method does not make sense
14483 location[0] = location[1] = 0;
14484 return;
14485 }
14486
Gilles Debunnecea45132011-11-24 02:19:27 +010014487 float[] position = mAttachInfo.mTmpTransformLocation;
14488 position[0] = position[1] = 0.0f;
14489
14490 if (!hasIdentityMatrix()) {
14491 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014492 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014493
Gilles Debunnecea45132011-11-24 02:19:27 +010014494 position[0] += mLeft;
14495 position[1] += mTop;
14496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014497 ViewParent viewParent = mParent;
14498 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014499 final View view = (View) viewParent;
14500
14501 position[0] -= view.mScrollX;
14502 position[1] -= view.mScrollY;
14503
14504 if (!view.hasIdentityMatrix()) {
14505 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014506 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014507
14508 position[0] += view.mLeft;
14509 position[1] += view.mTop;
14510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014511 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070014512 }
Romain Guy8506ab42009-06-11 17:35:47 -070014513
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014514 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014515 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010014516 final ViewRootImpl vr = (ViewRootImpl) viewParent;
14517 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014518 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014519
14520 location[0] = (int) (position[0] + 0.5f);
14521 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014522 }
14523
14524 /**
14525 * {@hide}
14526 * @param id the id of the view to be found
14527 * @return the view of the specified id, null if cannot be found
14528 */
14529 protected View findViewTraversal(int id) {
14530 if (id == mID) {
14531 return this;
14532 }
14533 return null;
14534 }
14535
14536 /**
14537 * {@hide}
14538 * @param tag the tag of the view to be found
14539 * @return the view of specified tag, null if cannot be found
14540 */
14541 protected View findViewWithTagTraversal(Object tag) {
14542 if (tag != null && tag.equals(mTag)) {
14543 return this;
14544 }
14545 return null;
14546 }
14547
14548 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014549 * {@hide}
14550 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070014551 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080014552 * @return The first view that matches the predicate or null.
14553 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070014554 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080014555 if (predicate.apply(this)) {
14556 return this;
14557 }
14558 return null;
14559 }
14560
14561 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014562 * Look for a child view with the given id. If this view has the given
14563 * id, return this view.
14564 *
14565 * @param id The id to search for.
14566 * @return The view that has the given id in the hierarchy or null
14567 */
14568 public final View findViewById(int id) {
14569 if (id < 0) {
14570 return null;
14571 }
14572 return findViewTraversal(id);
14573 }
14574
14575 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070014576 * Finds a view by its unuque and stable accessibility id.
14577 *
14578 * @param accessibilityId The searched accessibility id.
14579 * @return The found view.
14580 */
14581 final View findViewByAccessibilityId(int accessibilityId) {
14582 if (accessibilityId < 0) {
14583 return null;
14584 }
14585 return findViewByAccessibilityIdTraversal(accessibilityId);
14586 }
14587
14588 /**
14589 * Performs the traversal to find a view by its unuque and stable accessibility id.
14590 *
14591 * <strong>Note:</strong>This method does not stop at the root namespace
14592 * boundary since the user can touch the screen at an arbitrary location
14593 * potentially crossing the root namespace bounday which will send an
14594 * accessibility event to accessibility services and they should be able
14595 * to obtain the event source. Also accessibility ids are guaranteed to be
14596 * unique in the window.
14597 *
14598 * @param accessibilityId The accessibility id.
14599 * @return The found view.
14600 */
14601 View findViewByAccessibilityIdTraversal(int accessibilityId) {
14602 if (getAccessibilityViewId() == accessibilityId) {
14603 return this;
14604 }
14605 return null;
14606 }
14607
14608 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014609 * Look for a child view with the given tag. If this view has the given
14610 * tag, return this view.
14611 *
14612 * @param tag The tag to search for, using "tag.equals(getTag())".
14613 * @return The View that has the given tag in the hierarchy or null
14614 */
14615 public final View findViewWithTag(Object tag) {
14616 if (tag == null) {
14617 return null;
14618 }
14619 return findViewWithTagTraversal(tag);
14620 }
14621
14622 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014623 * {@hide}
14624 * Look for a child view that matches the specified predicate.
14625 * If this view matches the predicate, return this view.
14626 *
14627 * @param predicate The predicate to evaluate.
14628 * @return The first view that matches the predicate or null.
14629 */
14630 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070014631 return findViewByPredicateTraversal(predicate, null);
14632 }
14633
14634 /**
14635 * {@hide}
14636 * Look for a child view that matches the specified predicate,
14637 * starting with the specified view and its descendents and then
14638 * recusively searching the ancestors and siblings of that view
14639 * until this view is reached.
14640 *
14641 * This method is useful in cases where the predicate does not match
14642 * a single unique view (perhaps multiple views use the same id)
14643 * and we are trying to find the view that is "closest" in scope to the
14644 * starting view.
14645 *
14646 * @param start The view to start from.
14647 * @param predicate The predicate to evaluate.
14648 * @return The first view that matches the predicate or null.
14649 */
14650 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
14651 View childToSkip = null;
14652 for (;;) {
14653 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
14654 if (view != null || start == this) {
14655 return view;
14656 }
14657
14658 ViewParent parent = start.getParent();
14659 if (parent == null || !(parent instanceof View)) {
14660 return null;
14661 }
14662
14663 childToSkip = start;
14664 start = (View) parent;
14665 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080014666 }
14667
14668 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014669 * Sets the identifier for this view. The identifier does not have to be
14670 * unique in this view's hierarchy. The identifier should be a positive
14671 * number.
14672 *
14673 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070014674 * @see #getId()
14675 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014676 *
14677 * @param id a number used to identify the view
14678 *
14679 * @attr ref android.R.styleable#View_id
14680 */
14681 public void setId(int id) {
14682 mID = id;
14683 }
14684
14685 /**
14686 * {@hide}
14687 *
14688 * @param isRoot true if the view belongs to the root namespace, false
14689 * otherwise
14690 */
14691 public void setIsRootNamespace(boolean isRoot) {
14692 if (isRoot) {
14693 mPrivateFlags |= IS_ROOT_NAMESPACE;
14694 } else {
14695 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
14696 }
14697 }
14698
14699 /**
14700 * {@hide}
14701 *
14702 * @return true if the view belongs to the root namespace, false otherwise
14703 */
14704 public boolean isRootNamespace() {
14705 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
14706 }
14707
14708 /**
14709 * Returns this view's identifier.
14710 *
14711 * @return a positive integer used to identify the view or {@link #NO_ID}
14712 * if the view has no ID
14713 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014714 * @see #setId(int)
14715 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014716 * @attr ref android.R.styleable#View_id
14717 */
14718 @ViewDebug.CapturedViewProperty
14719 public int getId() {
14720 return mID;
14721 }
14722
14723 /**
14724 * Returns this view's tag.
14725 *
14726 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070014727 *
14728 * @see #setTag(Object)
14729 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014730 */
14731 @ViewDebug.ExportedProperty
14732 public Object getTag() {
14733 return mTag;
14734 }
14735
14736 /**
14737 * Sets the tag associated with this view. A tag can be used to mark
14738 * a view in its hierarchy and does not have to be unique within the
14739 * hierarchy. Tags can also be used to store data within a view without
14740 * resorting to another data structure.
14741 *
14742 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070014743 *
14744 * @see #getTag()
14745 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014746 */
14747 public void setTag(final Object tag) {
14748 mTag = tag;
14749 }
14750
14751 /**
Romain Guyd90a3312009-05-06 14:54:28 -070014752 * Returns the tag associated with this view and the specified key.
14753 *
14754 * @param key The key identifying the tag
14755 *
14756 * @return the Object stored in this view as a tag
14757 *
14758 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070014759 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070014760 */
14761 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014762 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070014763 return null;
14764 }
14765
14766 /**
14767 * Sets a tag associated with this view and a key. A tag can be used
14768 * to mark a view in its hierarchy and does not have to be unique within
14769 * the hierarchy. Tags can also be used to store data within a view
14770 * without resorting to another data structure.
14771 *
14772 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070014773 * application to ensure it is unique (see the <a
14774 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
14775 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070014776 * the Android framework or not associated with any package will cause
14777 * an {@link IllegalArgumentException} to be thrown.
14778 *
14779 * @param key The key identifying the tag
14780 * @param tag An Object to tag the view with
14781 *
14782 * @throws IllegalArgumentException If they specified key is not valid
14783 *
14784 * @see #setTag(Object)
14785 * @see #getTag(int)
14786 */
14787 public void setTag(int key, final Object tag) {
14788 // If the package id is 0x00 or 0x01, it's either an undefined package
14789 // or a framework id
14790 if ((key >>> 24) < 2) {
14791 throw new IllegalArgumentException("The key must be an application-specific "
14792 + "resource id.");
14793 }
14794
Adam Powell2b2f6d62011-09-23 11:15:39 -070014795 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014796 }
14797
14798 /**
14799 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
14800 * framework id.
14801 *
14802 * @hide
14803 */
14804 public void setTagInternal(int key, Object tag) {
14805 if ((key >>> 24) != 0x1) {
14806 throw new IllegalArgumentException("The key must be a framework-specific "
14807 + "resource id.");
14808 }
14809
Adam Powell2b2f6d62011-09-23 11:15:39 -070014810 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014811 }
14812
Adam Powell2b2f6d62011-09-23 11:15:39 -070014813 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014814 if (mKeyedTags == null) {
14815 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070014816 }
14817
Adam Powell7db82ac2011-09-22 19:44:04 -070014818 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014819 }
14820
14821 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014822 * Prints information about this view in the log output, with the tag
14823 * {@link #VIEW_LOG_TAG}.
14824 *
14825 * @hide
14826 */
14827 public void debug() {
14828 debug(0);
14829 }
14830
14831 /**
14832 * Prints information about this view in the log output, with the tag
14833 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
14834 * indentation defined by the <code>depth</code>.
14835 *
14836 * @param depth the indentation level
14837 *
14838 * @hide
14839 */
14840 protected void debug(int depth) {
14841 String output = debugIndent(depth - 1);
14842
14843 output += "+ " + this;
14844 int id = getId();
14845 if (id != -1) {
14846 output += " (id=" + id + ")";
14847 }
14848 Object tag = getTag();
14849 if (tag != null) {
14850 output += " (tag=" + tag + ")";
14851 }
14852 Log.d(VIEW_LOG_TAG, output);
14853
14854 if ((mPrivateFlags & FOCUSED) != 0) {
14855 output = debugIndent(depth) + " FOCUSED";
14856 Log.d(VIEW_LOG_TAG, output);
14857 }
14858
14859 output = debugIndent(depth);
14860 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
14861 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
14862 + "} ";
14863 Log.d(VIEW_LOG_TAG, output);
14864
14865 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
14866 || mPaddingBottom != 0) {
14867 output = debugIndent(depth);
14868 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
14869 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
14870 Log.d(VIEW_LOG_TAG, output);
14871 }
14872
14873 output = debugIndent(depth);
14874 output += "mMeasureWidth=" + mMeasuredWidth +
14875 " mMeasureHeight=" + mMeasuredHeight;
14876 Log.d(VIEW_LOG_TAG, output);
14877
14878 output = debugIndent(depth);
14879 if (mLayoutParams == null) {
14880 output += "BAD! no layout params";
14881 } else {
14882 output = mLayoutParams.debug(output);
14883 }
14884 Log.d(VIEW_LOG_TAG, output);
14885
14886 output = debugIndent(depth);
14887 output += "flags={";
14888 output += View.printFlags(mViewFlags);
14889 output += "}";
14890 Log.d(VIEW_LOG_TAG, output);
14891
14892 output = debugIndent(depth);
14893 output += "privateFlags={";
14894 output += View.printPrivateFlags(mPrivateFlags);
14895 output += "}";
14896 Log.d(VIEW_LOG_TAG, output);
14897 }
14898
14899 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090014900 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014901 *
14902 * @param depth the indentation level
14903 * @return a String containing (depth * 2 + 3) * 2 white spaces
14904 *
14905 * @hide
14906 */
14907 protected static String debugIndent(int depth) {
14908 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
14909 for (int i = 0; i < (depth * 2) + 3; i++) {
14910 spaces.append(' ').append(' ');
14911 }
14912 return spaces.toString();
14913 }
14914
14915 /**
14916 * <p>Return the offset of the widget's text baseline from the widget's top
14917 * boundary. If this widget does not support baseline alignment, this
14918 * method returns -1. </p>
14919 *
14920 * @return the offset of the baseline within the widget's bounds or -1
14921 * if baseline alignment is not supported
14922 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070014923 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014924 public int getBaseline() {
14925 return -1;
14926 }
14927
14928 /**
14929 * Call this when something has changed which has invalidated the
14930 * layout of this view. This will schedule a layout pass of the view
14931 * tree.
14932 */
14933 public void requestLayout() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014934 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080014935 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014936
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070014937 if (mLayoutParams != null) {
14938 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
14939 }
14940
14941 if (mParent != null && !mParent.isLayoutRequested()) {
14942 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014943 }
14944 }
14945
14946 /**
14947 * Forces this view to be laid out during the next layout pass.
14948 * This method does not call requestLayout() or forceLayout()
14949 * on the parent.
14950 */
14951 public void forceLayout() {
14952 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080014953 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014954 }
14955
14956 /**
14957 * <p>
14958 * This is called to find out how big a view should be. The parent
14959 * supplies constraint information in the width and height parameters.
14960 * </p>
14961 *
14962 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080014963 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014964 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080014965 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014966 * </p>
14967 *
14968 *
14969 * @param widthMeasureSpec Horizontal space requirements as imposed by the
14970 * parent
14971 * @param heightMeasureSpec Vertical space requirements as imposed by the
14972 * parent
14973 *
14974 * @see #onMeasure(int, int)
14975 */
14976 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
14977 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
14978 widthMeasureSpec != mOldWidthMeasureSpec ||
14979 heightMeasureSpec != mOldHeightMeasureSpec) {
14980
14981 // first clears the measured dimension flag
14982 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
14983
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014984 // measure ourselves, this should set the measured dimension flag back
14985 onMeasure(widthMeasureSpec, heightMeasureSpec);
14986
14987 // flag not set, setMeasuredDimension() was not invoked, we raise
14988 // an exception to warn the developer
14989 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
14990 throw new IllegalStateException("onMeasure() did not set the"
14991 + " measured dimension by calling"
14992 + " setMeasuredDimension()");
14993 }
14994
14995 mPrivateFlags |= LAYOUT_REQUIRED;
14996 }
14997
14998 mOldWidthMeasureSpec = widthMeasureSpec;
14999 mOldHeightMeasureSpec = heightMeasureSpec;
15000 }
15001
15002 /**
15003 * <p>
15004 * Measure the view and its content to determine the measured width and the
15005 * measured height. This method is invoked by {@link #measure(int, int)} and
15006 * should be overriden by subclasses to provide accurate and efficient
15007 * measurement of their contents.
15008 * </p>
15009 *
15010 * <p>
15011 * <strong>CONTRACT:</strong> When overriding this method, you
15012 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
15013 * measured width and height of this view. Failure to do so will trigger an
15014 * <code>IllegalStateException</code>, thrown by
15015 * {@link #measure(int, int)}. Calling the superclass'
15016 * {@link #onMeasure(int, int)} is a valid use.
15017 * </p>
15018 *
15019 * <p>
15020 * The base class implementation of measure defaults to the background size,
15021 * unless a larger size is allowed by the MeasureSpec. Subclasses should
15022 * override {@link #onMeasure(int, int)} to provide better measurements of
15023 * their content.
15024 * </p>
15025 *
15026 * <p>
15027 * If this method is overridden, it is the subclass's responsibility to make
15028 * sure the measured height and width are at least the view's minimum height
15029 * and width ({@link #getSuggestedMinimumHeight()} and
15030 * {@link #getSuggestedMinimumWidth()}).
15031 * </p>
15032 *
15033 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
15034 * The requirements are encoded with
15035 * {@link android.view.View.MeasureSpec}.
15036 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
15037 * The requirements are encoded with
15038 * {@link android.view.View.MeasureSpec}.
15039 *
15040 * @see #getMeasuredWidth()
15041 * @see #getMeasuredHeight()
15042 * @see #setMeasuredDimension(int, int)
15043 * @see #getSuggestedMinimumHeight()
15044 * @see #getSuggestedMinimumWidth()
15045 * @see android.view.View.MeasureSpec#getMode(int)
15046 * @see android.view.View.MeasureSpec#getSize(int)
15047 */
15048 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
15049 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
15050 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
15051 }
15052
15053 /**
15054 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
15055 * measured width and measured height. Failing to do so will trigger an
15056 * exception at measurement time.</p>
15057 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080015058 * @param measuredWidth The measured width of this view. May be a complex
15059 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15060 * {@link #MEASURED_STATE_TOO_SMALL}.
15061 * @param measuredHeight The measured height of this view. May be a complex
15062 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15063 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015064 */
15065 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
15066 mMeasuredWidth = measuredWidth;
15067 mMeasuredHeight = measuredHeight;
15068
15069 mPrivateFlags |= MEASURED_DIMENSION_SET;
15070 }
15071
15072 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080015073 * Merge two states as returned by {@link #getMeasuredState()}.
15074 * @param curState The current state as returned from a view or the result
15075 * of combining multiple views.
15076 * @param newState The new view state to combine.
15077 * @return Returns a new integer reflecting the combination of the two
15078 * states.
15079 */
15080 public static int combineMeasuredStates(int curState, int newState) {
15081 return curState | newState;
15082 }
15083
15084 /**
15085 * Version of {@link #resolveSizeAndState(int, int, int)}
15086 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
15087 */
15088 public static int resolveSize(int size, int measureSpec) {
15089 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
15090 }
15091
15092 /**
15093 * Utility to reconcile a desired size and state, with constraints imposed
15094 * by a MeasureSpec. Will take the desired size, unless a different size
15095 * is imposed by the constraints. The returned value is a compound integer,
15096 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
15097 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
15098 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015099 *
15100 * @param size How big the view wants to be
15101 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080015102 * @return Size information bit mask as defined by
15103 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015104 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080015105 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015106 int result = size;
15107 int specMode = MeasureSpec.getMode(measureSpec);
15108 int specSize = MeasureSpec.getSize(measureSpec);
15109 switch (specMode) {
15110 case MeasureSpec.UNSPECIFIED:
15111 result = size;
15112 break;
15113 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080015114 if (specSize < size) {
15115 result = specSize | MEASURED_STATE_TOO_SMALL;
15116 } else {
15117 result = size;
15118 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015119 break;
15120 case MeasureSpec.EXACTLY:
15121 result = specSize;
15122 break;
15123 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080015124 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015125 }
15126
15127 /**
15128 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070015129 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015130 * by the MeasureSpec.
15131 *
15132 * @param size Default size for this view
15133 * @param measureSpec Constraints imposed by the parent
15134 * @return The size this view should be.
15135 */
15136 public static int getDefaultSize(int size, int measureSpec) {
15137 int result = size;
15138 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070015139 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015140
15141 switch (specMode) {
15142 case MeasureSpec.UNSPECIFIED:
15143 result = size;
15144 break;
15145 case MeasureSpec.AT_MOST:
15146 case MeasureSpec.EXACTLY:
15147 result = specSize;
15148 break;
15149 }
15150 return result;
15151 }
15152
15153 /**
15154 * Returns the suggested minimum height that the view should use. This
15155 * returns the maximum of the view's minimum height
15156 * and the background's minimum height
15157 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
15158 * <p>
15159 * When being used in {@link #onMeasure(int, int)}, the caller should still
15160 * ensure the returned height is within the requirements of the parent.
15161 *
15162 * @return The suggested minimum height of the view.
15163 */
15164 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015165 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015167 }
15168
15169 /**
15170 * Returns the suggested minimum width that the view should use. This
15171 * returns the maximum of the view's minimum width)
15172 * and the background's minimum width
15173 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
15174 * <p>
15175 * When being used in {@link #onMeasure(int, int)}, the caller should still
15176 * ensure the returned width is within the requirements of the parent.
15177 *
15178 * @return The suggested minimum width of the view.
15179 */
15180 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015181 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
15182 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015183
Philip Milne6c8ea062012-04-03 17:38:43 -070015184 /**
15185 * Returns the minimum height of the view.
15186 *
15187 * @return the minimum height the view will try to be.
15188 *
15189 * @see #setMinimumHeight(int)
15190 *
15191 * @attr ref android.R.styleable#View_minHeight
15192 */
15193 public int getMinimumHeight() {
15194 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015195 }
15196
15197 /**
15198 * Sets the minimum height of the view. It is not guaranteed the view will
15199 * be able to achieve this minimum height (for example, if its parent layout
15200 * constrains it with less available height).
15201 *
15202 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015203 *
15204 * @see #getMinimumHeight()
15205 *
15206 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015207 */
15208 public void setMinimumHeight(int minHeight) {
15209 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070015210 requestLayout();
15211 }
15212
15213 /**
15214 * Returns the minimum width of the view.
15215 *
15216 * @return the minimum width the view will try to be.
15217 *
15218 * @see #setMinimumWidth(int)
15219 *
15220 * @attr ref android.R.styleable#View_minWidth
15221 */
15222 public int getMinimumWidth() {
15223 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015224 }
15225
15226 /**
15227 * Sets the minimum width of the view. It is not guaranteed the view will
15228 * be able to achieve this minimum width (for example, if its parent layout
15229 * constrains it with less available width).
15230 *
15231 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015232 *
15233 * @see #getMinimumWidth()
15234 *
15235 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015236 */
15237 public void setMinimumWidth(int minWidth) {
15238 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070015239 requestLayout();
15240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015241 }
15242
15243 /**
15244 * Get the animation currently associated with this view.
15245 *
15246 * @return The animation that is currently playing or
15247 * scheduled to play for this view.
15248 */
15249 public Animation getAnimation() {
15250 return mCurrentAnimation;
15251 }
15252
15253 /**
15254 * Start the specified animation now.
15255 *
15256 * @param animation the animation to start now
15257 */
15258 public void startAnimation(Animation animation) {
15259 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
15260 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080015261 invalidateParentCaches();
15262 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015263 }
15264
15265 /**
15266 * Cancels any animations for this view.
15267 */
15268 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080015269 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080015270 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080015271 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015272 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080015273 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015274 }
15275
15276 /**
15277 * Sets the next animation to play for this view.
15278 * If you want the animation to play immediately, use
Chet Haase42428932012-05-11 15:39:07 -070015279 * {@link #startAnimation(android.view.animation.Animation)} instead.
15280 * This method provides allows fine-grained
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015281 * control over the start time and invalidation, but you
15282 * must make sure that 1) the animation has a start time set, and
Chet Haase42428932012-05-11 15:39:07 -070015283 * 2) the view's parent (which controls animations on its children)
15284 * will be invalidated when the animation is supposed to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015285 * start.
15286 *
15287 * @param animation The next animation, or null.
15288 */
15289 public void setAnimation(Animation animation) {
15290 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070015291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015292 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070015293 // If the screen is off assume the animation start time is now instead of
15294 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
15295 // would cause the animation to start when the screen turns back on
15296 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
15297 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
15298 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
15299 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015300 animation.reset();
15301 }
15302 }
15303
15304 /**
15305 * Invoked by a parent ViewGroup to notify the start of the animation
15306 * currently associated with this view. If you override this method,
15307 * always call super.onAnimationStart();
15308 *
15309 * @see #setAnimation(android.view.animation.Animation)
15310 * @see #getAnimation()
15311 */
15312 protected void onAnimationStart() {
15313 mPrivateFlags |= ANIMATION_STARTED;
15314 }
15315
15316 /**
15317 * Invoked by a parent ViewGroup to notify the end of the animation
15318 * currently associated with this view. If you override this method,
15319 * always call super.onAnimationEnd();
15320 *
15321 * @see #setAnimation(android.view.animation.Animation)
15322 * @see #getAnimation()
15323 */
15324 protected void onAnimationEnd() {
15325 mPrivateFlags &= ~ANIMATION_STARTED;
15326 }
15327
15328 /**
15329 * Invoked if there is a Transform that involves alpha. Subclass that can
15330 * draw themselves with the specified alpha should return true, and then
15331 * respect that alpha when their onDraw() is called. If this returns false
15332 * then the view may be redirected to draw into an offscreen buffer to
15333 * fulfill the request, which will look fine, but may be slower than if the
15334 * subclass handles it internally. The default implementation returns false.
15335 *
15336 * @param alpha The alpha (0..255) to apply to the view's drawing
15337 * @return true if the view can draw with the specified alpha.
15338 */
15339 protected boolean onSetAlpha(int alpha) {
15340 return false;
15341 }
15342
15343 /**
15344 * This is used by the RootView to perform an optimization when
15345 * the view hierarchy contains one or several SurfaceView.
15346 * SurfaceView is always considered transparent, but its children are not,
15347 * therefore all View objects remove themselves from the global transparent
15348 * region (passed as a parameter to this function).
15349 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015350 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015351 *
15352 * @return Returns true if the effective visibility of the view at this
15353 * point is opaque, regardless of the transparent region; returns false
15354 * if it is possible for underlying windows to be seen behind the view.
15355 *
15356 * {@hide}
15357 */
15358 public boolean gatherTransparentRegion(Region region) {
15359 final AttachInfo attachInfo = mAttachInfo;
15360 if (region != null && attachInfo != null) {
15361 final int pflags = mPrivateFlags;
15362 if ((pflags & SKIP_DRAW) == 0) {
15363 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
15364 // remove it from the transparent region.
15365 final int[] location = attachInfo.mTransparentLocation;
15366 getLocationInWindow(location);
15367 region.op(location[0], location[1], location[0] + mRight - mLeft,
15368 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Philip Milne6c8ea062012-04-03 17:38:43 -070015369 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015370 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
15371 // exists, so we remove the background drawable's non-transparent
15372 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070015373 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015374 }
15375 }
15376 return true;
15377 }
15378
15379 /**
15380 * Play a sound effect for this view.
15381 *
15382 * <p>The framework will play sound effects for some built in actions, such as
15383 * clicking, but you may wish to play these effects in your widget,
15384 * for instance, for internal navigation.
15385 *
15386 * <p>The sound effect will only be played if sound effects are enabled by the user, and
15387 * {@link #isSoundEffectsEnabled()} is true.
15388 *
15389 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
15390 */
15391 public void playSoundEffect(int soundConstant) {
15392 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
15393 return;
15394 }
15395 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
15396 }
15397
15398 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015399 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015400 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015401 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015402 *
15403 * <p>The framework will provide haptic feedback for some built in actions,
15404 * such as long presses, but you may wish to provide feedback for your
15405 * own widget.
15406 *
15407 * <p>The feedback will only be performed if
15408 * {@link #isHapticFeedbackEnabled()} is true.
15409 *
15410 * @param feedbackConstant One of the constants defined in
15411 * {@link HapticFeedbackConstants}
15412 */
15413 public boolean performHapticFeedback(int feedbackConstant) {
15414 return performHapticFeedback(feedbackConstant, 0);
15415 }
15416
15417 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015418 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015419 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015420 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015421 *
15422 * @param feedbackConstant One of the constants defined in
15423 * {@link HapticFeedbackConstants}
15424 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
15425 */
15426 public boolean performHapticFeedback(int feedbackConstant, int flags) {
15427 if (mAttachInfo == null) {
15428 return false;
15429 }
Romain Guyf607bdc2010-09-10 19:20:06 -070015430 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070015431 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015432 && !isHapticFeedbackEnabled()) {
15433 return false;
15434 }
Romain Guy812ccbe2010-06-01 14:07:24 -070015435 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
15436 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015437 }
15438
15439 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015440 * Request that the visibility of the status bar or other screen/window
15441 * decorations be changed.
15442 *
15443 * <p>This method is used to put the over device UI into temporary modes
15444 * where the user's attention is focused more on the application content,
15445 * by dimming or hiding surrounding system affordances. This is typically
15446 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
15447 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
15448 * to be placed behind the action bar (and with these flags other system
15449 * affordances) so that smooth transitions between hiding and showing them
15450 * can be done.
15451 *
15452 * <p>Two representative examples of the use of system UI visibility is
15453 * implementing a content browsing application (like a magazine reader)
15454 * and a video playing application.
15455 *
15456 * <p>The first code shows a typical implementation of a View in a content
15457 * browsing application. In this implementation, the application goes
15458 * into a content-oriented mode by hiding the status bar and action bar,
15459 * and putting the navigation elements into lights out mode. The user can
15460 * then interact with content while in this mode. Such an application should
15461 * provide an easy way for the user to toggle out of the mode (such as to
15462 * check information in the status bar or access notifications). In the
15463 * implementation here, this is done simply by tapping on the content.
15464 *
15465 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
15466 * content}
15467 *
15468 * <p>This second code sample shows a typical implementation of a View
15469 * in a video playing application. In this situation, while the video is
15470 * playing the application would like to go into a complete full-screen mode,
15471 * to use as much of the display as possible for the video. When in this state
15472 * the user can not interact with the application; the system intercepts
Dianne Hackborncf675782012-05-10 15:07:24 -070015473 * touching on the screen to pop the UI out of full screen mode. See
15474 * {@link #fitSystemWindows(Rect)} for a sample layout that goes with this code.
Dianne Hackborn98014352012-04-05 18:31:41 -070015475 *
15476 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
15477 * content}
15478 *
15479 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15480 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15481 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15482 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015483 */
15484 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040015485 if (visibility != mSystemUiVisibility) {
15486 mSystemUiVisibility = visibility;
15487 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15488 mParent.recomputeViewAttributes(this);
15489 }
Joe Onorato664644d2011-01-23 17:53:23 -080015490 }
15491 }
15492
15493 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015494 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
15495 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15496 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15497 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15498 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015499 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080015500 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080015501 return mSystemUiVisibility;
15502 }
15503
Scott Mainec6331b2011-05-24 16:55:56 -070015504 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070015505 * Returns the current system UI visibility that is currently set for
15506 * the entire window. This is the combination of the
15507 * {@link #setSystemUiVisibility(int)} values supplied by all of the
15508 * views in the window.
15509 */
15510 public int getWindowSystemUiVisibility() {
15511 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
15512 }
15513
15514 /**
15515 * Override to find out when the window's requested system UI visibility
15516 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
15517 * This is different from the callbacks recieved through
15518 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
15519 * in that this is only telling you about the local request of the window,
15520 * not the actual values applied by the system.
15521 */
15522 public void onWindowSystemUiVisibilityChanged(int visible) {
15523 }
15524
15525 /**
15526 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
15527 * the view hierarchy.
15528 */
15529 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
15530 onWindowSystemUiVisibilityChanged(visible);
15531 }
15532
15533 /**
Scott Mainec6331b2011-05-24 16:55:56 -070015534 * Set a listener to receive callbacks when the visibility of the system bar changes.
15535 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
15536 */
Joe Onorato664644d2011-01-23 17:53:23 -080015537 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015538 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080015539 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15540 mParent.recomputeViewAttributes(this);
15541 }
15542 }
15543
15544 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015545 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
15546 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080015547 */
15548 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015549 ListenerInfo li = mListenerInfo;
15550 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
15551 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080015552 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080015553 }
15554 }
15555
Dianne Hackborncf675782012-05-10 15:07:24 -070015556 boolean updateLocalSystemUiVisibility(int localValue, int localChanges) {
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015557 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
15558 if (val != mSystemUiVisibility) {
15559 setSystemUiVisibility(val);
Dianne Hackborncf675782012-05-10 15:07:24 -070015560 return true;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015561 }
Dianne Hackborncf675782012-05-10 15:07:24 -070015562 return false;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015563 }
15564
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070015565 /** @hide */
15566 public void setDisabledSystemUiVisibility(int flags) {
15567 if (mAttachInfo != null) {
15568 if (mAttachInfo.mDisabledSystemUiVisibility != flags) {
15569 mAttachInfo.mDisabledSystemUiVisibility = flags;
15570 if (mParent != null) {
15571 mParent.recomputeViewAttributes(this);
15572 }
15573 }
15574 }
15575 }
15576
Joe Onorato664644d2011-01-23 17:53:23 -080015577 /**
Joe Malin32736f02011-01-19 16:14:20 -080015578 * Creates an image that the system displays during the drag and drop
15579 * operation. This is called a &quot;drag shadow&quot;. The default implementation
15580 * for a DragShadowBuilder based on a View returns an image that has exactly the same
15581 * appearance as the given View. The default also positions the center of the drag shadow
15582 * directly under the touch point. If no View is provided (the constructor with no parameters
15583 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
15584 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
15585 * default is an invisible drag shadow.
15586 * <p>
15587 * You are not required to use the View you provide to the constructor as the basis of the
15588 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
15589 * anything you want as the drag shadow.
15590 * </p>
15591 * <p>
15592 * You pass a DragShadowBuilder object to the system when you start the drag. The system
15593 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
15594 * size and position of the drag shadow. It uses this data to construct a
15595 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
15596 * so that your application can draw the shadow image in the Canvas.
15597 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070015598 *
15599 * <div class="special reference">
15600 * <h3>Developer Guides</h3>
15601 * <p>For a guide to implementing drag and drop features, read the
15602 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
15603 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070015604 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015605 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070015606 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070015607
15608 /**
Joe Malin32736f02011-01-19 16:14:20 -080015609 * Constructs a shadow image builder based on a View. By default, the resulting drag
15610 * shadow will have the same appearance and dimensions as the View, with the touch point
15611 * over the center of the View.
15612 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070015613 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015614 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070015615 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070015616 }
15617
Christopher Tate17ed60c2011-01-18 12:50:26 -080015618 /**
15619 * Construct a shadow builder object with no associated View. This
15620 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
15621 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
15622 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080015623 * reference to any View object. If they are not overridden, then the result is an
15624 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080015625 */
15626 public DragShadowBuilder() {
15627 mView = new WeakReference<View>(null);
15628 }
15629
15630 /**
15631 * Returns the View object that had been passed to the
15632 * {@link #View.DragShadowBuilder(View)}
15633 * constructor. If that View parameter was {@code null} or if the
15634 * {@link #View.DragShadowBuilder()}
15635 * constructor was used to instantiate the builder object, this method will return
15636 * null.
15637 *
15638 * @return The View object associate with this builder object.
15639 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070015640 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070015641 final public View getView() {
15642 return mView.get();
15643 }
15644
Christopher Tate2c095f32010-10-04 14:13:40 -070015645 /**
Joe Malin32736f02011-01-19 16:14:20 -080015646 * Provides the metrics for the shadow image. These include the dimensions of
15647 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070015648 * be centered under the touch location while dragging.
15649 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015650 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080015651 * same as the dimensions of the View itself and centers the shadow under
15652 * the touch point.
15653 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070015654 *
Joe Malin32736f02011-01-19 16:14:20 -080015655 * @param shadowSize A {@link android.graphics.Point} containing the width and height
15656 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
15657 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
15658 * image.
15659 *
15660 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
15661 * shadow image that should be underneath the touch point during the drag and drop
15662 * operation. Your application must set {@link android.graphics.Point#x} to the
15663 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070015664 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015665 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070015666 final View view = mView.get();
15667 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015668 shadowSize.set(view.getWidth(), view.getHeight());
15669 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070015670 } else {
15671 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
15672 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015673 }
15674
15675 /**
Joe Malin32736f02011-01-19 16:14:20 -080015676 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
15677 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015678 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070015679 *
Joe Malin32736f02011-01-19 16:14:20 -080015680 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070015681 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015682 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070015683 final View view = mView.get();
15684 if (view != null) {
15685 view.draw(canvas);
15686 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015687 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070015688 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015689 }
15690 }
15691
15692 /**
Joe Malin32736f02011-01-19 16:14:20 -080015693 * Starts a drag and drop operation. When your application calls this method, it passes a
15694 * {@link android.view.View.DragShadowBuilder} object to the system. The
15695 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
15696 * to get metrics for the drag shadow, and then calls the object's
15697 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
15698 * <p>
15699 * Once the system has the drag shadow, it begins the drag and drop operation by sending
15700 * drag events to all the View objects in your application that are currently visible. It does
15701 * this either by calling the View object's drag listener (an implementation of
15702 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
15703 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
15704 * Both are passed a {@link android.view.DragEvent} object that has a
15705 * {@link android.view.DragEvent#getAction()} value of
15706 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
15707 * </p>
15708 * <p>
15709 * Your application can invoke startDrag() on any attached View object. The View object does not
15710 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
15711 * be related to the View the user selected for dragging.
15712 * </p>
15713 * @param data A {@link android.content.ClipData} object pointing to the data to be
15714 * transferred by the drag and drop operation.
15715 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
15716 * drag shadow.
15717 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
15718 * drop operation. This Object is put into every DragEvent object sent by the system during the
15719 * current drag.
15720 * <p>
15721 * myLocalState is a lightweight mechanism for the sending information from the dragged View
15722 * to the target Views. For example, it can contain flags that differentiate between a
15723 * a copy operation and a move operation.
15724 * </p>
15725 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
15726 * so the parameter should be set to 0.
15727 * @return {@code true} if the method completes successfully, or
15728 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
15729 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070015730 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015731 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015732 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070015733 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015734 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070015735 }
15736 boolean okay = false;
15737
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015738 Point shadowSize = new Point();
15739 Point shadowTouchPoint = new Point();
15740 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070015741
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015742 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
15743 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
15744 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070015745 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015746
Chris Tatea32dcf72010-10-14 12:13:50 -070015747 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015748 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
15749 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070015750 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015751 Surface surface = new Surface();
15752 try {
15753 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015754 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070015755 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070015756 + " surface=" + surface);
15757 if (token != null) {
15758 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070015759 try {
Chris Tate6b391282010-10-14 15:48:59 -070015760 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015761 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070015762 } finally {
15763 surface.unlockCanvasAndPost(canvas);
15764 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015765
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015766 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080015767
15768 // Cache the local state object for delivery with DragEvents
15769 root.setLocalDragState(myLocalState);
15770
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015771 // repurpose 'shadowSize' for the last touch point
15772 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070015773
Christopher Tatea53146c2010-09-07 11:57:52 -070015774 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015775 shadowSize.x, shadowSize.y,
15776 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070015777 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070015778
15779 // Off and running! Release our local surface instance; the drag
15780 // shadow surface is now managed by the system process.
15781 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070015782 }
15783 } catch (Exception e) {
15784 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
15785 surface.destroy();
15786 }
15787
15788 return okay;
15789 }
15790
Christopher Tatea53146c2010-09-07 11:57:52 -070015791 /**
Joe Malin32736f02011-01-19 16:14:20 -080015792 * Handles drag events sent by the system following a call to
15793 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
15794 *<p>
15795 * When the system calls this method, it passes a
15796 * {@link android.view.DragEvent} object. A call to
15797 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
15798 * in DragEvent. The method uses these to determine what is happening in the drag and drop
15799 * operation.
15800 * @param event The {@link android.view.DragEvent} sent by the system.
15801 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
15802 * in DragEvent, indicating the type of drag event represented by this object.
15803 * @return {@code true} if the method was successful, otherwise {@code false}.
15804 * <p>
15805 * The method should return {@code true} in response to an action type of
15806 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
15807 * operation.
15808 * </p>
15809 * <p>
15810 * The method should also return {@code true} in response to an action type of
15811 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
15812 * {@code false} if it didn't.
15813 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015814 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070015815 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070015816 return false;
15817 }
15818
15819 /**
Joe Malin32736f02011-01-19 16:14:20 -080015820 * Detects if this View is enabled and has a drag event listener.
15821 * If both are true, then it calls the drag event listener with the
15822 * {@link android.view.DragEvent} it received. If the drag event listener returns
15823 * {@code true}, then dispatchDragEvent() returns {@code true}.
15824 * <p>
15825 * For all other cases, the method calls the
15826 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
15827 * method and returns its result.
15828 * </p>
15829 * <p>
15830 * This ensures that a drag event is always consumed, even if the View does not have a drag
15831 * event listener. However, if the View has a listener and the listener returns true, then
15832 * onDragEvent() is not called.
15833 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015834 */
15835 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080015836 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015837 ListenerInfo li = mListenerInfo;
15838 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
15839 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070015840 return true;
15841 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015842 return onDragEvent(event);
15843 }
15844
Christopher Tate3d4bf172011-03-28 16:16:46 -070015845 boolean canAcceptDrag() {
15846 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
15847 }
15848
Christopher Tatea53146c2010-09-07 11:57:52 -070015849 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070015850 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
15851 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070015852 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070015853 */
15854 public void onCloseSystemDialogs(String reason) {
15855 }
Joe Malin32736f02011-01-19 16:14:20 -080015856
Dianne Hackbornffa42482009-09-23 22:20:11 -070015857 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015858 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070015859 * update a Region being computed for
15860 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015861 * that any non-transparent parts of the Drawable are removed from the
15862 * given transparent region.
15863 *
15864 * @param dr The Drawable whose transparency is to be applied to the region.
15865 * @param region A Region holding the current transparency information,
15866 * where any parts of the region that are set are considered to be
15867 * transparent. On return, this region will be modified to have the
15868 * transparency information reduced by the corresponding parts of the
15869 * Drawable that are not transparent.
15870 * {@hide}
15871 */
15872 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
15873 if (DBG) {
15874 Log.i("View", "Getting transparent region for: " + this);
15875 }
15876 final Region r = dr.getTransparentRegion();
15877 final Rect db = dr.getBounds();
15878 final AttachInfo attachInfo = mAttachInfo;
15879 if (r != null && attachInfo != null) {
15880 final int w = getRight()-getLeft();
15881 final int h = getBottom()-getTop();
15882 if (db.left > 0) {
15883 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
15884 r.op(0, 0, db.left, h, Region.Op.UNION);
15885 }
15886 if (db.right < w) {
15887 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
15888 r.op(db.right, 0, w, h, Region.Op.UNION);
15889 }
15890 if (db.top > 0) {
15891 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
15892 r.op(0, 0, w, db.top, Region.Op.UNION);
15893 }
15894 if (db.bottom < h) {
15895 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
15896 r.op(0, db.bottom, w, h, Region.Op.UNION);
15897 }
15898 final int[] location = attachInfo.mTransparentLocation;
15899 getLocationInWindow(location);
15900 r.translate(location[0], location[1]);
15901 region.op(r, Region.Op.INTERSECT);
15902 } else {
15903 region.op(db, Region.Op.DIFFERENCE);
15904 }
15905 }
15906
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015907 private void checkForLongClick(int delayOffset) {
15908 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
15909 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015910
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015911 if (mPendingCheckForLongPress == null) {
15912 mPendingCheckForLongPress = new CheckForLongPress();
15913 }
15914 mPendingCheckForLongPress.rememberWindowAttachCount();
15915 postDelayed(mPendingCheckForLongPress,
15916 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015917 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015918 }
15919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015920 /**
15921 * Inflate a view from an XML resource. This convenience method wraps the {@link
15922 * LayoutInflater} class, which provides a full range of options for view inflation.
15923 *
15924 * @param context The Context object for your activity or application.
15925 * @param resource The resource ID to inflate
15926 * @param root A view group that will be the parent. Used to properly inflate the
15927 * layout_* parameters.
15928 * @see LayoutInflater
15929 */
15930 public static View inflate(Context context, int resource, ViewGroup root) {
15931 LayoutInflater factory = LayoutInflater.from(context);
15932 return factory.inflate(resource, root);
15933 }
Romain Guy33e72ae2010-07-17 12:40:29 -070015934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015935 /**
Adam Powell637d3372010-08-25 14:37:03 -070015936 * Scroll the view with standard behavior for scrolling beyond the normal
15937 * content boundaries. Views that call this method should override
15938 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
15939 * results of an over-scroll operation.
15940 *
15941 * Views can use this method to handle any touch or fling-based scrolling.
15942 *
15943 * @param deltaX Change in X in pixels
15944 * @param deltaY Change in Y in pixels
15945 * @param scrollX Current X scroll value in pixels before applying deltaX
15946 * @param scrollY Current Y scroll value in pixels before applying deltaY
15947 * @param scrollRangeX Maximum content scroll range along the X axis
15948 * @param scrollRangeY Maximum content scroll range along the Y axis
15949 * @param maxOverScrollX Number of pixels to overscroll by in either direction
15950 * along the X axis.
15951 * @param maxOverScrollY Number of pixels to overscroll by in either direction
15952 * along the Y axis.
15953 * @param isTouchEvent true if this scroll operation is the result of a touch event.
15954 * @return true if scrolling was clamped to an over-scroll boundary along either
15955 * axis, false otherwise.
15956 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070015957 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070015958 protected boolean overScrollBy(int deltaX, int deltaY,
15959 int scrollX, int scrollY,
15960 int scrollRangeX, int scrollRangeY,
15961 int maxOverScrollX, int maxOverScrollY,
15962 boolean isTouchEvent) {
15963 final int overScrollMode = mOverScrollMode;
15964 final boolean canScrollHorizontal =
15965 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
15966 final boolean canScrollVertical =
15967 computeVerticalScrollRange() > computeVerticalScrollExtent();
15968 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
15969 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
15970 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
15971 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
15972
15973 int newScrollX = scrollX + deltaX;
15974 if (!overScrollHorizontal) {
15975 maxOverScrollX = 0;
15976 }
15977
15978 int newScrollY = scrollY + deltaY;
15979 if (!overScrollVertical) {
15980 maxOverScrollY = 0;
15981 }
15982
15983 // Clamp values if at the limits and record
15984 final int left = -maxOverScrollX;
15985 final int right = maxOverScrollX + scrollRangeX;
15986 final int top = -maxOverScrollY;
15987 final int bottom = maxOverScrollY + scrollRangeY;
15988
15989 boolean clampedX = false;
15990 if (newScrollX > right) {
15991 newScrollX = right;
15992 clampedX = true;
15993 } else if (newScrollX < left) {
15994 newScrollX = left;
15995 clampedX = true;
15996 }
15997
15998 boolean clampedY = false;
15999 if (newScrollY > bottom) {
16000 newScrollY = bottom;
16001 clampedY = true;
16002 } else if (newScrollY < top) {
16003 newScrollY = top;
16004 clampedY = true;
16005 }
16006
16007 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
16008
16009 return clampedX || clampedY;
16010 }
16011
16012 /**
16013 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
16014 * respond to the results of an over-scroll operation.
16015 *
16016 * @param scrollX New X scroll value in pixels
16017 * @param scrollY New Y scroll value in pixels
16018 * @param clampedX True if scrollX was clamped to an over-scroll boundary
16019 * @param clampedY True if scrollY was clamped to an over-scroll boundary
16020 */
16021 protected void onOverScrolled(int scrollX, int scrollY,
16022 boolean clampedX, boolean clampedY) {
16023 // Intentionally empty.
16024 }
16025
16026 /**
16027 * Returns the over-scroll mode for this view. The result will be
16028 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16029 * (allow over-scrolling only if the view content is larger than the container),
16030 * or {@link #OVER_SCROLL_NEVER}.
16031 *
16032 * @return This view's over-scroll mode.
16033 */
16034 public int getOverScrollMode() {
16035 return mOverScrollMode;
16036 }
16037
16038 /**
16039 * Set the over-scroll mode for this view. Valid over-scroll modes are
16040 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16041 * (allow over-scrolling only if the view content is larger than the container),
16042 * or {@link #OVER_SCROLL_NEVER}.
16043 *
16044 * Setting the over-scroll mode of a view will have an effect only if the
16045 * view is capable of scrolling.
16046 *
16047 * @param overScrollMode The new over-scroll mode for this view.
16048 */
16049 public void setOverScrollMode(int overScrollMode) {
16050 if (overScrollMode != OVER_SCROLL_ALWAYS &&
16051 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
16052 overScrollMode != OVER_SCROLL_NEVER) {
16053 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
16054 }
16055 mOverScrollMode = overScrollMode;
16056 }
16057
16058 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016059 * Gets a scale factor that determines the distance the view should scroll
16060 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
16061 * @return The vertical scroll scale factor.
16062 * @hide
16063 */
16064 protected float getVerticalScrollFactor() {
16065 if (mVerticalScrollFactor == 0) {
16066 TypedValue outValue = new TypedValue();
16067 if (!mContext.getTheme().resolveAttribute(
16068 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
16069 throw new IllegalStateException(
16070 "Expected theme to define listPreferredItemHeight.");
16071 }
16072 mVerticalScrollFactor = outValue.getDimension(
16073 mContext.getResources().getDisplayMetrics());
16074 }
16075 return mVerticalScrollFactor;
16076 }
16077
16078 /**
16079 * Gets a scale factor that determines the distance the view should scroll
16080 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
16081 * @return The horizontal scroll scale factor.
16082 * @hide
16083 */
16084 protected float getHorizontalScrollFactor() {
16085 // TODO: Should use something else.
16086 return getVerticalScrollFactor();
16087 }
16088
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016089 /**
16090 * Return the value specifying the text direction or policy that was set with
16091 * {@link #setTextDirection(int)}.
16092 *
16093 * @return the defined text direction. It can be one of:
16094 *
16095 * {@link #TEXT_DIRECTION_INHERIT},
16096 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16097 * {@link #TEXT_DIRECTION_ANY_RTL},
16098 * {@link #TEXT_DIRECTION_LTR},
16099 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016100 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016101 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016102 @ViewDebug.ExportedProperty(category = "text", mapping = {
16103 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
16104 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
16105 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
16106 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
16107 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
16108 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
16109 })
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016110 public int getTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016111 return (mPrivateFlags2 & TEXT_DIRECTION_MASK) >> TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016112 }
16113
16114 /**
16115 * Set the text direction.
16116 *
16117 * @param textDirection the direction to set. Should be one of:
16118 *
16119 * {@link #TEXT_DIRECTION_INHERIT},
16120 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16121 * {@link #TEXT_DIRECTION_ANY_RTL},
16122 * {@link #TEXT_DIRECTION_LTR},
16123 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016124 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016125 */
16126 public void setTextDirection(int textDirection) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016127 if (getTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016128 // Reset the current text direction and the resolved one
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016129 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016130 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016131 // Set the new text direction
16132 mPrivateFlags2 |= ((textDirection << TEXT_DIRECTION_MASK_SHIFT) & TEXT_DIRECTION_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016133 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016134 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016135 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016136 }
16137 }
16138
16139 /**
16140 * Return the resolved text direction.
16141 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016142 * This needs resolution if the value is TEXT_DIRECTION_INHERIT. The resolution matches
16143 * {@link #getTextDirection()}if it is not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds
16144 * up the parent chain of the view. if there is no parent, then it will return the default
16145 * {@link #TEXT_DIRECTION_FIRST_STRONG}.
16146 *
16147 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016148 *
Doug Feltcb3791202011-07-07 11:57:48 -070016149 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16150 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016151 * {@link #TEXT_DIRECTION_LTR},
16152 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016153 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016154 */
16155 public int getResolvedTextDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070016156 // The text direction will be resolved only if needed
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016157 if ((mPrivateFlags2 & TEXT_DIRECTION_RESOLVED) != TEXT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016158 resolveTextDirection();
16159 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016160 return (mPrivateFlags2 & TEXT_DIRECTION_RESOLVED_MASK) >> TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016161 }
16162
16163 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016164 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
16165 * resolution is done.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016166 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016167 public void resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016168 // Reset any previous text direction resolution
16169 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
16170
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016171 if (hasRtlSupport()) {
16172 // Set resolved text direction flag depending on text direction flag
16173 final int textDirection = getTextDirection();
16174 switch(textDirection) {
16175 case TEXT_DIRECTION_INHERIT:
16176 if (canResolveTextDirection()) {
16177 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016178
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016179 // Set current resolved direction to the same value as the parent's one
16180 final int parentResolvedDirection = viewGroup.getResolvedTextDirection();
16181 switch (parentResolvedDirection) {
16182 case TEXT_DIRECTION_FIRST_STRONG:
16183 case TEXT_DIRECTION_ANY_RTL:
16184 case TEXT_DIRECTION_LTR:
16185 case TEXT_DIRECTION_RTL:
16186 case TEXT_DIRECTION_LOCALE:
16187 mPrivateFlags2 |=
16188 (parentResolvedDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16189 break;
16190 default:
16191 // Default resolved direction is "first strong" heuristic
16192 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
16193 }
16194 } else {
16195 // We cannot do the resolution if there is no parent, so use the default one
16196 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016197 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016198 break;
16199 case TEXT_DIRECTION_FIRST_STRONG:
16200 case TEXT_DIRECTION_ANY_RTL:
16201 case TEXT_DIRECTION_LTR:
16202 case TEXT_DIRECTION_RTL:
16203 case TEXT_DIRECTION_LOCALE:
16204 // Resolved direction is the same as text direction
16205 mPrivateFlags2 |= (textDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16206 break;
16207 default:
16208 // Default resolved direction is "first strong" heuristic
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016209 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016210 }
16211 } else {
16212 // Default resolved direction is "first strong" heuristic
16213 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016214 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016215
16216 // Set to resolved
16217 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016218 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016219 }
16220
16221 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016222 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016223 * resolution should override this method.
16224 *
16225 * The default implementation does nothing.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016226 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016227 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016228 }
16229
16230 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016231 * Check if text direction resolution can be done.
16232 *
16233 * @return true if text direction resolution can be done otherwise return false.
16234 */
16235 public boolean canResolveTextDirection() {
16236 switch (getTextDirection()) {
16237 case TEXT_DIRECTION_INHERIT:
16238 return (mParent != null) && (mParent instanceof ViewGroup);
16239 default:
16240 return true;
16241 }
16242 }
16243
16244 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016245 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016246 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016247 * reset is done.
16248 */
16249 public void resetResolvedTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016250 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016251 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016252 }
16253
16254 /**
16255 * Called when text direction is reset. Subclasses that care about text direction reset should
16256 * override this method and do a reset of the text direction of their children. The default
16257 * implementation does nothing.
16258 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016259 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016260 }
16261
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016262 /**
16263 * Return the value specifying the text alignment or policy that was set with
16264 * {@link #setTextAlignment(int)}.
16265 *
16266 * @return the defined text alignment. It can be one of:
16267 *
16268 * {@link #TEXT_ALIGNMENT_INHERIT},
16269 * {@link #TEXT_ALIGNMENT_GRAVITY},
16270 * {@link #TEXT_ALIGNMENT_CENTER},
16271 * {@link #TEXT_ALIGNMENT_TEXT_START},
16272 * {@link #TEXT_ALIGNMENT_TEXT_END},
16273 * {@link #TEXT_ALIGNMENT_VIEW_START},
16274 * {@link #TEXT_ALIGNMENT_VIEW_END}
16275 */
16276 @ViewDebug.ExportedProperty(category = "text", mapping = {
16277 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16278 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16279 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16280 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16281 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16282 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16283 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16284 })
16285 public int getTextAlignment() {
16286 return (mPrivateFlags2 & TEXT_ALIGNMENT_MASK) >> TEXT_ALIGNMENT_MASK_SHIFT;
16287 }
16288
16289 /**
16290 * Set the text alignment.
16291 *
16292 * @param textAlignment The text alignment to set. Should be one of
16293 *
16294 * {@link #TEXT_ALIGNMENT_INHERIT},
16295 * {@link #TEXT_ALIGNMENT_GRAVITY},
16296 * {@link #TEXT_ALIGNMENT_CENTER},
16297 * {@link #TEXT_ALIGNMENT_TEXT_START},
16298 * {@link #TEXT_ALIGNMENT_TEXT_END},
16299 * {@link #TEXT_ALIGNMENT_VIEW_START},
16300 * {@link #TEXT_ALIGNMENT_VIEW_END}
16301 *
16302 * @attr ref android.R.styleable#View_textAlignment
16303 */
16304 public void setTextAlignment(int textAlignment) {
16305 if (textAlignment != getTextAlignment()) {
16306 // Reset the current and resolved text alignment
16307 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
16308 resetResolvedTextAlignment();
16309 // Set the new text alignment
16310 mPrivateFlags2 |= ((textAlignment << TEXT_ALIGNMENT_MASK_SHIFT) & TEXT_ALIGNMENT_MASK);
16311 // Refresh
16312 requestLayout();
16313 invalidate(true);
16314 }
16315 }
16316
16317 /**
16318 * Return the resolved text alignment.
16319 *
16320 * The resolved text alignment. This needs resolution if the value is
16321 * TEXT_ALIGNMENT_INHERIT. The resolution matches {@link #setTextAlignment(int)} if it is
16322 * not TEXT_ALIGNMENT_INHERIT, otherwise resolution proceeds up the parent chain of the view.
16323 *
16324 * @return the resolved text alignment. Returns one of:
16325 *
16326 * {@link #TEXT_ALIGNMENT_GRAVITY},
16327 * {@link #TEXT_ALIGNMENT_CENTER},
16328 * {@link #TEXT_ALIGNMENT_TEXT_START},
16329 * {@link #TEXT_ALIGNMENT_TEXT_END},
16330 * {@link #TEXT_ALIGNMENT_VIEW_START},
16331 * {@link #TEXT_ALIGNMENT_VIEW_END}
16332 */
16333 @ViewDebug.ExportedProperty(category = "text", mapping = {
16334 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16335 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16336 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16337 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16338 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16339 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16340 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16341 })
16342 public int getResolvedTextAlignment() {
16343 // If text alignment is not resolved, then resolve it
16344 if ((mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED) != TEXT_ALIGNMENT_RESOLVED) {
16345 resolveTextAlignment();
16346 }
16347 return (mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED_MASK) >> TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
16348 }
16349
16350 /**
16351 * Resolve the text alignment. Will call {@link View#onResolvedTextAlignmentChanged} when
16352 * resolution is done.
16353 */
16354 public void resolveTextAlignment() {
16355 // Reset any previous text alignment resolution
16356 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16357
16358 if (hasRtlSupport()) {
16359 // Set resolved text alignment flag depending on text alignment flag
16360 final int textAlignment = getTextAlignment();
16361 switch (textAlignment) {
16362 case TEXT_ALIGNMENT_INHERIT:
16363 // Check if we can resolve the text alignment
16364 if (canResolveLayoutDirection() && mParent instanceof View) {
16365 View view = (View) mParent;
16366
16367 final int parentResolvedTextAlignment = view.getResolvedTextAlignment();
16368 switch (parentResolvedTextAlignment) {
16369 case TEXT_ALIGNMENT_GRAVITY:
16370 case TEXT_ALIGNMENT_TEXT_START:
16371 case TEXT_ALIGNMENT_TEXT_END:
16372 case TEXT_ALIGNMENT_CENTER:
16373 case TEXT_ALIGNMENT_VIEW_START:
16374 case TEXT_ALIGNMENT_VIEW_END:
16375 // Resolved text alignment is the same as the parent resolved
16376 // text alignment
16377 mPrivateFlags2 |=
16378 (parentResolvedTextAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16379 break;
16380 default:
16381 // Use default resolved text alignment
16382 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16383 }
16384 }
16385 else {
16386 // We cannot do the resolution if there is no parent so use the default
16387 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16388 }
16389 break;
16390 case TEXT_ALIGNMENT_GRAVITY:
16391 case TEXT_ALIGNMENT_TEXT_START:
16392 case TEXT_ALIGNMENT_TEXT_END:
16393 case TEXT_ALIGNMENT_CENTER:
16394 case TEXT_ALIGNMENT_VIEW_START:
16395 case TEXT_ALIGNMENT_VIEW_END:
16396 // Resolved text alignment is the same as text alignment
16397 mPrivateFlags2 |= (textAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16398 break;
16399 default:
16400 // Use default resolved text alignment
16401 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16402 }
16403 } else {
16404 // Use default resolved text alignment
16405 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16406 }
16407
16408 // Set the resolved
16409 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED;
16410 onResolvedTextAlignmentChanged();
16411 }
16412
16413 /**
16414 * Check if text alignment resolution can be done.
16415 *
16416 * @return true if text alignment resolution can be done otherwise return false.
16417 */
16418 public boolean canResolveTextAlignment() {
16419 switch (getTextAlignment()) {
16420 case TEXT_DIRECTION_INHERIT:
16421 return (mParent != null);
16422 default:
16423 return true;
16424 }
16425 }
16426
16427 /**
16428 * Called when text alignment has been resolved. Subclasses that care about text alignment
16429 * resolution should override this method.
16430 *
16431 * The default implementation does nothing.
16432 */
16433 public void onResolvedTextAlignmentChanged() {
16434 }
16435
16436 /**
16437 * Reset resolved text alignment. Text alignment can be resolved with a call to
16438 * getResolvedTextAlignment(). Will call {@link View#onResolvedTextAlignmentReset} when
16439 * reset is done.
16440 */
16441 public void resetResolvedTextAlignment() {
16442 // Reset any previous text alignment resolution
16443 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16444 onResolvedTextAlignmentReset();
16445 }
16446
16447 /**
16448 * Called when text alignment is reset. Subclasses that care about text alignment reset should
16449 * override this method and do a reset of the text alignment of their children. The default
16450 * implementation does nothing.
16451 */
16452 public void onResolvedTextAlignmentReset() {
16453 }
16454
Chet Haaseb39f0512011-05-24 14:36:40 -070016455 //
16456 // Properties
16457 //
16458 /**
16459 * A Property wrapper around the <code>alpha</code> functionality handled by the
16460 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
16461 */
Chet Haased47f1532011-12-16 11:18:52 -080016462 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016463 @Override
16464 public void setValue(View object, float value) {
16465 object.setAlpha(value);
16466 }
16467
16468 @Override
16469 public Float get(View object) {
16470 return object.getAlpha();
16471 }
16472 };
16473
16474 /**
16475 * A Property wrapper around the <code>translationX</code> functionality handled by the
16476 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
16477 */
Chet Haased47f1532011-12-16 11:18:52 -080016478 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016479 @Override
16480 public void setValue(View object, float value) {
16481 object.setTranslationX(value);
16482 }
16483
16484 @Override
16485 public Float get(View object) {
16486 return object.getTranslationX();
16487 }
16488 };
16489
16490 /**
16491 * A Property wrapper around the <code>translationY</code> functionality handled by the
16492 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
16493 */
Chet Haased47f1532011-12-16 11:18:52 -080016494 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016495 @Override
16496 public void setValue(View object, float value) {
16497 object.setTranslationY(value);
16498 }
16499
16500 @Override
16501 public Float get(View object) {
16502 return object.getTranslationY();
16503 }
16504 };
16505
16506 /**
16507 * A Property wrapper around the <code>x</code> functionality handled by the
16508 * {@link View#setX(float)} and {@link View#getX()} methods.
16509 */
Chet Haased47f1532011-12-16 11:18:52 -080016510 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016511 @Override
16512 public void setValue(View object, float value) {
16513 object.setX(value);
16514 }
16515
16516 @Override
16517 public Float get(View object) {
16518 return object.getX();
16519 }
16520 };
16521
16522 /**
16523 * A Property wrapper around the <code>y</code> functionality handled by the
16524 * {@link View#setY(float)} and {@link View#getY()} methods.
16525 */
Chet Haased47f1532011-12-16 11:18:52 -080016526 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016527 @Override
16528 public void setValue(View object, float value) {
16529 object.setY(value);
16530 }
16531
16532 @Override
16533 public Float get(View object) {
16534 return object.getY();
16535 }
16536 };
16537
16538 /**
16539 * A Property wrapper around the <code>rotation</code> functionality handled by the
16540 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
16541 */
Chet Haased47f1532011-12-16 11:18:52 -080016542 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016543 @Override
16544 public void setValue(View object, float value) {
16545 object.setRotation(value);
16546 }
16547
16548 @Override
16549 public Float get(View object) {
16550 return object.getRotation();
16551 }
16552 };
16553
16554 /**
16555 * A Property wrapper around the <code>rotationX</code> functionality handled by the
16556 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
16557 */
Chet Haased47f1532011-12-16 11:18:52 -080016558 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016559 @Override
16560 public void setValue(View object, float value) {
16561 object.setRotationX(value);
16562 }
16563
16564 @Override
16565 public Float get(View object) {
16566 return object.getRotationX();
16567 }
16568 };
16569
16570 /**
16571 * A Property wrapper around the <code>rotationY</code> functionality handled by the
16572 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
16573 */
Chet Haased47f1532011-12-16 11:18:52 -080016574 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016575 @Override
16576 public void setValue(View object, float value) {
16577 object.setRotationY(value);
16578 }
16579
16580 @Override
16581 public Float get(View object) {
16582 return object.getRotationY();
16583 }
16584 };
16585
16586 /**
16587 * A Property wrapper around the <code>scaleX</code> functionality handled by the
16588 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
16589 */
Chet Haased47f1532011-12-16 11:18:52 -080016590 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016591 @Override
16592 public void setValue(View object, float value) {
16593 object.setScaleX(value);
16594 }
16595
16596 @Override
16597 public Float get(View object) {
16598 return object.getScaleX();
16599 }
16600 };
16601
16602 /**
16603 * A Property wrapper around the <code>scaleY</code> functionality handled by the
16604 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
16605 */
Chet Haased47f1532011-12-16 11:18:52 -080016606 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016607 @Override
16608 public void setValue(View object, float value) {
16609 object.setScaleY(value);
16610 }
16611
16612 @Override
16613 public Float get(View object) {
16614 return object.getScaleY();
16615 }
16616 };
16617
Jeff Brown33bbfd22011-02-24 20:55:35 -080016618 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016619 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
16620 * Each MeasureSpec represents a requirement for either the width or the height.
16621 * A MeasureSpec is comprised of a size and a mode. There are three possible
16622 * modes:
16623 * <dl>
16624 * <dt>UNSPECIFIED</dt>
16625 * <dd>
16626 * The parent has not imposed any constraint on the child. It can be whatever size
16627 * it wants.
16628 * </dd>
16629 *
16630 * <dt>EXACTLY</dt>
16631 * <dd>
16632 * The parent has determined an exact size for the child. The child is going to be
16633 * given those bounds regardless of how big it wants to be.
16634 * </dd>
16635 *
16636 * <dt>AT_MOST</dt>
16637 * <dd>
16638 * The child can be as large as it wants up to the specified size.
16639 * </dd>
16640 * </dl>
16641 *
16642 * MeasureSpecs are implemented as ints to reduce object allocation. This class
16643 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
16644 */
16645 public static class MeasureSpec {
16646 private static final int MODE_SHIFT = 30;
16647 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
16648
16649 /**
16650 * Measure specification mode: The parent has not imposed any constraint
16651 * on the child. It can be whatever size it wants.
16652 */
16653 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
16654
16655 /**
16656 * Measure specification mode: The parent has determined an exact size
16657 * for the child. The child is going to be given those bounds regardless
16658 * of how big it wants to be.
16659 */
16660 public static final int EXACTLY = 1 << MODE_SHIFT;
16661
16662 /**
16663 * Measure specification mode: The child can be as large as it wants up
16664 * to the specified size.
16665 */
16666 public static final int AT_MOST = 2 << MODE_SHIFT;
16667
16668 /**
16669 * Creates a measure specification based on the supplied size and mode.
16670 *
16671 * The mode must always be one of the following:
16672 * <ul>
16673 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
16674 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
16675 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
16676 * </ul>
16677 *
16678 * @param size the size of the measure specification
16679 * @param mode the mode of the measure specification
16680 * @return the measure specification based on size and mode
16681 */
16682 public static int makeMeasureSpec(int size, int mode) {
16683 return size + mode;
16684 }
16685
16686 /**
16687 * Extracts the mode from the supplied measure specification.
16688 *
16689 * @param measureSpec the measure specification to extract the mode from
16690 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
16691 * {@link android.view.View.MeasureSpec#AT_MOST} or
16692 * {@link android.view.View.MeasureSpec#EXACTLY}
16693 */
16694 public static int getMode(int measureSpec) {
16695 return (measureSpec & MODE_MASK);
16696 }
16697
16698 /**
16699 * Extracts the size from the supplied measure specification.
16700 *
16701 * @param measureSpec the measure specification to extract the size from
16702 * @return the size in pixels defined in the supplied measure specification
16703 */
16704 public static int getSize(int measureSpec) {
16705 return (measureSpec & ~MODE_MASK);
16706 }
16707
16708 /**
16709 * Returns a String representation of the specified measure
16710 * specification.
16711 *
16712 * @param measureSpec the measure specification to convert to a String
16713 * @return a String with the following format: "MeasureSpec: MODE SIZE"
16714 */
16715 public static String toString(int measureSpec) {
16716 int mode = getMode(measureSpec);
16717 int size = getSize(measureSpec);
16718
16719 StringBuilder sb = new StringBuilder("MeasureSpec: ");
16720
16721 if (mode == UNSPECIFIED)
16722 sb.append("UNSPECIFIED ");
16723 else if (mode == EXACTLY)
16724 sb.append("EXACTLY ");
16725 else if (mode == AT_MOST)
16726 sb.append("AT_MOST ");
16727 else
16728 sb.append(mode).append(" ");
16729
16730 sb.append(size);
16731 return sb.toString();
16732 }
16733 }
16734
16735 class CheckForLongPress implements Runnable {
16736
16737 private int mOriginalWindowAttachCount;
16738
16739 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070016740 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016741 && mOriginalWindowAttachCount == mWindowAttachCount) {
16742 if (performLongClick()) {
16743 mHasPerformedLongPress = true;
16744 }
16745 }
16746 }
16747
16748 public void rememberWindowAttachCount() {
16749 mOriginalWindowAttachCount = mWindowAttachCount;
16750 }
16751 }
Joe Malin32736f02011-01-19 16:14:20 -080016752
Adam Powelle14579b2009-12-16 18:39:52 -080016753 private final class CheckForTap implements Runnable {
16754 public void run() {
16755 mPrivateFlags &= ~PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080016756 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016757 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080016758 }
16759 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016760
Adam Powella35d7682010-03-12 14:48:13 -080016761 private final class PerformClick implements Runnable {
16762 public void run() {
16763 performClick();
16764 }
16765 }
16766
Dianne Hackborn63042d62011-01-26 18:56:29 -080016767 /** @hide */
16768 public void hackTurnOffWindowResizeAnim(boolean off) {
16769 mAttachInfo.mTurnOffWindowResizeAnim = off;
16770 }
Joe Malin32736f02011-01-19 16:14:20 -080016771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016772 /**
Chet Haasea00f3862011-02-22 06:34:40 -080016773 * This method returns a ViewPropertyAnimator object, which can be used to animate
16774 * specific properties on this View.
16775 *
16776 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
16777 */
16778 public ViewPropertyAnimator animate() {
16779 if (mAnimator == null) {
16780 mAnimator = new ViewPropertyAnimator(this);
16781 }
16782 return mAnimator;
16783 }
16784
16785 /**
Jean Chalard405bc512012-05-29 19:12:34 +090016786 * Interface definition for a callback to be invoked when a hardware key event is
16787 * dispatched to this view. The callback will be invoked before the key event is
16788 * given to the view. This is only useful for hardware keyboards; a software input
16789 * method has no obligation to trigger this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016790 */
16791 public interface OnKeyListener {
16792 /**
Jean Chalard405bc512012-05-29 19:12:34 +090016793 * Called when a hardware key is dispatched to a view. This allows listeners to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016794 * get a chance to respond before the target view.
Jean Chalard405bc512012-05-29 19:12:34 +090016795 * <p>Key presses in software keyboards will generally NOT trigger this method,
16796 * although some may elect to do so in some situations. Do not assume a
16797 * software input method has to be key-based; even if it is, it may use key presses
16798 * in a different way than you expect, so there is no way to reliably catch soft
16799 * input key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016800 *
16801 * @param v The view the key has been dispatched to.
16802 * @param keyCode The code for the physical key that was pressed
16803 * @param event The KeyEvent object containing full information about
16804 * the event.
16805 * @return True if the listener has consumed the event, false otherwise.
16806 */
16807 boolean onKey(View v, int keyCode, KeyEvent event);
16808 }
16809
16810 /**
16811 * Interface definition for a callback to be invoked when a touch event is
16812 * dispatched to this view. The callback will be invoked before the touch
16813 * event is given to the view.
16814 */
16815 public interface OnTouchListener {
16816 /**
16817 * Called when a touch event is dispatched to a view. This allows listeners to
16818 * get a chance to respond before the target view.
16819 *
16820 * @param v The view the touch event has been dispatched to.
16821 * @param event The MotionEvent object containing full information about
16822 * the event.
16823 * @return True if the listener has consumed the event, false otherwise.
16824 */
16825 boolean onTouch(View v, MotionEvent event);
16826 }
16827
16828 /**
Jeff Brown10b62902011-06-20 16:40:37 -070016829 * Interface definition for a callback to be invoked when a hover event is
16830 * dispatched to this view. The callback will be invoked before the hover
16831 * event is given to the view.
16832 */
16833 public interface OnHoverListener {
16834 /**
16835 * Called when a hover event is dispatched to a view. This allows listeners to
16836 * get a chance to respond before the target view.
16837 *
16838 * @param v The view the hover event has been dispatched to.
16839 * @param event The MotionEvent object containing full information about
16840 * the event.
16841 * @return True if the listener has consumed the event, false otherwise.
16842 */
16843 boolean onHover(View v, MotionEvent event);
16844 }
16845
16846 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016847 * Interface definition for a callback to be invoked when a generic motion event is
16848 * dispatched to this view. The callback will be invoked before the generic motion
16849 * event is given to the view.
16850 */
16851 public interface OnGenericMotionListener {
16852 /**
16853 * Called when a generic motion event is dispatched to a view. This allows listeners to
16854 * get a chance to respond before the target view.
16855 *
16856 * @param v The view the generic motion event has been dispatched to.
16857 * @param event The MotionEvent object containing full information about
16858 * the event.
16859 * @return True if the listener has consumed the event, false otherwise.
16860 */
16861 boolean onGenericMotion(View v, MotionEvent event);
16862 }
16863
16864 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016865 * Interface definition for a callback to be invoked when a view has been clicked and held.
16866 */
16867 public interface OnLongClickListener {
16868 /**
16869 * Called when a view has been clicked and held.
16870 *
16871 * @param v The view that was clicked and held.
16872 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080016873 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016874 */
16875 boolean onLongClick(View v);
16876 }
16877
16878 /**
Chris Tate32affef2010-10-18 15:29:21 -070016879 * Interface definition for a callback to be invoked when a drag is being dispatched
16880 * to this view. The callback will be invoked before the hosting view's own
16881 * onDrag(event) method. If the listener wants to fall back to the hosting view's
16882 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070016883 *
16884 * <div class="special reference">
16885 * <h3>Developer Guides</h3>
16886 * <p>For a guide to implementing drag and drop features, read the
16887 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
16888 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070016889 */
16890 public interface OnDragListener {
16891 /**
16892 * Called when a drag event is dispatched to a view. This allows listeners
16893 * to get a chance to override base View behavior.
16894 *
Joe Malin32736f02011-01-19 16:14:20 -080016895 * @param v The View that received the drag event.
16896 * @param event The {@link android.view.DragEvent} object for the drag event.
16897 * @return {@code true} if the drag event was handled successfully, or {@code false}
16898 * if the drag event was not handled. Note that {@code false} will trigger the View
16899 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070016900 */
16901 boolean onDrag(View v, DragEvent event);
16902 }
16903
16904 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016905 * Interface definition for a callback to be invoked when the focus state of
16906 * a view changed.
16907 */
16908 public interface OnFocusChangeListener {
16909 /**
16910 * Called when the focus state of a view has changed.
16911 *
16912 * @param v The view whose state has changed.
16913 * @param hasFocus The new focus state of v.
16914 */
16915 void onFocusChange(View v, boolean hasFocus);
16916 }
16917
16918 /**
16919 * Interface definition for a callback to be invoked when a view is clicked.
16920 */
16921 public interface OnClickListener {
16922 /**
16923 * Called when a view has been clicked.
16924 *
16925 * @param v The view that was clicked.
16926 */
16927 void onClick(View v);
16928 }
16929
16930 /**
16931 * Interface definition for a callback to be invoked when the context menu
16932 * for this view is being built.
16933 */
16934 public interface OnCreateContextMenuListener {
16935 /**
16936 * Called when the context menu for this view is being built. It is not
16937 * safe to hold onto the menu after this method returns.
16938 *
16939 * @param menu The context menu that is being built
16940 * @param v The view for which the context menu is being built
16941 * @param menuInfo Extra information about the item for which the
16942 * context menu should be shown. This information will vary
16943 * depending on the class of v.
16944 */
16945 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
16946 }
16947
Joe Onorato664644d2011-01-23 17:53:23 -080016948 /**
16949 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016950 * visibility. This reports <strong>global</strong> changes to the system UI
Dianne Hackborncf675782012-05-10 15:07:24 -070016951 * state, not what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080016952 *
Philip Milne6c8ea062012-04-03 17:38:43 -070016953 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080016954 */
16955 public interface OnSystemUiVisibilityChangeListener {
16956 /**
16957 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070016958 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080016959 *
Dianne Hackborncf675782012-05-10 15:07:24 -070016960 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
16961 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, and {@link #SYSTEM_UI_FLAG_FULLSCREEN}.
16962 * This tells you the <strong>global</strong> state of these UI visibility
16963 * flags, not what your app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080016964 */
16965 public void onSystemUiVisibilityChange(int visibility);
16966 }
16967
Adam Powell4afd62b2011-02-18 15:02:18 -080016968 /**
16969 * Interface definition for a callback to be invoked when this view is attached
16970 * or detached from its window.
16971 */
16972 public interface OnAttachStateChangeListener {
16973 /**
16974 * Called when the view is attached to a window.
16975 * @param v The view that was attached
16976 */
16977 public void onViewAttachedToWindow(View v);
16978 /**
16979 * Called when the view is detached from a window.
16980 * @param v The view that was detached
16981 */
16982 public void onViewDetachedFromWindow(View v);
16983 }
16984
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016985 private final class UnsetPressedState implements Runnable {
16986 public void run() {
16987 setPressed(false);
16988 }
16989 }
16990
16991 /**
16992 * Base class for derived classes that want to save and restore their own
16993 * state in {@link android.view.View#onSaveInstanceState()}.
16994 */
16995 public static class BaseSavedState extends AbsSavedState {
16996 /**
16997 * Constructor used when reading from a parcel. Reads the state of the superclass.
16998 *
16999 * @param source
17000 */
17001 public BaseSavedState(Parcel source) {
17002 super(source);
17003 }
17004
17005 /**
17006 * Constructor called by derived classes when creating their SavedState objects
17007 *
17008 * @param superState The state of the superclass of this view
17009 */
17010 public BaseSavedState(Parcelable superState) {
17011 super(superState);
17012 }
17013
17014 public static final Parcelable.Creator<BaseSavedState> CREATOR =
17015 new Parcelable.Creator<BaseSavedState>() {
17016 public BaseSavedState createFromParcel(Parcel in) {
17017 return new BaseSavedState(in);
17018 }
17019
17020 public BaseSavedState[] newArray(int size) {
17021 return new BaseSavedState[size];
17022 }
17023 };
17024 }
17025
17026 /**
17027 * A set of information given to a view when it is attached to its parent
17028 * window.
17029 */
17030 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017031 interface Callbacks {
17032 void playSoundEffect(int effectId);
17033 boolean performHapticFeedback(int effectId, boolean always);
17034 }
17035
17036 /**
17037 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
17038 * to a Handler. This class contains the target (View) to invalidate and
17039 * the coordinates of the dirty rectangle.
17040 *
17041 * For performance purposes, this class also implements a pool of up to
17042 * POOL_LIMIT objects that get reused. This reduces memory allocations
17043 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017044 */
Romain Guyd928d682009-03-31 17:52:16 -070017045 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017046 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070017047 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
17048 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070017049 public InvalidateInfo newInstance() {
17050 return new InvalidateInfo();
17051 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017052
Romain Guyd928d682009-03-31 17:52:16 -070017053 public void onAcquired(InvalidateInfo element) {
17054 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017055
Romain Guyd928d682009-03-31 17:52:16 -070017056 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070017057 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070017058 }
17059 }, POOL_LIMIT)
17060 );
17061
17062 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017063 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017064
17065 View target;
17066
17067 int left;
17068 int top;
17069 int right;
17070 int bottom;
17071
Romain Guyd928d682009-03-31 17:52:16 -070017072 public void setNextPoolable(InvalidateInfo element) {
17073 mNext = element;
17074 }
17075
17076 public InvalidateInfo getNextPoolable() {
17077 return mNext;
17078 }
17079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017080 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070017081 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017082 }
17083
17084 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070017085 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017086 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017087
17088 public boolean isPooled() {
17089 return mIsPooled;
17090 }
17091
17092 public void setPooled(boolean isPooled) {
17093 mIsPooled = isPooled;
17094 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017095 }
17096
17097 final IWindowSession mSession;
17098
17099 final IWindow mWindow;
17100
17101 final IBinder mWindowToken;
17102
17103 final Callbacks mRootCallbacks;
17104
Romain Guy59a12ca2011-06-09 17:48:21 -070017105 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080017106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017107 /**
17108 * The top view of the hierarchy.
17109 */
17110 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070017111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017112 IBinder mPanelParentWindowToken;
17113 Surface mSurface;
17114
Romain Guyb051e892010-09-28 19:09:36 -070017115 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080017116 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070017117 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080017118
Romain Guy7e4e5612012-03-05 14:37:29 -080017119 boolean mScreenOn;
17120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017121 /**
Romain Guy8506ab42009-06-11 17:35:47 -070017122 * Scale factor used by the compatibility mode
17123 */
17124 float mApplicationScale;
17125
17126 /**
17127 * Indicates whether the application is in compatibility mode
17128 */
17129 boolean mScalingRequired;
17130
17131 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017132 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080017133 */
17134 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080017135
Dianne Hackborn63042d62011-01-26 18:56:29 -080017136 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017137 * Left position of this view's window
17138 */
17139 int mWindowLeft;
17140
17141 /**
17142 * Top position of this view's window
17143 */
17144 int mWindowTop;
17145
17146 /**
Svetoslav Ganov86783472012-06-06 21:12:20 -070017147 * Left actual position of this view's window.
17148 *
17149 * TODO: This is a workaround for 6623031. Remove when fixed.
17150 */
17151 int mActualWindowLeft;
17152
17153 /**
17154 * Actual top position of this view's window.
17155 *
17156 * TODO: This is a workaround for 6623031. Remove when fixed.
17157 */
17158 int mActualWindowTop;
17159
17160 /**
Adam Powell26153a32010-11-08 15:22:27 -080017161 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070017162 */
Adam Powell26153a32010-11-08 15:22:27 -080017163 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070017164
17165 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017166 * For windows that are full-screen but using insets to layout inside
17167 * of the screen decorations, these are the current insets for the
17168 * content of the window.
17169 */
17170 final Rect mContentInsets = new Rect();
17171
17172 /**
17173 * For windows that are full-screen but using insets to layout inside
17174 * of the screen decorations, these are the current insets for the
17175 * actual visible parts of the window.
17176 */
17177 final Rect mVisibleInsets = new Rect();
17178
17179 /**
17180 * The internal insets given by this window. This value is
17181 * supplied by the client (through
17182 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
17183 * be given to the window manager when changed to be used in laying
17184 * out windows behind it.
17185 */
17186 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
17187 = new ViewTreeObserver.InternalInsetsInfo();
17188
17189 /**
17190 * All views in the window's hierarchy that serve as scroll containers,
17191 * used to determine if the window can be resized or must be panned
17192 * to adjust for a soft input area.
17193 */
17194 final ArrayList<View> mScrollContainers = new ArrayList<View>();
17195
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070017196 final KeyEvent.DispatcherState mKeyDispatchState
17197 = new KeyEvent.DispatcherState();
17198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017199 /**
17200 * Indicates whether the view's window currently has the focus.
17201 */
17202 boolean mHasWindowFocus;
17203
17204 /**
17205 * The current visibility of the window.
17206 */
17207 int mWindowVisibility;
17208
17209 /**
17210 * Indicates the time at which drawing started to occur.
17211 */
17212 long mDrawingTime;
17213
17214 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070017215 * Indicates whether or not ignoring the DIRTY_MASK flags.
17216 */
17217 boolean mIgnoreDirtyState;
17218
17219 /**
Romain Guy02ccac62011-06-24 13:20:23 -070017220 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
17221 * to avoid clearing that flag prematurely.
17222 */
17223 boolean mSetIgnoreDirtyState = false;
17224
17225 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017226 * Indicates whether the view's window is currently in touch mode.
17227 */
17228 boolean mInTouchMode;
17229
17230 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017231 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017232 * the next time it performs a traversal
17233 */
17234 boolean mRecomputeGlobalAttributes;
17235
17236 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017237 * Always report new attributes at next traversal.
17238 */
17239 boolean mForceReportNewAttributes;
17240
17241 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017242 * Set during a traveral if any views want to keep the screen on.
17243 */
17244 boolean mKeepScreenOn;
17245
17246 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017247 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
17248 */
17249 int mSystemUiVisibility;
17250
17251 /**
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070017252 * Hack to force certain system UI visibility flags to be cleared.
17253 */
17254 int mDisabledSystemUiVisibility;
17255
17256 /**
Dianne Hackborncf675782012-05-10 15:07:24 -070017257 * Last global system UI visibility reported by the window manager.
17258 */
17259 int mGlobalSystemUiVisibility;
17260
17261 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017262 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
17263 * attached.
17264 */
17265 boolean mHasSystemUiListeners;
17266
17267 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017268 * Set if the visibility of any views has changed.
17269 */
17270 boolean mViewVisibilityChanged;
17271
17272 /**
17273 * Set to true if a view has been scrolled.
17274 */
17275 boolean mViewScrollChanged;
17276
17277 /**
17278 * Global to the view hierarchy used as a temporary for dealing with
17279 * x/y points in the transparent region computations.
17280 */
17281 final int[] mTransparentLocation = new int[2];
17282
17283 /**
17284 * Global to the view hierarchy used as a temporary for dealing with
17285 * x/y points in the ViewGroup.invalidateChild implementation.
17286 */
17287 final int[] mInvalidateChildLocation = new int[2];
17288
Chet Haasec3aa3612010-06-17 08:50:37 -070017289
17290 /**
17291 * Global to the view hierarchy used as a temporary for dealing with
17292 * x/y location when view is transformed.
17293 */
17294 final float[] mTmpTransformLocation = new float[2];
17295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017296 /**
17297 * The view tree observer used to dispatch global events like
17298 * layout, pre-draw, touch mode change, etc.
17299 */
17300 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
17301
17302 /**
17303 * A Canvas used by the view hierarchy to perform bitmap caching.
17304 */
17305 Canvas mCanvas;
17306
17307 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080017308 * The view root impl.
17309 */
17310 final ViewRootImpl mViewRootImpl;
17311
17312 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070017313 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017314 * handler can be used to pump events in the UI events queue.
17315 */
17316 final Handler mHandler;
17317
17318 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017319 * Temporary for use in computing invalidate rectangles while
17320 * calling up the hierarchy.
17321 */
17322 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070017323
17324 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070017325 * Temporary for use in computing hit areas with transformed views
17326 */
17327 final RectF mTmpTransformRect = new RectF();
17328
17329 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070017330 * Temporary list for use in collecting focusable descendents of a view.
17331 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070017332 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070017333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017334 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017335 * The id of the window for accessibility purposes.
17336 */
17337 int mAccessibilityWindowId = View.NO_ID;
17338
17339 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -070017340 * Whether to ingore not exposed for accessibility Views when
17341 * reporting the view tree to accessibility services.
17342 */
17343 boolean mIncludeNotImportantViews;
17344
17345 /**
17346 * The drawable for highlighting accessibility focus.
17347 */
17348 Drawable mAccessibilityFocusDrawable;
17349
17350 /**
Philip Milne10ca24a2012-04-23 15:38:27 -070017351 * Show where the margins, bounds and layout bounds are for each view.
17352 */
Dianne Hackborna53de062012-05-08 18:53:51 -070017353 boolean mDebugLayout = SystemProperties.getBoolean(DEBUG_LAYOUT_PROPERTY, false);
Philip Milne10ca24a2012-04-23 15:38:27 -070017354
17355 /**
Romain Guyab4c4f4f2012-05-06 13:11:24 -070017356 * Point used to compute visible regions.
17357 */
17358 final Point mPoint = new Point();
17359
17360 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017361 * Creates a new set of attachment information with the specified
17362 * events handler and thread.
17363 *
17364 * @param handler the events handler the view must use
17365 */
17366 AttachInfo(IWindowSession session, IWindow window,
Jeff Browna175a5b2012-02-15 19:18:31 -080017367 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017368 mSession = session;
17369 mWindow = window;
17370 mWindowToken = window.asBinder();
Jeff Browna175a5b2012-02-15 19:18:31 -080017371 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017372 mHandler = handler;
17373 mRootCallbacks = effectPlayer;
17374 }
17375 }
17376
17377 /**
17378 * <p>ScrollabilityCache holds various fields used by a View when scrolling
17379 * is supported. This avoids keeping too many unused fields in most
17380 * instances of View.</p>
17381 */
Mike Cleronf116bf82009-09-27 19:14:12 -070017382 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080017383
Mike Cleronf116bf82009-09-27 19:14:12 -070017384 /**
17385 * Scrollbars are not visible
17386 */
17387 public static final int OFF = 0;
17388
17389 /**
17390 * Scrollbars are visible
17391 */
17392 public static final int ON = 1;
17393
17394 /**
17395 * Scrollbars are fading away
17396 */
17397 public static final int FADING = 2;
17398
17399 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080017400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017401 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070017402 public int scrollBarDefaultDelayBeforeFade;
17403 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017404
17405 public int scrollBarSize;
17406 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070017407 public float[] interpolatorValues;
17408 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017409
17410 public final Paint paint;
17411 public final Matrix matrix;
17412 public Shader shader;
17413
Mike Cleronf116bf82009-09-27 19:14:12 -070017414 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
17415
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017416 private static final float[] OPAQUE = { 255 };
17417 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080017418
Mike Cleronf116bf82009-09-27 19:14:12 -070017419 /**
17420 * When fading should start. This time moves into the future every time
17421 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
17422 */
17423 public long fadeStartTime;
17424
17425
17426 /**
17427 * The current state of the scrollbars: ON, OFF, or FADING
17428 */
17429 public int state = OFF;
17430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017431 private int mLastColor;
17432
Mike Cleronf116bf82009-09-27 19:14:12 -070017433 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017434 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
17435 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070017436 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
17437 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017438
17439 paint = new Paint();
17440 matrix = new Matrix();
17441 // use use a height of 1, and then wack the matrix each time we
17442 // actually use it.
17443 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017445 paint.setShader(shader);
17446 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070017447 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017448 }
Romain Guy8506ab42009-06-11 17:35:47 -070017449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017450 public void setFadeColor(int color) {
17451 if (color != 0 && color != mLastColor) {
17452 mLastColor = color;
17453 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070017454
Romain Guye55e1a72009-08-27 10:42:26 -070017455 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
17456 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017458 paint.setShader(shader);
17459 // Restore the default transfer mode (src_over)
17460 paint.setXfermode(null);
17461 }
17462 }
Joe Malin32736f02011-01-19 16:14:20 -080017463
Mike Cleronf116bf82009-09-27 19:14:12 -070017464 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070017465 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070017466 if (now >= fadeStartTime) {
17467
17468 // the animation fades the scrollbars out by changing
17469 // the opacity (alpha) from fully opaque to fully
17470 // transparent
17471 int nextFrame = (int) now;
17472 int framesCount = 0;
17473
17474 Interpolator interpolator = scrollBarInterpolator;
17475
17476 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017477 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070017478
17479 // End transparent
17480 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017481 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070017482
17483 state = FADING;
17484
17485 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080017486 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070017487 }
17488 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070017489 }
Mike Cleronf116bf82009-09-27 19:14:12 -070017490
Svetoslav Ganova0156172011-06-26 17:55:44 -070017491 /**
17492 * Resuable callback for sending
17493 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
17494 */
17495 private class SendViewScrolledAccessibilityEvent implements Runnable {
17496 public volatile boolean mIsPending;
17497
17498 public void run() {
17499 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
17500 mIsPending = false;
17501 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017502 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017503
17504 /**
17505 * <p>
17506 * This class represents a delegate that can be registered in a {@link View}
17507 * to enhance accessibility support via composition rather via inheritance.
17508 * It is specifically targeted to widget developers that extend basic View
17509 * classes i.e. classes in package android.view, that would like their
17510 * applications to be backwards compatible.
17511 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080017512 * <div class="special reference">
17513 * <h3>Developer Guides</h3>
17514 * <p>For more information about making applications accessible, read the
17515 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
17516 * developer guide.</p>
17517 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017518 * <p>
17519 * A scenario in which a developer would like to use an accessibility delegate
17520 * is overriding a method introduced in a later API version then the minimal API
17521 * version supported by the application. For example, the method
17522 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
17523 * in API version 4 when the accessibility APIs were first introduced. If a
17524 * developer would like his application to run on API version 4 devices (assuming
17525 * all other APIs used by the application are version 4 or lower) and take advantage
17526 * of this method, instead of overriding the method which would break the application's
17527 * backwards compatibility, he can override the corresponding method in this
17528 * delegate and register the delegate in the target View if the API version of
17529 * the system is high enough i.e. the API version is same or higher to the API
17530 * version that introduced
17531 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
17532 * </p>
17533 * <p>
17534 * Here is an example implementation:
17535 * </p>
17536 * <code><pre><p>
17537 * if (Build.VERSION.SDK_INT >= 14) {
17538 * // If the API version is equal of higher than the version in
17539 * // which onInitializeAccessibilityNodeInfo was introduced we
17540 * // register a delegate with a customized implementation.
17541 * View view = findViewById(R.id.view_id);
17542 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
17543 * public void onInitializeAccessibilityNodeInfo(View host,
17544 * AccessibilityNodeInfo info) {
17545 * // Let the default implementation populate the info.
17546 * super.onInitializeAccessibilityNodeInfo(host, info);
17547 * // Set some other information.
17548 * info.setEnabled(host.isEnabled());
17549 * }
17550 * });
17551 * }
17552 * </code></pre></p>
17553 * <p>
17554 * This delegate contains methods that correspond to the accessibility methods
17555 * in View. If a delegate has been specified the implementation in View hands
17556 * off handling to the corresponding method in this delegate. The default
17557 * implementation the delegate methods behaves exactly as the corresponding
17558 * method in View for the case of no accessibility delegate been set. Hence,
17559 * to customize the behavior of a View method, clients can override only the
17560 * corresponding delegate method without altering the behavior of the rest
17561 * accessibility related methods of the host view.
17562 * </p>
17563 */
17564 public static class AccessibilityDelegate {
17565
17566 /**
17567 * Sends an accessibility event of the given type. If accessibility is not
17568 * enabled this method has no effect.
17569 * <p>
17570 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
17571 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
17572 * been set.
17573 * </p>
17574 *
17575 * @param host The View hosting the delegate.
17576 * @param eventType The type of the event to send.
17577 *
17578 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
17579 */
17580 public void sendAccessibilityEvent(View host, int eventType) {
17581 host.sendAccessibilityEventInternal(eventType);
17582 }
17583
17584 /**
alanv8eeefef2012-05-07 16:57:53 -070017585 * Performs the specified accessibility action on the view. For
17586 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
17587 * <p>
17588 * The default implementation behaves as
17589 * {@link View#performAccessibilityAction(int, Bundle)
17590 * View#performAccessibilityAction(int, Bundle)} for the case of
17591 * no accessibility delegate been set.
17592 * </p>
17593 *
17594 * @param action The action to perform.
17595 * @return Whether the action was performed.
17596 *
17597 * @see View#performAccessibilityAction(int, Bundle)
17598 * View#performAccessibilityAction(int, Bundle)
17599 */
17600 public boolean performAccessibilityAction(View host, int action, Bundle args) {
17601 return host.performAccessibilityActionInternal(action, args);
17602 }
17603
17604 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017605 * Sends an accessibility event. This method behaves exactly as
17606 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
17607 * empty {@link AccessibilityEvent} and does not perform a check whether
17608 * accessibility is enabled.
17609 * <p>
17610 * The default implementation behaves as
17611 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17612 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
17613 * the case of no accessibility delegate been set.
17614 * </p>
17615 *
17616 * @param host The View hosting the delegate.
17617 * @param event The event to send.
17618 *
17619 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17620 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17621 */
17622 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
17623 host.sendAccessibilityEventUncheckedInternal(event);
17624 }
17625
17626 /**
17627 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
17628 * to its children for adding their text content to the event.
17629 * <p>
17630 * The default implementation behaves as
17631 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17632 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
17633 * the case of no accessibility delegate been set.
17634 * </p>
17635 *
17636 * @param host The View hosting the delegate.
17637 * @param event The event.
17638 * @return True if the event population was completed.
17639 *
17640 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17641 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17642 */
17643 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17644 return host.dispatchPopulateAccessibilityEventInternal(event);
17645 }
17646
17647 /**
17648 * Gives a chance to the host View to populate the accessibility event with its
17649 * text content.
17650 * <p>
17651 * The default implementation behaves as
17652 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
17653 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
17654 * the case of no accessibility delegate been set.
17655 * </p>
17656 *
17657 * @param host The View hosting the delegate.
17658 * @param event The accessibility event which to populate.
17659 *
17660 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
17661 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
17662 */
17663 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17664 host.onPopulateAccessibilityEventInternal(event);
17665 }
17666
17667 /**
17668 * Initializes an {@link AccessibilityEvent} with information about the
17669 * the host View which is the event source.
17670 * <p>
17671 * The default implementation behaves as
17672 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
17673 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
17674 * the case of no accessibility delegate been set.
17675 * </p>
17676 *
17677 * @param host The View hosting the delegate.
17678 * @param event The event to initialize.
17679 *
17680 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
17681 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
17682 */
17683 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
17684 host.onInitializeAccessibilityEventInternal(event);
17685 }
17686
17687 /**
17688 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
17689 * <p>
17690 * The default implementation behaves as
17691 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17692 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
17693 * the case of no accessibility delegate been set.
17694 * </p>
17695 *
17696 * @param host The View hosting the delegate.
17697 * @param info The instance to initialize.
17698 *
17699 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17700 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17701 */
17702 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
17703 host.onInitializeAccessibilityNodeInfoInternal(info);
17704 }
17705
17706 /**
17707 * Called when a child of the host View has requested sending an
17708 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
17709 * to augment the event.
17710 * <p>
17711 * The default implementation behaves as
17712 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17713 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
17714 * the case of no accessibility delegate been set.
17715 * </p>
17716 *
17717 * @param host The View hosting the delegate.
17718 * @param child The child which requests sending the event.
17719 * @param event The event to be sent.
17720 * @return True if the event should be sent
17721 *
17722 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17723 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17724 */
17725 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
17726 AccessibilityEvent event) {
17727 return host.onRequestSendAccessibilityEventInternal(child, event);
17728 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070017729
17730 /**
17731 * Gets the provider for managing a virtual view hierarchy rooted at this View
17732 * and reported to {@link android.accessibilityservice.AccessibilityService}s
17733 * that explore the window content.
17734 * <p>
17735 * The default implementation behaves as
17736 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
17737 * the case of no accessibility delegate been set.
17738 * </p>
17739 *
17740 * @return The provider.
17741 *
17742 * @see AccessibilityNodeProvider
17743 */
17744 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
17745 return null;
17746 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017747 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017748}