blob: c20351b563adc435987ef35c0a1900ae31fa1037 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view;
18
Christopher Tatea53146c2010-09-07 11:57:52 -070019import android.content.ClipData;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.Context;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080021import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.res.Resources;
23import android.content.res.TypedArray;
24import android.graphics.Bitmap;
Adam Powell2b342f02010-08-18 18:14:13 -070025import android.graphics.Camera;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.graphics.Canvas;
Philip Milne1557fd72012-04-04 23:41:34 -070027import android.graphics.Insets;
Mike Cleronf116bf82009-09-27 19:14:12 -070028import android.graphics.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.graphics.LinearGradient;
30import android.graphics.Matrix;
31import android.graphics.Paint;
32import android.graphics.PixelFormat;
svetoslavganov75986cf2009-05-14 22:28:01 -070033import android.graphics.Point;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.graphics.PorterDuff;
35import android.graphics.PorterDuffXfermode;
36import android.graphics.Rect;
Adam Powell6e346362010-07-23 10:18:23 -070037import android.graphics.RectF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.graphics.Region;
39import android.graphics.Shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.graphics.drawable.ColorDrawable;
41import android.graphics.drawable.Drawable;
Svetoslav Ganovaa780c12012-04-19 23:01:39 -070042import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.os.Handler;
44import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.os.Parcel;
46import android.os.Parcelable;
47import android.os.RemoteException;
48import android.os.SystemClock;
Philip Milne10ca24a2012-04-23 15:38:27 -070049import android.os.SystemProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.util.AttributeSet;
Doug Feltcb3791202011-07-07 11:57:48 -070051import android.util.FloatProperty;
52import android.util.LocaleUtil;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070054import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070055import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070056import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.util.Pools;
Doug Feltcb3791202011-07-07 11:57:48 -070058import android.util.Property;
svetoslavganov75986cf2009-05-14 22:28:01 -070059import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080060import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.view.ContextMenu.ContextMenuInfo;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -070062import android.view.AccessibilityIterators.TextSegmentIterator;
63import android.view.AccessibilityIterators.CharacterTextSegmentIterator;
64import android.view.AccessibilityIterators.WordTextSegmentIterator;
65import android.view.AccessibilityIterators.ParagraphTextSegmentIterator;
svetoslavganov75986cf2009-05-14 22:28:01 -070066import android.view.accessibility.AccessibilityEvent;
67import android.view.accessibility.AccessibilityEventSource;
68import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070069import android.view.accessibility.AccessibilityNodeInfo;
Svetoslav Ganov02107852011-10-03 17:06:56 -070070import android.view.accessibility.AccessibilityNodeProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070072import android.view.animation.AnimationUtils;
Chet Haase64a48c12012-02-13 16:33:29 -080073import android.view.animation.Transformation;
svetoslavganov75986cf2009-05-14 22:28:01 -070074import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.view.inputmethod.InputConnection;
76import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.widget.ScrollBarDrawable;
78
Romain Guy1ef3fdb2011-09-09 15:30:30 -070079import static android.os.Build.VERSION_CODES.*;
Philip Milne6c8ea062012-04-03 17:38:43 -070080import static java.lang.Math.max;
Romain Guy1ef3fdb2011-09-09 15:30:30 -070081
Doug Feltcb3791202011-07-07 11:57:48 -070082import com.android.internal.R;
83import com.android.internal.util.Predicate;
84import com.android.internal.view.menu.MenuBuilder;
85
Christopher Tatea0374192010-10-05 13:06:41 -070086import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070087import java.lang.reflect.InvocationTargetException;
88import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089import java.util.ArrayList;
90import java.util.Arrays;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070091import java.util.Locale;
Adam Powell4afd62b2011-02-18 15:02:18 -080092import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093
94/**
95 * <p>
96 * This class represents the basic building block for user interface components. A View
97 * occupies a rectangular area on the screen and is responsible for drawing and
98 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070099 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
101 * are invisible containers that hold other Views (or other ViewGroups) and define
102 * their layout properties.
103 * </p>
104 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700105 * <div class="special reference">
106 * <h3>Developer Guides</h3>
107 * <p>For information about using this class to develop your application's user interface,
108 * read the <a href="{@docRoot}guide/topics/ui/index.html">User Interface</a> developer guide.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700110 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 * <a name="Using"></a>
112 * <h3>Using Views</h3>
113 * <p>
114 * All of the views in a window are arranged in a single tree. You can add views
115 * either from code or by specifying a tree of views in one or more XML layout
116 * files. There are many specialized subclasses of views that act as controls or
117 * are capable of displaying text, images, or other content.
118 * </p>
119 * <p>
120 * Once you have created a tree of views, there are typically a few types of
121 * common operations you may wish to perform:
122 * <ul>
123 * <li><strong>Set properties:</strong> for example setting the text of a
124 * {@link android.widget.TextView}. The available properties and the methods
125 * that set them will vary among the different subclasses of views. Note that
126 * properties that are known at build time can be set in the XML layout
127 * files.</li>
128 * <li><strong>Set focus:</strong> The framework will handled moving focus in
129 * response to user input. To force focus to a specific view, call
130 * {@link #requestFocus}.</li>
131 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
132 * that will be notified when something interesting happens to the view. For
133 * example, all views will let you set a listener to be notified when the view
134 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700135 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
Philip Milne6c8ea062012-04-03 17:38:43 -0700136 * Other view subclasses offer more specialized listeners. For example, a Button
Romain Guy5c22a8c2011-05-13 11:48:45 -0700137 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700139 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 * </ul>
141 * </p>
142 * <p><em>
143 * Note: The Android framework is responsible for measuring, laying out and
144 * drawing views. You should not call methods that perform these actions on
145 * views yourself unless you are actually implementing a
146 * {@link android.view.ViewGroup}.
147 * </em></p>
148 *
149 * <a name="Lifecycle"></a>
150 * <h3>Implementing a Custom View</h3>
151 *
152 * <p>
153 * To implement a custom view, you will usually begin by providing overrides for
154 * some of the standard methods that the framework calls on all views. You do
155 * not need to override all of these methods. In fact, you can start by just
156 * overriding {@link #onDraw(android.graphics.Canvas)}.
157 * <table border="2" width="85%" align="center" cellpadding="5">
158 * <thead>
159 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
160 * </thead>
161 *
162 * <tbody>
163 * <tr>
164 * <td rowspan="2">Creation</td>
165 * <td>Constructors</td>
166 * <td>There is a form of the constructor that are called when the view
167 * is created from code and a form that is called when the view is
168 * inflated from a layout file. The second form should parse and apply
169 * any attributes defined in the layout file.
170 * </td>
171 * </tr>
172 * <tr>
173 * <td><code>{@link #onFinishInflate()}</code></td>
174 * <td>Called after a view and all of its children has been inflated
175 * from XML.</td>
176 * </tr>
177 *
178 * <tr>
179 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700180 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 * <td>Called to determine the size requirements for this view and all
182 * of its children.
183 * </td>
184 * </tr>
185 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700186 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 * <td>Called when this view should assign a size and position to all
188 * of its children.
189 * </td>
190 * </tr>
191 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700192 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 * <td>Called when the size of this view has changed.
194 * </td>
195 * </tr>
196 *
197 * <tr>
198 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700199 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 * <td>Called when the view should render its content.
201 * </td>
202 * </tr>
203 *
204 * <tr>
205 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700206 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 * <td>Called when a new key event occurs.
208 * </td>
209 * </tr>
210 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700211 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 * <td>Called when a key up event occurs.
213 * </td>
214 * </tr>
215 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700216 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 * <td>Called when a trackball motion event occurs.
218 * </td>
219 * </tr>
220 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700221 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 * <td>Called when a touch screen motion event occurs.
223 * </td>
224 * </tr>
225 *
226 * <tr>
227 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700228 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 * <td>Called when the view gains or loses focus.
230 * </td>
231 * </tr>
232 *
233 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700234 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 * <td>Called when the window containing the view gains or loses focus.
236 * </td>
237 * </tr>
238 *
239 * <tr>
240 * <td rowspan="3">Attaching</td>
241 * <td><code>{@link #onAttachedToWindow()}</code></td>
242 * <td>Called when the view is attached to a window.
243 * </td>
244 * </tr>
245 *
246 * <tr>
247 * <td><code>{@link #onDetachedFromWindow}</code></td>
248 * <td>Called when the view is detached from its window.
249 * </td>
250 * </tr>
251 *
252 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700253 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 * <td>Called when the visibility of the window containing the view
255 * has changed.
256 * </td>
257 * </tr>
258 * </tbody>
259 *
260 * </table>
261 * </p>
262 *
263 * <a name="IDs"></a>
264 * <h3>IDs</h3>
265 * Views may have an integer id associated with them. These ids are typically
266 * assigned in the layout XML files, and are used to find specific views within
267 * the view tree. A common pattern is to:
268 * <ul>
269 * <li>Define a Button in the layout file and assign it a unique ID.
270 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700271 * &lt;Button
272 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 * android:layout_width="wrap_content"
274 * android:layout_height="wrap_content"
275 * android:text="@string/my_button_text"/&gt;
276 * </pre></li>
277 * <li>From the onCreate method of an Activity, find the Button
278 * <pre class="prettyprint">
279 * Button myButton = (Button) findViewById(R.id.my_button);
280 * </pre></li>
281 * </ul>
282 * <p>
283 * View IDs need not be unique throughout the tree, but it is good practice to
284 * ensure that they are at least unique within the part of the tree you are
285 * searching.
286 * </p>
287 *
288 * <a name="Position"></a>
289 * <h3>Position</h3>
290 * <p>
291 * The geometry of a view is that of a rectangle. A view has a location,
292 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
293 * two dimensions, expressed as a width and a height. The unit for location
294 * and dimensions is the pixel.
295 * </p>
296 *
297 * <p>
298 * It is possible to retrieve the location of a view by invoking the methods
299 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
300 * coordinate of the rectangle representing the view. The latter returns the
301 * top, or Y, coordinate of the rectangle representing the view. These methods
302 * both return the location of the view relative to its parent. For instance,
303 * when getLeft() returns 20, that means the view is located 20 pixels to the
304 * right of the left edge of its direct parent.
305 * </p>
306 *
307 * <p>
308 * In addition, several convenience methods are offered to avoid unnecessary
309 * computations, namely {@link #getRight()} and {@link #getBottom()}.
310 * These methods return the coordinates of the right and bottom edges of the
311 * rectangle representing the view. For instance, calling {@link #getRight()}
312 * is similar to the following computation: <code>getLeft() + getWidth()</code>
313 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
314 * </p>
315 *
316 * <a name="SizePaddingMargins"></a>
317 * <h3>Size, padding and margins</h3>
318 * <p>
319 * The size of a view is expressed with a width and a height. A view actually
320 * possess two pairs of width and height values.
321 * </p>
322 *
323 * <p>
324 * The first pair is known as <em>measured width</em> and
325 * <em>measured height</em>. These dimensions define how big a view wants to be
326 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
327 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
328 * and {@link #getMeasuredHeight()}.
329 * </p>
330 *
331 * <p>
332 * The second pair is simply known as <em>width</em> and <em>height</em>, or
333 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
334 * dimensions define the actual size of the view on screen, at drawing time and
335 * after layout. These values may, but do not have to, be different from the
336 * measured width and height. The width and height can be obtained by calling
337 * {@link #getWidth()} and {@link #getHeight()}.
338 * </p>
339 *
340 * <p>
341 * To measure its dimensions, a view takes into account its padding. The padding
342 * is expressed in pixels for the left, top, right and bottom parts of the view.
343 * Padding can be used to offset the content of the view by a specific amount of
344 * pixels. For instance, a left padding of 2 will push the view's content by
345 * 2 pixels to the right of the left edge. Padding can be set using the
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -0700346 * {@link #setPadding(int, int, int, int)} method and queried by calling
347 * {@link #getPaddingLeft()}, {@link #getPaddingTop()}, {@link #getPaddingRight()},
348 * {@link #getPaddingBottom()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 * </p>
350 *
351 * <p>
352 * Even though a view can define a padding, it does not provide any support for
353 * margins. However, view groups provide such a support. Refer to
354 * {@link android.view.ViewGroup} and
355 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
356 * </p>
357 *
358 * <a name="Layout"></a>
359 * <h3>Layout</h3>
360 * <p>
361 * Layout is a two pass process: a measure pass and a layout pass. The measuring
362 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
363 * of the view tree. Each view pushes dimension specifications down the tree
364 * during the recursion. At the end of the measure pass, every view has stored
365 * its measurements. The second pass happens in
366 * {@link #layout(int,int,int,int)} and is also top-down. During
367 * this pass each parent is responsible for positioning all of its children
368 * using the sizes computed in the measure pass.
369 * </p>
370 *
371 * <p>
372 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
373 * {@link #getMeasuredHeight()} values must be set, along with those for all of
374 * that view's descendants. A view's measured width and measured height values
375 * must respect the constraints imposed by the view's parents. This guarantees
376 * that at the end of the measure pass, all parents accept all of their
377 * children's measurements. A parent view may call measure() more than once on
378 * its children. For example, the parent may measure each child once with
379 * unspecified dimensions to find out how big they want to be, then call
380 * measure() on them again with actual numbers if the sum of all the children's
381 * unconstrained sizes is too big or too small.
382 * </p>
383 *
384 * <p>
385 * The measure pass uses two classes to communicate dimensions. The
386 * {@link MeasureSpec} class is used by views to tell their parents how they
387 * want to be measured and positioned. The base LayoutParams class just
388 * describes how big the view wants to be for both width and height. For each
389 * dimension, it can specify one of:
390 * <ul>
391 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800392 * <li>MATCH_PARENT, which means the view wants to be as big as its parent
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 * (minus padding)
394 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
395 * enclose its content (plus padding).
396 * </ul>
397 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
398 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
399 * an X and Y value.
400 * </p>
401 *
402 * <p>
403 * MeasureSpecs are used to push requirements down the tree from parent to
404 * child. A MeasureSpec can be in one of three modes:
405 * <ul>
406 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
407 * of a child view. For example, a LinearLayout may call measure() on its child
408 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
409 * tall the child view wants to be given a width of 240 pixels.
410 * <li>EXACTLY: This is used by the parent to impose an exact size on the
411 * child. The child must use this size, and guarantee that all of its
412 * descendants will fit within this size.
413 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
414 * child. The child must gurantee that it and all of its descendants will fit
415 * within this size.
416 * </ul>
417 * </p>
418 *
419 * <p>
420 * To intiate a layout, call {@link #requestLayout}. This method is typically
421 * called by a view on itself when it believes that is can no longer fit within
422 * its current bounds.
423 * </p>
424 *
425 * <a name="Drawing"></a>
426 * <h3>Drawing</h3>
427 * <p>
428 * Drawing is handled by walking the tree and rendering each view that
Joe Fernandez558459f2011-10-13 16:47:36 -0700429 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 * this means that parents will draw before (i.e., behind) their children, with
431 * siblings drawn in the order they appear in the tree.
432 * If you set a background drawable for a View, then the View will draw it for you
433 * before calling back to its <code>onDraw()</code> method.
434 * </p>
435 *
436 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700437 * Note that the framework will not draw views that are not in the invalid region.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 * </p>
439 *
440 * <p>
441 * To force a view to draw, call {@link #invalidate()}.
442 * </p>
443 *
444 * <a name="EventHandlingThreading"></a>
445 * <h3>Event Handling and Threading</h3>
446 * <p>
447 * The basic cycle of a view is as follows:
448 * <ol>
449 * <li>An event comes in and is dispatched to the appropriate view. The view
450 * handles the event and notifies any listeners.</li>
451 * <li>If in the course of processing the event, the view's bounds may need
452 * to be changed, the view will call {@link #requestLayout()}.</li>
453 * <li>Similarly, if in the course of processing the event the view's appearance
454 * may need to be changed, the view will call {@link #invalidate()}.</li>
455 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
456 * the framework will take care of measuring, laying out, and drawing the tree
457 * as appropriate.</li>
458 * </ol>
459 * </p>
460 *
461 * <p><em>Note: The entire view tree is single threaded. You must always be on
462 * the UI thread when calling any method on any view.</em>
463 * If you are doing work on other threads and want to update the state of a view
464 * from that thread, you should use a {@link Handler}.
465 * </p>
466 *
467 * <a name="FocusHandling"></a>
468 * <h3>Focus Handling</h3>
469 * <p>
470 * The framework will handle routine focus movement in response to user input.
471 * This includes changing the focus as views are removed or hidden, or as new
472 * views become available. Views indicate their willingness to take focus
473 * through the {@link #isFocusable} method. To change whether a view can take
474 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
475 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
476 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
477 * </p>
478 * <p>
479 * Focus movement is based on an algorithm which finds the nearest neighbor in a
480 * given direction. In rare cases, the default algorithm may not match the
481 * intended behavior of the developer. In these situations, you can provide
482 * explicit overrides by using these XML attributes in the layout file:
483 * <pre>
484 * nextFocusDown
485 * nextFocusLeft
486 * nextFocusRight
487 * nextFocusUp
488 * </pre>
489 * </p>
490 *
491 *
492 * <p>
493 * To get a particular view to take focus, call {@link #requestFocus()}.
494 * </p>
495 *
496 * <a name="TouchMode"></a>
497 * <h3>Touch Mode</h3>
498 * <p>
499 * When a user is navigating a user interface via directional keys such as a D-pad, it is
500 * necessary to give focus to actionable items such as buttons so the user can see
501 * what will take input. If the device has touch capabilities, however, and the user
502 * begins interacting with the interface by touching it, it is no longer necessary to
503 * always highlight, or give focus to, a particular view. This motivates a mode
504 * for interaction named 'touch mode'.
505 * </p>
506 * <p>
507 * For a touch capable device, once the user touches the screen, the device
508 * will enter touch mode. From this point onward, only views for which
509 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
510 * Other views that are touchable, like buttons, will not take focus when touched; they will
511 * only fire the on click listeners.
512 * </p>
513 * <p>
514 * Any time a user hits a directional key, such as a D-pad direction, the view device will
515 * exit touch mode, and find a view to take focus, so that the user may resume interacting
516 * with the user interface without touching the screen again.
517 * </p>
518 * <p>
519 * The touch mode state is maintained across {@link android.app.Activity}s. Call
520 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
521 * </p>
522 *
523 * <a name="Scrolling"></a>
524 * <h3>Scrolling</h3>
525 * <p>
526 * The framework provides basic support for views that wish to internally
527 * scroll their content. This includes keeping track of the X and Y scroll
528 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800529 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700530 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 * </p>
532 *
533 * <a name="Tags"></a>
534 * <h3>Tags</h3>
535 * <p>
536 * Unlike IDs, tags are not used to identify views. Tags are essentially an
537 * extra piece of information that can be associated with a view. They are most
538 * often used as a convenience to store data related to views in the views
539 * themselves rather than by putting them in a separate structure.
540 * </p>
541 *
Chet Haasecb150fe2012-05-03 15:15:05 -0700542 * <a name="Properties"></a>
543 * <h3>Properties</h3>
544 * <p>
545 * The View class exposes an {@link #ALPHA} property, as well as several transform-related
546 * properties, such as {@link #TRANSLATION_X} and {@link #TRANSLATION_Y}. These properties are
547 * available both in the {@link Property} form as well as in similarly-named setter/getter
548 * methods (such as {@link #setAlpha(float)} for {@link #ALPHA}). These properties can
549 * be used to set persistent state associated with these rendering-related properties on the view.
550 * The properties and methods can also be used in conjunction with
551 * {@link android.animation.Animator Animator}-based animations, described more in the
552 * <a href="#Animation">Animation</a> section.
553 * </p>
554 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 * <a name="Animation"></a>
556 * <h3>Animation</h3>
557 * <p>
Chet Haasecb150fe2012-05-03 15:15:05 -0700558 * Starting with Android 3.0, the preferred way of animating views is to use the
559 * {@link android.animation} package APIs. These {@link android.animation.Animator Animator}-based
560 * classes change actual properties of the View object, such as {@link #setAlpha(float) alpha} and
561 * {@link #setTranslationX(float) translationX}. This behavior is contrasted to that of the pre-3.0
562 * {@link android.view.animation.Animation Animation}-based classes, which instead animate only
563 * how the view is drawn on the display. In particular, the {@link ViewPropertyAnimator} class
564 * makes animating these View properties particularly easy and efficient.
565 * </p>
566 * <p>
567 * Alternatively, you can use the pre-3.0 animation classes to animate how Views are rendered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 * You can attach an {@link Animation} object to a view using
569 * {@link #setAnimation(Animation)} or
570 * {@link #startAnimation(Animation)}. The animation can alter the scale,
571 * rotation, translation and alpha of a view over time. If the animation is
572 * attached to a view that has children, the animation will affect the entire
573 * subtree rooted by that node. When an animation is started, the framework will
574 * take care of redrawing the appropriate views until the animation completes.
575 * </p>
576 *
Jeff Brown85a31762010-09-01 17:01:00 -0700577 * <a name="Security"></a>
578 * <h3>Security</h3>
579 * <p>
580 * Sometimes it is essential that an application be able to verify that an action
581 * is being performed with the full knowledge and consent of the user, such as
582 * granting a permission request, making a purchase or clicking on an advertisement.
583 * Unfortunately, a malicious application could try to spoof the user into
584 * performing these actions, unaware, by concealing the intended purpose of the view.
585 * As a remedy, the framework offers a touch filtering mechanism that can be used to
586 * improve the security of views that provide access to sensitive functionality.
587 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700588 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800589 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700590 * will discard touches that are received whenever the view's window is obscured by
591 * another visible window. As a result, the view will not receive touches whenever a
592 * toast, dialog or other window appears above the view's window.
593 * </p><p>
594 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700595 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
596 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700597 * </p>
598 *
Romain Guy171c5922011-01-06 10:04:23 -0800599 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700600 * @attr ref android.R.styleable#View_background
601 * @attr ref android.R.styleable#View_clickable
602 * @attr ref android.R.styleable#View_contentDescription
603 * @attr ref android.R.styleable#View_drawingCacheQuality
604 * @attr ref android.R.styleable#View_duplicateParentState
605 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700606 * @attr ref android.R.styleable#View_requiresFadingEdge
Philip Milne6c8ea062012-04-03 17:38:43 -0700607 * @attr ref android.R.styleable#View_fadeScrollbars
Romain Guyd6a463a2009-05-21 23:10:10 -0700608 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700609 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700611 * @attr ref android.R.styleable#View_isScrollContainer
612 * @attr ref android.R.styleable#View_focusable
613 * @attr ref android.R.styleable#View_focusableInTouchMode
614 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
615 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800616 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700617 * @attr ref android.R.styleable#View_longClickable
618 * @attr ref android.R.styleable#View_minHeight
619 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 * @attr ref android.R.styleable#View_nextFocusDown
621 * @attr ref android.R.styleable#View_nextFocusLeft
622 * @attr ref android.R.styleable#View_nextFocusRight
623 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700624 * @attr ref android.R.styleable#View_onClick
625 * @attr ref android.R.styleable#View_padding
626 * @attr ref android.R.styleable#View_paddingBottom
627 * @attr ref android.R.styleable#View_paddingLeft
628 * @attr ref android.R.styleable#View_paddingRight
629 * @attr ref android.R.styleable#View_paddingTop
630 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800631 * @attr ref android.R.styleable#View_rotation
632 * @attr ref android.R.styleable#View_rotationX
633 * @attr ref android.R.styleable#View_rotationY
634 * @attr ref android.R.styleable#View_scaleX
635 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 * @attr ref android.R.styleable#View_scrollX
637 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700638 * @attr ref android.R.styleable#View_scrollbarSize
639 * @attr ref android.R.styleable#View_scrollbarStyle
640 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700641 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
642 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
644 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 * @attr ref android.R.styleable#View_scrollbarThumbVertical
646 * @attr ref android.R.styleable#View_scrollbarTrackVertical
647 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
648 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700649 * @attr ref android.R.styleable#View_soundEffectsEnabled
650 * @attr ref android.R.styleable#View_tag
Chet Haase73066682010-11-29 15:55:32 -0800651 * @attr ref android.R.styleable#View_transformPivotX
652 * @attr ref android.R.styleable#View_transformPivotY
653 * @attr ref android.R.styleable#View_translationX
654 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700655 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 *
657 * @see android.view.ViewGroup
658 */
Adam Powell8fc54f92011-09-07 16:40:45 -0700659public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Callback,
660 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 private static final boolean DBG = false;
662
663 /**
664 * The logging tag used by this class with android.util.Log.
665 */
666 protected static final String VIEW_LOG_TAG = "View";
667
668 /**
Romain Guy4b8c4f82012-04-27 15:48:35 -0700669 * When set to true, apps will draw debugging information about their layouts.
670 *
671 * @hide
672 */
673 public static final String DEBUG_LAYOUT_PROPERTY = "debug.layout";
674
675 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 * Used to mark a View that has no ID.
677 */
678 public static final int NO_ID = -1;
679
680 /**
681 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
682 * calling setFlags.
683 */
684 private static final int NOT_FOCUSABLE = 0x00000000;
685
686 /**
687 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
688 * setFlags.
689 */
690 private static final int FOCUSABLE = 0x00000001;
691
692 /**
693 * Mask for use with setFlags indicating bits used for focus.
694 */
695 private static final int FOCUSABLE_MASK = 0x00000001;
696
697 /**
698 * This view will adjust its padding to fit sytem windows (e.g. status bar)
699 */
700 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
701
702 /**
Scott Main812634c22011-07-27 13:22:35 -0700703 * This view is visible.
704 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
705 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 */
707 public static final int VISIBLE = 0x00000000;
708
709 /**
710 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700711 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
712 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 */
714 public static final int INVISIBLE = 0x00000004;
715
716 /**
717 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700718 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
719 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 */
721 public static final int GONE = 0x00000008;
722
723 /**
724 * Mask for use with setFlags indicating bits used for visibility.
725 * {@hide}
726 */
727 static final int VISIBILITY_MASK = 0x0000000C;
728
729 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
730
731 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700732 * This view is enabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 * Use with ENABLED_MASK when calling setFlags.
734 * {@hide}
735 */
736 static final int ENABLED = 0x00000000;
737
738 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700739 * This view is disabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 * Use with ENABLED_MASK when calling setFlags.
741 * {@hide}
742 */
743 static final int DISABLED = 0x00000020;
744
745 /**
746 * Mask for use with setFlags indicating bits used for indicating whether
747 * this view is enabled
748 * {@hide}
749 */
750 static final int ENABLED_MASK = 0x00000020;
751
752 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700753 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
754 * called and further optimizations will be performed. It is okay to have
755 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 * {@hide}
757 */
758 static final int WILL_NOT_DRAW = 0x00000080;
759
760 /**
761 * Mask for use with setFlags indicating bits used for indicating whether
762 * this view is will draw
763 * {@hide}
764 */
765 static final int DRAW_MASK = 0x00000080;
766
767 /**
768 * <p>This view doesn't show scrollbars.</p>
769 * {@hide}
770 */
771 static final int SCROLLBARS_NONE = 0x00000000;
772
773 /**
774 * <p>This view shows horizontal scrollbars.</p>
775 * {@hide}
776 */
777 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
778
779 /**
780 * <p>This view shows vertical scrollbars.</p>
781 * {@hide}
782 */
783 static final int SCROLLBARS_VERTICAL = 0x00000200;
784
785 /**
786 * <p>Mask for use with setFlags indicating bits used for indicating which
787 * scrollbars are enabled.</p>
788 * {@hide}
789 */
790 static final int SCROLLBARS_MASK = 0x00000300;
791
Jeff Brown85a31762010-09-01 17:01:00 -0700792 /**
793 * Indicates that the view should filter touches when its window is obscured.
794 * Refer to the class comments for more information about this security feature.
795 * {@hide}
796 */
797 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
798
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700799 /**
800 * Set for framework elements that use FITS_SYSTEM_WINDOWS, to indicate
801 * that they are optional and should be skipped if the window has
802 * requested system UI flags that ignore those insets for layout.
803 */
804 static final int OPTIONAL_FITS_SYSTEM_WINDOWS = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800805
806 /**
807 * <p>This view doesn't show fading edges.</p>
808 * {@hide}
809 */
810 static final int FADING_EDGE_NONE = 0x00000000;
811
812 /**
813 * <p>This view shows horizontal fading edges.</p>
814 * {@hide}
815 */
816 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
817
818 /**
819 * <p>This view shows vertical fading edges.</p>
820 * {@hide}
821 */
822 static final int FADING_EDGE_VERTICAL = 0x00002000;
823
824 /**
825 * <p>Mask for use with setFlags indicating bits used for indicating which
826 * fading edges are enabled.</p>
827 * {@hide}
828 */
829 static final int FADING_EDGE_MASK = 0x00003000;
830
831 /**
832 * <p>Indicates this view can be clicked. When clickable, a View reacts
833 * to clicks by notifying the OnClickListener.<p>
834 * {@hide}
835 */
836 static final int CLICKABLE = 0x00004000;
837
838 /**
839 * <p>Indicates this view is caching its drawing into a bitmap.</p>
840 * {@hide}
841 */
842 static final int DRAWING_CACHE_ENABLED = 0x00008000;
843
844 /**
845 * <p>Indicates that no icicle should be saved for this view.<p>
846 * {@hide}
847 */
848 static final int SAVE_DISABLED = 0x000010000;
849
850 /**
851 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
852 * property.</p>
853 * {@hide}
854 */
855 static final int SAVE_DISABLED_MASK = 0x000010000;
856
857 /**
858 * <p>Indicates that no drawing cache should ever be created for this view.<p>
859 * {@hide}
860 */
861 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
862
863 /**
864 * <p>Indicates this view can take / keep focus when int touch mode.</p>
865 * {@hide}
866 */
867 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
868
869 /**
870 * <p>Enables low quality mode for the drawing cache.</p>
871 */
872 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
873
874 /**
875 * <p>Enables high quality mode for the drawing cache.</p>
876 */
877 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
878
879 /**
880 * <p>Enables automatic quality mode for the drawing cache.</p>
881 */
882 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
883
884 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
885 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
886 };
887
888 /**
889 * <p>Mask for use with setFlags indicating bits used for the cache
890 * quality property.</p>
891 * {@hide}
892 */
893 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
894
895 /**
896 * <p>
897 * Indicates this view can be long clicked. When long clickable, a View
898 * reacts to long clicks by notifying the OnLongClickListener or showing a
899 * context menu.
900 * </p>
901 * {@hide}
902 */
903 static final int LONG_CLICKABLE = 0x00200000;
904
905 /**
906 * <p>Indicates that this view gets its drawable states from its direct parent
907 * and ignores its original internal states.</p>
908 *
909 * @hide
910 */
911 static final int DUPLICATE_PARENT_STATE = 0x00400000;
912
913 /**
914 * The scrollbar style to display the scrollbars inside the content area,
915 * without increasing the padding. The scrollbars will be overlaid with
916 * translucency on the view's content.
917 */
918 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
919
920 /**
921 * The scrollbar style to display the scrollbars inside the padded area,
922 * increasing the padding of the view. The scrollbars will not overlap the
923 * content area of the view.
924 */
925 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
926
927 /**
928 * The scrollbar style to display the scrollbars at the edge of the view,
929 * without increasing the padding. The scrollbars will be overlaid with
930 * translucency.
931 */
932 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
933
934 /**
935 * The scrollbar style to display the scrollbars at the edge of the view,
936 * increasing the padding of the view. The scrollbars will only overlap the
937 * background, if any.
938 */
939 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
940
941 /**
942 * Mask to check if the scrollbar style is overlay or inset.
943 * {@hide}
944 */
945 static final int SCROLLBARS_INSET_MASK = 0x01000000;
946
947 /**
948 * Mask to check if the scrollbar style is inside or outside.
949 * {@hide}
950 */
951 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
952
953 /**
954 * Mask for scrollbar style.
955 * {@hide}
956 */
957 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
958
959 /**
960 * View flag indicating that the screen should remain on while the
961 * window containing this view is visible to the user. This effectively
962 * takes care of automatically setting the WindowManager's
963 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
964 */
965 public static final int KEEP_SCREEN_ON = 0x04000000;
966
967 /**
968 * View flag indicating whether this view should have sound effects enabled
969 * for events such as clicking and touching.
970 */
971 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
972
973 /**
974 * View flag indicating whether this view should have haptic feedback
975 * enabled for events such as long presses.
976 */
977 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
978
979 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700980 * <p>Indicates that the view hierarchy should stop saving state when
981 * it reaches this view. If state saving is initiated immediately at
982 * the view, it will be allowed.
983 * {@hide}
984 */
985 static final int PARENT_SAVE_DISABLED = 0x20000000;
986
987 /**
988 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
989 * {@hide}
990 */
991 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
992
993 /**
svetoslavganov75986cf2009-05-14 22:28:01 -0700994 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
995 * should add all focusable Views regardless if they are focusable in touch mode.
996 */
997 public static final int FOCUSABLES_ALL = 0x00000000;
998
999 /**
1000 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1001 * should add only Views focusable in touch mode.
1002 */
1003 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1004
1005 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07001006 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1007 * should add only accessibility focusable Views.
1008 *
1009 * @hide
1010 */
1011 public static final int FOCUSABLES_ACCESSIBILITY = 0x00000002;
1012
1013 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001014 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 * item.
1016 */
1017 public static final int FOCUS_BACKWARD = 0x00000001;
1018
1019 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001020 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 * item.
1022 */
1023 public static final int FOCUS_FORWARD = 0x00000002;
1024
1025 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001026 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 */
1028 public static final int FOCUS_LEFT = 0x00000011;
1029
1030 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001031 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 */
1033 public static final int FOCUS_UP = 0x00000021;
1034
1035 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001036 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 */
1038 public static final int FOCUS_RIGHT = 0x00000042;
1039
1040 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001041 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 */
1043 public static final int FOCUS_DOWN = 0x00000082;
1044
Svetoslav Ganov42138042012-03-20 11:51:39 -07001045 // Accessibility focus directions.
1046
1047 /**
1048 * The accessibility focus which is the current user position when
1049 * interacting with the accessibility framework.
1050 */
1051 public static final int FOCUS_ACCESSIBILITY = 0x00001000;
1052
1053 /**
1054 * Use with {@link #focusSearch(int)}. Move acessibility focus left.
1055 */
1056 public static final int ACCESSIBILITY_FOCUS_LEFT = FOCUS_LEFT | FOCUS_ACCESSIBILITY;
1057
1058 /**
1059 * Use with {@link #focusSearch(int)}. Move acessibility focus up.
1060 */
1061 public static final int ACCESSIBILITY_FOCUS_UP = FOCUS_UP | FOCUS_ACCESSIBILITY;
1062
1063 /**
1064 * Use with {@link #focusSearch(int)}. Move acessibility focus right.
1065 */
1066 public static final int ACCESSIBILITY_FOCUS_RIGHT = FOCUS_RIGHT | FOCUS_ACCESSIBILITY;
1067
1068 /**
1069 * Use with {@link #focusSearch(int)}. Move acessibility focus down.
1070 */
1071 public static final int ACCESSIBILITY_FOCUS_DOWN = FOCUS_DOWN | FOCUS_ACCESSIBILITY;
1072
1073 /**
Svetoslav Ganovd6e716d2012-04-20 18:36:11 -07001074 * Use with {@link #focusSearch(int)}. Move acessibility focus forward.
Svetoslav Ganov42138042012-03-20 11:51:39 -07001075 */
1076 public static final int ACCESSIBILITY_FOCUS_FORWARD = FOCUS_FORWARD | FOCUS_ACCESSIBILITY;
1077
1078 /**
Svetoslav Ganovd6e716d2012-04-20 18:36:11 -07001079 * Use with {@link #focusSearch(int)}. Move acessibility focus backward.
Svetoslav Ganov42138042012-03-20 11:51:39 -07001080 */
1081 public static final int ACCESSIBILITY_FOCUS_BACKWARD = FOCUS_BACKWARD | FOCUS_ACCESSIBILITY;
1082
1083 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001084 * Bits of {@link #getMeasuredWidthAndState()} and
1085 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1086 */
1087 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1088
1089 /**
1090 * Bits of {@link #getMeasuredWidthAndState()} and
1091 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1092 */
1093 public static final int MEASURED_STATE_MASK = 0xff000000;
1094
1095 /**
1096 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1097 * for functions that combine both width and height into a single int,
1098 * such as {@link #getMeasuredState()} and the childState argument of
1099 * {@link #resolveSizeAndState(int, int, int)}.
1100 */
1101 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1102
1103 /**
1104 * Bit of {@link #getMeasuredWidthAndState()} and
1105 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1106 * is smaller that the space the view would like to have.
1107 */
1108 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1109
1110 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 * Base View state sets
1112 */
1113 // Singles
1114 /**
1115 * Indicates the view has no states set. States are used with
1116 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1117 * view depending on its state.
1118 *
1119 * @see android.graphics.drawable.Drawable
1120 * @see #getDrawableState()
1121 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001122 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 /**
1124 * Indicates the view is enabled. States are used with
1125 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1126 * view depending on its state.
1127 *
1128 * @see android.graphics.drawable.Drawable
1129 * @see #getDrawableState()
1130 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001131 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 /**
1133 * Indicates the view is focused. States are used with
1134 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1135 * view depending on its state.
1136 *
1137 * @see android.graphics.drawable.Drawable
1138 * @see #getDrawableState()
1139 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001140 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 /**
1142 * Indicates the view is selected. States are used with
1143 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1144 * view depending on its state.
1145 *
1146 * @see android.graphics.drawable.Drawable
1147 * @see #getDrawableState()
1148 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001149 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 /**
1151 * Indicates the view is pressed. States are used with
1152 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1153 * view depending on its state.
1154 *
1155 * @see android.graphics.drawable.Drawable
1156 * @see #getDrawableState()
1157 * @hide
1158 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001159 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 /**
1161 * Indicates the view's window has focus. States are used with
1162 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1163 * view depending on its state.
1164 *
1165 * @see android.graphics.drawable.Drawable
1166 * @see #getDrawableState()
1167 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001168 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 // Doubles
1170 /**
1171 * Indicates the view is enabled and has the focus.
1172 *
1173 * @see #ENABLED_STATE_SET
1174 * @see #FOCUSED_STATE_SET
1175 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001176 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 /**
1178 * Indicates the view is enabled and selected.
1179 *
1180 * @see #ENABLED_STATE_SET
1181 * @see #SELECTED_STATE_SET
1182 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001183 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 /**
1185 * Indicates the view is enabled and that its window has focus.
1186 *
1187 * @see #ENABLED_STATE_SET
1188 * @see #WINDOW_FOCUSED_STATE_SET
1189 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001190 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 /**
1192 * Indicates the view is focused and selected.
1193 *
1194 * @see #FOCUSED_STATE_SET
1195 * @see #SELECTED_STATE_SET
1196 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001197 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 /**
1199 * Indicates the view has the focus and that its window has the focus.
1200 *
1201 * @see #FOCUSED_STATE_SET
1202 * @see #WINDOW_FOCUSED_STATE_SET
1203 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001204 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 /**
1206 * Indicates the view is selected and that its window has the focus.
1207 *
1208 * @see #SELECTED_STATE_SET
1209 * @see #WINDOW_FOCUSED_STATE_SET
1210 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001211 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 // Triples
1213 /**
1214 * Indicates the view is enabled, focused and selected.
1215 *
1216 * @see #ENABLED_STATE_SET
1217 * @see #FOCUSED_STATE_SET
1218 * @see #SELECTED_STATE_SET
1219 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001220 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 /**
1222 * Indicates the view is enabled, focused and its window has the focus.
1223 *
1224 * @see #ENABLED_STATE_SET
1225 * @see #FOCUSED_STATE_SET
1226 * @see #WINDOW_FOCUSED_STATE_SET
1227 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001228 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 /**
1230 * Indicates the view is enabled, selected and its window has the focus.
1231 *
1232 * @see #ENABLED_STATE_SET
1233 * @see #SELECTED_STATE_SET
1234 * @see #WINDOW_FOCUSED_STATE_SET
1235 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001236 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 /**
1238 * Indicates the view is focused, selected and its window has the focus.
1239 *
1240 * @see #FOCUSED_STATE_SET
1241 * @see #SELECTED_STATE_SET
1242 * @see #WINDOW_FOCUSED_STATE_SET
1243 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001244 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 /**
1246 * Indicates the view is enabled, focused, selected and its window
1247 * has the focus.
1248 *
1249 * @see #ENABLED_STATE_SET
1250 * @see #FOCUSED_STATE_SET
1251 * @see #SELECTED_STATE_SET
1252 * @see #WINDOW_FOCUSED_STATE_SET
1253 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001254 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 /**
1256 * Indicates the view is pressed and its window has the focus.
1257 *
1258 * @see #PRESSED_STATE_SET
1259 * @see #WINDOW_FOCUSED_STATE_SET
1260 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001261 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 /**
1263 * Indicates the view is pressed and selected.
1264 *
1265 * @see #PRESSED_STATE_SET
1266 * @see #SELECTED_STATE_SET
1267 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001268 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269 /**
1270 * Indicates the view is pressed, selected and its window has the focus.
1271 *
1272 * @see #PRESSED_STATE_SET
1273 * @see #SELECTED_STATE_SET
1274 * @see #WINDOW_FOCUSED_STATE_SET
1275 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001276 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 /**
1278 * Indicates the view is pressed and focused.
1279 *
1280 * @see #PRESSED_STATE_SET
1281 * @see #FOCUSED_STATE_SET
1282 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001283 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 /**
1285 * Indicates the view is pressed, focused and its window has the focus.
1286 *
1287 * @see #PRESSED_STATE_SET
1288 * @see #FOCUSED_STATE_SET
1289 * @see #WINDOW_FOCUSED_STATE_SET
1290 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001291 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 /**
1293 * Indicates the view is pressed, focused and selected.
1294 *
1295 * @see #PRESSED_STATE_SET
1296 * @see #SELECTED_STATE_SET
1297 * @see #FOCUSED_STATE_SET
1298 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001299 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 /**
1301 * Indicates the view is pressed, focused, selected and its window has the focus.
1302 *
1303 * @see #PRESSED_STATE_SET
1304 * @see #FOCUSED_STATE_SET
1305 * @see #SELECTED_STATE_SET
1306 * @see #WINDOW_FOCUSED_STATE_SET
1307 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001308 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 /**
1310 * Indicates the view is pressed and enabled.
1311 *
1312 * @see #PRESSED_STATE_SET
1313 * @see #ENABLED_STATE_SET
1314 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001315 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 /**
1317 * Indicates the view is pressed, enabled and its window has the focus.
1318 *
1319 * @see #PRESSED_STATE_SET
1320 * @see #ENABLED_STATE_SET
1321 * @see #WINDOW_FOCUSED_STATE_SET
1322 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001323 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 /**
1325 * Indicates the view is pressed, enabled and selected.
1326 *
1327 * @see #PRESSED_STATE_SET
1328 * @see #ENABLED_STATE_SET
1329 * @see #SELECTED_STATE_SET
1330 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001331 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 /**
1333 * Indicates the view is pressed, enabled, selected and its window has the
1334 * focus.
1335 *
1336 * @see #PRESSED_STATE_SET
1337 * @see #ENABLED_STATE_SET
1338 * @see #SELECTED_STATE_SET
1339 * @see #WINDOW_FOCUSED_STATE_SET
1340 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001341 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 /**
1343 * Indicates the view is pressed, enabled and focused.
1344 *
1345 * @see #PRESSED_STATE_SET
1346 * @see #ENABLED_STATE_SET
1347 * @see #FOCUSED_STATE_SET
1348 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001349 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 /**
1351 * Indicates the view is pressed, enabled, focused and its window has the
1352 * focus.
1353 *
1354 * @see #PRESSED_STATE_SET
1355 * @see #ENABLED_STATE_SET
1356 * @see #FOCUSED_STATE_SET
1357 * @see #WINDOW_FOCUSED_STATE_SET
1358 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001359 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 /**
1361 * Indicates the view is pressed, enabled, focused and selected.
1362 *
1363 * @see #PRESSED_STATE_SET
1364 * @see #ENABLED_STATE_SET
1365 * @see #SELECTED_STATE_SET
1366 * @see #FOCUSED_STATE_SET
1367 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001368 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 /**
1370 * Indicates the view is pressed, enabled, focused, selected and its window
1371 * has the focus.
1372 *
1373 * @see #PRESSED_STATE_SET
1374 * @see #ENABLED_STATE_SET
1375 * @see #SELECTED_STATE_SET
1376 * @see #FOCUSED_STATE_SET
1377 * @see #WINDOW_FOCUSED_STATE_SET
1378 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001379 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380
1381 /**
1382 * The order here is very important to {@link #getDrawableState()}
1383 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001384 private static final int[][] VIEW_STATE_SETS;
1385
Romain Guyb051e892010-09-28 19:09:36 -07001386 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1387 static final int VIEW_STATE_SELECTED = 1 << 1;
1388 static final int VIEW_STATE_FOCUSED = 1 << 2;
1389 static final int VIEW_STATE_ENABLED = 1 << 3;
1390 static final int VIEW_STATE_PRESSED = 1 << 4;
1391 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001392 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001393 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001394 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1395 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001396
1397 static final int[] VIEW_STATE_IDS = new int[] {
1398 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1399 R.attr.state_selected, VIEW_STATE_SELECTED,
1400 R.attr.state_focused, VIEW_STATE_FOCUSED,
1401 R.attr.state_enabled, VIEW_STATE_ENABLED,
1402 R.attr.state_pressed, VIEW_STATE_PRESSED,
1403 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001404 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001405 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001406 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
Svetoslav Ganov42138042012-03-20 11:51:39 -07001407 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 };
1409
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001410 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001411 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1412 throw new IllegalStateException(
1413 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1414 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001415 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001416 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001417 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001418 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001419 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001420 orderedIds[i * 2] = viewState;
1421 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001422 }
1423 }
1424 }
Romain Guyb051e892010-09-28 19:09:36 -07001425 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1426 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1427 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001428 int numBits = Integer.bitCount(i);
1429 int[] set = new int[numBits];
1430 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001431 for (int j = 0; j < orderedIds.length; j += 2) {
1432 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001433 set[pos++] = orderedIds[j];
1434 }
1435 }
1436 VIEW_STATE_SETS[i] = set;
1437 }
1438
1439 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1440 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1441 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1442 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1443 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1444 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1445 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1446 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1447 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1448 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1449 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1450 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1451 | VIEW_STATE_FOCUSED];
1452 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1453 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1454 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1455 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1456 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1457 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1458 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1459 | VIEW_STATE_ENABLED];
1460 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1461 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1462 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1463 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1464 | VIEW_STATE_ENABLED];
1465 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1466 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1467 | VIEW_STATE_ENABLED];
1468 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1469 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1470 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1471
1472 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1473 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1474 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1475 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1476 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1477 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1478 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1479 | VIEW_STATE_PRESSED];
1480 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1481 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1482 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1483 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1484 | VIEW_STATE_PRESSED];
1485 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1486 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1487 | VIEW_STATE_PRESSED];
1488 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1489 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1490 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1491 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1492 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1493 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1494 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1495 | VIEW_STATE_PRESSED];
1496 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1497 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1498 | VIEW_STATE_PRESSED];
1499 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1500 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1501 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1502 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1503 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1504 | VIEW_STATE_PRESSED];
1505 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1506 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1507 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1508 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1509 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1510 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1511 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1512 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1513 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1514 | VIEW_STATE_PRESSED];
1515 }
1516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001518 * Accessibility event types that are dispatched for text population.
1519 */
1520 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1521 AccessibilityEvent.TYPE_VIEW_CLICKED
1522 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1523 | AccessibilityEvent.TYPE_VIEW_SELECTED
1524 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1525 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1526 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001527 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
Svetoslav Ganov84dd52e2011-11-18 10:24:00 -08001528 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
Svetoslav Ganov42138042012-03-20 11:51:39 -07001529 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001530 | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED
1531 | AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001532
1533 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534 * Temporary Rect currently for use in setBackground(). This will probably
1535 * be extended in the future to hold our own class with more than just
1536 * a Rect. :)
1537 */
1538 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001539
1540 /**
1541 * Map used to store views' tags.
1542 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001543 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001546 * The next available accessiiblity id.
1547 */
1548 private static int sNextAccessibilityViewId;
1549
1550 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 * The animation currently associated with this view.
1552 * @hide
1553 */
1554 protected Animation mCurrentAnimation = null;
1555
1556 /**
1557 * Width as measured during measure pass.
1558 * {@hide}
1559 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001560 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001561 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562
1563 /**
1564 * Height as measured during measure pass.
1565 * {@hide}
1566 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001567 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001568 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569
1570 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001571 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1572 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1573 * its display list. This flag, used only when hw accelerated, allows us to clear the
1574 * flag while retaining this information until it's needed (at getDisplayList() time and
1575 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1576 *
1577 * {@hide}
1578 */
1579 boolean mRecreateDisplayList = false;
1580
1581 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001582 * The view's identifier.
1583 * {@hide}
1584 *
1585 * @see #setId(int)
1586 * @see #getId()
1587 */
1588 @ViewDebug.ExportedProperty(resolveId = true)
1589 int mID = NO_ID;
1590
1591 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001592 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001593 */
1594 int mAccessibilityViewId = NO_ID;
1595
1596 /**
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001597 * @hide
1598 */
1599 private int mAccessibilityCursorPosition = -1;
1600
1601 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 * The view's tag.
1603 * {@hide}
1604 *
1605 * @see #setTag(Object)
1606 * @see #getTag()
1607 */
1608 protected Object mTag;
1609
1610 // for mPrivateFlags:
1611 /** {@hide} */
1612 static final int WANTS_FOCUS = 0x00000001;
1613 /** {@hide} */
1614 static final int FOCUSED = 0x00000002;
1615 /** {@hide} */
1616 static final int SELECTED = 0x00000004;
1617 /** {@hide} */
1618 static final int IS_ROOT_NAMESPACE = 0x00000008;
1619 /** {@hide} */
1620 static final int HAS_BOUNDS = 0x00000010;
1621 /** {@hide} */
1622 static final int DRAWN = 0x00000020;
1623 /**
1624 * When this flag is set, this view is running an animation on behalf of its
1625 * children and should therefore not cancel invalidate requests, even if they
1626 * lie outside of this view's bounds.
1627 *
1628 * {@hide}
1629 */
1630 static final int DRAW_ANIMATION = 0x00000040;
1631 /** {@hide} */
1632 static final int SKIP_DRAW = 0x00000080;
1633 /** {@hide} */
1634 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1635 /** {@hide} */
1636 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1637 /** {@hide} */
1638 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1639 /** {@hide} */
1640 static final int MEASURED_DIMENSION_SET = 0x00000800;
1641 /** {@hide} */
1642 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001643 /** {@hide} */
1644 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001645
1646 private static final int PRESSED = 0x00004000;
1647
1648 /** {@hide} */
1649 static final int DRAWING_CACHE_VALID = 0x00008000;
1650 /**
1651 * Flag used to indicate that this view should be drawn once more (and only once
1652 * more) after its animation has completed.
1653 * {@hide}
1654 */
1655 static final int ANIMATION_STARTED = 0x00010000;
1656
1657 private static final int SAVE_STATE_CALLED = 0x00020000;
1658
1659 /**
1660 * Indicates that the View returned true when onSetAlpha() was called and that
1661 * the alpha must be restored.
1662 * {@hide}
1663 */
1664 static final int ALPHA_SET = 0x00040000;
1665
1666 /**
1667 * Set by {@link #setScrollContainer(boolean)}.
1668 */
1669 static final int SCROLL_CONTAINER = 0x00080000;
1670
1671 /**
1672 * Set by {@link #setScrollContainer(boolean)}.
1673 */
1674 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1675
1676 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001677 * View flag indicating whether this view was invalidated (fully or partially.)
1678 *
1679 * @hide
1680 */
1681 static final int DIRTY = 0x00200000;
1682
1683 /**
1684 * View flag indicating whether this view was invalidated by an opaque
1685 * invalidate request.
1686 *
1687 * @hide
1688 */
1689 static final int DIRTY_OPAQUE = 0x00400000;
1690
1691 /**
1692 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1693 *
1694 * @hide
1695 */
1696 static final int DIRTY_MASK = 0x00600000;
1697
1698 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001699 * Indicates whether the background is opaque.
1700 *
1701 * @hide
1702 */
1703 static final int OPAQUE_BACKGROUND = 0x00800000;
1704
1705 /**
1706 * Indicates whether the scrollbars are opaque.
1707 *
1708 * @hide
1709 */
1710 static final int OPAQUE_SCROLLBARS = 0x01000000;
1711
1712 /**
1713 * Indicates whether the view is opaque.
1714 *
1715 * @hide
1716 */
1717 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001718
Adam Powelle14579b2009-12-16 18:39:52 -08001719 /**
1720 * Indicates a prepressed state;
1721 * the short time between ACTION_DOWN and recognizing
1722 * a 'real' press. Prepressed is used to recognize quick taps
1723 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001724 *
Adam Powelle14579b2009-12-16 18:39:52 -08001725 * @hide
1726 */
1727 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001728
Adam Powellc9fbaab2010-02-16 17:16:19 -08001729 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001730 * Indicates whether the view is temporarily detached.
1731 *
1732 * @hide
1733 */
1734 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001735
Adam Powell8568c3a2010-04-19 14:26:11 -07001736 /**
1737 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001738 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001739 * @hide
1740 */
1741 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001742
1743 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001744 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1745 * @hide
1746 */
1747 private static final int HOVERED = 0x10000000;
1748
1749 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001750 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1751 * for transform operations
1752 *
1753 * @hide
1754 */
Adam Powellf37df072010-09-17 16:22:49 -07001755 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001756
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001757 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001758 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001759
Chet Haasefd2b0022010-08-06 13:08:56 -07001760 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001761 * Indicates that this view was specifically invalidated, not just dirtied because some
1762 * child view was invalidated. The flag is used to determine when we need to recreate
1763 * a view's display list (as opposed to just returning a reference to its existing
1764 * display list).
1765 *
1766 * @hide
1767 */
1768 static final int INVALIDATED = 0x80000000;
1769
Christopher Tate3d4bf172011-03-28 16:16:46 -07001770 /* Masks for mPrivateFlags2 */
1771
1772 /**
1773 * Indicates that this view has reported that it can accept the current drag's content.
1774 * Cleared when the drag operation concludes.
1775 * @hide
1776 */
1777 static final int DRAG_CAN_ACCEPT = 0x00000001;
1778
1779 /**
1780 * Indicates that this view is currently directly under the drag location in a
1781 * drag-and-drop operation involving content that it can accept. Cleared when
1782 * the drag exits the view, or when the drag operation concludes.
1783 * @hide
1784 */
1785 static final int DRAG_HOVERED = 0x00000002;
1786
Cibu Johny86666632010-02-22 13:01:02 -08001787 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001788 * Horizontal layout direction of this view is from Left to Right.
1789 * Use with {@link #setLayoutDirection}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001790 * @hide
Cibu Johny86666632010-02-22 13:01:02 -08001791 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001792 public static final int LAYOUT_DIRECTION_LTR = 0;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001793
1794 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001795 * Horizontal layout direction of this view is from Right to Left.
1796 * Use with {@link #setLayoutDirection}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001797 * @hide
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001798 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001799 public static final int LAYOUT_DIRECTION_RTL = 1;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001800
1801 /**
1802 * Horizontal layout direction of this view is inherited from its parent.
1803 * Use with {@link #setLayoutDirection}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001804 * @hide
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001805 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001806 public static final int LAYOUT_DIRECTION_INHERIT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001807
1808 /**
1809 * Horizontal layout direction of this view is from deduced from the default language
1810 * script for the locale. Use with {@link #setLayoutDirection}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001811 * @hide
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001812 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001813 public static final int LAYOUT_DIRECTION_LOCALE = 3;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001814
1815 /**
1816 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001817 * @hide
1818 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001819 static final int LAYOUT_DIRECTION_MASK_SHIFT = 2;
1820
1821 /**
1822 * Mask for use with private flags indicating bits used for horizontal layout direction.
1823 * @hide
1824 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001825 static final int LAYOUT_DIRECTION_MASK = 0x00000003 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001826
1827 /**
1828 * Indicates whether the view horizontal layout direction has been resolved and drawn to the
1829 * right-to-left direction.
1830 * @hide
1831 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001832 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 4 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001833
1834 /**
1835 * Indicates whether the view horizontal layout direction has been resolved.
1836 * @hide
1837 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001838 static final int LAYOUT_DIRECTION_RESOLVED = 8 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001839
1840 /**
1841 * Mask for use with private flags indicating bits used for resolved horizontal layout direction.
1842 * @hide
1843 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001844 static final int LAYOUT_DIRECTION_RESOLVED_MASK = 0x0000000C << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001845
1846 /*
1847 * Array of horizontal layout direction flags for mapping attribute "layoutDirection" to correct
1848 * flag value.
1849 * @hide
1850 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001851 private static final int[] LAYOUT_DIRECTION_FLAGS = {
1852 LAYOUT_DIRECTION_LTR,
1853 LAYOUT_DIRECTION_RTL,
1854 LAYOUT_DIRECTION_INHERIT,
1855 LAYOUT_DIRECTION_LOCALE
1856 };
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001857
1858 /**
1859 * Default horizontal layout direction.
1860 * @hide
1861 */
1862 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001863
Adam Powell539ee872012-02-03 19:00:49 -08001864 /**
1865 * Indicates that the view is tracking some sort of transient state
1866 * that the app should not need to be aware of, but that the framework
1867 * should take special care to preserve.
1868 *
1869 * @hide
1870 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001871 static final int HAS_TRANSIENT_STATE = 0x00000100;
Adam Powell539ee872012-02-03 19:00:49 -08001872
1873
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001874 /**
1875 * Text direction is inherited thru {@link ViewGroup}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001876 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001877 */
1878 public static final int TEXT_DIRECTION_INHERIT = 0;
1879
1880 /**
1881 * Text direction is using "first strong algorithm". The first strong directional character
1882 * determines the paragraph direction. If there is no strong directional character, the
1883 * paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001884 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001885 */
1886 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
1887
1888 /**
1889 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
1890 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
1891 * If there are neither, the paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001892 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001893 */
1894 public static final int TEXT_DIRECTION_ANY_RTL = 2;
1895
1896 /**
1897 * Text direction is forced to LTR.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001898 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001899 */
1900 public static final int TEXT_DIRECTION_LTR = 3;
1901
1902 /**
1903 * Text direction is forced to RTL.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001904 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001905 */
1906 public static final int TEXT_DIRECTION_RTL = 4;
1907
1908 /**
1909 * Text direction is coming from the system Locale.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001910 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001911 */
1912 public static final int TEXT_DIRECTION_LOCALE = 5;
1913
1914 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001915 * Default text direction is inherited
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001916 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001917 */
1918 protected static int TEXT_DIRECTION_DEFAULT = TEXT_DIRECTION_INHERIT;
1919
1920 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001921 * Bit shift to get the horizontal layout direction. (bits after LAYOUT_DIRECTION_RESOLVED)
1922 * @hide
1923 */
1924 static final int TEXT_DIRECTION_MASK_SHIFT = 6;
1925
1926 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001927 * Mask for use with private flags indicating bits used for text direction.
1928 * @hide
1929 */
1930 static final int TEXT_DIRECTION_MASK = 0x00000007 << TEXT_DIRECTION_MASK_SHIFT;
1931
1932 /**
1933 * Array of text direction flags for mapping attribute "textDirection" to correct
1934 * flag value.
1935 * @hide
1936 */
1937 private static final int[] TEXT_DIRECTION_FLAGS = {
1938 TEXT_DIRECTION_INHERIT << TEXT_DIRECTION_MASK_SHIFT,
1939 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_MASK_SHIFT,
1940 TEXT_DIRECTION_ANY_RTL << TEXT_DIRECTION_MASK_SHIFT,
1941 TEXT_DIRECTION_LTR << TEXT_DIRECTION_MASK_SHIFT,
1942 TEXT_DIRECTION_RTL << TEXT_DIRECTION_MASK_SHIFT,
1943 TEXT_DIRECTION_LOCALE << TEXT_DIRECTION_MASK_SHIFT
1944 };
1945
1946 /**
1947 * Indicates whether the view text direction has been resolved.
1948 * @hide
1949 */
1950 static final int TEXT_DIRECTION_RESOLVED = 0x00000008 << TEXT_DIRECTION_MASK_SHIFT;
1951
1952 /**
1953 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1954 * @hide
1955 */
1956 static final int TEXT_DIRECTION_RESOLVED_MASK_SHIFT = 10;
1957
1958 /**
1959 * Mask for use with private flags indicating bits used for resolved text direction.
1960 * @hide
1961 */
1962 static final int TEXT_DIRECTION_RESOLVED_MASK = 0x00000007 << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1963
1964 /**
1965 * Indicates whether the view text direction has been resolved to the "first strong" heuristic.
1966 * @hide
1967 */
1968 static final int TEXT_DIRECTION_RESOLVED_DEFAULT =
1969 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1970
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001971 /*
1972 * Default text alignment. The text alignment of this View is inherited from its parent.
1973 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001974 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001975 */
1976 public static final int TEXT_ALIGNMENT_INHERIT = 0;
1977
1978 /**
1979 * Default for the root view. The gravity determines the text alignment, ALIGN_NORMAL,
1980 * ALIGN_CENTER, or ALIGN_OPPOSITE, which are relative to each paragraph’s text direction.
1981 *
1982 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001983 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001984 */
1985 public static final int TEXT_ALIGNMENT_GRAVITY = 1;
1986
1987 /**
1988 * Align to the start of the paragraph, e.g. ALIGN_NORMAL.
1989 *
1990 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001991 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001992 */
1993 public static final int TEXT_ALIGNMENT_TEXT_START = 2;
1994
1995 /**
1996 * Align to the end of the paragraph, e.g. ALIGN_OPPOSITE.
1997 *
1998 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001999 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002000 */
2001 public static final int TEXT_ALIGNMENT_TEXT_END = 3;
2002
2003 /**
2004 * Center the paragraph, e.g. ALIGN_CENTER.
2005 *
2006 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07002007 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002008 */
2009 public static final int TEXT_ALIGNMENT_CENTER = 4;
2010
2011 /**
2012 * Align to the start of the view, which is ALIGN_LEFT if the view’s resolved
2013 * layoutDirection is LTR, and ALIGN_RIGHT otherwise.
2014 *
2015 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07002016 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002017 */
2018 public static final int TEXT_ALIGNMENT_VIEW_START = 5;
2019
2020 /**
2021 * Align to the end of the view, which is ALIGN_RIGHT if the view’s resolved
2022 * layoutDirection is LTR, and ALIGN_LEFT otherwise.
2023 *
2024 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07002025 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002026 */
2027 public static final int TEXT_ALIGNMENT_VIEW_END = 6;
2028
2029 /**
2030 * Default text alignment is inherited
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07002031 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002032 */
2033 protected static int TEXT_ALIGNMENT_DEFAULT = TEXT_ALIGNMENT_GRAVITY;
2034
2035 /**
2036 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
2037 * @hide
2038 */
2039 static final int TEXT_ALIGNMENT_MASK_SHIFT = 13;
2040
2041 /**
2042 * Mask for use with private flags indicating bits used for text alignment.
2043 * @hide
2044 */
2045 static final int TEXT_ALIGNMENT_MASK = 0x00000007 << TEXT_ALIGNMENT_MASK_SHIFT;
2046
2047 /**
2048 * Array of text direction flags for mapping attribute "textAlignment" to correct
2049 * flag value.
2050 * @hide
2051 */
2052 private static final int[] TEXT_ALIGNMENT_FLAGS = {
2053 TEXT_ALIGNMENT_INHERIT << TEXT_ALIGNMENT_MASK_SHIFT,
2054 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_MASK_SHIFT,
2055 TEXT_ALIGNMENT_TEXT_START << TEXT_ALIGNMENT_MASK_SHIFT,
2056 TEXT_ALIGNMENT_TEXT_END << TEXT_ALIGNMENT_MASK_SHIFT,
2057 TEXT_ALIGNMENT_CENTER << TEXT_ALIGNMENT_MASK_SHIFT,
2058 TEXT_ALIGNMENT_VIEW_START << TEXT_ALIGNMENT_MASK_SHIFT,
2059 TEXT_ALIGNMENT_VIEW_END << TEXT_ALIGNMENT_MASK_SHIFT
2060 };
2061
2062 /**
2063 * Indicates whether the view text alignment has been resolved.
2064 * @hide
2065 */
2066 static final int TEXT_ALIGNMENT_RESOLVED = 0x00000008 << TEXT_ALIGNMENT_MASK_SHIFT;
2067
2068 /**
2069 * Bit shift to get the resolved text alignment.
2070 * @hide
2071 */
2072 static final int TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT = 17;
2073
2074 /**
2075 * Mask for use with private flags indicating bits used for text alignment.
2076 * @hide
2077 */
2078 static final int TEXT_ALIGNMENT_RESOLVED_MASK = 0x00000007 << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
2079
2080 /**
2081 * Indicates whether if the view text alignment has been resolved to gravity
2082 */
2083 public static final int TEXT_ALIGNMENT_RESOLVED_DEFAULT =
2084 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
2085
Svetoslav Ganov42138042012-03-20 11:51:39 -07002086 // Accessiblity constants for mPrivateFlags2
2087
2088 /**
2089 * Shift for accessibility related bits in {@link #mPrivateFlags2}.
2090 */
2091 static final int IMPORTANT_FOR_ACCESSIBILITY_SHIFT = 20;
2092
2093 /**
2094 * Automatically determine whether a view is important for accessibility.
2095 */
2096 public static final int IMPORTANT_FOR_ACCESSIBILITY_AUTO = 0x00000000;
2097
2098 /**
2099 * The view is important for accessibility.
2100 */
2101 public static final int IMPORTANT_FOR_ACCESSIBILITY_YES = 0x00000001;
2102
2103 /**
2104 * The view is not important for accessibility.
2105 */
2106 public static final int IMPORTANT_FOR_ACCESSIBILITY_NO = 0x00000002;
2107
2108 /**
2109 * The default whether the view is important for accessiblity.
2110 */
2111 static final int IMPORTANT_FOR_ACCESSIBILITY_DEFAULT = IMPORTANT_FOR_ACCESSIBILITY_AUTO;
2112
2113 /**
2114 * Mask for obtainig the bits which specify how to determine
2115 * whether a view is important for accessibility.
2116 */
2117 static final int IMPORTANT_FOR_ACCESSIBILITY_MASK = (IMPORTANT_FOR_ACCESSIBILITY_AUTO
2118 | IMPORTANT_FOR_ACCESSIBILITY_YES | IMPORTANT_FOR_ACCESSIBILITY_NO)
2119 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
2120
2121 /**
2122 * Flag indicating whether a view has accessibility focus.
2123 */
2124 static final int ACCESSIBILITY_FOCUSED = 0x00000040 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
2125
2126 /**
2127 * Flag indicating whether a view state for accessibility has changed.
2128 */
2129 static final int ACCESSIBILITY_STATE_CHANGED = 0x00000080 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07002130
Christopher Tate3d4bf172011-03-28 16:16:46 -07002131 /* End of masks for mPrivateFlags2 */
2132
2133 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
2134
Chet Haasedaf98e92011-01-10 14:10:36 -08002135 /**
Adam Powell637d3372010-08-25 14:37:03 -07002136 * Always allow a user to over-scroll this view, provided it is a
2137 * view that can scroll.
2138 *
2139 * @see #getOverScrollMode()
2140 * @see #setOverScrollMode(int)
2141 */
2142 public static final int OVER_SCROLL_ALWAYS = 0;
2143
2144 /**
2145 * Allow a user to over-scroll this view only if the content is large
2146 * enough to meaningfully scroll, provided it is a view that can scroll.
2147 *
2148 * @see #getOverScrollMode()
2149 * @see #setOverScrollMode(int)
2150 */
2151 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
2152
2153 /**
2154 * Never allow a user to over-scroll this view.
2155 *
2156 * @see #getOverScrollMode()
2157 * @see #setOverScrollMode(int)
2158 */
2159 public static final int OVER_SCROLL_NEVER = 2;
2160
2161 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002162 * Special constant for {@link #setSystemUiVisibility(int)}: View has
2163 * requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08002164 *
Joe Malin32736f02011-01-19 16:14:20 -08002165 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002166 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002167 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08002168
2169 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002170 * Flag for {@link #setSystemUiVisibility(int)}: View has requested the
2171 * system UI to enter an unobtrusive "low profile" mode.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002172 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002173 * <p>This is for use in games, book readers, video players, or any other
Philip Milne6c8ea062012-04-03 17:38:43 -07002174 * "immersive" application where the usual system chrome is deemed too distracting.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002175 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002176 * <p>In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08002177 *
Joe Malin32736f02011-01-19 16:14:20 -08002178 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002179 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002180 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
2181
2182 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002183 * Flag for {@link #setSystemUiVisibility(int)}: View has requested that the
2184 * system navigation be temporarily hidden.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002185 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002186 * <p>This is an even less obtrusive state than that called for by
Daniel Sandler60ee2562011-07-22 12:34:33 -04002187 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
2188 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
2189 * those to disappear. This is useful (in conjunction with the
Philip Milne6c8ea062012-04-03 17:38:43 -07002190 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
Daniel Sandler60ee2562011-07-22 12:34:33 -04002191 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
2192 * window flags) for displaying content using every last pixel on the display.
2193 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002194 * <p>There is a limitation: because navigation controls are so important, the least user
2195 * interaction will cause them to reappear immediately. When this happens, both
2196 * this flag and {@link #SYSTEM_UI_FLAG_FULLSCREEN} will be cleared automatically,
2197 * so that both elements reappear at the same time.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002198 *
2199 * @see #setSystemUiVisibility(int)
2200 */
2201 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
2202
2203 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002204 * Flag for {@link #setSystemUiVisibility(int)}: View has requested to go
2205 * into the normal fullscreen mode so that its content can take over the screen
2206 * while still allowing the user to interact with the application.
2207 *
2208 * <p>This has the same visual effect as
2209 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN
2210 * WindowManager.LayoutParams.FLAG_FULLSCREEN},
2211 * meaning that non-critical screen decorations (such as the status bar) will be
2212 * hidden while the user is in the View's window, focusing the experience on
2213 * that content. Unlike the window flag, if you are using ActionBar in
2214 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2215 * Window.FEATURE_ACTION_BAR_OVERLAY}, then enabling this flag will also
2216 * hide the action bar.
2217 *
2218 * <p>This approach to going fullscreen is best used over the window flag when
2219 * it is a transient state -- that is, the application does this at certain
2220 * points in its user interaction where it wants to allow the user to focus
2221 * on content, but not as a continuous state. For situations where the application
2222 * would like to simply stay full screen the entire time (such as a game that
2223 * wants to take over the screen), the
2224 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN window flag}
2225 * is usually a better approach. The state set here will be removed by the system
2226 * in various situations (such as the user moving to another application) like
2227 * the other system UI states.
2228 *
2229 * <p>When using this flag, the application should provide some easy facility
2230 * for the user to go out of it. A common example would be in an e-book
2231 * reader, where tapping on the screen brings back whatever screen and UI
2232 * decorations that had been hidden while the user was immersed in reading
2233 * the book.
2234 *
2235 * @see #setSystemUiVisibility(int)
2236 */
2237 public static final int SYSTEM_UI_FLAG_FULLSCREEN = 0x00000004;
2238
2239 /**
2240 * Flag for {@link #setSystemUiVisibility(int)}: When using other layout
2241 * flags, we would like a stable view of the content insets given to
2242 * {@link #fitSystemWindows(Rect)}. This means that the insets seen there
2243 * will always represent the worst case that the application can expect
2244 * as a continue state. In practice this means with any of system bar,
2245 * nav bar, and status bar shown, but not the space that would be needed
2246 * for an input method.
2247 *
2248 * <p>If you are using ActionBar in
2249 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2250 * Window.FEATURE_ACTION_BAR_OVERLAY}, this flag will also impact the
2251 * insets it adds to those given to the application.
2252 */
2253 public static final int SYSTEM_UI_FLAG_LAYOUT_STABLE = 0x00000100;
2254
2255 /**
2256 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2257 * to be layed out as if it has requested
2258 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, even if it currently hasn't. This
2259 * allows it to avoid artifacts when switching in and out of that mode, at
2260 * the expense that some of its user interface may be covered by screen
2261 * decorations when they are shown. You can perform layout of your inner
2262 * UI elements to account for the navagation system UI through the
2263 * {@link #fitSystemWindows(Rect)} method.
2264 */
2265 public static final int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 0x00000200;
2266
2267 /**
2268 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2269 * to be layed out as if it has requested
2270 * {@link #SYSTEM_UI_FLAG_FULLSCREEN}, even if it currently hasn't. This
2271 * allows it to avoid artifacts when switching in and out of that mode, at
2272 * the expense that some of its user interface may be covered by screen
2273 * decorations when they are shown. You can perform layout of your inner
2274 * UI elements to account for non-fullscreen system UI through the
2275 * {@link #fitSystemWindows(Rect)} method.
2276 */
2277 public static final int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 0x00000400;
2278
2279 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04002280 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
2281 */
2282 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
2283
2284 /**
2285 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
2286 */
2287 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08002288
2289 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002290 * @hide
2291 *
2292 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2293 * out of the public fields to keep the undefined bits out of the developer's way.
2294 *
2295 * Flag to make the status bar not expandable. Unless you also
2296 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
2297 */
2298 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
2299
2300 /**
2301 * @hide
2302 *
2303 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2304 * out of the public fields to keep the undefined bits out of the developer's way.
2305 *
2306 * Flag to hide notification icons and scrolling ticker text.
2307 */
2308 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
2309
2310 /**
2311 * @hide
2312 *
2313 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2314 * out of the public fields to keep the undefined bits out of the developer's way.
2315 *
2316 * Flag to disable incoming notification alerts. This will not block
2317 * icons, but it will block sound, vibrating and other visual or aural notifications.
2318 */
2319 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
2320
2321 /**
2322 * @hide
2323 *
2324 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2325 * out of the public fields to keep the undefined bits out of the developer's way.
2326 *
2327 * Flag to hide only the scrolling ticker. Note that
2328 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
2329 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
2330 */
2331 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
2332
2333 /**
2334 * @hide
2335 *
2336 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2337 * out of the public fields to keep the undefined bits out of the developer's way.
2338 *
2339 * Flag to hide the center system info area.
2340 */
2341 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
2342
2343 /**
2344 * @hide
2345 *
2346 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2347 * out of the public fields to keep the undefined bits out of the developer's way.
2348 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002349 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002350 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2351 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04002352 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002353
2354 /**
2355 * @hide
2356 *
2357 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2358 * out of the public fields to keep the undefined bits out of the developer's way.
2359 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002360 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08002361 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2362 */
2363 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
2364
2365 /**
2366 * @hide
2367 *
2368 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2369 * out of the public fields to keep the undefined bits out of the developer's way.
2370 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002371 * Flag to hide only the clock. You might use this if your activity has
2372 * its own clock making the status bar's clock redundant.
2373 */
Joe Onorato6478adc2011-01-27 21:15:01 -08002374 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
2375
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002376 /**
2377 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04002378 *
2379 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2380 * out of the public fields to keep the undefined bits out of the developer's way.
2381 *
2382 * Flag to hide only the recent apps button. Don't use this
2383 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2384 */
2385 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
2386
2387 /**
2388 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002389 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002390 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002391
2392 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002393 * These are the system UI flags that can be cleared by events outside
2394 * of an application. Currently this is just the ability to tap on the
2395 * screen while hiding the navigation bar to have it return.
2396 * @hide
2397 */
2398 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002399 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION
2400 | SYSTEM_UI_FLAG_FULLSCREEN;
2401
2402 /**
2403 * Flags that can impact the layout in relation to system UI.
2404 */
2405 public static final int SYSTEM_UI_LAYOUT_FLAGS =
2406 SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
2407 | SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002408
2409 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07002410 * Find views that render the specified text.
2411 *
2412 * @see #findViewsWithText(ArrayList, CharSequence, int)
2413 */
2414 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
2415
2416 /**
2417 * Find find views that contain the specified content description.
2418 *
2419 * @see #findViewsWithText(ArrayList, CharSequence, int)
2420 */
2421 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
2422
2423 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07002424 * Find views that contain {@link AccessibilityNodeProvider}. Such
2425 * a View is a root of virtual view hierarchy and may contain the searched
2426 * text. If this flag is set Views with providers are automatically
2427 * added and it is a responsibility of the client to call the APIs of
2428 * the provider to determine whether the virtual tree rooted at this View
2429 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
2430 * represeting the virtual views with this text.
2431 *
2432 * @see #findViewsWithText(ArrayList, CharSequence, int)
2433 *
2434 * @hide
2435 */
2436 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
2437
2438 /**
Romain Guybb9908b2012-03-08 11:14:07 -08002439 * Indicates that the screen has changed state and is now off.
2440 *
2441 * @see #onScreenStateChanged(int)
2442 */
2443 public static final int SCREEN_STATE_OFF = 0x0;
2444
2445 /**
2446 * Indicates that the screen has changed state and is now on.
2447 *
Romain Guy1e3d3132012-03-08 15:55:56 -08002448 * @see #onScreenStateChanged(int)
Romain Guybb9908b2012-03-08 11:14:07 -08002449 */
2450 public static final int SCREEN_STATE_ON = 0x1;
2451
2452 /**
Adam Powell637d3372010-08-25 14:37:03 -07002453 * Controls the over-scroll mode for this view.
2454 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2455 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2456 * and {@link #OVER_SCROLL_NEVER}.
2457 */
2458 private int mOverScrollMode;
2459
2460 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002461 * The parent this view is attached to.
2462 * {@hide}
2463 *
2464 * @see #getParent()
2465 */
2466 protected ViewParent mParent;
2467
2468 /**
2469 * {@hide}
2470 */
2471 AttachInfo mAttachInfo;
2472
2473 /**
2474 * {@hide}
2475 */
Romain Guy809a7f62009-05-14 15:44:42 -07002476 @ViewDebug.ExportedProperty(flagMapping = {
2477 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
2478 name = "FORCE_LAYOUT"),
2479 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
2480 name = "LAYOUT_REQUIRED"),
2481 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002482 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07002483 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
2484 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
2485 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2486 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
2487 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002488 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002489 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002490
2491 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002492 * This view's request for the visibility of the status bar.
2493 * @hide
2494 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002495 @ViewDebug.ExportedProperty(flagMapping = {
2496 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2497 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2498 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2499 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2500 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2501 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2502 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2503 equals = SYSTEM_UI_FLAG_VISIBLE,
2504 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2505 })
Joe Onorato664644d2011-01-23 17:53:23 -08002506 int mSystemUiVisibility;
2507
2508 /**
Chet Haase563d4f22012-04-18 16:20:08 -07002509 * Reference count for transient state.
2510 * @see #setHasTransientState(boolean)
2511 */
2512 int mTransientStateCount = 0;
2513
2514 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002515 * Count of how many windows this view has been attached to.
2516 */
2517 int mWindowAttachCount;
2518
2519 /**
2520 * The layout parameters associated with this view and used by the parent
2521 * {@link android.view.ViewGroup} to determine how this view should be
2522 * laid out.
2523 * {@hide}
2524 */
2525 protected ViewGroup.LayoutParams mLayoutParams;
2526
2527 /**
2528 * The view flags hold various views states.
2529 * {@hide}
2530 */
2531 @ViewDebug.ExportedProperty
2532 int mViewFlags;
2533
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002534 static class TransformationInfo {
2535 /**
2536 * The transform matrix for the View. This transform is calculated internally
2537 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2538 * is used by default. Do *not* use this variable directly; instead call
2539 * getMatrix(), which will automatically recalculate the matrix if necessary
2540 * to get the correct matrix based on the latest rotation and scale properties.
2541 */
2542 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002543
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002544 /**
2545 * The transform matrix for the View. This transform is calculated internally
2546 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2547 * is used by default. Do *not* use this variable directly; instead call
2548 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2549 * to get the correct matrix based on the latest rotation and scale properties.
2550 */
2551 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002552
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002553 /**
2554 * An internal variable that tracks whether we need to recalculate the
2555 * transform matrix, based on whether the rotation or scaleX/Y properties
2556 * have changed since the matrix was last calculated.
2557 */
2558 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002559
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002560 /**
2561 * An internal variable that tracks whether we need to recalculate the
2562 * transform matrix, based on whether the rotation or scaleX/Y properties
2563 * have changed since the matrix was last calculated.
2564 */
2565 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002566
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002567 /**
2568 * A variable that tracks whether we need to recalculate the
2569 * transform matrix, based on whether the rotation or scaleX/Y properties
2570 * have changed since the matrix was last calculated. This variable
2571 * is only valid after a call to updateMatrix() or to a function that
2572 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2573 */
2574 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002575
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002576 /**
2577 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2578 */
2579 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002580
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002581 /**
2582 * This matrix is used when computing the matrix for 3D rotations.
2583 */
2584 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002585
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002586 /**
2587 * These prev values are used to recalculate a centered pivot point when necessary. The
2588 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2589 * set), so thes values are only used then as well.
2590 */
2591 private int mPrevWidth = -1;
2592 private int mPrevHeight = -1;
Philip Milne6c8ea062012-04-03 17:38:43 -07002593
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002594 /**
2595 * The degrees rotation around the vertical axis through the pivot point.
2596 */
2597 @ViewDebug.ExportedProperty
2598 float mRotationY = 0f;
2599
2600 /**
2601 * The degrees rotation around the horizontal axis through the pivot point.
2602 */
2603 @ViewDebug.ExportedProperty
2604 float mRotationX = 0f;
2605
2606 /**
2607 * The degrees rotation around the pivot point.
2608 */
2609 @ViewDebug.ExportedProperty
2610 float mRotation = 0f;
2611
2612 /**
2613 * The amount of translation of the object away from its left property (post-layout).
2614 */
2615 @ViewDebug.ExportedProperty
2616 float mTranslationX = 0f;
2617
2618 /**
2619 * The amount of translation of the object away from its top property (post-layout).
2620 */
2621 @ViewDebug.ExportedProperty
2622 float mTranslationY = 0f;
2623
2624 /**
2625 * The amount of scale in the x direction around the pivot point. A
2626 * value of 1 means no scaling is applied.
2627 */
2628 @ViewDebug.ExportedProperty
2629 float mScaleX = 1f;
2630
2631 /**
2632 * The amount of scale in the y direction around the pivot point. A
2633 * value of 1 means no scaling is applied.
2634 */
2635 @ViewDebug.ExportedProperty
2636 float mScaleY = 1f;
2637
2638 /**
Chet Haasea33de552012-02-03 16:28:24 -08002639 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002640 */
2641 @ViewDebug.ExportedProperty
2642 float mPivotX = 0f;
2643
2644 /**
Chet Haasea33de552012-02-03 16:28:24 -08002645 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002646 */
2647 @ViewDebug.ExportedProperty
2648 float mPivotY = 0f;
2649
2650 /**
2651 * The opacity of the View. This is a value from 0 to 1, where 0 means
2652 * completely transparent and 1 means completely opaque.
2653 */
2654 @ViewDebug.ExportedProperty
2655 float mAlpha = 1f;
2656 }
2657
2658 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002659
Joe Malin32736f02011-01-19 16:14:20 -08002660 private boolean mLastIsOpaque;
2661
Chet Haasefd2b0022010-08-06 13:08:56 -07002662 /**
2663 * Convenience value to check for float values that are close enough to zero to be considered
2664 * zero.
2665 */
Romain Guy2542d192010-08-18 11:47:12 -07002666 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002667
2668 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002669 * The distance in pixels from the left edge of this view's parent
2670 * to the left edge of this view.
2671 * {@hide}
2672 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002673 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002674 protected int mLeft;
2675 /**
2676 * The distance in pixels from the left edge of this view's parent
2677 * to the right edge of this view.
2678 * {@hide}
2679 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002680 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002681 protected int mRight;
2682 /**
2683 * The distance in pixels from the top edge of this view's parent
2684 * to the top edge of this view.
2685 * {@hide}
2686 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002687 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002688 protected int mTop;
2689 /**
2690 * The distance in pixels from the top edge of this view's parent
2691 * to the bottom edge of this view.
2692 * {@hide}
2693 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002694 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002695 protected int mBottom;
2696
2697 /**
2698 * The offset, in pixels, by which the content of this view is scrolled
2699 * horizontally.
2700 * {@hide}
2701 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002702 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002703 protected int mScrollX;
2704 /**
2705 * The offset, in pixels, by which the content of this view is scrolled
2706 * vertically.
2707 * {@hide}
2708 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002709 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002710 protected int mScrollY;
2711
2712 /**
2713 * The left padding in pixels, that is the distance in pixels between the
2714 * left edge of this view and the left edge of its content.
2715 * {@hide}
2716 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002717 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002718 protected int mPaddingLeft;
2719 /**
2720 * The right padding in pixels, that is the distance in pixels between the
2721 * right edge of this view and the right edge of its content.
2722 * {@hide}
2723 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002724 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002725 protected int mPaddingRight;
2726 /**
2727 * The top padding in pixels, that is the distance in pixels between the
2728 * top edge of this view and the top edge of its content.
2729 * {@hide}
2730 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002731 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002732 protected int mPaddingTop;
2733 /**
2734 * The bottom padding in pixels, that is the distance in pixels between the
2735 * bottom edge of this view and the bottom edge of its content.
2736 * {@hide}
2737 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002738 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002739 protected int mPaddingBottom;
2740
2741 /**
Philip Milne1557fd72012-04-04 23:41:34 -07002742 * The layout insets in pixels, that is the distance in pixels between the
2743 * visible edges of this view its bounds.
2744 */
2745 private Insets mLayoutInsets;
2746
2747 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002748 * Briefly describes the view and is primarily used for accessibility support.
2749 */
2750 private CharSequence mContentDescription;
2751
2752 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002753 * Cache the paddingRight 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 mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002759
2760 /**
2761 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002762 *
2763 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002764 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002765 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002766 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002767
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002768 /**
Adam Powell20232d02010-12-08 21:08:53 -08002769 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002770 *
2771 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002772 */
2773 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002774 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002775
2776 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002777 * Cache if the user padding is relative.
2778 *
2779 */
2780 @ViewDebug.ExportedProperty(category = "padding")
2781 boolean mUserPaddingRelative;
2782
2783 /**
2784 * Cache the paddingStart set by the user to append to the scrollbar's size.
2785 *
2786 */
2787 @ViewDebug.ExportedProperty(category = "padding")
2788 int mUserPaddingStart;
2789
2790 /**
2791 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2792 *
2793 */
2794 @ViewDebug.ExportedProperty(category = "padding")
2795 int mUserPaddingEnd;
2796
2797 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002798 * @hide
2799 */
2800 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2801 /**
2802 * @hide
2803 */
2804 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002805
Philip Milne6c8ea062012-04-03 17:38:43 -07002806 private Drawable mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002807
2808 private int mBackgroundResource;
2809 private boolean mBackgroundSizeChanged;
2810
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002811 static class ListenerInfo {
2812 /**
2813 * Listener used to dispatch focus change events.
2814 * This field should be made private, so it is hidden from the SDK.
2815 * {@hide}
2816 */
2817 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002818
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002819 /**
2820 * Listeners for layout change events.
2821 */
2822 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002823
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002824 /**
2825 * Listeners for attach events.
2826 */
2827 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002828
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002829 /**
2830 * Listener used to dispatch click events.
2831 * This field should be made private, so it is hidden from the SDK.
2832 * {@hide}
2833 */
2834 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002835
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002836 /**
2837 * Listener used to dispatch long click events.
2838 * This field should be made private, so it is hidden from the SDK.
2839 * {@hide}
2840 */
2841 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002842
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002843 /**
2844 * Listener used to build the context menu.
2845 * This field should be made private, so it is hidden from the SDK.
2846 * {@hide}
2847 */
2848 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002849
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002850 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002851
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002852 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002853
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002854 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002855
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002856 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002857
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002858 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002859
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002860 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2861 }
2862
2863 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002865 /**
2866 * The application environment this view lives in.
2867 * This field should be made private, so it is hidden from the SDK.
2868 * {@hide}
2869 */
2870 protected Context mContext;
2871
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002872 private final Resources mResources;
2873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002874 private ScrollabilityCache mScrollCache;
2875
2876 private int[] mDrawableState = null;
2877
Romain Guy0211a0a2011-02-14 16:34:59 -08002878 /**
2879 * Set to true when drawing cache is enabled and cannot be created.
Philip Milne6c8ea062012-04-03 17:38:43 -07002880 *
Romain Guy0211a0a2011-02-14 16:34:59 -08002881 * @hide
2882 */
2883 public boolean mCachingFailed;
2884
Romain Guy02890fd2010-08-06 17:58:44 -07002885 private Bitmap mDrawingCache;
2886 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002887 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002888 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002889
2890 /**
2891 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2892 * the user may specify which view to go to next.
2893 */
2894 private int mNextFocusLeftId = View.NO_ID;
2895
2896 /**
2897 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2898 * the user may specify which view to go to next.
2899 */
2900 private int mNextFocusRightId = View.NO_ID;
2901
2902 /**
2903 * When this view has focus and the next focus is {@link #FOCUS_UP},
2904 * the user may specify which view to go to next.
2905 */
2906 private int mNextFocusUpId = View.NO_ID;
2907
2908 /**
2909 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2910 * the user may specify which view to go to next.
2911 */
2912 private int mNextFocusDownId = View.NO_ID;
2913
Jeff Brown4e6319b2010-12-13 10:36:51 -08002914 /**
2915 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2916 * the user may specify which view to go to next.
2917 */
2918 int mNextFocusForwardId = View.NO_ID;
2919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002920 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002921 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002922 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002923 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002925 private UnsetPressedState mUnsetPressedState;
2926
2927 /**
2928 * Whether the long press's action has been invoked. The tap's action is invoked on the
2929 * up event while a long press is invoked as soon as the long press duration is reached, so
2930 * a long press could be performed before the tap is checked, in which case the tap's action
2931 * should not be invoked.
2932 */
2933 private boolean mHasPerformedLongPress;
2934
2935 /**
2936 * The minimum height of the view. We'll try our best to have the height
2937 * of this view to at least this amount.
2938 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002939 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002940 private int mMinHeight;
2941
2942 /**
2943 * The minimum width of the view. We'll try our best to have the width
2944 * of this view to at least this amount.
2945 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002946 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002947 private int mMinWidth;
2948
2949 /**
2950 * The delegate to handle touch events that are physically in this view
2951 * but should be handled by another view.
2952 */
2953 private TouchDelegate mTouchDelegate = null;
2954
2955 /**
2956 * Solid color to use as a background when creating the drawing cache. Enables
2957 * the cache to use 16 bit bitmaps instead of 32 bit.
2958 */
2959 private int mDrawingCacheBackgroundColor = 0;
2960
2961 /**
2962 * Special tree observer used when mAttachInfo is null.
2963 */
2964 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002965
Adam Powelle14579b2009-12-16 18:39:52 -08002966 /**
2967 * Cache the touch slop from the context that created the view.
2968 */
2969 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002971 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002972 * Object that handles automatic animation of view properties.
2973 */
2974 private ViewPropertyAnimator mAnimator = null;
2975
2976 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002977 * Flag indicating that a drag can cross window boundaries. When
2978 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2979 * with this flag set, all visible applications will be able to participate
2980 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002981 *
2982 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002983 */
2984 public static final int DRAG_FLAG_GLOBAL = 1;
2985
2986 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002987 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2988 */
2989 private float mVerticalScrollFactor;
2990
2991 /**
Adam Powell20232d02010-12-08 21:08:53 -08002992 * Position of the vertical scroll bar.
2993 */
2994 private int mVerticalScrollbarPosition;
2995
2996 /**
2997 * Position the scroll bar at the default position as determined by the system.
2998 */
2999 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
3000
3001 /**
3002 * Position the scroll bar along the left edge.
3003 */
3004 public static final int SCROLLBAR_POSITION_LEFT = 1;
3005
3006 /**
3007 * Position the scroll bar along the right edge.
3008 */
3009 public static final int SCROLLBAR_POSITION_RIGHT = 2;
3010
3011 /**
Romain Guy171c5922011-01-06 10:04:23 -08003012 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08003013 *
3014 * @see #getLayerType()
3015 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003016 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08003017 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003018 */
3019 public static final int LAYER_TYPE_NONE = 0;
3020
3021 /**
3022 * <p>Indicates that the view has a software layer. A software layer is backed
3023 * by a bitmap and causes the view to be rendered using Android's software
3024 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003025 *
Romain Guy171c5922011-01-06 10:04:23 -08003026 * <p>Software layers have various usages:</p>
3027 * <p>When the application is not using hardware acceleration, a software layer
3028 * is useful to apply a specific color filter and/or blending mode and/or
3029 * translucency to a view and all its children.</p>
3030 * <p>When the application is using hardware acceleration, a software layer
3031 * is useful to render drawing primitives not supported by the hardware
3032 * accelerated pipeline. It can also be used to cache a complex view tree
3033 * into a texture and reduce the complexity of drawing operations. For instance,
3034 * when animating a complex view tree with a translation, a software layer can
3035 * be used to render the view tree only once.</p>
3036 * <p>Software layers should be avoided when the affected view tree updates
3037 * often. Every update will require to re-render the software layer, which can
3038 * potentially be slow (particularly when hardware acceleration is turned on
3039 * since the layer will have to be uploaded into a hardware texture after every
3040 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08003041 *
3042 * @see #getLayerType()
3043 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003044 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08003045 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003046 */
3047 public static final int LAYER_TYPE_SOFTWARE = 1;
3048
3049 /**
3050 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
3051 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
3052 * OpenGL hardware) and causes the view to be rendered using Android's hardware
3053 * rendering pipeline, but only if hardware acceleration is turned on for the
3054 * view hierarchy. When hardware acceleration is turned off, hardware layers
3055 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003056 *
Romain Guy171c5922011-01-06 10:04:23 -08003057 * <p>A hardware layer is useful to apply a specific color filter and/or
3058 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08003059 * <p>A hardware layer can be used to cache a complex view tree into a
3060 * texture and reduce the complexity of drawing operations. For instance,
3061 * when animating a complex view tree with a translation, a hardware layer can
3062 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08003063 * <p>A hardware layer can also be used to increase the rendering quality when
3064 * rotation transformations are applied on a view. It can also be used to
3065 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003066 *
3067 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08003068 * @see #setLayerType(int, android.graphics.Paint)
3069 * @see #LAYER_TYPE_NONE
3070 * @see #LAYER_TYPE_SOFTWARE
3071 */
3072 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08003073
Romain Guy3aaff3a2011-01-12 14:18:47 -08003074 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
3075 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
3076 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
3077 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
3078 })
Romain Guy171c5922011-01-06 10:04:23 -08003079 int mLayerType = LAYER_TYPE_NONE;
3080 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08003081 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08003082
3083 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07003084 * Set to true when the view is sending hover accessibility events because it
3085 * is the innermost hovered view.
3086 */
3087 private boolean mSendingHoverAccessibilityEvents;
3088
3089 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003090 * Simple constructor to use when creating a view from code.
3091 *
3092 * @param context The Context the view is running in, through which it can
3093 * access the current theme, resources, etc.
3094 */
3095 public View(Context context) {
3096 mContext = context;
3097 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003098 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003099 // Set layout and text direction defaults
3100 mPrivateFlags2 = (LAYOUT_DIRECTION_DEFAULT << LAYOUT_DIRECTION_MASK_SHIFT) |
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003101 (TEXT_DIRECTION_DEFAULT << TEXT_DIRECTION_MASK_SHIFT) |
Svetoslav Ganov42138042012-03-20 11:51:39 -07003102 (TEXT_ALIGNMENT_DEFAULT << TEXT_ALIGNMENT_MASK_SHIFT) |
3103 (IMPORTANT_FOR_ACCESSIBILITY_DEFAULT << IMPORTANT_FOR_ACCESSIBILITY_SHIFT);
Adam Powelle14579b2009-12-16 18:39:52 -08003104 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07003105 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003106 mUserPaddingStart = -1;
3107 mUserPaddingEnd = -1;
3108 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003109 }
3110
3111 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07003112 * Delegate for injecting accessiblity functionality.
3113 */
3114 AccessibilityDelegate mAccessibilityDelegate;
3115
3116 /**
3117 * Consistency verifier for debugging purposes.
3118 * @hide
3119 */
3120 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
3121 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
3122 new InputEventConsistencyVerifier(this, 0) : null;
3123
3124 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003125 * Constructor that is called when inflating a view from XML. This is called
3126 * when a view is being constructed from an XML file, supplying attributes
3127 * that were specified in the XML file. This version uses a default style of
3128 * 0, so the only attribute values applied are those in the Context's Theme
3129 * and the given AttributeSet.
3130 *
3131 * <p>
3132 * The method onFinishInflate() will be called after all children have been
3133 * added.
3134 *
3135 * @param context The Context the view is running in, through which it can
3136 * access the current theme, resources, etc.
3137 * @param attrs The attributes of the XML tag that is inflating the view.
3138 * @see #View(Context, AttributeSet, int)
3139 */
3140 public View(Context context, AttributeSet attrs) {
3141 this(context, attrs, 0);
3142 }
3143
3144 /**
3145 * Perform inflation from XML and apply a class-specific base style. This
3146 * constructor of View allows subclasses to use their own base style when
3147 * they are inflating. For example, a Button class's constructor would call
3148 * this version of the super class constructor and supply
3149 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
3150 * the theme's button style to modify all of the base view attributes (in
3151 * particular its background) as well as the Button class's attributes.
3152 *
3153 * @param context The Context the view is running in, through which it can
3154 * access the current theme, resources, etc.
3155 * @param attrs The attributes of the XML tag that is inflating the view.
3156 * @param defStyle The default style to apply to this view. If 0, no style
3157 * will be applied (beyond what is included in the theme). This may
3158 * either be an attribute resource, whose value will be retrieved
3159 * from the current theme, or an explicit style resource.
3160 * @see #View(Context, AttributeSet)
3161 */
3162 public View(Context context, AttributeSet attrs, int defStyle) {
3163 this(context);
3164
3165 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
3166 defStyle, 0);
3167
3168 Drawable background = null;
3169
3170 int leftPadding = -1;
3171 int topPadding = -1;
3172 int rightPadding = -1;
3173 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003174 int startPadding = -1;
3175 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003176
3177 int padding = -1;
3178
3179 int viewFlagValues = 0;
3180 int viewFlagMasks = 0;
3181
3182 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07003183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003184 int x = 0;
3185 int y = 0;
3186
Chet Haase73066682010-11-29 15:55:32 -08003187 float tx = 0;
3188 float ty = 0;
3189 float rotation = 0;
3190 float rotationX = 0;
3191 float rotationY = 0;
3192 float sx = 1f;
3193 float sy = 1f;
3194 boolean transformSet = false;
3195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003196 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
3197
Adam Powell637d3372010-08-25 14:37:03 -07003198 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003199 final int N = a.getIndexCount();
3200 for (int i = 0; i < N; i++) {
3201 int attr = a.getIndex(i);
3202 switch (attr) {
3203 case com.android.internal.R.styleable.View_background:
3204 background = a.getDrawable(attr);
3205 break;
3206 case com.android.internal.R.styleable.View_padding:
3207 padding = a.getDimensionPixelSize(attr, -1);
3208 break;
3209 case com.android.internal.R.styleable.View_paddingLeft:
3210 leftPadding = a.getDimensionPixelSize(attr, -1);
3211 break;
3212 case com.android.internal.R.styleable.View_paddingTop:
3213 topPadding = a.getDimensionPixelSize(attr, -1);
3214 break;
3215 case com.android.internal.R.styleable.View_paddingRight:
3216 rightPadding = a.getDimensionPixelSize(attr, -1);
3217 break;
3218 case com.android.internal.R.styleable.View_paddingBottom:
3219 bottomPadding = a.getDimensionPixelSize(attr, -1);
3220 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003221 case com.android.internal.R.styleable.View_paddingStart:
3222 startPadding = a.getDimensionPixelSize(attr, -1);
3223 break;
3224 case com.android.internal.R.styleable.View_paddingEnd:
3225 endPadding = a.getDimensionPixelSize(attr, -1);
3226 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003227 case com.android.internal.R.styleable.View_scrollX:
3228 x = a.getDimensionPixelOffset(attr, 0);
3229 break;
3230 case com.android.internal.R.styleable.View_scrollY:
3231 y = a.getDimensionPixelOffset(attr, 0);
3232 break;
Chet Haase73066682010-11-29 15:55:32 -08003233 case com.android.internal.R.styleable.View_alpha:
3234 setAlpha(a.getFloat(attr, 1f));
3235 break;
3236 case com.android.internal.R.styleable.View_transformPivotX:
3237 setPivotX(a.getDimensionPixelOffset(attr, 0));
3238 break;
3239 case com.android.internal.R.styleable.View_transformPivotY:
3240 setPivotY(a.getDimensionPixelOffset(attr, 0));
3241 break;
3242 case com.android.internal.R.styleable.View_translationX:
3243 tx = a.getDimensionPixelOffset(attr, 0);
3244 transformSet = true;
3245 break;
3246 case com.android.internal.R.styleable.View_translationY:
3247 ty = a.getDimensionPixelOffset(attr, 0);
3248 transformSet = true;
3249 break;
3250 case com.android.internal.R.styleable.View_rotation:
3251 rotation = a.getFloat(attr, 0);
3252 transformSet = true;
3253 break;
3254 case com.android.internal.R.styleable.View_rotationX:
3255 rotationX = a.getFloat(attr, 0);
3256 transformSet = true;
3257 break;
3258 case com.android.internal.R.styleable.View_rotationY:
3259 rotationY = a.getFloat(attr, 0);
3260 transformSet = true;
3261 break;
3262 case com.android.internal.R.styleable.View_scaleX:
3263 sx = a.getFloat(attr, 1f);
3264 transformSet = true;
3265 break;
3266 case com.android.internal.R.styleable.View_scaleY:
3267 sy = a.getFloat(attr, 1f);
3268 transformSet = true;
3269 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003270 case com.android.internal.R.styleable.View_id:
3271 mID = a.getResourceId(attr, NO_ID);
3272 break;
3273 case com.android.internal.R.styleable.View_tag:
3274 mTag = a.getText(attr);
3275 break;
3276 case com.android.internal.R.styleable.View_fitsSystemWindows:
3277 if (a.getBoolean(attr, false)) {
3278 viewFlagValues |= FITS_SYSTEM_WINDOWS;
3279 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
3280 }
3281 break;
3282 case com.android.internal.R.styleable.View_focusable:
3283 if (a.getBoolean(attr, false)) {
3284 viewFlagValues |= FOCUSABLE;
3285 viewFlagMasks |= FOCUSABLE_MASK;
3286 }
3287 break;
3288 case com.android.internal.R.styleable.View_focusableInTouchMode:
3289 if (a.getBoolean(attr, false)) {
3290 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
3291 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
3292 }
3293 break;
3294 case com.android.internal.R.styleable.View_clickable:
3295 if (a.getBoolean(attr, false)) {
3296 viewFlagValues |= CLICKABLE;
3297 viewFlagMasks |= CLICKABLE;
3298 }
3299 break;
3300 case com.android.internal.R.styleable.View_longClickable:
3301 if (a.getBoolean(attr, false)) {
3302 viewFlagValues |= LONG_CLICKABLE;
3303 viewFlagMasks |= LONG_CLICKABLE;
3304 }
3305 break;
3306 case com.android.internal.R.styleable.View_saveEnabled:
3307 if (!a.getBoolean(attr, true)) {
3308 viewFlagValues |= SAVE_DISABLED;
3309 viewFlagMasks |= SAVE_DISABLED_MASK;
3310 }
3311 break;
3312 case com.android.internal.R.styleable.View_duplicateParentState:
3313 if (a.getBoolean(attr, false)) {
3314 viewFlagValues |= DUPLICATE_PARENT_STATE;
3315 viewFlagMasks |= DUPLICATE_PARENT_STATE;
3316 }
3317 break;
3318 case com.android.internal.R.styleable.View_visibility:
3319 final int visibility = a.getInt(attr, 0);
3320 if (visibility != 0) {
3321 viewFlagValues |= VISIBILITY_FLAGS[visibility];
3322 viewFlagMasks |= VISIBILITY_MASK;
3323 }
3324 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003325 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003326 // Clear any layout direction flags (included resolved bits) already set
3327 mPrivateFlags2 &= ~(LAYOUT_DIRECTION_MASK | LAYOUT_DIRECTION_RESOLVED_MASK);
3328 // Set the layout direction flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003329 final int layoutDirection = a.getInt(attr, -1);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003330 final int value = (layoutDirection != -1) ?
3331 LAYOUT_DIRECTION_FLAGS[layoutDirection] : LAYOUT_DIRECTION_DEFAULT;
3332 mPrivateFlags2 |= (value << LAYOUT_DIRECTION_MASK_SHIFT);
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07003333 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003334 case com.android.internal.R.styleable.View_drawingCacheQuality:
3335 final int cacheQuality = a.getInt(attr, 0);
3336 if (cacheQuality != 0) {
3337 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
3338 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
3339 }
3340 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07003341 case com.android.internal.R.styleable.View_contentDescription:
3342 mContentDescription = a.getString(attr);
3343 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003344 case com.android.internal.R.styleable.View_soundEffectsEnabled:
3345 if (!a.getBoolean(attr, true)) {
3346 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
3347 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
3348 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003349 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003350 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
3351 if (!a.getBoolean(attr, true)) {
3352 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
3353 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
3354 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003355 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003356 case R.styleable.View_scrollbars:
3357 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
3358 if (scrollbars != SCROLLBARS_NONE) {
3359 viewFlagValues |= scrollbars;
3360 viewFlagMasks |= SCROLLBARS_MASK;
3361 initializeScrollbars(a);
3362 }
3363 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07003364 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003365 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07003366 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
3367 // Ignore the attribute starting with ICS
3368 break;
3369 }
3370 // With builds < ICS, fall through and apply fading edges
3371 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003372 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
3373 if (fadingEdge != FADING_EDGE_NONE) {
3374 viewFlagValues |= fadingEdge;
3375 viewFlagMasks |= FADING_EDGE_MASK;
3376 initializeFadingEdge(a);
3377 }
3378 break;
3379 case R.styleable.View_scrollbarStyle:
3380 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3381 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3382 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3383 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3384 }
3385 break;
3386 case R.styleable.View_isScrollContainer:
3387 setScrollContainer = true;
3388 if (a.getBoolean(attr, false)) {
3389 setScrollContainer(true);
3390 }
3391 break;
3392 case com.android.internal.R.styleable.View_keepScreenOn:
3393 if (a.getBoolean(attr, false)) {
3394 viewFlagValues |= KEEP_SCREEN_ON;
3395 viewFlagMasks |= KEEP_SCREEN_ON;
3396 }
3397 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003398 case R.styleable.View_filterTouchesWhenObscured:
3399 if (a.getBoolean(attr, false)) {
3400 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3401 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3402 }
3403 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003404 case R.styleable.View_nextFocusLeft:
3405 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3406 break;
3407 case R.styleable.View_nextFocusRight:
3408 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3409 break;
3410 case R.styleable.View_nextFocusUp:
3411 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3412 break;
3413 case R.styleable.View_nextFocusDown:
3414 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3415 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003416 case R.styleable.View_nextFocusForward:
3417 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3418 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003419 case R.styleable.View_minWidth:
3420 mMinWidth = a.getDimensionPixelSize(attr, 0);
3421 break;
3422 case R.styleable.View_minHeight:
3423 mMinHeight = a.getDimensionPixelSize(attr, 0);
3424 break;
Romain Guy9a817362009-05-01 10:57:14 -07003425 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003426 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003427 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003428 + "be used within a restricted context");
3429 }
3430
Romain Guy9a817362009-05-01 10:57:14 -07003431 final String handlerName = a.getString(attr);
3432 if (handlerName != null) {
3433 setOnClickListener(new OnClickListener() {
3434 private Method mHandler;
3435
3436 public void onClick(View v) {
3437 if (mHandler == null) {
3438 try {
3439 mHandler = getContext().getClass().getMethod(handlerName,
3440 View.class);
3441 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003442 int id = getId();
3443 String idText = id == NO_ID ? "" : " with id '"
3444 + getContext().getResources().getResourceEntryName(
3445 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003446 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003447 handlerName + "(View) in the activity "
3448 + getContext().getClass() + " for onClick handler"
3449 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003450 }
3451 }
3452
3453 try {
3454 mHandler.invoke(getContext(), View.this);
3455 } catch (IllegalAccessException e) {
3456 throw new IllegalStateException("Could not execute non "
3457 + "public method of the activity", e);
3458 } catch (InvocationTargetException e) {
3459 throw new IllegalStateException("Could not execute "
3460 + "method of the activity", e);
3461 }
3462 }
3463 });
3464 }
3465 break;
Adam Powell637d3372010-08-25 14:37:03 -07003466 case R.styleable.View_overScrollMode:
3467 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3468 break;
Adam Powell20232d02010-12-08 21:08:53 -08003469 case R.styleable.View_verticalScrollbarPosition:
3470 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3471 break;
Romain Guy171c5922011-01-06 10:04:23 -08003472 case R.styleable.View_layerType:
3473 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3474 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003475 case R.styleable.View_textDirection:
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003476 // Clear any text direction flag already set
3477 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
3478 // Set the text direction flags depending on the value of the attribute
3479 final int textDirection = a.getInt(attr, -1);
3480 if (textDirection != -1) {
3481 mPrivateFlags2 |= TEXT_DIRECTION_FLAGS[textDirection];
3482 }
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003483 break;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003484 case R.styleable.View_textAlignment:
3485 // Clear any text alignment flag already set
3486 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
3487 // Set the text alignment flag depending on the value of the attribute
3488 final int textAlignment = a.getInt(attr, TEXT_ALIGNMENT_DEFAULT);
3489 mPrivateFlags2 |= TEXT_ALIGNMENT_FLAGS[textAlignment];
3490 break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07003491 case R.styleable.View_importantForAccessibility:
3492 setImportantForAccessibility(a.getInt(attr,
3493 IMPORTANT_FOR_ACCESSIBILITY_DEFAULT));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003494 }
3495 }
3496
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003497 a.recycle();
3498
Adam Powell637d3372010-08-25 14:37:03 -07003499 setOverScrollMode(overScrollMode);
3500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003501 if (background != null) {
Philip Milne6c8ea062012-04-03 17:38:43 -07003502 setBackground(background);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003503 }
3504
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003505 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3506 // layout direction). Those cached values will be used later during padding resolution.
3507 mUserPaddingStart = startPadding;
3508 mUserPaddingEnd = endPadding;
3509
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003510 updateUserPaddingRelative();
3511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003512 if (padding >= 0) {
3513 leftPadding = padding;
3514 topPadding = padding;
3515 rightPadding = padding;
3516 bottomPadding = padding;
3517 }
3518
3519 // If the user specified the padding (either with android:padding or
3520 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3521 // use the default padding or the padding from the background drawable
3522 // (stored at this point in mPadding*)
3523 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3524 topPadding >= 0 ? topPadding : mPaddingTop,
3525 rightPadding >= 0 ? rightPadding : mPaddingRight,
3526 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3527
3528 if (viewFlagMasks != 0) {
3529 setFlags(viewFlagValues, viewFlagMasks);
3530 }
3531
3532 // Needs to be called after mViewFlags is set
3533 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3534 recomputePadding();
3535 }
3536
3537 if (x != 0 || y != 0) {
3538 scrollTo(x, y);
3539 }
3540
Chet Haase73066682010-11-29 15:55:32 -08003541 if (transformSet) {
3542 setTranslationX(tx);
3543 setTranslationY(ty);
3544 setRotation(rotation);
3545 setRotationX(rotationX);
3546 setRotationY(rotationY);
3547 setScaleX(sx);
3548 setScaleY(sy);
3549 }
3550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003551 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3552 setScrollContainer(true);
3553 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003554
3555 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003556 }
3557
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003558 private void updateUserPaddingRelative() {
3559 mUserPaddingRelative = (mUserPaddingStart >= 0 || mUserPaddingEnd >= 0);
3560 }
3561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003562 /**
3563 * Non-public constructor for use in testing
3564 */
3565 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003566 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003567 }
3568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003569 /**
3570 * <p>
3571 * Initializes the fading edges from a given set of styled attributes. This
3572 * method should be called by subclasses that need fading edges and when an
3573 * instance of these subclasses is created programmatically rather than
3574 * being inflated from XML. This method is automatically called when the XML
3575 * is inflated.
3576 * </p>
3577 *
3578 * @param a the styled attributes set to initialize the fading edges from
3579 */
3580 protected void initializeFadingEdge(TypedArray a) {
3581 initScrollCache();
3582
3583 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3584 R.styleable.View_fadingEdgeLength,
3585 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3586 }
3587
3588 /**
3589 * Returns the size of the vertical faded edges used to indicate that more
3590 * content in this view is visible.
3591 *
3592 * @return The size in pixels of the vertical faded edge or 0 if vertical
3593 * faded edges are not enabled for this view.
3594 * @attr ref android.R.styleable#View_fadingEdgeLength
3595 */
3596 public int getVerticalFadingEdgeLength() {
3597 if (isVerticalFadingEdgeEnabled()) {
3598 ScrollabilityCache cache = mScrollCache;
3599 if (cache != null) {
3600 return cache.fadingEdgeLength;
3601 }
3602 }
3603 return 0;
3604 }
3605
3606 /**
3607 * Set the size of the faded edge used to indicate that more content in this
3608 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003609 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3610 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3611 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003612 *
3613 * @param length The size in pixels of the faded edge used to indicate that more
3614 * content in this view is visible.
3615 */
3616 public void setFadingEdgeLength(int length) {
3617 initScrollCache();
3618 mScrollCache.fadingEdgeLength = length;
3619 }
3620
3621 /**
3622 * Returns the size of the horizontal faded edges used to indicate that more
3623 * content in this view is visible.
3624 *
3625 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3626 * faded edges are not enabled for this view.
3627 * @attr ref android.R.styleable#View_fadingEdgeLength
3628 */
3629 public int getHorizontalFadingEdgeLength() {
3630 if (isHorizontalFadingEdgeEnabled()) {
3631 ScrollabilityCache cache = mScrollCache;
3632 if (cache != null) {
3633 return cache.fadingEdgeLength;
3634 }
3635 }
3636 return 0;
3637 }
3638
3639 /**
3640 * Returns the width of the vertical scrollbar.
3641 *
3642 * @return The width in pixels of the vertical scrollbar or 0 if there
3643 * is no vertical scrollbar.
3644 */
3645 public int getVerticalScrollbarWidth() {
3646 ScrollabilityCache cache = mScrollCache;
3647 if (cache != null) {
3648 ScrollBarDrawable scrollBar = cache.scrollBar;
3649 if (scrollBar != null) {
3650 int size = scrollBar.getSize(true);
3651 if (size <= 0) {
3652 size = cache.scrollBarSize;
3653 }
3654 return size;
3655 }
3656 return 0;
3657 }
3658 return 0;
3659 }
3660
3661 /**
3662 * Returns the height of the horizontal scrollbar.
3663 *
3664 * @return The height in pixels of the horizontal scrollbar or 0 if
3665 * there is no horizontal scrollbar.
3666 */
3667 protected int getHorizontalScrollbarHeight() {
3668 ScrollabilityCache cache = mScrollCache;
3669 if (cache != null) {
3670 ScrollBarDrawable scrollBar = cache.scrollBar;
3671 if (scrollBar != null) {
3672 int size = scrollBar.getSize(false);
3673 if (size <= 0) {
3674 size = cache.scrollBarSize;
3675 }
3676 return size;
3677 }
3678 return 0;
3679 }
3680 return 0;
3681 }
3682
3683 /**
3684 * <p>
3685 * Initializes the scrollbars from a given set of styled attributes. This
3686 * method should be called by subclasses that need scrollbars and when an
3687 * instance of these subclasses is created programmatically rather than
3688 * being inflated from XML. This method is automatically called when the XML
3689 * is inflated.
3690 * </p>
3691 *
3692 * @param a the styled attributes set to initialize the scrollbars from
3693 */
3694 protected void initializeScrollbars(TypedArray a) {
3695 initScrollCache();
3696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003697 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003698
Mike Cleronf116bf82009-09-27 19:14:12 -07003699 if (scrollabilityCache.scrollBar == null) {
3700 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3701 }
Joe Malin32736f02011-01-19 16:14:20 -08003702
Romain Guy8bda2482010-03-02 11:42:11 -08003703 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003704
Mike Cleronf116bf82009-09-27 19:14:12 -07003705 if (!fadeScrollbars) {
3706 scrollabilityCache.state = ScrollabilityCache.ON;
3707 }
3708 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003709
3710
Mike Cleronf116bf82009-09-27 19:14:12 -07003711 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3712 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3713 .getScrollBarFadeDuration());
3714 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3715 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3716 ViewConfiguration.getScrollDefaultDelay());
3717
Joe Malin32736f02011-01-19 16:14:20 -08003718
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003719 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3720 com.android.internal.R.styleable.View_scrollbarSize,
3721 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3722
3723 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3724 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3725
3726 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3727 if (thumb != null) {
3728 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3729 }
3730
3731 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3732 false);
3733 if (alwaysDraw) {
3734 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3735 }
3736
3737 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3738 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3739
3740 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3741 if (thumb != null) {
3742 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3743 }
3744
3745 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3746 false);
3747 if (alwaysDraw) {
3748 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3749 }
3750
3751 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003752 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003753 }
3754
3755 /**
3756 * <p>
3757 * Initalizes the scrollability cache if necessary.
3758 * </p>
3759 */
3760 private void initScrollCache() {
3761 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003762 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003763 }
3764 }
3765
Philip Milne6c8ea062012-04-03 17:38:43 -07003766 private ScrollabilityCache getScrollCache() {
3767 initScrollCache();
3768 return mScrollCache;
3769 }
3770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003771 /**
Adam Powell20232d02010-12-08 21:08:53 -08003772 * Set the position of the vertical scroll bar. Should be one of
3773 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3774 * {@link #SCROLLBAR_POSITION_RIGHT}.
3775 *
3776 * @param position Where the vertical scroll bar should be positioned.
3777 */
3778 public void setVerticalScrollbarPosition(int position) {
3779 if (mVerticalScrollbarPosition != position) {
3780 mVerticalScrollbarPosition = position;
3781 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003782 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003783 }
3784 }
3785
3786 /**
3787 * @return The position where the vertical scroll bar will show, if applicable.
3788 * @see #setVerticalScrollbarPosition(int)
3789 */
3790 public int getVerticalScrollbarPosition() {
3791 return mVerticalScrollbarPosition;
3792 }
3793
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003794 ListenerInfo getListenerInfo() {
3795 if (mListenerInfo != null) {
3796 return mListenerInfo;
3797 }
3798 mListenerInfo = new ListenerInfo();
3799 return mListenerInfo;
3800 }
3801
Adam Powell20232d02010-12-08 21:08:53 -08003802 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003803 * Register a callback to be invoked when focus of this view changed.
3804 *
3805 * @param l The callback that will run.
3806 */
3807 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003808 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003809 }
3810
3811 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003812 * Add a listener that will be called when the bounds of the view change due to
3813 * layout processing.
3814 *
3815 * @param listener The listener that will be called when layout bounds change.
3816 */
3817 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003818 ListenerInfo li = getListenerInfo();
3819 if (li.mOnLayoutChangeListeners == null) {
3820 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003821 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003822 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3823 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003824 }
Chet Haase21cd1382010-09-01 17:42:29 -07003825 }
3826
3827 /**
3828 * Remove a listener for layout changes.
3829 *
3830 * @param listener The listener for layout bounds change.
3831 */
3832 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003833 ListenerInfo li = mListenerInfo;
3834 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003835 return;
3836 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003837 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003838 }
3839
3840 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003841 * Add a listener for attach state changes.
3842 *
3843 * This listener will be called whenever this view is attached or detached
3844 * from a window. Remove the listener using
3845 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3846 *
3847 * @param listener Listener to attach
3848 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3849 */
3850 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003851 ListenerInfo li = getListenerInfo();
3852 if (li.mOnAttachStateChangeListeners == null) {
3853 li.mOnAttachStateChangeListeners
3854 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003855 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003856 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003857 }
3858
3859 /**
3860 * Remove a listener for attach state changes. The listener will receive no further
3861 * notification of window attach/detach events.
3862 *
3863 * @param listener Listener to remove
3864 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3865 */
3866 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003867 ListenerInfo li = mListenerInfo;
3868 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003869 return;
3870 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003871 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003872 }
3873
3874 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003875 * Returns the focus-change callback registered for this view.
3876 *
3877 * @return The callback, or null if one is not registered.
3878 */
3879 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003880 ListenerInfo li = mListenerInfo;
3881 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003882 }
3883
3884 /**
3885 * Register a callback to be invoked when this view is clicked. If this view is not
3886 * clickable, it becomes clickable.
3887 *
3888 * @param l The callback that will run
3889 *
3890 * @see #setClickable(boolean)
3891 */
3892 public void setOnClickListener(OnClickListener l) {
3893 if (!isClickable()) {
3894 setClickable(true);
3895 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003896 getListenerInfo().mOnClickListener = l;
3897 }
3898
3899 /**
3900 * Return whether this view has an attached OnClickListener. Returns
3901 * true if there is a listener, false if there is none.
3902 */
3903 public boolean hasOnClickListeners() {
3904 ListenerInfo li = mListenerInfo;
3905 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003906 }
3907
3908 /**
3909 * Register a callback to be invoked when this view is clicked and held. If this view is not
3910 * long clickable, it becomes long clickable.
3911 *
3912 * @param l The callback that will run
3913 *
3914 * @see #setLongClickable(boolean)
3915 */
3916 public void setOnLongClickListener(OnLongClickListener l) {
3917 if (!isLongClickable()) {
3918 setLongClickable(true);
3919 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003920 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003921 }
3922
3923 /**
3924 * Register a callback to be invoked when the context menu for this view is
3925 * being built. If this view is not long clickable, it becomes long clickable.
3926 *
3927 * @param l The callback that will run
3928 *
3929 */
3930 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3931 if (!isLongClickable()) {
3932 setLongClickable(true);
3933 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003934 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003935 }
3936
3937 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003938 * Call this view's OnClickListener, if it is defined. Performs all normal
3939 * actions associated with clicking: reporting accessibility event, playing
3940 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003941 *
3942 * @return True there was an assigned OnClickListener that was called, false
3943 * otherwise is returned.
3944 */
3945 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003946 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3947
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003948 ListenerInfo li = mListenerInfo;
3949 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003950 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003951 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003952 return true;
3953 }
3954
3955 return false;
3956 }
3957
3958 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003959 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
3960 * this only calls the listener, and does not do any associated clicking
3961 * actions like reporting an accessibility event.
3962 *
3963 * @return True there was an assigned OnClickListener that was called, false
3964 * otherwise is returned.
3965 */
3966 public boolean callOnClick() {
3967 ListenerInfo li = mListenerInfo;
3968 if (li != null && li.mOnClickListener != null) {
3969 li.mOnClickListener.onClick(this);
3970 return true;
3971 }
3972 return false;
3973 }
3974
3975 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003976 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3977 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003978 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003979 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003980 */
3981 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003982 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3983
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003984 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003985 ListenerInfo li = mListenerInfo;
3986 if (li != null && li.mOnLongClickListener != null) {
3987 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003988 }
3989 if (!handled) {
3990 handled = showContextMenu();
3991 }
3992 if (handled) {
3993 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3994 }
3995 return handled;
3996 }
3997
3998 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003999 * Performs button-related actions during a touch down event.
4000 *
4001 * @param event The event.
4002 * @return True if the down was consumed.
4003 *
4004 * @hide
4005 */
4006 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
4007 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
4008 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
4009 return true;
4010 }
4011 }
4012 return false;
4013 }
4014
4015 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004016 * Bring up the context menu for this view.
4017 *
4018 * @return Whether a context menu was displayed.
4019 */
4020 public boolean showContextMenu() {
4021 return getParent().showContextMenuForChild(this);
4022 }
4023
4024 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004025 * Bring up the context menu for this view, referring to the item under the specified point.
4026 *
4027 * @param x The referenced x coordinate.
4028 * @param y The referenced y coordinate.
4029 * @param metaState The keyboard modifiers that were pressed.
4030 * @return Whether a context menu was displayed.
4031 *
4032 * @hide
4033 */
4034 public boolean showContextMenu(float x, float y, int metaState) {
4035 return showContextMenu();
4036 }
4037
4038 /**
Adam Powell6e346362010-07-23 10:18:23 -07004039 * Start an action mode.
4040 *
4041 * @param callback Callback that will control the lifecycle of the action mode
4042 * @return The new action mode if it is started, null otherwise
4043 *
4044 * @see ActionMode
4045 */
4046 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08004047 ViewParent parent = getParent();
4048 if (parent == null) return null;
4049 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07004050 }
4051
4052 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004053 * Register a callback to be invoked when a key is pressed in this view.
4054 * @param l the key listener to attach to this view
4055 */
4056 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004057 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004058 }
4059
4060 /**
4061 * Register a callback to be invoked when a touch event is sent to this view.
4062 * @param l the touch listener to attach to this view
4063 */
4064 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004065 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004066 }
4067
4068 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004069 * Register a callback to be invoked when a generic motion event is sent to this view.
4070 * @param l the generic motion listener to attach to this view
4071 */
4072 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004073 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08004074 }
4075
4076 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07004077 * Register a callback to be invoked when a hover event is sent to this view.
4078 * @param l the hover listener to attach to this view
4079 */
4080 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004081 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07004082 }
4083
4084 /**
Joe Malin32736f02011-01-19 16:14:20 -08004085 * Register a drag event listener callback object for this View. The parameter is
4086 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
4087 * View, the system calls the
4088 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
4089 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07004090 */
4091 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004092 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07004093 }
4094
4095 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07004096 * Give this view focus. This will cause
4097 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004098 *
4099 * Note: this does not check whether this {@link View} should get focus, it just
4100 * gives it focus no matter what. It should only be called internally by framework
4101 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
4102 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004103 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
4104 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004105 * focus moved when requestFocus() is called. It may not always
4106 * apply, in which case use the default View.FOCUS_DOWN.
4107 * @param previouslyFocusedRect The rectangle of the view that had focus
4108 * prior in this View's coordinate system.
4109 */
4110 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
4111 if (DBG) {
4112 System.out.println(this + " requestFocus()");
4113 }
4114
4115 if ((mPrivateFlags & FOCUSED) == 0) {
4116 mPrivateFlags |= FOCUSED;
4117
4118 if (mParent != null) {
4119 mParent.requestChildFocus(this, this);
4120 }
4121
4122 onFocusChanged(true, direction, previouslyFocusedRect);
4123 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004124
4125 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4126 notifyAccessibilityStateChanged();
4127 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004128 }
4129 }
4130
4131 /**
4132 * Request that a rectangle of this view be visible on the screen,
4133 * scrolling if necessary just enough.
4134 *
4135 * <p>A View should call this if it maintains some notion of which part
4136 * of its content is interesting. For example, a text editing view
4137 * should call this when its cursor moves.
4138 *
4139 * @param rectangle The rectangle.
4140 * @return Whether any parent scrolled.
4141 */
4142 public boolean requestRectangleOnScreen(Rect rectangle) {
4143 return requestRectangleOnScreen(rectangle, false);
4144 }
4145
4146 /**
4147 * Request that a rectangle of this view be visible on the screen,
4148 * scrolling if necessary just enough.
4149 *
4150 * <p>A View should call this if it maintains some notion of which part
4151 * of its content is interesting. For example, a text editing view
4152 * should call this when its cursor moves.
4153 *
4154 * <p>When <code>immediate</code> is set to true, scrolling will not be
4155 * animated.
4156 *
4157 * @param rectangle The rectangle.
4158 * @param immediate True to forbid animated scrolling, false otherwise
4159 * @return Whether any parent scrolled.
4160 */
4161 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
4162 View child = this;
4163 ViewParent parent = mParent;
4164 boolean scrolled = false;
4165 while (parent != null) {
4166 scrolled |= parent.requestChildRectangleOnScreen(child,
4167 rectangle, immediate);
4168
4169 // offset rect so next call has the rectangle in the
4170 // coordinate system of its direct child.
4171 rectangle.offset(child.getLeft(), child.getTop());
4172 rectangle.offset(-child.getScrollX(), -child.getScrollY());
4173
4174 if (!(parent instanceof View)) {
4175 break;
4176 }
Romain Guy8506ab42009-06-11 17:35:47 -07004177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004178 child = (View) parent;
4179 parent = child.getParent();
4180 }
4181 return scrolled;
4182 }
4183
4184 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004185 * Called when this view wants to give up focus. If focus is cleared
4186 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
4187 * <p>
4188 * <strong>Note:</strong> When a View clears focus the framework is trying
4189 * to give focus to the first focusable View from the top. Hence, if this
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004190 * View is the first from the top that can take focus, then all callbacks
4191 * related to clearing focus will be invoked after wich the framework will
4192 * give focus to this view.
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004193 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004194 */
4195 public void clearFocus() {
4196 if (DBG) {
4197 System.out.println(this + " clearFocus()");
4198 }
4199
4200 if ((mPrivateFlags & FOCUSED) != 0) {
4201 mPrivateFlags &= ~FOCUSED;
4202
4203 if (mParent != null) {
4204 mParent.clearChildFocus(this);
4205 }
4206
4207 onFocusChanged(false, 0, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004209 refreshDrawableState();
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004210
4211 ensureInputFocusOnFirstFocusable();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004212
4213 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4214 notifyAccessibilityStateChanged();
4215 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004216 }
4217 }
4218
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004219 void ensureInputFocusOnFirstFocusable() {
4220 View root = getRootView();
4221 if (root != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004222 root.requestFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004223 }
4224 }
4225
4226 /**
4227 * Called internally by the view system when a new view is getting focus.
4228 * This is what clears the old focus.
4229 */
4230 void unFocus() {
4231 if (DBG) {
4232 System.out.println(this + " unFocus()");
4233 }
4234
4235 if ((mPrivateFlags & FOCUSED) != 0) {
4236 mPrivateFlags &= ~FOCUSED;
4237
4238 onFocusChanged(false, 0, null);
4239 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004240
4241 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4242 notifyAccessibilityStateChanged();
4243 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004244 }
4245 }
4246
4247 /**
4248 * Returns true if this view has focus iteself, or is the ancestor of the
4249 * view that has focus.
4250 *
4251 * @return True if this view has or contains focus, false otherwise.
4252 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004253 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004254 public boolean hasFocus() {
4255 return (mPrivateFlags & FOCUSED) != 0;
4256 }
4257
4258 /**
4259 * Returns true if this view is focusable or if it contains a reachable View
4260 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
4261 * is a View whose parents do not block descendants focus.
4262 *
4263 * Only {@link #VISIBLE} views are considered focusable.
4264 *
4265 * @return True if the view is focusable or if the view contains a focusable
4266 * View, false otherwise.
4267 *
4268 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
4269 */
4270 public boolean hasFocusable() {
4271 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4272 }
4273
4274 /**
4275 * Called by the view system when the focus state of this view changes.
4276 * When the focus change event is caused by directional navigation, direction
4277 * and previouslyFocusedRect provide insight into where the focus is coming from.
4278 * When overriding, be sure to call up through to the super class so that
4279 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004280 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004281 * @param gainFocus True if the View has focus; false otherwise.
4282 * @param direction The direction focus has moved when requestFocus()
4283 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004284 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4285 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4286 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004287 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4288 * system, of the previously focused view. If applicable, this will be
4289 * passed in as finer grained information about where the focus is coming
4290 * from (in addition to direction). Will be <code>null</code> otherwise.
4291 */
4292 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004293 if (gainFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004294 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4295 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
4296 requestAccessibilityFocus();
4297 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004298 }
4299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004300 InputMethodManager imm = InputMethodManager.peekInstance();
4301 if (!gainFocus) {
4302 if (isPressed()) {
4303 setPressed(false);
4304 }
4305 if (imm != null && mAttachInfo != null
4306 && mAttachInfo.mHasWindowFocus) {
4307 imm.focusOut(this);
4308 }
Romain Guya2431d02009-04-30 16:30:00 -07004309 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004310 } else if (imm != null && mAttachInfo != null
4311 && mAttachInfo.mHasWindowFocus) {
4312 imm.focusIn(this);
4313 }
Romain Guy8506ab42009-06-11 17:35:47 -07004314
Romain Guy0fd89bf2011-01-26 15:41:30 -08004315 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004316 ListenerInfo li = mListenerInfo;
4317 if (li != null && li.mOnFocusChangeListener != null) {
4318 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004319 }
Joe Malin32736f02011-01-19 16:14:20 -08004320
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004321 if (mAttachInfo != null) {
4322 mAttachInfo.mKeyDispatchState.reset(this);
4323 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004324 }
4325
4326 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004327 * Sends an accessibility event of the given type. If accessiiblity is
4328 * not enabled this method has no effect. The default implementation calls
4329 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4330 * to populate information about the event source (this View), then calls
4331 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4332 * populate the text content of the event source including its descendants,
4333 * and last calls
4334 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4335 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004336 * <p>
4337 * If an {@link AccessibilityDelegate} has been specified via calling
4338 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4339 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4340 * responsible for handling this call.
4341 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004342 *
Scott Mainb303d832011-10-12 16:45:18 -07004343 * @param eventType The type of the event to send, as defined by several types from
4344 * {@link android.view.accessibility.AccessibilityEvent}, such as
4345 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4346 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004347 *
4348 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4349 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4350 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004351 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004352 */
4353 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004354 if (mAccessibilityDelegate != null) {
4355 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4356 } else {
4357 sendAccessibilityEventInternal(eventType);
4358 }
4359 }
4360
4361 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004362 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4363 * {@link AccessibilityEvent} to make an announcement which is related to some
4364 * sort of a context change for which none of the events representing UI transitions
4365 * is a good fit. For example, announcing a new page in a book. If accessibility
4366 * is not enabled this method does nothing.
4367 *
4368 * @param text The announcement text.
4369 */
4370 public void announceForAccessibility(CharSequence text) {
4371 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4372 AccessibilityEvent event = AccessibilityEvent.obtain(
4373 AccessibilityEvent.TYPE_ANNOUNCEMENT);
4374 event.getText().add(text);
4375 sendAccessibilityEventUnchecked(event);
4376 }
4377 }
4378
4379 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004380 * @see #sendAccessibilityEvent(int)
4381 *
4382 * Note: Called from the default {@link AccessibilityDelegate}.
4383 */
4384 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004385 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4386 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4387 }
4388 }
4389
4390 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004391 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4392 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004393 * perform a check whether accessibility is enabled.
4394 * <p>
4395 * If an {@link AccessibilityDelegate} has been specified via calling
4396 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4397 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4398 * is responsible for handling this call.
4399 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004400 *
4401 * @param event The event to send.
4402 *
4403 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004404 */
4405 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004406 if (mAccessibilityDelegate != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004407 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004408 } else {
4409 sendAccessibilityEventUncheckedInternal(event);
4410 }
4411 }
4412
4413 /**
4414 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4415 *
4416 * Note: Called from the default {@link AccessibilityDelegate}.
4417 */
4418 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004419 if (!isShown()) {
4420 return;
4421 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004422 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004423 // Only a subset of accessibility events populates text content.
4424 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4425 dispatchPopulateAccessibilityEvent(event);
4426 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07004427 // Intercept accessibility focus events fired by virtual nodes to keep
4428 // track of accessibility focus position in such nodes.
4429 final int eventType = event.getEventType();
4430 switch (eventType) {
4431 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED: {
4432 final long virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
4433 event.getSourceNodeId());
4434 if (virtualNodeId != AccessibilityNodeInfo.UNDEFINED) {
4435 ViewRootImpl viewRootImpl = getViewRootImpl();
4436 if (viewRootImpl != null) {
4437 viewRootImpl.setAccessibilityFocusedHost(this);
4438 }
4439 }
4440 } break;
4441 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED: {
4442 final long virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
4443 event.getSourceNodeId());
4444 if (virtualNodeId != AccessibilityNodeInfo.UNDEFINED) {
4445 ViewRootImpl viewRootImpl = getViewRootImpl();
4446 if (viewRootImpl != null) {
4447 viewRootImpl.setAccessibilityFocusedHost(null);
4448 }
4449 }
4450 } break;
4451 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004452 // In the beginning we called #isShown(), so we know that getParent() is not null.
4453 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004454 }
4455
4456 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004457 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4458 * to its children for adding their text content to the event. Note that the
4459 * event text is populated in a separate dispatch path since we add to the
4460 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004461 * A typical implementation will call
4462 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4463 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4464 * on each child. Override this method if custom population of the event text
4465 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004466 * <p>
4467 * If an {@link AccessibilityDelegate} has been specified via calling
4468 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4469 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4470 * is responsible for handling this call.
4471 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004472 * <p>
4473 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4474 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4475 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004476 *
4477 * @param event The event.
4478 *
4479 * @return True if the event population was completed.
4480 */
4481 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004482 if (mAccessibilityDelegate != null) {
4483 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4484 } else {
4485 return dispatchPopulateAccessibilityEventInternal(event);
4486 }
4487 }
4488
4489 /**
4490 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4491 *
4492 * Note: Called from the default {@link AccessibilityDelegate}.
4493 */
4494 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004495 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004496 return false;
4497 }
4498
4499 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004500 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004501 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004502 * text content. While this method is free to modify event
4503 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004504 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4505 * <p>
4506 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004507 * to the text added by the super implementation:
4508 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004509 * super.onPopulateAccessibilityEvent(event);
4510 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4511 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4512 * mCurrentDate.getTimeInMillis(), flags);
4513 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004514 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004515 * <p>
4516 * If an {@link AccessibilityDelegate} has been specified via calling
4517 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4518 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4519 * is responsible for handling this call.
4520 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004521 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4522 * information to the event, in case the default implementation has basic information to add.
4523 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004524 *
4525 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004526 *
4527 * @see #sendAccessibilityEvent(int)
4528 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004529 */
4530 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004531 if (mAccessibilityDelegate != null) {
4532 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4533 } else {
4534 onPopulateAccessibilityEventInternal(event);
4535 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004536 }
4537
4538 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004539 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4540 *
4541 * Note: Called from the default {@link AccessibilityDelegate}.
4542 */
4543 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4544
4545 }
4546
4547 /**
4548 * Initializes an {@link AccessibilityEvent} with information about
4549 * this View which is the event source. In other words, the source of
4550 * an accessibility event is the view whose state change triggered firing
4551 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004552 * <p>
4553 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004554 * to properties set by the super implementation:
4555 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4556 * super.onInitializeAccessibilityEvent(event);
4557 * event.setPassword(true);
4558 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004559 * <p>
4560 * If an {@link AccessibilityDelegate} has been specified via calling
4561 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4562 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4563 * is responsible for handling this call.
4564 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004565 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4566 * information to the event, in case the default implementation has basic information to add.
4567 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004568 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004569 *
4570 * @see #sendAccessibilityEvent(int)
4571 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4572 */
4573 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004574 if (mAccessibilityDelegate != null) {
4575 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4576 } else {
4577 onInitializeAccessibilityEventInternal(event);
4578 }
4579 }
4580
4581 /**
4582 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4583 *
4584 * Note: Called from the default {@link AccessibilityDelegate}.
4585 */
4586 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004587 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004588 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004589 event.setPackageName(getContext().getPackageName());
4590 event.setEnabled(isEnabled());
4591 event.setContentDescription(mContentDescription);
4592
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004593 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004594 ArrayList<View> focusablesTempList = mAttachInfo.mTempArrayList;
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004595 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4596 FOCUSABLES_ALL);
4597 event.setItemCount(focusablesTempList.size());
4598 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4599 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004600 }
4601 }
4602
4603 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004604 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4605 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4606 * This method is responsible for obtaining an accessibility node info from a
4607 * pool of reusable instances and calling
4608 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4609 * initialize the former.
4610 * <p>
4611 * Note: The client is responsible for recycling the obtained instance by calling
4612 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4613 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004614 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004615 * @return A populated {@link AccessibilityNodeInfo}.
4616 *
4617 * @see AccessibilityNodeInfo
4618 */
4619 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004620 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4621 if (provider != null) {
4622 return provider.createAccessibilityNodeInfo(View.NO_ID);
4623 } else {
4624 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4625 onInitializeAccessibilityNodeInfo(info);
4626 return info;
4627 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004628 }
4629
4630 /**
4631 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4632 * The base implementation sets:
4633 * <ul>
4634 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004635 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4636 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004637 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4638 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4639 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4640 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4641 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4642 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4643 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4644 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4645 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4646 * </ul>
4647 * <p>
4648 * Subclasses should override this method, call the super implementation,
4649 * and set additional attributes.
4650 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004651 * <p>
4652 * If an {@link AccessibilityDelegate} has been specified via calling
4653 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4654 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4655 * is responsible for handling this call.
4656 * </p>
4657 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004658 * @param info The instance to initialize.
4659 */
4660 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004661 if (mAccessibilityDelegate != null) {
4662 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4663 } else {
4664 onInitializeAccessibilityNodeInfoInternal(info);
4665 }
4666 }
4667
4668 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004669 * Gets the location of this view in screen coordintates.
4670 *
4671 * @param outRect The output location
4672 */
4673 private void getBoundsOnScreen(Rect outRect) {
4674 if (mAttachInfo == null) {
4675 return;
4676 }
4677
4678 RectF position = mAttachInfo.mTmpTransformRect;
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004679 position.set(0, 0, mRight - mLeft, mBottom - mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004680
4681 if (!hasIdentityMatrix()) {
4682 getMatrix().mapRect(position);
4683 }
4684
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004685 position.offset(mLeft, mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004686
4687 ViewParent parent = mParent;
4688 while (parent instanceof View) {
4689 View parentView = (View) parent;
4690
4691 position.offset(-parentView.mScrollX, -parentView.mScrollY);
4692
4693 if (!parentView.hasIdentityMatrix()) {
4694 parentView.getMatrix().mapRect(position);
4695 }
4696
4697 position.offset(parentView.mLeft, parentView.mTop);
4698
4699 parent = parentView.mParent;
4700 }
4701
4702 if (parent instanceof ViewRootImpl) {
4703 ViewRootImpl viewRootImpl = (ViewRootImpl) parent;
4704 position.offset(0, -viewRootImpl.mCurScrollY);
4705 }
4706
4707 position.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
4708
4709 outRect.set((int) (position.left + 0.5f), (int) (position.top + 0.5f),
4710 (int) (position.right + 0.5f), (int) (position.bottom + 0.5f));
4711 }
4712
4713 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004714 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4715 *
4716 * Note: Called from the default {@link AccessibilityDelegate}.
4717 */
4718 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004719 Rect bounds = mAttachInfo.mTmpInvalRect;
4720 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004721 info.setBoundsInParent(bounds);
4722
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004723 getBoundsOnScreen(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004724 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004725
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004726 if ((mPrivateFlags & IS_ROOT_NAMESPACE) == 0) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004727 ViewParent parent = getParentForAccessibility();
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004728 if (parent instanceof View) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004729 info.setParent((View) parent);
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004730 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004731 }
4732
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004733 info.setVisibleToUser(isVisibleToUser());
4734
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004735 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004736 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004737 info.setContentDescription(getContentDescription());
4738
4739 info.setEnabled(isEnabled());
4740 info.setClickable(isClickable());
4741 info.setFocusable(isFocusable());
4742 info.setFocused(isFocused());
Svetoslav Ganov42138042012-03-20 11:51:39 -07004743 info.setAccessibilityFocused(isAccessibilityFocused());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004744 info.setSelected(isSelected());
4745 info.setLongClickable(isLongClickable());
4746
4747 // TODO: These make sense only if we are in an AdapterView but all
4748 // views can be selected. Maybe from accessiiblity perspective
4749 // we should report as selectable view in an AdapterView.
4750 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4751 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4752
4753 if (isFocusable()) {
4754 if (isFocused()) {
4755 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4756 } else {
4757 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4758 }
4759 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004760
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07004761 if (!isAccessibilityFocused()) {
4762 info.addAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
4763 } else {
4764 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
4765 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004766
4767 if (isClickable()) {
4768 info.addAction(AccessibilityNodeInfo.ACTION_CLICK);
4769 }
4770
4771 if (isLongClickable()) {
4772 info.addAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
4773 }
4774
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004775 if (mContentDescription != null && mContentDescription.length() > 0) {
Svetoslav Ganov2b435aa2012-05-04 17:16:37 -07004776 info.addAction(AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY);
4777 info.addAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY);
4778 info.setMovementGranularities(AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004779 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD
4780 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH);
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004781 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004782 }
4783
4784 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004785 * Computes whether this view is visible to the user. Such a view is
4786 * attached, visible, all its predecessors are visible, it is not clipped
4787 * entirely by its predecessors, and has an alpha greater than zero.
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004788 *
4789 * @return Whether the view is visible on the screen.
4790 */
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004791 private boolean isVisibleToUser() {
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004792 // The first two checks are made also made by isShown() which
4793 // however traverses the tree up to the parent to catch that.
4794 // Therefore, we do some fail fast check to minimize the up
4795 // tree traversal.
4796 return (mAttachInfo != null
4797 && mAttachInfo.mWindowVisibility == View.VISIBLE
4798 && getAlpha() > 0
4799 && isShown()
4800 && getGlobalVisibleRect(mAttachInfo.mTmpInvalRect));
4801 }
4802
4803 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004804 * Sets a delegate for implementing accessibility support via compositon as
4805 * opposed to inheritance. The delegate's primary use is for implementing
4806 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4807 *
4808 * @param delegate The delegate instance.
4809 *
4810 * @see AccessibilityDelegate
4811 */
4812 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4813 mAccessibilityDelegate = delegate;
4814 }
4815
4816 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004817 * Gets the provider for managing a virtual view hierarchy rooted at this View
4818 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4819 * that explore the window content.
4820 * <p>
4821 * If this method returns an instance, this instance is responsible for managing
4822 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4823 * View including the one representing the View itself. Similarly the returned
4824 * instance is responsible for performing accessibility actions on any virtual
4825 * view or the root view itself.
4826 * </p>
4827 * <p>
4828 * If an {@link AccessibilityDelegate} has been specified via calling
4829 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4830 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4831 * is responsible for handling this call.
4832 * </p>
4833 *
4834 * @return The provider.
4835 *
4836 * @see AccessibilityNodeProvider
4837 */
4838 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4839 if (mAccessibilityDelegate != null) {
4840 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4841 } else {
4842 return null;
4843 }
4844 }
4845
4846 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004847 * Gets the unique identifier of this view on the screen for accessibility purposes.
4848 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4849 *
4850 * @return The view accessibility id.
4851 *
4852 * @hide
4853 */
4854 public int getAccessibilityViewId() {
4855 if (mAccessibilityViewId == NO_ID) {
4856 mAccessibilityViewId = sNextAccessibilityViewId++;
4857 }
4858 return mAccessibilityViewId;
4859 }
4860
4861 /**
4862 * Gets the unique identifier of the window in which this View reseides.
4863 *
4864 * @return The window accessibility id.
4865 *
4866 * @hide
4867 */
4868 public int getAccessibilityWindowId() {
4869 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4870 }
4871
4872 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004873 * Gets the {@link View} description. It briefly describes the view and is
4874 * primarily used for accessibility support. Set this property to enable
4875 * better accessibility support for your application. This is especially
4876 * true for views that do not have textual representation (For example,
4877 * ImageButton).
4878 *
Svetoslav Ganov42138042012-03-20 11:51:39 -07004879 * @return The content description.
svetoslavganov75986cf2009-05-14 22:28:01 -07004880 *
4881 * @attr ref android.R.styleable#View_contentDescription
4882 */
Svetoslav Ganov42138042012-03-20 11:51:39 -07004883 @ViewDebug.ExportedProperty(category = "accessibility")
svetoslavganov75986cf2009-05-14 22:28:01 -07004884 public CharSequence getContentDescription() {
4885 return mContentDescription;
4886 }
4887
4888 /**
4889 * Sets the {@link View} description. It briefly describes the view and is
4890 * primarily used for accessibility support. Set this property to enable
4891 * better accessibility support for your application. This is especially
4892 * true for views that do not have textual representation (For example,
4893 * ImageButton).
4894 *
4895 * @param contentDescription The content description.
4896 *
4897 * @attr ref android.R.styleable#View_contentDescription
4898 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07004899 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07004900 public void setContentDescription(CharSequence contentDescription) {
4901 mContentDescription = contentDescription;
4902 }
4903
4904 /**
Romain Guya2431d02009-04-30 16:30:00 -07004905 * Invoked whenever this view loses focus, either by losing window focus or by losing
4906 * focus within its window. This method can be used to clear any state tied to the
4907 * focus. For instance, if a button is held pressed with the trackball and the window
4908 * loses focus, this method can be used to cancel the press.
4909 *
4910 * Subclasses of View overriding this method should always call super.onFocusLost().
4911 *
4912 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004913 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004914 *
4915 * @hide pending API council approval
4916 */
4917 protected void onFocusLost() {
4918 resetPressedState();
4919 }
4920
4921 private void resetPressedState() {
4922 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4923 return;
4924 }
4925
4926 if (isPressed()) {
4927 setPressed(false);
4928
4929 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004930 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004931 }
4932 }
4933 }
4934
4935 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004936 * Returns true if this view has focus
4937 *
4938 * @return True if this view has focus, false otherwise.
4939 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004940 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004941 public boolean isFocused() {
4942 return (mPrivateFlags & FOCUSED) != 0;
4943 }
4944
4945 /**
4946 * Find the view in the hierarchy rooted at this view that currently has
4947 * focus.
4948 *
4949 * @return The view that currently has focus, or null if no focused view can
4950 * be found.
4951 */
4952 public View findFocus() {
4953 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4954 }
4955
4956 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07004957 * Indicates whether this view is one of the set of scrollable containers in
4958 * its window.
4959 *
4960 * @return whether this view is one of the set of scrollable containers in
4961 * its window
4962 *
4963 * @attr ref android.R.styleable#View_isScrollContainer
4964 */
4965 public boolean isScrollContainer() {
4966 return (mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0;
4967 }
4968
4969 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004970 * Change whether this view is one of the set of scrollable containers in
4971 * its window. This will be used to determine whether the window can
4972 * resize or must pan when a soft input area is open -- scrollable
4973 * containers allow the window to use resize mode since the container
4974 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07004975 *
4976 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004977 */
4978 public void setScrollContainer(boolean isScrollContainer) {
4979 if (isScrollContainer) {
4980 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4981 mAttachInfo.mScrollContainers.add(this);
4982 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4983 }
4984 mPrivateFlags |= SCROLL_CONTAINER;
4985 } else {
4986 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4987 mAttachInfo.mScrollContainers.remove(this);
4988 }
4989 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4990 }
4991 }
4992
4993 /**
4994 * Returns the quality of the drawing cache.
4995 *
4996 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4997 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4998 *
4999 * @see #setDrawingCacheQuality(int)
5000 * @see #setDrawingCacheEnabled(boolean)
5001 * @see #isDrawingCacheEnabled()
5002 *
5003 * @attr ref android.R.styleable#View_drawingCacheQuality
5004 */
5005 public int getDrawingCacheQuality() {
5006 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
5007 }
5008
5009 /**
5010 * Set the drawing cache quality of this view. This value is used only when the
5011 * drawing cache is enabled
5012 *
5013 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5014 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5015 *
5016 * @see #getDrawingCacheQuality()
5017 * @see #setDrawingCacheEnabled(boolean)
5018 * @see #isDrawingCacheEnabled()
5019 *
5020 * @attr ref android.R.styleable#View_drawingCacheQuality
5021 */
5022 public void setDrawingCacheQuality(int quality) {
5023 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
5024 }
5025
5026 /**
5027 * Returns whether the screen should remain on, corresponding to the current
5028 * value of {@link #KEEP_SCREEN_ON}.
5029 *
5030 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
5031 *
5032 * @see #setKeepScreenOn(boolean)
5033 *
5034 * @attr ref android.R.styleable#View_keepScreenOn
5035 */
5036 public boolean getKeepScreenOn() {
5037 return (mViewFlags & KEEP_SCREEN_ON) != 0;
5038 }
5039
5040 /**
5041 * Controls whether the screen should remain on, modifying the
5042 * value of {@link #KEEP_SCREEN_ON}.
5043 *
5044 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
5045 *
5046 * @see #getKeepScreenOn()
5047 *
5048 * @attr ref android.R.styleable#View_keepScreenOn
5049 */
5050 public void setKeepScreenOn(boolean keepScreenOn) {
5051 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
5052 }
5053
5054 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005055 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5056 * @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 -08005057 *
5058 * @attr ref android.R.styleable#View_nextFocusLeft
5059 */
5060 public int getNextFocusLeftId() {
5061 return mNextFocusLeftId;
5062 }
5063
5064 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005065 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5066 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
5067 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005068 *
5069 * @attr ref android.R.styleable#View_nextFocusLeft
5070 */
5071 public void setNextFocusLeftId(int nextFocusLeftId) {
5072 mNextFocusLeftId = nextFocusLeftId;
5073 }
5074
5075 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005076 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5077 * @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 -08005078 *
5079 * @attr ref android.R.styleable#View_nextFocusRight
5080 */
5081 public int getNextFocusRightId() {
5082 return mNextFocusRightId;
5083 }
5084
5085 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005086 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5087 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
5088 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005089 *
5090 * @attr ref android.R.styleable#View_nextFocusRight
5091 */
5092 public void setNextFocusRightId(int nextFocusRightId) {
5093 mNextFocusRightId = nextFocusRightId;
5094 }
5095
5096 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005097 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5098 * @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 -08005099 *
5100 * @attr ref android.R.styleable#View_nextFocusUp
5101 */
5102 public int getNextFocusUpId() {
5103 return mNextFocusUpId;
5104 }
5105
5106 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005107 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5108 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
5109 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005110 *
5111 * @attr ref android.R.styleable#View_nextFocusUp
5112 */
5113 public void setNextFocusUpId(int nextFocusUpId) {
5114 mNextFocusUpId = nextFocusUpId;
5115 }
5116
5117 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005118 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5119 * @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 -08005120 *
5121 * @attr ref android.R.styleable#View_nextFocusDown
5122 */
5123 public int getNextFocusDownId() {
5124 return mNextFocusDownId;
5125 }
5126
5127 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005128 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5129 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
5130 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005131 *
5132 * @attr ref android.R.styleable#View_nextFocusDown
5133 */
5134 public void setNextFocusDownId(int nextFocusDownId) {
5135 mNextFocusDownId = nextFocusDownId;
5136 }
5137
5138 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005139 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5140 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
5141 *
5142 * @attr ref android.R.styleable#View_nextFocusForward
5143 */
5144 public int getNextFocusForwardId() {
5145 return mNextFocusForwardId;
5146 }
5147
5148 /**
5149 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5150 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
5151 * decide automatically.
5152 *
5153 * @attr ref android.R.styleable#View_nextFocusForward
5154 */
5155 public void setNextFocusForwardId(int nextFocusForwardId) {
5156 mNextFocusForwardId = nextFocusForwardId;
5157 }
5158
5159 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005160 * Returns the visibility of this view and all of its ancestors
5161 *
5162 * @return True if this view and all of its ancestors are {@link #VISIBLE}
5163 */
5164 public boolean isShown() {
5165 View current = this;
5166 //noinspection ConstantConditions
5167 do {
5168 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5169 return false;
5170 }
5171 ViewParent parent = current.mParent;
5172 if (parent == null) {
5173 return false; // We are not attached to the view root
5174 }
5175 if (!(parent instanceof View)) {
5176 return true;
5177 }
5178 current = (View) parent;
5179 } while (current != null);
5180
5181 return false;
5182 }
5183
5184 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005185 * Called by the view hierarchy when the content insets for a window have
5186 * changed, to allow it to adjust its content to fit within those windows.
5187 * The content insets tell you the space that the status bar, input method,
5188 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005189 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005190 * <p>You do not normally need to deal with this function, since the default
5191 * window decoration given to applications takes care of applying it to the
5192 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
5193 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
5194 * and your content can be placed under those system elements. You can then
5195 * use this method within your view hierarchy if you have parts of your UI
5196 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005197 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005198 * <p>The default implementation of this method simply applies the content
5199 * inset's to the view's padding. This can be enabled through
5200 * {@link #setFitsSystemWindows(boolean)}. Alternatively, you can override
5201 * the method and handle the insets however you would like. Note that the
5202 * insets provided by the framework are always relative to the far edges
5203 * of the window, not accounting for the location of the called view within
5204 * that window. (In fact when this method is called you do not yet know
5205 * where the layout will place the view, as it is done before layout happens.)
5206 *
5207 * <p>Note: unlike many View methods, there is no dispatch phase to this
5208 * call. If you are overriding it in a ViewGroup and want to allow the
5209 * call to continue to your children, you must be sure to call the super
5210 * implementation.
5211 *
Dianne Hackborncf675782012-05-10 15:07:24 -07005212 * <p>Here is a sample layout that makes use of fitting system windows
5213 * to have controls for a video view placed inside of the window decorations
5214 * that it hides and shows. This can be used with code like the second
5215 * sample (video player) shown in {@link #setSystemUiVisibility(int)}.
5216 *
5217 * {@sample development/samples/ApiDemos/res/layout/video_player.xml complete}
5218 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005219 * @param insets Current content insets of the window. Prior to
5220 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
5221 * the insets or else you and Android will be unhappy.
5222 *
5223 * @return Return true if this view applied the insets and it should not
5224 * continue propagating further down the hierarchy, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005225 */
5226 protected boolean fitSystemWindows(Rect insets) {
5227 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005228 mUserPaddingStart = -1;
5229 mUserPaddingEnd = -1;
5230 mUserPaddingRelative = false;
5231 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
5232 || mAttachInfo == null
5233 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
5234 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
5235 return true;
5236 } else {
5237 internalSetPadding(0, 0, 0, 0);
5238 return false;
5239 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005240 }
5241 return false;
5242 }
5243
5244 /**
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005245 * Set whether or not this view should account for system screen decorations
5246 * such as the status bar and inset its content. This allows this view to be
5247 * positioned in absolute screen coordinates and remain visible to the user.
5248 *
5249 * <p>This should only be used by top-level window decor views.
5250 *
5251 * @param fitSystemWindows true to inset content for system screen decorations, false for
5252 * default behavior.
5253 *
5254 * @attr ref android.R.styleable#View_fitsSystemWindows
5255 */
5256 public void setFitsSystemWindows(boolean fitSystemWindows) {
5257 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
5258 }
5259
5260 /**
Dianne Hackborncf675782012-05-10 15:07:24 -07005261 * Check for state of {@link #setFitsSystemWindows(boolean). If this method
5262 * returns true, this view
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005263 * will account for system screen decorations such as the status bar and inset its
5264 * content. This allows the view to be positioned in absolute screen coordinates
5265 * and remain visible to the user.
5266 *
5267 * @return true if this view will adjust its content bounds for system screen decorations.
5268 *
5269 * @attr ref android.R.styleable#View_fitsSystemWindows
5270 */
Dianne Hackborncf675782012-05-10 15:07:24 -07005271 public boolean getFitsSystemWindows() {
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005272 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
5273 }
5274
Dianne Hackbornb1b55e62012-05-10 16:25:54 -07005275 /** @hide */
5276 public boolean fitsSystemWindows() {
5277 return getFitsSystemWindows();
5278 }
5279
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005280 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005281 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
5282 */
5283 public void requestFitSystemWindows() {
5284 if (mParent != null) {
5285 mParent.requestFitSystemWindows();
5286 }
5287 }
5288
5289 /**
5290 * For use by PhoneWindow to make its own system window fitting optional.
5291 * @hide
5292 */
5293 public void makeOptionalFitsSystemWindows() {
5294 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5295 }
5296
5297 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005298 * Returns the visibility status for this view.
5299 *
5300 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5301 * @attr ref android.R.styleable#View_visibility
5302 */
5303 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005304 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5305 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5306 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005307 })
5308 public int getVisibility() {
5309 return mViewFlags & VISIBILITY_MASK;
5310 }
5311
5312 /**
5313 * Set the enabled state of this view.
5314 *
5315 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5316 * @attr ref android.R.styleable#View_visibility
5317 */
5318 @RemotableViewMethod
5319 public void setVisibility(int visibility) {
5320 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005321 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005322 }
5323
5324 /**
5325 * Returns the enabled status for this view. The interpretation of the
5326 * enabled state varies by subclass.
5327 *
5328 * @return True if this view is enabled, false otherwise.
5329 */
5330 @ViewDebug.ExportedProperty
5331 public boolean isEnabled() {
5332 return (mViewFlags & ENABLED_MASK) == ENABLED;
5333 }
5334
5335 /**
5336 * Set the enabled state of this view. The interpretation of the enabled
5337 * state varies by subclass.
5338 *
5339 * @param enabled True if this view is enabled, false otherwise.
5340 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005341 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005342 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005343 if (enabled == isEnabled()) return;
5344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005345 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5346
5347 /*
5348 * The View most likely has to change its appearance, so refresh
5349 * the drawable state.
5350 */
5351 refreshDrawableState();
5352
5353 // Invalidate too, since the default behavior for views is to be
5354 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005355 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005356 }
5357
5358 /**
5359 * Set whether this view can receive the focus.
5360 *
5361 * Setting this to false will also ensure that this view is not focusable
5362 * in touch mode.
5363 *
5364 * @param focusable If true, this view can receive the focus.
5365 *
5366 * @see #setFocusableInTouchMode(boolean)
5367 * @attr ref android.R.styleable#View_focusable
5368 */
5369 public void setFocusable(boolean focusable) {
5370 if (!focusable) {
5371 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5372 }
5373 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5374 }
5375
5376 /**
5377 * Set whether this view can receive focus while in touch mode.
5378 *
5379 * Setting this to true will also ensure that this view is focusable.
5380 *
5381 * @param focusableInTouchMode If true, this view can receive the focus while
5382 * in touch mode.
5383 *
5384 * @see #setFocusable(boolean)
5385 * @attr ref android.R.styleable#View_focusableInTouchMode
5386 */
5387 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5388 // Focusable in touch mode should always be set before the focusable flag
5389 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5390 // which, in touch mode, will not successfully request focus on this view
5391 // because the focusable in touch mode flag is not set
5392 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5393 if (focusableInTouchMode) {
5394 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5395 }
5396 }
5397
5398 /**
5399 * Set whether this view should have sound effects enabled for events such as
5400 * clicking and touching.
5401 *
5402 * <p>You may wish to disable sound effects for a view if you already play sounds,
5403 * for instance, a dial key that plays dtmf tones.
5404 *
5405 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5406 * @see #isSoundEffectsEnabled()
5407 * @see #playSoundEffect(int)
5408 * @attr ref android.R.styleable#View_soundEffectsEnabled
5409 */
5410 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5411 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5412 }
5413
5414 /**
5415 * @return whether this view should have sound effects enabled for events such as
5416 * clicking and touching.
5417 *
5418 * @see #setSoundEffectsEnabled(boolean)
5419 * @see #playSoundEffect(int)
5420 * @attr ref android.R.styleable#View_soundEffectsEnabled
5421 */
5422 @ViewDebug.ExportedProperty
5423 public boolean isSoundEffectsEnabled() {
5424 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5425 }
5426
5427 /**
5428 * Set whether this view should have haptic feedback for events such as
5429 * long presses.
5430 *
5431 * <p>You may wish to disable haptic feedback if your view already controls
5432 * its own haptic feedback.
5433 *
5434 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5435 * @see #isHapticFeedbackEnabled()
5436 * @see #performHapticFeedback(int)
5437 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5438 */
5439 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5440 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5441 }
5442
5443 /**
5444 * @return whether this view should have haptic feedback enabled for events
5445 * long presses.
5446 *
5447 * @see #setHapticFeedbackEnabled(boolean)
5448 * @see #performHapticFeedback(int)
5449 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5450 */
5451 @ViewDebug.ExportedProperty
5452 public boolean isHapticFeedbackEnabled() {
5453 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5454 }
5455
5456 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005457 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005458 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005459 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5460 * {@link #LAYOUT_DIRECTION_RTL},
5461 * {@link #LAYOUT_DIRECTION_INHERIT} or
5462 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005463 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005464 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005465 * @hide
Cibu Johny7632cb92010-02-22 13:01:02 -08005466 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005467 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005468 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5469 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5470 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5471 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005472 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005473 public int getLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005474 return (mPrivateFlags2 & LAYOUT_DIRECTION_MASK) >> LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005475 }
5476
5477 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005478 * Set the layout direction for this view. This will propagate a reset of layout direction
5479 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005480 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005481 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
5482 * {@link #LAYOUT_DIRECTION_RTL},
5483 * {@link #LAYOUT_DIRECTION_INHERIT} or
5484 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005485 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005486 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005487 * @hide
Cibu Johny7632cb92010-02-22 13:01:02 -08005488 */
5489 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005490 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005491 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005492 // Reset the current layout direction and the resolved one
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005493 mPrivateFlags2 &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07005494 resetResolvedLayoutDirection();
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005495 // Set the new layout direction (filtered) and ask for a layout pass
5496 mPrivateFlags2 |=
5497 ((layoutDirection << LAYOUT_DIRECTION_MASK_SHIFT) & LAYOUT_DIRECTION_MASK);
5498 requestLayout();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005499 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005500 }
5501
5502 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005503 * Returns the resolved layout direction for this view.
5504 *
5505 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005506 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005507 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005508 */
5509 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005510 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5511 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005512 })
5513 public int getResolvedLayoutDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -07005514 // The layout diretion will be resolved only if needed
5515 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) != LAYOUT_DIRECTION_RESOLVED) {
5516 resolveLayoutDirection();
5517 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07005518 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005519 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
5520 }
5521
5522 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005523 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5524 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005525 *
5526 * @return true if the layout is right-to-left.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005527 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005528 */
5529 @ViewDebug.ExportedProperty(category = "layout")
5530 public boolean isLayoutRtl() {
5531 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
5532 }
5533
5534 /**
Adam Powell539ee872012-02-03 19:00:49 -08005535 * Indicates whether the view is currently tracking transient state that the
5536 * app should not need to concern itself with saving and restoring, but that
5537 * the framework should take special note to preserve when possible.
5538 *
Adam Powell785c4472012-05-02 21:25:39 -07005539 * <p>A view with transient state cannot be trivially rebound from an external
5540 * data source, such as an adapter binding item views in a list. This may be
5541 * because the view is performing an animation, tracking user selection
5542 * of content, or similar.</p>
5543 *
Adam Powell539ee872012-02-03 19:00:49 -08005544 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005545 */
5546 @ViewDebug.ExportedProperty(category = "layout")
5547 public boolean hasTransientState() {
5548 return (mPrivateFlags2 & HAS_TRANSIENT_STATE) == HAS_TRANSIENT_STATE;
5549 }
5550
5551 /**
5552 * Set whether this view is currently tracking transient state that the
Chet Haase563d4f22012-04-18 16:20:08 -07005553 * framework should attempt to preserve when possible. This flag is reference counted,
5554 * so every call to setHasTransientState(true) should be paired with a later call
5555 * to setHasTransientState(false).
Adam Powell539ee872012-02-03 19:00:49 -08005556 *
Adam Powell785c4472012-05-02 21:25:39 -07005557 * <p>A view with transient state cannot be trivially rebound from an external
5558 * data source, such as an adapter binding item views in a list. This may be
5559 * because the view is performing an animation, tracking user selection
5560 * of content, or similar.</p>
5561 *
Adam Powell539ee872012-02-03 19:00:49 -08005562 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005563 */
5564 public void setHasTransientState(boolean hasTransientState) {
Chet Haase563d4f22012-04-18 16:20:08 -07005565 mTransientStateCount = hasTransientState ? mTransientStateCount + 1 :
5566 mTransientStateCount - 1;
5567 if (mTransientStateCount < 0) {
5568 mTransientStateCount = 0;
5569 Log.e(VIEW_LOG_TAG, "hasTransientState decremented below 0: " +
5570 "unmatched pair of setHasTransientState calls");
5571 }
5572 if ((hasTransientState && mTransientStateCount == 1) ||
Adam Powell057a5852012-05-11 10:28:38 -07005573 (!hasTransientState && mTransientStateCount == 0)) {
Chet Haase563d4f22012-04-18 16:20:08 -07005574 // update flag if we've just incremented up from 0 or decremented down to 0
5575 mPrivateFlags2 = (mPrivateFlags2 & ~HAS_TRANSIENT_STATE) |
5576 (hasTransientState ? HAS_TRANSIENT_STATE : 0);
5577 if (mParent != null) {
5578 try {
5579 mParent.childHasTransientStateChanged(this, hasTransientState);
5580 } catch (AbstractMethodError e) {
5581 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5582 " does not fully implement ViewParent", e);
5583 }
Adam Powell539ee872012-02-03 19:00:49 -08005584 }
5585 }
5586 }
5587
5588 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005589 * If this view doesn't do any drawing on its own, set this flag to
5590 * allow further optimizations. By default, this flag is not set on
5591 * View, but could be set on some View subclasses such as ViewGroup.
5592 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005593 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5594 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005595 *
5596 * @param willNotDraw whether or not this View draw on its own
5597 */
5598 public void setWillNotDraw(boolean willNotDraw) {
5599 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5600 }
5601
5602 /**
5603 * Returns whether or not this View draws on its own.
5604 *
5605 * @return true if this view has nothing to draw, false otherwise
5606 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005607 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005608 public boolean willNotDraw() {
5609 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
5610 }
5611
5612 /**
5613 * When a View's drawing cache is enabled, drawing is redirected to an
5614 * offscreen bitmap. Some views, like an ImageView, must be able to
5615 * bypass this mechanism if they already draw a single bitmap, to avoid
5616 * unnecessary usage of the memory.
5617 *
5618 * @param willNotCacheDrawing true if this view does not cache its
5619 * drawing, false otherwise
5620 */
5621 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
5622 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
5623 }
5624
5625 /**
5626 * Returns whether or not this View can cache its drawing or not.
5627 *
5628 * @return true if this view does not cache its drawing, false otherwise
5629 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005630 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005631 public boolean willNotCacheDrawing() {
5632 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5633 }
5634
5635 /**
5636 * Indicates whether this view reacts to click events or not.
5637 *
5638 * @return true if the view is clickable, false otherwise
5639 *
5640 * @see #setClickable(boolean)
5641 * @attr ref android.R.styleable#View_clickable
5642 */
5643 @ViewDebug.ExportedProperty
5644 public boolean isClickable() {
5645 return (mViewFlags & CLICKABLE) == CLICKABLE;
5646 }
5647
5648 /**
5649 * Enables or disables click events for this view. When a view
5650 * is clickable it will change its state to "pressed" on every click.
5651 * Subclasses should set the view clickable to visually react to
5652 * user's clicks.
5653 *
5654 * @param clickable true to make the view clickable, false otherwise
5655 *
5656 * @see #isClickable()
5657 * @attr ref android.R.styleable#View_clickable
5658 */
5659 public void setClickable(boolean clickable) {
5660 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5661 }
5662
5663 /**
5664 * Indicates whether this view reacts to long click events or not.
5665 *
5666 * @return true if the view is long clickable, false otherwise
5667 *
5668 * @see #setLongClickable(boolean)
5669 * @attr ref android.R.styleable#View_longClickable
5670 */
5671 public boolean isLongClickable() {
5672 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5673 }
5674
5675 /**
5676 * Enables or disables long click events for this view. When a view is long
5677 * clickable it reacts to the user holding down the button for a longer
5678 * duration than a tap. This event can either launch the listener or a
5679 * context menu.
5680 *
5681 * @param longClickable true to make the view long clickable, false otherwise
5682 * @see #isLongClickable()
5683 * @attr ref android.R.styleable#View_longClickable
5684 */
5685 public void setLongClickable(boolean longClickable) {
5686 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5687 }
5688
5689 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005690 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005691 *
5692 * @see #isClickable()
5693 * @see #setClickable(boolean)
5694 *
5695 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5696 * the View's internal state from a previously set "pressed" state.
5697 */
5698 public void setPressed(boolean pressed) {
Adam Powell035a1fc2012-02-27 15:23:50 -08005699 final boolean needsRefresh = pressed != ((mPrivateFlags & PRESSED) == PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005700
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005701 if (pressed) {
5702 mPrivateFlags |= PRESSED;
5703 } else {
5704 mPrivateFlags &= ~PRESSED;
5705 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005706
5707 if (needsRefresh) {
5708 refreshDrawableState();
5709 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005710 dispatchSetPressed(pressed);
5711 }
5712
5713 /**
5714 * Dispatch setPressed to all of this View's children.
5715 *
5716 * @see #setPressed(boolean)
5717 *
5718 * @param pressed The new pressed state
5719 */
5720 protected void dispatchSetPressed(boolean pressed) {
5721 }
5722
5723 /**
5724 * Indicates whether the view is currently in pressed state. Unless
5725 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5726 * the pressed state.
5727 *
Philip Milne6c8ea062012-04-03 17:38:43 -07005728 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005729 * @see #isClickable()
5730 * @see #setClickable(boolean)
5731 *
5732 * @return true if the view is currently pressed, false otherwise
5733 */
5734 public boolean isPressed() {
5735 return (mPrivateFlags & PRESSED) == PRESSED;
5736 }
5737
5738 /**
5739 * Indicates whether this view will save its state (that is,
5740 * whether its {@link #onSaveInstanceState} method will be called).
5741 *
5742 * @return Returns true if the view state saving is enabled, else false.
5743 *
5744 * @see #setSaveEnabled(boolean)
5745 * @attr ref android.R.styleable#View_saveEnabled
5746 */
5747 public boolean isSaveEnabled() {
5748 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5749 }
5750
5751 /**
5752 * Controls whether the saving of this view's state is
5753 * enabled (that is, whether its {@link #onSaveInstanceState} method
5754 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005755 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005756 * for its state to be saved. This flag can only disable the
5757 * saving of this view; any child views may still have their state saved.
5758 *
5759 * @param enabled Set to false to <em>disable</em> state saving, or true
5760 * (the default) to allow it.
5761 *
5762 * @see #isSaveEnabled()
5763 * @see #setId(int)
5764 * @see #onSaveInstanceState()
5765 * @attr ref android.R.styleable#View_saveEnabled
5766 */
5767 public void setSaveEnabled(boolean enabled) {
5768 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5769 }
5770
Jeff Brown85a31762010-09-01 17:01:00 -07005771 /**
5772 * Gets whether the framework should discard touches when the view's
5773 * window is obscured by another visible window.
5774 * Refer to the {@link View} security documentation for more details.
5775 *
5776 * @return True if touch filtering is enabled.
5777 *
5778 * @see #setFilterTouchesWhenObscured(boolean)
5779 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5780 */
5781 @ViewDebug.ExportedProperty
5782 public boolean getFilterTouchesWhenObscured() {
5783 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5784 }
5785
5786 /**
5787 * Sets whether the framework should discard touches when the view's
5788 * window is obscured by another visible window.
5789 * Refer to the {@link View} security documentation for more details.
5790 *
5791 * @param enabled True if touch filtering should be enabled.
5792 *
5793 * @see #getFilterTouchesWhenObscured
5794 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5795 */
5796 public void setFilterTouchesWhenObscured(boolean enabled) {
5797 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5798 FILTER_TOUCHES_WHEN_OBSCURED);
5799 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005800
5801 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005802 * Indicates whether the entire hierarchy under this view will save its
5803 * state when a state saving traversal occurs from its parent. The default
5804 * is true; if false, these views will not be saved unless
5805 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5806 *
5807 * @return Returns true if the view state saving from parent is enabled, else false.
5808 *
5809 * @see #setSaveFromParentEnabled(boolean)
5810 */
5811 public boolean isSaveFromParentEnabled() {
5812 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5813 }
5814
5815 /**
5816 * Controls whether the entire hierarchy under this view will save its
5817 * state when a state saving traversal occurs from its parent. The default
5818 * is true; if false, these views will not be saved unless
5819 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5820 *
5821 * @param enabled Set to false to <em>disable</em> state saving, or true
5822 * (the default) to allow it.
5823 *
5824 * @see #isSaveFromParentEnabled()
5825 * @see #setId(int)
5826 * @see #onSaveInstanceState()
5827 */
5828 public void setSaveFromParentEnabled(boolean enabled) {
5829 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5830 }
5831
5832
5833 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005834 * Returns whether this View is able to take focus.
5835 *
5836 * @return True if this view can take focus, or false otherwise.
5837 * @attr ref android.R.styleable#View_focusable
5838 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005839 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005840 public final boolean isFocusable() {
5841 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
5842 }
5843
5844 /**
5845 * When a view is focusable, it may not want to take focus when in touch mode.
5846 * For example, a button would like focus when the user is navigating via a D-pad
5847 * so that the user can click on it, but once the user starts touching the screen,
5848 * the button shouldn't take focus
5849 * @return Whether the view is focusable in touch mode.
5850 * @attr ref android.R.styleable#View_focusableInTouchMode
5851 */
5852 @ViewDebug.ExportedProperty
5853 public final boolean isFocusableInTouchMode() {
5854 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5855 }
5856
5857 /**
5858 * Find the nearest view in the specified direction that can take focus.
5859 * This does not actually give focus to that view.
5860 *
5861 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5862 *
5863 * @return The nearest focusable in the specified direction, or null if none
5864 * can be found.
5865 */
5866 public View focusSearch(int direction) {
5867 if (mParent != null) {
5868 return mParent.focusSearch(this, direction);
5869 } else {
5870 return null;
5871 }
5872 }
5873
5874 /**
5875 * This method is the last chance for the focused view and its ancestors to
5876 * respond to an arrow key. This is called when the focused view did not
5877 * consume the key internally, nor could the view system find a new view in
5878 * the requested direction to give focus to.
5879 *
5880 * @param focused The currently focused view.
5881 * @param direction The direction focus wants to move. One of FOCUS_UP,
5882 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5883 * @return True if the this view consumed this unhandled move.
5884 */
5885 public boolean dispatchUnhandledMove(View focused, int direction) {
5886 return false;
5887 }
5888
5889 /**
5890 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005891 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005892 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005893 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5894 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005895 * @return The user specified next view, or null if there is none.
5896 */
5897 View findUserSetNextFocus(View root, int direction) {
5898 switch (direction) {
5899 case FOCUS_LEFT:
5900 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005901 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005902 case FOCUS_RIGHT:
5903 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005904 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005905 case FOCUS_UP:
5906 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005907 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005908 case FOCUS_DOWN:
5909 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005910 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005911 case FOCUS_FORWARD:
5912 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005913 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005914 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08005915 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08005916 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005917 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005918 @Override
5919 public boolean apply(View t) {
5920 return t.mNextFocusForwardId == id;
5921 }
5922 });
5923 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005924 }
5925 return null;
5926 }
5927
Jeff Brown4dfbec22011-08-15 14:55:37 -07005928 private View findViewInsideOutShouldExist(View root, final int childViewId) {
5929 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
5930 @Override
5931 public boolean apply(View t) {
5932 return t.mID == childViewId;
5933 }
5934 });
5935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005936 if (result == null) {
5937 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
5938 + "by user for id " + childViewId);
5939 }
5940 return result;
5941 }
5942
5943 /**
5944 * Find and return all focusable views that are descendants of this view,
5945 * possibly including this view if it is focusable itself.
5946 *
5947 * @param direction The direction of the focus
5948 * @return A list of focusable views
5949 */
5950 public ArrayList<View> getFocusables(int direction) {
5951 ArrayList<View> result = new ArrayList<View>(24);
5952 addFocusables(result, direction);
5953 return result;
5954 }
5955
5956 /**
5957 * Add any focusable views that are descendants of this view (possibly
5958 * including this view if it is focusable itself) to views. If we are in touch mode,
5959 * only add views that are also focusable in touch mode.
5960 *
5961 * @param views Focusable views found so far
5962 * @param direction The direction of the focus
5963 */
5964 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005965 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
5966 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005967
svetoslavganov75986cf2009-05-14 22:28:01 -07005968 /**
5969 * Adds any focusable views that are descendants of this view (possibly
5970 * including this view if it is focusable itself) to views. This method
5971 * adds all focusable views regardless if we are in touch mode or
Svetoslav Ganov42138042012-03-20 11:51:39 -07005972 * only views focusable in touch mode if we are in touch mode or
5973 * only views that can take accessibility focus if accessibility is enabeld
5974 * depending on the focusable mode paramater.
svetoslavganov75986cf2009-05-14 22:28:01 -07005975 *
5976 * @param views Focusable views found so far or null if all we are interested is
5977 * the number of focusables.
5978 * @param direction The direction of the focus.
5979 * @param focusableMode The type of focusables to be added.
5980 *
5981 * @see #FOCUSABLES_ALL
5982 * @see #FOCUSABLES_TOUCH_MODE
5983 */
5984 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07005985 if (views == null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005986 return;
5987 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07005988 if ((focusableMode & FOCUSABLES_ACCESSIBILITY) == FOCUSABLES_ACCESSIBILITY) {
5989 if (AccessibilityManager.getInstance(mContext).isEnabled()
5990 && includeForAccessibility()) {
5991 views.add(this);
5992 return;
5993 }
5994 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07005995 if (!isFocusable()) {
5996 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07005997 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07005998 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE
5999 && isInTouchMode() && !isFocusableInTouchMode()) {
6000 return;
6001 }
6002 views.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006003 }
6004
6005 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006006 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006007 * The search is performed by either the text that the View renders or the content
6008 * description that describes the view for accessibility purposes and the view does
6009 * not render or both. Clients can specify how the search is to be performed via
6010 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
6011 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006012 *
6013 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006014 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07006015 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006016 * @see #FIND_VIEWS_WITH_TEXT
6017 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
6018 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006019 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006020 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07006021 if (getAccessibilityNodeProvider() != null) {
6022 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
6023 outViews.add(this);
6024 }
6025 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006026 && (searched != null && searched.length() > 0)
6027 && (mContentDescription != null && mContentDescription.length() > 0)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006028 String searchedLowerCase = searched.toString().toLowerCase();
6029 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
6030 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
6031 outViews.add(this);
6032 }
6033 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006034 }
6035
6036 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006037 * Find and return all touchable views that are descendants of this view,
6038 * possibly including this view if it is touchable itself.
6039 *
6040 * @return A list of touchable views
6041 */
6042 public ArrayList<View> getTouchables() {
6043 ArrayList<View> result = new ArrayList<View>();
6044 addTouchables(result);
6045 return result;
6046 }
6047
6048 /**
6049 * Add any touchable views that are descendants of this view (possibly
6050 * including this view if it is touchable itself) to views.
6051 *
6052 * @param views Touchable views found so far
6053 */
6054 public void addTouchables(ArrayList<View> views) {
6055 final int viewFlags = mViewFlags;
6056
6057 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
6058 && (viewFlags & ENABLED_MASK) == ENABLED) {
6059 views.add(this);
6060 }
6061 }
6062
6063 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006064 * Returns whether this View is accessibility focused.
6065 *
6066 * @return True if this View is accessibility focused.
6067 */
6068 boolean isAccessibilityFocused() {
6069 return (mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0;
6070 }
6071
6072 /**
6073 * Call this to try to give accessibility focus to this view.
6074 *
6075 * A view will not actually take focus if {@link AccessibilityManager#isEnabled()}
6076 * returns false or the view is no visible or the view already has accessibility
6077 * focus.
6078 *
6079 * See also {@link #focusSearch(int)}, which is what you call to say that you
6080 * have focus, and you want your parent to look for the next one.
6081 *
6082 * @return Whether this view actually took accessibility focus.
6083 *
6084 * @hide
6085 */
6086 public boolean requestAccessibilityFocus() {
Svetoslav Ganov07b726c2012-04-30 12:24:57 -07006087 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
6088 if (!manager.isEnabled() || !manager.isTouchExplorationEnabled()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006089 return false;
6090 }
6091 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6092 return false;
6093 }
6094 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) == 0) {
6095 mPrivateFlags2 |= ACCESSIBILITY_FOCUSED;
6096 ViewRootImpl viewRootImpl = getViewRootImpl();
6097 if (viewRootImpl != null) {
6098 viewRootImpl.setAccessibilityFocusedHost(this);
6099 }
6100 invalidate();
6101 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
6102 notifyAccessibilityStateChanged();
6103 // Try to give input focus to this view - not a descendant.
6104 requestFocusNoSearch(View.FOCUS_DOWN, null);
6105 return true;
6106 }
6107 return false;
6108 }
6109
6110 /**
6111 * Call this to try to clear accessibility focus of this view.
6112 *
6113 * See also {@link #focusSearch(int)}, which is what you call to say that you
6114 * have focus, and you want your parent to look for the next one.
6115 *
6116 * @hide
6117 */
6118 public void clearAccessibilityFocus() {
6119 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6120 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
6121 ViewRootImpl viewRootImpl = getViewRootImpl();
6122 if (viewRootImpl != null) {
6123 viewRootImpl.setAccessibilityFocusedHost(null);
6124 }
6125 invalidate();
6126 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
6127 notifyAccessibilityStateChanged();
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006128
6129 // Clear the text navigation state.
6130 setAccessibilityCursorPosition(-1);
6131
Svetoslav Ganov42138042012-03-20 11:51:39 -07006132 // Try to move accessibility focus to the input focus.
6133 View rootView = getRootView();
6134 if (rootView != null) {
6135 View inputFocus = rootView.findFocus();
6136 if (inputFocus != null) {
6137 inputFocus.requestAccessibilityFocus();
6138 }
6139 }
6140 }
6141 }
6142
6143 /**
6144 * Find the best view to take accessibility focus from a hover.
6145 * This function finds the deepest actionable view and if that
6146 * fails ask the parent to take accessibility focus from hover.
6147 *
6148 * @param x The X hovered location in this view coorditantes.
6149 * @param y The Y hovered location in this view coorditantes.
6150 * @return Whether the request was handled.
6151 *
6152 * @hide
6153 */
6154 public boolean requestAccessibilityFocusFromHover(float x, float y) {
6155 if (onRequestAccessibilityFocusFromHover(x, y)) {
6156 return true;
6157 }
6158 ViewParent parent = mParent;
6159 if (parent instanceof View) {
6160 View parentView = (View) parent;
6161
6162 float[] position = mAttachInfo.mTmpTransformLocation;
6163 position[0] = x;
6164 position[1] = y;
6165
6166 // Compensate for the transformation of the current matrix.
6167 if (!hasIdentityMatrix()) {
6168 getMatrix().mapPoints(position);
6169 }
6170
6171 // Compensate for the parent scroll and the offset
6172 // of this view stop from the parent top.
6173 position[0] += mLeft - parentView.mScrollX;
6174 position[1] += mTop - parentView.mScrollY;
6175
6176 return parentView.requestAccessibilityFocusFromHover(position[0], position[1]);
6177 }
6178 return false;
6179 }
6180
6181 /**
6182 * Requests to give this View focus from hover.
6183 *
6184 * @param x The X hovered location in this view coorditantes.
6185 * @param y The Y hovered location in this view coorditantes.
6186 * @return Whether the request was handled.
6187 *
6188 * @hide
6189 */
6190 public boolean onRequestAccessibilityFocusFromHover(float x, float y) {
6191 if (includeForAccessibility()
6192 && (isActionableForAccessibility() || hasListenersForAccessibility())) {
6193 return requestAccessibilityFocus();
6194 }
6195 return false;
6196 }
6197
6198 /**
6199 * Clears accessibility focus without calling any callback methods
6200 * normally invoked in {@link #clearAccessibilityFocus()}. This method
6201 * is used for clearing accessibility focus when giving this focus to
6202 * another view.
6203 */
6204 void clearAccessibilityFocusNoCallbacks() {
6205 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6206 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
6207 invalidate();
6208 }
6209 }
6210
6211 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006212 * Call this to try to give focus to a specific view or to one of its
6213 * descendants.
6214 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006215 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6216 * false), or if it is focusable and it is not focusable in touch mode
6217 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006218 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006219 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006220 * have focus, and you want your parent to look for the next one.
6221 *
6222 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
6223 * {@link #FOCUS_DOWN} and <code>null</code>.
6224 *
6225 * @return Whether this view or one of its descendants actually took focus.
6226 */
6227 public final boolean requestFocus() {
6228 return requestFocus(View.FOCUS_DOWN);
6229 }
6230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006231 /**
6232 * Call this to try to give focus to a specific view or to one of its
6233 * descendants and give it a hint about what direction focus is heading.
6234 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006235 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6236 * false), or if it is focusable and it is not focusable in touch mode
6237 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006238 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006239 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006240 * have focus, and you want your parent to look for the next one.
6241 *
6242 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
6243 * <code>null</code> set for the previously focused rectangle.
6244 *
6245 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6246 * @return Whether this view or one of its descendants actually took focus.
6247 */
6248 public final boolean requestFocus(int direction) {
6249 return requestFocus(direction, null);
6250 }
6251
6252 /**
6253 * Call this to try to give focus to a specific view or to one of its descendants
6254 * and give it hints about the direction and a specific rectangle that the focus
6255 * is coming from. The rectangle can help give larger views a finer grained hint
6256 * about where focus is coming from, and therefore, where to show selection, or
6257 * forward focus change internally.
6258 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006259 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6260 * false), or if it is focusable and it is not focusable in touch mode
6261 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006262 *
6263 * A View will not take focus if it is not visible.
6264 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006265 * A View will not take focus if one of its parents has
6266 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
6267 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006268 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006269 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006270 * have focus, and you want your parent to look for the next one.
6271 *
6272 * You may wish to override this method if your custom {@link View} has an internal
6273 * {@link View} that it wishes to forward the request to.
6274 *
6275 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6276 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
6277 * to give a finer grained hint about where focus is coming from. May be null
6278 * if there is no hint.
6279 * @return Whether this view or one of its descendants actually took focus.
6280 */
6281 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006282 return requestFocusNoSearch(direction, previouslyFocusedRect);
6283 }
6284
6285 private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006286 // need to be focusable
6287 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
6288 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6289 return false;
6290 }
6291
6292 // need to be focusable in touch mode if in touch mode
6293 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006294 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
6295 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006296 }
6297
6298 // need to not have any parents blocking us
6299 if (hasAncestorThatBlocksDescendantFocus()) {
6300 return false;
6301 }
6302
6303 handleFocusGainInternal(direction, previouslyFocusedRect);
6304 return true;
6305 }
6306
6307 /**
6308 * Call this to try to give focus to a specific view or to one of its descendants. This is a
6309 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
6310 * touch mode to request focus when they are touched.
6311 *
6312 * @return Whether this view or one of its descendants actually took focus.
6313 *
6314 * @see #isInTouchMode()
6315 *
6316 */
6317 public final boolean requestFocusFromTouch() {
6318 // Leave touch mode if we need to
6319 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006320 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07006321 if (viewRoot != null) {
6322 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006323 }
6324 }
6325 return requestFocus(View.FOCUS_DOWN);
6326 }
6327
6328 /**
6329 * @return Whether any ancestor of this view blocks descendant focus.
6330 */
6331 private boolean hasAncestorThatBlocksDescendantFocus() {
6332 ViewParent ancestor = mParent;
6333 while (ancestor instanceof ViewGroup) {
6334 final ViewGroup vgAncestor = (ViewGroup) ancestor;
6335 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
6336 return true;
6337 } else {
6338 ancestor = vgAncestor.getParent();
6339 }
6340 }
6341 return false;
6342 }
6343
6344 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006345 * Gets the mode for determining whether this View is important for accessibility
6346 * which is if it fires accessibility events and if it is reported to
6347 * accessibility services that query the screen.
6348 *
6349 * @return The mode for determining whether a View is important for accessibility.
6350 *
6351 * @attr ref android.R.styleable#View_importantForAccessibility
6352 *
6353 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6354 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6355 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6356 */
6357 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
6358 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_AUTO,
6359 to = "IMPORTANT_FOR_ACCESSIBILITY_AUTO"),
6360 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_YES,
6361 to = "IMPORTANT_FOR_ACCESSIBILITY_YES"),
6362 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_NO,
6363 to = "IMPORTANT_FOR_ACCESSIBILITY_NO")
6364 })
6365 public int getImportantForAccessibility() {
6366 return (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6367 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6368 }
6369
6370 /**
6371 * Sets how to determine whether this view is important for accessibility
6372 * which is if it fires accessibility events and if it is reported to
6373 * accessibility services that query the screen.
6374 *
6375 * @param mode How to determine whether this view is important for accessibility.
6376 *
6377 * @attr ref android.R.styleable#View_importantForAccessibility
6378 *
6379 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6380 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6381 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6382 */
6383 public void setImportantForAccessibility(int mode) {
6384 if (mode != getImportantForAccessibility()) {
6385 mPrivateFlags2 &= ~IMPORTANT_FOR_ACCESSIBILITY_MASK;
6386 mPrivateFlags2 |= (mode << IMPORTANT_FOR_ACCESSIBILITY_SHIFT)
6387 & IMPORTANT_FOR_ACCESSIBILITY_MASK;
6388 notifyAccessibilityStateChanged();
6389 }
6390 }
6391
6392 /**
6393 * Gets whether this view should be exposed for accessibility.
6394 *
6395 * @return Whether the view is exposed for accessibility.
6396 *
6397 * @hide
6398 */
6399 public boolean isImportantForAccessibility() {
6400 final int mode = (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6401 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6402 switch (mode) {
6403 case IMPORTANT_FOR_ACCESSIBILITY_YES:
6404 return true;
6405 case IMPORTANT_FOR_ACCESSIBILITY_NO:
6406 return false;
6407 case IMPORTANT_FOR_ACCESSIBILITY_AUTO:
6408 return isActionableForAccessibility() || hasListenersForAccessibility();
6409 default:
6410 throw new IllegalArgumentException("Unknow important for accessibility mode: "
6411 + mode);
6412 }
6413 }
6414
6415 /**
6416 * Gets the parent for accessibility purposes. Note that the parent for
6417 * accessibility is not necessary the immediate parent. It is the first
6418 * predecessor that is important for accessibility.
6419 *
6420 * @return The parent for accessibility purposes.
6421 */
6422 public ViewParent getParentForAccessibility() {
6423 if (mParent instanceof View) {
6424 View parentView = (View) mParent;
6425 if (parentView.includeForAccessibility()) {
6426 return mParent;
6427 } else {
6428 return mParent.getParentForAccessibility();
6429 }
6430 }
6431 return null;
6432 }
6433
6434 /**
6435 * Adds the children of a given View for accessibility. Since some Views are
6436 * not important for accessibility the children for accessibility are not
6437 * necessarily direct children of the riew, rather they are the first level of
6438 * descendants important for accessibility.
6439 *
6440 * @param children The list of children for accessibility.
6441 */
6442 public void addChildrenForAccessibility(ArrayList<View> children) {
6443 if (includeForAccessibility()) {
6444 children.add(this);
6445 }
6446 }
6447
6448 /**
6449 * Whether to regard this view for accessibility. A view is regarded for
6450 * accessibility if it is important for accessibility or the querying
6451 * accessibility service has explicitly requested that view not
6452 * important for accessibility are regarded.
6453 *
6454 * @return Whether to regard the view for accessibility.
6455 */
6456 boolean includeForAccessibility() {
6457 if (mAttachInfo != null) {
6458 if (!mAttachInfo.mIncludeNotImportantViews) {
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07006459 return isImportantForAccessibility();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006460 } else {
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07006461 return true;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006462 }
6463 }
6464 return false;
6465 }
6466
6467 /**
6468 * Returns whether the View is considered actionable from
6469 * accessibility perspective. Such view are important for
6470 * accessiiblity.
6471 *
6472 * @return True if the view is actionable for accessibility.
6473 */
6474 private boolean isActionableForAccessibility() {
6475 return (isClickable() || isLongClickable() || isFocusable());
6476 }
6477
6478 /**
6479 * Returns whether the View has registered callbacks wich makes it
6480 * important for accessiiblity.
6481 *
6482 * @return True if the view is actionable for accessibility.
6483 */
6484 private boolean hasListenersForAccessibility() {
6485 ListenerInfo info = getListenerInfo();
6486 return mTouchDelegate != null || info.mOnKeyListener != null
6487 || info.mOnTouchListener != null || info.mOnGenericMotionListener != null
6488 || info.mOnHoverListener != null || info.mOnDragListener != null;
6489 }
6490
6491 /**
6492 * Notifies accessibility services that some view's important for
6493 * accessibility state has changed. Note that such notifications
6494 * are made at most once every
6495 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}
6496 * to avoid unnecessary load to the system. Also once a view has
6497 * made a notifucation this method is a NOP until the notification has
6498 * been sent to clients.
6499 *
6500 * @hide
6501 *
6502 * TODO: Makse sure this method is called for any view state change
6503 * that is interesting for accessilility purposes.
6504 */
6505 public void notifyAccessibilityStateChanged() {
6506 if ((mPrivateFlags2 & ACCESSIBILITY_STATE_CHANGED) == 0) {
6507 mPrivateFlags2 |= ACCESSIBILITY_STATE_CHANGED;
6508 if (mParent != null) {
6509 mParent.childAccessibilityStateChanged(this);
6510 }
6511 }
6512 }
6513
6514 /**
6515 * Reset the state indicating the this view has requested clients
6516 * interested in its accessiblity state to be notified.
6517 *
6518 * @hide
6519 */
6520 public void resetAccessibilityStateChanged() {
6521 mPrivateFlags2 &= ~ACCESSIBILITY_STATE_CHANGED;
6522 }
6523
6524 /**
6525 * Performs the specified accessibility action on the view. For
6526 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
alanv8eeefef2012-05-07 16:57:53 -07006527 * <p>
6528 * If an {@link AccessibilityDelegate} has been specified via calling
6529 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
6530 * {@link AccessibilityDelegate#performAccessibilityAction(View, int, Bundle)}
6531 * is responsible for handling this call.
6532 * </p>
Svetoslav Ganov42138042012-03-20 11:51:39 -07006533 *
6534 * @param action The action to perform.
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006535 * @param arguments Optional action arguments.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006536 * @return Whether the action was performed.
6537 */
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006538 public boolean performAccessibilityAction(int action, Bundle arguments) {
alanv8eeefef2012-05-07 16:57:53 -07006539 if (mAccessibilityDelegate != null) {
6540 return mAccessibilityDelegate.performAccessibilityAction(this, action, arguments);
6541 } else {
6542 return performAccessibilityActionInternal(action, arguments);
6543 }
6544 }
6545
6546 /**
6547 * @see #performAccessibilityAction(int, Bundle)
6548 *
6549 * Note: Called from the default {@link AccessibilityDelegate}.
6550 */
6551 boolean performAccessibilityActionInternal(int action, Bundle arguments) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006552 switch (action) {
6553 case AccessibilityNodeInfo.ACTION_CLICK: {
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006554 if (isClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006555 return performClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006556 }
6557 } break;
6558 case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
6559 if (isLongClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006560 return performLongClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006561 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006562 } break;
6563 case AccessibilityNodeInfo.ACTION_FOCUS: {
6564 if (!hasFocus()) {
6565 // Get out of touch mode since accessibility
6566 // wants to move focus around.
6567 getViewRootImpl().ensureTouchMode(false);
6568 return requestFocus();
6569 }
6570 } break;
6571 case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS: {
6572 if (hasFocus()) {
6573 clearFocus();
6574 return !isFocused();
6575 }
6576 } break;
6577 case AccessibilityNodeInfo.ACTION_SELECT: {
6578 if (!isSelected()) {
6579 setSelected(true);
6580 return isSelected();
6581 }
6582 } break;
6583 case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
6584 if (isSelected()) {
6585 setSelected(false);
6586 return !isSelected();
6587 }
6588 } break;
6589 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
6590 if (!isAccessibilityFocused()) {
6591 return requestAccessibilityFocus();
6592 }
6593 } break;
6594 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
6595 if (isAccessibilityFocused()) {
6596 clearAccessibilityFocus();
6597 return true;
6598 }
6599 } break;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006600 case AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY: {
6601 if (arguments != null) {
6602 final int granularity = arguments.getInt(
6603 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6604 return nextAtGranularity(granularity);
6605 }
6606 } break;
6607 case AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY: {
6608 if (arguments != null) {
6609 final int granularity = arguments.getInt(
6610 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6611 return previousAtGranularity(granularity);
6612 }
6613 } break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006614 }
6615 return false;
6616 }
6617
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006618 private boolean nextAtGranularity(int granularity) {
6619 CharSequence text = getIterableTextForAccessibility();
6620 if (text != null && text.length() > 0) {
6621 return false;
6622 }
6623 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6624 if (iterator == null) {
6625 return false;
6626 }
6627 final int current = getAccessibilityCursorPosition();
6628 final int[] range = iterator.following(current);
6629 if (range == null) {
6630 setAccessibilityCursorPosition(-1);
6631 return false;
6632 }
6633 final int start = range[0];
6634 final int end = range[1];
6635 setAccessibilityCursorPosition(start);
6636 sendViewTextTraversedAtGranularityEvent(
6637 AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY,
6638 granularity, start, end);
6639 return true;
6640 }
6641
6642 private boolean previousAtGranularity(int granularity) {
6643 CharSequence text = getIterableTextForAccessibility();
6644 if (text != null && text.length() > 0) {
6645 return false;
6646 }
6647 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6648 if (iterator == null) {
6649 return false;
6650 }
6651 final int selectionStart = getAccessibilityCursorPosition();
6652 final int current = selectionStart >= 0 ? selectionStart : text.length() + 1;
6653 final int[] range = iterator.preceding(current);
6654 if (range == null) {
6655 setAccessibilityCursorPosition(-1);
6656 return false;
6657 }
6658 final int start = range[0];
6659 final int end = range[1];
6660 setAccessibilityCursorPosition(end);
6661 sendViewTextTraversedAtGranularityEvent(
6662 AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
6663 granularity, start, end);
6664 return true;
6665 }
6666
6667 /**
6668 * Gets the text reported for accessibility purposes.
6669 *
6670 * @return The accessibility text.
6671 *
6672 * @hide
6673 */
6674 public CharSequence getIterableTextForAccessibility() {
6675 return mContentDescription;
6676 }
6677
6678 /**
6679 * @hide
6680 */
6681 public int getAccessibilityCursorPosition() {
6682 return mAccessibilityCursorPosition;
6683 }
6684
6685 /**
6686 * @hide
6687 */
6688 public void setAccessibilityCursorPosition(int position) {
6689 mAccessibilityCursorPosition = position;
6690 }
6691
6692 private void sendViewTextTraversedAtGranularityEvent(int action, int granularity,
6693 int fromIndex, int toIndex) {
6694 if (mParent == null) {
6695 return;
6696 }
6697 AccessibilityEvent event = AccessibilityEvent.obtain(
6698 AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY);
6699 onInitializeAccessibilityEvent(event);
6700 onPopulateAccessibilityEvent(event);
6701 event.setFromIndex(fromIndex);
6702 event.setToIndex(toIndex);
6703 event.setAction(action);
6704 event.setMovementGranularity(granularity);
6705 mParent.requestSendAccessibilityEvent(this, event);
6706 }
6707
6708 /**
6709 * @hide
6710 */
6711 public TextSegmentIterator getIteratorForGranularity(int granularity) {
6712 switch (granularity) {
6713 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER: {
6714 CharSequence text = getIterableTextForAccessibility();
6715 if (text != null && text.length() > 0) {
6716 CharacterTextSegmentIterator iterator =
6717 CharacterTextSegmentIterator.getInstance(mContext);
6718 iterator.initialize(text.toString());
6719 return iterator;
6720 }
6721 } break;
6722 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD: {
6723 CharSequence text = getIterableTextForAccessibility();
6724 if (text != null && text.length() > 0) {
6725 WordTextSegmentIterator iterator =
6726 WordTextSegmentIterator.getInstance(mContext);
6727 iterator.initialize(text.toString());
6728 return iterator;
6729 }
6730 } break;
6731 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH: {
6732 CharSequence text = getIterableTextForAccessibility();
6733 if (text != null && text.length() > 0) {
6734 ParagraphTextSegmentIterator iterator =
6735 ParagraphTextSegmentIterator.getInstance();
6736 iterator.initialize(text.toString());
6737 return iterator;
6738 }
6739 } break;
6740 }
6741 return null;
6742 }
6743
Svetoslav Ganov42138042012-03-20 11:51:39 -07006744 /**
Romain Guya440b002010-02-24 15:57:54 -08006745 * @hide
6746 */
6747 public void dispatchStartTemporaryDetach() {
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07006748 clearAccessibilityFocus();
Romain Guya440b002010-02-24 15:57:54 -08006749 onStartTemporaryDetach();
6750 }
6751
6752 /**
6753 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006754 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
6755 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08006756 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006757 */
6758 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08006759 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08006760 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08006761 }
6762
6763 /**
6764 * @hide
6765 */
6766 public void dispatchFinishTemporaryDetach() {
6767 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006768 }
Romain Guy8506ab42009-06-11 17:35:47 -07006769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006770 /**
6771 * Called after {@link #onStartTemporaryDetach} when the container is done
6772 * changing the view.
6773 */
6774 public void onFinishTemporaryDetach() {
6775 }
Romain Guy8506ab42009-06-11 17:35:47 -07006776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006777 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006778 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
6779 * for this view's window. Returns null if the view is not currently attached
6780 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07006781 * just use the standard high-level event callbacks like
6782 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006783 */
6784 public KeyEvent.DispatcherState getKeyDispatcherState() {
6785 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
6786 }
Joe Malin32736f02011-01-19 16:14:20 -08006787
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006788 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006789 * Dispatch a key event before it is processed by any input method
6790 * associated with the view hierarchy. This can be used to intercept
6791 * key events in special situations before the IME consumes them; a
6792 * typical example would be handling the BACK key to update the application's
6793 * UI instead of allowing the IME to see it and close itself.
6794 *
6795 * @param event The key event to be dispatched.
6796 * @return True if the event was handled, false otherwise.
6797 */
6798 public boolean dispatchKeyEventPreIme(KeyEvent event) {
6799 return onKeyPreIme(event.getKeyCode(), event);
6800 }
6801
6802 /**
6803 * Dispatch a key event to the next view on the focus path. This path runs
6804 * from the top of the view tree down to the currently focused view. If this
6805 * view has focus, it will dispatch to itself. Otherwise it will dispatch
6806 * the next node down the focus path. This method also fires any key
6807 * listeners.
6808 *
6809 * @param event The key event to be dispatched.
6810 * @return True if the event was handled, false otherwise.
6811 */
6812 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006813 if (mInputEventConsistencyVerifier != null) {
6814 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
6815 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006816
Jeff Brown21bc5c92011-02-28 18:27:14 -08006817 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07006818 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006819 ListenerInfo li = mListenerInfo;
6820 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6821 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006822 return true;
6823 }
6824
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006825 if (event.dispatch(this, mAttachInfo != null
6826 ? mAttachInfo.mKeyDispatchState : null, this)) {
6827 return true;
6828 }
6829
6830 if (mInputEventConsistencyVerifier != null) {
6831 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6832 }
6833 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006834 }
6835
6836 /**
6837 * Dispatches a key shortcut event.
6838 *
6839 * @param event The key event to be dispatched.
6840 * @return True if the event was handled by the view, false otherwise.
6841 */
6842 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
6843 return onKeyShortcut(event.getKeyCode(), event);
6844 }
6845
6846 /**
6847 * Pass the touch screen motion event down to the target view, or this
6848 * view if it is the target.
6849 *
6850 * @param event The motion event to be dispatched.
6851 * @return True if the event was handled by the view, false otherwise.
6852 */
6853 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006854 if (mInputEventConsistencyVerifier != null) {
6855 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
6856 }
6857
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006858 if (onFilterTouchEventForSecurity(event)) {
6859 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006860 ListenerInfo li = mListenerInfo;
6861 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6862 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006863 return true;
6864 }
6865
6866 if (onTouchEvent(event)) {
6867 return true;
6868 }
Jeff Brown85a31762010-09-01 17:01:00 -07006869 }
6870
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006871 if (mInputEventConsistencyVerifier != null) {
6872 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006873 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006874 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006875 }
6876
6877 /**
Jeff Brown85a31762010-09-01 17:01:00 -07006878 * Filter the touch event to apply security policies.
6879 *
6880 * @param event The motion event to be filtered.
6881 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08006882 *
Jeff Brown85a31762010-09-01 17:01:00 -07006883 * @see #getFilterTouchesWhenObscured
6884 */
6885 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07006886 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07006887 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
6888 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
6889 // Window is obscured, drop this touch.
6890 return false;
6891 }
6892 return true;
6893 }
6894
6895 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006896 * Pass a trackball motion event down to the focused view.
6897 *
6898 * @param event The motion event to be dispatched.
6899 * @return True if the event was handled by the view, false otherwise.
6900 */
6901 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006902 if (mInputEventConsistencyVerifier != null) {
6903 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
6904 }
6905
Romain Guy02ccac62011-06-24 13:20:23 -07006906 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006907 }
6908
6909 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08006910 * Dispatch a generic motion event.
6911 * <p>
6912 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6913 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08006914 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07006915 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006916 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08006917 *
6918 * @param event The motion event to be dispatched.
6919 * @return True if the event was handled by the view, false otherwise.
6920 */
6921 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006922 if (mInputEventConsistencyVerifier != null) {
6923 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
6924 }
6925
Jeff Browna032cc02011-03-07 16:56:21 -08006926 final int source = event.getSource();
6927 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
6928 final int action = event.getAction();
6929 if (action == MotionEvent.ACTION_HOVER_ENTER
6930 || action == MotionEvent.ACTION_HOVER_MOVE
6931 || action == MotionEvent.ACTION_HOVER_EXIT) {
6932 if (dispatchHoverEvent(event)) {
6933 return true;
6934 }
6935 } else if (dispatchGenericPointerEvent(event)) {
6936 return true;
6937 }
6938 } else if (dispatchGenericFocusedEvent(event)) {
6939 return true;
6940 }
6941
Jeff Brown10b62902011-06-20 16:40:37 -07006942 if (dispatchGenericMotionEventInternal(event)) {
6943 return true;
6944 }
6945
6946 if (mInputEventConsistencyVerifier != null) {
6947 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6948 }
6949 return false;
6950 }
6951
6952 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07006953 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006954 ListenerInfo li = mListenerInfo;
6955 if (li != null && li.mOnGenericMotionListener != null
6956 && (mViewFlags & ENABLED_MASK) == ENABLED
6957 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006958 return true;
6959 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006960
6961 if (onGenericMotionEvent(event)) {
6962 return true;
6963 }
6964
6965 if (mInputEventConsistencyVerifier != null) {
6966 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6967 }
6968 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08006969 }
6970
6971 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006972 * Dispatch a hover event.
6973 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07006974 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07006975 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006976 * </p>
6977 *
6978 * @param event The motion event to be dispatched.
6979 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006980 */
6981 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07006982 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006983 ListenerInfo li = mListenerInfo;
6984 if (li != null && li.mOnHoverListener != null
6985 && (mViewFlags & ENABLED_MASK) == ENABLED
6986 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07006987 return true;
6988 }
6989
Jeff Browna032cc02011-03-07 16:56:21 -08006990 return onHoverEvent(event);
6991 }
6992
6993 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006994 * Returns true if the view has a child to which it has recently sent
6995 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
6996 * it does not have a hovered child, then it must be the innermost hovered view.
6997 * @hide
6998 */
6999 protected boolean hasHoveredChild() {
7000 return false;
7001 }
7002
7003 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007004 * Dispatch a generic motion event to the view under the first pointer.
7005 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007006 * Do not call this method directly.
7007 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007008 * </p>
7009 *
7010 * @param event The motion event to be dispatched.
7011 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007012 */
7013 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
7014 return false;
7015 }
7016
7017 /**
7018 * Dispatch a generic motion event to the currently focused view.
7019 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007020 * Do not call this method directly.
7021 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007022 * </p>
7023 *
7024 * @param event The motion event to be dispatched.
7025 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007026 */
7027 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
7028 return false;
7029 }
7030
7031 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007032 * Dispatch a pointer event.
7033 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007034 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
7035 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
7036 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08007037 * and should not be expected to handle other pointing device features.
7038 * </p>
7039 *
7040 * @param event The motion event to be dispatched.
7041 * @return True if the event was handled by the view, false otherwise.
7042 * @hide
7043 */
7044 public final boolean dispatchPointerEvent(MotionEvent event) {
7045 if (event.isTouchEvent()) {
7046 return dispatchTouchEvent(event);
7047 } else {
7048 return dispatchGenericMotionEvent(event);
7049 }
7050 }
7051
7052 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007053 * Called when the window containing this view gains or loses window focus.
7054 * ViewGroups should override to route to their children.
7055 *
7056 * @param hasFocus True if the window containing this view now has focus,
7057 * false otherwise.
7058 */
7059 public void dispatchWindowFocusChanged(boolean hasFocus) {
7060 onWindowFocusChanged(hasFocus);
7061 }
7062
7063 /**
7064 * Called when the window containing this view gains or loses focus. Note
7065 * that this is separate from view focus: to receive key events, both
7066 * your view and its window must have focus. If a window is displayed
7067 * on top of yours that takes input focus, then your own window will lose
7068 * focus but the view focus will remain unchanged.
7069 *
7070 * @param hasWindowFocus True if the window containing this view now has
7071 * focus, false otherwise.
7072 */
7073 public void onWindowFocusChanged(boolean hasWindowFocus) {
7074 InputMethodManager imm = InputMethodManager.peekInstance();
7075 if (!hasWindowFocus) {
7076 if (isPressed()) {
7077 setPressed(false);
7078 }
7079 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
7080 imm.focusOut(this);
7081 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007082 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08007083 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07007084 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007085 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
7086 imm.focusIn(this);
7087 }
7088 refreshDrawableState();
7089 }
7090
7091 /**
7092 * Returns true if this view is in a window that currently has window focus.
7093 * Note that this is not the same as the view itself having focus.
7094 *
7095 * @return True if this view is in a window that currently has window focus.
7096 */
7097 public boolean hasWindowFocus() {
7098 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
7099 }
7100
7101 /**
Adam Powell326d8082009-12-09 15:10:07 -08007102 * Dispatch a view visibility change down the view hierarchy.
7103 * ViewGroups should override to route to their children.
7104 * @param changedView The view whose visibility changed. Could be 'this' or
7105 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007106 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7107 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007108 */
7109 protected void dispatchVisibilityChanged(View changedView, int visibility) {
7110 onVisibilityChanged(changedView, visibility);
7111 }
7112
7113 /**
7114 * Called when the visibility of the view or an ancestor of the view is changed.
7115 * @param changedView The view whose visibility changed. Could be 'this' or
7116 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007117 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7118 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007119 */
7120 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007121 if (visibility == VISIBLE) {
7122 if (mAttachInfo != null) {
7123 initialAwakenScrollBars();
7124 } else {
7125 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
7126 }
7127 }
Adam Powell326d8082009-12-09 15:10:07 -08007128 }
7129
7130 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08007131 * Dispatch a hint about whether this view is displayed. For instance, when
7132 * a View moves out of the screen, it might receives a display hint indicating
7133 * the view is not displayed. Applications should not <em>rely</em> on this hint
7134 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007135 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007136 * @param hint A hint about whether or not this view is displayed:
7137 * {@link #VISIBLE} or {@link #INVISIBLE}.
7138 */
7139 public void dispatchDisplayHint(int hint) {
7140 onDisplayHint(hint);
7141 }
7142
7143 /**
7144 * Gives this view a hint about whether is displayed or not. For instance, when
7145 * a View moves out of the screen, it might receives a display hint indicating
7146 * the view is not displayed. Applications should not <em>rely</em> on this hint
7147 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007148 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007149 * @param hint A hint about whether or not this view is displayed:
7150 * {@link #VISIBLE} or {@link #INVISIBLE}.
7151 */
7152 protected void onDisplayHint(int hint) {
7153 }
7154
7155 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007156 * Dispatch a window visibility change down the view hierarchy.
7157 * ViewGroups should override to route to their children.
7158 *
7159 * @param visibility The new visibility of the window.
7160 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007161 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007162 */
7163 public void dispatchWindowVisibilityChanged(int visibility) {
7164 onWindowVisibilityChanged(visibility);
7165 }
7166
7167 /**
7168 * Called when the window containing has change its visibility
7169 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
7170 * that this tells you whether or not your window is being made visible
7171 * to the window manager; this does <em>not</em> tell you whether or not
7172 * your window is obscured by other windows on the screen, even if it
7173 * is itself visible.
7174 *
7175 * @param visibility The new visibility of the window.
7176 */
7177 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007178 if (visibility == VISIBLE) {
7179 initialAwakenScrollBars();
7180 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007181 }
7182
7183 /**
7184 * Returns the current visibility of the window this view is attached to
7185 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
7186 *
7187 * @return Returns the current visibility of the view's window.
7188 */
7189 public int getWindowVisibility() {
7190 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
7191 }
7192
7193 /**
7194 * Retrieve the overall visible display size in which the window this view is
7195 * attached to has been positioned in. This takes into account screen
7196 * decorations above the window, for both cases where the window itself
7197 * is being position inside of them or the window is being placed under
7198 * then and covered insets are used for the window to position its content
7199 * inside. In effect, this tells you the available area where content can
7200 * be placed and remain visible to users.
7201 *
7202 * <p>This function requires an IPC back to the window manager to retrieve
7203 * the requested information, so should not be used in performance critical
7204 * code like drawing.
7205 *
7206 * @param outRect Filled in with the visible display frame. If the view
7207 * is not attached to a window, this is simply the raw display size.
7208 */
7209 public void getWindowVisibleDisplayFrame(Rect outRect) {
7210 if (mAttachInfo != null) {
7211 try {
7212 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
7213 } catch (RemoteException e) {
7214 return;
7215 }
7216 // XXX This is really broken, and probably all needs to be done
7217 // in the window manager, and we need to know more about whether
7218 // we want the area behind or in front of the IME.
7219 final Rect insets = mAttachInfo.mVisibleInsets;
7220 outRect.left += insets.left;
7221 outRect.top += insets.top;
7222 outRect.right -= insets.right;
7223 outRect.bottom -= insets.bottom;
7224 return;
7225 }
7226 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07007227 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007228 }
7229
7230 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007231 * Dispatch a notification about a resource configuration change down
7232 * the view hierarchy.
7233 * ViewGroups should override to route to their children.
7234 *
7235 * @param newConfig The new resource configuration.
7236 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007237 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007238 */
7239 public void dispatchConfigurationChanged(Configuration newConfig) {
7240 onConfigurationChanged(newConfig);
7241 }
7242
7243 /**
7244 * Called when the current configuration of the resources being used
7245 * by the application have changed. You can use this to decide when
7246 * to reload resources that can changed based on orientation and other
7247 * configuration characterstics. You only need to use this if you are
7248 * not relying on the normal {@link android.app.Activity} mechanism of
7249 * recreating the activity instance upon a configuration change.
7250 *
7251 * @param newConfig The new resource configuration.
7252 */
7253 protected void onConfigurationChanged(Configuration newConfig) {
7254 }
7255
7256 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007257 * Private function to aggregate all per-view attributes in to the view
7258 * root.
7259 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007260 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7261 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007262 }
7263
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007264 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7265 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08007266 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007267 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007268 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007269 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007270 ListenerInfo li = mListenerInfo;
7271 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007272 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007273 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007274 }
7275 }
7276
7277 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08007278 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007279 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08007280 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
7281 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007282 ai.mRecomputeGlobalAttributes = true;
7283 }
7284 }
7285 }
7286
7287 /**
7288 * Returns whether the device is currently in touch mode. Touch mode is entered
7289 * once the user begins interacting with the device by touch, and affects various
7290 * things like whether focus is always visible to the user.
7291 *
7292 * @return Whether the device is in touch mode.
7293 */
7294 @ViewDebug.ExportedProperty
7295 public boolean isInTouchMode() {
7296 if (mAttachInfo != null) {
7297 return mAttachInfo.mInTouchMode;
7298 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07007299 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007300 }
7301 }
7302
7303 /**
7304 * Returns the context the view is running in, through which it can
7305 * access the current theme, resources, etc.
7306 *
7307 * @return The view's Context.
7308 */
7309 @ViewDebug.CapturedViewProperty
7310 public final Context getContext() {
7311 return mContext;
7312 }
7313
7314 /**
7315 * Handle a key event before it is processed by any input method
7316 * associated with the view hierarchy. This can be used to intercept
7317 * key events in special situations before the IME consumes them; a
7318 * typical example would be handling the BACK key to update the application's
7319 * UI instead of allowing the IME to see it and close itself.
7320 *
7321 * @param keyCode The value in event.getKeyCode().
7322 * @param event Description of the key event.
7323 * @return If you handled the event, return true. If you want to allow the
7324 * event to be handled by the next receiver, return false.
7325 */
7326 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7327 return false;
7328 }
7329
7330 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007331 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7332 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007333 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7334 * is released, if the view is enabled and clickable.
7335 *
7336 * @param keyCode A key code that represents the button pressed, from
7337 * {@link android.view.KeyEvent}.
7338 * @param event The KeyEvent object that defines the button action.
7339 */
7340 public boolean onKeyDown(int keyCode, KeyEvent event) {
7341 boolean result = false;
7342
7343 switch (keyCode) {
7344 case KeyEvent.KEYCODE_DPAD_CENTER:
7345 case KeyEvent.KEYCODE_ENTER: {
7346 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7347 return true;
7348 }
7349 // Long clickable items don't necessarily have to be clickable
7350 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7351 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7352 (event.getRepeatCount() == 0)) {
7353 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007354 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007355 return true;
7356 }
7357 break;
7358 }
7359 }
7360 return result;
7361 }
7362
7363 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007364 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7365 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7366 * the event).
7367 */
7368 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7369 return false;
7370 }
7371
7372 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007373 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7374 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007375 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7376 * {@link KeyEvent#KEYCODE_ENTER} is released.
7377 *
7378 * @param keyCode A key code that represents the button pressed, from
7379 * {@link android.view.KeyEvent}.
7380 * @param event The KeyEvent object that defines the button action.
7381 */
7382 public boolean onKeyUp(int keyCode, KeyEvent event) {
7383 boolean result = false;
7384
7385 switch (keyCode) {
7386 case KeyEvent.KEYCODE_DPAD_CENTER:
7387 case KeyEvent.KEYCODE_ENTER: {
7388 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7389 return true;
7390 }
7391 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7392 setPressed(false);
7393
7394 if (!mHasPerformedLongPress) {
7395 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007396 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007397
7398 result = performClick();
7399 }
7400 }
7401 break;
7402 }
7403 }
7404 return result;
7405 }
7406
7407 /**
7408 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7409 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7410 * the event).
7411 *
7412 * @param keyCode A key code that represents the button pressed, from
7413 * {@link android.view.KeyEvent}.
7414 * @param repeatCount The number of times the action was made.
7415 * @param event The KeyEvent object that defines the button action.
7416 */
7417 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7418 return false;
7419 }
7420
7421 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007422 * Called on the focused view when a key shortcut event is not handled.
7423 * Override this method to implement local key shortcuts for the View.
7424 * Key shortcuts can also be implemented by setting the
7425 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007426 *
7427 * @param keyCode The value in event.getKeyCode().
7428 * @param event Description of the key event.
7429 * @return If you handled the event, return true. If you want to allow the
7430 * event to be handled by the next receiver, return false.
7431 */
7432 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7433 return false;
7434 }
7435
7436 /**
7437 * Check whether the called view is a text editor, in which case it
7438 * would make sense to automatically display a soft input window for
7439 * it. Subclasses should override this if they implement
7440 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007441 * a call on that method would return a non-null InputConnection, and
7442 * they are really a first-class editor that the user would normally
7443 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007444 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007445 * <p>The default implementation always returns false. This does
7446 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7447 * will not be called or the user can not otherwise perform edits on your
7448 * view; it is just a hint to the system that this is not the primary
7449 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007450 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007451 * @return Returns true if this view is a text editor, else false.
7452 */
7453 public boolean onCheckIsTextEditor() {
7454 return false;
7455 }
Romain Guy8506ab42009-06-11 17:35:47 -07007456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007457 /**
7458 * Create a new InputConnection for an InputMethod to interact
7459 * with the view. The default implementation returns null, since it doesn't
7460 * support input methods. You can override this to implement such support.
7461 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007462 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007463 * <p>When implementing this, you probably also want to implement
7464 * {@link #onCheckIsTextEditor()} to indicate you will return a
7465 * non-null InputConnection.
7466 *
7467 * @param outAttrs Fill in with attribute information about the connection.
7468 */
7469 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7470 return null;
7471 }
7472
7473 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007474 * Called by the {@link android.view.inputmethod.InputMethodManager}
7475 * when a view who is not the current
7476 * input connection target is trying to make a call on the manager. The
7477 * default implementation returns false; you can override this to return
7478 * true for certain views if you are performing InputConnection proxying
7479 * to them.
7480 * @param view The View that is making the InputMethodManager call.
7481 * @return Return true to allow the call, false to reject.
7482 */
7483 public boolean checkInputConnectionProxy(View view) {
7484 return false;
7485 }
Romain Guy8506ab42009-06-11 17:35:47 -07007486
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007487 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007488 * Show the context menu for this view. It is not safe to hold on to the
7489 * menu after returning from this method.
7490 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007491 * You should normally not overload this method. Overload
7492 * {@link #onCreateContextMenu(ContextMenu)} or define an
7493 * {@link OnCreateContextMenuListener} to add items to the context menu.
7494 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007495 * @param menu The context menu to populate
7496 */
7497 public void createContextMenu(ContextMenu menu) {
7498 ContextMenuInfo menuInfo = getContextMenuInfo();
7499
7500 // Sets the current menu info so all items added to menu will have
7501 // my extra info set.
7502 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7503
7504 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007505 ListenerInfo li = mListenerInfo;
7506 if (li != null && li.mOnCreateContextMenuListener != null) {
7507 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007508 }
7509
7510 // Clear the extra information so subsequent items that aren't mine don't
7511 // have my extra info.
7512 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7513
7514 if (mParent != null) {
7515 mParent.createContextMenu(menu);
7516 }
7517 }
7518
7519 /**
7520 * Views should implement this if they have extra information to associate
7521 * with the context menu. The return result is supplied as a parameter to
7522 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7523 * callback.
7524 *
7525 * @return Extra information about the item for which the context menu
7526 * should be shown. This information will vary across different
7527 * subclasses of View.
7528 */
7529 protected ContextMenuInfo getContextMenuInfo() {
7530 return null;
7531 }
7532
7533 /**
7534 * Views should implement this if the view itself is going to add items to
7535 * the context menu.
7536 *
7537 * @param menu the context menu to populate
7538 */
7539 protected void onCreateContextMenu(ContextMenu menu) {
7540 }
7541
7542 /**
7543 * Implement this method to handle trackball motion events. The
7544 * <em>relative</em> movement of the trackball since the last event
7545 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
7546 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
7547 * that a movement of 1 corresponds to the user pressing one DPAD key (so
7548 * they will often be fractional values, representing the more fine-grained
7549 * movement information available from a trackball).
7550 *
7551 * @param event The motion event.
7552 * @return True if the event was handled, false otherwise.
7553 */
7554 public boolean onTrackballEvent(MotionEvent event) {
7555 return false;
7556 }
7557
7558 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08007559 * Implement this method to handle generic motion events.
7560 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08007561 * Generic motion events describe joystick movements, mouse hovers, track pad
7562 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08007563 * {@link MotionEvent#getSource() source} of the motion event specifies
7564 * the class of input that was received. Implementations of this method
7565 * must examine the bits in the source before processing the event.
7566 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007567 * </p><p>
7568 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7569 * are delivered to the view under the pointer. All other generic motion events are
7570 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08007571 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07007572 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08007573 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007574 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
7575 * // process the joystick movement...
7576 * return true;
7577 * }
7578 * }
7579 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
7580 * switch (event.getAction()) {
7581 * case MotionEvent.ACTION_HOVER_MOVE:
7582 * // process the mouse hover movement...
7583 * return true;
7584 * case MotionEvent.ACTION_SCROLL:
7585 * // process the scroll wheel movement...
7586 * return true;
7587 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08007588 * }
7589 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07007590 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08007591 *
7592 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08007593 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08007594 */
7595 public boolean onGenericMotionEvent(MotionEvent event) {
7596 return false;
7597 }
7598
7599 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007600 * Implement this method to handle hover events.
7601 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07007602 * This method is called whenever a pointer is hovering into, over, or out of the
7603 * bounds of a view and the view is not currently being touched.
7604 * Hover events are represented as pointer events with action
7605 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
7606 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
7607 * </p>
7608 * <ul>
7609 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
7610 * when the pointer enters the bounds of the view.</li>
7611 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
7612 * when the pointer has already entered the bounds of the view and has moved.</li>
7613 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
7614 * when the pointer has exited the bounds of the view or when the pointer is
7615 * about to go down due to a button click, tap, or similar user action that
7616 * causes the view to be touched.</li>
7617 * </ul>
7618 * <p>
7619 * The view should implement this method to return true to indicate that it is
7620 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08007621 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07007622 * The default implementation calls {@link #setHovered} to update the hovered state
7623 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07007624 * is enabled and is clickable. The default implementation also sends hover
7625 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08007626 * </p>
7627 *
7628 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07007629 * @return True if the view handled the hover event.
7630 *
7631 * @see #isHovered
7632 * @see #setHovered
7633 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007634 */
7635 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007636 // The root view may receive hover (or touch) events that are outside the bounds of
7637 // the window. This code ensures that we only send accessibility events for
7638 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07007639 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007640 if (!mSendingHoverAccessibilityEvents) {
7641 if ((action == MotionEvent.ACTION_HOVER_ENTER
7642 || action == MotionEvent.ACTION_HOVER_MOVE)
7643 && !hasHoveredChild()
7644 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007645 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007646 mSendingHoverAccessibilityEvents = true;
7647 requestAccessibilityFocusFromHover((int) event.getX(), (int) event.getY());
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007648 }
7649 } else {
7650 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07007651 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007652 && !pointInView(event.getX(), event.getY()))) {
7653 mSendingHoverAccessibilityEvents = false;
7654 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007655 // If the window does not have input focus we take away accessibility
7656 // focus as soon as the user stop hovering over the view.
Jeff Brown59a422e2012-04-19 15:19:19 -07007657 if (mAttachInfo != null && !mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007658 getViewRootImpl().setAccessibilityFocusedHost(null);
7659 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007660 }
Jeff Browna1b24182011-07-28 13:38:24 -07007661 }
7662
Jeff Brown87b7f802011-06-21 18:35:45 -07007663 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007664 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07007665 case MotionEvent.ACTION_HOVER_ENTER:
7666 setHovered(true);
7667 break;
7668 case MotionEvent.ACTION_HOVER_EXIT:
7669 setHovered(false);
7670 break;
7671 }
Jeff Browna1b24182011-07-28 13:38:24 -07007672
7673 // Dispatch the event to onGenericMotionEvent before returning true.
7674 // This is to provide compatibility with existing applications that
7675 // handled HOVER_MOVE events in onGenericMotionEvent and that would
7676 // break because of the new default handling for hoverable views
7677 // in onHoverEvent.
7678 // Note that onGenericMotionEvent will be called by default when
7679 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
7680 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07007681 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08007682 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007683
Svetoslav Ganov736c2752011-04-22 18:30:36 -07007684 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08007685 }
7686
7687 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007688 * Returns true if the view should handle {@link #onHoverEvent}
7689 * by calling {@link #setHovered} to change its hovered state.
7690 *
7691 * @return True if the view is hoverable.
7692 */
7693 private boolean isHoverable() {
7694 final int viewFlags = mViewFlags;
7695 if ((viewFlags & ENABLED_MASK) == DISABLED) {
7696 return false;
7697 }
7698
7699 return (viewFlags & CLICKABLE) == CLICKABLE
7700 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
7701 }
7702
7703 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007704 * Returns true if the view is currently hovered.
7705 *
7706 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007707 *
7708 * @see #setHovered
7709 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007710 */
Jeff Brown10b62902011-06-20 16:40:37 -07007711 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08007712 public boolean isHovered() {
7713 return (mPrivateFlags & HOVERED) != 0;
7714 }
7715
7716 /**
7717 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007718 * <p>
7719 * Calling this method also changes the drawable state of the view. This
7720 * enables the view to react to hover by using different drawable resources
7721 * to change its appearance.
7722 * </p><p>
7723 * The {@link #onHoverChanged} method is called when the hovered state changes.
7724 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08007725 *
7726 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007727 *
7728 * @see #isHovered
7729 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007730 */
7731 public void setHovered(boolean hovered) {
7732 if (hovered) {
7733 if ((mPrivateFlags & HOVERED) == 0) {
7734 mPrivateFlags |= HOVERED;
7735 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007736 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08007737 }
7738 } else {
7739 if ((mPrivateFlags & HOVERED) != 0) {
7740 mPrivateFlags &= ~HOVERED;
7741 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007742 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08007743 }
7744 }
7745 }
7746
7747 /**
Jeff Brown10b62902011-06-20 16:40:37 -07007748 * Implement this method to handle hover state changes.
7749 * <p>
7750 * This method is called whenever the hover state changes as a result of a
7751 * call to {@link #setHovered}.
7752 * </p>
7753 *
7754 * @param hovered The current hover state, as returned by {@link #isHovered}.
7755 *
7756 * @see #isHovered
7757 * @see #setHovered
7758 */
7759 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07007760 }
7761
7762 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007763 * Implement this method to handle touch screen motion events.
7764 *
7765 * @param event The motion event.
7766 * @return True if the event was handled, false otherwise.
7767 */
7768 public boolean onTouchEvent(MotionEvent event) {
7769 final int viewFlags = mViewFlags;
7770
7771 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007772 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08007773 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007774 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007775 // A disabled view that is clickable still consumes the touch
7776 // events, it just doesn't respond to them.
7777 return (((viewFlags & CLICKABLE) == CLICKABLE ||
7778 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
7779 }
7780
7781 if (mTouchDelegate != null) {
7782 if (mTouchDelegate.onTouchEvent(event)) {
7783 return true;
7784 }
7785 }
7786
7787 if (((viewFlags & CLICKABLE) == CLICKABLE ||
7788 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
7789 switch (event.getAction()) {
7790 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08007791 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
7792 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007793 // take focus if we don't have it already and we should in
7794 // touch mode.
7795 boolean focusTaken = false;
7796 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
7797 focusTaken = requestFocus();
7798 }
7799
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007800 if (prepressed) {
7801 // The button is being released before we actually
7802 // showed it as pressed. Make it show the pressed
7803 // state now (before scheduling the click) to ensure
7804 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08007805 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007806 }
Joe Malin32736f02011-01-19 16:14:20 -08007807
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007808 if (!mHasPerformedLongPress) {
7809 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007810 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007811
7812 // Only perform take click actions if we were in the pressed state
7813 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08007814 // Use a Runnable and post this rather than calling
7815 // performClick directly. This lets other visual state
7816 // of the view update before click actions start.
7817 if (mPerformClick == null) {
7818 mPerformClick = new PerformClick();
7819 }
7820 if (!post(mPerformClick)) {
7821 performClick();
7822 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007823 }
7824 }
7825
7826 if (mUnsetPressedState == null) {
7827 mUnsetPressedState = new UnsetPressedState();
7828 }
7829
Adam Powelle14579b2009-12-16 18:39:52 -08007830 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08007831 postDelayed(mUnsetPressedState,
7832 ViewConfiguration.getPressedStateDuration());
7833 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007834 // If the post failed, unpress right now
7835 mUnsetPressedState.run();
7836 }
Adam Powelle14579b2009-12-16 18:39:52 -08007837 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007838 }
7839 break;
7840
7841 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08007842 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007843
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07007844 if (performButtonActionOnTouchDown(event)) {
7845 break;
7846 }
7847
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007848 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07007849 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007850
7851 // For views inside a scrolling container, delay the pressed feedback for
7852 // a short period in case this is a scroll.
7853 if (isInScrollingContainer) {
7854 mPrivateFlags |= PREPRESSED;
7855 if (mPendingCheckForTap == null) {
7856 mPendingCheckForTap = new CheckForTap();
7857 }
7858 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
7859 } else {
7860 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08007861 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007862 checkForLongClick(0);
7863 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007864 break;
7865
7866 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08007867 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08007868 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007869 break;
7870
7871 case MotionEvent.ACTION_MOVE:
7872 final int x = (int) event.getX();
7873 final int y = (int) event.getY();
7874
7875 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07007876 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007877 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08007878 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007879 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08007880 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05007881 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007882
Adam Powell4d6f0662012-02-21 15:11:11 -08007883 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007884 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007885 }
7886 break;
7887 }
7888 return true;
7889 }
7890
7891 return false;
7892 }
7893
7894 /**
Adam Powell10298662011-08-14 18:26:30 -07007895 * @hide
7896 */
7897 public boolean isInScrollingContainer() {
7898 ViewParent p = getParent();
7899 while (p != null && p instanceof ViewGroup) {
7900 if (((ViewGroup) p).shouldDelayChildPressedState()) {
7901 return true;
7902 }
7903 p = p.getParent();
7904 }
7905 return false;
7906 }
7907
7908 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05007909 * Remove the longpress detection timer.
7910 */
7911 private void removeLongPressCallback() {
7912 if (mPendingCheckForLongPress != null) {
7913 removeCallbacks(mPendingCheckForLongPress);
7914 }
7915 }
Adam Powell3cb8b632011-01-21 15:34:14 -08007916
7917 /**
7918 * Remove the pending click action
7919 */
7920 private void removePerformClickCallback() {
7921 if (mPerformClick != null) {
7922 removeCallbacks(mPerformClick);
7923 }
7924 }
7925
Adam Powelle14579b2009-12-16 18:39:52 -08007926 /**
Romain Guya440b002010-02-24 15:57:54 -08007927 * Remove the prepress detection timer.
7928 */
7929 private void removeUnsetPressCallback() {
7930 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
7931 setPressed(false);
7932 removeCallbacks(mUnsetPressedState);
7933 }
7934 }
7935
7936 /**
Adam Powelle14579b2009-12-16 18:39:52 -08007937 * Remove the tap detection timer.
7938 */
7939 private void removeTapCallback() {
7940 if (mPendingCheckForTap != null) {
7941 mPrivateFlags &= ~PREPRESSED;
7942 removeCallbacks(mPendingCheckForTap);
7943 }
7944 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007945
7946 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007947 * Cancels a pending long press. Your subclass can use this if you
7948 * want the context menu to come up if the user presses and holds
7949 * at the same place, but you don't want it to come up if they press
7950 * and then move around enough to cause scrolling.
7951 */
7952 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05007953 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08007954
7955 /*
7956 * The prepressed state handled by the tap callback is a display
7957 * construct, but the tap callback will post a long press callback
7958 * less its own timeout. Remove it here.
7959 */
7960 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007961 }
7962
7963 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07007964 * Remove the pending callback for sending a
7965 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
7966 */
7967 private void removeSendViewScrolledAccessibilityEventCallback() {
7968 if (mSendViewScrolledAccessibilityEvent != null) {
7969 removeCallbacks(mSendViewScrolledAccessibilityEvent);
7970 }
7971 }
7972
7973 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007974 * Sets the TouchDelegate for this View.
7975 */
7976 public void setTouchDelegate(TouchDelegate delegate) {
7977 mTouchDelegate = delegate;
7978 }
7979
7980 /**
7981 * Gets the TouchDelegate for this View.
7982 */
7983 public TouchDelegate getTouchDelegate() {
7984 return mTouchDelegate;
7985 }
7986
7987 /**
7988 * Set flags controlling behavior of this view.
7989 *
7990 * @param flags Constant indicating the value which should be set
7991 * @param mask Constant indicating the bit range that should be changed
7992 */
7993 void setFlags(int flags, int mask) {
7994 int old = mViewFlags;
7995 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
7996
7997 int changed = mViewFlags ^ old;
7998 if (changed == 0) {
7999 return;
8000 }
8001 int privateFlags = mPrivateFlags;
8002
8003 /* Check if the FOCUSABLE bit has changed */
8004 if (((changed & FOCUSABLE_MASK) != 0) &&
8005 ((privateFlags & HAS_BOUNDS) !=0)) {
8006 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
8007 && ((privateFlags & FOCUSED) != 0)) {
8008 /* Give up focus if we are no longer focusable */
8009 clearFocus();
8010 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
8011 && ((privateFlags & FOCUSED) == 0)) {
8012 /*
8013 * Tell the view system that we are now available to take focus
8014 * if no one else already has it.
8015 */
8016 if (mParent != null) mParent.focusableViewAvailable(this);
8017 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008018 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8019 notifyAccessibilityStateChanged();
8020 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008021 }
8022
8023 if ((flags & VISIBILITY_MASK) == VISIBLE) {
8024 if ((changed & VISIBILITY_MASK) != 0) {
8025 /*
Chet Haase4324ead2011-08-24 21:31:03 -07008026 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07008027 * it was not visible. Marking it drawn ensures that the invalidation will
8028 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008029 */
Chet Haaseaceafe62011-08-26 15:44:33 -07008030 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07008031 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008032
8033 needGlobalAttributesUpdate(true);
8034
8035 // a view becoming visible is worth notifying the parent
8036 // about in case nothing has focus. even if this specific view
8037 // isn't focusable, it may contain something that is, so let
8038 // the root view try to give this focus if nothing else does.
8039 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
8040 mParent.focusableViewAvailable(this);
8041 }
8042 }
8043 }
8044
8045 /* Check if the GONE bit has changed */
8046 if ((changed & GONE) != 0) {
8047 needGlobalAttributesUpdate(false);
8048 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008049
Romain Guyecd80ee2009-12-03 17:13:02 -08008050 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
8051 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008052 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08008053 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07008054 if (mParent instanceof View) {
8055 // GONE views noop invalidation, so invalidate the parent
8056 ((View) mParent).invalidate(true);
8057 }
8058 // Mark the view drawn to ensure that it gets invalidated properly the next
8059 // time it is visible and gets invalidated
8060 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008061 }
8062 if (mAttachInfo != null) {
8063 mAttachInfo.mViewVisibilityChanged = true;
8064 }
8065 }
8066
8067 /* Check if the VISIBLE bit has changed */
8068 if ((changed & INVISIBLE) != 0) {
8069 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07008070 /*
8071 * If this view is becoming invisible, set the DRAWN flag so that
8072 * the next invalidate() will not be skipped.
8073 */
8074 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008075
8076 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008077 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008078 if (getRootView() != this) {
8079 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008080 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008081 }
8082 }
8083 if (mAttachInfo != null) {
8084 mAttachInfo.mViewVisibilityChanged = true;
8085 }
8086 }
8087
Adam Powell326d8082009-12-09 15:10:07 -08008088 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07008089 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08008090 ((ViewGroup) mParent).onChildVisibilityChanged(this,
8091 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08008092 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07008093 } else if (mParent != null) {
8094 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07008095 }
Adam Powell326d8082009-12-09 15:10:07 -08008096 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
8097 }
8098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008099 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
8100 destroyDrawingCache();
8101 }
8102
8103 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
8104 destroyDrawingCache();
8105 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008106 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008107 }
8108
8109 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
8110 destroyDrawingCache();
8111 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8112 }
8113
8114 if ((changed & DRAW_MASK) != 0) {
8115 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07008116 if (mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008117 mPrivateFlags &= ~SKIP_DRAW;
8118 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
8119 } else {
8120 mPrivateFlags |= SKIP_DRAW;
8121 }
8122 } else {
8123 mPrivateFlags &= ~SKIP_DRAW;
8124 }
8125 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08008126 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008127 }
8128
8129 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08008130 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008131 mParent.recomputeViewAttributes(this);
8132 }
8133 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008134
8135 if (AccessibilityManager.getInstance(mContext).isEnabled()
8136 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
8137 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
8138 notifyAccessibilityStateChanged();
8139 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008140 }
8141
8142 /**
8143 * Change the view's z order in the tree, so it's on top of other sibling
8144 * views
8145 */
8146 public void bringToFront() {
8147 if (mParent != null) {
8148 mParent.bringChildToFront(this);
8149 }
8150 }
8151
8152 /**
8153 * This is called in response to an internal scroll in this view (i.e., the
8154 * view scrolled its own contents). This is typically as a result of
8155 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
8156 * called.
8157 *
8158 * @param l Current horizontal scroll origin.
8159 * @param t Current vertical scroll origin.
8160 * @param oldl Previous horizontal scroll origin.
8161 * @param oldt Previous vertical scroll origin.
8162 */
8163 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07008164 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8165 postSendViewScrolledAccessibilityEventCallback();
8166 }
8167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008168 mBackgroundSizeChanged = true;
8169
8170 final AttachInfo ai = mAttachInfo;
8171 if (ai != null) {
8172 ai.mViewScrollChanged = true;
8173 }
8174 }
8175
8176 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008177 * Interface definition for a callback to be invoked when the layout bounds of a view
8178 * changes due to layout processing.
8179 */
8180 public interface OnLayoutChangeListener {
8181 /**
8182 * Called when the focus state of a view has changed.
8183 *
8184 * @param v The view whose state has changed.
8185 * @param left The new value of the view's left property.
8186 * @param top The new value of the view's top property.
8187 * @param right The new value of the view's right property.
8188 * @param bottom The new value of the view's bottom property.
8189 * @param oldLeft The previous value of the view's left property.
8190 * @param oldTop The previous value of the view's top property.
8191 * @param oldRight The previous value of the view's right property.
8192 * @param oldBottom The previous value of the view's bottom property.
8193 */
8194 void onLayoutChange(View v, int left, int top, int right, int bottom,
8195 int oldLeft, int oldTop, int oldRight, int oldBottom);
8196 }
8197
8198 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008199 * This is called during layout when the size of this view has changed. If
8200 * you were just added to the view hierarchy, you're called with the old
8201 * values of 0.
8202 *
8203 * @param w Current width of this view.
8204 * @param h Current height of this view.
8205 * @param oldw Old width of this view.
8206 * @param oldh Old height of this view.
8207 */
8208 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
8209 }
8210
8211 /**
8212 * Called by draw to draw the child views. This may be overridden
8213 * by derived classes to gain control just before its children are drawn
8214 * (but after its own view has been drawn).
8215 * @param canvas the canvas on which to draw the view
8216 */
8217 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008219 }
8220
8221 /**
8222 * Gets the parent of this view. Note that the parent is a
8223 * ViewParent and not necessarily a View.
8224 *
8225 * @return Parent of this view.
8226 */
8227 public final ViewParent getParent() {
8228 return mParent;
8229 }
8230
8231 /**
Chet Haasecca2c982011-05-20 14:34:18 -07008232 * Set the horizontal scrolled position of your view. This will cause a call to
8233 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8234 * invalidated.
8235 * @param value the x position to scroll to
8236 */
8237 public void setScrollX(int value) {
8238 scrollTo(value, mScrollY);
8239 }
8240
8241 /**
8242 * Set the vertical scrolled position of your view. This will cause a call to
8243 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8244 * invalidated.
8245 * @param value the y position to scroll to
8246 */
8247 public void setScrollY(int value) {
8248 scrollTo(mScrollX, value);
8249 }
8250
8251 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008252 * Return the scrolled left position of this view. This is the left edge of
8253 * the displayed part of your view. You do not need to draw any pixels
8254 * farther left, since those are outside of the frame of your view on
8255 * screen.
8256 *
8257 * @return The left edge of the displayed part of your view, in pixels.
8258 */
8259 public final int getScrollX() {
8260 return mScrollX;
8261 }
8262
8263 /**
8264 * Return the scrolled top position of this view. This is the top edge of
8265 * the displayed part of your view. You do not need to draw any pixels above
8266 * it, since those are outside of the frame of your view on screen.
8267 *
8268 * @return The top edge of the displayed part of your view, in pixels.
8269 */
8270 public final int getScrollY() {
8271 return mScrollY;
8272 }
8273
8274 /**
8275 * Return the width of the your view.
8276 *
8277 * @return The width of your view, in pixels.
8278 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008279 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008280 public final int getWidth() {
8281 return mRight - mLeft;
8282 }
8283
8284 /**
8285 * Return the height of your view.
8286 *
8287 * @return The height of your view, in pixels.
8288 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008289 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008290 public final int getHeight() {
8291 return mBottom - mTop;
8292 }
8293
8294 /**
8295 * Return the visible drawing bounds of your view. Fills in the output
8296 * rectangle with the values from getScrollX(), getScrollY(),
8297 * getWidth(), and getHeight().
8298 *
8299 * @param outRect The (scrolled) drawing bounds of the view.
8300 */
8301 public void getDrawingRect(Rect outRect) {
8302 outRect.left = mScrollX;
8303 outRect.top = mScrollY;
8304 outRect.right = mScrollX + (mRight - mLeft);
8305 outRect.bottom = mScrollY + (mBottom - mTop);
8306 }
8307
8308 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008309 * Like {@link #getMeasuredWidthAndState()}, but only returns the
8310 * raw width component (that is the result is masked by
8311 * {@link #MEASURED_SIZE_MASK}).
8312 *
8313 * @return The raw measured width of this view.
8314 */
8315 public final int getMeasuredWidth() {
8316 return mMeasuredWidth & MEASURED_SIZE_MASK;
8317 }
8318
8319 /**
8320 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008321 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008322 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008323 * This should be used during measurement and layout calculations only. Use
8324 * {@link #getWidth()} to see how wide a view is after layout.
8325 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008326 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008327 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008328 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008329 return mMeasuredWidth;
8330 }
8331
8332 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008333 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8334 * raw width component (that is the result is masked by
8335 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008336 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008337 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008338 */
8339 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008340 return mMeasuredHeight & MEASURED_SIZE_MASK;
8341 }
8342
8343 /**
8344 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008345 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008346 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8347 * This should be used during measurement and layout calculations only. Use
8348 * {@link #getHeight()} to see how wide a view is after layout.
8349 *
8350 * @return The measured width of this view as a bit mask.
8351 */
8352 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008353 return mMeasuredHeight;
8354 }
8355
8356 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008357 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8358 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8359 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8360 * and the height component is at the shifted bits
8361 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8362 */
8363 public final int getMeasuredState() {
8364 return (mMeasuredWidth&MEASURED_STATE_MASK)
8365 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8366 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8367 }
8368
8369 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008370 * The transform matrix of this view, which is calculated based on the current
8371 * roation, scale, and pivot properties.
8372 *
8373 * @see #getRotation()
8374 * @see #getScaleX()
8375 * @see #getScaleY()
8376 * @see #getPivotX()
8377 * @see #getPivotY()
8378 * @return The current transform matrix for the view
8379 */
8380 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008381 if (mTransformationInfo != null) {
8382 updateMatrix();
8383 return mTransformationInfo.mMatrix;
8384 }
8385 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008386 }
8387
8388 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008389 * Utility function to determine if the value is far enough away from zero to be
8390 * considered non-zero.
8391 * @param value A floating point value to check for zero-ness
8392 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8393 */
8394 private static boolean nonzero(float value) {
8395 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8396 }
8397
8398 /**
Jeff Brown86671742010-09-30 20:00:15 -07008399 * Returns true if the transform matrix is the identity matrix.
8400 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008401 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008402 * @return True if the transform matrix is the identity matrix, false otherwise.
8403 */
Jeff Brown86671742010-09-30 20:00:15 -07008404 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008405 if (mTransformationInfo != null) {
8406 updateMatrix();
8407 return mTransformationInfo.mMatrixIsIdentity;
8408 }
8409 return true;
8410 }
8411
8412 void ensureTransformationInfo() {
8413 if (mTransformationInfo == null) {
8414 mTransformationInfo = new TransformationInfo();
8415 }
Jeff Brown86671742010-09-30 20:00:15 -07008416 }
8417
8418 /**
8419 * Recomputes the transform matrix if necessary.
8420 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008421 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008422 final TransformationInfo info = mTransformationInfo;
8423 if (info == null) {
8424 return;
8425 }
8426 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008427 // transform-related properties have changed since the last time someone
8428 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008429
8430 // Figure out if we need to update the pivot point
8431 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008432 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8433 info.mPrevWidth = mRight - mLeft;
8434 info.mPrevHeight = mBottom - mTop;
8435 info.mPivotX = info.mPrevWidth / 2f;
8436 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008437 }
8438 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008439 info.mMatrix.reset();
8440 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8441 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8442 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8443 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008444 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008445 if (info.mCamera == null) {
8446 info.mCamera = new Camera();
8447 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008448 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008449 info.mCamera.save();
8450 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8451 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8452 info.mCamera.getMatrix(info.matrix3D);
8453 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8454 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8455 info.mPivotY + info.mTranslationY);
8456 info.mMatrix.postConcat(info.matrix3D);
8457 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008458 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008459 info.mMatrixDirty = false;
8460 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8461 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008462 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008463 }
8464
8465 /**
8466 * Utility method to retrieve the inverse of the current mMatrix property.
8467 * We cache the matrix to avoid recalculating it when transform properties
8468 * have not changed.
8469 *
8470 * @return The inverse of the current matrix of this view.
8471 */
Jeff Brown86671742010-09-30 20:00:15 -07008472 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008473 final TransformationInfo info = mTransformationInfo;
8474 if (info != null) {
8475 updateMatrix();
8476 if (info.mInverseMatrixDirty) {
8477 if (info.mInverseMatrix == null) {
8478 info.mInverseMatrix = new Matrix();
8479 }
8480 info.mMatrix.invert(info.mInverseMatrix);
8481 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008482 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008483 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008484 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008485 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008486 }
8487
8488 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008489 * Gets the distance along the Z axis from the camera to this view.
8490 *
8491 * @see #setCameraDistance(float)
8492 *
8493 * @return The distance along the Z axis.
8494 */
8495 public float getCameraDistance() {
8496 ensureTransformationInfo();
8497 final float dpi = mResources.getDisplayMetrics().densityDpi;
8498 final TransformationInfo info = mTransformationInfo;
8499 if (info.mCamera == null) {
8500 info.mCamera = new Camera();
8501 info.matrix3D = new Matrix();
8502 }
8503 return -(info.mCamera.getLocationZ() * dpi);
8504 }
8505
8506 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008507 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8508 * views are drawn) from the camera to this view. The camera's distance
8509 * affects 3D transformations, for instance rotations around the X and Y
8510 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008511 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008512 * use a camera distance that's greater than the height (X axis rotation) or
8513 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008514 *
Romain Guya5364ee2011-02-24 14:46:04 -08008515 * <p>The distance of the camera from the view plane can have an affect on the
8516 * perspective distortion of the view when it is rotated around the x or y axis.
8517 * For example, a large distance will result in a large viewing angle, and there
8518 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008519 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008520 * also result in some drawing artifacts if the rotated view ends up partially
8521 * behind the camera (which is why the recommendation is to use a distance at
8522 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008523 *
Romain Guya5364ee2011-02-24 14:46:04 -08008524 * <p>The distance is expressed in "depth pixels." The default distance depends
8525 * on the screen density. For instance, on a medium density display, the
8526 * default distance is 1280. On a high density display, the default distance
8527 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008528 *
Romain Guya5364ee2011-02-24 14:46:04 -08008529 * <p>If you want to specify a distance that leads to visually consistent
8530 * results across various densities, use the following formula:</p>
8531 * <pre>
8532 * float scale = context.getResources().getDisplayMetrics().density;
8533 * view.setCameraDistance(distance * scale);
8534 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07008535 *
Romain Guya5364ee2011-02-24 14:46:04 -08008536 * <p>The density scale factor of a high density display is 1.5,
8537 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008538 *
Romain Guya5364ee2011-02-24 14:46:04 -08008539 * @param distance The distance in "depth pixels", if negative the opposite
8540 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07008541 *
8542 * @see #setRotationX(float)
8543 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08008544 */
8545 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008546 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08008547
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008548 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08008549 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008550 final TransformationInfo info = mTransformationInfo;
8551 if (info.mCamera == null) {
8552 info.mCamera = new Camera();
8553 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08008554 }
8555
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008556 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
8557 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08008558
Chet Haase9d1992d2012-03-13 11:03:25 -07008559 invalidateViewProperty(false, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008560 if (mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07008561 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08008562 }
Romain Guya5364ee2011-02-24 14:46:04 -08008563 }
8564
8565 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008566 * The degrees that the view is rotated around the pivot point.
8567 *
Philip Milne6c8ea062012-04-03 17:38:43 -07008568 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07008569 * @see #getPivotX()
8570 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008571 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008572 * @return The degrees of rotation.
8573 */
Chet Haasea5531132012-02-02 13:41:44 -08008574 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008575 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008576 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008577 }
8578
8579 /**
Chet Haase897247b2010-09-09 14:54:47 -07008580 * Sets the degrees that the view is rotated around the pivot point. Increasing values
8581 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07008582 *
8583 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008584 *
8585 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07008586 * @see #getPivotX()
8587 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008588 * @see #setRotationX(float)
8589 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08008590 *
8591 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07008592 */
8593 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008594 ensureTransformationInfo();
8595 final TransformationInfo info = mTransformationInfo;
8596 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008597 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008598 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008599 info.mRotation = rotation;
8600 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008601 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008602 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008603 mDisplayList.setRotation(rotation);
8604 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008605 }
8606 }
8607
8608 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008609 * The degrees that the view is rotated around the vertical axis through the pivot point.
8610 *
8611 * @see #getPivotX()
8612 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008613 * @see #setRotationY(float)
8614 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008615 * @return The degrees of Y rotation.
8616 */
Chet Haasea5531132012-02-02 13:41:44 -08008617 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008618 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008619 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008620 }
8621
8622 /**
Chet Haase897247b2010-09-09 14:54:47 -07008623 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
8624 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
8625 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008626 *
Romain Guya5364ee2011-02-24 14:46:04 -08008627 * When rotating large views, it is recommended to adjust the camera distance
8628 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008629 *
8630 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008631 *
8632 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07008633 * @see #getPivotX()
8634 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008635 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008636 * @see #setRotationX(float)
8637 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008638 *
8639 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07008640 */
8641 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008642 ensureTransformationInfo();
8643 final TransformationInfo info = mTransformationInfo;
8644 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008645 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008646 info.mRotationY = rotationY;
8647 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008648 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008649 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008650 mDisplayList.setRotationY(rotationY);
8651 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008652 }
8653 }
8654
8655 /**
8656 * The degrees that the view is rotated around the horizontal axis through the pivot point.
8657 *
8658 * @see #getPivotX()
8659 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008660 * @see #setRotationX(float)
8661 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008662 * @return The degrees of X rotation.
8663 */
Chet Haasea5531132012-02-02 13:41:44 -08008664 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008665 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008666 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008667 }
8668
8669 /**
Chet Haase897247b2010-09-09 14:54:47 -07008670 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
8671 * Increasing values result in clockwise rotation from the viewpoint of looking down the
8672 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008673 *
Romain Guya5364ee2011-02-24 14:46:04 -08008674 * When rotating large views, it is recommended to adjust the camera distance
8675 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008676 *
8677 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008678 *
8679 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07008680 * @see #getPivotX()
8681 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008682 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008683 * @see #setRotationY(float)
8684 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008685 *
8686 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07008687 */
8688 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008689 ensureTransformationInfo();
8690 final TransformationInfo info = mTransformationInfo;
8691 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008692 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008693 info.mRotationX = rotationX;
8694 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008695 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008696 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008697 mDisplayList.setRotationX(rotationX);
8698 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008699 }
8700 }
8701
8702 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008703 * The amount that the view is scaled in x around the pivot point, as a proportion of
8704 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
8705 *
Joe Onorato93162322010-09-16 15:42:01 -04008706 * <p>By default, this is 1.0f.
8707 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008708 * @see #getPivotX()
8709 * @see #getPivotY()
8710 * @return The scaling factor.
8711 */
Chet Haasea5531132012-02-02 13:41:44 -08008712 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008713 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008714 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008715 }
8716
8717 /**
8718 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
8719 * the view's unscaled width. A value of 1 means that no scaling is applied.
8720 *
8721 * @param scaleX The scaling factor.
8722 * @see #getPivotX()
8723 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008724 *
8725 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07008726 */
8727 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008728 ensureTransformationInfo();
8729 final TransformationInfo info = mTransformationInfo;
8730 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008731 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008732 info.mScaleX = scaleX;
8733 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008734 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008735 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008736 mDisplayList.setScaleX(scaleX);
8737 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008738 }
8739 }
8740
8741 /**
8742 * The amount that the view is scaled in y around the pivot point, as a proportion of
8743 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
8744 *
Joe Onorato93162322010-09-16 15:42:01 -04008745 * <p>By default, this is 1.0f.
8746 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008747 * @see #getPivotX()
8748 * @see #getPivotY()
8749 * @return The scaling factor.
8750 */
Chet Haasea5531132012-02-02 13:41:44 -08008751 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008752 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008753 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008754 }
8755
8756 /**
8757 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
8758 * the view's unscaled width. A value of 1 means that no scaling is applied.
8759 *
8760 * @param scaleY The scaling factor.
8761 * @see #getPivotX()
8762 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008763 *
8764 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07008765 */
8766 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008767 ensureTransformationInfo();
8768 final TransformationInfo info = mTransformationInfo;
8769 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008770 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008771 info.mScaleY = scaleY;
8772 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008773 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008774 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008775 mDisplayList.setScaleY(scaleY);
8776 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008777 }
8778 }
8779
8780 /**
8781 * The x location of the point around which the view is {@link #setRotation(float) rotated}
8782 * and {@link #setScaleX(float) scaled}.
8783 *
8784 * @see #getRotation()
8785 * @see #getScaleX()
8786 * @see #getScaleY()
8787 * @see #getPivotY()
8788 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008789 *
8790 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008791 */
Chet Haasea5531132012-02-02 13:41:44 -08008792 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008793 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008794 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008795 }
8796
8797 /**
8798 * Sets the x location of the point around which the view is
8799 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07008800 * By default, the pivot point is centered on the object.
8801 * Setting this property disables this behavior and causes the view to use only the
8802 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008803 *
8804 * @param pivotX The x location of the pivot point.
8805 * @see #getRotation()
8806 * @see #getScaleX()
8807 * @see #getScaleY()
8808 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008809 *
8810 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008811 */
8812 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008813 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07008814 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008815 final TransformationInfo info = mTransformationInfo;
8816 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008817 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008818 info.mPivotX = pivotX;
8819 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008820 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008821 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008822 mDisplayList.setPivotX(pivotX);
8823 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008824 }
8825 }
8826
8827 /**
8828 * The y location of the point around which the view is {@link #setRotation(float) rotated}
8829 * and {@link #setScaleY(float) scaled}.
8830 *
8831 * @see #getRotation()
8832 * @see #getScaleX()
8833 * @see #getScaleY()
8834 * @see #getPivotY()
8835 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008836 *
8837 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07008838 */
Chet Haasea5531132012-02-02 13:41:44 -08008839 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008840 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008841 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008842 }
8843
8844 /**
8845 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07008846 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
8847 * Setting this property disables this behavior and causes the view to use only the
8848 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008849 *
8850 * @param pivotY The y location of the pivot point.
8851 * @see #getRotation()
8852 * @see #getScaleX()
8853 * @see #getScaleY()
8854 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008855 *
8856 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07008857 */
8858 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008859 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07008860 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008861 final TransformationInfo info = mTransformationInfo;
8862 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008863 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008864 info.mPivotY = pivotY;
8865 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008866 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008867 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008868 mDisplayList.setPivotY(pivotY);
8869 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008870 }
8871 }
8872
8873 /**
8874 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
8875 * completely transparent and 1 means the view is completely opaque.
8876 *
Joe Onorato93162322010-09-16 15:42:01 -04008877 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07008878 * @return The opacity of the view.
8879 */
Chet Haasea5531132012-02-02 13:41:44 -08008880 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008881 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008882 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008883 }
8884
8885 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07008886 * Returns whether this View has content which overlaps. This function, intended to be
8887 * overridden by specific View types, is an optimization when alpha is set on a view. If
8888 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
8889 * and then composited it into place, which can be expensive. If the view has no overlapping
8890 * rendering, the view can draw each primitive with the appropriate alpha value directly.
8891 * An example of overlapping rendering is a TextView with a background image, such as a
8892 * Button. An example of non-overlapping rendering is a TextView with no background, or
8893 * an ImageView with only the foreground image. The default implementation returns true;
8894 * subclasses should override if they have cases which can be optimized.
8895 *
8896 * @return true if the content in this view might overlap, false otherwise.
8897 */
8898 public boolean hasOverlappingRendering() {
8899 return true;
8900 }
8901
8902 /**
Romain Guy171c5922011-01-06 10:04:23 -08008903 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
8904 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008905 *
Romain Guy171c5922011-01-06 10:04:23 -08008906 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
8907 * responsible for applying the opacity itself. Otherwise, calling this method is
8908 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08008909 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07008910 *
Chet Haasea5531132012-02-02 13:41:44 -08008911 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
8912 * performance implications. It is generally best to use the alpha property sparingly and
8913 * transiently, as in the case of fading animations.</p>
8914 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008915 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08008916 *
Joe Malin32736f02011-01-19 16:14:20 -08008917 * @see #setLayerType(int, android.graphics.Paint)
8918 *
Chet Haase73066682010-11-29 15:55:32 -08008919 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07008920 */
8921 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008922 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008923 if (mTransformationInfo.mAlpha != alpha) {
8924 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008925 if (onSetAlpha((int) (alpha * 255))) {
8926 mPrivateFlags |= ALPHA_SET;
8927 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07008928 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008929 invalidate(true);
8930 } else {
8931 mPrivateFlags &= ~ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07008932 invalidateViewProperty(true, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008933 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008934 mDisplayList.setAlpha(alpha);
8935 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008936 }
Chet Haaseed032702010-10-01 14:05:54 -07008937 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008938 }
8939
8940 /**
Chet Haasea00f3862011-02-22 06:34:40 -08008941 * Faster version of setAlpha() which performs the same steps except there are
8942 * no calls to invalidate(). The caller of this function should perform proper invalidation
8943 * on the parent and this object. The return value indicates whether the subclass handles
8944 * alpha (the return value for onSetAlpha()).
8945 *
8946 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008947 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
8948 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08008949 */
8950 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008951 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008952 if (mTransformationInfo.mAlpha != alpha) {
8953 mTransformationInfo.mAlpha = alpha;
8954 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
8955 if (subclassHandlesAlpha) {
8956 mPrivateFlags |= ALPHA_SET;
8957 return true;
8958 } else {
8959 mPrivateFlags &= ~ALPHA_SET;
Chet Haase1271e2c2012-04-20 09:54:27 -07008960 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008961 mDisplayList.setAlpha(alpha);
8962 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008963 }
Chet Haasea00f3862011-02-22 06:34:40 -08008964 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008965 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08008966 }
8967
8968 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008969 * Top position of this view relative to its parent.
8970 *
8971 * @return The top of this view, in pixels.
8972 */
8973 @ViewDebug.CapturedViewProperty
8974 public final int getTop() {
8975 return mTop;
8976 }
8977
8978 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008979 * Sets the top position of this view relative to its parent. This method is meant to be called
8980 * by the layout system and should not generally be called otherwise, because the property
8981 * may be changed at any time by the layout.
8982 *
8983 * @param top The top of this view, in pixels.
8984 */
8985 public final void setTop(int top) {
8986 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07008987 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008988 final boolean matrixIsIdentity = mTransformationInfo == null
8989 || mTransformationInfo.mMatrixIsIdentity;
8990 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008991 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008992 int minTop;
8993 int yLoc;
8994 if (top < mTop) {
8995 minTop = top;
8996 yLoc = top - mTop;
8997 } else {
8998 minTop = mTop;
8999 yLoc = 0;
9000 }
Chet Haasee9140a72011-02-16 16:23:29 -08009001 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009002 }
9003 } else {
9004 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009005 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009006 }
9007
Chet Haaseed032702010-10-01 14:05:54 -07009008 int width = mRight - mLeft;
9009 int oldHeight = mBottom - mTop;
9010
Chet Haase21cd1382010-09-01 17:42:29 -07009011 mTop = top;
Chet Haase1271e2c2012-04-20 09:54:27 -07009012 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009013 mDisplayList.setTop(mTop);
9014 }
Chet Haase21cd1382010-09-01 17:42:29 -07009015
Chet Haaseed032702010-10-01 14:05:54 -07009016 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9017
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009018 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009019 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9020 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009021 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009022 }
Chet Haase21cd1382010-09-01 17:42:29 -07009023 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009024 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009025 }
Chet Haase55dbb652010-12-21 20:15:08 -08009026 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009027 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07009028 }
9029 }
9030
9031 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009032 * Bottom position of this view relative to its parent.
9033 *
9034 * @return The bottom of this view, in pixels.
9035 */
9036 @ViewDebug.CapturedViewProperty
9037 public final int getBottom() {
9038 return mBottom;
9039 }
9040
9041 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08009042 * True if this view has changed since the last time being drawn.
9043 *
9044 * @return The dirty state of this view.
9045 */
9046 public boolean isDirty() {
9047 return (mPrivateFlags & DIRTY_MASK) != 0;
9048 }
9049
9050 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009051 * Sets the bottom position of this view relative to its parent. This method is meant to be
9052 * called by the layout system and should not generally be called otherwise, because the
9053 * property may be changed at any time by the layout.
9054 *
9055 * @param bottom The bottom of this view, in pixels.
9056 */
9057 public final void setBottom(int bottom) {
9058 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07009059 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009060 final boolean matrixIsIdentity = mTransformationInfo == null
9061 || mTransformationInfo.mMatrixIsIdentity;
9062 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009063 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009064 int maxBottom;
9065 if (bottom < mBottom) {
9066 maxBottom = mBottom;
9067 } else {
9068 maxBottom = bottom;
9069 }
Chet Haasee9140a72011-02-16 16:23:29 -08009070 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009071 }
9072 } else {
9073 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009074 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009075 }
9076
Chet Haaseed032702010-10-01 14:05:54 -07009077 int width = mRight - mLeft;
9078 int oldHeight = mBottom - mTop;
9079
Chet Haase21cd1382010-09-01 17:42:29 -07009080 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -07009081 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009082 mDisplayList.setBottom(mBottom);
9083 }
Chet Haase21cd1382010-09-01 17:42:29 -07009084
Chet Haaseed032702010-10-01 14:05:54 -07009085 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9086
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009087 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009088 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9089 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009090 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009091 }
Chet Haase21cd1382010-09-01 17:42:29 -07009092 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009093 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009094 }
Chet Haase55dbb652010-12-21 20:15:08 -08009095 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009096 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07009097 }
9098 }
9099
9100 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009101 * Left position of this view relative to its parent.
9102 *
9103 * @return The left edge of this view, in pixels.
9104 */
9105 @ViewDebug.CapturedViewProperty
9106 public final int getLeft() {
9107 return mLeft;
9108 }
9109
9110 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009111 * Sets the left position of this view relative to its parent. This method is meant to be called
9112 * by the layout system and should not generally be called otherwise, because the property
9113 * may be changed at any time by the layout.
9114 *
9115 * @param left The bottom of this view, in pixels.
9116 */
9117 public final void setLeft(int left) {
9118 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07009119 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009120 final boolean matrixIsIdentity = mTransformationInfo == null
9121 || mTransformationInfo.mMatrixIsIdentity;
9122 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009123 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009124 int minLeft;
9125 int xLoc;
9126 if (left < mLeft) {
9127 minLeft = left;
9128 xLoc = left - mLeft;
9129 } else {
9130 minLeft = mLeft;
9131 xLoc = 0;
9132 }
Chet Haasee9140a72011-02-16 16:23:29 -08009133 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009134 }
9135 } else {
9136 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009137 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009138 }
9139
Chet Haaseed032702010-10-01 14:05:54 -07009140 int oldWidth = mRight - mLeft;
9141 int height = mBottom - mTop;
9142
Chet Haase21cd1382010-09-01 17:42:29 -07009143 mLeft = left;
Chet Haase1271e2c2012-04-20 09:54:27 -07009144 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009145 mDisplayList.setLeft(left);
9146 }
Chet Haase21cd1382010-09-01 17:42:29 -07009147
Chet Haaseed032702010-10-01 14:05:54 -07009148 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9149
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009150 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009151 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9152 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009153 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009154 }
Chet Haase21cd1382010-09-01 17:42:29 -07009155 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009156 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009157 }
Chet Haase55dbb652010-12-21 20:15:08 -08009158 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009159 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07009160 }
9161 }
9162
9163 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009164 * Right position of this view relative to its parent.
9165 *
9166 * @return The right edge of this view, in pixels.
9167 */
9168 @ViewDebug.CapturedViewProperty
9169 public final int getRight() {
9170 return mRight;
9171 }
9172
9173 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009174 * Sets the right position of this view relative to its parent. This method is meant to be called
9175 * by the layout system and should not generally be called otherwise, because the property
9176 * may be changed at any time by the layout.
9177 *
9178 * @param right The bottom of this view, in pixels.
9179 */
9180 public final void setRight(int right) {
9181 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07009182 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009183 final boolean matrixIsIdentity = mTransformationInfo == null
9184 || mTransformationInfo.mMatrixIsIdentity;
9185 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009186 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009187 int maxRight;
9188 if (right < mRight) {
9189 maxRight = mRight;
9190 } else {
9191 maxRight = right;
9192 }
Chet Haasee9140a72011-02-16 16:23:29 -08009193 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009194 }
9195 } else {
9196 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009197 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009198 }
9199
Chet Haaseed032702010-10-01 14:05:54 -07009200 int oldWidth = mRight - mLeft;
9201 int height = mBottom - mTop;
9202
Chet Haase21cd1382010-09-01 17:42:29 -07009203 mRight = right;
Chet Haase1271e2c2012-04-20 09:54:27 -07009204 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009205 mDisplayList.setRight(mRight);
9206 }
Chet Haase21cd1382010-09-01 17:42:29 -07009207
Chet Haaseed032702010-10-01 14:05:54 -07009208 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9209
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009210 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009211 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9212 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009213 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009214 }
Chet Haase21cd1382010-09-01 17:42:29 -07009215 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009216 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009217 }
Chet Haase55dbb652010-12-21 20:15:08 -08009218 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009219 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07009220 }
9221 }
9222
9223 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009224 * The visual x position of this view, in pixels. This is equivalent to the
9225 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08009226 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07009227 *
Chet Haasedf030d22010-07-30 17:22:38 -07009228 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009229 */
Chet Haasea5531132012-02-02 13:41:44 -08009230 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009231 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009232 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009233 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009234
Chet Haasedf030d22010-07-30 17:22:38 -07009235 /**
9236 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
9237 * {@link #setTranslationX(float) translationX} property to be the difference between
9238 * the x value passed in and the current {@link #getLeft() left} property.
9239 *
9240 * @param x The visual x position of this view, in pixels.
9241 */
9242 public void setX(float x) {
9243 setTranslationX(x - mLeft);
9244 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009245
Chet Haasedf030d22010-07-30 17:22:38 -07009246 /**
9247 * The visual y position of this view, in pixels. This is equivalent to the
9248 * {@link #setTranslationY(float) translationY} property plus the current
9249 * {@link #getTop() top} property.
9250 *
9251 * @return The visual y position of this view, in pixels.
9252 */
Chet Haasea5531132012-02-02 13:41:44 -08009253 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009254 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009255 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009256 }
9257
9258 /**
9259 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
9260 * {@link #setTranslationY(float) translationY} property to be the difference between
9261 * the y value passed in and the current {@link #getTop() top} property.
9262 *
9263 * @param y The visual y position of this view, in pixels.
9264 */
9265 public void setY(float y) {
9266 setTranslationY(y - mTop);
9267 }
9268
9269
9270 /**
9271 * The horizontal location of this view relative to its {@link #getLeft() left} position.
9272 * This position is post-layout, in addition to wherever the object's
9273 * layout placed it.
9274 *
9275 * @return The horizontal position of this view relative to its left position, in pixels.
9276 */
Chet Haasea5531132012-02-02 13:41:44 -08009277 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009278 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009279 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07009280 }
9281
9282 /**
9283 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
9284 * This effectively positions the object post-layout, in addition to wherever the object's
9285 * layout placed it.
9286 *
9287 * @param translationX The horizontal position of this view relative to its left position,
9288 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009289 *
9290 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07009291 */
9292 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009293 ensureTransformationInfo();
9294 final TransformationInfo info = mTransformationInfo;
9295 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07009296 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009297 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009298 info.mTranslationX = translationX;
9299 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009300 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009301 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009302 mDisplayList.setTranslationX(translationX);
9303 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009304 }
9305 }
9306
9307 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009308 * The horizontal location of this view relative to its {@link #getTop() top} position.
9309 * This position is post-layout, in addition to wherever the object's
9310 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009311 *
Chet Haasedf030d22010-07-30 17:22:38 -07009312 * @return The vertical position of this view relative to its top position,
9313 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009314 */
Chet Haasea5531132012-02-02 13:41:44 -08009315 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009316 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009317 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009318 }
9319
9320 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009321 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9322 * This effectively positions the object post-layout, in addition to wherever the object's
9323 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009324 *
Chet Haasedf030d22010-07-30 17:22:38 -07009325 * @param translationY The vertical position of this view relative to its top position,
9326 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009327 *
9328 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009329 */
Chet Haasedf030d22010-07-30 17:22:38 -07009330 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009331 ensureTransformationInfo();
9332 final TransformationInfo info = mTransformationInfo;
9333 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009334 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009335 info.mTranslationY = translationY;
9336 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009337 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009338 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009339 mDisplayList.setTranslationY(translationY);
9340 }
Chet Haasedf030d22010-07-30 17:22:38 -07009341 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009342 }
9343
9344 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009345 * Hit rectangle in parent's coordinates
9346 *
9347 * @param outRect The hit rectangle of the view.
9348 */
9349 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009350 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009351 final TransformationInfo info = mTransformationInfo;
9352 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009353 outRect.set(mLeft, mTop, mRight, mBottom);
9354 } else {
9355 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009356 tmpRect.set(-info.mPivotX, -info.mPivotY,
9357 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9358 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009359 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9360 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009361 }
9362 }
9363
9364 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009365 * Determines whether the given point, in local coordinates is inside the view.
9366 */
9367 /*package*/ final boolean pointInView(float localX, float localY) {
9368 return localX >= 0 && localX < (mRight - mLeft)
9369 && localY >= 0 && localY < (mBottom - mTop);
9370 }
9371
9372 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009373 * Utility method to determine whether the given point, in local coordinates,
9374 * is inside the view, where the area of the view is expanded by the slop factor.
9375 * This method is called while processing touch-move events to determine if the event
9376 * is still within the view.
9377 */
9378 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009379 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009380 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009381 }
9382
9383 /**
9384 * When a view has focus and the user navigates away from it, the next view is searched for
9385 * starting from the rectangle filled in by this method.
9386 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009387 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
9388 * of the view. However, if your view maintains some idea of internal selection,
9389 * such as a cursor, or a selected row or column, you should override this method and
9390 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009391 *
9392 * @param r The rectangle to fill in, in this view's coordinates.
9393 */
9394 public void getFocusedRect(Rect r) {
9395 getDrawingRect(r);
9396 }
9397
9398 /**
9399 * If some part of this view is not clipped by any of its parents, then
9400 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009401 * coordinates (without taking possible View rotations into account), offset
9402 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9403 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009404 *
9405 * @param r If true is returned, r holds the global coordinates of the
9406 * visible portion of this view.
9407 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9408 * between this view and its root. globalOffet may be null.
9409 * @return true if r is non-empty (i.e. part of the view is visible at the
9410 * root level.
9411 */
9412 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9413 int width = mRight - mLeft;
9414 int height = mBottom - mTop;
9415 if (width > 0 && height > 0) {
9416 r.set(0, 0, width, height);
9417 if (globalOffset != null) {
9418 globalOffset.set(-mScrollX, -mScrollY);
9419 }
9420 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9421 }
9422 return false;
9423 }
9424
9425 public final boolean getGlobalVisibleRect(Rect r) {
9426 return getGlobalVisibleRect(r, null);
9427 }
9428
9429 public final boolean getLocalVisibleRect(Rect r) {
Romain Guyab4c4f4f2012-05-06 13:11:24 -07009430 final Point offset = mAttachInfo != null ? mAttachInfo.mPoint : new Point();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009431 if (getGlobalVisibleRect(r, offset)) {
9432 r.offset(-offset.x, -offset.y); // make r local
9433 return true;
9434 }
9435 return false;
9436 }
9437
9438 /**
9439 * Offset this view's vertical location by the specified number of pixels.
9440 *
9441 * @param offset the number of pixels to offset the view by
9442 */
9443 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009444 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009445 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009446 final boolean matrixIsIdentity = mTransformationInfo == null
9447 || mTransformationInfo.mMatrixIsIdentity;
9448 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009449 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009450 invalidateViewProperty(false, false);
9451 } else {
9452 final ViewParent p = mParent;
9453 if (p != null && mAttachInfo != null) {
9454 final Rect r = mAttachInfo.mTmpInvalRect;
9455 int minTop;
9456 int maxBottom;
9457 int yLoc;
9458 if (offset < 0) {
9459 minTop = mTop + offset;
9460 maxBottom = mBottom;
9461 yLoc = offset;
9462 } else {
9463 minTop = mTop;
9464 maxBottom = mBottom + offset;
9465 yLoc = 0;
9466 }
9467 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
9468 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009469 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009470 }
9471 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009472 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009473 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009474
Chet Haasec3aa3612010-06-17 08:50:37 -07009475 mTop += offset;
9476 mBottom += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009477 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009478 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009479 invalidateViewProperty(false, false);
9480 } else {
9481 if (!matrixIsIdentity) {
9482 invalidateViewProperty(false, true);
9483 }
9484 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009485 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009486 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009487 }
9488
9489 /**
9490 * Offset this view's horizontal location by the specified amount of pixels.
9491 *
9492 * @param offset the numer of pixels to offset the view by
9493 */
9494 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009495 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009496 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009497 final boolean matrixIsIdentity = mTransformationInfo == null
9498 || mTransformationInfo.mMatrixIsIdentity;
9499 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009500 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009501 invalidateViewProperty(false, false);
9502 } else {
9503 final ViewParent p = mParent;
9504 if (p != null && mAttachInfo != null) {
9505 final Rect r = mAttachInfo.mTmpInvalRect;
9506 int minLeft;
9507 int maxRight;
9508 if (offset < 0) {
9509 minLeft = mLeft + offset;
9510 maxRight = mRight;
9511 } else {
9512 minLeft = mLeft;
9513 maxRight = mRight + offset;
9514 }
9515 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
9516 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009517 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009518 }
9519 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009520 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009521 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009522
Chet Haasec3aa3612010-06-17 08:50:37 -07009523 mLeft += offset;
9524 mRight += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009525 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009526 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009527 invalidateViewProperty(false, false);
9528 } else {
9529 if (!matrixIsIdentity) {
9530 invalidateViewProperty(false, true);
9531 }
9532 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009533 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009534 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009535 }
9536
9537 /**
9538 * Get the LayoutParams associated with this view. All views should have
9539 * layout parameters. These supply parameters to the <i>parent</i> of this
9540 * view specifying how it should be arranged. There are many subclasses of
9541 * ViewGroup.LayoutParams, and these correspond to the different subclasses
9542 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08009543 *
9544 * This method may return null if this View is not attached to a parent
9545 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
9546 * was not invoked successfully. When a View is attached to a parent
9547 * ViewGroup, this method must not return null.
9548 *
9549 * @return The LayoutParams associated with this view, or null if no
9550 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009551 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07009552 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009553 public ViewGroup.LayoutParams getLayoutParams() {
9554 return mLayoutParams;
9555 }
9556
9557 /**
9558 * Set the layout parameters associated with this view. These supply
9559 * parameters to the <i>parent</i> of this view specifying how it should be
9560 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
9561 * correspond to the different subclasses of ViewGroup that are responsible
9562 * for arranging their children.
9563 *
Romain Guy01c174b2011-02-22 11:51:06 -08009564 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009565 */
9566 public void setLayoutParams(ViewGroup.LayoutParams params) {
9567 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08009568 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009569 }
9570 mLayoutParams = params;
Philip Milned7dd8902012-01-26 16:55:30 -08009571 if (mParent instanceof ViewGroup) {
9572 ((ViewGroup) mParent).onSetLayoutParams(this, params);
9573 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009574 requestLayout();
9575 }
9576
9577 /**
9578 * Set the scrolled position of your view. This will cause a call to
9579 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9580 * invalidated.
9581 * @param x the x position to scroll to
9582 * @param y the y position to scroll to
9583 */
9584 public void scrollTo(int x, int y) {
9585 if (mScrollX != x || mScrollY != y) {
9586 int oldX = mScrollX;
9587 int oldY = mScrollY;
9588 mScrollX = x;
9589 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009590 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009591 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07009592 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009593 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -07009594 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009595 }
9596 }
9597
9598 /**
9599 * Move the scrolled position of your view. This will cause a call to
9600 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9601 * invalidated.
9602 * @param x the amount of pixels to scroll by horizontally
9603 * @param y the amount of pixels to scroll by vertically
9604 */
9605 public void scrollBy(int x, int y) {
9606 scrollTo(mScrollX + x, mScrollY + y);
9607 }
9608
9609 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009610 * <p>Trigger the scrollbars to draw. When invoked this method starts an
9611 * animation to fade the scrollbars out after a default delay. If a subclass
9612 * provides animated scrolling, the start delay should equal the duration
9613 * of the scrolling animation.</p>
9614 *
9615 * <p>The animation starts only if at least one of the scrollbars is
9616 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
9617 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9618 * this method returns true, and false otherwise. If the animation is
9619 * started, this method calls {@link #invalidate()}; in that case the
9620 * caller should not call {@link #invalidate()}.</p>
9621 *
9622 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07009623 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07009624 *
9625 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
9626 * and {@link #scrollTo(int, int)}.</p>
9627 *
9628 * @return true if the animation is played, false otherwise
9629 *
9630 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07009631 * @see #scrollBy(int, int)
9632 * @see #scrollTo(int, int)
9633 * @see #isHorizontalScrollBarEnabled()
9634 * @see #isVerticalScrollBarEnabled()
9635 * @see #setHorizontalScrollBarEnabled(boolean)
9636 * @see #setVerticalScrollBarEnabled(boolean)
9637 */
9638 protected boolean awakenScrollBars() {
9639 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07009640 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07009641 }
9642
9643 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07009644 * Trigger the scrollbars to draw.
9645 * This method differs from awakenScrollBars() only in its default duration.
9646 * initialAwakenScrollBars() will show the scroll bars for longer than
9647 * usual to give the user more of a chance to notice them.
9648 *
9649 * @return true if the animation is played, false otherwise.
9650 */
9651 private boolean initialAwakenScrollBars() {
9652 return mScrollCache != null &&
9653 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
9654 }
9655
9656 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009657 * <p>
9658 * Trigger the scrollbars to draw. When invoked this method starts an
9659 * animation to fade the scrollbars out after a fixed delay. If a subclass
9660 * provides animated scrolling, the start delay should equal the duration of
9661 * the scrolling animation.
9662 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009663 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009664 * <p>
9665 * The animation starts only if at least one of the scrollbars is enabled,
9666 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9667 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9668 * this method returns true, and false otherwise. If the animation is
9669 * started, this method calls {@link #invalidate()}; in that case the caller
9670 * should not call {@link #invalidate()}.
9671 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009672 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009673 * <p>
9674 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07009675 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07009676 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009677 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009678 * @param startDelay the delay, in milliseconds, after which the animation
9679 * should start; when the delay is 0, the animation starts
9680 * immediately
9681 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009682 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009683 * @see #scrollBy(int, int)
9684 * @see #scrollTo(int, int)
9685 * @see #isHorizontalScrollBarEnabled()
9686 * @see #isVerticalScrollBarEnabled()
9687 * @see #setHorizontalScrollBarEnabled(boolean)
9688 * @see #setVerticalScrollBarEnabled(boolean)
9689 */
9690 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07009691 return awakenScrollBars(startDelay, true);
9692 }
Joe Malin32736f02011-01-19 16:14:20 -08009693
Mike Cleron290947b2009-09-29 18:34:32 -07009694 /**
9695 * <p>
9696 * Trigger the scrollbars to draw. When invoked this method starts an
9697 * animation to fade the scrollbars out after a fixed delay. If a subclass
9698 * provides animated scrolling, the start delay should equal the duration of
9699 * the scrolling animation.
9700 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009701 *
Mike Cleron290947b2009-09-29 18:34:32 -07009702 * <p>
9703 * The animation starts only if at least one of the scrollbars is enabled,
9704 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9705 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9706 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08009707 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07009708 * is set to true; in that case the caller
9709 * should not call {@link #invalidate()}.
9710 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009711 *
Mike Cleron290947b2009-09-29 18:34:32 -07009712 * <p>
9713 * This method should be invoked everytime a subclass directly updates the
9714 * scroll parameters.
9715 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009716 *
Mike Cleron290947b2009-09-29 18:34:32 -07009717 * @param startDelay the delay, in milliseconds, after which the animation
9718 * should start; when the delay is 0, the animation starts
9719 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08009720 *
Mike Cleron290947b2009-09-29 18:34:32 -07009721 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08009722 *
Mike Cleron290947b2009-09-29 18:34:32 -07009723 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009724 *
Mike Cleron290947b2009-09-29 18:34:32 -07009725 * @see #scrollBy(int, int)
9726 * @see #scrollTo(int, int)
9727 * @see #isHorizontalScrollBarEnabled()
9728 * @see #isVerticalScrollBarEnabled()
9729 * @see #setHorizontalScrollBarEnabled(boolean)
9730 * @see #setVerticalScrollBarEnabled(boolean)
9731 */
9732 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07009733 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08009734
Mike Cleronf116bf82009-09-27 19:14:12 -07009735 if (scrollCache == null || !scrollCache.fadeScrollBars) {
9736 return false;
9737 }
9738
9739 if (scrollCache.scrollBar == null) {
9740 scrollCache.scrollBar = new ScrollBarDrawable();
9741 }
9742
9743 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
9744
Mike Cleron290947b2009-09-29 18:34:32 -07009745 if (invalidate) {
9746 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009747 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -07009748 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009749
9750 if (scrollCache.state == ScrollabilityCache.OFF) {
9751 // FIXME: this is copied from WindowManagerService.
9752 // We should get this value from the system when it
9753 // is possible to do so.
9754 final int KEY_REPEAT_FIRST_DELAY = 750;
9755 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
9756 }
9757
9758 // Tell mScrollCache when we should start fading. This may
9759 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07009760 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07009761 scrollCache.fadeStartTime = fadeStartTime;
9762 scrollCache.state = ScrollabilityCache.ON;
9763
9764 // Schedule our fader to run, unscheduling any old ones first
9765 if (mAttachInfo != null) {
9766 mAttachInfo.mHandler.removeCallbacks(scrollCache);
9767 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
9768 }
9769
9770 return true;
9771 }
9772
9773 return false;
9774 }
9775
9776 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07009777 * Do not invalidate views which are not visible and which are not running an animation. They
9778 * will not get drawn and they should not set dirty flags as if they will be drawn
9779 */
9780 private boolean skipInvalidate() {
9781 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
9782 (!(mParent instanceof ViewGroup) ||
9783 !((ViewGroup) mParent).isViewTransitioning(this));
9784 }
9785 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07009786 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009787 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
9788 * in the future. This must be called from a UI thread. To call from a non-UI
9789 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009790 *
9791 * WARNING: This method is destructive to dirty.
9792 * @param dirty the rectangle representing the bounds of the dirty region
9793 */
9794 public void invalidate(Rect dirty) {
9795 if (ViewDebug.TRACE_HIERARCHY) {
9796 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
9797 }
9798
Chet Haaseaceafe62011-08-26 15:44:33 -07009799 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009800 return;
9801 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009802 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08009803 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
9804 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009805 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08009806 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07009807 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009808 final ViewParent p = mParent;
9809 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08009810 //noinspection PointlessBooleanExpression,ConstantConditions
9811 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9812 if (p != null && ai != null && ai.mHardwareAccelerated) {
9813 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009814 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009815 p.invalidateChild(this, null);
9816 return;
9817 }
Romain Guyaf636eb2010-12-09 17:47:21 -08009818 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009819 if (p != null && ai != null) {
9820 final int scrollX = mScrollX;
9821 final int scrollY = mScrollY;
9822 final Rect r = ai.mTmpInvalRect;
9823 r.set(dirty.left - scrollX, dirty.top - scrollY,
9824 dirty.right - scrollX, dirty.bottom - scrollY);
9825 mParent.invalidateChild(this, r);
9826 }
9827 }
9828 }
9829
9830 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07009831 * 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 -08009832 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009833 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
9834 * will be called at some point in the future. This must be called from
9835 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009836 * @param l the left position of the dirty region
9837 * @param t the top position of the dirty region
9838 * @param r the right position of the dirty region
9839 * @param b the bottom position of the dirty region
9840 */
9841 public void invalidate(int l, int t, int r, int b) {
9842 if (ViewDebug.TRACE_HIERARCHY) {
9843 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
9844 }
9845
Chet Haaseaceafe62011-08-26 15:44:33 -07009846 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009847 return;
9848 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009849 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08009850 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
9851 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009852 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08009853 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07009854 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009855 final ViewParent p = mParent;
9856 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08009857 //noinspection PointlessBooleanExpression,ConstantConditions
9858 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9859 if (p != null && ai != null && ai.mHardwareAccelerated) {
9860 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009861 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009862 p.invalidateChild(this, null);
9863 return;
9864 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08009865 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009866 if (p != null && ai != null && l < r && t < b) {
9867 final int scrollX = mScrollX;
9868 final int scrollY = mScrollY;
9869 final Rect tmpr = ai.mTmpInvalRect;
9870 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
9871 p.invalidateChild(this, tmpr);
9872 }
9873 }
9874 }
9875
9876 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009877 * Invalidate the whole view. If the view is visible,
9878 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
9879 * the future. This must be called from a UI thread. To call from a non-UI thread,
9880 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009881 */
9882 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07009883 invalidate(true);
9884 }
Joe Malin32736f02011-01-19 16:14:20 -08009885
Chet Haaseed032702010-10-01 14:05:54 -07009886 /**
9887 * This is where the invalidate() work actually happens. A full invalidate()
9888 * causes the drawing cache to be invalidated, but this function can be called with
9889 * invalidateCache set to false to skip that invalidation step for cases that do not
9890 * need it (for example, a component that remains at the same dimensions with the same
9891 * content).
9892 *
9893 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
9894 * well. This is usually true for a full invalidate, but may be set to false if the
9895 * View's contents or dimensions have not changed.
9896 */
Romain Guy849d0a32011-02-01 17:20:48 -08009897 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009898 if (ViewDebug.TRACE_HIERARCHY) {
9899 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
9900 }
9901
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) ||
Romain Guyc5d55862011-01-21 19:01:46 -08009906 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08009907 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
9908 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07009909 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07009910 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -07009911 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08009912 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07009913 mPrivateFlags &= ~DRAWING_CACHE_VALID;
9914 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009915 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07009916 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08009917 //noinspection PointlessBooleanExpression,ConstantConditions
9918 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9919 if (p != null && ai != null && ai.mHardwareAccelerated) {
9920 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009921 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009922 p.invalidateChild(this, null);
9923 return;
9924 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08009925 }
Michael Jurkaebefea42010-11-15 16:04:01 -08009926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009927 if (p != null && ai != null) {
9928 final Rect r = ai.mTmpInvalRect;
9929 r.set(0, 0, mRight - mLeft, mBottom - mTop);
9930 // Don't call invalidate -- we don't want to internally scroll
9931 // our own bounds
9932 p.invalidateChild(this, r);
9933 }
9934 }
9935 }
9936
9937 /**
Chet Haase9d1992d2012-03-13 11:03:25 -07009938 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
9939 * set any flags or handle all of the cases handled by the default invalidation methods.
9940 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
9941 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
9942 * walk up the hierarchy, transforming the dirty rect as necessary.
9943 *
9944 * The method also handles normal invalidation logic if display list properties are not
9945 * being used in this view. The invalidateParent and forceRedraw flags are used by that
9946 * backup approach, to handle these cases used in the various property-setting methods.
9947 *
9948 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
9949 * are not being used in this view
9950 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
9951 * list properties are not being used in this view
9952 */
9953 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009954 if (mDisplayList == null || (mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009955 if (invalidateParent) {
9956 invalidateParentCaches();
9957 }
9958 if (forceRedraw) {
9959 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
9960 }
9961 invalidate(false);
9962 } else {
9963 final AttachInfo ai = mAttachInfo;
9964 final ViewParent p = mParent;
9965 if (p != null && ai != null) {
9966 final Rect r = ai.mTmpInvalRect;
9967 r.set(0, 0, mRight - mLeft, mBottom - mTop);
9968 if (mParent instanceof ViewGroup) {
9969 ((ViewGroup) mParent).invalidateChildFast(this, r);
9970 } else {
9971 mParent.invalidateChild(this, r);
9972 }
9973 }
9974 }
9975 }
9976
9977 /**
9978 * Utility method to transform a given Rect by the current matrix of this view.
9979 */
9980 void transformRect(final Rect rect) {
9981 if (!getMatrix().isIdentity()) {
9982 RectF boundingRect = mAttachInfo.mTmpTransformRect;
9983 boundingRect.set(rect);
9984 getMatrix().mapRect(boundingRect);
9985 rect.set((int) (boundingRect.left - 0.5f),
9986 (int) (boundingRect.top - 0.5f),
9987 (int) (boundingRect.right + 0.5f),
9988 (int) (boundingRect.bottom + 0.5f));
9989 }
9990 }
9991
9992 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08009993 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08009994 * is used to force the parent to rebuild its display list (when hardware-accelerated),
9995 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08009996 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
9997 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08009998 *
9999 * @hide
10000 */
Romain Guy0fd89bf2011-01-26 15:41:30 -080010001 protected void invalidateParentCaches() {
10002 if (mParent instanceof View) {
10003 ((View) mParent).mPrivateFlags |= INVALIDATED;
10004 }
10005 }
Joe Malin32736f02011-01-19 16:14:20 -080010006
Romain Guy0fd89bf2011-01-26 15:41:30 -080010007 /**
10008 * Used to indicate that the parent of this view should be invalidated. This functionality
10009 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10010 * which is necessary when various parent-managed properties of the view change, such as
10011 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
10012 * an invalidation event to the parent.
10013 *
10014 * @hide
10015 */
10016 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -080010017 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010018 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -080010019 }
10020 }
10021
10022 /**
Romain Guy24443ea2009-05-11 11:56:30 -070010023 * Indicates whether this View is opaque. An opaque View guarantees that it will
10024 * draw all the pixels overlapping its bounds using a fully opaque color.
10025 *
10026 * Subclasses of View should override this method whenever possible to indicate
10027 * whether an instance is opaque. Opaque Views are treated in a special way by
10028 * the View hierarchy, possibly allowing it to perform optimizations during
10029 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -070010030 *
Romain Guy24443ea2009-05-11 11:56:30 -070010031 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -070010032 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010033 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -070010034 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -070010035 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -070010036 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
10037 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -070010038 }
10039
Adam Powell20232d02010-12-08 21:08:53 -080010040 /**
10041 * @hide
10042 */
10043 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -070010044 // Opaque if:
10045 // - Has a background
10046 // - Background is opaque
10047 // - Doesn't have scrollbars or scrollbars are inside overlay
10048
Philip Milne6c8ea062012-04-03 17:38:43 -070010049 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Romain Guy8f1344f52009-05-15 16:03:59 -070010050 mPrivateFlags |= OPAQUE_BACKGROUND;
10051 } else {
10052 mPrivateFlags &= ~OPAQUE_BACKGROUND;
10053 }
10054
10055 final int flags = mViewFlags;
10056 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
10057 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
10058 mPrivateFlags |= OPAQUE_SCROLLBARS;
10059 } else {
10060 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
10061 }
10062 }
10063
10064 /**
10065 * @hide
10066 */
10067 protected boolean hasOpaqueScrollbars() {
10068 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -070010069 }
10070
10071 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010072 * @return A handler associated with the thread running the View. This
10073 * handler can be used to pump events in the UI events queue.
10074 */
10075 public Handler getHandler() {
10076 if (mAttachInfo != null) {
10077 return mAttachInfo.mHandler;
10078 }
10079 return null;
10080 }
10081
10082 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080010083 * Gets the view root associated with the View.
10084 * @return The view root, or null if none.
10085 * @hide
10086 */
10087 public ViewRootImpl getViewRootImpl() {
10088 if (mAttachInfo != null) {
10089 return mAttachInfo.mViewRootImpl;
10090 }
10091 return null;
10092 }
10093
10094 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010095 * <p>Causes the Runnable to be added to the message queue.
10096 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010097 *
Romain Guye63a4f32011-08-11 11:33:31 -070010098 * <p>This method can be invoked from outside of the UI thread
10099 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010100 *
10101 * @param action The Runnable that will be executed.
10102 *
10103 * @return Returns true if the Runnable was successfully placed in to the
10104 * message queue. Returns false on failure, usually because the
10105 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010106 *
10107 * @see #postDelayed
10108 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010109 */
10110 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010111 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010112 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010113 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010114 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010115 // Assume that post will succeed later
10116 ViewRootImpl.getRunQueue().post(action);
10117 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010118 }
10119
10120 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010121 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010122 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -070010123 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010124 *
Romain Guye63a4f32011-08-11 11:33:31 -070010125 * <p>This method can be invoked from outside of the UI thread
10126 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010127 *
10128 * @param action The Runnable that will be executed.
10129 * @param delayMillis The delay (in milliseconds) until the Runnable
10130 * will be executed.
10131 *
10132 * @return true if the Runnable was successfully placed in to the
10133 * message queue. Returns false on failure, usually because the
10134 * looper processing the message queue is exiting. Note that a
10135 * result of true does not mean the Runnable will be processed --
10136 * if the looper is quit before the delivery time of the message
10137 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010138 *
10139 * @see #post
10140 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010141 */
10142 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010143 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010144 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010145 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010146 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010147 // Assume that post will succeed later
10148 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10149 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010150 }
10151
10152 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010153 * <p>Causes the Runnable to execute on the next animation time step.
10154 * The runnable will be run on the user interface thread.</p>
10155 *
10156 * <p>This method can be invoked from outside of the UI thread
10157 * only when this View is attached to a window.</p>
10158 *
10159 * @param action The Runnable that will be executed.
10160 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010161 * @see #postOnAnimationDelayed
10162 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010163 */
10164 public void postOnAnimation(Runnable action) {
10165 final AttachInfo attachInfo = mAttachInfo;
10166 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010167 attachInfo.mViewRootImpl.mChoreographer.postCallback(
10168 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010169 } else {
10170 // Assume that post will succeed later
10171 ViewRootImpl.getRunQueue().post(action);
10172 }
10173 }
10174
10175 /**
10176 * <p>Causes the Runnable to execute on the next animation time step,
10177 * after the specified amount of time elapses.
10178 * The runnable will be run on the user interface thread.</p>
10179 *
10180 * <p>This method can be invoked from outside of the UI thread
10181 * only when this View is attached to a window.</p>
10182 *
10183 * @param action The Runnable that will be executed.
10184 * @param delayMillis The delay (in milliseconds) until the Runnable
10185 * will be executed.
10186 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010187 * @see #postOnAnimation
10188 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010189 */
10190 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
10191 final AttachInfo attachInfo = mAttachInfo;
10192 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010193 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
10194 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010195 } else {
10196 // Assume that post will succeed later
10197 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10198 }
10199 }
10200
10201 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010202 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010203 *
Romain Guye63a4f32011-08-11 11:33:31 -070010204 * <p>This method can be invoked from outside of the UI thread
10205 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010206 *
10207 * @param action The Runnable to remove from the message handling queue
10208 *
10209 * @return true if this view could ask the Handler to remove the Runnable,
10210 * false otherwise. When the returned value is true, the Runnable
10211 * may or may not have been actually removed from the message queue
10212 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010213 *
10214 * @see #post
10215 * @see #postDelayed
10216 * @see #postOnAnimation
10217 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010218 */
10219 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080010220 if (action != null) {
10221 final AttachInfo attachInfo = mAttachInfo;
10222 if (attachInfo != null) {
10223 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010224 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
10225 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -080010226 } else {
10227 // Assume that post will succeed later
10228 ViewRootImpl.getRunQueue().removeCallbacks(action);
10229 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010230 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010231 return true;
10232 }
10233
10234 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010235 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
10236 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010237 *
Romain Guye63a4f32011-08-11 11:33:31 -070010238 * <p>This method can be invoked from outside of the UI thread
10239 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010240 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010241 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010242 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010243 */
10244 public void postInvalidate() {
10245 postInvalidateDelayed(0);
10246 }
10247
10248 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010249 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10250 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010251 *
Romain Guye63a4f32011-08-11 11:33:31 -070010252 * <p>This method can be invoked from outside of the UI thread
10253 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010254 *
10255 * @param left The left coordinate of the rectangle to invalidate.
10256 * @param top The top coordinate of the rectangle to invalidate.
10257 * @param right The right coordinate of the rectangle to invalidate.
10258 * @param bottom The bottom coordinate of the rectangle to invalidate.
10259 *
10260 * @see #invalidate(int, int, int, int)
10261 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010262 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010263 */
10264 public void postInvalidate(int left, int top, int right, int bottom) {
10265 postInvalidateDelayed(0, left, top, right, bottom);
10266 }
10267
10268 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010269 * <p>Cause an invalidate to happen on a subsequent cycle through the event
10270 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010271 *
Romain Guye63a4f32011-08-11 11:33:31 -070010272 * <p>This method can be invoked from outside of the UI thread
10273 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010274 *
10275 * @param delayMilliseconds the duration in milliseconds to delay the
10276 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010277 *
10278 * @see #invalidate()
10279 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010280 */
10281 public void postInvalidateDelayed(long delayMilliseconds) {
10282 // We try only with the AttachInfo because there's no point in invalidating
10283 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010284 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010285 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010286 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010287 }
10288 }
10289
10290 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010291 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10292 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010293 *
Romain Guye63a4f32011-08-11 11:33:31 -070010294 * <p>This method can be invoked from outside of the UI thread
10295 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010296 *
10297 * @param delayMilliseconds the duration in milliseconds to delay the
10298 * invalidation by
10299 * @param left The left coordinate of the rectangle to invalidate.
10300 * @param top The top coordinate of the rectangle to invalidate.
10301 * @param right The right coordinate of the rectangle to invalidate.
10302 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010303 *
10304 * @see #invalidate(int, int, int, int)
10305 * @see #invalidate(Rect)
10306 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010307 */
10308 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
10309 int right, int bottom) {
10310
10311 // We try only with the AttachInfo because there's no point in invalidating
10312 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010313 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010314 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010315 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10316 info.target = this;
10317 info.left = left;
10318 info.top = top;
10319 info.right = right;
10320 info.bottom = bottom;
10321
Jeff Browna175a5b2012-02-15 19:18:31 -080010322 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010323 }
10324 }
10325
10326 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010327 * <p>Cause an invalidate to happen on the next animation time step, typically the
10328 * next display frame.</p>
10329 *
10330 * <p>This method can be invoked from outside of the UI thread
10331 * only when this View is attached to a window.</p>
10332 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010333 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010334 */
10335 public void postInvalidateOnAnimation() {
10336 // We try only with the AttachInfo because there's no point in invalidating
10337 // if we are not attached to our window
10338 final AttachInfo attachInfo = mAttachInfo;
10339 if (attachInfo != null) {
10340 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10341 }
10342 }
10343
10344 /**
10345 * <p>Cause an invalidate of the specified area to happen on the next animation
10346 * time step, typically the next display frame.</p>
10347 *
10348 * <p>This method can be invoked from outside of the UI thread
10349 * only when this View is attached to a window.</p>
10350 *
10351 * @param left The left coordinate of the rectangle to invalidate.
10352 * @param top The top coordinate of the rectangle to invalidate.
10353 * @param right The right coordinate of the rectangle to invalidate.
10354 * @param bottom The bottom coordinate of the rectangle to invalidate.
10355 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010356 * @see #invalidate(int, int, int, int)
10357 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010358 */
10359 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10360 // We try only with the AttachInfo because there's no point in invalidating
10361 // if we are not attached to our window
10362 final AttachInfo attachInfo = mAttachInfo;
10363 if (attachInfo != null) {
10364 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10365 info.target = this;
10366 info.left = left;
10367 info.top = top;
10368 info.right = right;
10369 info.bottom = bottom;
10370
10371 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10372 }
10373 }
10374
10375 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010376 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10377 * This event is sent at most once every
10378 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10379 */
10380 private void postSendViewScrolledAccessibilityEventCallback() {
10381 if (mSendViewScrolledAccessibilityEvent == null) {
10382 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10383 }
10384 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10385 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10386 postDelayed(mSendViewScrolledAccessibilityEvent,
10387 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10388 }
10389 }
10390
10391 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010392 * Called by a parent to request that a child update its values for mScrollX
10393 * and mScrollY if necessary. This will typically be done if the child is
10394 * animating a scroll using a {@link android.widget.Scroller Scroller}
10395 * object.
10396 */
10397 public void computeScroll() {
10398 }
10399
10400 /**
10401 * <p>Indicate whether the horizontal edges are faded when the view is
10402 * scrolled horizontally.</p>
10403 *
10404 * @return true if the horizontal edges should are faded on scroll, false
10405 * otherwise
10406 *
10407 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010408 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010409 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010410 */
10411 public boolean isHorizontalFadingEdgeEnabled() {
10412 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10413 }
10414
10415 /**
10416 * <p>Define whether the horizontal edges should be faded when this view
10417 * is scrolled horizontally.</p>
10418 *
10419 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10420 * be faded when the view is scrolled
10421 * horizontally
10422 *
10423 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010424 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010425 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010426 */
10427 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10428 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10429 if (horizontalFadingEdgeEnabled) {
10430 initScrollCache();
10431 }
10432
10433 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10434 }
10435 }
10436
10437 /**
10438 * <p>Indicate whether the vertical edges are faded when the view is
10439 * scrolled horizontally.</p>
10440 *
10441 * @return true if the vertical edges should are faded on scroll, false
10442 * otherwise
10443 *
10444 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010445 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010446 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010447 */
10448 public boolean isVerticalFadingEdgeEnabled() {
10449 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10450 }
10451
10452 /**
10453 * <p>Define whether the vertical edges should be faded when this view
10454 * is scrolled vertically.</p>
10455 *
10456 * @param verticalFadingEdgeEnabled true if the vertical edges should
10457 * be faded when the view is scrolled
10458 * vertically
10459 *
10460 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010461 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010462 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010463 */
10464 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10465 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10466 if (verticalFadingEdgeEnabled) {
10467 initScrollCache();
10468 }
10469
10470 mViewFlags ^= FADING_EDGE_VERTICAL;
10471 }
10472 }
10473
10474 /**
10475 * Returns the strength, or intensity, of the top faded edge. The strength is
10476 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10477 * returns 0.0 or 1.0 but no value in between.
10478 *
10479 * Subclasses should override this method to provide a smoother fade transition
10480 * when scrolling occurs.
10481 *
10482 * @return the intensity of the top fade as a float between 0.0f and 1.0f
10483 */
10484 protected float getTopFadingEdgeStrength() {
10485 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
10486 }
10487
10488 /**
10489 * Returns the strength, or intensity, of the bottom faded edge. The strength is
10490 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10491 * returns 0.0 or 1.0 but no value in between.
10492 *
10493 * Subclasses should override this method to provide a smoother fade transition
10494 * when scrolling occurs.
10495 *
10496 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
10497 */
10498 protected float getBottomFadingEdgeStrength() {
10499 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
10500 computeVerticalScrollRange() ? 1.0f : 0.0f;
10501 }
10502
10503 /**
10504 * Returns the strength, or intensity, of the left faded edge. The strength is
10505 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10506 * returns 0.0 or 1.0 but no value in between.
10507 *
10508 * Subclasses should override this method to provide a smoother fade transition
10509 * when scrolling occurs.
10510 *
10511 * @return the intensity of the left fade as a float between 0.0f and 1.0f
10512 */
10513 protected float getLeftFadingEdgeStrength() {
10514 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
10515 }
10516
10517 /**
10518 * Returns the strength, or intensity, of the right faded edge. The strength is
10519 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10520 * returns 0.0 or 1.0 but no value in between.
10521 *
10522 * Subclasses should override this method to provide a smoother fade transition
10523 * when scrolling occurs.
10524 *
10525 * @return the intensity of the right fade as a float between 0.0f and 1.0f
10526 */
10527 protected float getRightFadingEdgeStrength() {
10528 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
10529 computeHorizontalScrollRange() ? 1.0f : 0.0f;
10530 }
10531
10532 /**
10533 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
10534 * scrollbar is not drawn by default.</p>
10535 *
10536 * @return true if the horizontal scrollbar should be painted, false
10537 * otherwise
10538 *
10539 * @see #setHorizontalScrollBarEnabled(boolean)
10540 */
10541 public boolean isHorizontalScrollBarEnabled() {
10542 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10543 }
10544
10545 /**
10546 * <p>Define whether the horizontal scrollbar should be drawn or not. The
10547 * scrollbar is not drawn by default.</p>
10548 *
10549 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
10550 * be painted
10551 *
10552 * @see #isHorizontalScrollBarEnabled()
10553 */
10554 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
10555 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
10556 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010557 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010558 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010559 }
10560 }
10561
10562 /**
10563 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
10564 * scrollbar is not drawn by default.</p>
10565 *
10566 * @return true if the vertical scrollbar should be painted, false
10567 * otherwise
10568 *
10569 * @see #setVerticalScrollBarEnabled(boolean)
10570 */
10571 public boolean isVerticalScrollBarEnabled() {
10572 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
10573 }
10574
10575 /**
10576 * <p>Define whether the vertical scrollbar should be drawn or not. The
10577 * scrollbar is not drawn by default.</p>
10578 *
10579 * @param verticalScrollBarEnabled true if the vertical scrollbar should
10580 * be painted
10581 *
10582 * @see #isVerticalScrollBarEnabled()
10583 */
10584 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
10585 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
10586 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010587 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010588 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010589 }
10590 }
10591
Adam Powell20232d02010-12-08 21:08:53 -080010592 /**
10593 * @hide
10594 */
10595 protected void recomputePadding() {
10596 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010597 }
Joe Malin32736f02011-01-19 16:14:20 -080010598
Mike Cleronfe81d382009-09-28 14:22:16 -070010599 /**
10600 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080010601 *
Mike Cleronfe81d382009-09-28 14:22:16 -070010602 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080010603 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010604 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070010605 */
10606 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
10607 initScrollCache();
10608 final ScrollabilityCache scrollabilityCache = mScrollCache;
10609 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010610 if (fadeScrollbars) {
10611 scrollabilityCache.state = ScrollabilityCache.OFF;
10612 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070010613 scrollabilityCache.state = ScrollabilityCache.ON;
10614 }
10615 }
Joe Malin32736f02011-01-19 16:14:20 -080010616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010617 /**
Joe Malin32736f02011-01-19 16:14:20 -080010618 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010619 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080010620 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010621 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070010622 *
10623 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070010624 */
10625 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080010626 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010627 }
Joe Malin32736f02011-01-19 16:14:20 -080010628
Mike Cleron52f0a642009-09-28 18:21:37 -070010629 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070010630 *
10631 * Returns the delay before scrollbars fade.
10632 *
10633 * @return the delay before scrollbars fade
10634 *
10635 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10636 */
10637 public int getScrollBarDefaultDelayBeforeFade() {
10638 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
10639 mScrollCache.scrollBarDefaultDelayBeforeFade;
10640 }
10641
10642 /**
10643 * Define the delay before scrollbars fade.
10644 *
10645 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
10646 *
10647 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10648 */
10649 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
10650 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
10651 }
10652
10653 /**
10654 *
10655 * Returns the scrollbar fade duration.
10656 *
10657 * @return the scrollbar fade duration
10658 *
10659 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10660 */
10661 public int getScrollBarFadeDuration() {
10662 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
10663 mScrollCache.scrollBarFadeDuration;
10664 }
10665
10666 /**
10667 * Define the scrollbar fade duration.
10668 *
10669 * @param scrollBarFadeDuration - the scrollbar fade duration
10670 *
10671 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10672 */
10673 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
10674 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
10675 }
10676
10677 /**
10678 *
10679 * Returns the scrollbar size.
10680 *
10681 * @return the scrollbar size
10682 *
10683 * @attr ref android.R.styleable#View_scrollbarSize
10684 */
10685 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070010686 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070010687 mScrollCache.scrollBarSize;
10688 }
10689
10690 /**
10691 * Define the scrollbar size.
10692 *
10693 * @param scrollBarSize - the scrollbar size
10694 *
10695 * @attr ref android.R.styleable#View_scrollbarSize
10696 */
10697 public void setScrollBarSize(int scrollBarSize) {
10698 getScrollCache().scrollBarSize = scrollBarSize;
10699 }
10700
10701 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010702 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
10703 * inset. When inset, they add to the padding of the view. And the scrollbars
10704 * can be drawn inside the padding area or on the edge of the view. For example,
10705 * if a view has a background drawable and you want to draw the scrollbars
10706 * inside the padding specified by the drawable, you can use
10707 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
10708 * appear at the edge of the view, ignoring the padding, then you can use
10709 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
10710 * @param style the style of the scrollbars. Should be one of
10711 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
10712 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
10713 * @see #SCROLLBARS_INSIDE_OVERLAY
10714 * @see #SCROLLBARS_INSIDE_INSET
10715 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10716 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010717 *
10718 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010719 */
10720 public void setScrollBarStyle(int style) {
10721 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
10722 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070010723 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010724 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010725 }
10726 }
10727
10728 /**
10729 * <p>Returns the current scrollbar style.</p>
10730 * @return the current scrollbar style
10731 * @see #SCROLLBARS_INSIDE_OVERLAY
10732 * @see #SCROLLBARS_INSIDE_INSET
10733 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10734 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010735 *
10736 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010737 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070010738 @ViewDebug.ExportedProperty(mapping = {
10739 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
10740 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
10741 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
10742 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
10743 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010744 public int getScrollBarStyle() {
10745 return mViewFlags & SCROLLBARS_STYLE_MASK;
10746 }
10747
10748 /**
10749 * <p>Compute the horizontal range that the horizontal scrollbar
10750 * represents.</p>
10751 *
10752 * <p>The range is expressed in arbitrary units that must be the same as the
10753 * units used by {@link #computeHorizontalScrollExtent()} and
10754 * {@link #computeHorizontalScrollOffset()}.</p>
10755 *
10756 * <p>The default range is the drawing width of this view.</p>
10757 *
10758 * @return the total horizontal range represented by the horizontal
10759 * scrollbar
10760 *
10761 * @see #computeHorizontalScrollExtent()
10762 * @see #computeHorizontalScrollOffset()
10763 * @see android.widget.ScrollBarDrawable
10764 */
10765 protected int computeHorizontalScrollRange() {
10766 return getWidth();
10767 }
10768
10769 /**
10770 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
10771 * within the horizontal range. This value is used to compute the position
10772 * of the thumb within the scrollbar's track.</p>
10773 *
10774 * <p>The range is expressed in arbitrary units that must be the same as the
10775 * units used by {@link #computeHorizontalScrollRange()} and
10776 * {@link #computeHorizontalScrollExtent()}.</p>
10777 *
10778 * <p>The default offset is the scroll offset of this view.</p>
10779 *
10780 * @return the horizontal offset of the scrollbar's thumb
10781 *
10782 * @see #computeHorizontalScrollRange()
10783 * @see #computeHorizontalScrollExtent()
10784 * @see android.widget.ScrollBarDrawable
10785 */
10786 protected int computeHorizontalScrollOffset() {
10787 return mScrollX;
10788 }
10789
10790 /**
10791 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
10792 * within the horizontal range. This value is used to compute the length
10793 * of the thumb within the scrollbar's track.</p>
10794 *
10795 * <p>The range is expressed in arbitrary units that must be the same as the
10796 * units used by {@link #computeHorizontalScrollRange()} and
10797 * {@link #computeHorizontalScrollOffset()}.</p>
10798 *
10799 * <p>The default extent is the drawing width of this view.</p>
10800 *
10801 * @return the horizontal extent of the scrollbar's thumb
10802 *
10803 * @see #computeHorizontalScrollRange()
10804 * @see #computeHorizontalScrollOffset()
10805 * @see android.widget.ScrollBarDrawable
10806 */
10807 protected int computeHorizontalScrollExtent() {
10808 return getWidth();
10809 }
10810
10811 /**
10812 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
10813 *
10814 * <p>The range is expressed in arbitrary units that must be the same as the
10815 * units used by {@link #computeVerticalScrollExtent()} and
10816 * {@link #computeVerticalScrollOffset()}.</p>
10817 *
10818 * @return the total vertical range represented by the vertical scrollbar
10819 *
10820 * <p>The default range is the drawing height of this view.</p>
10821 *
10822 * @see #computeVerticalScrollExtent()
10823 * @see #computeVerticalScrollOffset()
10824 * @see android.widget.ScrollBarDrawable
10825 */
10826 protected int computeVerticalScrollRange() {
10827 return getHeight();
10828 }
10829
10830 /**
10831 * <p>Compute the vertical offset of the vertical scrollbar's thumb
10832 * within the horizontal range. This value is used to compute the position
10833 * of the thumb within the scrollbar's track.</p>
10834 *
10835 * <p>The range is expressed in arbitrary units that must be the same as the
10836 * units used by {@link #computeVerticalScrollRange()} and
10837 * {@link #computeVerticalScrollExtent()}.</p>
10838 *
10839 * <p>The default offset is the scroll offset of this view.</p>
10840 *
10841 * @return the vertical offset of the scrollbar's thumb
10842 *
10843 * @see #computeVerticalScrollRange()
10844 * @see #computeVerticalScrollExtent()
10845 * @see android.widget.ScrollBarDrawable
10846 */
10847 protected int computeVerticalScrollOffset() {
10848 return mScrollY;
10849 }
10850
10851 /**
10852 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
10853 * within the vertical range. This value is used to compute the length
10854 * of the thumb within the scrollbar's track.</p>
10855 *
10856 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080010857 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010858 * {@link #computeVerticalScrollOffset()}.</p>
10859 *
10860 * <p>The default extent is the drawing height of this view.</p>
10861 *
10862 * @return the vertical extent of the scrollbar's thumb
10863 *
10864 * @see #computeVerticalScrollRange()
10865 * @see #computeVerticalScrollOffset()
10866 * @see android.widget.ScrollBarDrawable
10867 */
10868 protected int computeVerticalScrollExtent() {
10869 return getHeight();
10870 }
10871
10872 /**
Adam Powell69159442011-06-13 17:53:06 -070010873 * Check if this view can be scrolled horizontally in a certain direction.
10874 *
10875 * @param direction Negative to check scrolling left, positive to check scrolling right.
10876 * @return true if this view can be scrolled in the specified direction, false otherwise.
10877 */
10878 public boolean canScrollHorizontally(int direction) {
10879 final int offset = computeHorizontalScrollOffset();
10880 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
10881 if (range == 0) return false;
10882 if (direction < 0) {
10883 return offset > 0;
10884 } else {
10885 return offset < range - 1;
10886 }
10887 }
10888
10889 /**
10890 * Check if this view can be scrolled vertically in a certain direction.
10891 *
10892 * @param direction Negative to check scrolling up, positive to check scrolling down.
10893 * @return true if this view can be scrolled in the specified direction, false otherwise.
10894 */
10895 public boolean canScrollVertically(int direction) {
10896 final int offset = computeVerticalScrollOffset();
10897 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
10898 if (range == 0) return false;
10899 if (direction < 0) {
10900 return offset > 0;
10901 } else {
10902 return offset < range - 1;
10903 }
10904 }
10905
10906 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010907 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
10908 * scrollbars are painted only if they have been awakened first.</p>
10909 *
10910 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080010911 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010912 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010913 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080010914 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010915 // scrollbars are drawn only when the animation is running
10916 final ScrollabilityCache cache = mScrollCache;
10917 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080010918
Mike Cleronf116bf82009-09-27 19:14:12 -070010919 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080010920
Mike Cleronf116bf82009-09-27 19:14:12 -070010921 if (state == ScrollabilityCache.OFF) {
10922 return;
10923 }
Joe Malin32736f02011-01-19 16:14:20 -080010924
Mike Cleronf116bf82009-09-27 19:14:12 -070010925 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080010926
Mike Cleronf116bf82009-09-27 19:14:12 -070010927 if (state == ScrollabilityCache.FADING) {
10928 // We're fading -- get our fade interpolation
10929 if (cache.interpolatorValues == null) {
10930 cache.interpolatorValues = new float[1];
10931 }
Joe Malin32736f02011-01-19 16:14:20 -080010932
Mike Cleronf116bf82009-09-27 19:14:12 -070010933 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080010934
Mike Cleronf116bf82009-09-27 19:14:12 -070010935 // Stops the animation if we're done
10936 if (cache.scrollBarInterpolator.timeToValues(values) ==
10937 Interpolator.Result.FREEZE_END) {
10938 cache.state = ScrollabilityCache.OFF;
10939 } else {
10940 cache.scrollBar.setAlpha(Math.round(values[0]));
10941 }
Joe Malin32736f02011-01-19 16:14:20 -080010942
10943 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070010944 // drawing. We only want this when we're fading so that
10945 // we prevent excessive redraws
10946 invalidate = true;
10947 } else {
10948 // We're just on -- but we may have been fading before so
10949 // reset alpha
10950 cache.scrollBar.setAlpha(255);
10951 }
10952
Joe Malin32736f02011-01-19 16:14:20 -080010953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010954 final int viewFlags = mViewFlags;
10955
10956 final boolean drawHorizontalScrollBar =
10957 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10958 final boolean drawVerticalScrollBar =
10959 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
10960 && !isVerticalScrollBarHidden();
10961
10962 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
10963 final int width = mRight - mLeft;
10964 final int height = mBottom - mTop;
10965
10966 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010967
Mike Reede8853fc2009-09-04 14:01:48 -040010968 final int scrollX = mScrollX;
10969 final int scrollY = mScrollY;
10970 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
10971
Mike Cleronf116bf82009-09-27 19:14:12 -070010972 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080010973
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010974 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080010975 int size = scrollBar.getSize(false);
10976 if (size <= 0) {
10977 size = cache.scrollBarSize;
10978 }
10979
Mike Cleronf116bf82009-09-27 19:14:12 -070010980 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040010981 computeHorizontalScrollOffset(),
10982 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040010983 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070010984 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080010985 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070010986 left = scrollX + (mPaddingLeft & inside);
10987 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
10988 bottom = top + size;
10989 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
10990 if (invalidate) {
10991 invalidate(left, top, right, bottom);
10992 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010993 }
10994
10995 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080010996 int size = scrollBar.getSize(true);
10997 if (size <= 0) {
10998 size = cache.scrollBarSize;
10999 }
11000
Mike Reede8853fc2009-09-04 14:01:48 -040011001 scrollBar.setParameters(computeVerticalScrollRange(),
11002 computeVerticalScrollOffset(),
11003 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -080011004 switch (mVerticalScrollbarPosition) {
11005 default:
11006 case SCROLLBAR_POSITION_DEFAULT:
11007 case SCROLLBAR_POSITION_RIGHT:
11008 left = scrollX + width - size - (mUserPaddingRight & inside);
11009 break;
11010 case SCROLLBAR_POSITION_LEFT:
11011 left = scrollX + (mUserPaddingLeft & inside);
11012 break;
11013 }
Mike Cleronf116bf82009-09-27 19:14:12 -070011014 top = scrollY + (mPaddingTop & inside);
11015 right = left + size;
11016 bottom = scrollY + height - (mUserPaddingBottom & inside);
11017 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
11018 if (invalidate) {
11019 invalidate(left, top, right, bottom);
11020 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011021 }
11022 }
11023 }
11024 }
Romain Guy8506ab42009-06-11 17:35:47 -070011025
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011026 /**
Romain Guy8506ab42009-06-11 17:35:47 -070011027 * 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 -080011028 * FastScroller is visible.
11029 * @return whether to temporarily hide the vertical scrollbar
11030 * @hide
11031 */
11032 protected boolean isVerticalScrollBarHidden() {
11033 return false;
11034 }
11035
11036 /**
11037 * <p>Draw the horizontal scrollbar if
11038 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
11039 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011040 * @param canvas the canvas on which to draw the scrollbar
11041 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011042 *
11043 * @see #isHorizontalScrollBarEnabled()
11044 * @see #computeHorizontalScrollRange()
11045 * @see #computeHorizontalScrollExtent()
11046 * @see #computeHorizontalScrollOffset()
11047 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070011048 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011049 */
Romain Guy8fb95422010-08-17 18:38:51 -070011050 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
11051 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011052 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011053 scrollBar.draw(canvas);
11054 }
Mike Reede8853fc2009-09-04 14:01:48 -040011055
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011056 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011057 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
11058 * returns true.</p>
11059 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011060 * @param canvas the canvas on which to draw the scrollbar
11061 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011062 *
11063 * @see #isVerticalScrollBarEnabled()
11064 * @see #computeVerticalScrollRange()
11065 * @see #computeVerticalScrollExtent()
11066 * @see #computeVerticalScrollOffset()
11067 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040011068 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011069 */
Romain Guy8fb95422010-08-17 18:38:51 -070011070 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
11071 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040011072 scrollBar.setBounds(l, t, r, b);
11073 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011074 }
11075
11076 /**
11077 * Implement this to do your drawing.
11078 *
11079 * @param canvas the canvas on which the background will be drawn
11080 */
11081 protected void onDraw(Canvas canvas) {
11082 }
11083
11084 /*
11085 * Caller is responsible for calling requestLayout if necessary.
11086 * (This allows addViewInLayout to not request a new layout.)
11087 */
11088 void assignParent(ViewParent parent) {
11089 if (mParent == null) {
11090 mParent = parent;
11091 } else if (parent == null) {
11092 mParent = null;
11093 } else {
11094 throw new RuntimeException("view " + this + " being added, but"
11095 + " it already has a parent");
11096 }
11097 }
11098
11099 /**
11100 * This is called when the view is attached to a window. At this point it
11101 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011102 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
11103 * however it may be called any time before the first onDraw -- including
11104 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011105 *
11106 * @see #onDetachedFromWindow()
11107 */
11108 protected void onAttachedToWindow() {
11109 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
11110 mParent.requestTransparentRegion(this);
11111 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011112
Adam Powell8568c3a2010-04-19 14:26:11 -070011113 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
11114 initialAwakenScrollBars();
11115 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
11116 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011117
Chet Haasea9b61ac2010-12-20 07:40:25 -080011118 jumpDrawablesToCurrentState();
Romain Guy2a0f2282012-05-08 14:43:12 -070011119
Fabrice Di Meglioa6461452011-08-19 15:42:04 -070011120 // Order is important here: LayoutDirection MUST be resolved before Padding
11121 // and TextDirection
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011122 resolveLayoutDirection();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011123 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011124 resolveTextDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011125 resolveTextAlignment();
Romain Guy2a0f2282012-05-08 14:43:12 -070011126
Svetoslav Ganov42138042012-03-20 11:51:39 -070011127 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070011128 if (isFocused()) {
11129 InputMethodManager imm = InputMethodManager.peekInstance();
11130 imm.focusIn(this);
11131 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011132
11133 if (mAttachInfo != null && mDisplayList != null) {
11134 mAttachInfo.mViewRootImpl.dequeueDisplayList(mDisplayList);
11135 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011136 }
Cibu Johny86666632010-02-22 13:01:02 -080011137
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011138 /**
Romain Guybb9908b2012-03-08 11:14:07 -080011139 * @see #onScreenStateChanged(int)
11140 */
11141 void dispatchScreenStateChanged(int screenState) {
11142 onScreenStateChanged(screenState);
11143 }
11144
11145 /**
11146 * This method is called whenever the state of the screen this view is
11147 * attached to changes. A state change will usually occurs when the screen
11148 * turns on or off (whether it happens automatically or the user does it
11149 * manually.)
11150 *
11151 * @param screenState The new state of the screen. Can be either
11152 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
11153 */
11154 public void onScreenStateChanged(int screenState) {
11155 }
11156
11157 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011158 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
11159 */
11160 private boolean hasRtlSupport() {
11161 return mContext.getApplicationInfo().hasRtlSupport();
11162 }
11163
11164 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011165 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
11166 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011167 * Will call {@link View#onResolvedLayoutDirectionChanged} when resolution is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011168 * @hide
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011169 */
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011170 public void resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011171 // Clear any previous layout direction resolution
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070011172 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011173
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011174 if (hasRtlSupport()) {
11175 // Set resolved depending on layout direction
11176 switch (getLayoutDirection()) {
11177 case LAYOUT_DIRECTION_INHERIT:
11178 // If this is root view, no need to look at parent's layout dir.
11179 if (canResolveLayoutDirection()) {
11180 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011181
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011182 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
11183 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11184 }
11185 } else {
11186 // Nothing to do, LTR by default
11187 }
11188 break;
11189 case LAYOUT_DIRECTION_RTL:
11190 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11191 break;
11192 case LAYOUT_DIRECTION_LOCALE:
11193 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011194 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11195 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011196 break;
11197 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011198 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011199 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011200 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011201
11202 // Set to resolved
11203 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011204 onResolvedLayoutDirectionChanged();
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080011205 // Resolve padding
11206 resolvePadding();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011207 }
11208
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011209 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011210 * Called when layout direction has been resolved.
11211 *
11212 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011213 * @hide
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011214 */
11215 public void onResolvedLayoutDirectionChanged() {
11216 }
11217
11218 /**
11219 * Resolve padding depending on layout direction.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011220 * @hide
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011221 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011222 public void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011223 // If the user specified the absolute padding (either with android:padding or
11224 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
11225 // use the default padding or the padding from the background drawable
11226 // (stored at this point in mPadding*)
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011227 int resolvedLayoutDirection = getResolvedLayoutDirection();
11228 switch (resolvedLayoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011229 case LAYOUT_DIRECTION_RTL:
11230 // Start user padding override Right user padding. Otherwise, if Right user
11231 // padding is not defined, use the default Right padding. If Right user padding
11232 // is defined, just use it.
11233 if (mUserPaddingStart >= 0) {
11234 mUserPaddingRight = mUserPaddingStart;
11235 } else if (mUserPaddingRight < 0) {
11236 mUserPaddingRight = mPaddingRight;
11237 }
11238 if (mUserPaddingEnd >= 0) {
11239 mUserPaddingLeft = mUserPaddingEnd;
11240 } else if (mUserPaddingLeft < 0) {
11241 mUserPaddingLeft = mPaddingLeft;
11242 }
11243 break;
11244 case LAYOUT_DIRECTION_LTR:
11245 default:
11246 // Start user padding override Left user padding. Otherwise, if Left user
11247 // padding is not defined, use the default left padding. If Left user padding
11248 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070011249 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011250 mUserPaddingLeft = mUserPaddingStart;
11251 } else if (mUserPaddingLeft < 0) {
11252 mUserPaddingLeft = mPaddingLeft;
11253 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070011254 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011255 mUserPaddingRight = mUserPaddingEnd;
11256 } else if (mUserPaddingRight < 0) {
11257 mUserPaddingRight = mPaddingRight;
11258 }
11259 }
11260
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011261 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
11262
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080011263 if(isPaddingRelative()) {
11264 setPaddingRelative(mUserPaddingStart, mPaddingTop, mUserPaddingEnd, mUserPaddingBottom);
11265 } else {
11266 recomputePadding();
11267 }
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011268 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011269 }
11270
11271 /**
11272 * Resolve padding depending on the layout direction. Subclasses that care about
11273 * padding resolution should override this method. The default implementation does
11274 * nothing.
11275 *
11276 * @param layoutDirection the direction of the layout
11277 *
Fabrice Di Meglioe8dc07d2012-03-09 17:10:19 -080011278 * @see {@link #LAYOUT_DIRECTION_LTR}
11279 * @see {@link #LAYOUT_DIRECTION_RTL}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011280 * @hide
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011281 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011282 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011283 }
11284
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011285 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011286 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011287 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011288 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011289 * @hide
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011290 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011291 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011292 switch (getLayoutDirection()) {
11293 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011294 return (mParent != null) && (mParent instanceof ViewGroup);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011295 default:
11296 return true;
11297 }
11298 }
11299
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011300 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011301 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
11302 * when reset is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011303 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011304 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011305 public void resetResolvedLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070011306 // Reset the current resolved bits
11307 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011308 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080011309 // Reset also the text direction
11310 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011311 }
11312
11313 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011314 * Called during reset of resolved layout direction.
11315 *
11316 * Subclasses need to override this method to clear cached information that depends on the
11317 * resolved layout direction, or to inform child views that inherit their layout direction.
11318 *
11319 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011320 * @hide
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011321 */
11322 public void onResolvedLayoutDirectionReset() {
11323 }
11324
11325 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011326 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011327 *
11328 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011329 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011330 * @hide
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011331 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011332 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -080011333 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011334 }
11335
11336 /**
11337 * This is called when the view is detached from a window. At this point it
11338 * no longer has a surface for drawing.
11339 *
11340 * @see #onAttachedToWindow()
11341 */
11342 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -080011343 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080011344
Romain Guya440b002010-02-24 15:57:54 -080011345 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050011346 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080011347 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070011348 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080011349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011350 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011351
Romain Guya998dff2012-03-23 18:58:36 -070011352 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011353
11354 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011355 if (mDisplayList != null) {
Romain Guy2a0f2282012-05-08 14:43:12 -070011356 mAttachInfo.mViewRootImpl.enqueueDisplayList(mDisplayList);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011357 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011358 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011359 } else {
11360 if (mDisplayList != null) {
11361 // Should never happen
11362 mDisplayList.invalidate();
11363 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011364 }
11365
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011366 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011367
11368 resetResolvedLayoutDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011369 resetResolvedTextAlignment();
Svetoslav Ganov42138042012-03-20 11:51:39 -070011370 resetAccessibilityStateChanged();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011371 }
11372
11373 /**
11374 * @return The number of times this view has been attached to a window
11375 */
11376 protected int getWindowAttachCount() {
11377 return mWindowAttachCount;
11378 }
11379
11380 /**
11381 * Retrieve a unique token identifying the window this view is attached to.
11382 * @return Return the window's token for use in
11383 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11384 */
11385 public IBinder getWindowToken() {
11386 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11387 }
11388
11389 /**
11390 * Retrieve a unique token identifying the top-level "real" window of
11391 * the window that this view is attached to. That is, this is like
11392 * {@link #getWindowToken}, except if the window this view in is a panel
11393 * window (attached to another containing window), then the token of
11394 * the containing window is returned instead.
11395 *
11396 * @return Returns the associated window token, either
11397 * {@link #getWindowToken()} or the containing window's token.
11398 */
11399 public IBinder getApplicationWindowToken() {
11400 AttachInfo ai = mAttachInfo;
11401 if (ai != null) {
11402 IBinder appWindowToken = ai.mPanelParentWindowToken;
11403 if (appWindowToken == null) {
11404 appWindowToken = ai.mWindowToken;
11405 }
11406 return appWindowToken;
11407 }
11408 return null;
11409 }
11410
11411 /**
11412 * Retrieve private session object this view hierarchy is using to
11413 * communicate with the window manager.
11414 * @return the session object to communicate with the window manager
11415 */
11416 /*package*/ IWindowSession getWindowSession() {
11417 return mAttachInfo != null ? mAttachInfo.mSession : null;
11418 }
11419
11420 /**
11421 * @param info the {@link android.view.View.AttachInfo} to associated with
11422 * this view
11423 */
11424 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
11425 //System.out.println("Attached! " + this);
11426 mAttachInfo = info;
11427 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011428 // We will need to evaluate the drawable state at least once.
11429 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011430 if (mFloatingTreeObserver != null) {
11431 info.mTreeObserver.merge(mFloatingTreeObserver);
11432 mFloatingTreeObserver = null;
11433 }
11434 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
11435 mAttachInfo.mScrollContainers.add(this);
11436 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
11437 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070011438 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011439 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080011440
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011441 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011442 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011443 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011444 if (listeners != null && listeners.size() > 0) {
11445 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11446 // perform the dispatching. The iterator is a safe guard against listeners that
11447 // could mutate the list by calling the various add/remove methods. This prevents
11448 // the array from being modified while we iterate it.
11449 for (OnAttachStateChangeListener listener : listeners) {
11450 listener.onViewAttachedToWindow(this);
11451 }
11452 }
11453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011454 int vis = info.mWindowVisibility;
11455 if (vis != GONE) {
11456 onWindowVisibilityChanged(vis);
11457 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011458 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
11459 // If nobody has evaluated the drawable state yet, then do it now.
11460 refreshDrawableState();
11461 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011462 }
11463
11464 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011465 AttachInfo info = mAttachInfo;
11466 if (info != null) {
11467 int vis = info.mWindowVisibility;
11468 if (vis != GONE) {
11469 onWindowVisibilityChanged(GONE);
11470 }
11471 }
11472
11473 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080011474
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011475 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011476 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011477 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011478 if (listeners != null && listeners.size() > 0) {
11479 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11480 // perform the dispatching. The iterator is a safe guard against listeners that
11481 // could mutate the list by calling the various add/remove methods. This prevents
11482 // the array from being modified while we iterate it.
11483 for (OnAttachStateChangeListener listener : listeners) {
11484 listener.onViewDetachedFromWindow(this);
11485 }
11486 }
11487
Romain Guy01d5edc2011-01-28 11:28:53 -080011488 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011489 mAttachInfo.mScrollContainers.remove(this);
11490 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
11491 }
Romain Guy01d5edc2011-01-28 11:28:53 -080011492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011493 mAttachInfo = null;
11494 }
11495
11496 /**
11497 * Store this view hierarchy's frozen state into the given container.
11498 *
11499 * @param container The SparseArray in which to save the view's state.
11500 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011501 * @see #restoreHierarchyState(android.util.SparseArray)
11502 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11503 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011504 */
11505 public void saveHierarchyState(SparseArray<Parcelable> container) {
11506 dispatchSaveInstanceState(container);
11507 }
11508
11509 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011510 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
11511 * this view and its children. May be overridden to modify how freezing happens to a
11512 * 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 -080011513 *
11514 * @param container The SparseArray in which to save the view's state.
11515 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011516 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11517 * @see #saveHierarchyState(android.util.SparseArray)
11518 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011519 */
11520 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
11521 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
11522 mPrivateFlags &= ~SAVE_STATE_CALLED;
11523 Parcelable state = onSaveInstanceState();
11524 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11525 throw new IllegalStateException(
11526 "Derived class did not call super.onSaveInstanceState()");
11527 }
11528 if (state != null) {
11529 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
11530 // + ": " + state);
11531 container.put(mID, state);
11532 }
11533 }
11534 }
11535
11536 /**
11537 * Hook allowing a view to generate a representation of its internal state
11538 * that can later be used to create a new instance with that same state.
11539 * This state should only contain information that is not persistent or can
11540 * not be reconstructed later. For example, you will never store your
11541 * current position on screen because that will be computed again when a
11542 * new instance of the view is placed in its view hierarchy.
11543 * <p>
11544 * Some examples of things you may store here: the current cursor position
11545 * in a text view (but usually not the text itself since that is stored in a
11546 * content provider or other persistent storage), the currently selected
11547 * item in a list view.
11548 *
11549 * @return Returns a Parcelable object containing the view's current dynamic
11550 * state, or null if there is nothing interesting to save. The
11551 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070011552 * @see #onRestoreInstanceState(android.os.Parcelable)
11553 * @see #saveHierarchyState(android.util.SparseArray)
11554 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011555 * @see #setSaveEnabled(boolean)
11556 */
11557 protected Parcelable onSaveInstanceState() {
11558 mPrivateFlags |= SAVE_STATE_CALLED;
11559 return BaseSavedState.EMPTY_STATE;
11560 }
11561
11562 /**
11563 * Restore this view hierarchy's frozen state from the given container.
11564 *
11565 * @param container The SparseArray which holds previously frozen states.
11566 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011567 * @see #saveHierarchyState(android.util.SparseArray)
11568 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11569 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011570 */
11571 public void restoreHierarchyState(SparseArray<Parcelable> container) {
11572 dispatchRestoreInstanceState(container);
11573 }
11574
11575 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011576 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
11577 * state for this view and its children. May be overridden to modify how restoring
11578 * happens to a view's children; for example, some views may want to not store state
11579 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011580 *
11581 * @param container The SparseArray which holds previously saved state.
11582 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011583 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11584 * @see #restoreHierarchyState(android.util.SparseArray)
11585 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011586 */
11587 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
11588 if (mID != NO_ID) {
11589 Parcelable state = container.get(mID);
11590 if (state != null) {
11591 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
11592 // + ": " + state);
11593 mPrivateFlags &= ~SAVE_STATE_CALLED;
11594 onRestoreInstanceState(state);
11595 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11596 throw new IllegalStateException(
11597 "Derived class did not call super.onRestoreInstanceState()");
11598 }
11599 }
11600 }
11601 }
11602
11603 /**
11604 * Hook allowing a view to re-apply a representation of its internal state that had previously
11605 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
11606 * null state.
11607 *
11608 * @param state The frozen state that had previously been returned by
11609 * {@link #onSaveInstanceState}.
11610 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011611 * @see #onSaveInstanceState()
11612 * @see #restoreHierarchyState(android.util.SparseArray)
11613 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011614 */
11615 protected void onRestoreInstanceState(Parcelable state) {
11616 mPrivateFlags |= SAVE_STATE_CALLED;
11617 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080011618 throw new IllegalArgumentException("Wrong state class, expecting View State but "
11619 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080011620 + "when two views of different type have the same id in the same hierarchy. "
11621 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080011622 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011623 }
11624 }
11625
11626 /**
11627 * <p>Return the time at which the drawing of the view hierarchy started.</p>
11628 *
11629 * @return the drawing start time in milliseconds
11630 */
11631 public long getDrawingTime() {
11632 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
11633 }
11634
11635 /**
11636 * <p>Enables or disables the duplication of the parent's state into this view. When
11637 * duplication is enabled, this view gets its drawable state from its parent rather
11638 * than from its own internal properties.</p>
11639 *
11640 * <p>Note: in the current implementation, setting this property to true after the
11641 * view was added to a ViewGroup might have no effect at all. This property should
11642 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
11643 *
11644 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
11645 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011646 *
Gilles Debunnefb817032011-01-13 13:52:49 -080011647 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
11648 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011649 *
11650 * @param enabled True to enable duplication of the parent's drawable state, false
11651 * to disable it.
11652 *
11653 * @see #getDrawableState()
11654 * @see #isDuplicateParentStateEnabled()
11655 */
11656 public void setDuplicateParentStateEnabled(boolean enabled) {
11657 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
11658 }
11659
11660 /**
11661 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
11662 *
11663 * @return True if this view's drawable state is duplicated from the parent,
11664 * false otherwise
11665 *
11666 * @see #getDrawableState()
11667 * @see #setDuplicateParentStateEnabled(boolean)
11668 */
11669 public boolean isDuplicateParentStateEnabled() {
11670 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
11671 }
11672
11673 /**
Romain Guy171c5922011-01-06 10:04:23 -080011674 * <p>Specifies the type of layer backing this view. The layer can be
11675 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
11676 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011677 *
Romain Guy171c5922011-01-06 10:04:23 -080011678 * <p>A layer is associated with an optional {@link android.graphics.Paint}
11679 * instance that controls how the layer is composed on screen. The following
11680 * properties of the paint are taken into account when composing the layer:</p>
11681 * <ul>
11682 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
11683 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
11684 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
11685 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080011686 *
Romain Guy171c5922011-01-06 10:04:23 -080011687 * <p>If this view has an alpha value set to < 1.0 by calling
11688 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
11689 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
11690 * equivalent to setting a hardware layer on this view and providing a paint with
11691 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080011692 *
Romain Guy171c5922011-01-06 10:04:23 -080011693 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
11694 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
11695 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011696 *
Romain Guy171c5922011-01-06 10:04:23 -080011697 * @param layerType The ype of layer to use with this view, must be one of
11698 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11699 * {@link #LAYER_TYPE_HARDWARE}
11700 * @param paint The paint used to compose the layer. This argument is optional
11701 * and can be null. It is ignored when the layer type is
11702 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080011703 *
11704 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080011705 * @see #LAYER_TYPE_NONE
11706 * @see #LAYER_TYPE_SOFTWARE
11707 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080011708 * @see #setAlpha(float)
11709 *
Romain Guy171c5922011-01-06 10:04:23 -080011710 * @attr ref android.R.styleable#View_layerType
11711 */
11712 public void setLayerType(int layerType, Paint paint) {
11713 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080011714 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080011715 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
11716 }
Chet Haasedaf98e92011-01-10 14:10:36 -080011717
Romain Guyd6cd5722011-01-17 14:42:41 -080011718 if (layerType == mLayerType) {
11719 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
11720 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011721 invalidateParentCaches();
11722 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080011723 }
11724 return;
11725 }
Romain Guy171c5922011-01-06 10:04:23 -080011726
11727 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080011728 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070011729 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070011730 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011731 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080011732 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070011733 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011734 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080011735 default:
11736 break;
Romain Guy171c5922011-01-06 10:04:23 -080011737 }
11738
11739 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080011740 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
11741 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
11742 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080011743
Romain Guy0fd89bf2011-01-26 15:41:30 -080011744 invalidateParentCaches();
11745 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080011746 }
11747
11748 /**
Romain Guy59c7f802011-09-29 17:21:45 -070011749 * Indicates whether this view has a static layer. A view with layer type
11750 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
11751 * dynamic.
11752 */
11753 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080011754 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070011755 }
11756
11757 /**
Romain Guy171c5922011-01-06 10:04:23 -080011758 * Indicates what type of layer is currently associated with this view. By default
11759 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
11760 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
11761 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080011762 *
Romain Guy171c5922011-01-06 10:04:23 -080011763 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11764 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080011765 *
11766 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070011767 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080011768 * @see #LAYER_TYPE_NONE
11769 * @see #LAYER_TYPE_SOFTWARE
11770 * @see #LAYER_TYPE_HARDWARE
11771 */
11772 public int getLayerType() {
11773 return mLayerType;
11774 }
Joe Malin32736f02011-01-19 16:14:20 -080011775
Romain Guy6c319ca2011-01-11 14:29:25 -080011776 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080011777 * Forces this view's layer to be created and this view to be rendered
11778 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
11779 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070011780 *
Romain Guyf1ae1062011-03-02 18:16:04 -080011781 * This method can for instance be used to render a view into its layer before
11782 * starting an animation. If this view is complex, rendering into the layer
11783 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070011784 *
Romain Guyf1ae1062011-03-02 18:16:04 -080011785 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070011786 *
11787 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080011788 */
11789 public void buildLayer() {
11790 if (mLayerType == LAYER_TYPE_NONE) return;
11791
11792 if (mAttachInfo == null) {
11793 throw new IllegalStateException("This view must be attached to a window first");
11794 }
11795
11796 switch (mLayerType) {
11797 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080011798 if (mAttachInfo.mHardwareRenderer != null &&
11799 mAttachInfo.mHardwareRenderer.isEnabled() &&
11800 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080011801 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080011802 }
Romain Guyf1ae1062011-03-02 18:16:04 -080011803 break;
11804 case LAYER_TYPE_SOFTWARE:
11805 buildDrawingCache(true);
11806 break;
11807 }
11808 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011809
Romain Guy9c4b79a2011-11-10 19:23:58 -080011810 // Make sure the HardwareRenderer.validate() was invoked before calling this method
11811 void flushLayer() {
11812 if (mLayerType == LAYER_TYPE_HARDWARE && mHardwareLayer != null) {
11813 mHardwareLayer.flush();
11814 }
11815 }
Romain Guyf1ae1062011-03-02 18:16:04 -080011816
11817 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080011818 * <p>Returns a hardware layer that can be used to draw this view again
11819 * without executing its draw method.</p>
11820 *
11821 * @return A HardwareLayer ready to render, or null if an error occurred.
11822 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080011823 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070011824 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
11825 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080011826 return null;
11827 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011828
Romain Guy9c4b79a2011-11-10 19:23:58 -080011829 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080011830
11831 final int width = mRight - mLeft;
11832 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080011833
Romain Guy6c319ca2011-01-11 14:29:25 -080011834 if (width == 0 || height == 0) {
11835 return null;
11836 }
11837
11838 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
11839 if (mHardwareLayer == null) {
11840 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
11841 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070011842 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080011843 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
11844 mHardwareLayer.resize(width, height);
Michael Jurka952e02b2012-03-13 18:34:35 -070011845 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080011846 }
11847
Romain Guy5cd5c3f2011-10-17 17:10:02 -070011848 // The layer is not valid if the underlying GPU resources cannot be allocated
11849 if (!mHardwareLayer.isValid()) {
11850 return null;
11851 }
11852
Chet Haasea1cff502012-02-21 13:43:44 -080011853 mHardwareLayer.redraw(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
Michael Jurka7e52caf2012-03-06 15:57:06 -080011854 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080011855 }
11856
11857 return mHardwareLayer;
11858 }
Romain Guy171c5922011-01-06 10:04:23 -080011859
Romain Guy589b0bb2011-10-10 13:57:47 -070011860 /**
11861 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070011862 *
Romain Guy589b0bb2011-10-10 13:57:47 -070011863 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070011864 *
11865 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070011866 * @see #LAYER_TYPE_HARDWARE
11867 */
Romain Guya998dff2012-03-23 18:58:36 -070011868 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070011869 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080011870 AttachInfo info = mAttachInfo;
11871 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070011872 info.mHardwareRenderer.isEnabled() &&
11873 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080011874 mHardwareLayer.destroy();
11875 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080011876
Romain Guy9c4b79a2011-11-10 19:23:58 -080011877 invalidate(true);
11878 invalidateParentCaches();
11879 }
Romain Guy65b345f2011-07-27 18:51:50 -070011880 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070011881 }
Romain Guy65b345f2011-07-27 18:51:50 -070011882 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070011883 }
11884
Romain Guy171c5922011-01-06 10:04:23 -080011885 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080011886 * Destroys all hardware rendering resources. This method is invoked
11887 * when the system needs to reclaim resources. Upon execution of this
11888 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070011889 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080011890 * Note: you <strong>must</strong> call
11891 * <code>super.destroyHardwareResources()</code> when overriding
11892 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070011893 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080011894 * @hide
11895 */
11896 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070011897 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011898 }
11899
11900 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011901 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
11902 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
11903 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
11904 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
11905 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
11906 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011907 *
Romain Guy171c5922011-01-06 10:04:23 -080011908 * <p>Enabling the drawing cache is similar to
11909 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080011910 * acceleration is turned off. When hardware acceleration is turned on, enabling the
11911 * drawing cache has no effect on rendering because the system uses a different mechanism
11912 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
11913 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
11914 * for information on how to enable software and hardware layers.</p>
11915 *
11916 * <p>This API can be used to manually generate
11917 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
11918 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011919 *
11920 * @param enabled true to enable the drawing cache, false otherwise
11921 *
11922 * @see #isDrawingCacheEnabled()
11923 * @see #getDrawingCache()
11924 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080011925 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011926 */
11927 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080011928 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011929 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
11930 }
11931
11932 /**
11933 * <p>Indicates whether the drawing cache is enabled for this view.</p>
11934 *
11935 * @return true if the drawing cache is enabled
11936 *
11937 * @see #setDrawingCacheEnabled(boolean)
11938 * @see #getDrawingCache()
11939 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070011940 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011941 public boolean isDrawingCacheEnabled() {
11942 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
11943 }
11944
11945 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080011946 * Debugging utility which recursively outputs the dirty state of a view and its
11947 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080011948 *
Chet Haasedaf98e92011-01-10 14:10:36 -080011949 * @hide
11950 */
Romain Guy676b1732011-02-14 14:45:33 -080011951 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080011952 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
11953 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
11954 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
11955 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
11956 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
11957 if (clear) {
11958 mPrivateFlags &= clearMask;
11959 }
11960 if (this instanceof ViewGroup) {
11961 ViewGroup parent = (ViewGroup) this;
11962 final int count = parent.getChildCount();
11963 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080011964 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080011965 child.outputDirtyFlags(indent + " ", clear, clearMask);
11966 }
11967 }
11968 }
11969
11970 /**
11971 * This method is used by ViewGroup to cause its children to restore or recreate their
11972 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
11973 * to recreate its own display list, which would happen if it went through the normal
11974 * draw/dispatchDraw mechanisms.
11975 *
11976 * @hide
11977 */
11978 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080011979
11980 /**
11981 * A view that is not attached or hardware accelerated cannot create a display list.
11982 * This method checks these conditions and returns the appropriate result.
11983 *
11984 * @return true if view has the ability to create a display list, false otherwise.
11985 *
11986 * @hide
11987 */
11988 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080011989 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080011990 }
Joe Malin32736f02011-01-19 16:14:20 -080011991
Chet Haasedaf98e92011-01-10 14:10:36 -080011992 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080011993 * @return The HardwareRenderer associated with that view or null if hardware rendering
11994 * is not supported or this this has not been attached to a window.
11995 *
11996 * @hide
11997 */
11998 public HardwareRenderer getHardwareRenderer() {
11999 if (mAttachInfo != null) {
12000 return mAttachInfo.mHardwareRenderer;
12001 }
12002 return null;
12003 }
12004
12005 /**
Chet Haasea1cff502012-02-21 13:43:44 -080012006 * Returns a DisplayList. If the incoming displayList is null, one will be created.
12007 * Otherwise, the same display list will be returned (after having been rendered into
12008 * along the way, depending on the invalidation state of the view).
12009 *
12010 * @param displayList The previous version of this displayList, could be null.
12011 * @param isLayer Whether the requester of the display list is a layer. If so,
12012 * the view will avoid creating a layer inside the resulting display list.
12013 * @return A new or reused DisplayList object.
12014 */
12015 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
12016 if (!canHaveDisplayList()) {
12017 return null;
12018 }
12019
12020 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
12021 displayList == null || !displayList.isValid() ||
12022 (!isLayer && mRecreateDisplayList))) {
12023 // Don't need to recreate the display list, just need to tell our
12024 // children to restore/recreate theirs
12025 if (displayList != null && displayList.isValid() &&
12026 !isLayer && !mRecreateDisplayList) {
12027 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12028 mPrivateFlags &= ~DIRTY_MASK;
12029 dispatchGetDisplayList();
12030
12031 return displayList;
12032 }
12033
12034 if (!isLayer) {
12035 // If we got here, we're recreating it. Mark it as such to ensure that
12036 // we copy in child display lists into ours in drawChild()
12037 mRecreateDisplayList = true;
12038 }
12039 if (displayList == null) {
12040 final String name = getClass().getSimpleName();
12041 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
12042 // If we're creating a new display list, make sure our parent gets invalidated
12043 // since they will need to recreate their display list to account for this
12044 // new child display list.
12045 invalidateParentCaches();
12046 }
12047
12048 boolean caching = false;
12049 final HardwareCanvas canvas = displayList.start();
Chet Haasea1cff502012-02-21 13:43:44 -080012050 int width = mRight - mLeft;
12051 int height = mBottom - mTop;
12052
12053 try {
12054 canvas.setViewport(width, height);
12055 // The dirty rect should always be null for a display list
12056 canvas.onPreDraw(null);
12057 int layerType = (
12058 !(mParent instanceof ViewGroup) || ((ViewGroup)mParent).mDrawLayers) ?
12059 getLayerType() : LAYER_TYPE_NONE;
Chet Haase1271e2c2012-04-20 09:54:27 -070012060 if (!isLayer && layerType != LAYER_TYPE_NONE) {
Chet Haaseb85967b2012-03-26 14:37:51 -070012061 if (layerType == LAYER_TYPE_HARDWARE) {
12062 final HardwareLayer layer = getHardwareLayer();
12063 if (layer != null && layer.isValid()) {
12064 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
12065 } else {
12066 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
12067 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
12068 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12069 }
12070 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080012071 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070012072 buildDrawingCache(true);
12073 Bitmap cache = getDrawingCache(true);
12074 if (cache != null) {
12075 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
12076 caching = true;
12077 }
Chet Haasea1cff502012-02-21 13:43:44 -080012078 }
Chet Haasea1cff502012-02-21 13:43:44 -080012079 } else {
12080
12081 computeScroll();
12082
Chet Haasea1cff502012-02-21 13:43:44 -080012083 canvas.translate(-mScrollX, -mScrollY);
12084 if (!isLayer) {
12085 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12086 mPrivateFlags &= ~DIRTY_MASK;
12087 }
12088
12089 // Fast path for layouts with no backgrounds
12090 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12091 dispatchDraw(canvas);
12092 } else {
12093 draw(canvas);
12094 }
12095 }
12096 } finally {
Chet Haasea1cff502012-02-21 13:43:44 -080012097 canvas.onPostDraw();
12098
12099 displayList.end();
Chet Haase1271e2c2012-04-20 09:54:27 -070012100 displayList.setCaching(caching);
12101 if (isLayer) {
Chet Haasea1cff502012-02-21 13:43:44 -080012102 displayList.setLeftTopRightBottom(0, 0, width, height);
12103 } else {
12104 setDisplayListProperties(displayList);
12105 }
12106 }
12107 } else if (!isLayer) {
12108 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12109 mPrivateFlags &= ~DIRTY_MASK;
12110 }
12111
12112 return displayList;
12113 }
12114
12115 /**
12116 * Get the DisplayList for the HardwareLayer
12117 *
12118 * @param layer The HardwareLayer whose DisplayList we want
12119 * @return A DisplayList fopr the specified HardwareLayer
12120 */
12121 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
12122 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
12123 layer.setDisplayList(displayList);
12124 return displayList;
12125 }
12126
12127
12128 /**
Romain Guyb051e892010-09-28 19:09:36 -070012129 * <p>Returns a display list that can be used to draw this view again
12130 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012131 *
Romain Guyb051e892010-09-28 19:09:36 -070012132 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080012133 *
12134 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070012135 */
Chet Haasedaf98e92011-01-10 14:10:36 -080012136 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080012137 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070012138 return mDisplayList;
12139 }
12140
12141 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012142 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012143 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012144 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012145 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012146 * @see #getDrawingCache(boolean)
12147 */
12148 public Bitmap getDrawingCache() {
12149 return getDrawingCache(false);
12150 }
12151
12152 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012153 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
12154 * is null when caching is disabled. If caching is enabled and the cache is not ready,
12155 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
12156 * draw from the cache when the cache is enabled. To benefit from the cache, you must
12157 * request the drawing cache by calling this method and draw it on screen if the
12158 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012159 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012160 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12161 * this method will create a bitmap of the same size as this view. Because this bitmap
12162 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12163 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12164 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12165 * size than the view. This implies that your application must be able to handle this
12166 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012167 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012168 * @param autoScale Indicates whether the generated bitmap should be scaled based on
12169 * the current density of the screen when the application is in compatibility
12170 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012171 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012172 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012173 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012174 * @see #setDrawingCacheEnabled(boolean)
12175 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070012176 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012177 * @see #destroyDrawingCache()
12178 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012179 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012180 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
12181 return null;
12182 }
12183 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012184 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012185 }
Romain Guy02890fd2010-08-06 17:58:44 -070012186 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012187 }
12188
12189 /**
12190 * <p>Frees the resources used by the drawing cache. If you call
12191 * {@link #buildDrawingCache()} manually without calling
12192 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12193 * should cleanup the cache with this method afterwards.</p>
12194 *
12195 * @see #setDrawingCacheEnabled(boolean)
12196 * @see #buildDrawingCache()
12197 * @see #getDrawingCache()
12198 */
12199 public void destroyDrawingCache() {
12200 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012201 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012202 mDrawingCache = null;
12203 }
Romain Guyfbd8f692009-06-26 14:51:58 -070012204 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012205 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070012206 mUnscaledDrawingCache = null;
12207 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012208 }
12209
12210 /**
12211 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070012212 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012213 * view will always be drawn on top of a solid color.
12214 *
12215 * @param color The background color to use for the drawing cache's bitmap
12216 *
12217 * @see #setDrawingCacheEnabled(boolean)
12218 * @see #buildDrawingCache()
12219 * @see #getDrawingCache()
12220 */
12221 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080012222 if (color != mDrawingCacheBackgroundColor) {
12223 mDrawingCacheBackgroundColor = color;
12224 mPrivateFlags &= ~DRAWING_CACHE_VALID;
12225 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012226 }
12227
12228 /**
12229 * @see #setDrawingCacheBackgroundColor(int)
12230 *
12231 * @return The background color to used for the drawing cache's bitmap
12232 */
12233 public int getDrawingCacheBackgroundColor() {
12234 return mDrawingCacheBackgroundColor;
12235 }
12236
12237 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012238 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012239 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012240 * @see #buildDrawingCache(boolean)
12241 */
12242 public void buildDrawingCache() {
12243 buildDrawingCache(false);
12244 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080012245
Romain Guyfbd8f692009-06-26 14:51:58 -070012246 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012247 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
12248 *
12249 * <p>If you call {@link #buildDrawingCache()} manually without calling
12250 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12251 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012252 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012253 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12254 * this method will create a bitmap of the same size as this view. Because this bitmap
12255 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12256 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12257 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12258 * size than the view. This implies that your application must be able to handle this
12259 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012260 *
Romain Guy0d9275e2010-10-26 14:22:30 -070012261 * <p>You should avoid calling this method when hardware acceleration is enabled. If
12262 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080012263 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070012264 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012265 *
12266 * @see #getDrawingCache()
12267 * @see #destroyDrawingCache()
12268 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012269 public void buildDrawingCache(boolean autoScale) {
12270 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070012271 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012272 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012273
12274 if (ViewDebug.TRACE_HIERARCHY) {
12275 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
12276 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012277
Romain Guy8506ab42009-06-11 17:35:47 -070012278 int width = mRight - mLeft;
12279 int height = mBottom - mTop;
12280
12281 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070012282 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070012283
Romain Guye1123222009-06-29 14:24:56 -070012284 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012285 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
12286 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070012287 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012288
12289 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070012290 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080012291 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012292
12293 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070012294 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080012295 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012296 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
12297 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080012298 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012299 return;
12300 }
12301
12302 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070012303 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012304
12305 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012306 Bitmap.Config quality;
12307 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080012308 // Never pick ARGB_4444 because it looks awful
12309 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012310 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
12311 case DRAWING_CACHE_QUALITY_AUTO:
12312 quality = Bitmap.Config.ARGB_8888;
12313 break;
12314 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080012315 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012316 break;
12317 case DRAWING_CACHE_QUALITY_HIGH:
12318 quality = Bitmap.Config.ARGB_8888;
12319 break;
12320 default:
12321 quality = Bitmap.Config.ARGB_8888;
12322 break;
12323 }
12324 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070012325 // Optimization for translucent windows
12326 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080012327 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012328 }
12329
12330 // Try to cleanup memory
12331 if (bitmap != null) bitmap.recycle();
12332
12333 try {
12334 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070012335 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070012336 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070012337 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012338 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070012339 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012340 }
Adam Powell26153a32010-11-08 15:22:27 -080012341 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012342 } catch (OutOfMemoryError e) {
12343 // If there is not enough memory to create the bitmap cache, just
12344 // ignore the issue as bitmap caches are not required to draw the
12345 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012346 if (autoScale) {
12347 mDrawingCache = null;
12348 } else {
12349 mUnscaledDrawingCache = null;
12350 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012351 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012352 return;
12353 }
12354
12355 clear = drawingCacheBackgroundColor != 0;
12356 }
12357
12358 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012359 if (attachInfo != null) {
12360 canvas = attachInfo.mCanvas;
12361 if (canvas == null) {
12362 canvas = new Canvas();
12363 }
12364 canvas.setBitmap(bitmap);
12365 // Temporarily clobber the cached Canvas in case one of our children
12366 // is also using a drawing cache. Without this, the children would
12367 // steal the canvas by attaching their own bitmap to it and bad, bad
12368 // thing would happen (invisible views, corrupted drawings, etc.)
12369 attachInfo.mCanvas = null;
12370 } else {
12371 // This case should hopefully never or seldom happen
12372 canvas = new Canvas(bitmap);
12373 }
12374
12375 if (clear) {
12376 bitmap.eraseColor(drawingCacheBackgroundColor);
12377 }
12378
12379 computeScroll();
12380 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080012381
Romain Guye1123222009-06-29 14:24:56 -070012382 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012383 final float scale = attachInfo.mApplicationScale;
12384 canvas.scale(scale, scale);
12385 }
Joe Malin32736f02011-01-19 16:14:20 -080012386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012387 canvas.translate(-mScrollX, -mScrollY);
12388
Romain Guy5bcdff42009-05-14 21:27:18 -070012389 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080012390 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
12391 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070012392 mPrivateFlags |= DRAWING_CACHE_VALID;
12393 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012394
12395 // Fast path for layouts with no backgrounds
12396 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12397 if (ViewDebug.TRACE_HIERARCHY) {
12398 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
12399 }
Romain Guy5bcdff42009-05-14 21:27:18 -070012400 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012401 dispatchDraw(canvas);
12402 } else {
12403 draw(canvas);
12404 }
12405
12406 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012407 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012408
12409 if (attachInfo != null) {
12410 // Restore the cached Canvas for our siblings
12411 attachInfo.mCanvas = canvas;
12412 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012413 }
12414 }
12415
12416 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012417 * Create a snapshot of the view into a bitmap. We should probably make
12418 * some form of this public, but should think about the API.
12419 */
Romain Guy223ff5c2010-03-02 17:07:47 -080012420 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012421 int width = mRight - mLeft;
12422 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012423
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012424 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070012425 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012426 width = (int) ((width * scale) + 0.5f);
12427 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080012428
Romain Guy8c11e312009-09-14 15:15:30 -070012429 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012430 if (bitmap == null) {
12431 throw new OutOfMemoryError();
12432 }
12433
Romain Guyc529d8d2011-09-06 15:01:39 -070012434 Resources resources = getResources();
12435 if (resources != null) {
12436 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
12437 }
Joe Malin32736f02011-01-19 16:14:20 -080012438
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012439 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012440 if (attachInfo != null) {
12441 canvas = attachInfo.mCanvas;
12442 if (canvas == null) {
12443 canvas = new Canvas();
12444 }
12445 canvas.setBitmap(bitmap);
12446 // Temporarily clobber the cached Canvas in case one of our children
12447 // is also using a drawing cache. Without this, the children would
12448 // steal the canvas by attaching their own bitmap to it and bad, bad
12449 // things would happen (invisible views, corrupted drawings, etc.)
12450 attachInfo.mCanvas = null;
12451 } else {
12452 // This case should hopefully never or seldom happen
12453 canvas = new Canvas(bitmap);
12454 }
12455
Romain Guy5bcdff42009-05-14 21:27:18 -070012456 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012457 bitmap.eraseColor(backgroundColor);
12458 }
12459
12460 computeScroll();
12461 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012462 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012463 canvas.translate(-mScrollX, -mScrollY);
12464
Romain Guy5bcdff42009-05-14 21:27:18 -070012465 // Temporarily remove the dirty mask
12466 int flags = mPrivateFlags;
12467 mPrivateFlags &= ~DIRTY_MASK;
12468
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012469 // Fast path for layouts with no backgrounds
12470 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12471 dispatchDraw(canvas);
12472 } else {
12473 draw(canvas);
12474 }
12475
Romain Guy5bcdff42009-05-14 21:27:18 -070012476 mPrivateFlags = flags;
12477
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012478 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012479 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012480
12481 if (attachInfo != null) {
12482 // Restore the cached Canvas for our siblings
12483 attachInfo.mCanvas = canvas;
12484 }
Romain Guy8506ab42009-06-11 17:35:47 -070012485
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012486 return bitmap;
12487 }
12488
12489 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012490 * Indicates whether this View is currently in edit mode. A View is usually
12491 * in edit mode when displayed within a developer tool. For instance, if
12492 * this View is being drawn by a visual user interface builder, this method
12493 * should return true.
12494 *
12495 * Subclasses should check the return value of this method to provide
12496 * different behaviors if their normal behavior might interfere with the
12497 * host environment. For instance: the class spawns a thread in its
12498 * constructor, the drawing code relies on device-specific features, etc.
12499 *
12500 * This method is usually checked in the drawing code of custom widgets.
12501 *
12502 * @return True if this View is in edit mode, false otherwise.
12503 */
12504 public boolean isInEditMode() {
12505 return false;
12506 }
12507
12508 /**
12509 * If the View draws content inside its padding and enables fading edges,
12510 * it needs to support padding offsets. Padding offsets are added to the
12511 * fading edges to extend the length of the fade so that it covers pixels
12512 * drawn inside the padding.
12513 *
12514 * Subclasses of this class should override this method if they need
12515 * to draw content inside the padding.
12516 *
12517 * @return True if padding offset must be applied, false otherwise.
12518 *
12519 * @see #getLeftPaddingOffset()
12520 * @see #getRightPaddingOffset()
12521 * @see #getTopPaddingOffset()
12522 * @see #getBottomPaddingOffset()
12523 *
12524 * @since CURRENT
12525 */
12526 protected boolean isPaddingOffsetRequired() {
12527 return false;
12528 }
12529
12530 /**
12531 * Amount by which to extend the left fading region. Called only when
12532 * {@link #isPaddingOffsetRequired()} returns true.
12533 *
12534 * @return The left padding offset in pixels.
12535 *
12536 * @see #isPaddingOffsetRequired()
12537 *
12538 * @since CURRENT
12539 */
12540 protected int getLeftPaddingOffset() {
12541 return 0;
12542 }
12543
12544 /**
12545 * Amount by which to extend the right fading region. Called only when
12546 * {@link #isPaddingOffsetRequired()} returns true.
12547 *
12548 * @return The right padding offset in pixels.
12549 *
12550 * @see #isPaddingOffsetRequired()
12551 *
12552 * @since CURRENT
12553 */
12554 protected int getRightPaddingOffset() {
12555 return 0;
12556 }
12557
12558 /**
12559 * Amount by which to extend the top fading region. Called only when
12560 * {@link #isPaddingOffsetRequired()} returns true.
12561 *
12562 * @return The top padding offset in pixels.
12563 *
12564 * @see #isPaddingOffsetRequired()
12565 *
12566 * @since CURRENT
12567 */
12568 protected int getTopPaddingOffset() {
12569 return 0;
12570 }
12571
12572 /**
12573 * Amount by which to extend the bottom fading region. Called only when
12574 * {@link #isPaddingOffsetRequired()} returns true.
12575 *
12576 * @return The bottom padding offset in pixels.
12577 *
12578 * @see #isPaddingOffsetRequired()
12579 *
12580 * @since CURRENT
12581 */
12582 protected int getBottomPaddingOffset() {
12583 return 0;
12584 }
12585
12586 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070012587 * @hide
12588 * @param offsetRequired
12589 */
12590 protected int getFadeTop(boolean offsetRequired) {
12591 int top = mPaddingTop;
12592 if (offsetRequired) top += getTopPaddingOffset();
12593 return top;
12594 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012595
Romain Guyf2fc4602011-07-19 15:20:03 -070012596 /**
12597 * @hide
12598 * @param offsetRequired
12599 */
12600 protected int getFadeHeight(boolean offsetRequired) {
12601 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070012602 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070012603 return mBottom - mTop - mPaddingBottom - padding;
12604 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012605
Romain Guyf2fc4602011-07-19 15:20:03 -070012606 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012607 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070012608 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012609 *
Romain Guy2bffd262010-09-12 17:40:02 -070012610 * <p>Even if this method returns true, it does not mean that every call
12611 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
12612 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012613 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070012614 * window is hardware accelerated,
12615 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
12616 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012617 *
Romain Guy2bffd262010-09-12 17:40:02 -070012618 * @return True if the view is attached to a window and the window is
12619 * hardware accelerated; false in any other case.
12620 */
12621 public boolean isHardwareAccelerated() {
12622 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
12623 }
Joe Malin32736f02011-01-19 16:14:20 -080012624
Romain Guy2bffd262010-09-12 17:40:02 -070012625 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080012626 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
12627 * case of an active Animation being run on the view.
12628 */
12629 private boolean drawAnimation(ViewGroup parent, long drawingTime,
12630 Animation a, boolean scalingRequired) {
12631 Transformation invalidationTransform;
12632 final int flags = parent.mGroupFlags;
12633 final boolean initialized = a.isInitialized();
12634 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070012635 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080012636 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
12637 onAnimationStart();
12638 }
12639
12640 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
12641 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
12642 if (parent.mInvalidationTransformation == null) {
12643 parent.mInvalidationTransformation = new Transformation();
12644 }
12645 invalidationTransform = parent.mInvalidationTransformation;
12646 a.getTransformation(drawingTime, invalidationTransform, 1f);
12647 } else {
12648 invalidationTransform = parent.mChildTransformation;
12649 }
12650 if (more) {
12651 if (!a.willChangeBounds()) {
12652 if ((flags & (parent.FLAG_OPTIMIZE_INVALIDATE | parent.FLAG_ANIMATION_DONE)) ==
12653 parent.FLAG_OPTIMIZE_INVALIDATE) {
12654 parent.mGroupFlags |= parent.FLAG_INVALIDATE_REQUIRED;
12655 } else if ((flags & parent.FLAG_INVALIDATE_REQUIRED) == 0) {
12656 // The child need to draw an animation, potentially offscreen, so
12657 // make sure we do not cancel invalidate requests
12658 parent.mPrivateFlags |= DRAW_ANIMATION;
12659 parent.invalidate(mLeft, mTop, mRight, mBottom);
12660 }
12661 } else {
12662 if (parent.mInvalidateRegion == null) {
12663 parent.mInvalidateRegion = new RectF();
12664 }
12665 final RectF region = parent.mInvalidateRegion;
12666 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
12667 invalidationTransform);
12668
12669 // The child need to draw an animation, potentially offscreen, so
12670 // make sure we do not cancel invalidate requests
12671 parent.mPrivateFlags |= DRAW_ANIMATION;
12672
12673 final int left = mLeft + (int) region.left;
12674 final int top = mTop + (int) region.top;
12675 parent.invalidate(left, top, left + (int) (region.width() + .5f),
12676 top + (int) (region.height() + .5f));
12677 }
12678 }
12679 return more;
12680 }
12681
Chet Haasea1cff502012-02-21 13:43:44 -080012682 /**
12683 * This method is called by getDisplayList() when a display list is created or re-rendered.
12684 * It sets or resets the current value of all properties on that display list (resetting is
12685 * necessary when a display list is being re-created, because we need to make sure that
12686 * previously-set transform values
12687 */
12688 void setDisplayListProperties(DisplayList displayList) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012689 if (displayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012690 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070012691 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080012692 if (mParent instanceof ViewGroup) {
12693 displayList.setClipChildren(
12694 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
12695 }
Chet Haase9420abd2012-03-29 16:28:32 -070012696 float alpha = 1;
12697 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
12698 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
12699 ViewGroup parentVG = (ViewGroup) mParent;
12700 final boolean hasTransform =
12701 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
12702 if (hasTransform) {
12703 Transformation transform = parentVG.mChildTransformation;
12704 final int transformType = parentVG.mChildTransformation.getTransformationType();
12705 if (transformType != Transformation.TYPE_IDENTITY) {
12706 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
12707 alpha = transform.getAlpha();
12708 }
12709 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
12710 displayList.setStaticMatrix(transform.getMatrix());
12711 }
12712 }
12713 }
Chet Haasea1cff502012-02-21 13:43:44 -080012714 }
12715 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070012716 alpha *= mTransformationInfo.mAlpha;
12717 if (alpha < 1) {
12718 final int multipliedAlpha = (int) (255 * alpha);
12719 if (onSetAlpha(multipliedAlpha)) {
12720 alpha = 1;
12721 }
12722 }
12723 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080012724 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
12725 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
12726 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
12727 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070012728 if (mTransformationInfo.mCamera == null) {
12729 mTransformationInfo.mCamera = new Camera();
12730 mTransformationInfo.matrix3D = new Matrix();
12731 }
12732 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Chet Haasea1cff502012-02-21 13:43:44 -080012733 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == PIVOT_EXPLICITLY_SET) {
12734 displayList.setPivotX(getPivotX());
12735 displayList.setPivotY(getPivotY());
12736 }
Chet Haase9420abd2012-03-29 16:28:32 -070012737 } else if (alpha < 1) {
12738 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080012739 }
12740 }
12741 }
12742
Chet Haasebcca79a2012-02-14 08:45:14 -080012743 /**
Chet Haase64a48c12012-02-13 16:33:29 -080012744 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
12745 * This draw() method is an implementation detail and is not intended to be overridden or
12746 * to be called from anywhere else other than ViewGroup.drawChild().
12747 */
12748 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012749 boolean useDisplayListProperties = mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080012750 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080012751 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080012752 final int flags = parent.mGroupFlags;
12753
Chet Haasea1cff502012-02-21 13:43:44 -080012754 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080012755 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080012756 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012757 }
12758
12759 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080012760 boolean concatMatrix = false;
12761
12762 boolean scalingRequired = false;
12763 boolean caching;
12764 int layerType = parent.mDrawLayers ? getLayerType() : LAYER_TYPE_NONE;
12765
12766 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080012767 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
12768 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080012769 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070012770 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080012771 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
12772 } else {
12773 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
12774 }
12775
Chet Haasebcca79a2012-02-14 08:45:14 -080012776 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080012777 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012778 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080012779 concatMatrix = a.willChangeTransformationMatrix();
Chet Haasebcca79a2012-02-14 08:45:14 -080012780 transformToApply = parent.mChildTransformation;
Chet Haase9420abd2012-03-29 16:28:32 -070012781 } else if (!useDisplayListProperties &&
12782 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012783 final boolean hasTransform =
12784 parent.getChildStaticTransformation(this, parent.mChildTransformation);
Chet Haase64a48c12012-02-13 16:33:29 -080012785 if (hasTransform) {
12786 final int transformType = parent.mChildTransformation.getTransformationType();
12787 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
12788 parent.mChildTransformation : null;
12789 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
12790 }
12791 }
12792
12793 concatMatrix |= !childHasIdentityMatrix;
12794
12795 // Sets the flag as early as possible to allow draw() implementations
12796 // to call invalidate() successfully when doing animations
12797 mPrivateFlags |= DRAWN;
12798
Chet Haasebcca79a2012-02-14 08:45:14 -080012799 if (!concatMatrix && canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Chet Haase64a48c12012-02-13 16:33:29 -080012800 (mPrivateFlags & DRAW_ANIMATION) == 0) {
12801 return more;
12802 }
12803
12804 if (hardwareAccelerated) {
12805 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
12806 // retain the flag's value temporarily in the mRecreateDisplayList flag
12807 mRecreateDisplayList = (mPrivateFlags & INVALIDATED) == INVALIDATED;
12808 mPrivateFlags &= ~INVALIDATED;
12809 }
12810
12811 computeScroll();
12812
12813 final int sx = mScrollX;
12814 final int sy = mScrollY;
12815
12816 DisplayList displayList = null;
12817 Bitmap cache = null;
12818 boolean hasDisplayList = false;
12819 if (caching) {
12820 if (!hardwareAccelerated) {
12821 if (layerType != LAYER_TYPE_NONE) {
12822 layerType = LAYER_TYPE_SOFTWARE;
12823 buildDrawingCache(true);
12824 }
12825 cache = getDrawingCache(true);
12826 } else {
12827 switch (layerType) {
12828 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070012829 if (useDisplayListProperties) {
12830 hasDisplayList = canHaveDisplayList();
12831 } else {
12832 buildDrawingCache(true);
12833 cache = getDrawingCache(true);
12834 }
Chet Haase64a48c12012-02-13 16:33:29 -080012835 break;
Chet Haasea1cff502012-02-21 13:43:44 -080012836 case LAYER_TYPE_HARDWARE:
12837 if (useDisplayListProperties) {
12838 hasDisplayList = canHaveDisplayList();
12839 }
12840 break;
Chet Haase64a48c12012-02-13 16:33:29 -080012841 case LAYER_TYPE_NONE:
12842 // Delay getting the display list until animation-driven alpha values are
12843 // set up and possibly passed on to the view
12844 hasDisplayList = canHaveDisplayList();
12845 break;
12846 }
12847 }
12848 }
Chet Haasea1cff502012-02-21 13:43:44 -080012849 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070012850 if (useDisplayListProperties) {
12851 displayList = getDisplayList();
12852 if (!displayList.isValid()) {
12853 // Uncommon, but possible. If a view is removed from the hierarchy during the call
12854 // to getDisplayList(), the display list will be marked invalid and we should not
12855 // try to use it again.
12856 displayList = null;
12857 hasDisplayList = false;
12858 useDisplayListProperties = false;
12859 }
12860 }
Chet Haase64a48c12012-02-13 16:33:29 -080012861
12862 final boolean hasNoCache = cache == null || hasDisplayList;
12863 final boolean offsetForScroll = cache == null && !hasDisplayList &&
12864 layerType != LAYER_TYPE_HARDWARE;
12865
Chet Haasea1cff502012-02-21 13:43:44 -080012866 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070012867 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012868 restoreTo = canvas.save();
12869 }
Chet Haase64a48c12012-02-13 16:33:29 -080012870 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012871 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080012872 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080012873 if (!useDisplayListProperties) {
12874 canvas.translate(mLeft, mTop);
12875 }
Chet Haase64a48c12012-02-13 16:33:29 -080012876 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080012877 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070012878 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080012879 restoreTo = canvas.save();
12880 }
Chet Haase64a48c12012-02-13 16:33:29 -080012881 // mAttachInfo cannot be null, otherwise scalingRequired == false
12882 final float scale = 1.0f / mAttachInfo.mApplicationScale;
12883 canvas.scale(scale, scale);
12884 }
12885 }
12886
Chet Haasea1cff502012-02-21 13:43:44 -080012887 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070012888 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix()) {
Chet Haase64a48c12012-02-13 16:33:29 -080012889 if (transformToApply != null || !childHasIdentityMatrix) {
12890 int transX = 0;
12891 int transY = 0;
12892
12893 if (offsetForScroll) {
12894 transX = -sx;
12895 transY = -sy;
12896 }
12897
12898 if (transformToApply != null) {
12899 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070012900 if (useDisplayListProperties) {
12901 displayList.setAnimationMatrix(transformToApply.getMatrix());
12902 } else {
12903 // Undo the scroll translation, apply the transformation matrix,
12904 // then redo the scroll translate to get the correct result.
12905 canvas.translate(-transX, -transY);
12906 canvas.concat(transformToApply.getMatrix());
12907 canvas.translate(transX, transY);
12908 }
Chet Haasea1cff502012-02-21 13:43:44 -080012909 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012910 }
12911
12912 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070012913 if (transformAlpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080012914 alpha *= transformToApply.getAlpha();
Chet Haasea1cff502012-02-21 13:43:44 -080012915 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012916 }
12917 }
12918
Chet Haasea1cff502012-02-21 13:43:44 -080012919 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080012920 canvas.translate(-transX, -transY);
12921 canvas.concat(getMatrix());
12922 canvas.translate(transX, transY);
12923 }
12924 }
12925
Chet Haase9420abd2012-03-29 16:28:32 -070012926 if (alpha < 1) {
Chet Haasea1cff502012-02-21 13:43:44 -080012927 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012928 if (hasNoCache) {
12929 final int multipliedAlpha = (int) (255 * alpha);
12930 if (!onSetAlpha(multipliedAlpha)) {
12931 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080012932 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080012933 layerType != LAYER_TYPE_NONE) {
12934 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
12935 }
Chet Haase9420abd2012-03-29 16:28:32 -070012936 if (useDisplayListProperties) {
12937 displayList.setAlpha(alpha * getAlpha());
12938 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070012939 final int scrollX = hasDisplayList ? 0 : sx;
12940 final int scrollY = hasDisplayList ? 0 : sy;
12941 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
12942 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080012943 }
12944 } else {
12945 // Alpha is handled by the child directly, clobber the layer's alpha
12946 mPrivateFlags |= ALPHA_SET;
12947 }
12948 }
12949 }
12950 } else if ((mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
12951 onSetAlpha(255);
12952 mPrivateFlags &= ~ALPHA_SET;
12953 }
12954
Chet Haasea1cff502012-02-21 13:43:44 -080012955 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
12956 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080012957 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012958 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080012959 } else {
12960 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012961 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080012962 } else {
12963 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
12964 }
12965 }
12966 }
12967
Chet Haase9420abd2012-03-29 16:28:32 -070012968 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080012969 displayList = getDisplayList();
12970 if (!displayList.isValid()) {
12971 // Uncommon, but possible. If a view is removed from the hierarchy during the call
12972 // to getDisplayList(), the display list will be marked invalid and we should not
12973 // try to use it again.
12974 displayList = null;
12975 hasDisplayList = false;
12976 }
12977 }
12978
12979 if (hasNoCache) {
12980 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080012981 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080012982 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080012983 if (layer != null && layer.isValid()) {
12984 mLayerPaint.setAlpha((int) (alpha * 255));
12985 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
12986 layerRendered = true;
12987 } else {
12988 final int scrollX = hasDisplayList ? 0 : sx;
12989 final int scrollY = hasDisplayList ? 0 : sy;
12990 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080012991 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080012992 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12993 }
12994 }
12995
12996 if (!layerRendered) {
12997 if (!hasDisplayList) {
12998 // Fast path for layouts with no backgrounds
12999 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
13000 if (ViewDebug.TRACE_HIERARCHY) {
13001 ViewDebug.trace(parent, ViewDebug.HierarchyTraceType.DRAW);
13002 }
13003 mPrivateFlags &= ~DIRTY_MASK;
13004 dispatchDraw(canvas);
13005 } else {
13006 draw(canvas);
13007 }
13008 } else {
13009 mPrivateFlags &= ~DIRTY_MASK;
Chet Haase1271e2c2012-04-20 09:54:27 -070013010 ((HardwareCanvas) canvas).drawDisplayList(displayList, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080013011 }
13012 }
13013 } else if (cache != null) {
13014 mPrivateFlags &= ~DIRTY_MASK;
13015 Paint cachePaint;
13016
13017 if (layerType == LAYER_TYPE_NONE) {
13018 cachePaint = parent.mCachePaint;
13019 if (cachePaint == null) {
13020 cachePaint = new Paint();
13021 cachePaint.setDither(false);
13022 parent.mCachePaint = cachePaint;
13023 }
Chet Haase9420abd2012-03-29 16:28:32 -070013024 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080013025 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080013026 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
13027 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013028 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080013029 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080013030 }
13031 } else {
13032 cachePaint = mLayerPaint;
13033 cachePaint.setAlpha((int) (alpha * 255));
13034 }
13035 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
13036 }
13037
Chet Haasea1cff502012-02-21 13:43:44 -080013038 if (restoreTo >= 0) {
13039 canvas.restoreToCount(restoreTo);
13040 }
Chet Haase64a48c12012-02-13 16:33:29 -080013041
13042 if (a != null && !more) {
13043 if (!hardwareAccelerated && !a.getFillAfter()) {
13044 onSetAlpha(255);
13045 }
13046 parent.finishAnimatingView(this, a);
13047 }
13048
13049 if (more && hardwareAccelerated) {
13050 // invalidation is the trigger to recreate display lists, so if we're using
13051 // display lists to render, force an invalidate to allow the animation to
13052 // continue drawing another frame
13053 parent.invalidate(true);
13054 if (a.hasAlpha() && (mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
13055 // alpha animations should cause the child to recreate its display list
13056 invalidate(true);
13057 }
13058 }
13059
13060 mRecreateDisplayList = false;
13061
13062 return more;
13063 }
13064
13065 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013066 * Manually render this view (and all of its children) to the given Canvas.
13067 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070013068 * called. When implementing a view, implement
13069 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
13070 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013071 *
13072 * @param canvas The Canvas to which the View is rendered.
13073 */
13074 public void draw(Canvas canvas) {
13075 if (ViewDebug.TRACE_HIERARCHY) {
13076 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
13077 }
13078
Romain Guy5bcdff42009-05-14 21:27:18 -070013079 final int privateFlags = mPrivateFlags;
13080 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
13081 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
13082 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070013083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013084 /*
13085 * Draw traversal performs several drawing steps which must be executed
13086 * in the appropriate order:
13087 *
13088 * 1. Draw the background
13089 * 2. If necessary, save the canvas' layers to prepare for fading
13090 * 3. Draw view's content
13091 * 4. Draw children
13092 * 5. If necessary, draw the fading edges and restore layers
13093 * 6. Draw decorations (scrollbars for instance)
13094 */
13095
13096 // Step 1, draw the background, if needed
13097 int saveCount;
13098
Romain Guy24443ea2009-05-11 11:56:30 -070013099 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013100 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070013101 if (background != null) {
13102 final int scrollX = mScrollX;
13103 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013104
Romain Guy24443ea2009-05-11 11:56:30 -070013105 if (mBackgroundSizeChanged) {
13106 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
13107 mBackgroundSizeChanged = false;
13108 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013109
Romain Guy24443ea2009-05-11 11:56:30 -070013110 if ((scrollX | scrollY) == 0) {
13111 background.draw(canvas);
13112 } else {
13113 canvas.translate(scrollX, scrollY);
13114 background.draw(canvas);
13115 canvas.translate(-scrollX, -scrollY);
13116 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013117 }
13118 }
13119
13120 // skip step 2 & 5 if possible (common case)
13121 final int viewFlags = mViewFlags;
13122 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
13123 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
13124 if (!verticalEdges && !horizontalEdges) {
13125 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013126 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013127
13128 // Step 4, draw the children
13129 dispatchDraw(canvas);
13130
13131 // Step 6, draw decorations (scrollbars)
13132 onDrawScrollBars(canvas);
13133
13134 // we're done...
13135 return;
13136 }
13137
13138 /*
13139 * Here we do the full fledged routine...
13140 * (this is an uncommon case where speed matters less,
13141 * this is why we repeat some of the tests that have been
13142 * done above)
13143 */
13144
13145 boolean drawTop = false;
13146 boolean drawBottom = false;
13147 boolean drawLeft = false;
13148 boolean drawRight = false;
13149
13150 float topFadeStrength = 0.0f;
13151 float bottomFadeStrength = 0.0f;
13152 float leftFadeStrength = 0.0f;
13153 float rightFadeStrength = 0.0f;
13154
13155 // Step 2, save the canvas' layers
13156 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013157
13158 final boolean offsetRequired = isPaddingOffsetRequired();
13159 if (offsetRequired) {
13160 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013161 }
13162
13163 int left = mScrollX + paddingLeft;
13164 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070013165 int top = mScrollY + getFadeTop(offsetRequired);
13166 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013167
13168 if (offsetRequired) {
13169 right += getRightPaddingOffset();
13170 bottom += getBottomPaddingOffset();
13171 }
13172
13173 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070013174 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013175 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013176
13177 // clip the fade length if top and bottom fades overlap
13178 // overlapping fades produce odd-looking artifacts
13179 if (verticalEdges && (top + length > bottom - length)) {
13180 length = (bottom - top) / 2;
13181 }
13182
13183 // also clip horizontal fades if necessary
13184 if (horizontalEdges && (left + length > right - length)) {
13185 length = (right - left) / 2;
13186 }
13187
13188 if (verticalEdges) {
13189 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013190 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013191 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013192 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013193 }
13194
13195 if (horizontalEdges) {
13196 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013197 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013198 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013199 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013200 }
13201
13202 saveCount = canvas.getSaveCount();
13203
13204 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070013205 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013206 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
13207
13208 if (drawTop) {
13209 canvas.saveLayer(left, top, right, top + length, null, flags);
13210 }
13211
13212 if (drawBottom) {
13213 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
13214 }
13215
13216 if (drawLeft) {
13217 canvas.saveLayer(left, top, left + length, bottom, null, flags);
13218 }
13219
13220 if (drawRight) {
13221 canvas.saveLayer(right - length, top, right, bottom, null, flags);
13222 }
13223 } else {
13224 scrollabilityCache.setFadeColor(solidColor);
13225 }
13226
13227 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013228 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013229
13230 // Step 4, draw the children
13231 dispatchDraw(canvas);
13232
13233 // Step 5, draw the fade effect and restore layers
13234 final Paint p = scrollabilityCache.paint;
13235 final Matrix matrix = scrollabilityCache.matrix;
13236 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013237
13238 if (drawTop) {
13239 matrix.setScale(1, fadeHeight * topFadeStrength);
13240 matrix.postTranslate(left, top);
13241 fade.setLocalMatrix(matrix);
13242 canvas.drawRect(left, top, right, top + length, p);
13243 }
13244
13245 if (drawBottom) {
13246 matrix.setScale(1, fadeHeight * bottomFadeStrength);
13247 matrix.postRotate(180);
13248 matrix.postTranslate(left, bottom);
13249 fade.setLocalMatrix(matrix);
13250 canvas.drawRect(left, bottom - length, right, bottom, p);
13251 }
13252
13253 if (drawLeft) {
13254 matrix.setScale(1, fadeHeight * leftFadeStrength);
13255 matrix.postRotate(-90);
13256 matrix.postTranslate(left, top);
13257 fade.setLocalMatrix(matrix);
13258 canvas.drawRect(left, top, left + length, bottom, p);
13259 }
13260
13261 if (drawRight) {
13262 matrix.setScale(1, fadeHeight * rightFadeStrength);
13263 matrix.postRotate(90);
13264 matrix.postTranslate(right, top);
13265 fade.setLocalMatrix(matrix);
13266 canvas.drawRect(right - length, top, right, bottom, p);
13267 }
13268
13269 canvas.restoreToCount(saveCount);
13270
13271 // Step 6, draw decorations (scrollbars)
13272 onDrawScrollBars(canvas);
13273 }
13274
13275 /**
13276 * Override this if your view is known to always be drawn on top of a solid color background,
13277 * and needs to draw fading edges. Returning a non-zero color enables the view system to
13278 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
13279 * should be set to 0xFF.
13280 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013281 * @see #setVerticalFadingEdgeEnabled(boolean)
13282 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013283 *
13284 * @return The known solid color background for this view, or 0 if the color may vary
13285 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013286 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013287 public int getSolidColor() {
13288 return 0;
13289 }
13290
13291 /**
13292 * Build a human readable string representation of the specified view flags.
13293 *
13294 * @param flags the view flags to convert to a string
13295 * @return a String representing the supplied flags
13296 */
13297 private static String printFlags(int flags) {
13298 String output = "";
13299 int numFlags = 0;
13300 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
13301 output += "TAKES_FOCUS";
13302 numFlags++;
13303 }
13304
13305 switch (flags & VISIBILITY_MASK) {
13306 case INVISIBLE:
13307 if (numFlags > 0) {
13308 output += " ";
13309 }
13310 output += "INVISIBLE";
13311 // USELESS HERE numFlags++;
13312 break;
13313 case GONE:
13314 if (numFlags > 0) {
13315 output += " ";
13316 }
13317 output += "GONE";
13318 // USELESS HERE numFlags++;
13319 break;
13320 default:
13321 break;
13322 }
13323 return output;
13324 }
13325
13326 /**
13327 * Build a human readable string representation of the specified private
13328 * view flags.
13329 *
13330 * @param privateFlags the private view flags to convert to a string
13331 * @return a String representing the supplied flags
13332 */
13333 private static String printPrivateFlags(int privateFlags) {
13334 String output = "";
13335 int numFlags = 0;
13336
13337 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
13338 output += "WANTS_FOCUS";
13339 numFlags++;
13340 }
13341
13342 if ((privateFlags & FOCUSED) == FOCUSED) {
13343 if (numFlags > 0) {
13344 output += " ";
13345 }
13346 output += "FOCUSED";
13347 numFlags++;
13348 }
13349
13350 if ((privateFlags & SELECTED) == SELECTED) {
13351 if (numFlags > 0) {
13352 output += " ";
13353 }
13354 output += "SELECTED";
13355 numFlags++;
13356 }
13357
13358 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
13359 if (numFlags > 0) {
13360 output += " ";
13361 }
13362 output += "IS_ROOT_NAMESPACE";
13363 numFlags++;
13364 }
13365
13366 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
13367 if (numFlags > 0) {
13368 output += " ";
13369 }
13370 output += "HAS_BOUNDS";
13371 numFlags++;
13372 }
13373
13374 if ((privateFlags & DRAWN) == DRAWN) {
13375 if (numFlags > 0) {
13376 output += " ";
13377 }
13378 output += "DRAWN";
13379 // USELESS HERE numFlags++;
13380 }
13381 return output;
13382 }
13383
13384 /**
13385 * <p>Indicates whether or not this view's layout will be requested during
13386 * the next hierarchy layout pass.</p>
13387 *
13388 * @return true if the layout will be forced during next layout pass
13389 */
13390 public boolean isLayoutRequested() {
13391 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
13392 }
13393
13394 /**
13395 * Assign a size and position to a view and all of its
13396 * descendants
13397 *
13398 * <p>This is the second phase of the layout mechanism.
13399 * (The first is measuring). In this phase, each parent calls
13400 * layout on all of its children to position them.
13401 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080013402 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013403 *
Chet Haase9c087442011-01-12 16:20:16 -080013404 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013405 * Derived classes with children should override
13406 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080013407 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013408 *
13409 * @param l Left position, relative to parent
13410 * @param t Top position, relative to parent
13411 * @param r Right position, relative to parent
13412 * @param b Bottom position, relative to parent
13413 */
Romain Guy5429e1d2010-09-07 12:38:00 -070013414 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080013415 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070013416 int oldL = mLeft;
13417 int oldT = mTop;
13418 int oldB = mBottom;
13419 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013420 boolean changed = setFrame(l, t, r, b);
13421 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
13422 if (ViewDebug.TRACE_HIERARCHY) {
13423 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
13424 }
13425
13426 onLayout(changed, l, t, r, b);
13427 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070013428
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013429 ListenerInfo li = mListenerInfo;
13430 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070013431 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013432 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070013433 int numListeners = listenersCopy.size();
13434 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070013435 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070013436 }
13437 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013438 }
13439 mPrivateFlags &= ~FORCE_LAYOUT;
13440 }
13441
13442 /**
13443 * Called from layout when this view should
13444 * assign a size and position to each of its children.
13445 *
13446 * Derived classes with children should override
13447 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070013448 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013449 * @param changed This is a new size or position for this view
13450 * @param left Left position, relative to parent
13451 * @param top Top position, relative to parent
13452 * @param right Right position, relative to parent
13453 * @param bottom Bottom position, relative to parent
13454 */
13455 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
13456 }
13457
13458 /**
13459 * Assign a size and position to this view.
13460 *
13461 * This is called from layout.
13462 *
13463 * @param left Left position, relative to parent
13464 * @param top Top position, relative to parent
13465 * @param right Right position, relative to parent
13466 * @param bottom Bottom position, relative to parent
13467 * @return true if the new size and position are different than the
13468 * previous ones
13469 * {@hide}
13470 */
13471 protected boolean setFrame(int left, int top, int right, int bottom) {
13472 boolean changed = false;
13473
13474 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070013475 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013476 + right + "," + bottom + ")");
13477 }
13478
13479 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
13480 changed = true;
13481
13482 // Remember our drawn bit
13483 int drawn = mPrivateFlags & DRAWN;
13484
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013485 int oldWidth = mRight - mLeft;
13486 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070013487 int newWidth = right - left;
13488 int newHeight = bottom - top;
13489 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
13490
13491 // Invalidate our old position
13492 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013493
13494 mLeft = left;
13495 mTop = top;
13496 mRight = right;
13497 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -070013498 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013499 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
13500 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013501
13502 mPrivateFlags |= HAS_BOUNDS;
13503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013504
Chet Haase75755e22011-07-18 17:48:25 -070013505 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013506 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
13507 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013508 if (mTransformationInfo != null) {
13509 mTransformationInfo.mMatrixDirty = true;
13510 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013511 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013512 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
13513 }
13514
13515 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
13516 // If we are visible, force the DRAWN bit to on so that
13517 // this invalidate will go through (at least to our parent).
13518 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013519 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013520 // the DRAWN bit.
13521 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070013522 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080013523 // parent display list may need to be recreated based on a change in the bounds
13524 // of any child
13525 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013526 }
13527
13528 // Reset drawn bit to original value (invalidate turns it off)
13529 mPrivateFlags |= drawn;
13530
13531 mBackgroundSizeChanged = true;
13532 }
13533 return changed;
13534 }
13535
13536 /**
13537 * Finalize inflating a view from XML. This is called as the last phase
13538 * of inflation, after all child views have been added.
13539 *
13540 * <p>Even if the subclass overrides onFinishInflate, they should always be
13541 * sure to call the super method, so that we get called.
13542 */
13543 protected void onFinishInflate() {
13544 }
13545
13546 /**
13547 * Returns the resources associated with this view.
13548 *
13549 * @return Resources object.
13550 */
13551 public Resources getResources() {
13552 return mResources;
13553 }
13554
13555 /**
13556 * Invalidates the specified Drawable.
13557 *
13558 * @param drawable the drawable to invalidate
13559 */
13560 public void invalidateDrawable(Drawable drawable) {
13561 if (verifyDrawable(drawable)) {
13562 final Rect dirty = drawable.getBounds();
13563 final int scrollX = mScrollX;
13564 final int scrollY = mScrollY;
13565
13566 invalidate(dirty.left + scrollX, dirty.top + scrollY,
13567 dirty.right + scrollX, dirty.bottom + scrollY);
13568 }
13569 }
13570
13571 /**
13572 * Schedules an action on a drawable to occur at a specified time.
13573 *
13574 * @param who the recipient of the action
13575 * @param what the action to run on the drawable
13576 * @param when the time at which the action must occur. Uses the
13577 * {@link SystemClock#uptimeMillis} timebase.
13578 */
13579 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080013580 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013581 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080013582 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013583 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
13584 Choreographer.CALLBACK_ANIMATION, what, who,
13585 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080013586 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013587 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080013588 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013589 }
13590 }
13591
13592 /**
13593 * Cancels a scheduled action on a drawable.
13594 *
13595 * @param who the recipient of the action
13596 * @param what the action to cancel
13597 */
13598 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080013599 if (verifyDrawable(who) && what != null) {
13600 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013601 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13602 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080013603 } else {
13604 ViewRootImpl.getRunQueue().removeCallbacks(what);
13605 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013606 }
13607 }
13608
13609 /**
13610 * Unschedule any events associated with the given Drawable. This can be
13611 * used when selecting a new Drawable into a view, so that the previous
13612 * one is completely unscheduled.
13613 *
13614 * @param who The Drawable to unschedule.
13615 *
13616 * @see #drawableStateChanged
13617 */
13618 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080013619 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013620 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13621 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013622 }
13623 }
13624
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013625 /**
13626 * Return the layout direction of a given Drawable.
13627 *
13628 * @param who the Drawable to query
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070013629 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013630 */
13631 public int getResolvedLayoutDirection(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013632 return (who == mBackground) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070013633 }
13634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013635 /**
13636 * If your view subclass is displaying its own Drawable objects, it should
13637 * override this function and return true for any Drawable it is
13638 * displaying. This allows animations for those drawables to be
13639 * scheduled.
13640 *
13641 * <p>Be sure to call through to the super class when overriding this
13642 * function.
13643 *
13644 * @param who The Drawable to verify. Return true if it is one you are
13645 * displaying, else return the result of calling through to the
13646 * super class.
13647 *
13648 * @return boolean If true than the Drawable is being displayed in the
13649 * view; else false and it is not allowed to animate.
13650 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013651 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
13652 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013653 */
13654 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013655 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013656 }
13657
13658 /**
13659 * This function is called whenever the state of the view changes in such
13660 * a way that it impacts the state of drawables being shown.
13661 *
13662 * <p>Be sure to call through to the superclass when overriding this
13663 * function.
13664 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013665 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013666 */
13667 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013668 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013669 if (d != null && d.isStateful()) {
13670 d.setState(getDrawableState());
13671 }
13672 }
13673
13674 /**
13675 * Call this to force a view to update its drawable state. This will cause
13676 * drawableStateChanged to be called on this view. Views that are interested
13677 * in the new state should call getDrawableState.
13678 *
13679 * @see #drawableStateChanged
13680 * @see #getDrawableState
13681 */
13682 public void refreshDrawableState() {
13683 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
13684 drawableStateChanged();
13685
13686 ViewParent parent = mParent;
13687 if (parent != null) {
13688 parent.childDrawableStateChanged(this);
13689 }
13690 }
13691
13692 /**
13693 * Return an array of resource IDs of the drawable states representing the
13694 * current state of the view.
13695 *
13696 * @return The current drawable state
13697 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013698 * @see Drawable#setState(int[])
13699 * @see #drawableStateChanged()
13700 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013701 */
13702 public final int[] getDrawableState() {
13703 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
13704 return mDrawableState;
13705 } else {
13706 mDrawableState = onCreateDrawableState(0);
13707 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
13708 return mDrawableState;
13709 }
13710 }
13711
13712 /**
13713 * Generate the new {@link android.graphics.drawable.Drawable} state for
13714 * this view. This is called by the view
13715 * system when the cached Drawable state is determined to be invalid. To
13716 * retrieve the current state, you should use {@link #getDrawableState}.
13717 *
13718 * @param extraSpace if non-zero, this is the number of extra entries you
13719 * would like in the returned array in which you can place your own
13720 * states.
13721 *
13722 * @return Returns an array holding the current {@link Drawable} state of
13723 * the view.
13724 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013725 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013726 */
13727 protected int[] onCreateDrawableState(int extraSpace) {
13728 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
13729 mParent instanceof View) {
13730 return ((View) mParent).onCreateDrawableState(extraSpace);
13731 }
13732
13733 int[] drawableState;
13734
13735 int privateFlags = mPrivateFlags;
13736
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013737 int viewStateIndex = 0;
13738 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
13739 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
13740 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070013741 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013742 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
13743 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070013744 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
13745 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080013746 // This is set if HW acceleration is requested, even if the current
13747 // process doesn't allow it. This is just to allow app preview
13748 // windows to better match their app.
13749 viewStateIndex |= VIEW_STATE_ACCELERATED;
13750 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070013751 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013752
Christopher Tate3d4bf172011-03-28 16:16:46 -070013753 final int privateFlags2 = mPrivateFlags2;
13754 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
13755 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
13756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013757 drawableState = VIEW_STATE_SETS[viewStateIndex];
13758
13759 //noinspection ConstantIfStatement
13760 if (false) {
13761 Log.i("View", "drawableStateIndex=" + viewStateIndex);
13762 Log.i("View", toString()
13763 + " pressed=" + ((privateFlags & PRESSED) != 0)
13764 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
13765 + " fo=" + hasFocus()
13766 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013767 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013768 + ": " + Arrays.toString(drawableState));
13769 }
13770
13771 if (extraSpace == 0) {
13772 return drawableState;
13773 }
13774
13775 final int[] fullState;
13776 if (drawableState != null) {
13777 fullState = new int[drawableState.length + extraSpace];
13778 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
13779 } else {
13780 fullState = new int[extraSpace];
13781 }
13782
13783 return fullState;
13784 }
13785
13786 /**
13787 * Merge your own state values in <var>additionalState</var> into the base
13788 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070013789 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013790 *
13791 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070013792 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013793 * own additional state values.
13794 *
13795 * @param additionalState The additional state values you would like
13796 * added to <var>baseState</var>; this array is not modified.
13797 *
13798 * @return As a convenience, the <var>baseState</var> array you originally
13799 * passed into the function is returned.
13800 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013801 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013802 */
13803 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
13804 final int N = baseState.length;
13805 int i = N - 1;
13806 while (i >= 0 && baseState[i] == 0) {
13807 i--;
13808 }
13809 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
13810 return baseState;
13811 }
13812
13813 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070013814 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
13815 * on all Drawable objects associated with this view.
13816 */
13817 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013818 if (mBackground != null) {
13819 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070013820 }
13821 }
13822
13823 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013824 * Sets the background color for this view.
13825 * @param color the color of the background
13826 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000013827 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013828 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013829 if (mBackground instanceof ColorDrawable) {
13830 ((ColorDrawable) mBackground).setColor(color);
Chet Haase70d4ba12010-10-06 09:46:45 -070013831 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070013832 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070013833 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013834 }
13835
13836 /**
13837 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070013838 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013839 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070013840 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013841 * @attr ref android.R.styleable#View_background
13842 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000013843 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013844 public void setBackgroundResource(int resid) {
13845 if (resid != 0 && resid == mBackgroundResource) {
13846 return;
13847 }
13848
13849 Drawable d= null;
13850 if (resid != 0) {
13851 d = mResources.getDrawable(resid);
13852 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013853 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013854
13855 mBackgroundResource = resid;
13856 }
13857
13858 /**
13859 * Set the background to a given Drawable, or remove the background. If the
13860 * background has padding, this View's padding is set to the background's
13861 * padding. However, when a background is removed, this View's padding isn't
13862 * touched. If setting the padding is desired, please use
13863 * {@link #setPadding(int, int, int, int)}.
13864 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013865 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013866 * background
13867 */
Philip Milne6c8ea062012-04-03 17:38:43 -070013868 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070013869 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070013870 setBackgroundDrawable(background);
13871 }
13872
13873 /**
13874 * @deprecated use {@link #setBackground(Drawable)} instead
13875 */
13876 @Deprecated
13877 public void setBackgroundDrawable(Drawable background) {
13878 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070013879 return;
13880 }
13881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013882 boolean requestLayout = false;
13883
13884 mBackgroundResource = 0;
13885
13886 /*
13887 * Regardless of whether we're setting a new background or not, we want
13888 * to clear the previous drawable.
13889 */
Philip Milne6c8ea062012-04-03 17:38:43 -070013890 if (mBackground != null) {
13891 mBackground.setCallback(null);
13892 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013893 }
13894
Philip Milne6c8ea062012-04-03 17:38:43 -070013895 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013896 Rect padding = sThreadLocal.get();
13897 if (padding == null) {
13898 padding = new Rect();
13899 sThreadLocal.set(padding);
13900 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013901 if (background.getPadding(padding)) {
13902 switch (background.getResolvedLayoutDirectionSelf()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013903 case LAYOUT_DIRECTION_RTL:
13904 setPadding(padding.right, padding.top, padding.left, padding.bottom);
13905 break;
13906 case LAYOUT_DIRECTION_LTR:
13907 default:
13908 setPadding(padding.left, padding.top, padding.right, padding.bottom);
13909 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013910 }
13911
13912 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
13913 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070013914 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
13915 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013916 requestLayout = true;
13917 }
13918
Philip Milne6c8ea062012-04-03 17:38:43 -070013919 background.setCallback(this);
13920 if (background.isStateful()) {
13921 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013922 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013923 background.setVisible(getVisibility() == VISIBLE, false);
13924 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013925
13926 if ((mPrivateFlags & SKIP_DRAW) != 0) {
13927 mPrivateFlags &= ~SKIP_DRAW;
13928 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
13929 requestLayout = true;
13930 }
13931 } else {
13932 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070013933 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013934
13935 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
13936 /*
13937 * This view ONLY drew the background before and we're removing
13938 * the background, so now it won't draw anything
13939 * (hence we SKIP_DRAW)
13940 */
13941 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
13942 mPrivateFlags |= SKIP_DRAW;
13943 }
13944
13945 /*
13946 * When the background is set, we try to apply its padding to this
13947 * View. When the background is removed, we don't touch this View's
13948 * padding. This is noted in the Javadocs. Hence, we don't need to
13949 * requestLayout(), the invalidate() below is sufficient.
13950 */
13951
13952 // The old background's minimum size could have affected this
13953 // View's layout, so let's requestLayout
13954 requestLayout = true;
13955 }
13956
Romain Guy8f1344f52009-05-15 16:03:59 -070013957 computeOpaqueFlags();
13958
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013959 if (requestLayout) {
13960 requestLayout();
13961 }
13962
13963 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080013964 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013965 }
13966
13967 /**
13968 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070013969 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013970 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070013971 *
13972 * @see #setBackground(Drawable)
13973 *
13974 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013975 */
13976 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013977 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013978 }
13979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013980 /**
13981 * Sets the padding. The view may add on the space required to display
13982 * the scrollbars, depending on the style and visibility of the scrollbars.
13983 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
13984 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
13985 * from the values set in this call.
13986 *
13987 * @attr ref android.R.styleable#View_padding
13988 * @attr ref android.R.styleable#View_paddingBottom
13989 * @attr ref android.R.styleable#View_paddingLeft
13990 * @attr ref android.R.styleable#View_paddingRight
13991 * @attr ref android.R.styleable#View_paddingTop
13992 * @param left the left padding in pixels
13993 * @param top the top padding in pixels
13994 * @param right the right padding in pixels
13995 * @param bottom the bottom padding in pixels
13996 */
13997 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013998 mUserPaddingStart = -1;
13999 mUserPaddingEnd = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014000 mUserPaddingRelative = false;
14001
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014002 internalSetPadding(left, top, right, bottom);
14003 }
14004
14005 private void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080014006 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014007 mUserPaddingRight = right;
14008 mUserPaddingBottom = bottom;
14009
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014010 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014011 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070014012
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014013 // Common case is there are no scroll bars.
14014 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014015 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080014016 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014017 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080014018 switch (mVerticalScrollbarPosition) {
14019 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014020 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
14021 left += offset;
14022 } else {
14023 right += offset;
14024 }
14025 break;
Adam Powell20232d02010-12-08 21:08:53 -080014026 case SCROLLBAR_POSITION_RIGHT:
14027 right += offset;
14028 break;
14029 case SCROLLBAR_POSITION_LEFT:
14030 left += offset;
14031 break;
14032 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014033 }
Adam Powell20232d02010-12-08 21:08:53 -080014034 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014035 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
14036 ? 0 : getHorizontalScrollbarHeight();
14037 }
14038 }
Romain Guy8506ab42009-06-11 17:35:47 -070014039
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014040 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014041 changed = true;
14042 mPaddingLeft = left;
14043 }
14044 if (mPaddingTop != top) {
14045 changed = true;
14046 mPaddingTop = top;
14047 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014048 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014049 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014050 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014051 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014052 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014053 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014054 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014055 }
14056
14057 if (changed) {
14058 requestLayout();
14059 }
14060 }
14061
14062 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014063 * Sets the relative padding. The view may add on the space required to display
14064 * the scrollbars, depending on the style and visibility of the scrollbars.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014065 * from the values set in this call.
14066 *
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014067 * @param start the start padding in pixels
14068 * @param top the top padding in pixels
14069 * @param end the end padding in pixels
14070 * @param bottom the bottom padding in pixels
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014071 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014072 */
14073 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014074 mUserPaddingStart = start;
14075 mUserPaddingEnd = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014076 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014077
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014078 switch(getResolvedLayoutDirection()) {
14079 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014080 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014081 break;
14082 case LAYOUT_DIRECTION_LTR:
14083 default:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014084 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014085 }
14086 }
14087
14088 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014089 * Returns the top padding of this view.
14090 *
14091 * @return the top padding in pixels
14092 */
14093 public int getPaddingTop() {
14094 return mPaddingTop;
14095 }
14096
14097 /**
14098 * Returns the bottom padding of this view. If there are inset and enabled
14099 * scrollbars, this value may include the space required to display the
14100 * scrollbars as well.
14101 *
14102 * @return the bottom padding in pixels
14103 */
14104 public int getPaddingBottom() {
14105 return mPaddingBottom;
14106 }
14107
14108 /**
14109 * Returns the left padding of this view. If there are inset and enabled
14110 * scrollbars, this value may include the space required to display the
14111 * scrollbars as well.
14112 *
14113 * @return the left padding in pixels
14114 */
14115 public int getPaddingLeft() {
14116 return mPaddingLeft;
14117 }
14118
14119 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014120 * Returns the start padding of this view depending on its resolved layout direction.
14121 * If there are inset and enabled scrollbars, this value may include the space
14122 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014123 *
14124 * @return the start padding in pixels
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014125 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014126 */
14127 public int getPaddingStart() {
14128 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14129 mPaddingRight : mPaddingLeft;
14130 }
14131
14132 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014133 * Returns the right padding of this view. If there are inset and enabled
14134 * scrollbars, this value may include the space required to display the
14135 * scrollbars as well.
14136 *
14137 * @return the right padding in pixels
14138 */
14139 public int getPaddingRight() {
14140 return mPaddingRight;
14141 }
14142
14143 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014144 * Returns the end padding of this view depending on its resolved layout direction.
14145 * If there are inset and enabled scrollbars, this value may include the space
14146 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014147 *
14148 * @return the end padding in pixels
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014149 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014150 */
14151 public int getPaddingEnd() {
14152 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14153 mPaddingLeft : mPaddingRight;
14154 }
14155
14156 /**
14157 * Return if the padding as been set thru relative values
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014158 * {@link #setPaddingRelative(int, int, int, int)}
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014159 *
14160 * @return true if the padding is relative or false if it is not.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014161 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014162 */
14163 public boolean isPaddingRelative() {
14164 return mUserPaddingRelative;
14165 }
14166
14167 /**
Philip Milne1557fd72012-04-04 23:41:34 -070014168 * @hide
14169 */
Philip Milne7a23b492012-04-24 22:12:36 -070014170 public Insets getOpticalInsets() {
Philip Milne1557fd72012-04-04 23:41:34 -070014171 if (mLayoutInsets == null) {
Philip Milnebbd51f12012-04-18 15:09:05 -070014172 mLayoutInsets = (mBackground == null) ? Insets.NONE : mBackground.getLayoutInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070014173 }
14174 return mLayoutInsets;
14175 }
14176
14177 /**
14178 * @hide
14179 */
14180 public void setLayoutInsets(Insets layoutInsets) {
14181 mLayoutInsets = layoutInsets;
14182 }
14183
14184 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014185 * Changes the selection state of this view. A view can be selected or not.
14186 * Note that selection is not the same as focus. Views are typically
14187 * selected in the context of an AdapterView like ListView or GridView;
14188 * the selected view is the view that is highlighted.
14189 *
14190 * @param selected true if the view must be selected, false otherwise
14191 */
14192 public void setSelected(boolean selected) {
14193 if (((mPrivateFlags & SELECTED) != 0) != selected) {
14194 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070014195 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080014196 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014197 refreshDrawableState();
14198 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070014199 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
14200 notifyAccessibilityStateChanged();
14201 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014202 }
14203 }
14204
14205 /**
14206 * Dispatch setSelected to all of this View's children.
14207 *
14208 * @see #setSelected(boolean)
14209 *
14210 * @param selected The new selected state
14211 */
14212 protected void dispatchSetSelected(boolean selected) {
14213 }
14214
14215 /**
14216 * Indicates the selection state of this view.
14217 *
14218 * @return true if the view is selected, false otherwise
14219 */
14220 @ViewDebug.ExportedProperty
14221 public boolean isSelected() {
14222 return (mPrivateFlags & SELECTED) != 0;
14223 }
14224
14225 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014226 * Changes the activated state of this view. A view can be activated or not.
14227 * Note that activation is not the same as selection. Selection is
14228 * a transient property, representing the view (hierarchy) the user is
14229 * currently interacting with. Activation is a longer-term state that the
14230 * user can move views in and out of. For example, in a list view with
14231 * single or multiple selection enabled, the views in the current selection
14232 * set are activated. (Um, yeah, we are deeply sorry about the terminology
14233 * here.) The activated state is propagated down to children of the view it
14234 * is set on.
14235 *
14236 * @param activated true if the view must be activated, false otherwise
14237 */
14238 public void setActivated(boolean activated) {
14239 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
14240 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014241 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014242 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070014243 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014244 }
14245 }
14246
14247 /**
14248 * Dispatch setActivated to all of this View's children.
14249 *
14250 * @see #setActivated(boolean)
14251 *
14252 * @param activated The new activated state
14253 */
14254 protected void dispatchSetActivated(boolean activated) {
14255 }
14256
14257 /**
14258 * Indicates the activation state of this view.
14259 *
14260 * @return true if the view is activated, false otherwise
14261 */
14262 @ViewDebug.ExportedProperty
14263 public boolean isActivated() {
14264 return (mPrivateFlags & ACTIVATED) != 0;
14265 }
14266
14267 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014268 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
14269 * observer can be used to get notifications when global events, like
14270 * layout, happen.
14271 *
14272 * The returned ViewTreeObserver observer is not guaranteed to remain
14273 * valid for the lifetime of this View. If the caller of this method keeps
14274 * a long-lived reference to ViewTreeObserver, it should always check for
14275 * the return value of {@link ViewTreeObserver#isAlive()}.
14276 *
14277 * @return The ViewTreeObserver for this view's hierarchy.
14278 */
14279 public ViewTreeObserver getViewTreeObserver() {
14280 if (mAttachInfo != null) {
14281 return mAttachInfo.mTreeObserver;
14282 }
14283 if (mFloatingTreeObserver == null) {
14284 mFloatingTreeObserver = new ViewTreeObserver();
14285 }
14286 return mFloatingTreeObserver;
14287 }
14288
14289 /**
14290 * <p>Finds the topmost view in the current view hierarchy.</p>
14291 *
14292 * @return the topmost view containing this view
14293 */
14294 public View getRootView() {
14295 if (mAttachInfo != null) {
14296 final View v = mAttachInfo.mRootView;
14297 if (v != null) {
14298 return v;
14299 }
14300 }
Romain Guy8506ab42009-06-11 17:35:47 -070014301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014302 View parent = this;
14303
14304 while (parent.mParent != null && parent.mParent instanceof View) {
14305 parent = (View) parent.mParent;
14306 }
14307
14308 return parent;
14309 }
14310
14311 /**
14312 * <p>Computes the coordinates of this view on the screen. The argument
14313 * must be an array of two integers. After the method returns, the array
14314 * contains the x and y location in that order.</p>
14315 *
14316 * @param location an array of two integers in which to hold the coordinates
14317 */
14318 public void getLocationOnScreen(int[] location) {
14319 getLocationInWindow(location);
14320
14321 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070014322 if (info != null) {
14323 location[0] += info.mWindowLeft;
14324 location[1] += info.mWindowTop;
14325 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014326 }
14327
14328 /**
14329 * <p>Computes the coordinates of this view in its window. The argument
14330 * must be an array of two integers. After the method returns, the array
14331 * contains the x and y location in that order.</p>
14332 *
14333 * @param location an array of two integers in which to hold the coordinates
14334 */
14335 public void getLocationInWindow(int[] location) {
14336 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014337 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014338 }
14339
Gilles Debunne6583ce52011-12-06 18:09:02 -080014340 if (mAttachInfo == null) {
14341 // When the view is not attached to a window, this method does not make sense
14342 location[0] = location[1] = 0;
14343 return;
14344 }
14345
Gilles Debunnecea45132011-11-24 02:19:27 +010014346 float[] position = mAttachInfo.mTmpTransformLocation;
14347 position[0] = position[1] = 0.0f;
14348
14349 if (!hasIdentityMatrix()) {
14350 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014351 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014352
Gilles Debunnecea45132011-11-24 02:19:27 +010014353 position[0] += mLeft;
14354 position[1] += mTop;
14355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014356 ViewParent viewParent = mParent;
14357 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014358 final View view = (View) viewParent;
14359
14360 position[0] -= view.mScrollX;
14361 position[1] -= view.mScrollY;
14362
14363 if (!view.hasIdentityMatrix()) {
14364 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014365 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014366
14367 position[0] += view.mLeft;
14368 position[1] += view.mTop;
14369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014370 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070014371 }
Romain Guy8506ab42009-06-11 17:35:47 -070014372
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014373 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014374 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010014375 final ViewRootImpl vr = (ViewRootImpl) viewParent;
14376 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014377 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014378
14379 location[0] = (int) (position[0] + 0.5f);
14380 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014381 }
14382
14383 /**
14384 * {@hide}
14385 * @param id the id of the view to be found
14386 * @return the view of the specified id, null if cannot be found
14387 */
14388 protected View findViewTraversal(int id) {
14389 if (id == mID) {
14390 return this;
14391 }
14392 return null;
14393 }
14394
14395 /**
14396 * {@hide}
14397 * @param tag the tag of the view to be found
14398 * @return the view of specified tag, null if cannot be found
14399 */
14400 protected View findViewWithTagTraversal(Object tag) {
14401 if (tag != null && tag.equals(mTag)) {
14402 return this;
14403 }
14404 return null;
14405 }
14406
14407 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014408 * {@hide}
14409 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070014410 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080014411 * @return The first view that matches the predicate or null.
14412 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070014413 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080014414 if (predicate.apply(this)) {
14415 return this;
14416 }
14417 return null;
14418 }
14419
14420 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014421 * Look for a child view with the given id. If this view has the given
14422 * id, return this view.
14423 *
14424 * @param id The id to search for.
14425 * @return The view that has the given id in the hierarchy or null
14426 */
14427 public final View findViewById(int id) {
14428 if (id < 0) {
14429 return null;
14430 }
14431 return findViewTraversal(id);
14432 }
14433
14434 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070014435 * Finds a view by its unuque and stable accessibility id.
14436 *
14437 * @param accessibilityId The searched accessibility id.
14438 * @return The found view.
14439 */
14440 final View findViewByAccessibilityId(int accessibilityId) {
14441 if (accessibilityId < 0) {
14442 return null;
14443 }
14444 return findViewByAccessibilityIdTraversal(accessibilityId);
14445 }
14446
14447 /**
14448 * Performs the traversal to find a view by its unuque and stable accessibility id.
14449 *
14450 * <strong>Note:</strong>This method does not stop at the root namespace
14451 * boundary since the user can touch the screen at an arbitrary location
14452 * potentially crossing the root namespace bounday which will send an
14453 * accessibility event to accessibility services and they should be able
14454 * to obtain the event source. Also accessibility ids are guaranteed to be
14455 * unique in the window.
14456 *
14457 * @param accessibilityId The accessibility id.
14458 * @return The found view.
14459 */
14460 View findViewByAccessibilityIdTraversal(int accessibilityId) {
14461 if (getAccessibilityViewId() == accessibilityId) {
14462 return this;
14463 }
14464 return null;
14465 }
14466
14467 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014468 * Look for a child view with the given tag. If this view has the given
14469 * tag, return this view.
14470 *
14471 * @param tag The tag to search for, using "tag.equals(getTag())".
14472 * @return The View that has the given tag in the hierarchy or null
14473 */
14474 public final View findViewWithTag(Object tag) {
14475 if (tag == null) {
14476 return null;
14477 }
14478 return findViewWithTagTraversal(tag);
14479 }
14480
14481 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014482 * {@hide}
14483 * Look for a child view that matches the specified predicate.
14484 * If this view matches the predicate, return this view.
14485 *
14486 * @param predicate The predicate to evaluate.
14487 * @return The first view that matches the predicate or null.
14488 */
14489 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070014490 return findViewByPredicateTraversal(predicate, null);
14491 }
14492
14493 /**
14494 * {@hide}
14495 * Look for a child view that matches the specified predicate,
14496 * starting with the specified view and its descendents and then
14497 * recusively searching the ancestors and siblings of that view
14498 * until this view is reached.
14499 *
14500 * This method is useful in cases where the predicate does not match
14501 * a single unique view (perhaps multiple views use the same id)
14502 * and we are trying to find the view that is "closest" in scope to the
14503 * starting view.
14504 *
14505 * @param start The view to start from.
14506 * @param predicate The predicate to evaluate.
14507 * @return The first view that matches the predicate or null.
14508 */
14509 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
14510 View childToSkip = null;
14511 for (;;) {
14512 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
14513 if (view != null || start == this) {
14514 return view;
14515 }
14516
14517 ViewParent parent = start.getParent();
14518 if (parent == null || !(parent instanceof View)) {
14519 return null;
14520 }
14521
14522 childToSkip = start;
14523 start = (View) parent;
14524 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080014525 }
14526
14527 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014528 * Sets the identifier for this view. The identifier does not have to be
14529 * unique in this view's hierarchy. The identifier should be a positive
14530 * number.
14531 *
14532 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070014533 * @see #getId()
14534 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014535 *
14536 * @param id a number used to identify the view
14537 *
14538 * @attr ref android.R.styleable#View_id
14539 */
14540 public void setId(int id) {
14541 mID = id;
14542 }
14543
14544 /**
14545 * {@hide}
14546 *
14547 * @param isRoot true if the view belongs to the root namespace, false
14548 * otherwise
14549 */
14550 public void setIsRootNamespace(boolean isRoot) {
14551 if (isRoot) {
14552 mPrivateFlags |= IS_ROOT_NAMESPACE;
14553 } else {
14554 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
14555 }
14556 }
14557
14558 /**
14559 * {@hide}
14560 *
14561 * @return true if the view belongs to the root namespace, false otherwise
14562 */
14563 public boolean isRootNamespace() {
14564 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
14565 }
14566
14567 /**
14568 * Returns this view's identifier.
14569 *
14570 * @return a positive integer used to identify the view or {@link #NO_ID}
14571 * if the view has no ID
14572 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014573 * @see #setId(int)
14574 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014575 * @attr ref android.R.styleable#View_id
14576 */
14577 @ViewDebug.CapturedViewProperty
14578 public int getId() {
14579 return mID;
14580 }
14581
14582 /**
14583 * Returns this view's tag.
14584 *
14585 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070014586 *
14587 * @see #setTag(Object)
14588 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014589 */
14590 @ViewDebug.ExportedProperty
14591 public Object getTag() {
14592 return mTag;
14593 }
14594
14595 /**
14596 * Sets the tag associated with this view. A tag can be used to mark
14597 * a view in its hierarchy and does not have to be unique within the
14598 * hierarchy. Tags can also be used to store data within a view without
14599 * resorting to another data structure.
14600 *
14601 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070014602 *
14603 * @see #getTag()
14604 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014605 */
14606 public void setTag(final Object tag) {
14607 mTag = tag;
14608 }
14609
14610 /**
Romain Guyd90a3312009-05-06 14:54:28 -070014611 * Returns the tag associated with this view and the specified key.
14612 *
14613 * @param key The key identifying the tag
14614 *
14615 * @return the Object stored in this view as a tag
14616 *
14617 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070014618 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070014619 */
14620 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014621 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070014622 return null;
14623 }
14624
14625 /**
14626 * Sets a tag associated with this view and a key. A tag can be used
14627 * to mark a view in its hierarchy and does not have to be unique within
14628 * the hierarchy. Tags can also be used to store data within a view
14629 * without resorting to another data structure.
14630 *
14631 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070014632 * application to ensure it is unique (see the <a
14633 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
14634 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070014635 * the Android framework or not associated with any package will cause
14636 * an {@link IllegalArgumentException} to be thrown.
14637 *
14638 * @param key The key identifying the tag
14639 * @param tag An Object to tag the view with
14640 *
14641 * @throws IllegalArgumentException If they specified key is not valid
14642 *
14643 * @see #setTag(Object)
14644 * @see #getTag(int)
14645 */
14646 public void setTag(int key, final Object tag) {
14647 // If the package id is 0x00 or 0x01, it's either an undefined package
14648 // or a framework id
14649 if ((key >>> 24) < 2) {
14650 throw new IllegalArgumentException("The key must be an application-specific "
14651 + "resource id.");
14652 }
14653
Adam Powell2b2f6d62011-09-23 11:15:39 -070014654 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014655 }
14656
14657 /**
14658 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
14659 * framework id.
14660 *
14661 * @hide
14662 */
14663 public void setTagInternal(int key, Object tag) {
14664 if ((key >>> 24) != 0x1) {
14665 throw new IllegalArgumentException("The key must be a framework-specific "
14666 + "resource id.");
14667 }
14668
Adam Powell2b2f6d62011-09-23 11:15:39 -070014669 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014670 }
14671
Adam Powell2b2f6d62011-09-23 11:15:39 -070014672 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014673 if (mKeyedTags == null) {
14674 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070014675 }
14676
Adam Powell7db82ac2011-09-22 19:44:04 -070014677 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014678 }
14679
14680 /**
Romain Guy13922e02009-05-12 17:56:14 -070014681 * @param consistency The type of consistency. See ViewDebug for more information.
14682 *
14683 * @hide
14684 */
14685 protected boolean dispatchConsistencyCheck(int consistency) {
14686 return onConsistencyCheck(consistency);
14687 }
14688
14689 /**
14690 * Method that subclasses should implement to check their consistency. The type of
14691 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070014692 *
Romain Guy13922e02009-05-12 17:56:14 -070014693 * @param consistency The type of consistency. See ViewDebug for more information.
14694 *
14695 * @throws IllegalStateException if the view is in an inconsistent state.
14696 *
14697 * @hide
14698 */
14699 protected boolean onConsistencyCheck(int consistency) {
14700 boolean result = true;
14701
14702 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
14703 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
14704
14705 if (checkLayout) {
14706 if (getParent() == null) {
14707 result = false;
14708 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
14709 "View " + this + " does not have a parent.");
14710 }
14711
14712 if (mAttachInfo == null) {
14713 result = false;
14714 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
14715 "View " + this + " is not attached to a window.");
14716 }
14717 }
14718
14719 if (checkDrawing) {
14720 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
14721 // from their draw() method
14722
14723 if ((mPrivateFlags & DRAWN) != DRAWN &&
14724 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
14725 result = false;
14726 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
14727 "View " + this + " was invalidated but its drawing cache is valid.");
14728 }
14729 }
14730
14731 return result;
14732 }
14733
14734 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014735 * Prints information about this view in the log output, with the tag
14736 * {@link #VIEW_LOG_TAG}.
14737 *
14738 * @hide
14739 */
14740 public void debug() {
14741 debug(0);
14742 }
14743
14744 /**
14745 * Prints information about this view in the log output, with the tag
14746 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
14747 * indentation defined by the <code>depth</code>.
14748 *
14749 * @param depth the indentation level
14750 *
14751 * @hide
14752 */
14753 protected void debug(int depth) {
14754 String output = debugIndent(depth - 1);
14755
14756 output += "+ " + this;
14757 int id = getId();
14758 if (id != -1) {
14759 output += " (id=" + id + ")";
14760 }
14761 Object tag = getTag();
14762 if (tag != null) {
14763 output += " (tag=" + tag + ")";
14764 }
14765 Log.d(VIEW_LOG_TAG, output);
14766
14767 if ((mPrivateFlags & FOCUSED) != 0) {
14768 output = debugIndent(depth) + " FOCUSED";
14769 Log.d(VIEW_LOG_TAG, output);
14770 }
14771
14772 output = debugIndent(depth);
14773 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
14774 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
14775 + "} ";
14776 Log.d(VIEW_LOG_TAG, output);
14777
14778 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
14779 || mPaddingBottom != 0) {
14780 output = debugIndent(depth);
14781 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
14782 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
14783 Log.d(VIEW_LOG_TAG, output);
14784 }
14785
14786 output = debugIndent(depth);
14787 output += "mMeasureWidth=" + mMeasuredWidth +
14788 " mMeasureHeight=" + mMeasuredHeight;
14789 Log.d(VIEW_LOG_TAG, output);
14790
14791 output = debugIndent(depth);
14792 if (mLayoutParams == null) {
14793 output += "BAD! no layout params";
14794 } else {
14795 output = mLayoutParams.debug(output);
14796 }
14797 Log.d(VIEW_LOG_TAG, output);
14798
14799 output = debugIndent(depth);
14800 output += "flags={";
14801 output += View.printFlags(mViewFlags);
14802 output += "}";
14803 Log.d(VIEW_LOG_TAG, output);
14804
14805 output = debugIndent(depth);
14806 output += "privateFlags={";
14807 output += View.printPrivateFlags(mPrivateFlags);
14808 output += "}";
14809 Log.d(VIEW_LOG_TAG, output);
14810 }
14811
14812 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090014813 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014814 *
14815 * @param depth the indentation level
14816 * @return a String containing (depth * 2 + 3) * 2 white spaces
14817 *
14818 * @hide
14819 */
14820 protected static String debugIndent(int depth) {
14821 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
14822 for (int i = 0; i < (depth * 2) + 3; i++) {
14823 spaces.append(' ').append(' ');
14824 }
14825 return spaces.toString();
14826 }
14827
14828 /**
14829 * <p>Return the offset of the widget's text baseline from the widget's top
14830 * boundary. If this widget does not support baseline alignment, this
14831 * method returns -1. </p>
14832 *
14833 * @return the offset of the baseline within the widget's bounds or -1
14834 * if baseline alignment is not supported
14835 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070014836 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014837 public int getBaseline() {
14838 return -1;
14839 }
14840
14841 /**
14842 * Call this when something has changed which has invalidated the
14843 * layout of this view. This will schedule a layout pass of the view
14844 * tree.
14845 */
14846 public void requestLayout() {
14847 if (ViewDebug.TRACE_HIERARCHY) {
14848 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
14849 }
14850
14851 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080014852 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014853
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070014854 if (mLayoutParams != null) {
14855 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
14856 }
14857
14858 if (mParent != null && !mParent.isLayoutRequested()) {
14859 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014860 }
14861 }
14862
14863 /**
14864 * Forces this view to be laid out during the next layout pass.
14865 * This method does not call requestLayout() or forceLayout()
14866 * on the parent.
14867 */
14868 public void forceLayout() {
14869 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080014870 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014871 }
14872
14873 /**
14874 * <p>
14875 * This is called to find out how big a view should be. The parent
14876 * supplies constraint information in the width and height parameters.
14877 * </p>
14878 *
14879 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080014880 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014881 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080014882 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014883 * </p>
14884 *
14885 *
14886 * @param widthMeasureSpec Horizontal space requirements as imposed by the
14887 * parent
14888 * @param heightMeasureSpec Vertical space requirements as imposed by the
14889 * parent
14890 *
14891 * @see #onMeasure(int, int)
14892 */
14893 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
14894 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
14895 widthMeasureSpec != mOldWidthMeasureSpec ||
14896 heightMeasureSpec != mOldHeightMeasureSpec) {
14897
14898 // first clears the measured dimension flag
14899 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
14900
14901 if (ViewDebug.TRACE_HIERARCHY) {
14902 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
14903 }
14904
14905 // measure ourselves, this should set the measured dimension flag back
14906 onMeasure(widthMeasureSpec, heightMeasureSpec);
14907
14908 // flag not set, setMeasuredDimension() was not invoked, we raise
14909 // an exception to warn the developer
14910 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
14911 throw new IllegalStateException("onMeasure() did not set the"
14912 + " measured dimension by calling"
14913 + " setMeasuredDimension()");
14914 }
14915
14916 mPrivateFlags |= LAYOUT_REQUIRED;
14917 }
14918
14919 mOldWidthMeasureSpec = widthMeasureSpec;
14920 mOldHeightMeasureSpec = heightMeasureSpec;
14921 }
14922
14923 /**
14924 * <p>
14925 * Measure the view and its content to determine the measured width and the
14926 * measured height. This method is invoked by {@link #measure(int, int)} and
14927 * should be overriden by subclasses to provide accurate and efficient
14928 * measurement of their contents.
14929 * </p>
14930 *
14931 * <p>
14932 * <strong>CONTRACT:</strong> When overriding this method, you
14933 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
14934 * measured width and height of this view. Failure to do so will trigger an
14935 * <code>IllegalStateException</code>, thrown by
14936 * {@link #measure(int, int)}. Calling the superclass'
14937 * {@link #onMeasure(int, int)} is a valid use.
14938 * </p>
14939 *
14940 * <p>
14941 * The base class implementation of measure defaults to the background size,
14942 * unless a larger size is allowed by the MeasureSpec. Subclasses should
14943 * override {@link #onMeasure(int, int)} to provide better measurements of
14944 * their content.
14945 * </p>
14946 *
14947 * <p>
14948 * If this method is overridden, it is the subclass's responsibility to make
14949 * sure the measured height and width are at least the view's minimum height
14950 * and width ({@link #getSuggestedMinimumHeight()} and
14951 * {@link #getSuggestedMinimumWidth()}).
14952 * </p>
14953 *
14954 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
14955 * The requirements are encoded with
14956 * {@link android.view.View.MeasureSpec}.
14957 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
14958 * The requirements are encoded with
14959 * {@link android.view.View.MeasureSpec}.
14960 *
14961 * @see #getMeasuredWidth()
14962 * @see #getMeasuredHeight()
14963 * @see #setMeasuredDimension(int, int)
14964 * @see #getSuggestedMinimumHeight()
14965 * @see #getSuggestedMinimumWidth()
14966 * @see android.view.View.MeasureSpec#getMode(int)
14967 * @see android.view.View.MeasureSpec#getSize(int)
14968 */
14969 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
14970 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
14971 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
14972 }
14973
14974 /**
14975 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
14976 * measured width and measured height. Failing to do so will trigger an
14977 * exception at measurement time.</p>
14978 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080014979 * @param measuredWidth The measured width of this view. May be a complex
14980 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
14981 * {@link #MEASURED_STATE_TOO_SMALL}.
14982 * @param measuredHeight The measured height of this view. May be a complex
14983 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
14984 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014985 */
14986 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
14987 mMeasuredWidth = measuredWidth;
14988 mMeasuredHeight = measuredHeight;
14989
14990 mPrivateFlags |= MEASURED_DIMENSION_SET;
14991 }
14992
14993 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080014994 * Merge two states as returned by {@link #getMeasuredState()}.
14995 * @param curState The current state as returned from a view or the result
14996 * of combining multiple views.
14997 * @param newState The new view state to combine.
14998 * @return Returns a new integer reflecting the combination of the two
14999 * states.
15000 */
15001 public static int combineMeasuredStates(int curState, int newState) {
15002 return curState | newState;
15003 }
15004
15005 /**
15006 * Version of {@link #resolveSizeAndState(int, int, int)}
15007 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
15008 */
15009 public static int resolveSize(int size, int measureSpec) {
15010 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
15011 }
15012
15013 /**
15014 * Utility to reconcile a desired size and state, with constraints imposed
15015 * by a MeasureSpec. Will take the desired size, unless a different size
15016 * is imposed by the constraints. The returned value is a compound integer,
15017 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
15018 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
15019 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015020 *
15021 * @param size How big the view wants to be
15022 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080015023 * @return Size information bit mask as defined by
15024 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015025 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080015026 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015027 int result = size;
15028 int specMode = MeasureSpec.getMode(measureSpec);
15029 int specSize = MeasureSpec.getSize(measureSpec);
15030 switch (specMode) {
15031 case MeasureSpec.UNSPECIFIED:
15032 result = size;
15033 break;
15034 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080015035 if (specSize < size) {
15036 result = specSize | MEASURED_STATE_TOO_SMALL;
15037 } else {
15038 result = size;
15039 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015040 break;
15041 case MeasureSpec.EXACTLY:
15042 result = specSize;
15043 break;
15044 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080015045 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015046 }
15047
15048 /**
15049 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070015050 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015051 * by the MeasureSpec.
15052 *
15053 * @param size Default size for this view
15054 * @param measureSpec Constraints imposed by the parent
15055 * @return The size this view should be.
15056 */
15057 public static int getDefaultSize(int size, int measureSpec) {
15058 int result = size;
15059 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070015060 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015061
15062 switch (specMode) {
15063 case MeasureSpec.UNSPECIFIED:
15064 result = size;
15065 break;
15066 case MeasureSpec.AT_MOST:
15067 case MeasureSpec.EXACTLY:
15068 result = specSize;
15069 break;
15070 }
15071 return result;
15072 }
15073
15074 /**
15075 * Returns the suggested minimum height that the view should use. This
15076 * returns the maximum of the view's minimum height
15077 * and the background's minimum height
15078 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
15079 * <p>
15080 * When being used in {@link #onMeasure(int, int)}, the caller should still
15081 * ensure the returned height is within the requirements of the parent.
15082 *
15083 * @return The suggested minimum height of the view.
15084 */
15085 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015086 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015088 }
15089
15090 /**
15091 * Returns the suggested minimum width that the view should use. This
15092 * returns the maximum of the view's minimum width)
15093 * and the background's minimum width
15094 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
15095 * <p>
15096 * When being used in {@link #onMeasure(int, int)}, the caller should still
15097 * ensure the returned width is within the requirements of the parent.
15098 *
15099 * @return The suggested minimum width of the view.
15100 */
15101 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015102 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
15103 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015104
Philip Milne6c8ea062012-04-03 17:38:43 -070015105 /**
15106 * Returns the minimum height of the view.
15107 *
15108 * @return the minimum height the view will try to be.
15109 *
15110 * @see #setMinimumHeight(int)
15111 *
15112 * @attr ref android.R.styleable#View_minHeight
15113 */
15114 public int getMinimumHeight() {
15115 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015116 }
15117
15118 /**
15119 * Sets the minimum height of the view. It is not guaranteed the view will
15120 * be able to achieve this minimum height (for example, if its parent layout
15121 * constrains it with less available height).
15122 *
15123 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015124 *
15125 * @see #getMinimumHeight()
15126 *
15127 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015128 */
15129 public void setMinimumHeight(int minHeight) {
15130 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070015131 requestLayout();
15132 }
15133
15134 /**
15135 * Returns the minimum width of the view.
15136 *
15137 * @return the minimum width the view will try to be.
15138 *
15139 * @see #setMinimumWidth(int)
15140 *
15141 * @attr ref android.R.styleable#View_minWidth
15142 */
15143 public int getMinimumWidth() {
15144 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015145 }
15146
15147 /**
15148 * Sets the minimum width of the view. It is not guaranteed the view will
15149 * be able to achieve this minimum width (for example, if its parent layout
15150 * constrains it with less available width).
15151 *
15152 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015153 *
15154 * @see #getMinimumWidth()
15155 *
15156 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015157 */
15158 public void setMinimumWidth(int minWidth) {
15159 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070015160 requestLayout();
15161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015162 }
15163
15164 /**
15165 * Get the animation currently associated with this view.
15166 *
15167 * @return The animation that is currently playing or
15168 * scheduled to play for this view.
15169 */
15170 public Animation getAnimation() {
15171 return mCurrentAnimation;
15172 }
15173
15174 /**
15175 * Start the specified animation now.
15176 *
15177 * @param animation the animation to start now
15178 */
15179 public void startAnimation(Animation animation) {
15180 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
15181 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080015182 invalidateParentCaches();
15183 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015184 }
15185
15186 /**
15187 * Cancels any animations for this view.
15188 */
15189 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080015190 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080015191 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080015192 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015193 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080015194 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015195 }
15196
15197 /**
15198 * Sets the next animation to play for this view.
15199 * If you want the animation to play immediately, use
15200 * startAnimation. This method provides allows fine-grained
15201 * control over the start time and invalidation, but you
15202 * must make sure that 1) the animation has a start time set, and
15203 * 2) the view will be invalidated when the animation is supposed to
15204 * start.
15205 *
15206 * @param animation The next animation, or null.
15207 */
15208 public void setAnimation(Animation animation) {
15209 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070015210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015211 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070015212 // If the screen is off assume the animation start time is now instead of
15213 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
15214 // would cause the animation to start when the screen turns back on
15215 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
15216 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
15217 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
15218 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015219 animation.reset();
15220 }
15221 }
15222
15223 /**
15224 * Invoked by a parent ViewGroup to notify the start of the animation
15225 * currently associated with this view. If you override this method,
15226 * always call super.onAnimationStart();
15227 *
15228 * @see #setAnimation(android.view.animation.Animation)
15229 * @see #getAnimation()
15230 */
15231 protected void onAnimationStart() {
15232 mPrivateFlags |= ANIMATION_STARTED;
15233 }
15234
15235 /**
15236 * Invoked by a parent ViewGroup to notify the end of the animation
15237 * currently associated with this view. If you override this method,
15238 * always call super.onAnimationEnd();
15239 *
15240 * @see #setAnimation(android.view.animation.Animation)
15241 * @see #getAnimation()
15242 */
15243 protected void onAnimationEnd() {
15244 mPrivateFlags &= ~ANIMATION_STARTED;
15245 }
15246
15247 /**
15248 * Invoked if there is a Transform that involves alpha. Subclass that can
15249 * draw themselves with the specified alpha should return true, and then
15250 * respect that alpha when their onDraw() is called. If this returns false
15251 * then the view may be redirected to draw into an offscreen buffer to
15252 * fulfill the request, which will look fine, but may be slower than if the
15253 * subclass handles it internally. The default implementation returns false.
15254 *
15255 * @param alpha The alpha (0..255) to apply to the view's drawing
15256 * @return true if the view can draw with the specified alpha.
15257 */
15258 protected boolean onSetAlpha(int alpha) {
15259 return false;
15260 }
15261
15262 /**
15263 * This is used by the RootView to perform an optimization when
15264 * the view hierarchy contains one or several SurfaceView.
15265 * SurfaceView is always considered transparent, but its children are not,
15266 * therefore all View objects remove themselves from the global transparent
15267 * region (passed as a parameter to this function).
15268 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015269 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015270 *
15271 * @return Returns true if the effective visibility of the view at this
15272 * point is opaque, regardless of the transparent region; returns false
15273 * if it is possible for underlying windows to be seen behind the view.
15274 *
15275 * {@hide}
15276 */
15277 public boolean gatherTransparentRegion(Region region) {
15278 final AttachInfo attachInfo = mAttachInfo;
15279 if (region != null && attachInfo != null) {
15280 final int pflags = mPrivateFlags;
15281 if ((pflags & SKIP_DRAW) == 0) {
15282 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
15283 // remove it from the transparent region.
15284 final int[] location = attachInfo.mTransparentLocation;
15285 getLocationInWindow(location);
15286 region.op(location[0], location[1], location[0] + mRight - mLeft,
15287 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Philip Milne6c8ea062012-04-03 17:38:43 -070015288 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015289 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
15290 // exists, so we remove the background drawable's non-transparent
15291 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070015292 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015293 }
15294 }
15295 return true;
15296 }
15297
15298 /**
15299 * Play a sound effect for this view.
15300 *
15301 * <p>The framework will play sound effects for some built in actions, such as
15302 * clicking, but you may wish to play these effects in your widget,
15303 * for instance, for internal navigation.
15304 *
15305 * <p>The sound effect will only be played if sound effects are enabled by the user, and
15306 * {@link #isSoundEffectsEnabled()} is true.
15307 *
15308 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
15309 */
15310 public void playSoundEffect(int soundConstant) {
15311 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
15312 return;
15313 }
15314 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
15315 }
15316
15317 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015318 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015319 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015320 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015321 *
15322 * <p>The framework will provide haptic feedback for some built in actions,
15323 * such as long presses, but you may wish to provide feedback for your
15324 * own widget.
15325 *
15326 * <p>The feedback will only be performed if
15327 * {@link #isHapticFeedbackEnabled()} is true.
15328 *
15329 * @param feedbackConstant One of the constants defined in
15330 * {@link HapticFeedbackConstants}
15331 */
15332 public boolean performHapticFeedback(int feedbackConstant) {
15333 return performHapticFeedback(feedbackConstant, 0);
15334 }
15335
15336 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015337 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015338 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015339 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015340 *
15341 * @param feedbackConstant One of the constants defined in
15342 * {@link HapticFeedbackConstants}
15343 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
15344 */
15345 public boolean performHapticFeedback(int feedbackConstant, int flags) {
15346 if (mAttachInfo == null) {
15347 return false;
15348 }
Romain Guyf607bdc2010-09-10 19:20:06 -070015349 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070015350 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015351 && !isHapticFeedbackEnabled()) {
15352 return false;
15353 }
Romain Guy812ccbe2010-06-01 14:07:24 -070015354 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
15355 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015356 }
15357
15358 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015359 * Request that the visibility of the status bar or other screen/window
15360 * decorations be changed.
15361 *
15362 * <p>This method is used to put the over device UI into temporary modes
15363 * where the user's attention is focused more on the application content,
15364 * by dimming or hiding surrounding system affordances. This is typically
15365 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
15366 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
15367 * to be placed behind the action bar (and with these flags other system
15368 * affordances) so that smooth transitions between hiding and showing them
15369 * can be done.
15370 *
15371 * <p>Two representative examples of the use of system UI visibility is
15372 * implementing a content browsing application (like a magazine reader)
15373 * and a video playing application.
15374 *
15375 * <p>The first code shows a typical implementation of a View in a content
15376 * browsing application. In this implementation, the application goes
15377 * into a content-oriented mode by hiding the status bar and action bar,
15378 * and putting the navigation elements into lights out mode. The user can
15379 * then interact with content while in this mode. Such an application should
15380 * provide an easy way for the user to toggle out of the mode (such as to
15381 * check information in the status bar or access notifications). In the
15382 * implementation here, this is done simply by tapping on the content.
15383 *
15384 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
15385 * content}
15386 *
15387 * <p>This second code sample shows a typical implementation of a View
15388 * in a video playing application. In this situation, while the video is
15389 * playing the application would like to go into a complete full-screen mode,
15390 * to use as much of the display as possible for the video. When in this state
15391 * the user can not interact with the application; the system intercepts
Dianne Hackborncf675782012-05-10 15:07:24 -070015392 * touching on the screen to pop the UI out of full screen mode. See
15393 * {@link #fitSystemWindows(Rect)} for a sample layout that goes with this code.
Dianne Hackborn98014352012-04-05 18:31:41 -070015394 *
15395 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
15396 * content}
15397 *
15398 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15399 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15400 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15401 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015402 */
15403 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040015404 if (visibility != mSystemUiVisibility) {
15405 mSystemUiVisibility = visibility;
15406 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15407 mParent.recomputeViewAttributes(this);
15408 }
Joe Onorato664644d2011-01-23 17:53:23 -080015409 }
15410 }
15411
15412 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015413 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
15414 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15415 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15416 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15417 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015418 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080015419 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080015420 return mSystemUiVisibility;
15421 }
15422
Scott Mainec6331b2011-05-24 16:55:56 -070015423 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070015424 * Returns the current system UI visibility that is currently set for
15425 * the entire window. This is the combination of the
15426 * {@link #setSystemUiVisibility(int)} values supplied by all of the
15427 * views in the window.
15428 */
15429 public int getWindowSystemUiVisibility() {
15430 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
15431 }
15432
15433 /**
15434 * Override to find out when the window's requested system UI visibility
15435 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
15436 * This is different from the callbacks recieved through
15437 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
15438 * in that this is only telling you about the local request of the window,
15439 * not the actual values applied by the system.
15440 */
15441 public void onWindowSystemUiVisibilityChanged(int visible) {
15442 }
15443
15444 /**
15445 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
15446 * the view hierarchy.
15447 */
15448 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
15449 onWindowSystemUiVisibilityChanged(visible);
15450 }
15451
15452 /**
Scott Mainec6331b2011-05-24 16:55:56 -070015453 * Set a listener to receive callbacks when the visibility of the system bar changes.
15454 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
15455 */
Joe Onorato664644d2011-01-23 17:53:23 -080015456 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015457 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080015458 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15459 mParent.recomputeViewAttributes(this);
15460 }
15461 }
15462
15463 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015464 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
15465 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080015466 */
15467 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015468 ListenerInfo li = mListenerInfo;
15469 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
15470 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080015471 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080015472 }
15473 }
15474
Dianne Hackborncf675782012-05-10 15:07:24 -070015475 boolean updateLocalSystemUiVisibility(int localValue, int localChanges) {
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015476 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
15477 if (val != mSystemUiVisibility) {
15478 setSystemUiVisibility(val);
Dianne Hackborncf675782012-05-10 15:07:24 -070015479 return true;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015480 }
Dianne Hackborncf675782012-05-10 15:07:24 -070015481 return false;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015482 }
15483
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070015484 /** @hide */
15485 public void setDisabledSystemUiVisibility(int flags) {
15486 if (mAttachInfo != null) {
15487 if (mAttachInfo.mDisabledSystemUiVisibility != flags) {
15488 mAttachInfo.mDisabledSystemUiVisibility = flags;
15489 if (mParent != null) {
15490 mParent.recomputeViewAttributes(this);
15491 }
15492 }
15493 }
15494 }
15495
Joe Onorato664644d2011-01-23 17:53:23 -080015496 /**
Joe Malin32736f02011-01-19 16:14:20 -080015497 * Creates an image that the system displays during the drag and drop
15498 * operation. This is called a &quot;drag shadow&quot;. The default implementation
15499 * for a DragShadowBuilder based on a View returns an image that has exactly the same
15500 * appearance as the given View. The default also positions the center of the drag shadow
15501 * directly under the touch point. If no View is provided (the constructor with no parameters
15502 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
15503 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
15504 * default is an invisible drag shadow.
15505 * <p>
15506 * You are not required to use the View you provide to the constructor as the basis of the
15507 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
15508 * anything you want as the drag shadow.
15509 * </p>
15510 * <p>
15511 * You pass a DragShadowBuilder object to the system when you start the drag. The system
15512 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
15513 * size and position of the drag shadow. It uses this data to construct a
15514 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
15515 * so that your application can draw the shadow image in the Canvas.
15516 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070015517 *
15518 * <div class="special reference">
15519 * <h3>Developer Guides</h3>
15520 * <p>For a guide to implementing drag and drop features, read the
15521 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
15522 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070015523 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015524 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070015525 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070015526
15527 /**
Joe Malin32736f02011-01-19 16:14:20 -080015528 * Constructs a shadow image builder based on a View. By default, the resulting drag
15529 * shadow will have the same appearance and dimensions as the View, with the touch point
15530 * over the center of the View.
15531 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070015532 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015533 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070015534 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070015535 }
15536
Christopher Tate17ed60c2011-01-18 12:50:26 -080015537 /**
15538 * Construct a shadow builder object with no associated View. This
15539 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
15540 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
15541 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080015542 * reference to any View object. If they are not overridden, then the result is an
15543 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080015544 */
15545 public DragShadowBuilder() {
15546 mView = new WeakReference<View>(null);
15547 }
15548
15549 /**
15550 * Returns the View object that had been passed to the
15551 * {@link #View.DragShadowBuilder(View)}
15552 * constructor. If that View parameter was {@code null} or if the
15553 * {@link #View.DragShadowBuilder()}
15554 * constructor was used to instantiate the builder object, this method will return
15555 * null.
15556 *
15557 * @return The View object associate with this builder object.
15558 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070015559 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070015560 final public View getView() {
15561 return mView.get();
15562 }
15563
Christopher Tate2c095f32010-10-04 14:13:40 -070015564 /**
Joe Malin32736f02011-01-19 16:14:20 -080015565 * Provides the metrics for the shadow image. These include the dimensions of
15566 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070015567 * be centered under the touch location while dragging.
15568 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015569 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080015570 * same as the dimensions of the View itself and centers the shadow under
15571 * the touch point.
15572 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070015573 *
Joe Malin32736f02011-01-19 16:14:20 -080015574 * @param shadowSize A {@link android.graphics.Point} containing the width and height
15575 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
15576 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
15577 * image.
15578 *
15579 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
15580 * shadow image that should be underneath the touch point during the drag and drop
15581 * operation. Your application must set {@link android.graphics.Point#x} to the
15582 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070015583 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015584 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070015585 final View view = mView.get();
15586 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015587 shadowSize.set(view.getWidth(), view.getHeight());
15588 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070015589 } else {
15590 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
15591 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015592 }
15593
15594 /**
Joe Malin32736f02011-01-19 16:14:20 -080015595 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
15596 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015597 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070015598 *
Joe Malin32736f02011-01-19 16:14:20 -080015599 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070015600 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015601 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070015602 final View view = mView.get();
15603 if (view != null) {
15604 view.draw(canvas);
15605 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015606 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070015607 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015608 }
15609 }
15610
15611 /**
Joe Malin32736f02011-01-19 16:14:20 -080015612 * Starts a drag and drop operation. When your application calls this method, it passes a
15613 * {@link android.view.View.DragShadowBuilder} object to the system. The
15614 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
15615 * to get metrics for the drag shadow, and then calls the object's
15616 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
15617 * <p>
15618 * Once the system has the drag shadow, it begins the drag and drop operation by sending
15619 * drag events to all the View objects in your application that are currently visible. It does
15620 * this either by calling the View object's drag listener (an implementation of
15621 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
15622 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
15623 * Both are passed a {@link android.view.DragEvent} object that has a
15624 * {@link android.view.DragEvent#getAction()} value of
15625 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
15626 * </p>
15627 * <p>
15628 * Your application can invoke startDrag() on any attached View object. The View object does not
15629 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
15630 * be related to the View the user selected for dragging.
15631 * </p>
15632 * @param data A {@link android.content.ClipData} object pointing to the data to be
15633 * transferred by the drag and drop operation.
15634 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
15635 * drag shadow.
15636 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
15637 * drop operation. This Object is put into every DragEvent object sent by the system during the
15638 * current drag.
15639 * <p>
15640 * myLocalState is a lightweight mechanism for the sending information from the dragged View
15641 * to the target Views. For example, it can contain flags that differentiate between a
15642 * a copy operation and a move operation.
15643 * </p>
15644 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
15645 * so the parameter should be set to 0.
15646 * @return {@code true} if the method completes successfully, or
15647 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
15648 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070015649 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015650 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015651 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070015652 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015653 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070015654 }
15655 boolean okay = false;
15656
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015657 Point shadowSize = new Point();
15658 Point shadowTouchPoint = new Point();
15659 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070015660
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015661 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
15662 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
15663 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070015664 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015665
Chris Tatea32dcf72010-10-14 12:13:50 -070015666 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015667 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
15668 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070015669 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015670 Surface surface = new Surface();
15671 try {
15672 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015673 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070015674 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070015675 + " surface=" + surface);
15676 if (token != null) {
15677 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070015678 try {
Chris Tate6b391282010-10-14 15:48:59 -070015679 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015680 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070015681 } finally {
15682 surface.unlockCanvasAndPost(canvas);
15683 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015684
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015685 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080015686
15687 // Cache the local state object for delivery with DragEvents
15688 root.setLocalDragState(myLocalState);
15689
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015690 // repurpose 'shadowSize' for the last touch point
15691 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070015692
Christopher Tatea53146c2010-09-07 11:57:52 -070015693 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015694 shadowSize.x, shadowSize.y,
15695 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070015696 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070015697
15698 // Off and running! Release our local surface instance; the drag
15699 // shadow surface is now managed by the system process.
15700 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070015701 }
15702 } catch (Exception e) {
15703 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
15704 surface.destroy();
15705 }
15706
15707 return okay;
15708 }
15709
Christopher Tatea53146c2010-09-07 11:57:52 -070015710 /**
Joe Malin32736f02011-01-19 16:14:20 -080015711 * Handles drag events sent by the system following a call to
15712 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
15713 *<p>
15714 * When the system calls this method, it passes a
15715 * {@link android.view.DragEvent} object. A call to
15716 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
15717 * in DragEvent. The method uses these to determine what is happening in the drag and drop
15718 * operation.
15719 * @param event The {@link android.view.DragEvent} sent by the system.
15720 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
15721 * in DragEvent, indicating the type of drag event represented by this object.
15722 * @return {@code true} if the method was successful, otherwise {@code false}.
15723 * <p>
15724 * The method should return {@code true} in response to an action type of
15725 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
15726 * operation.
15727 * </p>
15728 * <p>
15729 * The method should also return {@code true} in response to an action type of
15730 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
15731 * {@code false} if it didn't.
15732 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015733 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070015734 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070015735 return false;
15736 }
15737
15738 /**
Joe Malin32736f02011-01-19 16:14:20 -080015739 * Detects if this View is enabled and has a drag event listener.
15740 * If both are true, then it calls the drag event listener with the
15741 * {@link android.view.DragEvent} it received. If the drag event listener returns
15742 * {@code true}, then dispatchDragEvent() returns {@code true}.
15743 * <p>
15744 * For all other cases, the method calls the
15745 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
15746 * method and returns its result.
15747 * </p>
15748 * <p>
15749 * This ensures that a drag event is always consumed, even if the View does not have a drag
15750 * event listener. However, if the View has a listener and the listener returns true, then
15751 * onDragEvent() is not called.
15752 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015753 */
15754 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080015755 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015756 ListenerInfo li = mListenerInfo;
15757 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
15758 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070015759 return true;
15760 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015761 return onDragEvent(event);
15762 }
15763
Christopher Tate3d4bf172011-03-28 16:16:46 -070015764 boolean canAcceptDrag() {
15765 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
15766 }
15767
Christopher Tatea53146c2010-09-07 11:57:52 -070015768 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070015769 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
15770 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070015771 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070015772 */
15773 public void onCloseSystemDialogs(String reason) {
15774 }
Joe Malin32736f02011-01-19 16:14:20 -080015775
Dianne Hackbornffa42482009-09-23 22:20:11 -070015776 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015777 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070015778 * update a Region being computed for
15779 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015780 * that any non-transparent parts of the Drawable are removed from the
15781 * given transparent region.
15782 *
15783 * @param dr The Drawable whose transparency is to be applied to the region.
15784 * @param region A Region holding the current transparency information,
15785 * where any parts of the region that are set are considered to be
15786 * transparent. On return, this region will be modified to have the
15787 * transparency information reduced by the corresponding parts of the
15788 * Drawable that are not transparent.
15789 * {@hide}
15790 */
15791 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
15792 if (DBG) {
15793 Log.i("View", "Getting transparent region for: " + this);
15794 }
15795 final Region r = dr.getTransparentRegion();
15796 final Rect db = dr.getBounds();
15797 final AttachInfo attachInfo = mAttachInfo;
15798 if (r != null && attachInfo != null) {
15799 final int w = getRight()-getLeft();
15800 final int h = getBottom()-getTop();
15801 if (db.left > 0) {
15802 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
15803 r.op(0, 0, db.left, h, Region.Op.UNION);
15804 }
15805 if (db.right < w) {
15806 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
15807 r.op(db.right, 0, w, h, Region.Op.UNION);
15808 }
15809 if (db.top > 0) {
15810 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
15811 r.op(0, 0, w, db.top, Region.Op.UNION);
15812 }
15813 if (db.bottom < h) {
15814 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
15815 r.op(0, db.bottom, w, h, Region.Op.UNION);
15816 }
15817 final int[] location = attachInfo.mTransparentLocation;
15818 getLocationInWindow(location);
15819 r.translate(location[0], location[1]);
15820 region.op(r, Region.Op.INTERSECT);
15821 } else {
15822 region.op(db, Region.Op.DIFFERENCE);
15823 }
15824 }
15825
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015826 private void checkForLongClick(int delayOffset) {
15827 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
15828 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015829
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015830 if (mPendingCheckForLongPress == null) {
15831 mPendingCheckForLongPress = new CheckForLongPress();
15832 }
15833 mPendingCheckForLongPress.rememberWindowAttachCount();
15834 postDelayed(mPendingCheckForLongPress,
15835 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015836 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015837 }
15838
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015839 /**
15840 * Inflate a view from an XML resource. This convenience method wraps the {@link
15841 * LayoutInflater} class, which provides a full range of options for view inflation.
15842 *
15843 * @param context The Context object for your activity or application.
15844 * @param resource The resource ID to inflate
15845 * @param root A view group that will be the parent. Used to properly inflate the
15846 * layout_* parameters.
15847 * @see LayoutInflater
15848 */
15849 public static View inflate(Context context, int resource, ViewGroup root) {
15850 LayoutInflater factory = LayoutInflater.from(context);
15851 return factory.inflate(resource, root);
15852 }
Romain Guy33e72ae2010-07-17 12:40:29 -070015853
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015854 /**
Adam Powell637d3372010-08-25 14:37:03 -070015855 * Scroll the view with standard behavior for scrolling beyond the normal
15856 * content boundaries. Views that call this method should override
15857 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
15858 * results of an over-scroll operation.
15859 *
15860 * Views can use this method to handle any touch or fling-based scrolling.
15861 *
15862 * @param deltaX Change in X in pixels
15863 * @param deltaY Change in Y in pixels
15864 * @param scrollX Current X scroll value in pixels before applying deltaX
15865 * @param scrollY Current Y scroll value in pixels before applying deltaY
15866 * @param scrollRangeX Maximum content scroll range along the X axis
15867 * @param scrollRangeY Maximum content scroll range along the Y axis
15868 * @param maxOverScrollX Number of pixels to overscroll by in either direction
15869 * along the X axis.
15870 * @param maxOverScrollY Number of pixels to overscroll by in either direction
15871 * along the Y axis.
15872 * @param isTouchEvent true if this scroll operation is the result of a touch event.
15873 * @return true if scrolling was clamped to an over-scroll boundary along either
15874 * axis, false otherwise.
15875 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070015876 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070015877 protected boolean overScrollBy(int deltaX, int deltaY,
15878 int scrollX, int scrollY,
15879 int scrollRangeX, int scrollRangeY,
15880 int maxOverScrollX, int maxOverScrollY,
15881 boolean isTouchEvent) {
15882 final int overScrollMode = mOverScrollMode;
15883 final boolean canScrollHorizontal =
15884 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
15885 final boolean canScrollVertical =
15886 computeVerticalScrollRange() > computeVerticalScrollExtent();
15887 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
15888 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
15889 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
15890 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
15891
15892 int newScrollX = scrollX + deltaX;
15893 if (!overScrollHorizontal) {
15894 maxOverScrollX = 0;
15895 }
15896
15897 int newScrollY = scrollY + deltaY;
15898 if (!overScrollVertical) {
15899 maxOverScrollY = 0;
15900 }
15901
15902 // Clamp values if at the limits and record
15903 final int left = -maxOverScrollX;
15904 final int right = maxOverScrollX + scrollRangeX;
15905 final int top = -maxOverScrollY;
15906 final int bottom = maxOverScrollY + scrollRangeY;
15907
15908 boolean clampedX = false;
15909 if (newScrollX > right) {
15910 newScrollX = right;
15911 clampedX = true;
15912 } else if (newScrollX < left) {
15913 newScrollX = left;
15914 clampedX = true;
15915 }
15916
15917 boolean clampedY = false;
15918 if (newScrollY > bottom) {
15919 newScrollY = bottom;
15920 clampedY = true;
15921 } else if (newScrollY < top) {
15922 newScrollY = top;
15923 clampedY = true;
15924 }
15925
15926 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
15927
15928 return clampedX || clampedY;
15929 }
15930
15931 /**
15932 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
15933 * respond to the results of an over-scroll operation.
15934 *
15935 * @param scrollX New X scroll value in pixels
15936 * @param scrollY New Y scroll value in pixels
15937 * @param clampedX True if scrollX was clamped to an over-scroll boundary
15938 * @param clampedY True if scrollY was clamped to an over-scroll boundary
15939 */
15940 protected void onOverScrolled(int scrollX, int scrollY,
15941 boolean clampedX, boolean clampedY) {
15942 // Intentionally empty.
15943 }
15944
15945 /**
15946 * Returns the over-scroll mode for this view. The result will be
15947 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
15948 * (allow over-scrolling only if the view content is larger than the container),
15949 * or {@link #OVER_SCROLL_NEVER}.
15950 *
15951 * @return This view's over-scroll mode.
15952 */
15953 public int getOverScrollMode() {
15954 return mOverScrollMode;
15955 }
15956
15957 /**
15958 * Set the over-scroll mode for this view. Valid over-scroll modes are
15959 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
15960 * (allow over-scrolling only if the view content is larger than the container),
15961 * or {@link #OVER_SCROLL_NEVER}.
15962 *
15963 * Setting the over-scroll mode of a view will have an effect only if the
15964 * view is capable of scrolling.
15965 *
15966 * @param overScrollMode The new over-scroll mode for this view.
15967 */
15968 public void setOverScrollMode(int overScrollMode) {
15969 if (overScrollMode != OVER_SCROLL_ALWAYS &&
15970 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
15971 overScrollMode != OVER_SCROLL_NEVER) {
15972 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
15973 }
15974 mOverScrollMode = overScrollMode;
15975 }
15976
15977 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080015978 * Gets a scale factor that determines the distance the view should scroll
15979 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
15980 * @return The vertical scroll scale factor.
15981 * @hide
15982 */
15983 protected float getVerticalScrollFactor() {
15984 if (mVerticalScrollFactor == 0) {
15985 TypedValue outValue = new TypedValue();
15986 if (!mContext.getTheme().resolveAttribute(
15987 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
15988 throw new IllegalStateException(
15989 "Expected theme to define listPreferredItemHeight.");
15990 }
15991 mVerticalScrollFactor = outValue.getDimension(
15992 mContext.getResources().getDisplayMetrics());
15993 }
15994 return mVerticalScrollFactor;
15995 }
15996
15997 /**
15998 * Gets a scale factor that determines the distance the view should scroll
15999 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
16000 * @return The horizontal scroll scale factor.
16001 * @hide
16002 */
16003 protected float getHorizontalScrollFactor() {
16004 // TODO: Should use something else.
16005 return getVerticalScrollFactor();
16006 }
16007
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016008 /**
16009 * Return the value specifying the text direction or policy that was set with
16010 * {@link #setTextDirection(int)}.
16011 *
16012 * @return the defined text direction. It can be one of:
16013 *
16014 * {@link #TEXT_DIRECTION_INHERIT},
16015 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16016 * {@link #TEXT_DIRECTION_ANY_RTL},
16017 * {@link #TEXT_DIRECTION_LTR},
16018 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016019 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016020 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016021 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016022 @ViewDebug.ExportedProperty(category = "text", mapping = {
16023 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
16024 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
16025 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
16026 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
16027 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
16028 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
16029 })
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016030 public int getTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016031 return (mPrivateFlags2 & TEXT_DIRECTION_MASK) >> TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016032 }
16033
16034 /**
16035 * Set the text direction.
16036 *
16037 * @param textDirection the direction to set. Should be one of:
16038 *
16039 * {@link #TEXT_DIRECTION_INHERIT},
16040 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16041 * {@link #TEXT_DIRECTION_ANY_RTL},
16042 * {@link #TEXT_DIRECTION_LTR},
16043 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016044 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016045 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016046 */
16047 public void setTextDirection(int textDirection) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016048 if (getTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016049 // Reset the current text direction and the resolved one
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016050 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016051 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016052 // Set the new text direction
16053 mPrivateFlags2 |= ((textDirection << TEXT_DIRECTION_MASK_SHIFT) & TEXT_DIRECTION_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016054 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016055 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016056 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016057 }
16058 }
16059
16060 /**
16061 * Return the resolved text direction.
16062 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016063 * This needs resolution if the value is TEXT_DIRECTION_INHERIT. The resolution matches
16064 * {@link #getTextDirection()}if it is not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds
16065 * up the parent chain of the view. if there is no parent, then it will return the default
16066 * {@link #TEXT_DIRECTION_FIRST_STRONG}.
16067 *
16068 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016069 *
Doug Feltcb3791202011-07-07 11:57:48 -070016070 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16071 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016072 * {@link #TEXT_DIRECTION_LTR},
16073 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016074 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016075 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016076 */
16077 public int getResolvedTextDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070016078 // The text direction will be resolved only if needed
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016079 if ((mPrivateFlags2 & TEXT_DIRECTION_RESOLVED) != TEXT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016080 resolveTextDirection();
16081 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016082 return (mPrivateFlags2 & TEXT_DIRECTION_RESOLVED_MASK) >> TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016083 }
16084
16085 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016086 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
16087 * resolution is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016088 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016089 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016090 public void resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016091 // Reset any previous text direction resolution
16092 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
16093
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016094 if (hasRtlSupport()) {
16095 // Set resolved text direction flag depending on text direction flag
16096 final int textDirection = getTextDirection();
16097 switch(textDirection) {
16098 case TEXT_DIRECTION_INHERIT:
16099 if (canResolveTextDirection()) {
16100 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016101
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016102 // Set current resolved direction to the same value as the parent's one
16103 final int parentResolvedDirection = viewGroup.getResolvedTextDirection();
16104 switch (parentResolvedDirection) {
16105 case TEXT_DIRECTION_FIRST_STRONG:
16106 case TEXT_DIRECTION_ANY_RTL:
16107 case TEXT_DIRECTION_LTR:
16108 case TEXT_DIRECTION_RTL:
16109 case TEXT_DIRECTION_LOCALE:
16110 mPrivateFlags2 |=
16111 (parentResolvedDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16112 break;
16113 default:
16114 // Default resolved direction is "first strong" heuristic
16115 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
16116 }
16117 } else {
16118 // We cannot do the resolution if there is no parent, so use the default one
16119 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016120 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016121 break;
16122 case TEXT_DIRECTION_FIRST_STRONG:
16123 case TEXT_DIRECTION_ANY_RTL:
16124 case TEXT_DIRECTION_LTR:
16125 case TEXT_DIRECTION_RTL:
16126 case TEXT_DIRECTION_LOCALE:
16127 // Resolved direction is the same as text direction
16128 mPrivateFlags2 |= (textDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16129 break;
16130 default:
16131 // Default resolved direction is "first strong" heuristic
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016132 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016133 }
16134 } else {
16135 // Default resolved direction is "first strong" heuristic
16136 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016137 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016138
16139 // Set to resolved
16140 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016141 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016142 }
16143
16144 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016145 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016146 * resolution should override this method.
16147 *
16148 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016149 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016150 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016151 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016152 }
16153
16154 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016155 * Check if text direction resolution can be done.
16156 *
16157 * @return true if text direction resolution can be done otherwise return false.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016158 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016159 */
16160 public boolean canResolveTextDirection() {
16161 switch (getTextDirection()) {
16162 case TEXT_DIRECTION_INHERIT:
16163 return (mParent != null) && (mParent instanceof ViewGroup);
16164 default:
16165 return true;
16166 }
16167 }
16168
16169 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016170 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016171 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016172 * reset is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016173 * @hide
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016174 */
16175 public void resetResolvedTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016176 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016177 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016178 }
16179
16180 /**
16181 * Called when text direction is reset. Subclasses that care about text direction reset should
16182 * override this method and do a reset of the text direction of their children. The default
16183 * implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016184 * @hide
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016185 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016186 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016187 }
16188
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016189 /**
16190 * Return the value specifying the text alignment or policy that was set with
16191 * {@link #setTextAlignment(int)}.
16192 *
16193 * @return the defined text alignment. It can be one of:
16194 *
16195 * {@link #TEXT_ALIGNMENT_INHERIT},
16196 * {@link #TEXT_ALIGNMENT_GRAVITY},
16197 * {@link #TEXT_ALIGNMENT_CENTER},
16198 * {@link #TEXT_ALIGNMENT_TEXT_START},
16199 * {@link #TEXT_ALIGNMENT_TEXT_END},
16200 * {@link #TEXT_ALIGNMENT_VIEW_START},
16201 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016202 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016203 */
16204 @ViewDebug.ExportedProperty(category = "text", mapping = {
16205 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16206 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16207 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16208 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16209 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16210 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16211 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16212 })
16213 public int getTextAlignment() {
16214 return (mPrivateFlags2 & TEXT_ALIGNMENT_MASK) >> TEXT_ALIGNMENT_MASK_SHIFT;
16215 }
16216
16217 /**
16218 * Set the text alignment.
16219 *
16220 * @param textAlignment The text alignment to set. Should be one of
16221 *
16222 * {@link #TEXT_ALIGNMENT_INHERIT},
16223 * {@link #TEXT_ALIGNMENT_GRAVITY},
16224 * {@link #TEXT_ALIGNMENT_CENTER},
16225 * {@link #TEXT_ALIGNMENT_TEXT_START},
16226 * {@link #TEXT_ALIGNMENT_TEXT_END},
16227 * {@link #TEXT_ALIGNMENT_VIEW_START},
16228 * {@link #TEXT_ALIGNMENT_VIEW_END}
16229 *
16230 * @attr ref android.R.styleable#View_textAlignment
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016231 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016232 */
16233 public void setTextAlignment(int textAlignment) {
16234 if (textAlignment != getTextAlignment()) {
16235 // Reset the current and resolved text alignment
16236 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
16237 resetResolvedTextAlignment();
16238 // Set the new text alignment
16239 mPrivateFlags2 |= ((textAlignment << TEXT_ALIGNMENT_MASK_SHIFT) & TEXT_ALIGNMENT_MASK);
16240 // Refresh
16241 requestLayout();
16242 invalidate(true);
16243 }
16244 }
16245
16246 /**
16247 * Return the resolved text alignment.
16248 *
16249 * The resolved text alignment. This needs resolution if the value is
16250 * TEXT_ALIGNMENT_INHERIT. The resolution matches {@link #setTextAlignment(int)} if it is
16251 * not TEXT_ALIGNMENT_INHERIT, otherwise resolution proceeds up the parent chain of the view.
16252 *
16253 * @return the resolved text alignment. Returns one of:
16254 *
16255 * {@link #TEXT_ALIGNMENT_GRAVITY},
16256 * {@link #TEXT_ALIGNMENT_CENTER},
16257 * {@link #TEXT_ALIGNMENT_TEXT_START},
16258 * {@link #TEXT_ALIGNMENT_TEXT_END},
16259 * {@link #TEXT_ALIGNMENT_VIEW_START},
16260 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016261 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016262 */
16263 @ViewDebug.ExportedProperty(category = "text", mapping = {
16264 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16265 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16266 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16267 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16268 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16269 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16270 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16271 })
16272 public int getResolvedTextAlignment() {
16273 // If text alignment is not resolved, then resolve it
16274 if ((mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED) != TEXT_ALIGNMENT_RESOLVED) {
16275 resolveTextAlignment();
16276 }
16277 return (mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED_MASK) >> TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
16278 }
16279
16280 /**
16281 * Resolve the text alignment. Will call {@link View#onResolvedTextAlignmentChanged} when
16282 * resolution is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016283 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016284 */
16285 public void resolveTextAlignment() {
16286 // Reset any previous text alignment resolution
16287 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16288
16289 if (hasRtlSupport()) {
16290 // Set resolved text alignment flag depending on text alignment flag
16291 final int textAlignment = getTextAlignment();
16292 switch (textAlignment) {
16293 case TEXT_ALIGNMENT_INHERIT:
16294 // Check if we can resolve the text alignment
16295 if (canResolveLayoutDirection() && mParent instanceof View) {
16296 View view = (View) mParent;
16297
16298 final int parentResolvedTextAlignment = view.getResolvedTextAlignment();
16299 switch (parentResolvedTextAlignment) {
16300 case TEXT_ALIGNMENT_GRAVITY:
16301 case TEXT_ALIGNMENT_TEXT_START:
16302 case TEXT_ALIGNMENT_TEXT_END:
16303 case TEXT_ALIGNMENT_CENTER:
16304 case TEXT_ALIGNMENT_VIEW_START:
16305 case TEXT_ALIGNMENT_VIEW_END:
16306 // Resolved text alignment is the same as the parent resolved
16307 // text alignment
16308 mPrivateFlags2 |=
16309 (parentResolvedTextAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16310 break;
16311 default:
16312 // Use default resolved text alignment
16313 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16314 }
16315 }
16316 else {
16317 // We cannot do the resolution if there is no parent so use the default
16318 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16319 }
16320 break;
16321 case TEXT_ALIGNMENT_GRAVITY:
16322 case TEXT_ALIGNMENT_TEXT_START:
16323 case TEXT_ALIGNMENT_TEXT_END:
16324 case TEXT_ALIGNMENT_CENTER:
16325 case TEXT_ALIGNMENT_VIEW_START:
16326 case TEXT_ALIGNMENT_VIEW_END:
16327 // Resolved text alignment is the same as text alignment
16328 mPrivateFlags2 |= (textAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16329 break;
16330 default:
16331 // Use default resolved text alignment
16332 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16333 }
16334 } else {
16335 // Use default resolved text alignment
16336 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16337 }
16338
16339 // Set the resolved
16340 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED;
16341 onResolvedTextAlignmentChanged();
16342 }
16343
16344 /**
16345 * Check if text alignment resolution can be done.
16346 *
16347 * @return true if text alignment resolution can be done otherwise return false.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016348 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016349 */
16350 public boolean canResolveTextAlignment() {
16351 switch (getTextAlignment()) {
16352 case TEXT_DIRECTION_INHERIT:
16353 return (mParent != null);
16354 default:
16355 return true;
16356 }
16357 }
16358
16359 /**
16360 * Called when text alignment has been resolved. Subclasses that care about text alignment
16361 * resolution should override this method.
16362 *
16363 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016364 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016365 */
16366 public void onResolvedTextAlignmentChanged() {
16367 }
16368
16369 /**
16370 * Reset resolved text alignment. Text alignment can be resolved with a call to
16371 * getResolvedTextAlignment(). Will call {@link View#onResolvedTextAlignmentReset} when
16372 * reset is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016373 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016374 */
16375 public void resetResolvedTextAlignment() {
16376 // Reset any previous text alignment resolution
16377 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16378 onResolvedTextAlignmentReset();
16379 }
16380
16381 /**
16382 * Called when text alignment is reset. Subclasses that care about text alignment reset should
16383 * override this method and do a reset of the text alignment of their children. The default
16384 * implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016385 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016386 */
16387 public void onResolvedTextAlignmentReset() {
16388 }
16389
Chet Haaseb39f0512011-05-24 14:36:40 -070016390 //
16391 // Properties
16392 //
16393 /**
16394 * A Property wrapper around the <code>alpha</code> functionality handled by the
16395 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
16396 */
Chet Haased47f1532011-12-16 11:18:52 -080016397 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016398 @Override
16399 public void setValue(View object, float value) {
16400 object.setAlpha(value);
16401 }
16402
16403 @Override
16404 public Float get(View object) {
16405 return object.getAlpha();
16406 }
16407 };
16408
16409 /**
16410 * A Property wrapper around the <code>translationX</code> functionality handled by the
16411 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
16412 */
Chet Haased47f1532011-12-16 11:18:52 -080016413 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016414 @Override
16415 public void setValue(View object, float value) {
16416 object.setTranslationX(value);
16417 }
16418
16419 @Override
16420 public Float get(View object) {
16421 return object.getTranslationX();
16422 }
16423 };
16424
16425 /**
16426 * A Property wrapper around the <code>translationY</code> functionality handled by the
16427 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
16428 */
Chet Haased47f1532011-12-16 11:18:52 -080016429 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016430 @Override
16431 public void setValue(View object, float value) {
16432 object.setTranslationY(value);
16433 }
16434
16435 @Override
16436 public Float get(View object) {
16437 return object.getTranslationY();
16438 }
16439 };
16440
16441 /**
16442 * A Property wrapper around the <code>x</code> functionality handled by the
16443 * {@link View#setX(float)} and {@link View#getX()} methods.
16444 */
Chet Haased47f1532011-12-16 11:18:52 -080016445 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016446 @Override
16447 public void setValue(View object, float value) {
16448 object.setX(value);
16449 }
16450
16451 @Override
16452 public Float get(View object) {
16453 return object.getX();
16454 }
16455 };
16456
16457 /**
16458 * A Property wrapper around the <code>y</code> functionality handled by the
16459 * {@link View#setY(float)} and {@link View#getY()} methods.
16460 */
Chet Haased47f1532011-12-16 11:18:52 -080016461 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016462 @Override
16463 public void setValue(View object, float value) {
16464 object.setY(value);
16465 }
16466
16467 @Override
16468 public Float get(View object) {
16469 return object.getY();
16470 }
16471 };
16472
16473 /**
16474 * A Property wrapper around the <code>rotation</code> functionality handled by the
16475 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
16476 */
Chet Haased47f1532011-12-16 11:18:52 -080016477 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016478 @Override
16479 public void setValue(View object, float value) {
16480 object.setRotation(value);
16481 }
16482
16483 @Override
16484 public Float get(View object) {
16485 return object.getRotation();
16486 }
16487 };
16488
16489 /**
16490 * A Property wrapper around the <code>rotationX</code> functionality handled by the
16491 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
16492 */
Chet Haased47f1532011-12-16 11:18:52 -080016493 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016494 @Override
16495 public void setValue(View object, float value) {
16496 object.setRotationX(value);
16497 }
16498
16499 @Override
16500 public Float get(View object) {
16501 return object.getRotationX();
16502 }
16503 };
16504
16505 /**
16506 * A Property wrapper around the <code>rotationY</code> functionality handled by the
16507 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
16508 */
Chet Haased47f1532011-12-16 11:18:52 -080016509 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016510 @Override
16511 public void setValue(View object, float value) {
16512 object.setRotationY(value);
16513 }
16514
16515 @Override
16516 public Float get(View object) {
16517 return object.getRotationY();
16518 }
16519 };
16520
16521 /**
16522 * A Property wrapper around the <code>scaleX</code> functionality handled by the
16523 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
16524 */
Chet Haased47f1532011-12-16 11:18:52 -080016525 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016526 @Override
16527 public void setValue(View object, float value) {
16528 object.setScaleX(value);
16529 }
16530
16531 @Override
16532 public Float get(View object) {
16533 return object.getScaleX();
16534 }
16535 };
16536
16537 /**
16538 * A Property wrapper around the <code>scaleY</code> functionality handled by the
16539 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
16540 */
Chet Haased47f1532011-12-16 11:18:52 -080016541 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016542 @Override
16543 public void setValue(View object, float value) {
16544 object.setScaleY(value);
16545 }
16546
16547 @Override
16548 public Float get(View object) {
16549 return object.getScaleY();
16550 }
16551 };
16552
Jeff Brown33bbfd22011-02-24 20:55:35 -080016553 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016554 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
16555 * Each MeasureSpec represents a requirement for either the width or the height.
16556 * A MeasureSpec is comprised of a size and a mode. There are three possible
16557 * modes:
16558 * <dl>
16559 * <dt>UNSPECIFIED</dt>
16560 * <dd>
16561 * The parent has not imposed any constraint on the child. It can be whatever size
16562 * it wants.
16563 * </dd>
16564 *
16565 * <dt>EXACTLY</dt>
16566 * <dd>
16567 * The parent has determined an exact size for the child. The child is going to be
16568 * given those bounds regardless of how big it wants to be.
16569 * </dd>
16570 *
16571 * <dt>AT_MOST</dt>
16572 * <dd>
16573 * The child can be as large as it wants up to the specified size.
16574 * </dd>
16575 * </dl>
16576 *
16577 * MeasureSpecs are implemented as ints to reduce object allocation. This class
16578 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
16579 */
16580 public static class MeasureSpec {
16581 private static final int MODE_SHIFT = 30;
16582 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
16583
16584 /**
16585 * Measure specification mode: The parent has not imposed any constraint
16586 * on the child. It can be whatever size it wants.
16587 */
16588 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
16589
16590 /**
16591 * Measure specification mode: The parent has determined an exact size
16592 * for the child. The child is going to be given those bounds regardless
16593 * of how big it wants to be.
16594 */
16595 public static final int EXACTLY = 1 << MODE_SHIFT;
16596
16597 /**
16598 * Measure specification mode: The child can be as large as it wants up
16599 * to the specified size.
16600 */
16601 public static final int AT_MOST = 2 << MODE_SHIFT;
16602
16603 /**
16604 * Creates a measure specification based on the supplied size and mode.
16605 *
16606 * The mode must always be one of the following:
16607 * <ul>
16608 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
16609 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
16610 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
16611 * </ul>
16612 *
16613 * @param size the size of the measure specification
16614 * @param mode the mode of the measure specification
16615 * @return the measure specification based on size and mode
16616 */
16617 public static int makeMeasureSpec(int size, int mode) {
16618 return size + mode;
16619 }
16620
16621 /**
16622 * Extracts the mode from the supplied measure specification.
16623 *
16624 * @param measureSpec the measure specification to extract the mode from
16625 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
16626 * {@link android.view.View.MeasureSpec#AT_MOST} or
16627 * {@link android.view.View.MeasureSpec#EXACTLY}
16628 */
16629 public static int getMode(int measureSpec) {
16630 return (measureSpec & MODE_MASK);
16631 }
16632
16633 /**
16634 * Extracts the size from the supplied measure specification.
16635 *
16636 * @param measureSpec the measure specification to extract the size from
16637 * @return the size in pixels defined in the supplied measure specification
16638 */
16639 public static int getSize(int measureSpec) {
16640 return (measureSpec & ~MODE_MASK);
16641 }
16642
16643 /**
16644 * Returns a String representation of the specified measure
16645 * specification.
16646 *
16647 * @param measureSpec the measure specification to convert to a String
16648 * @return a String with the following format: "MeasureSpec: MODE SIZE"
16649 */
16650 public static String toString(int measureSpec) {
16651 int mode = getMode(measureSpec);
16652 int size = getSize(measureSpec);
16653
16654 StringBuilder sb = new StringBuilder("MeasureSpec: ");
16655
16656 if (mode == UNSPECIFIED)
16657 sb.append("UNSPECIFIED ");
16658 else if (mode == EXACTLY)
16659 sb.append("EXACTLY ");
16660 else if (mode == AT_MOST)
16661 sb.append("AT_MOST ");
16662 else
16663 sb.append(mode).append(" ");
16664
16665 sb.append(size);
16666 return sb.toString();
16667 }
16668 }
16669
16670 class CheckForLongPress implements Runnable {
16671
16672 private int mOriginalWindowAttachCount;
16673
16674 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070016675 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016676 && mOriginalWindowAttachCount == mWindowAttachCount) {
16677 if (performLongClick()) {
16678 mHasPerformedLongPress = true;
16679 }
16680 }
16681 }
16682
16683 public void rememberWindowAttachCount() {
16684 mOriginalWindowAttachCount = mWindowAttachCount;
16685 }
16686 }
Joe Malin32736f02011-01-19 16:14:20 -080016687
Adam Powelle14579b2009-12-16 18:39:52 -080016688 private final class CheckForTap implements Runnable {
16689 public void run() {
16690 mPrivateFlags &= ~PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080016691 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016692 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080016693 }
16694 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016695
Adam Powella35d7682010-03-12 14:48:13 -080016696 private final class PerformClick implements Runnable {
16697 public void run() {
16698 performClick();
16699 }
16700 }
16701
Dianne Hackborn63042d62011-01-26 18:56:29 -080016702 /** @hide */
16703 public void hackTurnOffWindowResizeAnim(boolean off) {
16704 mAttachInfo.mTurnOffWindowResizeAnim = off;
16705 }
Joe Malin32736f02011-01-19 16:14:20 -080016706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016707 /**
Chet Haasea00f3862011-02-22 06:34:40 -080016708 * This method returns a ViewPropertyAnimator object, which can be used to animate
16709 * specific properties on this View.
16710 *
16711 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
16712 */
16713 public ViewPropertyAnimator animate() {
16714 if (mAnimator == null) {
16715 mAnimator = new ViewPropertyAnimator(this);
16716 }
16717 return mAnimator;
16718 }
16719
16720 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016721 * Interface definition for a callback to be invoked when a key event is
16722 * dispatched to this view. The callback will be invoked before the key
16723 * event is given to the view.
16724 */
16725 public interface OnKeyListener {
16726 /**
16727 * Called when a key is dispatched to a view. This allows listeners to
16728 * get a chance to respond before the target view.
16729 *
16730 * @param v The view the key has been dispatched to.
16731 * @param keyCode The code for the physical key that was pressed
16732 * @param event The KeyEvent object containing full information about
16733 * the event.
16734 * @return True if the listener has consumed the event, false otherwise.
16735 */
16736 boolean onKey(View v, int keyCode, KeyEvent event);
16737 }
16738
16739 /**
16740 * Interface definition for a callback to be invoked when a touch event is
16741 * dispatched to this view. The callback will be invoked before the touch
16742 * event is given to the view.
16743 */
16744 public interface OnTouchListener {
16745 /**
16746 * Called when a touch event is dispatched to a view. This allows listeners to
16747 * get a chance to respond before the target view.
16748 *
16749 * @param v The view the touch event has been dispatched to.
16750 * @param event The MotionEvent object containing full information about
16751 * the event.
16752 * @return True if the listener has consumed the event, false otherwise.
16753 */
16754 boolean onTouch(View v, MotionEvent event);
16755 }
16756
16757 /**
Jeff Brown10b62902011-06-20 16:40:37 -070016758 * Interface definition for a callback to be invoked when a hover event is
16759 * dispatched to this view. The callback will be invoked before the hover
16760 * event is given to the view.
16761 */
16762 public interface OnHoverListener {
16763 /**
16764 * Called when a hover event is dispatched to a view. This allows listeners to
16765 * get a chance to respond before the target view.
16766 *
16767 * @param v The view the hover event has been dispatched to.
16768 * @param event The MotionEvent object containing full information about
16769 * the event.
16770 * @return True if the listener has consumed the event, false otherwise.
16771 */
16772 boolean onHover(View v, MotionEvent event);
16773 }
16774
16775 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016776 * Interface definition for a callback to be invoked when a generic motion event is
16777 * dispatched to this view. The callback will be invoked before the generic motion
16778 * event is given to the view.
16779 */
16780 public interface OnGenericMotionListener {
16781 /**
16782 * Called when a generic motion event is dispatched to a view. This allows listeners to
16783 * get a chance to respond before the target view.
16784 *
16785 * @param v The view the generic motion event has been dispatched to.
16786 * @param event The MotionEvent object containing full information about
16787 * the event.
16788 * @return True if the listener has consumed the event, false otherwise.
16789 */
16790 boolean onGenericMotion(View v, MotionEvent event);
16791 }
16792
16793 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016794 * Interface definition for a callback to be invoked when a view has been clicked and held.
16795 */
16796 public interface OnLongClickListener {
16797 /**
16798 * Called when a view has been clicked and held.
16799 *
16800 * @param v The view that was clicked and held.
16801 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080016802 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016803 */
16804 boolean onLongClick(View v);
16805 }
16806
16807 /**
Chris Tate32affef2010-10-18 15:29:21 -070016808 * Interface definition for a callback to be invoked when a drag is being dispatched
16809 * to this view. The callback will be invoked before the hosting view's own
16810 * onDrag(event) method. If the listener wants to fall back to the hosting view's
16811 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070016812 *
16813 * <div class="special reference">
16814 * <h3>Developer Guides</h3>
16815 * <p>For a guide to implementing drag and drop features, read the
16816 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
16817 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070016818 */
16819 public interface OnDragListener {
16820 /**
16821 * Called when a drag event is dispatched to a view. This allows listeners
16822 * to get a chance to override base View behavior.
16823 *
Joe Malin32736f02011-01-19 16:14:20 -080016824 * @param v The View that received the drag event.
16825 * @param event The {@link android.view.DragEvent} object for the drag event.
16826 * @return {@code true} if the drag event was handled successfully, or {@code false}
16827 * if the drag event was not handled. Note that {@code false} will trigger the View
16828 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070016829 */
16830 boolean onDrag(View v, DragEvent event);
16831 }
16832
16833 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016834 * Interface definition for a callback to be invoked when the focus state of
16835 * a view changed.
16836 */
16837 public interface OnFocusChangeListener {
16838 /**
16839 * Called when the focus state of a view has changed.
16840 *
16841 * @param v The view whose state has changed.
16842 * @param hasFocus The new focus state of v.
16843 */
16844 void onFocusChange(View v, boolean hasFocus);
16845 }
16846
16847 /**
16848 * Interface definition for a callback to be invoked when a view is clicked.
16849 */
16850 public interface OnClickListener {
16851 /**
16852 * Called when a view has been clicked.
16853 *
16854 * @param v The view that was clicked.
16855 */
16856 void onClick(View v);
16857 }
16858
16859 /**
16860 * Interface definition for a callback to be invoked when the context menu
16861 * for this view is being built.
16862 */
16863 public interface OnCreateContextMenuListener {
16864 /**
16865 * Called when the context menu for this view is being built. It is not
16866 * safe to hold onto the menu after this method returns.
16867 *
16868 * @param menu The context menu that is being built
16869 * @param v The view for which the context menu is being built
16870 * @param menuInfo Extra information about the item for which the
16871 * context menu should be shown. This information will vary
16872 * depending on the class of v.
16873 */
16874 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
16875 }
16876
Joe Onorato664644d2011-01-23 17:53:23 -080016877 /**
16878 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016879 * visibility. This reports <strong>global</strong> changes to the system UI
Dianne Hackborncf675782012-05-10 15:07:24 -070016880 * state, not what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080016881 *
Philip Milne6c8ea062012-04-03 17:38:43 -070016882 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080016883 */
16884 public interface OnSystemUiVisibilityChangeListener {
16885 /**
16886 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070016887 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080016888 *
Dianne Hackborncf675782012-05-10 15:07:24 -070016889 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
16890 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, and {@link #SYSTEM_UI_FLAG_FULLSCREEN}.
16891 * This tells you the <strong>global</strong> state of these UI visibility
16892 * flags, not what your app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080016893 */
16894 public void onSystemUiVisibilityChange(int visibility);
16895 }
16896
Adam Powell4afd62b2011-02-18 15:02:18 -080016897 /**
16898 * Interface definition for a callback to be invoked when this view is attached
16899 * or detached from its window.
16900 */
16901 public interface OnAttachStateChangeListener {
16902 /**
16903 * Called when the view is attached to a window.
16904 * @param v The view that was attached
16905 */
16906 public void onViewAttachedToWindow(View v);
16907 /**
16908 * Called when the view is detached from a window.
16909 * @param v The view that was detached
16910 */
16911 public void onViewDetachedFromWindow(View v);
16912 }
16913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016914 private final class UnsetPressedState implements Runnable {
16915 public void run() {
16916 setPressed(false);
16917 }
16918 }
16919
16920 /**
16921 * Base class for derived classes that want to save and restore their own
16922 * state in {@link android.view.View#onSaveInstanceState()}.
16923 */
16924 public static class BaseSavedState extends AbsSavedState {
16925 /**
16926 * Constructor used when reading from a parcel. Reads the state of the superclass.
16927 *
16928 * @param source
16929 */
16930 public BaseSavedState(Parcel source) {
16931 super(source);
16932 }
16933
16934 /**
16935 * Constructor called by derived classes when creating their SavedState objects
16936 *
16937 * @param superState The state of the superclass of this view
16938 */
16939 public BaseSavedState(Parcelable superState) {
16940 super(superState);
16941 }
16942
16943 public static final Parcelable.Creator<BaseSavedState> CREATOR =
16944 new Parcelable.Creator<BaseSavedState>() {
16945 public BaseSavedState createFromParcel(Parcel in) {
16946 return new BaseSavedState(in);
16947 }
16948
16949 public BaseSavedState[] newArray(int size) {
16950 return new BaseSavedState[size];
16951 }
16952 };
16953 }
16954
16955 /**
16956 * A set of information given to a view when it is attached to its parent
16957 * window.
16958 */
16959 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016960 interface Callbacks {
16961 void playSoundEffect(int effectId);
16962 boolean performHapticFeedback(int effectId, boolean always);
16963 }
16964
16965 /**
16966 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
16967 * to a Handler. This class contains the target (View) to invalidate and
16968 * the coordinates of the dirty rectangle.
16969 *
16970 * For performance purposes, this class also implements a pool of up to
16971 * POOL_LIMIT objects that get reused. This reduces memory allocations
16972 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016973 */
Romain Guyd928d682009-03-31 17:52:16 -070016974 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016975 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070016976 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
16977 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070016978 public InvalidateInfo newInstance() {
16979 return new InvalidateInfo();
16980 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016981
Romain Guyd928d682009-03-31 17:52:16 -070016982 public void onAcquired(InvalidateInfo element) {
16983 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016984
Romain Guyd928d682009-03-31 17:52:16 -070016985 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070016986 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070016987 }
16988 }, POOL_LIMIT)
16989 );
16990
16991 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070016992 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016993
16994 View target;
16995
16996 int left;
16997 int top;
16998 int right;
16999 int bottom;
17000
Romain Guyd928d682009-03-31 17:52:16 -070017001 public void setNextPoolable(InvalidateInfo element) {
17002 mNext = element;
17003 }
17004
17005 public InvalidateInfo getNextPoolable() {
17006 return mNext;
17007 }
17008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017009 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070017010 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017011 }
17012
17013 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070017014 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017015 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017016
17017 public boolean isPooled() {
17018 return mIsPooled;
17019 }
17020
17021 public void setPooled(boolean isPooled) {
17022 mIsPooled = isPooled;
17023 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017024 }
17025
17026 final IWindowSession mSession;
17027
17028 final IWindow mWindow;
17029
17030 final IBinder mWindowToken;
17031
17032 final Callbacks mRootCallbacks;
17033
Romain Guy59a12ca2011-06-09 17:48:21 -070017034 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080017035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017036 /**
17037 * The top view of the hierarchy.
17038 */
17039 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070017040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017041 IBinder mPanelParentWindowToken;
17042 Surface mSurface;
17043
Romain Guyb051e892010-09-28 19:09:36 -070017044 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080017045 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070017046 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080017047
Romain Guy7e4e5612012-03-05 14:37:29 -080017048 boolean mScreenOn;
17049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017050 /**
Romain Guy8506ab42009-06-11 17:35:47 -070017051 * Scale factor used by the compatibility mode
17052 */
17053 float mApplicationScale;
17054
17055 /**
17056 * Indicates whether the application is in compatibility mode
17057 */
17058 boolean mScalingRequired;
17059
17060 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017061 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080017062 */
17063 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080017064
Dianne Hackborn63042d62011-01-26 18:56:29 -080017065 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017066 * Left position of this view's window
17067 */
17068 int mWindowLeft;
17069
17070 /**
17071 * Top position of this view's window
17072 */
17073 int mWindowTop;
17074
17075 /**
Adam Powell26153a32010-11-08 15:22:27 -080017076 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070017077 */
Adam Powell26153a32010-11-08 15:22:27 -080017078 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070017079
17080 /**
Dianne Hackborn3556c9a2012-05-04 16:31:25 -070017081 * Describes the parts of the window that are currently completely
17082 * obscured by system UI elements.
17083 */
17084 final Rect mSystemInsets = new Rect();
17085
17086 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017087 * For windows that are full-screen but using insets to layout inside
17088 * of the screen decorations, these are the current insets for the
17089 * content of the window.
17090 */
17091 final Rect mContentInsets = new Rect();
17092
17093 /**
17094 * For windows that are full-screen but using insets to layout inside
17095 * of the screen decorations, these are the current insets for the
17096 * actual visible parts of the window.
17097 */
17098 final Rect mVisibleInsets = new Rect();
17099
17100 /**
17101 * The internal insets given by this window. This value is
17102 * supplied by the client (through
17103 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
17104 * be given to the window manager when changed to be used in laying
17105 * out windows behind it.
17106 */
17107 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
17108 = new ViewTreeObserver.InternalInsetsInfo();
17109
17110 /**
17111 * All views in the window's hierarchy that serve as scroll containers,
17112 * used to determine if the window can be resized or must be panned
17113 * to adjust for a soft input area.
17114 */
17115 final ArrayList<View> mScrollContainers = new ArrayList<View>();
17116
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070017117 final KeyEvent.DispatcherState mKeyDispatchState
17118 = new KeyEvent.DispatcherState();
17119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017120 /**
17121 * Indicates whether the view's window currently has the focus.
17122 */
17123 boolean mHasWindowFocus;
17124
17125 /**
17126 * The current visibility of the window.
17127 */
17128 int mWindowVisibility;
17129
17130 /**
17131 * Indicates the time at which drawing started to occur.
17132 */
17133 long mDrawingTime;
17134
17135 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070017136 * Indicates whether or not ignoring the DIRTY_MASK flags.
17137 */
17138 boolean mIgnoreDirtyState;
17139
17140 /**
Romain Guy02ccac62011-06-24 13:20:23 -070017141 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
17142 * to avoid clearing that flag prematurely.
17143 */
17144 boolean mSetIgnoreDirtyState = false;
17145
17146 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017147 * Indicates whether the view's window is currently in touch mode.
17148 */
17149 boolean mInTouchMode;
17150
17151 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017152 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017153 * the next time it performs a traversal
17154 */
17155 boolean mRecomputeGlobalAttributes;
17156
17157 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017158 * Always report new attributes at next traversal.
17159 */
17160 boolean mForceReportNewAttributes;
17161
17162 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017163 * Set during a traveral if any views want to keep the screen on.
17164 */
17165 boolean mKeepScreenOn;
17166
17167 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017168 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
17169 */
17170 int mSystemUiVisibility;
17171
17172 /**
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070017173 * Hack to force certain system UI visibility flags to be cleared.
17174 */
17175 int mDisabledSystemUiVisibility;
17176
17177 /**
Dianne Hackborncf675782012-05-10 15:07:24 -070017178 * Last global system UI visibility reported by the window manager.
17179 */
17180 int mGlobalSystemUiVisibility;
17181
17182 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017183 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
17184 * attached.
17185 */
17186 boolean mHasSystemUiListeners;
17187
17188 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017189 * Set if the visibility of any views has changed.
17190 */
17191 boolean mViewVisibilityChanged;
17192
17193 /**
17194 * Set to true if a view has been scrolled.
17195 */
17196 boolean mViewScrollChanged;
17197
17198 /**
17199 * Global to the view hierarchy used as a temporary for dealing with
17200 * x/y points in the transparent region computations.
17201 */
17202 final int[] mTransparentLocation = new int[2];
17203
17204 /**
17205 * Global to the view hierarchy used as a temporary for dealing with
17206 * x/y points in the ViewGroup.invalidateChild implementation.
17207 */
17208 final int[] mInvalidateChildLocation = new int[2];
17209
Chet Haasec3aa3612010-06-17 08:50:37 -070017210
17211 /**
17212 * Global to the view hierarchy used as a temporary for dealing with
17213 * x/y location when view is transformed.
17214 */
17215 final float[] mTmpTransformLocation = new float[2];
17216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017217 /**
17218 * The view tree observer used to dispatch global events like
17219 * layout, pre-draw, touch mode change, etc.
17220 */
17221 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
17222
17223 /**
17224 * A Canvas used by the view hierarchy to perform bitmap caching.
17225 */
17226 Canvas mCanvas;
17227
17228 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080017229 * The view root impl.
17230 */
17231 final ViewRootImpl mViewRootImpl;
17232
17233 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070017234 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017235 * handler can be used to pump events in the UI events queue.
17236 */
17237 final Handler mHandler;
17238
17239 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017240 * Temporary for use in computing invalidate rectangles while
17241 * calling up the hierarchy.
17242 */
17243 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070017244
17245 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070017246 * Temporary for use in computing hit areas with transformed views
17247 */
17248 final RectF mTmpTransformRect = new RectF();
17249
17250 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070017251 * Temporary list for use in collecting focusable descendents of a view.
17252 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070017253 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070017254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017255 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017256 * The id of the window for accessibility purposes.
17257 */
17258 int mAccessibilityWindowId = View.NO_ID;
17259
17260 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -070017261 * Whether to ingore not exposed for accessibility Views when
17262 * reporting the view tree to accessibility services.
17263 */
17264 boolean mIncludeNotImportantViews;
17265
17266 /**
17267 * The drawable for highlighting accessibility focus.
17268 */
17269 Drawable mAccessibilityFocusDrawable;
17270
17271 /**
Philip Milne10ca24a2012-04-23 15:38:27 -070017272 * Show where the margins, bounds and layout bounds are for each view.
17273 */
Dianne Hackborna53de062012-05-08 18:53:51 -070017274 boolean mDebugLayout = SystemProperties.getBoolean(DEBUG_LAYOUT_PROPERTY, false);
Philip Milne10ca24a2012-04-23 15:38:27 -070017275
17276 /**
Romain Guyab4c4f4f2012-05-06 13:11:24 -070017277 * Point used to compute visible regions.
17278 */
17279 final Point mPoint = new Point();
17280
17281 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017282 * Creates a new set of attachment information with the specified
17283 * events handler and thread.
17284 *
17285 * @param handler the events handler the view must use
17286 */
17287 AttachInfo(IWindowSession session, IWindow window,
Jeff Browna175a5b2012-02-15 19:18:31 -080017288 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017289 mSession = session;
17290 mWindow = window;
17291 mWindowToken = window.asBinder();
Jeff Browna175a5b2012-02-15 19:18:31 -080017292 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017293 mHandler = handler;
17294 mRootCallbacks = effectPlayer;
17295 }
17296 }
17297
17298 /**
17299 * <p>ScrollabilityCache holds various fields used by a View when scrolling
17300 * is supported. This avoids keeping too many unused fields in most
17301 * instances of View.</p>
17302 */
Mike Cleronf116bf82009-09-27 19:14:12 -070017303 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080017304
Mike Cleronf116bf82009-09-27 19:14:12 -070017305 /**
17306 * Scrollbars are not visible
17307 */
17308 public static final int OFF = 0;
17309
17310 /**
17311 * Scrollbars are visible
17312 */
17313 public static final int ON = 1;
17314
17315 /**
17316 * Scrollbars are fading away
17317 */
17318 public static final int FADING = 2;
17319
17320 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080017321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017322 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070017323 public int scrollBarDefaultDelayBeforeFade;
17324 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017325
17326 public int scrollBarSize;
17327 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070017328 public float[] interpolatorValues;
17329 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017330
17331 public final Paint paint;
17332 public final Matrix matrix;
17333 public Shader shader;
17334
Mike Cleronf116bf82009-09-27 19:14:12 -070017335 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
17336
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017337 private static final float[] OPAQUE = { 255 };
17338 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080017339
Mike Cleronf116bf82009-09-27 19:14:12 -070017340 /**
17341 * When fading should start. This time moves into the future every time
17342 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
17343 */
17344 public long fadeStartTime;
17345
17346
17347 /**
17348 * The current state of the scrollbars: ON, OFF, or FADING
17349 */
17350 public int state = OFF;
17351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017352 private int mLastColor;
17353
Mike Cleronf116bf82009-09-27 19:14:12 -070017354 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017355 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
17356 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070017357 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
17358 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017359
17360 paint = new Paint();
17361 matrix = new Matrix();
17362 // use use a height of 1, and then wack the matrix each time we
17363 // actually use it.
17364 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017366 paint.setShader(shader);
17367 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070017368 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017369 }
Romain Guy8506ab42009-06-11 17:35:47 -070017370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017371 public void setFadeColor(int color) {
17372 if (color != 0 && color != mLastColor) {
17373 mLastColor = color;
17374 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070017375
Romain Guye55e1a72009-08-27 10:42:26 -070017376 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
17377 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017379 paint.setShader(shader);
17380 // Restore the default transfer mode (src_over)
17381 paint.setXfermode(null);
17382 }
17383 }
Joe Malin32736f02011-01-19 16:14:20 -080017384
Mike Cleronf116bf82009-09-27 19:14:12 -070017385 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070017386 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070017387 if (now >= fadeStartTime) {
17388
17389 // the animation fades the scrollbars out by changing
17390 // the opacity (alpha) from fully opaque to fully
17391 // transparent
17392 int nextFrame = (int) now;
17393 int framesCount = 0;
17394
17395 Interpolator interpolator = scrollBarInterpolator;
17396
17397 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017398 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070017399
17400 // End transparent
17401 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017402 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070017403
17404 state = FADING;
17405
17406 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080017407 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070017408 }
17409 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070017410 }
Mike Cleronf116bf82009-09-27 19:14:12 -070017411
Svetoslav Ganova0156172011-06-26 17:55:44 -070017412 /**
17413 * Resuable callback for sending
17414 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
17415 */
17416 private class SendViewScrolledAccessibilityEvent implements Runnable {
17417 public volatile boolean mIsPending;
17418
17419 public void run() {
17420 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
17421 mIsPending = false;
17422 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017423 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017424
17425 /**
17426 * <p>
17427 * This class represents a delegate that can be registered in a {@link View}
17428 * to enhance accessibility support via composition rather via inheritance.
17429 * It is specifically targeted to widget developers that extend basic View
17430 * classes i.e. classes in package android.view, that would like their
17431 * applications to be backwards compatible.
17432 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080017433 * <div class="special reference">
17434 * <h3>Developer Guides</h3>
17435 * <p>For more information about making applications accessible, read the
17436 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
17437 * developer guide.</p>
17438 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017439 * <p>
17440 * A scenario in which a developer would like to use an accessibility delegate
17441 * is overriding a method introduced in a later API version then the minimal API
17442 * version supported by the application. For example, the method
17443 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
17444 * in API version 4 when the accessibility APIs were first introduced. If a
17445 * developer would like his application to run on API version 4 devices (assuming
17446 * all other APIs used by the application are version 4 or lower) and take advantage
17447 * of this method, instead of overriding the method which would break the application's
17448 * backwards compatibility, he can override the corresponding method in this
17449 * delegate and register the delegate in the target View if the API version of
17450 * the system is high enough i.e. the API version is same or higher to the API
17451 * version that introduced
17452 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
17453 * </p>
17454 * <p>
17455 * Here is an example implementation:
17456 * </p>
17457 * <code><pre><p>
17458 * if (Build.VERSION.SDK_INT >= 14) {
17459 * // If the API version is equal of higher than the version in
17460 * // which onInitializeAccessibilityNodeInfo was introduced we
17461 * // register a delegate with a customized implementation.
17462 * View view = findViewById(R.id.view_id);
17463 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
17464 * public void onInitializeAccessibilityNodeInfo(View host,
17465 * AccessibilityNodeInfo info) {
17466 * // Let the default implementation populate the info.
17467 * super.onInitializeAccessibilityNodeInfo(host, info);
17468 * // Set some other information.
17469 * info.setEnabled(host.isEnabled());
17470 * }
17471 * });
17472 * }
17473 * </code></pre></p>
17474 * <p>
17475 * This delegate contains methods that correspond to the accessibility methods
17476 * in View. If a delegate has been specified the implementation in View hands
17477 * off handling to the corresponding method in this delegate. The default
17478 * implementation the delegate methods behaves exactly as the corresponding
17479 * method in View for the case of no accessibility delegate been set. Hence,
17480 * to customize the behavior of a View method, clients can override only the
17481 * corresponding delegate method without altering the behavior of the rest
17482 * accessibility related methods of the host view.
17483 * </p>
17484 */
17485 public static class AccessibilityDelegate {
17486
17487 /**
17488 * Sends an accessibility event of the given type. If accessibility is not
17489 * enabled this method has no effect.
17490 * <p>
17491 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
17492 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
17493 * been set.
17494 * </p>
17495 *
17496 * @param host The View hosting the delegate.
17497 * @param eventType The type of the event to send.
17498 *
17499 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
17500 */
17501 public void sendAccessibilityEvent(View host, int eventType) {
17502 host.sendAccessibilityEventInternal(eventType);
17503 }
17504
17505 /**
alanv8eeefef2012-05-07 16:57:53 -070017506 * Performs the specified accessibility action on the view. For
17507 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
17508 * <p>
17509 * The default implementation behaves as
17510 * {@link View#performAccessibilityAction(int, Bundle)
17511 * View#performAccessibilityAction(int, Bundle)} for the case of
17512 * no accessibility delegate been set.
17513 * </p>
17514 *
17515 * @param action The action to perform.
17516 * @return Whether the action was performed.
17517 *
17518 * @see View#performAccessibilityAction(int, Bundle)
17519 * View#performAccessibilityAction(int, Bundle)
17520 */
17521 public boolean performAccessibilityAction(View host, int action, Bundle args) {
17522 return host.performAccessibilityActionInternal(action, args);
17523 }
17524
17525 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017526 * Sends an accessibility event. This method behaves exactly as
17527 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
17528 * empty {@link AccessibilityEvent} and does not perform a check whether
17529 * accessibility is enabled.
17530 * <p>
17531 * The default implementation behaves as
17532 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17533 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
17534 * the case of no accessibility delegate been set.
17535 * </p>
17536 *
17537 * @param host The View hosting the delegate.
17538 * @param event The event to send.
17539 *
17540 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17541 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17542 */
17543 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
17544 host.sendAccessibilityEventUncheckedInternal(event);
17545 }
17546
17547 /**
17548 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
17549 * to its children for adding their text content to the event.
17550 * <p>
17551 * The default implementation behaves as
17552 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17553 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
17554 * the case of no accessibility delegate been set.
17555 * </p>
17556 *
17557 * @param host The View hosting the delegate.
17558 * @param event The event.
17559 * @return True if the event population was completed.
17560 *
17561 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17562 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17563 */
17564 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17565 return host.dispatchPopulateAccessibilityEventInternal(event);
17566 }
17567
17568 /**
17569 * Gives a chance to the host View to populate the accessibility event with its
17570 * text content.
17571 * <p>
17572 * The default implementation behaves as
17573 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
17574 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
17575 * the case of no accessibility delegate been set.
17576 * </p>
17577 *
17578 * @param host The View hosting the delegate.
17579 * @param event The accessibility event which to populate.
17580 *
17581 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
17582 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
17583 */
17584 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17585 host.onPopulateAccessibilityEventInternal(event);
17586 }
17587
17588 /**
17589 * Initializes an {@link AccessibilityEvent} with information about the
17590 * the host View which is the event source.
17591 * <p>
17592 * The default implementation behaves as
17593 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
17594 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
17595 * the case of no accessibility delegate been set.
17596 * </p>
17597 *
17598 * @param host The View hosting the delegate.
17599 * @param event The event to initialize.
17600 *
17601 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
17602 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
17603 */
17604 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
17605 host.onInitializeAccessibilityEventInternal(event);
17606 }
17607
17608 /**
17609 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
17610 * <p>
17611 * The default implementation behaves as
17612 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17613 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
17614 * the case of no accessibility delegate been set.
17615 * </p>
17616 *
17617 * @param host The View hosting the delegate.
17618 * @param info The instance to initialize.
17619 *
17620 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17621 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17622 */
17623 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
17624 host.onInitializeAccessibilityNodeInfoInternal(info);
17625 }
17626
17627 /**
17628 * Called when a child of the host View has requested sending an
17629 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
17630 * to augment the event.
17631 * <p>
17632 * The default implementation behaves as
17633 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17634 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
17635 * the case of no accessibility delegate been set.
17636 * </p>
17637 *
17638 * @param host The View hosting the delegate.
17639 * @param child The child which requests sending the event.
17640 * @param event The event to be sent.
17641 * @return True if the event should be sent
17642 *
17643 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17644 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17645 */
17646 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
17647 AccessibilityEvent event) {
17648 return host.onRequestSendAccessibilityEventInternal(child, event);
17649 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070017650
17651 /**
17652 * Gets the provider for managing a virtual view hierarchy rooted at this View
17653 * and reported to {@link android.accessibilityservice.AccessibilityService}s
17654 * that explore the window content.
17655 * <p>
17656 * The default implementation behaves as
17657 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
17658 * the case of no accessibility delegate been set.
17659 * </p>
17660 *
17661 * @return The provider.
17662 *
17663 * @see AccessibilityNodeProvider
17664 */
17665 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
17666 return null;
17667 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017668 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017669}