blob: d93c1be1facea0929f14bdadb90cc10b5f78057f [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;
Jeff Brownbd6e1502012-08-28 03:27:37 -070042import android.hardware.display.DisplayManagerGlobal;
Svetoslav Ganovaa780c12012-04-19 23:01:39 -070043import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.os.Handler;
45import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.os.Parcel;
47import android.os.Parcelable;
48import android.os.RemoteException;
49import android.os.SystemClock;
Philip Milne10ca24a2012-04-23 15:38:27 -070050import android.os.SystemProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.util.AttributeSet;
Doug Feltcb3791202011-07-07 11:57:48 -070052import android.util.FloatProperty;
53import android.util.LocaleUtil;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070055import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070056import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070057import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070058import android.util.Pools;
Doug Feltcb3791202011-07-07 11:57:48 -070059import android.util.Property;
svetoslavganov75986cf2009-05-14 22:28:01 -070060import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080061import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.view.ContextMenu.ContextMenuInfo;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -070063import android.view.AccessibilityIterators.TextSegmentIterator;
64import android.view.AccessibilityIterators.CharacterTextSegmentIterator;
65import android.view.AccessibilityIterators.WordTextSegmentIterator;
66import android.view.AccessibilityIterators.ParagraphTextSegmentIterator;
svetoslavganov75986cf2009-05-14 22:28:01 -070067import android.view.accessibility.AccessibilityEvent;
68import android.view.accessibility.AccessibilityEventSource;
69import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070070import android.view.accessibility.AccessibilityNodeInfo;
Svetoslav Ganov02107852011-10-03 17:06:56 -070071import android.view.accessibility.AccessibilityNodeProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070073import android.view.animation.AnimationUtils;
Chet Haase64a48c12012-02-13 16:33:29 -080074import android.view.animation.Transformation;
svetoslavganov75986cf2009-05-14 22:28:01 -070075import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.view.inputmethod.InputConnection;
77import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.widget.ScrollBarDrawable;
79
Romain Guy1ef3fdb2011-09-09 15:30:30 -070080import static android.os.Build.VERSION_CODES.*;
Philip Milne6c8ea062012-04-03 17:38:43 -070081import static java.lang.Math.max;
Romain Guy1ef3fdb2011-09-09 15:30:30 -070082
Doug Feltcb3791202011-07-07 11:57:48 -070083import com.android.internal.R;
84import com.android.internal.util.Predicate;
85import com.android.internal.view.menu.MenuBuilder;
86
Christopher Tatea0374192010-10-05 13:06:41 -070087import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070088import java.lang.reflect.InvocationTargetException;
89import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090import java.util.ArrayList;
91import java.util.Arrays;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070092import java.util.Locale;
Adam Powell4afd62b2011-02-18 15:02:18 -080093import java.util.concurrent.CopyOnWriteArrayList;
Adam Powella9108a22012-07-18 11:18:09 -070094import java.util.concurrent.atomic.AtomicInteger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095
96/**
97 * <p>
98 * This class represents the basic building block for user interface components. A View
99 * occupies a rectangular area on the screen and is responsible for drawing and
100 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -0700101 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
103 * are invisible containers that hold other Views (or other ViewGroups) and define
104 * their layout properties.
105 * </p>
106 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700107 * <div class="special reference">
108 * <h3>Developer Guides</h3>
109 * <p>For information about using this class to develop your application's user interface,
110 * 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 -0800111 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700112 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 * <a name="Using"></a>
114 * <h3>Using Views</h3>
115 * <p>
116 * All of the views in a window are arranged in a single tree. You can add views
117 * either from code or by specifying a tree of views in one or more XML layout
118 * files. There are many specialized subclasses of views that act as controls or
119 * are capable of displaying text, images, or other content.
120 * </p>
121 * <p>
122 * Once you have created a tree of views, there are typically a few types of
123 * common operations you may wish to perform:
124 * <ul>
125 * <li><strong>Set properties:</strong> for example setting the text of a
126 * {@link android.widget.TextView}. The available properties and the methods
127 * that set them will vary among the different subclasses of views. Note that
128 * properties that are known at build time can be set in the XML layout
129 * files.</li>
130 * <li><strong>Set focus:</strong> The framework will handled moving focus in
131 * response to user input. To force focus to a specific view, call
132 * {@link #requestFocus}.</li>
133 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
134 * that will be notified when something interesting happens to the view. For
135 * example, all views will let you set a listener to be notified when the view
136 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700137 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
Philip Milne6c8ea062012-04-03 17:38:43 -0700138 * Other view subclasses offer more specialized listeners. For example, a Button
Romain Guy5c22a8c2011-05-13 11:48:45 -0700139 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700141 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 * </ul>
143 * </p>
144 * <p><em>
145 * Note: The Android framework is responsible for measuring, laying out and
146 * drawing views. You should not call methods that perform these actions on
147 * views yourself unless you are actually implementing a
148 * {@link android.view.ViewGroup}.
149 * </em></p>
150 *
151 * <a name="Lifecycle"></a>
152 * <h3>Implementing a Custom View</h3>
153 *
154 * <p>
155 * To implement a custom view, you will usually begin by providing overrides for
156 * some of the standard methods that the framework calls on all views. You do
157 * not need to override all of these methods. In fact, you can start by just
158 * overriding {@link #onDraw(android.graphics.Canvas)}.
159 * <table border="2" width="85%" align="center" cellpadding="5">
160 * <thead>
161 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
162 * </thead>
163 *
164 * <tbody>
165 * <tr>
166 * <td rowspan="2">Creation</td>
167 * <td>Constructors</td>
168 * <td>There is a form of the constructor that are called when the view
169 * is created from code and a form that is called when the view is
170 * inflated from a layout file. The second form should parse and apply
171 * any attributes defined in the layout file.
172 * </td>
173 * </tr>
174 * <tr>
175 * <td><code>{@link #onFinishInflate()}</code></td>
176 * <td>Called after a view and all of its children has been inflated
177 * from XML.</td>
178 * </tr>
179 *
180 * <tr>
181 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700182 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 * <td>Called to determine the size requirements for this view and all
184 * of its children.
185 * </td>
186 * </tr>
187 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700188 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 * <td>Called when this view should assign a size and position to all
190 * of its children.
191 * </td>
192 * </tr>
193 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700194 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 * <td>Called when the size of this view has changed.
196 * </td>
197 * </tr>
198 *
199 * <tr>
200 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700201 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 * <td>Called when the view should render its content.
203 * </td>
204 * </tr>
205 *
206 * <tr>
207 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700208 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
Jean Chalard405bc512012-05-29 19:12:34 +0900209 * <td>Called when a new hardware key event occurs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 * </td>
211 * </tr>
212 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700213 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
Jean Chalard405bc512012-05-29 19:12:34 +0900214 * <td>Called when a hardware key up event occurs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 * </td>
216 * </tr>
217 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700218 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 * <td>Called when a trackball motion event occurs.
220 * </td>
221 * </tr>
222 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700223 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 * <td>Called when a touch screen motion event occurs.
225 * </td>
226 * </tr>
227 *
228 * <tr>
229 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700230 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 * <td>Called when the view gains or loses focus.
232 * </td>
233 * </tr>
234 *
235 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700236 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 * <td>Called when the window containing the view gains or loses focus.
238 * </td>
239 * </tr>
240 *
241 * <tr>
242 * <td rowspan="3">Attaching</td>
243 * <td><code>{@link #onAttachedToWindow()}</code></td>
244 * <td>Called when the view is attached to a window.
245 * </td>
246 * </tr>
247 *
248 * <tr>
249 * <td><code>{@link #onDetachedFromWindow}</code></td>
250 * <td>Called when the view is detached from its window.
251 * </td>
252 * </tr>
253 *
254 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700255 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 * <td>Called when the visibility of the window containing the view
257 * has changed.
258 * </td>
259 * </tr>
260 * </tbody>
261 *
262 * </table>
263 * </p>
264 *
265 * <a name="IDs"></a>
266 * <h3>IDs</h3>
267 * Views may have an integer id associated with them. These ids are typically
268 * assigned in the layout XML files, and are used to find specific views within
269 * the view tree. A common pattern is to:
270 * <ul>
271 * <li>Define a Button in the layout file and assign it a unique ID.
272 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700273 * &lt;Button
274 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 * android:layout_width="wrap_content"
276 * android:layout_height="wrap_content"
277 * android:text="@string/my_button_text"/&gt;
278 * </pre></li>
279 * <li>From the onCreate method of an Activity, find the Button
280 * <pre class="prettyprint">
281 * Button myButton = (Button) findViewById(R.id.my_button);
282 * </pre></li>
283 * </ul>
284 * <p>
285 * View IDs need not be unique throughout the tree, but it is good practice to
286 * ensure that they are at least unique within the part of the tree you are
287 * searching.
288 * </p>
289 *
290 * <a name="Position"></a>
291 * <h3>Position</h3>
292 * <p>
293 * The geometry of a view is that of a rectangle. A view has a location,
294 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
295 * two dimensions, expressed as a width and a height. The unit for location
296 * and dimensions is the pixel.
297 * </p>
298 *
299 * <p>
300 * It is possible to retrieve the location of a view by invoking the methods
301 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
302 * coordinate of the rectangle representing the view. The latter returns the
303 * top, or Y, coordinate of the rectangle representing the view. These methods
304 * both return the location of the view relative to its parent. For instance,
305 * when getLeft() returns 20, that means the view is located 20 pixels to the
306 * right of the left edge of its direct parent.
307 * </p>
308 *
309 * <p>
310 * In addition, several convenience methods are offered to avoid unnecessary
311 * computations, namely {@link #getRight()} and {@link #getBottom()}.
312 * These methods return the coordinates of the right and bottom edges of the
313 * rectangle representing the view. For instance, calling {@link #getRight()}
314 * is similar to the following computation: <code>getLeft() + getWidth()</code>
315 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
316 * </p>
317 *
318 * <a name="SizePaddingMargins"></a>
319 * <h3>Size, padding and margins</h3>
320 * <p>
321 * The size of a view is expressed with a width and a height. A view actually
322 * possess two pairs of width and height values.
323 * </p>
324 *
325 * <p>
326 * The first pair is known as <em>measured width</em> and
327 * <em>measured height</em>. These dimensions define how big a view wants to be
328 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
329 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
330 * and {@link #getMeasuredHeight()}.
331 * </p>
332 *
333 * <p>
334 * The second pair is simply known as <em>width</em> and <em>height</em>, or
335 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
336 * dimensions define the actual size of the view on screen, at drawing time and
337 * after layout. These values may, but do not have to, be different from the
338 * measured width and height. The width and height can be obtained by calling
339 * {@link #getWidth()} and {@link #getHeight()}.
340 * </p>
341 *
342 * <p>
343 * To measure its dimensions, a view takes into account its padding. The padding
344 * is expressed in pixels for the left, top, right and bottom parts of the view.
345 * Padding can be used to offset the content of the view by a specific amount of
346 * pixels. For instance, a left padding of 2 will push the view's content by
347 * 2 pixels to the right of the left edge. Padding can be set using the
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -0700348 * {@link #setPadding(int, int, int, int)} or {@link #setPaddingRelative(int, int, int, int)}
349 * method and queried by calling {@link #getPaddingLeft()}, {@link #getPaddingTop()},
350 * {@link #getPaddingRight()}, {@link #getPaddingBottom()}, {@link #getPaddingStart()},
351 * {@link #getPaddingEnd()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 * </p>
353 *
354 * <p>
355 * Even though a view can define a padding, it does not provide any support for
356 * margins. However, view groups provide such a support. Refer to
357 * {@link android.view.ViewGroup} and
358 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
359 * </p>
360 *
361 * <a name="Layout"></a>
362 * <h3>Layout</h3>
363 * <p>
364 * Layout is a two pass process: a measure pass and a layout pass. The measuring
365 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
366 * of the view tree. Each view pushes dimension specifications down the tree
367 * during the recursion. At the end of the measure pass, every view has stored
368 * its measurements. The second pass happens in
369 * {@link #layout(int,int,int,int)} and is also top-down. During
370 * this pass each parent is responsible for positioning all of its children
371 * using the sizes computed in the measure pass.
372 * </p>
373 *
374 * <p>
375 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
376 * {@link #getMeasuredHeight()} values must be set, along with those for all of
377 * that view's descendants. A view's measured width and measured height values
378 * must respect the constraints imposed by the view's parents. This guarantees
379 * that at the end of the measure pass, all parents accept all of their
380 * children's measurements. A parent view may call measure() more than once on
381 * its children. For example, the parent may measure each child once with
382 * unspecified dimensions to find out how big they want to be, then call
383 * measure() on them again with actual numbers if the sum of all the children's
384 * unconstrained sizes is too big or too small.
385 * </p>
386 *
387 * <p>
388 * The measure pass uses two classes to communicate dimensions. The
389 * {@link MeasureSpec} class is used by views to tell their parents how they
390 * want to be measured and positioned. The base LayoutParams class just
391 * describes how big the view wants to be for both width and height. For each
392 * dimension, it can specify one of:
393 * <ul>
394 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800395 * <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 -0800396 * (minus padding)
397 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
398 * enclose its content (plus padding).
399 * </ul>
400 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
401 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
402 * an X and Y value.
403 * </p>
404 *
405 * <p>
406 * MeasureSpecs are used to push requirements down the tree from parent to
407 * child. A MeasureSpec can be in one of three modes:
408 * <ul>
409 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
410 * of a child view. For example, a LinearLayout may call measure() on its child
411 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
412 * tall the child view wants to be given a width of 240 pixels.
413 * <li>EXACTLY: This is used by the parent to impose an exact size on the
414 * child. The child must use this size, and guarantee that all of its
415 * descendants will fit within this size.
416 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
417 * child. The child must gurantee that it and all of its descendants will fit
418 * within this size.
419 * </ul>
420 * </p>
421 *
422 * <p>
423 * To intiate a layout, call {@link #requestLayout}. This method is typically
424 * called by a view on itself when it believes that is can no longer fit within
425 * its current bounds.
426 * </p>
427 *
428 * <a name="Drawing"></a>
429 * <h3>Drawing</h3>
430 * <p>
431 * Drawing is handled by walking the tree and rendering each view that
Joe Fernandez558459f2011-10-13 16:47:36 -0700432 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 * this means that parents will draw before (i.e., behind) their children, with
434 * siblings drawn in the order they appear in the tree.
435 * If you set a background drawable for a View, then the View will draw it for you
436 * before calling back to its <code>onDraw()</code> method.
437 * </p>
438 *
439 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700440 * 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 -0800441 * </p>
442 *
443 * <p>
444 * To force a view to draw, call {@link #invalidate()}.
445 * </p>
446 *
447 * <a name="EventHandlingThreading"></a>
448 * <h3>Event Handling and Threading</h3>
449 * <p>
450 * The basic cycle of a view is as follows:
451 * <ol>
452 * <li>An event comes in and is dispatched to the appropriate view. The view
453 * handles the event and notifies any listeners.</li>
454 * <li>If in the course of processing the event, the view's bounds may need
455 * to be changed, the view will call {@link #requestLayout()}.</li>
456 * <li>Similarly, if in the course of processing the event the view's appearance
457 * may need to be changed, the view will call {@link #invalidate()}.</li>
458 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
459 * the framework will take care of measuring, laying out, and drawing the tree
460 * as appropriate.</li>
461 * </ol>
462 * </p>
463 *
464 * <p><em>Note: The entire view tree is single threaded. You must always be on
465 * the UI thread when calling any method on any view.</em>
466 * If you are doing work on other threads and want to update the state of a view
467 * from that thread, you should use a {@link Handler}.
468 * </p>
469 *
470 * <a name="FocusHandling"></a>
471 * <h3>Focus Handling</h3>
472 * <p>
473 * The framework will handle routine focus movement in response to user input.
474 * This includes changing the focus as views are removed or hidden, or as new
475 * views become available. Views indicate their willingness to take focus
476 * through the {@link #isFocusable} method. To change whether a view can take
477 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
478 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
479 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
480 * </p>
481 * <p>
482 * Focus movement is based on an algorithm which finds the nearest neighbor in a
483 * given direction. In rare cases, the default algorithm may not match the
484 * intended behavior of the developer. In these situations, you can provide
485 * explicit overrides by using these XML attributes in the layout file:
486 * <pre>
487 * nextFocusDown
488 * nextFocusLeft
489 * nextFocusRight
490 * nextFocusUp
491 * </pre>
492 * </p>
493 *
494 *
495 * <p>
496 * To get a particular view to take focus, call {@link #requestFocus()}.
497 * </p>
498 *
499 * <a name="TouchMode"></a>
500 * <h3>Touch Mode</h3>
501 * <p>
502 * When a user is navigating a user interface via directional keys such as a D-pad, it is
503 * necessary to give focus to actionable items such as buttons so the user can see
504 * what will take input. If the device has touch capabilities, however, and the user
505 * begins interacting with the interface by touching it, it is no longer necessary to
506 * always highlight, or give focus to, a particular view. This motivates a mode
507 * for interaction named 'touch mode'.
508 * </p>
509 * <p>
510 * For a touch capable device, once the user touches the screen, the device
511 * will enter touch mode. From this point onward, only views for which
512 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
513 * Other views that are touchable, like buttons, will not take focus when touched; they will
514 * only fire the on click listeners.
515 * </p>
516 * <p>
517 * Any time a user hits a directional key, such as a D-pad direction, the view device will
518 * exit touch mode, and find a view to take focus, so that the user may resume interacting
519 * with the user interface without touching the screen again.
520 * </p>
521 * <p>
522 * The touch mode state is maintained across {@link android.app.Activity}s. Call
523 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
524 * </p>
525 *
526 * <a name="Scrolling"></a>
527 * <h3>Scrolling</h3>
528 * <p>
529 * The framework provides basic support for views that wish to internally
530 * scroll their content. This includes keeping track of the X and Y scroll
531 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800532 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700533 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 * </p>
535 *
536 * <a name="Tags"></a>
537 * <h3>Tags</h3>
538 * <p>
539 * Unlike IDs, tags are not used to identify views. Tags are essentially an
540 * extra piece of information that can be associated with a view. They are most
541 * often used as a convenience to store data related to views in the views
542 * themselves rather than by putting them in a separate structure.
543 * </p>
544 *
Chet Haasecb150fe2012-05-03 15:15:05 -0700545 * <a name="Properties"></a>
546 * <h3>Properties</h3>
547 * <p>
548 * The View class exposes an {@link #ALPHA} property, as well as several transform-related
549 * properties, such as {@link #TRANSLATION_X} and {@link #TRANSLATION_Y}. These properties are
550 * available both in the {@link Property} form as well as in similarly-named setter/getter
551 * methods (such as {@link #setAlpha(float)} for {@link #ALPHA}). These properties can
552 * be used to set persistent state associated with these rendering-related properties on the view.
553 * The properties and methods can also be used in conjunction with
554 * {@link android.animation.Animator Animator}-based animations, described more in the
555 * <a href="#Animation">Animation</a> section.
556 * </p>
557 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 * <a name="Animation"></a>
559 * <h3>Animation</h3>
560 * <p>
Chet Haasecb150fe2012-05-03 15:15:05 -0700561 * Starting with Android 3.0, the preferred way of animating views is to use the
562 * {@link android.animation} package APIs. These {@link android.animation.Animator Animator}-based
563 * classes change actual properties of the View object, such as {@link #setAlpha(float) alpha} and
564 * {@link #setTranslationX(float) translationX}. This behavior is contrasted to that of the pre-3.0
565 * {@link android.view.animation.Animation Animation}-based classes, which instead animate only
566 * how the view is drawn on the display. In particular, the {@link ViewPropertyAnimator} class
567 * makes animating these View properties particularly easy and efficient.
568 * </p>
569 * <p>
570 * 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 -0800571 * You can attach an {@link Animation} object to a view using
572 * {@link #setAnimation(Animation)} or
573 * {@link #startAnimation(Animation)}. The animation can alter the scale,
574 * rotation, translation and alpha of a view over time. If the animation is
575 * attached to a view that has children, the animation will affect the entire
576 * subtree rooted by that node. When an animation is started, the framework will
577 * take care of redrawing the appropriate views until the animation completes.
578 * </p>
579 *
Jeff Brown85a31762010-09-01 17:01:00 -0700580 * <a name="Security"></a>
581 * <h3>Security</h3>
582 * <p>
583 * Sometimes it is essential that an application be able to verify that an action
584 * is being performed with the full knowledge and consent of the user, such as
585 * granting a permission request, making a purchase or clicking on an advertisement.
586 * Unfortunately, a malicious application could try to spoof the user into
587 * performing these actions, unaware, by concealing the intended purpose of the view.
588 * As a remedy, the framework offers a touch filtering mechanism that can be used to
589 * improve the security of views that provide access to sensitive functionality.
590 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700591 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800592 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700593 * will discard touches that are received whenever the view's window is obscured by
594 * another visible window. As a result, the view will not receive touches whenever a
595 * toast, dialog or other window appears above the view's window.
596 * </p><p>
597 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700598 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
599 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700600 * </p>
601 *
Romain Guy171c5922011-01-06 10:04:23 -0800602 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700603 * @attr ref android.R.styleable#View_background
604 * @attr ref android.R.styleable#View_clickable
605 * @attr ref android.R.styleable#View_contentDescription
606 * @attr ref android.R.styleable#View_drawingCacheQuality
607 * @attr ref android.R.styleable#View_duplicateParentState
608 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700609 * @attr ref android.R.styleable#View_requiresFadingEdge
Philip Milne6c8ea062012-04-03 17:38:43 -0700610 * @attr ref android.R.styleable#View_fadeScrollbars
Romain Guyd6a463a2009-05-21 23:10:10 -0700611 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700612 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700614 * @attr ref android.R.styleable#View_isScrollContainer
615 * @attr ref android.R.styleable#View_focusable
616 * @attr ref android.R.styleable#View_focusableInTouchMode
617 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
618 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800619 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700620 * @attr ref android.R.styleable#View_longClickable
621 * @attr ref android.R.styleable#View_minHeight
622 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 * @attr ref android.R.styleable#View_nextFocusDown
624 * @attr ref android.R.styleable#View_nextFocusLeft
625 * @attr ref android.R.styleable#View_nextFocusRight
626 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700627 * @attr ref android.R.styleable#View_onClick
628 * @attr ref android.R.styleable#View_padding
629 * @attr ref android.R.styleable#View_paddingBottom
630 * @attr ref android.R.styleable#View_paddingLeft
631 * @attr ref android.R.styleable#View_paddingRight
632 * @attr ref android.R.styleable#View_paddingTop
Fabrice Di Meglio101d5aa2012-02-16 18:36:06 -0800633 * @attr ref android.R.styleable#View_paddingStart
634 * @attr ref android.R.styleable#View_paddingEnd
Romain Guyd6a463a2009-05-21 23:10:10 -0700635 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800636 * @attr ref android.R.styleable#View_rotation
637 * @attr ref android.R.styleable#View_rotationX
638 * @attr ref android.R.styleable#View_rotationY
639 * @attr ref android.R.styleable#View_scaleX
640 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 * @attr ref android.R.styleable#View_scrollX
642 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700643 * @attr ref android.R.styleable#View_scrollbarSize
644 * @attr ref android.R.styleable#View_scrollbarStyle
645 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700646 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
647 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
649 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 * @attr ref android.R.styleable#View_scrollbarThumbVertical
651 * @attr ref android.R.styleable#View_scrollbarTrackVertical
652 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
653 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700654 * @attr ref android.R.styleable#View_soundEffectsEnabled
655 * @attr ref android.R.styleable#View_tag
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -0700656 * @attr ref android.R.styleable#View_textAlignment
Chet Haase73066682010-11-29 15:55:32 -0800657 * @attr ref android.R.styleable#View_transformPivotX
658 * @attr ref android.R.styleable#View_transformPivotY
659 * @attr ref android.R.styleable#View_translationX
660 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700661 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 *
663 * @see android.view.ViewGroup
664 */
Fabrice Di Megliob03b4342012-06-04 12:55:30 -0700665public class View implements Drawable.Callback, KeyEvent.Callback,
Adam Powell8fc54f92011-09-07 16:40:45 -0700666 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 private static final boolean DBG = false;
668
669 /**
670 * The logging tag used by this class with android.util.Log.
671 */
672 protected static final String VIEW_LOG_TAG = "View";
673
674 /**
Guang Zhu0d607fb2012-05-11 19:34:56 -0700675 * When set to true, apps will draw debugging information about their layouts.
Romain Guy4b8c4f82012-04-27 15:48:35 -0700676 *
677 * @hide
678 */
679 public static final String DEBUG_LAYOUT_PROPERTY = "debug.layout";
680
681 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 * Used to mark a View that has no ID.
683 */
684 public static final int NO_ID = -1;
685
686 /**
687 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
688 * calling setFlags.
689 */
690 private static final int NOT_FOCUSABLE = 0x00000000;
691
692 /**
693 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
694 * setFlags.
695 */
696 private static final int FOCUSABLE = 0x00000001;
697
698 /**
699 * Mask for use with setFlags indicating bits used for focus.
700 */
701 private static final int FOCUSABLE_MASK = 0x00000001;
702
703 /**
704 * This view will adjust its padding to fit sytem windows (e.g. status bar)
705 */
706 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
707
708 /**
Scott Main812634c22011-07-27 13:22:35 -0700709 * This view is visible.
710 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
711 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 */
713 public static final int VISIBLE = 0x00000000;
714
715 /**
716 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700717 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
718 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 */
720 public static final int INVISIBLE = 0x00000004;
721
722 /**
723 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700724 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
725 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726 */
727 public static final int GONE = 0x00000008;
728
729 /**
730 * Mask for use with setFlags indicating bits used for visibility.
731 * {@hide}
732 */
733 static final int VISIBILITY_MASK = 0x0000000C;
734
735 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
736
737 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700738 * This view is enabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800739 * Use with ENABLED_MASK when calling setFlags.
740 * {@hide}
741 */
742 static final int ENABLED = 0x00000000;
743
744 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700745 * This view is disabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 * Use with ENABLED_MASK when calling setFlags.
747 * {@hide}
748 */
749 static final int DISABLED = 0x00000020;
750
751 /**
752 * Mask for use with setFlags indicating bits used for indicating whether
753 * this view is enabled
754 * {@hide}
755 */
756 static final int ENABLED_MASK = 0x00000020;
757
758 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700759 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
760 * called and further optimizations will be performed. It is okay to have
761 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762 * {@hide}
763 */
764 static final int WILL_NOT_DRAW = 0x00000080;
765
766 /**
767 * Mask for use with setFlags indicating bits used for indicating whether
768 * this view is will draw
769 * {@hide}
770 */
771 static final int DRAW_MASK = 0x00000080;
772
773 /**
774 * <p>This view doesn't show scrollbars.</p>
775 * {@hide}
776 */
777 static final int SCROLLBARS_NONE = 0x00000000;
778
779 /**
780 * <p>This view shows horizontal scrollbars.</p>
781 * {@hide}
782 */
783 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
784
785 /**
786 * <p>This view shows vertical scrollbars.</p>
787 * {@hide}
788 */
789 static final int SCROLLBARS_VERTICAL = 0x00000200;
790
791 /**
792 * <p>Mask for use with setFlags indicating bits used for indicating which
793 * scrollbars are enabled.</p>
794 * {@hide}
795 */
796 static final int SCROLLBARS_MASK = 0x00000300;
797
Jeff Brown85a31762010-09-01 17:01:00 -0700798 /**
799 * Indicates that the view should filter touches when its window is obscured.
800 * Refer to the class comments for more information about this security feature.
801 * {@hide}
802 */
803 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
804
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700805 /**
806 * Set for framework elements that use FITS_SYSTEM_WINDOWS, to indicate
807 * that they are optional and should be skipped if the window has
808 * requested system UI flags that ignore those insets for layout.
809 */
810 static final int OPTIONAL_FITS_SYSTEM_WINDOWS = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800811
812 /**
813 * <p>This view doesn't show fading edges.</p>
814 * {@hide}
815 */
816 static final int FADING_EDGE_NONE = 0x00000000;
817
818 /**
819 * <p>This view shows horizontal fading edges.</p>
820 * {@hide}
821 */
822 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
823
824 /**
825 * <p>This view shows vertical fading edges.</p>
826 * {@hide}
827 */
828 static final int FADING_EDGE_VERTICAL = 0x00002000;
829
830 /**
831 * <p>Mask for use with setFlags indicating bits used for indicating which
832 * fading edges are enabled.</p>
833 * {@hide}
834 */
835 static final int FADING_EDGE_MASK = 0x00003000;
836
837 /**
838 * <p>Indicates this view can be clicked. When clickable, a View reacts
839 * to clicks by notifying the OnClickListener.<p>
840 * {@hide}
841 */
842 static final int CLICKABLE = 0x00004000;
843
844 /**
845 * <p>Indicates this view is caching its drawing into a bitmap.</p>
846 * {@hide}
847 */
848 static final int DRAWING_CACHE_ENABLED = 0x00008000;
849
850 /**
851 * <p>Indicates that no icicle should be saved for this view.<p>
852 * {@hide}
853 */
854 static final int SAVE_DISABLED = 0x000010000;
855
856 /**
857 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
858 * property.</p>
859 * {@hide}
860 */
861 static final int SAVE_DISABLED_MASK = 0x000010000;
862
863 /**
864 * <p>Indicates that no drawing cache should ever be created for this view.<p>
865 * {@hide}
866 */
867 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
868
869 /**
870 * <p>Indicates this view can take / keep focus when int touch mode.</p>
871 * {@hide}
872 */
873 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
874
875 /**
876 * <p>Enables low quality mode for the drawing cache.</p>
877 */
878 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
879
880 /**
881 * <p>Enables high quality mode for the drawing cache.</p>
882 */
883 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
884
885 /**
886 * <p>Enables automatic quality mode for the drawing cache.</p>
887 */
888 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
889
890 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
891 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
892 };
893
894 /**
895 * <p>Mask for use with setFlags indicating bits used for the cache
896 * quality property.</p>
897 * {@hide}
898 */
899 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
900
901 /**
902 * <p>
903 * Indicates this view can be long clicked. When long clickable, a View
904 * reacts to long clicks by notifying the OnLongClickListener or showing a
905 * context menu.
906 * </p>
907 * {@hide}
908 */
909 static final int LONG_CLICKABLE = 0x00200000;
910
911 /**
912 * <p>Indicates that this view gets its drawable states from its direct parent
913 * and ignores its original internal states.</p>
914 *
915 * @hide
916 */
917 static final int DUPLICATE_PARENT_STATE = 0x00400000;
918
919 /**
920 * The scrollbar style to display the scrollbars inside the content area,
921 * without increasing the padding. The scrollbars will be overlaid with
922 * translucency on the view's content.
923 */
924 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
925
926 /**
927 * The scrollbar style to display the scrollbars inside the padded area,
928 * increasing the padding of the view. The scrollbars will not overlap the
929 * content area of the view.
930 */
931 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
932
933 /**
934 * The scrollbar style to display the scrollbars at the edge of the view,
935 * without increasing the padding. The scrollbars will be overlaid with
936 * translucency.
937 */
938 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
939
940 /**
941 * The scrollbar style to display the scrollbars at the edge of the view,
942 * increasing the padding of the view. The scrollbars will only overlap the
943 * background, if any.
944 */
945 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
946
947 /**
948 * Mask to check if the scrollbar style is overlay or inset.
949 * {@hide}
950 */
951 static final int SCROLLBARS_INSET_MASK = 0x01000000;
952
953 /**
954 * Mask to check if the scrollbar style is inside or outside.
955 * {@hide}
956 */
957 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
958
959 /**
960 * Mask for scrollbar style.
961 * {@hide}
962 */
963 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
964
965 /**
966 * View flag indicating that the screen should remain on while the
967 * window containing this view is visible to the user. This effectively
968 * takes care of automatically setting the WindowManager's
969 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
970 */
971 public static final int KEEP_SCREEN_ON = 0x04000000;
972
973 /**
974 * View flag indicating whether this view should have sound effects enabled
975 * for events such as clicking and touching.
976 */
977 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
978
979 /**
980 * View flag indicating whether this view should have haptic feedback
981 * enabled for events such as long presses.
982 */
983 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
984
985 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700986 * <p>Indicates that the view hierarchy should stop saving state when
987 * it reaches this view. If state saving is initiated immediately at
988 * the view, it will be allowed.
989 * {@hide}
990 */
991 static final int PARENT_SAVE_DISABLED = 0x20000000;
992
993 /**
994 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
995 * {@hide}
996 */
997 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
998
999 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07001000 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1001 * should add all focusable Views regardless if they are focusable in touch mode.
1002 */
1003 public static final int FOCUSABLES_ALL = 0x00000000;
1004
1005 /**
1006 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1007 * should add only Views focusable in touch mode.
1008 */
1009 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1010
1011 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001012 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 * item.
1014 */
1015 public static final int FOCUS_BACKWARD = 0x00000001;
1016
1017 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001018 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 * item.
1020 */
1021 public static final int FOCUS_FORWARD = 0x00000002;
1022
1023 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001024 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 */
1026 public static final int FOCUS_LEFT = 0x00000011;
1027
1028 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001029 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 */
1031 public static final int FOCUS_UP = 0x00000021;
1032
1033 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001034 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 */
1036 public static final int FOCUS_RIGHT = 0x00000042;
1037
1038 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001039 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 */
1041 public static final int FOCUS_DOWN = 0x00000082;
1042
Svetoslav Ganov42138042012-03-20 11:51:39 -07001043 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001044 * Bits of {@link #getMeasuredWidthAndState()} and
1045 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1046 */
1047 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1048
1049 /**
1050 * Bits of {@link #getMeasuredWidthAndState()} and
1051 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1052 */
1053 public static final int MEASURED_STATE_MASK = 0xff000000;
1054
1055 /**
1056 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1057 * for functions that combine both width and height into a single int,
1058 * such as {@link #getMeasuredState()} and the childState argument of
1059 * {@link #resolveSizeAndState(int, int, int)}.
1060 */
1061 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1062
1063 /**
1064 * Bit of {@link #getMeasuredWidthAndState()} and
1065 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1066 * is smaller that the space the view would like to have.
1067 */
1068 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1069
1070 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001071 * Base View state sets
1072 */
1073 // Singles
1074 /**
1075 * Indicates the view has no states set. States are used with
1076 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1077 * view depending on its state.
1078 *
1079 * @see android.graphics.drawable.Drawable
1080 * @see #getDrawableState()
1081 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001082 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 /**
1084 * Indicates the view is enabled. States are used with
1085 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1086 * view depending on its state.
1087 *
1088 * @see android.graphics.drawable.Drawable
1089 * @see #getDrawableState()
1090 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001091 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 /**
1093 * Indicates the view is focused. States are used with
1094 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1095 * view depending on its state.
1096 *
1097 * @see android.graphics.drawable.Drawable
1098 * @see #getDrawableState()
1099 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001100 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 /**
1102 * Indicates the view is selected. States are used with
1103 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1104 * view depending on its state.
1105 *
1106 * @see android.graphics.drawable.Drawable
1107 * @see #getDrawableState()
1108 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001109 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 /**
1111 * Indicates the view is pressed. States are used with
1112 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1113 * view depending on its state.
1114 *
1115 * @see android.graphics.drawable.Drawable
1116 * @see #getDrawableState()
1117 * @hide
1118 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001119 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 /**
1121 * Indicates the view's window has focus. States are used with
1122 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1123 * view depending on its state.
1124 *
1125 * @see android.graphics.drawable.Drawable
1126 * @see #getDrawableState()
1127 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001128 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001129 // Doubles
1130 /**
1131 * Indicates the view is enabled and has the focus.
1132 *
1133 * @see #ENABLED_STATE_SET
1134 * @see #FOCUSED_STATE_SET
1135 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001136 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 /**
1138 * Indicates the view is enabled and selected.
1139 *
1140 * @see #ENABLED_STATE_SET
1141 * @see #SELECTED_STATE_SET
1142 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001143 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 /**
1145 * Indicates the view is enabled and that its window has focus.
1146 *
1147 * @see #ENABLED_STATE_SET
1148 * @see #WINDOW_FOCUSED_STATE_SET
1149 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001150 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 /**
1152 * Indicates the view is focused and selected.
1153 *
1154 * @see #FOCUSED_STATE_SET
1155 * @see #SELECTED_STATE_SET
1156 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001157 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 /**
1159 * Indicates the view has the focus and that its window has the focus.
1160 *
1161 * @see #FOCUSED_STATE_SET
1162 * @see #WINDOW_FOCUSED_STATE_SET
1163 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001164 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 /**
1166 * Indicates the view is selected and that its window has the focus.
1167 *
1168 * @see #SELECTED_STATE_SET
1169 * @see #WINDOW_FOCUSED_STATE_SET
1170 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001171 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001172 // Triples
1173 /**
1174 * Indicates the view is enabled, focused and selected.
1175 *
1176 * @see #ENABLED_STATE_SET
1177 * @see #FOCUSED_STATE_SET
1178 * @see #SELECTED_STATE_SET
1179 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001180 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 /**
1182 * Indicates the view is enabled, focused and its window has the focus.
1183 *
1184 * @see #ENABLED_STATE_SET
1185 * @see #FOCUSED_STATE_SET
1186 * @see #WINDOW_FOCUSED_STATE_SET
1187 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001188 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 /**
1190 * Indicates the view is enabled, selected and its window has the focus.
1191 *
1192 * @see #ENABLED_STATE_SET
1193 * @see #SELECTED_STATE_SET
1194 * @see #WINDOW_FOCUSED_STATE_SET
1195 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001196 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 /**
1198 * Indicates the view is focused, selected and its window has the focus.
1199 *
1200 * @see #FOCUSED_STATE_SET
1201 * @see #SELECTED_STATE_SET
1202 * @see #WINDOW_FOCUSED_STATE_SET
1203 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001204 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 /**
1206 * Indicates the view is enabled, focused, selected and its window
1207 * has the focus.
1208 *
1209 * @see #ENABLED_STATE_SET
1210 * @see #FOCUSED_STATE_SET
1211 * @see #SELECTED_STATE_SET
1212 * @see #WINDOW_FOCUSED_STATE_SET
1213 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001214 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 /**
1216 * Indicates the view is pressed and its window has the focus.
1217 *
1218 * @see #PRESSED_STATE_SET
1219 * @see #WINDOW_FOCUSED_STATE_SET
1220 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001221 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 /**
1223 * Indicates the view is pressed and selected.
1224 *
1225 * @see #PRESSED_STATE_SET
1226 * @see #SELECTED_STATE_SET
1227 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001228 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 /**
1230 * Indicates the view is pressed, selected and its window has the focus.
1231 *
1232 * @see #PRESSED_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[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 /**
1238 * Indicates the view is pressed and focused.
1239 *
1240 * @see #PRESSED_STATE_SET
1241 * @see #FOCUSED_STATE_SET
1242 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001243 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 /**
1245 * Indicates the view is pressed, focused and its window has the focus.
1246 *
1247 * @see #PRESSED_STATE_SET
1248 * @see #FOCUSED_STATE_SET
1249 * @see #WINDOW_FOCUSED_STATE_SET
1250 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001251 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 /**
1253 * Indicates the view is pressed, focused and selected.
1254 *
1255 * @see #PRESSED_STATE_SET
1256 * @see #SELECTED_STATE_SET
1257 * @see #FOCUSED_STATE_SET
1258 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001259 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 /**
1261 * Indicates the view is pressed, focused, selected and its window has the focus.
1262 *
1263 * @see #PRESSED_STATE_SET
1264 * @see #FOCUSED_STATE_SET
1265 * @see #SELECTED_STATE_SET
1266 * @see #WINDOW_FOCUSED_STATE_SET
1267 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001268 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269 /**
1270 * Indicates the view is pressed and enabled.
1271 *
1272 * @see #PRESSED_STATE_SET
1273 * @see #ENABLED_STATE_SET
1274 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001275 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 /**
1277 * Indicates the view is pressed, enabled and its window has the focus.
1278 *
1279 * @see #PRESSED_STATE_SET
1280 * @see #ENABLED_STATE_SET
1281 * @see #WINDOW_FOCUSED_STATE_SET
1282 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001283 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 /**
1285 * Indicates the view is pressed, enabled and selected.
1286 *
1287 * @see #PRESSED_STATE_SET
1288 * @see #ENABLED_STATE_SET
1289 * @see #SELECTED_STATE_SET
1290 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001291 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 /**
1293 * Indicates the view is pressed, enabled, selected and its window has the
1294 * focus.
1295 *
1296 * @see #PRESSED_STATE_SET
1297 * @see #ENABLED_STATE_SET
1298 * @see #SELECTED_STATE_SET
1299 * @see #WINDOW_FOCUSED_STATE_SET
1300 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001301 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001302 /**
1303 * Indicates the view is pressed, enabled and focused.
1304 *
1305 * @see #PRESSED_STATE_SET
1306 * @see #ENABLED_STATE_SET
1307 * @see #FOCUSED_STATE_SET
1308 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001309 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 /**
1311 * Indicates the view is pressed, enabled, focused and its window has the
1312 * focus.
1313 *
1314 * @see #PRESSED_STATE_SET
1315 * @see #ENABLED_STATE_SET
1316 * @see #FOCUSED_STATE_SET
1317 * @see #WINDOW_FOCUSED_STATE_SET
1318 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001319 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 /**
1321 * Indicates the view is pressed, enabled, focused and selected.
1322 *
1323 * @see #PRESSED_STATE_SET
1324 * @see #ENABLED_STATE_SET
1325 * @see #SELECTED_STATE_SET
1326 * @see #FOCUSED_STATE_SET
1327 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001328 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 /**
1330 * Indicates the view is pressed, enabled, focused, selected and its window
1331 * has the focus.
1332 *
1333 * @see #PRESSED_STATE_SET
1334 * @see #ENABLED_STATE_SET
1335 * @see #SELECTED_STATE_SET
1336 * @see #FOCUSED_STATE_SET
1337 * @see #WINDOW_FOCUSED_STATE_SET
1338 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001339 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340
1341 /**
1342 * The order here is very important to {@link #getDrawableState()}
1343 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001344 private static final int[][] VIEW_STATE_SETS;
1345
Romain Guyb051e892010-09-28 19:09:36 -07001346 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1347 static final int VIEW_STATE_SELECTED = 1 << 1;
1348 static final int VIEW_STATE_FOCUSED = 1 << 2;
1349 static final int VIEW_STATE_ENABLED = 1 << 3;
1350 static final int VIEW_STATE_PRESSED = 1 << 4;
1351 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001352 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001353 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001354 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1355 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001356
1357 static final int[] VIEW_STATE_IDS = new int[] {
1358 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1359 R.attr.state_selected, VIEW_STATE_SELECTED,
1360 R.attr.state_focused, VIEW_STATE_FOCUSED,
1361 R.attr.state_enabled, VIEW_STATE_ENABLED,
1362 R.attr.state_pressed, VIEW_STATE_PRESSED,
1363 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001364 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001365 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001366 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
Svetoslav Ganov42138042012-03-20 11:51:39 -07001367 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001368 };
1369
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001370 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001371 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1372 throw new IllegalStateException(
1373 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1374 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001375 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001376 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001377 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001378 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001379 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001380 orderedIds[i * 2] = viewState;
1381 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001382 }
1383 }
1384 }
Romain Guyb051e892010-09-28 19:09:36 -07001385 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1386 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1387 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001388 int numBits = Integer.bitCount(i);
1389 int[] set = new int[numBits];
1390 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001391 for (int j = 0; j < orderedIds.length; j += 2) {
1392 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001393 set[pos++] = orderedIds[j];
1394 }
1395 }
1396 VIEW_STATE_SETS[i] = set;
1397 }
1398
1399 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1400 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1401 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1402 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1403 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1404 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1405 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1406 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1407 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1408 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1409 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1410 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1411 | VIEW_STATE_FOCUSED];
1412 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1413 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1414 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1415 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1416 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1417 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1418 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1419 | VIEW_STATE_ENABLED];
1420 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1421 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1422 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1423 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1424 | VIEW_STATE_ENABLED];
1425 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1426 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1427 | VIEW_STATE_ENABLED];
1428 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1429 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1430 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1431
1432 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1433 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1434 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1435 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1436 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1437 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1438 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1439 | VIEW_STATE_PRESSED];
1440 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1441 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1442 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1443 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1444 | VIEW_STATE_PRESSED];
1445 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1446 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1447 | VIEW_STATE_PRESSED];
1448 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1449 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1450 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1451 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1452 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1453 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1454 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1455 | VIEW_STATE_PRESSED];
1456 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1457 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1458 | VIEW_STATE_PRESSED];
1459 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1460 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1461 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1462 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1463 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1464 | VIEW_STATE_PRESSED];
1465 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1466 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1467 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1468 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1469 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1470 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1471 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1472 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1473 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1474 | VIEW_STATE_PRESSED];
1475 }
1476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001477 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001478 * Accessibility event types that are dispatched for text population.
1479 */
1480 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1481 AccessibilityEvent.TYPE_VIEW_CLICKED
1482 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1483 | AccessibilityEvent.TYPE_VIEW_SELECTED
1484 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1485 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1486 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001487 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
Svetoslav Ganov84dd52e2011-11-18 10:24:00 -08001488 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
Svetoslav Ganov42138042012-03-20 11:51:39 -07001489 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001490 | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED
1491 | AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001492
1493 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001494 * Temporary Rect currently for use in setBackground(). This will probably
1495 * be extended in the future to hold our own class with more than just
1496 * a Rect. :)
1497 */
1498 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001499
1500 /**
1501 * Map used to store views' tags.
1502 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001503 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001506 * The next available accessiiblity id.
1507 */
1508 private static int sNextAccessibilityViewId;
1509
1510 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 * The animation currently associated with this view.
1512 * @hide
1513 */
1514 protected Animation mCurrentAnimation = null;
1515
1516 /**
1517 * Width as measured during measure pass.
1518 * {@hide}
1519 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001520 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001521 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522
1523 /**
1524 * Height as measured during measure pass.
1525 * {@hide}
1526 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001527 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001528 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529
1530 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001531 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1532 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1533 * its display list. This flag, used only when hw accelerated, allows us to clear the
1534 * flag while retaining this information until it's needed (at getDisplayList() time and
1535 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1536 *
1537 * {@hide}
1538 */
1539 boolean mRecreateDisplayList = false;
1540
1541 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001542 * The view's identifier.
1543 * {@hide}
1544 *
1545 * @see #setId(int)
1546 * @see #getId()
1547 */
1548 @ViewDebug.ExportedProperty(resolveId = true)
1549 int mID = NO_ID;
1550
1551 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001552 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001553 */
1554 int mAccessibilityViewId = NO_ID;
1555
1556 /**
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001557 * @hide
1558 */
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07001559 private int mAccessibilityCursorPosition = ACCESSIBILITY_CURSOR_POSITION_UNDEFINED;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001560
1561 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562 * The view's tag.
1563 * {@hide}
1564 *
1565 * @see #setTag(Object)
1566 * @see #getTag()
1567 */
1568 protected Object mTag;
1569
1570 // for mPrivateFlags:
1571 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001572 static final int PFLAG_WANTS_FOCUS = 0x00000001;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001573 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001574 static final int PFLAG_FOCUSED = 0x00000002;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001576 static final int PFLAG_SELECTED = 0x00000004;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001577 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001578 static final int PFLAG_IS_ROOT_NAMESPACE = 0x00000008;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001579 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001580 static final int PFLAG_HAS_BOUNDS = 0x00000010;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001582 static final int PFLAG_DRAWN = 0x00000020;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001583 /**
1584 * When this flag is set, this view is running an animation on behalf of its
1585 * children and should therefore not cancel invalidate requests, even if they
1586 * lie outside of this view's bounds.
1587 *
1588 * {@hide}
1589 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001590 static final int PFLAG_DRAW_ANIMATION = 0x00000040;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001592 static final int PFLAG_SKIP_DRAW = 0x00000080;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001593 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001594 static final int PFLAG_ONLY_DRAWS_BACKGROUND = 0x00000100;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001595 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001596 static final int PFLAG_REQUEST_TRANSPARENT_REGIONS = 0x00000200;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001597 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001598 static final int PFLAG_DRAWABLE_STATE_DIRTY = 0x00000400;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001600 static final int PFLAG_MEASURED_DIMENSION_SET = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001602 static final int PFLAG_FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001603 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001604 static final int PFLAG_LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001605
Dianne Hackborn4702a852012-08-17 15:18:29 -07001606 private static final int PFLAG_PRESSED = 0x00004000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001607
1608 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001609 static final int PFLAG_DRAWING_CACHE_VALID = 0x00008000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001610 /**
1611 * Flag used to indicate that this view should be drawn once more (and only once
1612 * more) after its animation has completed.
1613 * {@hide}
1614 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001615 static final int PFLAG_ANIMATION_STARTED = 0x00010000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616
Dianne Hackborn4702a852012-08-17 15:18:29 -07001617 private static final int PFLAG_SAVE_STATE_CALLED = 0x00020000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618
1619 /**
1620 * Indicates that the View returned true when onSetAlpha() was called and that
1621 * the alpha must be restored.
1622 * {@hide}
1623 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001624 static final int PFLAG_ALPHA_SET = 0x00040000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001625
1626 /**
1627 * Set by {@link #setScrollContainer(boolean)}.
1628 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001629 static final int PFLAG_SCROLL_CONTAINER = 0x00080000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001630
1631 /**
1632 * Set by {@link #setScrollContainer(boolean)}.
1633 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001634 static final int PFLAG_SCROLL_CONTAINER_ADDED = 0x00100000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001635
1636 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001637 * View flag indicating whether this view was invalidated (fully or partially.)
1638 *
1639 * @hide
1640 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001641 static final int PFLAG_DIRTY = 0x00200000;
Romain Guy809a7f62009-05-14 15:44:42 -07001642
1643 /**
1644 * View flag indicating whether this view was invalidated by an opaque
1645 * invalidate request.
1646 *
1647 * @hide
1648 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001649 static final int PFLAG_DIRTY_OPAQUE = 0x00400000;
Romain Guy809a7f62009-05-14 15:44:42 -07001650
1651 /**
Dianne Hackborn4702a852012-08-17 15:18:29 -07001652 * Mask for {@link #PFLAG_DIRTY} and {@link #PFLAG_DIRTY_OPAQUE}.
Romain Guy809a7f62009-05-14 15:44:42 -07001653 *
1654 * @hide
1655 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001656 static final int PFLAG_DIRTY_MASK = 0x00600000;
Romain Guy809a7f62009-05-14 15:44:42 -07001657
1658 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001659 * Indicates whether the background is opaque.
1660 *
1661 * @hide
1662 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001663 static final int PFLAG_OPAQUE_BACKGROUND = 0x00800000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001664
1665 /**
1666 * Indicates whether the scrollbars are opaque.
1667 *
1668 * @hide
1669 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001670 static final int PFLAG_OPAQUE_SCROLLBARS = 0x01000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001671
1672 /**
1673 * Indicates whether the view is opaque.
1674 *
1675 * @hide
1676 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001677 static final int PFLAG_OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001678
Adam Powelle14579b2009-12-16 18:39:52 -08001679 /**
1680 * Indicates a prepressed state;
1681 * the short time between ACTION_DOWN and recognizing
1682 * a 'real' press. Prepressed is used to recognize quick taps
1683 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001684 *
Adam Powelle14579b2009-12-16 18:39:52 -08001685 * @hide
1686 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001687 private static final int PFLAG_PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001688
Adam Powellc9fbaab2010-02-16 17:16:19 -08001689 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001690 * Indicates whether the view is temporarily detached.
1691 *
1692 * @hide
1693 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001694 static final int PFLAG_CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001695
Adam Powell8568c3a2010-04-19 14:26:11 -07001696 /**
1697 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001698 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001699 * @hide
1700 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001701 private static final int PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001702
1703 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001704 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1705 * @hide
1706 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001707 private static final int PFLAG_HOVERED = 0x10000000;
Jeff Browna032cc02011-03-07 16:56:21 -08001708
1709 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001710 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1711 * for transform operations
1712 *
1713 * @hide
1714 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001715 private static final int PFLAG_PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001716
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001717 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001718 static final int PFLAG_ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001719
Chet Haasefd2b0022010-08-06 13:08:56 -07001720 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001721 * Indicates that this view was specifically invalidated, not just dirtied because some
1722 * child view was invalidated. The flag is used to determine when we need to recreate
1723 * a view's display list (as opposed to just returning a reference to its existing
1724 * display list).
1725 *
1726 * @hide
1727 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001728 static final int PFLAG_INVALIDATED = 0x80000000;
Chet Haasedaf98e92011-01-10 14:10:36 -08001729
Christopher Tate3d4bf172011-03-28 16:16:46 -07001730 /* Masks for mPrivateFlags2 */
1731
1732 /**
1733 * Indicates that this view has reported that it can accept the current drag's content.
1734 * Cleared when the drag operation concludes.
1735 * @hide
1736 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001737 static final int PFLAG2_DRAG_CAN_ACCEPT = 0x00000001;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001738
1739 /**
1740 * Indicates that this view is currently directly under the drag location in a
1741 * drag-and-drop operation involving content that it can accept. Cleared when
1742 * the drag exits the view, or when the drag operation concludes.
1743 * @hide
1744 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001745 static final int PFLAG2_DRAG_HOVERED = 0x00000002;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001746
Cibu Johny86666632010-02-22 13:01:02 -08001747 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001748 * Horizontal layout direction of this view is from Left to Right.
1749 * Use with {@link #setLayoutDirection}.
Cibu Johny86666632010-02-22 13:01:02 -08001750 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001751 public static final int LAYOUT_DIRECTION_LTR = 0;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001752
1753 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001754 * Horizontal layout direction of this view is from Right to Left.
1755 * Use with {@link #setLayoutDirection}.
1756 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001757 public static final int LAYOUT_DIRECTION_RTL = 1;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001758
1759 /**
1760 * Horizontal layout direction of this view is inherited from its parent.
1761 * Use with {@link #setLayoutDirection}.
1762 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001763 public static final int LAYOUT_DIRECTION_INHERIT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001764
1765 /**
1766 * Horizontal layout direction of this view is from deduced from the default language
1767 * script for the locale. Use with {@link #setLayoutDirection}.
1768 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001769 public static final int LAYOUT_DIRECTION_LOCALE = 3;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001770
1771 /**
1772 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001773 * @hide
1774 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001775 static final int PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001776
1777 /**
1778 * Mask for use with private flags indicating bits used for horizontal layout direction.
1779 * @hide
1780 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001781 static final int PFLAG2_LAYOUT_DIRECTION_MASK = 0x00000003 << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001782
1783 /**
1784 * Indicates whether the view horizontal layout direction has been resolved and drawn to the
1785 * right-to-left direction.
1786 * @hide
1787 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001788 static final int PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL = 4 << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001789
1790 /**
1791 * Indicates whether the view horizontal layout direction has been resolved.
1792 * @hide
1793 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001794 static final int PFLAG2_LAYOUT_DIRECTION_RESOLVED = 8 << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001795
1796 /**
1797 * Mask for use with private flags indicating bits used for resolved horizontal layout direction.
1798 * @hide
1799 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001800 static final int PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK = 0x0000000C
1801 << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001802
1803 /*
1804 * Array of horizontal layout direction flags for mapping attribute "layoutDirection" to correct
1805 * flag value.
1806 * @hide
1807 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001808 private static final int[] LAYOUT_DIRECTION_FLAGS = {
1809 LAYOUT_DIRECTION_LTR,
1810 LAYOUT_DIRECTION_RTL,
1811 LAYOUT_DIRECTION_INHERIT,
1812 LAYOUT_DIRECTION_LOCALE
1813 };
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001814
1815 /**
1816 * Default horizontal layout direction.
1817 * @hide
1818 */
1819 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001820
Adam Powell539ee872012-02-03 19:00:49 -08001821 /**
1822 * Indicates that the view is tracking some sort of transient state
1823 * that the app should not need to be aware of, but that the framework
1824 * should take special care to preserve.
1825 *
1826 * @hide
1827 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001828 static final int PFLAG2_HAS_TRANSIENT_STATE = 0x00000100;
Adam Powell539ee872012-02-03 19:00:49 -08001829
1830
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001831 /**
1832 * Text direction is inherited thru {@link ViewGroup}
1833 */
1834 public static final int TEXT_DIRECTION_INHERIT = 0;
1835
1836 /**
1837 * Text direction is using "first strong algorithm". The first strong directional character
1838 * determines the paragraph direction. If there is no strong directional character, the
1839 * paragraph direction is the view's resolved layout direction.
1840 */
1841 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
1842
1843 /**
1844 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
1845 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
1846 * If there are neither, the paragraph direction is the view's resolved layout direction.
1847 */
1848 public static final int TEXT_DIRECTION_ANY_RTL = 2;
1849
1850 /**
1851 * Text direction is forced to LTR.
1852 */
1853 public static final int TEXT_DIRECTION_LTR = 3;
1854
1855 /**
1856 * Text direction is forced to RTL.
1857 */
1858 public static final int TEXT_DIRECTION_RTL = 4;
1859
1860 /**
1861 * Text direction is coming from the system Locale.
1862 */
1863 public static final int TEXT_DIRECTION_LOCALE = 5;
1864
1865 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001866 * Default text direction is inherited
1867 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001868 public static int TEXT_DIRECTION_DEFAULT = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001869
1870 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001871 * Bit shift to get the horizontal layout direction. (bits after LAYOUT_DIRECTION_RESOLVED)
1872 * @hide
1873 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001874 static final int PFLAG2_TEXT_DIRECTION_MASK_SHIFT = 6;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001875
1876 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001877 * Mask for use with private flags indicating bits used for text direction.
1878 * @hide
1879 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001880 static final int PFLAG2_TEXT_DIRECTION_MASK = 0x00000007
1881 << PFLAG2_TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001882
1883 /**
1884 * Array of text direction flags for mapping attribute "textDirection" to correct
1885 * flag value.
1886 * @hide
1887 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001888 private static final int[] PFLAG2_TEXT_DIRECTION_FLAGS = {
1889 TEXT_DIRECTION_INHERIT << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1890 TEXT_DIRECTION_FIRST_STRONG << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1891 TEXT_DIRECTION_ANY_RTL << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1892 TEXT_DIRECTION_LTR << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1893 TEXT_DIRECTION_RTL << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1894 TEXT_DIRECTION_LOCALE << PFLAG2_TEXT_DIRECTION_MASK_SHIFT
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001895 };
1896
1897 /**
1898 * Indicates whether the view text direction has been resolved.
1899 * @hide
1900 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001901 static final int PFLAG2_TEXT_DIRECTION_RESOLVED = 0x00000008
1902 << PFLAG2_TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001903
1904 /**
1905 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1906 * @hide
1907 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001908 static final int PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT = 10;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001909
1910 /**
1911 * Mask for use with private flags indicating bits used for resolved text direction.
1912 * @hide
1913 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001914 static final int PFLAG2_TEXT_DIRECTION_RESOLVED_MASK = 0x00000007
1915 << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001916
1917 /**
1918 * Indicates whether the view text direction has been resolved to the "first strong" heuristic.
1919 * @hide
1920 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001921 static final int PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT =
1922 TEXT_DIRECTION_FIRST_STRONG << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001923
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001924 /*
1925 * Default text alignment. The text alignment of this View is inherited from its parent.
1926 * Use with {@link #setTextAlignment(int)}
1927 */
1928 public static final int TEXT_ALIGNMENT_INHERIT = 0;
1929
1930 /**
1931 * Default for the root view. The gravity determines the text alignment, ALIGN_NORMAL,
1932 * ALIGN_CENTER, or ALIGN_OPPOSITE, which are relative to each paragraph’s text direction.
1933 *
1934 * Use with {@link #setTextAlignment(int)}
1935 */
1936 public static final int TEXT_ALIGNMENT_GRAVITY = 1;
1937
1938 /**
1939 * Align to the start of the paragraph, e.g. ALIGN_NORMAL.
1940 *
1941 * Use with {@link #setTextAlignment(int)}
1942 */
1943 public static final int TEXT_ALIGNMENT_TEXT_START = 2;
1944
1945 /**
1946 * Align to the end of the paragraph, e.g. ALIGN_OPPOSITE.
1947 *
1948 * Use with {@link #setTextAlignment(int)}
1949 */
1950 public static final int TEXT_ALIGNMENT_TEXT_END = 3;
1951
1952 /**
1953 * Center the paragraph, e.g. ALIGN_CENTER.
1954 *
1955 * Use with {@link #setTextAlignment(int)}
1956 */
1957 public static final int TEXT_ALIGNMENT_CENTER = 4;
1958
1959 /**
1960 * Align to the start of the view, which is ALIGN_LEFT if the view’s resolved
1961 * layoutDirection is LTR, and ALIGN_RIGHT otherwise.
1962 *
1963 * Use with {@link #setTextAlignment(int)}
1964 */
1965 public static final int TEXT_ALIGNMENT_VIEW_START = 5;
1966
1967 /**
1968 * Align to the end of the view, which is ALIGN_RIGHT if the view’s resolved
1969 * layoutDirection is LTR, and ALIGN_LEFT otherwise.
1970 *
1971 * Use with {@link #setTextAlignment(int)}
1972 */
1973 public static final int TEXT_ALIGNMENT_VIEW_END = 6;
1974
1975 /**
1976 * Default text alignment is inherited
1977 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001978 public static int TEXT_ALIGNMENT_DEFAULT = TEXT_ALIGNMENT_GRAVITY;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001979
1980 /**
1981 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1982 * @hide
1983 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001984 static final int PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT = 13;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001985
1986 /**
1987 * Mask for use with private flags indicating bits used for text alignment.
1988 * @hide
1989 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001990 static final int PFLAG2_TEXT_ALIGNMENT_MASK = 0x00000007 << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001991
1992 /**
1993 * Array of text direction flags for mapping attribute "textAlignment" to correct
1994 * flag value.
1995 * @hide
1996 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001997 private static final int[] PFLAG2_TEXT_ALIGNMENT_FLAGS = {
1998 TEXT_ALIGNMENT_INHERIT << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
1999 TEXT_ALIGNMENT_GRAVITY << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2000 TEXT_ALIGNMENT_TEXT_START << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2001 TEXT_ALIGNMENT_TEXT_END << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2002 TEXT_ALIGNMENT_CENTER << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2003 TEXT_ALIGNMENT_VIEW_START << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2004 TEXT_ALIGNMENT_VIEW_END << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002005 };
2006
2007 /**
2008 * Indicates whether the view text alignment has been resolved.
2009 * @hide
2010 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002011 static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED = 0x00000008 << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002012
2013 /**
2014 * Bit shift to get the resolved text alignment.
2015 * @hide
2016 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002017 static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT = 17;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002018
2019 /**
2020 * Mask for use with private flags indicating bits used for text alignment.
2021 * @hide
2022 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002023 static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK = 0x00000007
2024 << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002025
2026 /**
2027 * Indicates whether if the view text alignment has been resolved to gravity
2028 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002029 private static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT =
2030 TEXT_ALIGNMENT_GRAVITY << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002031
Svetoslav Ganov42138042012-03-20 11:51:39 -07002032 // Accessiblity constants for mPrivateFlags2
2033
2034 /**
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07002035 * Shift for the bits in {@link #mPrivateFlags2} related to the
2036 * "importantForAccessibility" attribute.
Svetoslav Ganov42138042012-03-20 11:51:39 -07002037 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002038 static final int PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT = 20;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002039
2040 /**
2041 * Automatically determine whether a view is important for accessibility.
2042 */
2043 public static final int IMPORTANT_FOR_ACCESSIBILITY_AUTO = 0x00000000;
2044
2045 /**
2046 * The view is important for accessibility.
2047 */
2048 public static final int IMPORTANT_FOR_ACCESSIBILITY_YES = 0x00000001;
2049
2050 /**
2051 * The view is not important for accessibility.
2052 */
2053 public static final int IMPORTANT_FOR_ACCESSIBILITY_NO = 0x00000002;
2054
2055 /**
2056 * The default whether the view is important for accessiblity.
2057 */
2058 static final int IMPORTANT_FOR_ACCESSIBILITY_DEFAULT = IMPORTANT_FOR_ACCESSIBILITY_AUTO;
2059
2060 /**
2061 * Mask for obtainig the bits which specify how to determine
2062 * whether a view is important for accessibility.
2063 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002064 static final int PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK = (IMPORTANT_FOR_ACCESSIBILITY_AUTO
Svetoslav Ganov42138042012-03-20 11:51:39 -07002065 | IMPORTANT_FOR_ACCESSIBILITY_YES | IMPORTANT_FOR_ACCESSIBILITY_NO)
Dianne Hackborn4702a852012-08-17 15:18:29 -07002066 << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002067
2068 /**
2069 * Flag indicating whether a view has accessibility focus.
2070 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002071 static final int PFLAG2_ACCESSIBILITY_FOCUSED = 0x00000040 << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002072
2073 /**
2074 * Flag indicating whether a view state for accessibility has changed.
2075 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002076 static final int PFLAG2_ACCESSIBILITY_STATE_CHANGED = 0x00000080
2077 << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07002078
Chet Haaseafd5c3e2012-05-10 13:21:10 -07002079 /**
Chet Haase1a3ab172012-05-11 08:41:20 -07002080 * Flag indicating whether a view failed the quickReject() check in draw(). This condition
2081 * is used to check whether later changes to the view's transform should invalidate the
2082 * view to force the quickReject test to run again.
2083 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002084 static final int PFLAG2_VIEW_QUICK_REJECTED = 0x10000000;
Chet Haase1a3ab172012-05-11 08:41:20 -07002085
Adam Powell0090f202012-08-07 17:15:30 -07002086 /**
2087 * Flag indicating that start/end padding has been resolved into left/right padding
2088 * for use in measurement, layout, drawing, etc. This is set by {@link #resolvePadding()}
2089 * and checked by {@link #measure(int, int)} to determine if padding needs to be resolved
2090 * during measurement. In some special cases this is required such as when an adapter-based
2091 * view measures prospective children without attaching them to a window.
2092 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002093 static final int PFLAG2_PADDING_RESOLVED = 0x20000000;
Adam Powell0090f202012-08-07 17:15:30 -07002094
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07002095 // There are a couple of flags left in mPrivateFlags2
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07002096
Christopher Tate3d4bf172011-03-28 16:16:46 -07002097 /* End of masks for mPrivateFlags2 */
2098
Chet Haase21433372012-06-05 07:54:09 -07002099 /* Masks for mPrivateFlags3 */
2100
2101 /**
2102 * Flag indicating that view has a transform animation set on it. This is used to track whether
2103 * an animation is cleared between successive frames, in order to tell the associated
2104 * DisplayList to clear its animation matrix.
2105 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002106 static final int PFLAG3_VIEW_IS_ANIMATING_TRANSFORM = 0x1;
Chet Haase21433372012-06-05 07:54:09 -07002107
2108 /**
2109 * Flag indicating that view has an alpha animation set on it. This is used to track whether an
2110 * animation is cleared between successive frames, in order to tell the associated
2111 * DisplayList to restore its alpha value.
2112 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002113 static final int PFLAG3_VIEW_IS_ANIMATING_ALPHA = 0x2;
Chet Haase21433372012-06-05 07:54:09 -07002114
2115
2116 /* End of masks for mPrivateFlags3 */
2117
Dianne Hackborn4702a852012-08-17 15:18:29 -07002118 static final int DRAG_MASK = PFLAG2_DRAG_CAN_ACCEPT | PFLAG2_DRAG_HOVERED;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002119
Chet Haasedaf98e92011-01-10 14:10:36 -08002120 /**
Adam Powell637d3372010-08-25 14:37:03 -07002121 * Always allow a user to over-scroll this view, provided it is a
2122 * view that can scroll.
2123 *
2124 * @see #getOverScrollMode()
2125 * @see #setOverScrollMode(int)
2126 */
2127 public static final int OVER_SCROLL_ALWAYS = 0;
2128
2129 /**
2130 * Allow a user to over-scroll this view only if the content is large
2131 * enough to meaningfully scroll, provided it is a view that can scroll.
2132 *
2133 * @see #getOverScrollMode()
2134 * @see #setOverScrollMode(int)
2135 */
2136 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
2137
2138 /**
2139 * Never allow a user to over-scroll this view.
2140 *
2141 * @see #getOverScrollMode()
2142 * @see #setOverScrollMode(int)
2143 */
2144 public static final int OVER_SCROLL_NEVER = 2;
2145
2146 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002147 * Special constant for {@link #setSystemUiVisibility(int)}: View has
2148 * requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08002149 *
Joe Malin32736f02011-01-19 16:14:20 -08002150 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002151 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002152 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08002153
2154 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002155 * Flag for {@link #setSystemUiVisibility(int)}: View has requested the
2156 * system UI to enter an unobtrusive "low profile" mode.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002157 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002158 * <p>This is for use in games, book readers, video players, or any other
Philip Milne6c8ea062012-04-03 17:38:43 -07002159 * "immersive" application where the usual system chrome is deemed too distracting.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002160 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002161 * <p>In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08002162 *
Joe Malin32736f02011-01-19 16:14:20 -08002163 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002164 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002165 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
2166
2167 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002168 * Flag for {@link #setSystemUiVisibility(int)}: View has requested that the
2169 * system navigation be temporarily hidden.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002170 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002171 * <p>This is an even less obtrusive state than that called for by
Daniel Sandler60ee2562011-07-22 12:34:33 -04002172 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
2173 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
2174 * those to disappear. This is useful (in conjunction with the
Philip Milne6c8ea062012-04-03 17:38:43 -07002175 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
Daniel Sandler60ee2562011-07-22 12:34:33 -04002176 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
2177 * window flags) for displaying content using every last pixel on the display.
2178 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002179 * <p>There is a limitation: because navigation controls are so important, the least user
2180 * interaction will cause them to reappear immediately. When this happens, both
2181 * this flag and {@link #SYSTEM_UI_FLAG_FULLSCREEN} will be cleared automatically,
2182 * so that both elements reappear at the same time.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002183 *
2184 * @see #setSystemUiVisibility(int)
2185 */
2186 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
2187
2188 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002189 * Flag for {@link #setSystemUiVisibility(int)}: View has requested to go
2190 * into the normal fullscreen mode so that its content can take over the screen
2191 * while still allowing the user to interact with the application.
2192 *
2193 * <p>This has the same visual effect as
2194 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN
2195 * WindowManager.LayoutParams.FLAG_FULLSCREEN},
2196 * meaning that non-critical screen decorations (such as the status bar) will be
2197 * hidden while the user is in the View's window, focusing the experience on
2198 * that content. Unlike the window flag, if you are using ActionBar in
2199 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2200 * Window.FEATURE_ACTION_BAR_OVERLAY}, then enabling this flag will also
2201 * hide the action bar.
2202 *
2203 * <p>This approach to going fullscreen is best used over the window flag when
2204 * it is a transient state -- that is, the application does this at certain
2205 * points in its user interaction where it wants to allow the user to focus
2206 * on content, but not as a continuous state. For situations where the application
2207 * would like to simply stay full screen the entire time (such as a game that
2208 * wants to take over the screen), the
2209 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN window flag}
2210 * is usually a better approach. The state set here will be removed by the system
2211 * in various situations (such as the user moving to another application) like
2212 * the other system UI states.
2213 *
2214 * <p>When using this flag, the application should provide some easy facility
2215 * for the user to go out of it. A common example would be in an e-book
2216 * reader, where tapping on the screen brings back whatever screen and UI
2217 * decorations that had been hidden while the user was immersed in reading
2218 * the book.
2219 *
2220 * @see #setSystemUiVisibility(int)
2221 */
2222 public static final int SYSTEM_UI_FLAG_FULLSCREEN = 0x00000004;
2223
2224 /**
2225 * Flag for {@link #setSystemUiVisibility(int)}: When using other layout
2226 * flags, we would like a stable view of the content insets given to
2227 * {@link #fitSystemWindows(Rect)}. This means that the insets seen there
2228 * will always represent the worst case that the application can expect
Dianne Hackborn5b5cc4d2012-05-16 13:15:00 -07002229 * as a continuous state. In the stock Android UI this is the space for
2230 * the system bar, nav bar, and status bar, but not more transient elements
2231 * such as an input method.
2232 *
2233 * The stable layout your UI sees is based on the system UI modes you can
2234 * switch to. That is, if you specify {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
2235 * then you will get a stable layout for changes of the
2236 * {@link #SYSTEM_UI_FLAG_FULLSCREEN} mode; if you specify
2237 * {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN} and
2238 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION}, then you can transition
2239 * to {@link #SYSTEM_UI_FLAG_FULLSCREEN} and {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}
2240 * with a stable layout. (Note that you should avoid using
2241 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} by itself.)
2242 *
Jeff Smitha45746e2012-07-19 14:19:24 -05002243 * If you have set the window flag {@link WindowManager.LayoutParams#FLAG_FULLSCREEN}
Dianne Hackborn5b5cc4d2012-05-16 13:15:00 -07002244 * to hide the status bar (instead of using {@link #SYSTEM_UI_FLAG_FULLSCREEN}),
2245 * then a hidden status bar will be considered a "stable" state for purposes
2246 * here. This allows your UI to continually hide the status bar, while still
2247 * using the system UI flags to hide the action bar while still retaining
2248 * a stable layout. Note that changing the window fullscreen flag will never
2249 * provide a stable layout for a clean transition.
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002250 *
2251 * <p>If you are using ActionBar in
2252 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2253 * Window.FEATURE_ACTION_BAR_OVERLAY}, this flag will also impact the
2254 * insets it adds to those given to the application.
2255 */
2256 public static final int SYSTEM_UI_FLAG_LAYOUT_STABLE = 0x00000100;
2257
2258 /**
2259 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2260 * to be layed out as if it has requested
2261 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, even if it currently hasn't. This
2262 * allows it to avoid artifacts when switching in and out of that mode, at
2263 * the expense that some of its user interface may be covered by screen
2264 * decorations when they are shown. You can perform layout of your inner
2265 * UI elements to account for the navagation system UI through the
2266 * {@link #fitSystemWindows(Rect)} method.
2267 */
2268 public static final int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 0x00000200;
2269
2270 /**
2271 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2272 * to be layed out as if it has requested
2273 * {@link #SYSTEM_UI_FLAG_FULLSCREEN}, even if it currently hasn't. This
2274 * allows it to avoid artifacts when switching in and out of that mode, at
2275 * the expense that some of its user interface may be covered by screen
2276 * decorations when they are shown. You can perform layout of your inner
2277 * UI elements to account for non-fullscreen system UI through the
2278 * {@link #fitSystemWindows(Rect)} method.
2279 */
2280 public static final int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 0x00000400;
2281
2282 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04002283 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
2284 */
2285 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
2286
2287 /**
2288 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
2289 */
2290 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08002291
2292 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002293 * @hide
2294 *
2295 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2296 * out of the public fields to keep the undefined bits out of the developer's way.
2297 *
2298 * Flag to make the status bar not expandable. Unless you also
2299 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
2300 */
2301 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
2302
2303 /**
2304 * @hide
2305 *
2306 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2307 * out of the public fields to keep the undefined bits out of the developer's way.
2308 *
2309 * Flag to hide notification icons and scrolling ticker text.
2310 */
2311 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
2312
2313 /**
2314 * @hide
2315 *
2316 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2317 * out of the public fields to keep the undefined bits out of the developer's way.
2318 *
2319 * Flag to disable incoming notification alerts. This will not block
2320 * icons, but it will block sound, vibrating and other visual or aural notifications.
2321 */
2322 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
2323
2324 /**
2325 * @hide
2326 *
2327 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2328 * out of the public fields to keep the undefined bits out of the developer's way.
2329 *
2330 * Flag to hide only the scrolling ticker. Note that
2331 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
2332 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
2333 */
2334 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
2335
2336 /**
2337 * @hide
2338 *
2339 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2340 * out of the public fields to keep the undefined bits out of the developer's way.
2341 *
2342 * Flag to hide the center system info area.
2343 */
2344 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
2345
2346 /**
2347 * @hide
2348 *
2349 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2350 * out of the public fields to keep the undefined bits out of the developer's way.
2351 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002352 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002353 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2354 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04002355 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002356
2357 /**
2358 * @hide
2359 *
2360 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2361 * out of the public fields to keep the undefined bits out of the developer's way.
2362 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002363 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08002364 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2365 */
2366 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
2367
2368 /**
2369 * @hide
2370 *
2371 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2372 * out of the public fields to keep the undefined bits out of the developer's way.
2373 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002374 * Flag to hide only the clock. You might use this if your activity has
2375 * its own clock making the status bar's clock redundant.
2376 */
Joe Onorato6478adc2011-01-27 21:15:01 -08002377 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
2378
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002379 /**
2380 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04002381 *
2382 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2383 * out of the public fields to keep the undefined bits out of the developer's way.
2384 *
2385 * Flag to hide only the recent apps button. Don't use this
2386 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2387 */
2388 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
2389
2390 /**
2391 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002392 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002393 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002394
2395 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002396 * These are the system UI flags that can be cleared by events outside
2397 * of an application. Currently this is just the ability to tap on the
2398 * screen while hiding the navigation bar to have it return.
2399 * @hide
2400 */
2401 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002402 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION
2403 | SYSTEM_UI_FLAG_FULLSCREEN;
2404
2405 /**
2406 * Flags that can impact the layout in relation to system UI.
2407 */
2408 public static final int SYSTEM_UI_LAYOUT_FLAGS =
2409 SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
2410 | SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002411
2412 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07002413 * Find views that render the specified text.
2414 *
2415 * @see #findViewsWithText(ArrayList, CharSequence, int)
2416 */
2417 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
2418
2419 /**
2420 * Find find views that contain the specified content description.
2421 *
2422 * @see #findViewsWithText(ArrayList, CharSequence, int)
2423 */
2424 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
2425
2426 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07002427 * Find views that contain {@link AccessibilityNodeProvider}. Such
2428 * a View is a root of virtual view hierarchy and may contain the searched
2429 * text. If this flag is set Views with providers are automatically
2430 * added and it is a responsibility of the client to call the APIs of
2431 * the provider to determine whether the virtual tree rooted at this View
2432 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
2433 * represeting the virtual views with this text.
2434 *
2435 * @see #findViewsWithText(ArrayList, CharSequence, int)
2436 *
2437 * @hide
2438 */
2439 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
2440
2441 /**
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07002442 * The undefined cursor position.
2443 */
2444 private static final int ACCESSIBILITY_CURSOR_POSITION_UNDEFINED = -1;
2445
2446 /**
Romain Guybb9908b2012-03-08 11:14:07 -08002447 * Indicates that the screen has changed state and is now off.
2448 *
2449 * @see #onScreenStateChanged(int)
2450 */
2451 public static final int SCREEN_STATE_OFF = 0x0;
2452
2453 /**
2454 * Indicates that the screen has changed state and is now on.
2455 *
Romain Guy1e3d3132012-03-08 15:55:56 -08002456 * @see #onScreenStateChanged(int)
Romain Guybb9908b2012-03-08 11:14:07 -08002457 */
2458 public static final int SCREEN_STATE_ON = 0x1;
2459
2460 /**
Adam Powell637d3372010-08-25 14:37:03 -07002461 * Controls the over-scroll mode for this view.
2462 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2463 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2464 * and {@link #OVER_SCROLL_NEVER}.
2465 */
2466 private int mOverScrollMode;
2467
2468 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002469 * The parent this view is attached to.
2470 * {@hide}
2471 *
2472 * @see #getParent()
2473 */
2474 protected ViewParent mParent;
2475
2476 /**
2477 * {@hide}
2478 */
2479 AttachInfo mAttachInfo;
2480
2481 /**
2482 * {@hide}
2483 */
Romain Guy809a7f62009-05-14 15:44:42 -07002484 @ViewDebug.ExportedProperty(flagMapping = {
Dianne Hackborn4702a852012-08-17 15:18:29 -07002485 @ViewDebug.FlagToString(mask = PFLAG_FORCE_LAYOUT, equals = PFLAG_FORCE_LAYOUT,
Romain Guy809a7f62009-05-14 15:44:42 -07002486 name = "FORCE_LAYOUT"),
Dianne Hackborn4702a852012-08-17 15:18:29 -07002487 @ViewDebug.FlagToString(mask = PFLAG_LAYOUT_REQUIRED, equals = PFLAG_LAYOUT_REQUIRED,
Romain Guy809a7f62009-05-14 15:44:42 -07002488 name = "LAYOUT_REQUIRED"),
Dianne Hackborn4702a852012-08-17 15:18:29 -07002489 @ViewDebug.FlagToString(mask = PFLAG_DRAWING_CACHE_VALID, equals = PFLAG_DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002490 name = "DRAWING_CACHE_INVALID", outputIf = false),
Dianne Hackborn4702a852012-08-17 15:18:29 -07002491 @ViewDebug.FlagToString(mask = PFLAG_DRAWN, equals = PFLAG_DRAWN, name = "DRAWN", outputIf = true),
2492 @ViewDebug.FlagToString(mask = PFLAG_DRAWN, equals = PFLAG_DRAWN, name = "NOT_DRAWN", outputIf = false),
2493 @ViewDebug.FlagToString(mask = PFLAG_DIRTY_MASK, equals = PFLAG_DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2494 @ViewDebug.FlagToString(mask = PFLAG_DIRTY_MASK, equals = PFLAG_DIRTY, name = "DIRTY")
Romain Guy809a7f62009-05-14 15:44:42 -07002495 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002496 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002497 int mPrivateFlags2;
Chet Haase21433372012-06-05 07:54:09 -07002498 int mPrivateFlags3;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002499
2500 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002501 * This view's request for the visibility of the status bar.
2502 * @hide
2503 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002504 @ViewDebug.ExportedProperty(flagMapping = {
2505 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2506 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2507 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2508 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2509 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2510 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2511 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2512 equals = SYSTEM_UI_FLAG_VISIBLE,
2513 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2514 })
Joe Onorato664644d2011-01-23 17:53:23 -08002515 int mSystemUiVisibility;
2516
2517 /**
Chet Haase563d4f22012-04-18 16:20:08 -07002518 * Reference count for transient state.
2519 * @see #setHasTransientState(boolean)
2520 */
2521 int mTransientStateCount = 0;
2522
2523 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002524 * Count of how many windows this view has been attached to.
2525 */
2526 int mWindowAttachCount;
2527
2528 /**
2529 * The layout parameters associated with this view and used by the parent
2530 * {@link android.view.ViewGroup} to determine how this view should be
2531 * laid out.
2532 * {@hide}
2533 */
2534 protected ViewGroup.LayoutParams mLayoutParams;
2535
2536 /**
2537 * The view flags hold various views states.
2538 * {@hide}
2539 */
2540 @ViewDebug.ExportedProperty
2541 int mViewFlags;
2542
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002543 static class TransformationInfo {
2544 /**
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 * getMatrix(), 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 final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002552
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002553 /**
2554 * The transform matrix for the View. This transform is calculated internally
2555 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2556 * is used by default. Do *not* use this variable directly; instead call
2557 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2558 * to get the correct matrix based on the latest rotation and scale properties.
2559 */
2560 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002561
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002562 /**
2563 * An internal variable that tracks whether we need to recalculate the
2564 * transform matrix, based on whether the rotation or scaleX/Y properties
2565 * have changed since the matrix was last calculated.
2566 */
2567 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002568
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002569 /**
2570 * An internal variable that tracks whether we need to recalculate the
2571 * transform matrix, based on whether the rotation or scaleX/Y properties
2572 * have changed since the matrix was last calculated.
2573 */
2574 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002575
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002576 /**
2577 * A variable that tracks whether we need to recalculate the
2578 * transform matrix, based on whether the rotation or scaleX/Y properties
2579 * have changed since the matrix was last calculated. This variable
2580 * is only valid after a call to updateMatrix() or to a function that
2581 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2582 */
2583 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002584
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002585 /**
2586 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2587 */
2588 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002589
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002590 /**
2591 * This matrix is used when computing the matrix for 3D rotations.
2592 */
2593 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002594
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002595 /**
2596 * These prev values are used to recalculate a centered pivot point when necessary. The
2597 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2598 * set), so thes values are only used then as well.
2599 */
2600 private int mPrevWidth = -1;
2601 private int mPrevHeight = -1;
Philip Milne6c8ea062012-04-03 17:38:43 -07002602
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002603 /**
2604 * The degrees rotation around the vertical axis through the pivot point.
2605 */
2606 @ViewDebug.ExportedProperty
2607 float mRotationY = 0f;
2608
2609 /**
2610 * The degrees rotation around the horizontal axis through the pivot point.
2611 */
2612 @ViewDebug.ExportedProperty
2613 float mRotationX = 0f;
2614
2615 /**
2616 * The degrees rotation around the pivot point.
2617 */
2618 @ViewDebug.ExportedProperty
2619 float mRotation = 0f;
2620
2621 /**
2622 * The amount of translation of the object away from its left property (post-layout).
2623 */
2624 @ViewDebug.ExportedProperty
2625 float mTranslationX = 0f;
2626
2627 /**
2628 * The amount of translation of the object away from its top property (post-layout).
2629 */
2630 @ViewDebug.ExportedProperty
2631 float mTranslationY = 0f;
2632
2633 /**
2634 * The amount of scale in the x direction around the pivot point. A
2635 * value of 1 means no scaling is applied.
2636 */
2637 @ViewDebug.ExportedProperty
2638 float mScaleX = 1f;
2639
2640 /**
2641 * The amount of scale in the y direction around the pivot point. A
2642 * value of 1 means no scaling is applied.
2643 */
2644 @ViewDebug.ExportedProperty
2645 float mScaleY = 1f;
2646
2647 /**
Chet Haasea33de552012-02-03 16:28:24 -08002648 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002649 */
2650 @ViewDebug.ExportedProperty
2651 float mPivotX = 0f;
2652
2653 /**
Chet Haasea33de552012-02-03 16:28:24 -08002654 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002655 */
2656 @ViewDebug.ExportedProperty
2657 float mPivotY = 0f;
2658
2659 /**
2660 * The opacity of the View. This is a value from 0 to 1, where 0 means
2661 * completely transparent and 1 means completely opaque.
2662 */
2663 @ViewDebug.ExportedProperty
2664 float mAlpha = 1f;
2665 }
2666
2667 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002668
Joe Malin32736f02011-01-19 16:14:20 -08002669 private boolean mLastIsOpaque;
2670
Chet Haasefd2b0022010-08-06 13:08:56 -07002671 /**
2672 * Convenience value to check for float values that are close enough to zero to be considered
2673 * zero.
2674 */
Romain Guy2542d192010-08-18 11:47:12 -07002675 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002676
2677 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002678 * The distance in pixels from the left edge of this view's parent
2679 * to the left edge of this view.
2680 * {@hide}
2681 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002682 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002683 protected int mLeft;
2684 /**
2685 * The distance in pixels from the left edge of this view's parent
2686 * to the right edge of this view.
2687 * {@hide}
2688 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002689 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002690 protected int mRight;
2691 /**
2692 * The distance in pixels from the top edge of this view's parent
2693 * to the top edge of this view.
2694 * {@hide}
2695 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002696 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002697 protected int mTop;
2698 /**
2699 * The distance in pixels from the top edge of this view's parent
2700 * to the bottom edge of this view.
2701 * {@hide}
2702 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002703 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002704 protected int mBottom;
2705
2706 /**
2707 * The offset, in pixels, by which the content of this view is scrolled
2708 * horizontally.
2709 * {@hide}
2710 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002711 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002712 protected int mScrollX;
2713 /**
2714 * The offset, in pixels, by which the content of this view is scrolled
2715 * vertically.
2716 * {@hide}
2717 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002718 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002719 protected int mScrollY;
2720
2721 /**
2722 * The left padding in pixels, that is the distance in pixels between the
2723 * left edge of this view and the left edge of its content.
2724 * {@hide}
2725 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002726 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002727 protected int mPaddingLeft;
2728 /**
2729 * The right padding in pixels, that is the distance in pixels between the
2730 * right edge of this view and the right edge of its content.
2731 * {@hide}
2732 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002733 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002734 protected int mPaddingRight;
2735 /**
2736 * The top padding in pixels, that is the distance in pixels between the
2737 * top edge of this view and the top edge of its content.
2738 * {@hide}
2739 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002740 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002741 protected int mPaddingTop;
2742 /**
2743 * The bottom padding in pixels, that is the distance in pixels between the
2744 * bottom edge of this view and the bottom edge of its content.
2745 * {@hide}
2746 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002747 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002748 protected int mPaddingBottom;
2749
2750 /**
Philip Milne1557fd72012-04-04 23:41:34 -07002751 * The layout insets in pixels, that is the distance in pixels between the
2752 * visible edges of this view its bounds.
2753 */
2754 private Insets mLayoutInsets;
2755
2756 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002757 * Briefly describes the view and is primarily used for accessibility support.
2758 */
2759 private CharSequence mContentDescription;
2760
2761 /**
Svetoslav Ganov33aef982012-09-13 12:49:03 -07002762 * Specifies the id of a view for which this view serves as a label for
2763 * accessibility purposes.
2764 */
2765 private int mLabelForId = View.NO_ID;
2766
2767 /**
2768 * Predicate for matching labeled view id with its label for
2769 * accessibility purposes.
2770 */
2771 private MatchLabelForPredicate mMatchLabelForPredicate;
2772
2773 /**
2774 * Predicate for matching a view by its id.
2775 */
2776 private MatchIdPredicate mMatchIdPredicate;
2777
2778 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002779 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002780 *
2781 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002782 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002783 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002784 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002785
2786 /**
2787 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002788 *
2789 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002790 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002791 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002792 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002793
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002794 /**
Adam Powell20232d02010-12-08 21:08:53 -08002795 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002796 *
2797 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002798 */
2799 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002800 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002801
2802 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002803 * Cache the paddingStart set by the user to append to the scrollbar's size.
2804 *
2805 */
2806 @ViewDebug.ExportedProperty(category = "padding")
2807 int mUserPaddingStart;
2808
2809 /**
2810 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2811 *
2812 */
2813 @ViewDebug.ExportedProperty(category = "padding")
2814 int mUserPaddingEnd;
2815
2816 /**
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07002817 * Whether a left padding has been defined during layout inflation.
2818 *
2819 * @hide
2820 */
2821 boolean mUserPaddingLeftDefined = false;
2822
2823 /**
2824 * Whether a right padding has been defined during layout inflation.
2825 *
2826 * @hide
2827 */
2828 boolean mUserPaddingRightDefined = false;
2829
2830 /**
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07002831 * Default undefined padding
2832 */
2833 private static final int UNDEFINED_PADDING = Integer.MIN_VALUE;
2834
2835 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002836 * @hide
2837 */
2838 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2839 /**
2840 * @hide
2841 */
2842 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002843
Philip Milne6c8ea062012-04-03 17:38:43 -07002844 private Drawable mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002845
2846 private int mBackgroundResource;
2847 private boolean mBackgroundSizeChanged;
2848
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002849 static class ListenerInfo {
2850 /**
2851 * Listener used to dispatch focus change events.
2852 * This field should be made private, so it is hidden from the SDK.
2853 * {@hide}
2854 */
2855 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002856
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002857 /**
2858 * Listeners for layout change events.
2859 */
2860 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002861
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002862 /**
2863 * Listeners for attach events.
2864 */
2865 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002866
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002867 /**
2868 * Listener used to dispatch click events.
2869 * This field should be made private, so it is hidden from the SDK.
2870 * {@hide}
2871 */
2872 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002873
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002874 /**
2875 * Listener used to dispatch long click events.
2876 * This field should be made private, so it is hidden from the SDK.
2877 * {@hide}
2878 */
2879 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002880
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002881 /**
2882 * Listener used to build the context menu.
2883 * This field should be made private, so it is hidden from the SDK.
2884 * {@hide}
2885 */
2886 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002887
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002888 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002889
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002890 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002891
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002892 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002893
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002894 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002895
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002896 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002897
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002898 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2899 }
2900
2901 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002903 /**
2904 * The application environment this view lives in.
2905 * This field should be made private, so it is hidden from the SDK.
2906 * {@hide}
2907 */
2908 protected Context mContext;
2909
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002910 private final Resources mResources;
2911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002912 private ScrollabilityCache mScrollCache;
2913
2914 private int[] mDrawableState = null;
2915
Romain Guy0211a0a2011-02-14 16:34:59 -08002916 /**
2917 * Set to true when drawing cache is enabled and cannot be created.
Philip Milne6c8ea062012-04-03 17:38:43 -07002918 *
Romain Guy0211a0a2011-02-14 16:34:59 -08002919 * @hide
2920 */
2921 public boolean mCachingFailed;
2922
Romain Guy02890fd2010-08-06 17:58:44 -07002923 private Bitmap mDrawingCache;
2924 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002925 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002926 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002927
2928 /**
2929 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2930 * the user may specify which view to go to next.
2931 */
2932 private int mNextFocusLeftId = View.NO_ID;
2933
2934 /**
2935 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2936 * the user may specify which view to go to next.
2937 */
2938 private int mNextFocusRightId = View.NO_ID;
2939
2940 /**
2941 * When this view has focus and the next focus is {@link #FOCUS_UP},
2942 * the user may specify which view to go to next.
2943 */
2944 private int mNextFocusUpId = View.NO_ID;
2945
2946 /**
2947 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2948 * the user may specify which view to go to next.
2949 */
2950 private int mNextFocusDownId = View.NO_ID;
2951
Jeff Brown4e6319b2010-12-13 10:36:51 -08002952 /**
2953 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2954 * the user may specify which view to go to next.
2955 */
2956 int mNextFocusForwardId = View.NO_ID;
2957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002958 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002959 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002960 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002961 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002962
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002963 private UnsetPressedState mUnsetPressedState;
2964
2965 /**
2966 * Whether the long press's action has been invoked. The tap's action is invoked on the
2967 * up event while a long press is invoked as soon as the long press duration is reached, so
2968 * a long press could be performed before the tap is checked, in which case the tap's action
2969 * should not be invoked.
2970 */
2971 private boolean mHasPerformedLongPress;
2972
2973 /**
2974 * The minimum height of the view. We'll try our best to have the height
2975 * of this view to at least this amount.
2976 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002977 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002978 private int mMinHeight;
2979
2980 /**
2981 * The minimum width of the view. We'll try our best to have the width
2982 * of this view to at least this amount.
2983 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002984 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002985 private int mMinWidth;
2986
2987 /**
2988 * The delegate to handle touch events that are physically in this view
2989 * but should be handled by another view.
2990 */
2991 private TouchDelegate mTouchDelegate = null;
2992
2993 /**
2994 * Solid color to use as a background when creating the drawing cache. Enables
2995 * the cache to use 16 bit bitmaps instead of 32 bit.
2996 */
2997 private int mDrawingCacheBackgroundColor = 0;
2998
2999 /**
3000 * Special tree observer used when mAttachInfo is null.
3001 */
3002 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08003003
Adam Powelle14579b2009-12-16 18:39:52 -08003004 /**
3005 * Cache the touch slop from the context that created the view.
3006 */
3007 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003009 /**
Chet Haasea00f3862011-02-22 06:34:40 -08003010 * Object that handles automatic animation of view properties.
3011 */
3012 private ViewPropertyAnimator mAnimator = null;
3013
3014 /**
Christopher Tate251602f2011-01-28 17:54:12 -08003015 * Flag indicating that a drag can cross window boundaries. When
3016 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
3017 * with this flag set, all visible applications will be able to participate
3018 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08003019 *
3020 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08003021 */
3022 public static final int DRAG_FLAG_GLOBAL = 1;
3023
3024 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003025 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
3026 */
3027 private float mVerticalScrollFactor;
3028
3029 /**
Adam Powell20232d02010-12-08 21:08:53 -08003030 * Position of the vertical scroll bar.
3031 */
3032 private int mVerticalScrollbarPosition;
3033
3034 /**
3035 * Position the scroll bar at the default position as determined by the system.
3036 */
3037 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
3038
3039 /**
3040 * Position the scroll bar along the left edge.
3041 */
3042 public static final int SCROLLBAR_POSITION_LEFT = 1;
3043
3044 /**
3045 * Position the scroll bar along the right edge.
3046 */
3047 public static final int SCROLLBAR_POSITION_RIGHT = 2;
3048
3049 /**
Romain Guy171c5922011-01-06 10:04:23 -08003050 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08003051 *
3052 * @see #getLayerType()
3053 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003054 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08003055 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003056 */
3057 public static final int LAYER_TYPE_NONE = 0;
3058
3059 /**
3060 * <p>Indicates that the view has a software layer. A software layer is backed
3061 * by a bitmap and causes the view to be rendered using Android's software
3062 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003063 *
Romain Guy171c5922011-01-06 10:04:23 -08003064 * <p>Software layers have various usages:</p>
3065 * <p>When the application is not using hardware acceleration, a software layer
3066 * is useful to apply a specific color filter and/or blending mode and/or
3067 * translucency to a view and all its children.</p>
3068 * <p>When the application is using hardware acceleration, a software layer
3069 * is useful to render drawing primitives not supported by the hardware
3070 * accelerated pipeline. It can also be used to cache a complex view tree
3071 * into a texture and reduce the complexity of drawing operations. For instance,
3072 * when animating a complex view tree with a translation, a software layer can
3073 * be used to render the view tree only once.</p>
3074 * <p>Software layers should be avoided when the affected view tree updates
3075 * often. Every update will require to re-render the software layer, which can
3076 * potentially be slow (particularly when hardware acceleration is turned on
3077 * since the layer will have to be uploaded into a hardware texture after every
3078 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08003079 *
3080 * @see #getLayerType()
3081 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003082 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08003083 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003084 */
3085 public static final int LAYER_TYPE_SOFTWARE = 1;
3086
3087 /**
3088 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
3089 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
3090 * OpenGL hardware) and causes the view to be rendered using Android's hardware
3091 * rendering pipeline, but only if hardware acceleration is turned on for the
3092 * view hierarchy. When hardware acceleration is turned off, hardware layers
3093 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003094 *
Romain Guy171c5922011-01-06 10:04:23 -08003095 * <p>A hardware layer is useful to apply a specific color filter and/or
3096 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08003097 * <p>A hardware layer can be used to cache a complex view tree into a
3098 * texture and reduce the complexity of drawing operations. For instance,
3099 * when animating a complex view tree with a translation, a hardware layer can
3100 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08003101 * <p>A hardware layer can also be used to increase the rendering quality when
3102 * rotation transformations are applied on a view. It can also be used to
3103 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003104 *
3105 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08003106 * @see #setLayerType(int, android.graphics.Paint)
3107 * @see #LAYER_TYPE_NONE
3108 * @see #LAYER_TYPE_SOFTWARE
3109 */
3110 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08003111
Romain Guy3aaff3a2011-01-12 14:18:47 -08003112 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
3113 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
3114 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
3115 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
3116 })
Romain Guy171c5922011-01-06 10:04:23 -08003117 int mLayerType = LAYER_TYPE_NONE;
3118 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08003119 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08003120
3121 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07003122 * Set to true when the view is sending hover accessibility events because it
3123 * is the innermost hovered view.
3124 */
3125 private boolean mSendingHoverAccessibilityEvents;
3126
Dianne Hackborn4702a852012-08-17 15:18:29 -07003127 /**
3128 * Delegate for injecting accessiblity functionality.
3129 */
3130 AccessibilityDelegate mAccessibilityDelegate;
3131
3132 /**
3133 * Consistency verifier for debugging purposes.
3134 * @hide
3135 */
3136 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
3137 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
3138 new InputEventConsistencyVerifier(this, 0) : null;
3139
Adam Powella9108a22012-07-18 11:18:09 -07003140 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
3141
Jeff Brown87b7f802011-06-21 18:35:45 -07003142 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003143 * Simple constructor to use when creating a view from code.
3144 *
3145 * @param context The Context the view is running in, through which it can
3146 * access the current theme, resources, etc.
3147 */
3148 public View(Context context) {
3149 mContext = context;
3150 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003151 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003152 // Set layout and text direction defaults
Dianne Hackborn4702a852012-08-17 15:18:29 -07003153 mPrivateFlags2 = (LAYOUT_DIRECTION_DEFAULT << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) |
3154 (TEXT_DIRECTION_DEFAULT << PFLAG2_TEXT_DIRECTION_MASK_SHIFT) |
3155 (TEXT_ALIGNMENT_DEFAULT << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT) |
3156 (IMPORTANT_FOR_ACCESSIBILITY_DEFAULT << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT);
Adam Powelle14579b2009-12-16 18:39:52 -08003157 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07003158 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003159 mUserPaddingStart = UNDEFINED_PADDING;
3160 mUserPaddingEnd = UNDEFINED_PADDING;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003161 }
3162
3163 /**
3164 * Constructor that is called when inflating a view from XML. This is called
3165 * when a view is being constructed from an XML file, supplying attributes
3166 * that were specified in the XML file. This version uses a default style of
3167 * 0, so the only attribute values applied are those in the Context's Theme
3168 * and the given AttributeSet.
3169 *
3170 * <p>
3171 * The method onFinishInflate() will be called after all children have been
3172 * added.
3173 *
3174 * @param context The Context the view is running in, through which it can
3175 * access the current theme, resources, etc.
3176 * @param attrs The attributes of the XML tag that is inflating the view.
3177 * @see #View(Context, AttributeSet, int)
3178 */
3179 public View(Context context, AttributeSet attrs) {
3180 this(context, attrs, 0);
3181 }
3182
3183 /**
3184 * Perform inflation from XML and apply a class-specific base style. This
3185 * constructor of View allows subclasses to use their own base style when
3186 * they are inflating. For example, a Button class's constructor would call
3187 * this version of the super class constructor and supply
3188 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
3189 * the theme's button style to modify all of the base view attributes (in
3190 * particular its background) as well as the Button class's attributes.
3191 *
3192 * @param context The Context the view is running in, through which it can
3193 * access the current theme, resources, etc.
3194 * @param attrs The attributes of the XML tag that is inflating the view.
3195 * @param defStyle The default style to apply to this view. If 0, no style
3196 * will be applied (beyond what is included in the theme). This may
3197 * either be an attribute resource, whose value will be retrieved
3198 * from the current theme, or an explicit style resource.
3199 * @see #View(Context, AttributeSet)
3200 */
3201 public View(Context context, AttributeSet attrs, int defStyle) {
3202 this(context);
3203
3204 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
3205 defStyle, 0);
3206
3207 Drawable background = null;
3208
3209 int leftPadding = -1;
3210 int topPadding = -1;
3211 int rightPadding = -1;
3212 int bottomPadding = -1;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003213 int startPadding = UNDEFINED_PADDING;
3214 int endPadding = UNDEFINED_PADDING;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003215
3216 int padding = -1;
3217
3218 int viewFlagValues = 0;
3219 int viewFlagMasks = 0;
3220
3221 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07003222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003223 int x = 0;
3224 int y = 0;
3225
Chet Haase73066682010-11-29 15:55:32 -08003226 float tx = 0;
3227 float ty = 0;
3228 float rotation = 0;
3229 float rotationX = 0;
3230 float rotationY = 0;
3231 float sx = 1f;
3232 float sy = 1f;
3233 boolean transformSet = false;
3234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003235 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
Adam Powell637d3372010-08-25 14:37:03 -07003236 int overScrollMode = mOverScrollMode;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003237 boolean initializeScrollbars = false;
3238
3239 final int targetSdkVersion = context.getApplicationInfo().targetSdkVersion;
3240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003241 final int N = a.getIndexCount();
3242 for (int i = 0; i < N; i++) {
3243 int attr = a.getIndex(i);
3244 switch (attr) {
3245 case com.android.internal.R.styleable.View_background:
3246 background = a.getDrawable(attr);
3247 break;
3248 case com.android.internal.R.styleable.View_padding:
3249 padding = a.getDimensionPixelSize(attr, -1);
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003250 mUserPaddingLeftDefined = true;
3251 mUserPaddingRightDefined = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003252 break;
3253 case com.android.internal.R.styleable.View_paddingLeft:
3254 leftPadding = a.getDimensionPixelSize(attr, -1);
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003255 mUserPaddingLeftDefined = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003256 break;
3257 case com.android.internal.R.styleable.View_paddingTop:
3258 topPadding = a.getDimensionPixelSize(attr, -1);
3259 break;
3260 case com.android.internal.R.styleable.View_paddingRight:
3261 rightPadding = a.getDimensionPixelSize(attr, -1);
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003262 mUserPaddingRightDefined = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003263 break;
3264 case com.android.internal.R.styleable.View_paddingBottom:
3265 bottomPadding = a.getDimensionPixelSize(attr, -1);
3266 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003267 case com.android.internal.R.styleable.View_paddingStart:
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003268 startPadding = a.getDimensionPixelSize(attr, UNDEFINED_PADDING);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003269 break;
3270 case com.android.internal.R.styleable.View_paddingEnd:
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003271 endPadding = a.getDimensionPixelSize(attr, UNDEFINED_PADDING);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003272 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003273 case com.android.internal.R.styleable.View_scrollX:
3274 x = a.getDimensionPixelOffset(attr, 0);
3275 break;
3276 case com.android.internal.R.styleable.View_scrollY:
3277 y = a.getDimensionPixelOffset(attr, 0);
3278 break;
Chet Haase73066682010-11-29 15:55:32 -08003279 case com.android.internal.R.styleable.View_alpha:
3280 setAlpha(a.getFloat(attr, 1f));
3281 break;
3282 case com.android.internal.R.styleable.View_transformPivotX:
3283 setPivotX(a.getDimensionPixelOffset(attr, 0));
3284 break;
3285 case com.android.internal.R.styleable.View_transformPivotY:
3286 setPivotY(a.getDimensionPixelOffset(attr, 0));
3287 break;
3288 case com.android.internal.R.styleable.View_translationX:
3289 tx = a.getDimensionPixelOffset(attr, 0);
3290 transformSet = true;
3291 break;
3292 case com.android.internal.R.styleable.View_translationY:
3293 ty = a.getDimensionPixelOffset(attr, 0);
3294 transformSet = true;
3295 break;
3296 case com.android.internal.R.styleable.View_rotation:
3297 rotation = a.getFloat(attr, 0);
3298 transformSet = true;
3299 break;
3300 case com.android.internal.R.styleable.View_rotationX:
3301 rotationX = a.getFloat(attr, 0);
3302 transformSet = true;
3303 break;
3304 case com.android.internal.R.styleable.View_rotationY:
3305 rotationY = a.getFloat(attr, 0);
3306 transformSet = true;
3307 break;
3308 case com.android.internal.R.styleable.View_scaleX:
3309 sx = a.getFloat(attr, 1f);
3310 transformSet = true;
3311 break;
3312 case com.android.internal.R.styleable.View_scaleY:
3313 sy = a.getFloat(attr, 1f);
3314 transformSet = true;
3315 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003316 case com.android.internal.R.styleable.View_id:
3317 mID = a.getResourceId(attr, NO_ID);
3318 break;
3319 case com.android.internal.R.styleable.View_tag:
3320 mTag = a.getText(attr);
3321 break;
3322 case com.android.internal.R.styleable.View_fitsSystemWindows:
3323 if (a.getBoolean(attr, false)) {
3324 viewFlagValues |= FITS_SYSTEM_WINDOWS;
3325 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
3326 }
3327 break;
3328 case com.android.internal.R.styleable.View_focusable:
3329 if (a.getBoolean(attr, false)) {
3330 viewFlagValues |= FOCUSABLE;
3331 viewFlagMasks |= FOCUSABLE_MASK;
3332 }
3333 break;
3334 case com.android.internal.R.styleable.View_focusableInTouchMode:
3335 if (a.getBoolean(attr, false)) {
3336 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
3337 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
3338 }
3339 break;
3340 case com.android.internal.R.styleable.View_clickable:
3341 if (a.getBoolean(attr, false)) {
3342 viewFlagValues |= CLICKABLE;
3343 viewFlagMasks |= CLICKABLE;
3344 }
3345 break;
3346 case com.android.internal.R.styleable.View_longClickable:
3347 if (a.getBoolean(attr, false)) {
3348 viewFlagValues |= LONG_CLICKABLE;
3349 viewFlagMasks |= LONG_CLICKABLE;
3350 }
3351 break;
3352 case com.android.internal.R.styleable.View_saveEnabled:
3353 if (!a.getBoolean(attr, true)) {
3354 viewFlagValues |= SAVE_DISABLED;
3355 viewFlagMasks |= SAVE_DISABLED_MASK;
3356 }
3357 break;
3358 case com.android.internal.R.styleable.View_duplicateParentState:
3359 if (a.getBoolean(attr, false)) {
3360 viewFlagValues |= DUPLICATE_PARENT_STATE;
3361 viewFlagMasks |= DUPLICATE_PARENT_STATE;
3362 }
3363 break;
3364 case com.android.internal.R.styleable.View_visibility:
3365 final int visibility = a.getInt(attr, 0);
3366 if (visibility != 0) {
3367 viewFlagValues |= VISIBILITY_FLAGS[visibility];
3368 viewFlagMasks |= VISIBILITY_MASK;
3369 }
3370 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003371 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003372 // Clear any layout direction flags (included resolved bits) already set
Dianne Hackborn4702a852012-08-17 15:18:29 -07003373 mPrivateFlags2 &= ~(PFLAG2_LAYOUT_DIRECTION_MASK | PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003374 // Set the layout direction flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003375 final int layoutDirection = a.getInt(attr, -1);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003376 final int value = (layoutDirection != -1) ?
3377 LAYOUT_DIRECTION_FLAGS[layoutDirection] : LAYOUT_DIRECTION_DEFAULT;
Dianne Hackborn4702a852012-08-17 15:18:29 -07003378 mPrivateFlags2 |= (value << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT);
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07003379 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003380 case com.android.internal.R.styleable.View_drawingCacheQuality:
3381 final int cacheQuality = a.getInt(attr, 0);
3382 if (cacheQuality != 0) {
3383 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
3384 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
3385 }
3386 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07003387 case com.android.internal.R.styleable.View_contentDescription:
Svetoslav Ganove47957a2012-06-05 14:46:50 -07003388 setContentDescription(a.getString(attr));
svetoslavganov75986cf2009-05-14 22:28:01 -07003389 break;
Svetoslav Ganov33aef982012-09-13 12:49:03 -07003390 case com.android.internal.R.styleable.View_labelFor:
3391 setLabelFor(a.getResourceId(attr, NO_ID));
3392 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003393 case com.android.internal.R.styleable.View_soundEffectsEnabled:
3394 if (!a.getBoolean(attr, true)) {
3395 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
3396 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
3397 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003398 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003399 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
3400 if (!a.getBoolean(attr, true)) {
3401 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
3402 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
3403 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003404 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003405 case R.styleable.View_scrollbars:
3406 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
3407 if (scrollbars != SCROLLBARS_NONE) {
3408 viewFlagValues |= scrollbars;
3409 viewFlagMasks |= SCROLLBARS_MASK;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003410 initializeScrollbars = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003411 }
3412 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07003413 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003414 case R.styleable.View_fadingEdge:
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003415 if (targetSdkVersion >= ICE_CREAM_SANDWICH) {
Romain Guy1ef3fdb2011-09-09 15:30:30 -07003416 // Ignore the attribute starting with ICS
3417 break;
3418 }
3419 // With builds < ICS, fall through and apply fading edges
3420 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003421 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
3422 if (fadingEdge != FADING_EDGE_NONE) {
3423 viewFlagValues |= fadingEdge;
3424 viewFlagMasks |= FADING_EDGE_MASK;
3425 initializeFadingEdge(a);
3426 }
3427 break;
3428 case R.styleable.View_scrollbarStyle:
3429 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3430 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3431 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3432 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3433 }
3434 break;
3435 case R.styleable.View_isScrollContainer:
3436 setScrollContainer = true;
3437 if (a.getBoolean(attr, false)) {
3438 setScrollContainer(true);
3439 }
3440 break;
3441 case com.android.internal.R.styleable.View_keepScreenOn:
3442 if (a.getBoolean(attr, false)) {
3443 viewFlagValues |= KEEP_SCREEN_ON;
3444 viewFlagMasks |= KEEP_SCREEN_ON;
3445 }
3446 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003447 case R.styleable.View_filterTouchesWhenObscured:
3448 if (a.getBoolean(attr, false)) {
3449 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3450 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3451 }
3452 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003453 case R.styleable.View_nextFocusLeft:
3454 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3455 break;
3456 case R.styleable.View_nextFocusRight:
3457 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3458 break;
3459 case R.styleable.View_nextFocusUp:
3460 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3461 break;
3462 case R.styleable.View_nextFocusDown:
3463 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3464 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003465 case R.styleable.View_nextFocusForward:
3466 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3467 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003468 case R.styleable.View_minWidth:
3469 mMinWidth = a.getDimensionPixelSize(attr, 0);
3470 break;
3471 case R.styleable.View_minHeight:
3472 mMinHeight = a.getDimensionPixelSize(attr, 0);
3473 break;
Romain Guy9a817362009-05-01 10:57:14 -07003474 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003475 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003476 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003477 + "be used within a restricted context");
3478 }
3479
Romain Guy9a817362009-05-01 10:57:14 -07003480 final String handlerName = a.getString(attr);
3481 if (handlerName != null) {
3482 setOnClickListener(new OnClickListener() {
3483 private Method mHandler;
3484
3485 public void onClick(View v) {
3486 if (mHandler == null) {
3487 try {
3488 mHandler = getContext().getClass().getMethod(handlerName,
3489 View.class);
3490 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003491 int id = getId();
3492 String idText = id == NO_ID ? "" : " with id '"
3493 + getContext().getResources().getResourceEntryName(
3494 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003495 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003496 handlerName + "(View) in the activity "
3497 + getContext().getClass() + " for onClick handler"
3498 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003499 }
3500 }
3501
3502 try {
3503 mHandler.invoke(getContext(), View.this);
3504 } catch (IllegalAccessException e) {
3505 throw new IllegalStateException("Could not execute non "
3506 + "public method of the activity", e);
3507 } catch (InvocationTargetException e) {
3508 throw new IllegalStateException("Could not execute "
3509 + "method of the activity", e);
3510 }
3511 }
3512 });
3513 }
3514 break;
Adam Powell637d3372010-08-25 14:37:03 -07003515 case R.styleable.View_overScrollMode:
3516 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3517 break;
Adam Powell20232d02010-12-08 21:08:53 -08003518 case R.styleable.View_verticalScrollbarPosition:
3519 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3520 break;
Romain Guy171c5922011-01-06 10:04:23 -08003521 case R.styleable.View_layerType:
3522 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3523 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003524 case R.styleable.View_textDirection:
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003525 // Clear any text direction flag already set
Dianne Hackborn4702a852012-08-17 15:18:29 -07003526 mPrivateFlags2 &= ~PFLAG2_TEXT_DIRECTION_MASK;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003527 // Set the text direction flags depending on the value of the attribute
3528 final int textDirection = a.getInt(attr, -1);
3529 if (textDirection != -1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07003530 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_FLAGS[textDirection];
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003531 }
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003532 break;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003533 case R.styleable.View_textAlignment:
3534 // Clear any text alignment flag already set
Dianne Hackborn4702a852012-08-17 15:18:29 -07003535 mPrivateFlags2 &= ~PFLAG2_TEXT_ALIGNMENT_MASK;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003536 // Set the text alignment flag depending on the value of the attribute
3537 final int textAlignment = a.getInt(attr, TEXT_ALIGNMENT_DEFAULT);
Dianne Hackborn4702a852012-08-17 15:18:29 -07003538 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_FLAGS[textAlignment];
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003539 break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07003540 case R.styleable.View_importantForAccessibility:
3541 setImportantForAccessibility(a.getInt(attr,
3542 IMPORTANT_FOR_ACCESSIBILITY_DEFAULT));
Svetoslav Ganov86783472012-06-06 21:12:20 -07003543 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003544 }
3545 }
3546
Adam Powell637d3372010-08-25 14:37:03 -07003547 setOverScrollMode(overScrollMode);
3548
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003549 // Cache start/end user padding as we cannot fully resolve padding here (we dont have yet
3550 // the resolved layout direction). Those cached values will be used later during padding
3551 // resolution.
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003552 mUserPaddingStart = startPadding;
3553 mUserPaddingEnd = endPadding;
3554
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003555 if (background != null) {
3556 setBackground(background);
3557 }
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003559 if (padding >= 0) {
3560 leftPadding = padding;
3561 topPadding = padding;
3562 rightPadding = padding;
3563 bottomPadding = padding;
3564 }
3565
3566 // If the user specified the padding (either with android:padding or
3567 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3568 // use the default padding or the padding from the background drawable
3569 // (stored at this point in mPadding*)
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003570 internalSetPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003571 topPadding >= 0 ? topPadding : mPaddingTop,
3572 rightPadding >= 0 ? rightPadding : mPaddingRight,
3573 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3574
3575 if (viewFlagMasks != 0) {
3576 setFlags(viewFlagValues, viewFlagMasks);
3577 }
3578
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003579 if (initializeScrollbars) {
3580 initializeScrollbars(a);
3581 }
3582
3583 a.recycle();
3584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003585 // Needs to be called after mViewFlags is set
3586 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3587 recomputePadding();
3588 }
3589
3590 if (x != 0 || y != 0) {
3591 scrollTo(x, y);
3592 }
3593
Chet Haase73066682010-11-29 15:55:32 -08003594 if (transformSet) {
3595 setTranslationX(tx);
3596 setTranslationY(ty);
3597 setRotation(rotation);
3598 setRotationX(rotationX);
3599 setRotationY(rotationY);
3600 setScaleX(sx);
3601 setScaleY(sy);
3602 }
3603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003604 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3605 setScrollContainer(true);
3606 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003607
3608 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003609 }
3610
3611 /**
3612 * Non-public constructor for use in testing
3613 */
3614 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003615 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003616 }
3617
Dianne Hackborn4702a852012-08-17 15:18:29 -07003618 public String toString() {
3619 StringBuilder out = new StringBuilder(128);
3620 out.append(getClass().getName());
3621 out.append('{');
3622 out.append(Integer.toHexString(System.identityHashCode(this)));
3623 out.append(' ');
3624 switch (mViewFlags&VISIBILITY_MASK) {
3625 case VISIBLE: out.append('V'); break;
3626 case INVISIBLE: out.append('I'); break;
3627 case GONE: out.append('G'); break;
3628 default: out.append('.'); break;
3629 }
3630 out.append((mViewFlags&FOCUSABLE_MASK) == FOCUSABLE ? 'F' : '.');
3631 out.append((mViewFlags&ENABLED_MASK) == ENABLED ? 'E' : '.');
3632 out.append((mViewFlags&DRAW_MASK) == WILL_NOT_DRAW ? '.' : 'D');
3633 out.append((mViewFlags&SCROLLBARS_HORIZONTAL) != 0 ? 'H' : '.');
3634 out.append((mViewFlags&SCROLLBARS_VERTICAL) != 0 ? 'V' : '.');
3635 out.append((mViewFlags&CLICKABLE) != 0 ? 'C' : '.');
3636 out.append((mViewFlags&LONG_CLICKABLE) != 0 ? 'L' : '.');
3637 out.append(' ');
3638 out.append((mPrivateFlags&PFLAG_IS_ROOT_NAMESPACE) != 0 ? 'R' : '.');
3639 out.append((mPrivateFlags&PFLAG_FOCUSED) != 0 ? 'F' : '.');
3640 out.append((mPrivateFlags&PFLAG_SELECTED) != 0 ? 'S' : '.');
3641 if ((mPrivateFlags&PFLAG_PREPRESSED) != 0) {
3642 out.append('p');
3643 } else {
3644 out.append((mPrivateFlags&PFLAG_PRESSED) != 0 ? 'P' : '.');
3645 }
3646 out.append((mPrivateFlags&PFLAG_HOVERED) != 0 ? 'H' : '.');
3647 out.append((mPrivateFlags&PFLAG_ACTIVATED) != 0 ? 'A' : '.');
3648 out.append((mPrivateFlags&PFLAG_INVALIDATED) != 0 ? 'I' : '.');
3649 out.append((mPrivateFlags&PFLAG_DIRTY_MASK) != 0 ? 'D' : '.');
3650 out.append(' ');
3651 out.append(mLeft);
3652 out.append(',');
3653 out.append(mTop);
3654 out.append('-');
3655 out.append(mRight);
3656 out.append(',');
3657 out.append(mBottom);
3658 final int id = getId();
3659 if (id != NO_ID) {
3660 out.append(" #");
3661 out.append(Integer.toHexString(id));
3662 final Resources r = mResources;
3663 if (id != 0 && r != null) {
3664 try {
3665 String pkgname;
3666 switch (id&0xff000000) {
3667 case 0x7f000000:
3668 pkgname="app";
3669 break;
3670 case 0x01000000:
3671 pkgname="android";
3672 break;
3673 default:
3674 pkgname = r.getResourcePackageName(id);
3675 break;
3676 }
3677 String typename = r.getResourceTypeName(id);
3678 String entryname = r.getResourceEntryName(id);
3679 out.append(" ");
3680 out.append(pkgname);
3681 out.append(":");
3682 out.append(typename);
3683 out.append("/");
3684 out.append(entryname);
3685 } catch (Resources.NotFoundException e) {
3686 }
3687 }
3688 }
3689 out.append("}");
3690 return out.toString();
3691 }
3692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003693 /**
3694 * <p>
3695 * Initializes the fading edges from a given set of styled attributes. This
3696 * method should be called by subclasses that need fading edges and when an
3697 * instance of these subclasses is created programmatically rather than
3698 * being inflated from XML. This method is automatically called when the XML
3699 * is inflated.
3700 * </p>
3701 *
3702 * @param a the styled attributes set to initialize the fading edges from
3703 */
3704 protected void initializeFadingEdge(TypedArray a) {
3705 initScrollCache();
3706
3707 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3708 R.styleable.View_fadingEdgeLength,
3709 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3710 }
3711
3712 /**
3713 * Returns the size of the vertical faded edges used to indicate that more
3714 * content in this view is visible.
3715 *
3716 * @return The size in pixels of the vertical faded edge or 0 if vertical
3717 * faded edges are not enabled for this view.
3718 * @attr ref android.R.styleable#View_fadingEdgeLength
3719 */
3720 public int getVerticalFadingEdgeLength() {
3721 if (isVerticalFadingEdgeEnabled()) {
3722 ScrollabilityCache cache = mScrollCache;
3723 if (cache != null) {
3724 return cache.fadingEdgeLength;
3725 }
3726 }
3727 return 0;
3728 }
3729
3730 /**
3731 * Set the size of the faded edge used to indicate that more content in this
3732 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003733 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3734 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3735 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003736 *
3737 * @param length The size in pixels of the faded edge used to indicate that more
3738 * content in this view is visible.
3739 */
3740 public void setFadingEdgeLength(int length) {
3741 initScrollCache();
3742 mScrollCache.fadingEdgeLength = length;
3743 }
3744
3745 /**
3746 * Returns the size of the horizontal faded edges used to indicate that more
3747 * content in this view is visible.
3748 *
3749 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3750 * faded edges are not enabled for this view.
3751 * @attr ref android.R.styleable#View_fadingEdgeLength
3752 */
3753 public int getHorizontalFadingEdgeLength() {
3754 if (isHorizontalFadingEdgeEnabled()) {
3755 ScrollabilityCache cache = mScrollCache;
3756 if (cache != null) {
3757 return cache.fadingEdgeLength;
3758 }
3759 }
3760 return 0;
3761 }
3762
3763 /**
3764 * Returns the width of the vertical scrollbar.
3765 *
3766 * @return The width in pixels of the vertical scrollbar or 0 if there
3767 * is no vertical scrollbar.
3768 */
3769 public int getVerticalScrollbarWidth() {
3770 ScrollabilityCache cache = mScrollCache;
3771 if (cache != null) {
3772 ScrollBarDrawable scrollBar = cache.scrollBar;
3773 if (scrollBar != null) {
3774 int size = scrollBar.getSize(true);
3775 if (size <= 0) {
3776 size = cache.scrollBarSize;
3777 }
3778 return size;
3779 }
3780 return 0;
3781 }
3782 return 0;
3783 }
3784
3785 /**
3786 * Returns the height of the horizontal scrollbar.
3787 *
3788 * @return The height in pixels of the horizontal scrollbar or 0 if
3789 * there is no horizontal scrollbar.
3790 */
3791 protected int getHorizontalScrollbarHeight() {
3792 ScrollabilityCache cache = mScrollCache;
3793 if (cache != null) {
3794 ScrollBarDrawable scrollBar = cache.scrollBar;
3795 if (scrollBar != null) {
3796 int size = scrollBar.getSize(false);
3797 if (size <= 0) {
3798 size = cache.scrollBarSize;
3799 }
3800 return size;
3801 }
3802 return 0;
3803 }
3804 return 0;
3805 }
3806
3807 /**
3808 * <p>
3809 * Initializes the scrollbars from a given set of styled attributes. This
3810 * method should be called by subclasses that need scrollbars and when an
3811 * instance of these subclasses is created programmatically rather than
3812 * being inflated from XML. This method is automatically called when the XML
3813 * is inflated.
3814 * </p>
3815 *
3816 * @param a the styled attributes set to initialize the scrollbars from
3817 */
3818 protected void initializeScrollbars(TypedArray a) {
3819 initScrollCache();
3820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003821 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003822
Mike Cleronf116bf82009-09-27 19:14:12 -07003823 if (scrollabilityCache.scrollBar == null) {
3824 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3825 }
Joe Malin32736f02011-01-19 16:14:20 -08003826
Romain Guy8bda2482010-03-02 11:42:11 -08003827 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003828
Mike Cleronf116bf82009-09-27 19:14:12 -07003829 if (!fadeScrollbars) {
3830 scrollabilityCache.state = ScrollabilityCache.ON;
3831 }
3832 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003833
3834
Mike Cleronf116bf82009-09-27 19:14:12 -07003835 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3836 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3837 .getScrollBarFadeDuration());
3838 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3839 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3840 ViewConfiguration.getScrollDefaultDelay());
3841
Joe Malin32736f02011-01-19 16:14:20 -08003842
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003843 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3844 com.android.internal.R.styleable.View_scrollbarSize,
3845 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3846
3847 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3848 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3849
3850 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3851 if (thumb != null) {
3852 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3853 }
3854
3855 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3856 false);
3857 if (alwaysDraw) {
3858 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3859 }
3860
3861 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3862 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3863
3864 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3865 if (thumb != null) {
3866 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3867 }
3868
3869 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3870 false);
3871 if (alwaysDraw) {
3872 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3873 }
3874
Fabrice Di Megliob03b4342012-06-04 12:55:30 -07003875 // Apply layout direction to the new Drawables if needed
3876 final int layoutDirection = getResolvedLayoutDirection();
3877 if (track != null) {
3878 track.setLayoutDirection(layoutDirection);
3879 }
3880 if (thumb != null) {
3881 thumb.setLayoutDirection(layoutDirection);
3882 }
3883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003884 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003885 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003886 }
3887
3888 /**
3889 * <p>
3890 * Initalizes the scrollability cache if necessary.
3891 * </p>
3892 */
3893 private void initScrollCache() {
3894 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003895 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003896 }
3897 }
3898
Philip Milne6c8ea062012-04-03 17:38:43 -07003899 private ScrollabilityCache getScrollCache() {
3900 initScrollCache();
3901 return mScrollCache;
3902 }
3903
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003904 /**
Adam Powell20232d02010-12-08 21:08:53 -08003905 * Set the position of the vertical scroll bar. Should be one of
3906 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3907 * {@link #SCROLLBAR_POSITION_RIGHT}.
3908 *
3909 * @param position Where the vertical scroll bar should be positioned.
3910 */
3911 public void setVerticalScrollbarPosition(int position) {
3912 if (mVerticalScrollbarPosition != position) {
3913 mVerticalScrollbarPosition = position;
3914 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003915 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003916 }
3917 }
3918
3919 /**
3920 * @return The position where the vertical scroll bar will show, if applicable.
3921 * @see #setVerticalScrollbarPosition(int)
3922 */
3923 public int getVerticalScrollbarPosition() {
3924 return mVerticalScrollbarPosition;
3925 }
3926
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003927 ListenerInfo getListenerInfo() {
3928 if (mListenerInfo != null) {
3929 return mListenerInfo;
3930 }
3931 mListenerInfo = new ListenerInfo();
3932 return mListenerInfo;
3933 }
3934
Adam Powell20232d02010-12-08 21:08:53 -08003935 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003936 * Register a callback to be invoked when focus of this view changed.
3937 *
3938 * @param l The callback that will run.
3939 */
3940 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003941 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003942 }
3943
3944 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003945 * Add a listener that will be called when the bounds of the view change due to
3946 * layout processing.
3947 *
3948 * @param listener The listener that will be called when layout bounds change.
3949 */
3950 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003951 ListenerInfo li = getListenerInfo();
3952 if (li.mOnLayoutChangeListeners == null) {
3953 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003954 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003955 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3956 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003957 }
Chet Haase21cd1382010-09-01 17:42:29 -07003958 }
3959
3960 /**
3961 * Remove a listener for layout changes.
3962 *
3963 * @param listener The listener for layout bounds change.
3964 */
3965 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003966 ListenerInfo li = mListenerInfo;
3967 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003968 return;
3969 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003970 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003971 }
3972
3973 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003974 * Add a listener for attach state changes.
3975 *
3976 * This listener will be called whenever this view is attached or detached
3977 * from a window. Remove the listener using
3978 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3979 *
3980 * @param listener Listener to attach
3981 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3982 */
3983 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003984 ListenerInfo li = getListenerInfo();
3985 if (li.mOnAttachStateChangeListeners == null) {
3986 li.mOnAttachStateChangeListeners
3987 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003988 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003989 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003990 }
3991
3992 /**
3993 * Remove a listener for attach state changes. The listener will receive no further
3994 * notification of window attach/detach events.
3995 *
3996 * @param listener Listener to remove
3997 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3998 */
3999 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004000 ListenerInfo li = mListenerInfo;
4001 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08004002 return;
4003 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004004 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08004005 }
4006
4007 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004008 * Returns the focus-change callback registered for this view.
4009 *
4010 * @return The callback, or null if one is not registered.
4011 */
4012 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004013 ListenerInfo li = mListenerInfo;
4014 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004015 }
4016
4017 /**
4018 * Register a callback to be invoked when this view is clicked. If this view is not
4019 * clickable, it becomes clickable.
4020 *
4021 * @param l The callback that will run
4022 *
4023 * @see #setClickable(boolean)
4024 */
4025 public void setOnClickListener(OnClickListener l) {
4026 if (!isClickable()) {
4027 setClickable(true);
4028 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004029 getListenerInfo().mOnClickListener = l;
4030 }
4031
4032 /**
4033 * Return whether this view has an attached OnClickListener. Returns
4034 * true if there is a listener, false if there is none.
4035 */
4036 public boolean hasOnClickListeners() {
4037 ListenerInfo li = mListenerInfo;
4038 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004039 }
4040
4041 /**
4042 * Register a callback to be invoked when this view is clicked and held. If this view is not
4043 * long clickable, it becomes long clickable.
4044 *
4045 * @param l The callback that will run
4046 *
4047 * @see #setLongClickable(boolean)
4048 */
4049 public void setOnLongClickListener(OnLongClickListener l) {
4050 if (!isLongClickable()) {
4051 setLongClickable(true);
4052 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004053 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004054 }
4055
4056 /**
4057 * Register a callback to be invoked when the context menu for this view is
4058 * being built. If this view is not long clickable, it becomes long clickable.
4059 *
4060 * @param l The callback that will run
4061 *
4062 */
4063 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
4064 if (!isLongClickable()) {
4065 setLongClickable(true);
4066 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004067 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004068 }
4069
4070 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004071 * Call this view's OnClickListener, if it is defined. Performs all normal
4072 * actions associated with clicking: reporting accessibility event, playing
4073 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004074 *
4075 * @return True there was an assigned OnClickListener that was called, false
4076 * otherwise is returned.
4077 */
4078 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07004079 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
4080
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004081 ListenerInfo li = mListenerInfo;
4082 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004083 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004084 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004085 return true;
4086 }
4087
4088 return false;
4089 }
4090
4091 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004092 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
4093 * this only calls the listener, and does not do any associated clicking
4094 * actions like reporting an accessibility event.
4095 *
4096 * @return True there was an assigned OnClickListener that was called, false
4097 * otherwise is returned.
4098 */
4099 public boolean callOnClick() {
4100 ListenerInfo li = mListenerInfo;
4101 if (li != null && li.mOnClickListener != null) {
4102 li.mOnClickListener.onClick(this);
4103 return true;
4104 }
4105 return false;
4106 }
4107
4108 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004109 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
4110 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004111 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004112 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004113 */
4114 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07004115 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
4116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004117 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004118 ListenerInfo li = mListenerInfo;
4119 if (li != null && li.mOnLongClickListener != null) {
4120 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004121 }
4122 if (!handled) {
4123 handled = showContextMenu();
4124 }
4125 if (handled) {
4126 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
4127 }
4128 return handled;
4129 }
4130
4131 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004132 * Performs button-related actions during a touch down event.
4133 *
4134 * @param event The event.
4135 * @return True if the down was consumed.
4136 *
4137 * @hide
4138 */
4139 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
4140 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
4141 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
4142 return true;
4143 }
4144 }
4145 return false;
4146 }
4147
4148 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004149 * Bring up the context menu for this view.
4150 *
4151 * @return Whether a context menu was displayed.
4152 */
4153 public boolean showContextMenu() {
4154 return getParent().showContextMenuForChild(this);
4155 }
4156
4157 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004158 * Bring up the context menu for this view, referring to the item under the specified point.
4159 *
4160 * @param x The referenced x coordinate.
4161 * @param y The referenced y coordinate.
4162 * @param metaState The keyboard modifiers that were pressed.
4163 * @return Whether a context menu was displayed.
4164 *
4165 * @hide
4166 */
4167 public boolean showContextMenu(float x, float y, int metaState) {
4168 return showContextMenu();
4169 }
4170
4171 /**
Adam Powell6e346362010-07-23 10:18:23 -07004172 * Start an action mode.
4173 *
4174 * @param callback Callback that will control the lifecycle of the action mode
4175 * @return The new action mode if it is started, null otherwise
4176 *
4177 * @see ActionMode
4178 */
4179 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08004180 ViewParent parent = getParent();
4181 if (parent == null) return null;
4182 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07004183 }
4184
4185 /**
Jean Chalard405bc512012-05-29 19:12:34 +09004186 * Register a callback to be invoked when a hardware key is pressed in this view.
4187 * Key presses in software input methods will generally not trigger the methods of
4188 * this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004189 * @param l the key listener to attach to this view
4190 */
4191 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004192 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004193 }
4194
4195 /**
4196 * Register a callback to be invoked when a touch event is sent to this view.
4197 * @param l the touch listener to attach to this view
4198 */
4199 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004200 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004201 }
4202
4203 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004204 * Register a callback to be invoked when a generic motion event is sent to this view.
4205 * @param l the generic motion listener to attach to this view
4206 */
4207 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004208 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08004209 }
4210
4211 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07004212 * Register a callback to be invoked when a hover event is sent to this view.
4213 * @param l the hover listener to attach to this view
4214 */
4215 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004216 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07004217 }
4218
4219 /**
Joe Malin32736f02011-01-19 16:14:20 -08004220 * Register a drag event listener callback object for this View. The parameter is
4221 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
4222 * View, the system calls the
4223 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
4224 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07004225 */
4226 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004227 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07004228 }
4229
4230 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07004231 * Give this view focus. This will cause
4232 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004233 *
4234 * Note: this does not check whether this {@link View} should get focus, it just
4235 * gives it focus no matter what. It should only be called internally by framework
4236 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
4237 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004238 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
4239 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004240 * focus moved when requestFocus() is called. It may not always
4241 * apply, in which case use the default View.FOCUS_DOWN.
4242 * @param previouslyFocusedRect The rectangle of the view that had focus
4243 * prior in this View's coordinate system.
4244 */
4245 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
4246 if (DBG) {
4247 System.out.println(this + " requestFocus()");
4248 }
4249
Dianne Hackborn4702a852012-08-17 15:18:29 -07004250 if ((mPrivateFlags & PFLAG_FOCUSED) == 0) {
4251 mPrivateFlags |= PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004252
4253 if (mParent != null) {
4254 mParent.requestChildFocus(this, this);
4255 }
4256
4257 onFocusChanged(true, direction, previouslyFocusedRect);
4258 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004259
4260 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4261 notifyAccessibilityStateChanged();
4262 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004263 }
4264 }
4265
4266 /**
4267 * Request that a rectangle of this view be visible on the screen,
4268 * scrolling if necessary just enough.
4269 *
4270 * <p>A View should call this if it maintains some notion of which part
4271 * of its content is interesting. For example, a text editing view
4272 * should call this when its cursor moves.
4273 *
4274 * @param rectangle The rectangle.
4275 * @return Whether any parent scrolled.
4276 */
4277 public boolean requestRectangleOnScreen(Rect rectangle) {
4278 return requestRectangleOnScreen(rectangle, false);
4279 }
4280
4281 /**
4282 * Request that a rectangle of this view be visible on the screen,
4283 * scrolling if necessary just enough.
4284 *
4285 * <p>A View should call this if it maintains some notion of which part
4286 * of its content is interesting. For example, a text editing view
4287 * should call this when its cursor moves.
4288 *
4289 * <p>When <code>immediate</code> is set to true, scrolling will not be
4290 * animated.
4291 *
4292 * @param rectangle The rectangle.
4293 * @param immediate True to forbid animated scrolling, false otherwise
4294 * @return Whether any parent scrolled.
4295 */
4296 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004297 if (mAttachInfo == null) {
4298 return false;
4299 }
4300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004301 View child = this;
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004302
4303 RectF position = mAttachInfo.mTmpTransformRect;
4304 position.set(rectangle);
4305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004306 ViewParent parent = mParent;
4307 boolean scrolled = false;
4308 while (parent != null) {
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004309 rectangle.set((int) position.left, (int) position.top,
4310 (int) position.right, (int) position.bottom);
4311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004312 scrolled |= parent.requestChildRectangleOnScreen(child,
4313 rectangle, immediate);
4314
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004315 if (!child.hasIdentityMatrix()) {
4316 child.getMatrix().mapRect(position);
4317 }
4318
4319 position.offset(child.mLeft, child.mTop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004320
4321 if (!(parent instanceof View)) {
4322 break;
4323 }
Romain Guy8506ab42009-06-11 17:35:47 -07004324
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004325 View parentView = (View) parent;
4326
4327 position.offset(-parentView.getScrollX(), -parentView.getScrollY());
4328
4329 child = parentView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004330 parent = child.getParent();
4331 }
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004333 return scrolled;
4334 }
4335
4336 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004337 * Called when this view wants to give up focus. If focus is cleared
4338 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
4339 * <p>
4340 * <strong>Note:</strong> When a View clears focus the framework is trying
4341 * to give focus to the first focusable View from the top. Hence, if this
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004342 * View is the first from the top that can take focus, then all callbacks
4343 * related to clearing focus will be invoked after wich the framework will
4344 * give focus to this view.
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004345 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004346 */
4347 public void clearFocus() {
4348 if (DBG) {
4349 System.out.println(this + " clearFocus()");
4350 }
4351
Dianne Hackborn4702a852012-08-17 15:18:29 -07004352 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
4353 mPrivateFlags &= ~PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004354
4355 if (mParent != null) {
4356 mParent.clearChildFocus(this);
4357 }
4358
4359 onFocusChanged(false, 0, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004361 refreshDrawableState();
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004362
4363 ensureInputFocusOnFirstFocusable();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004364
4365 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4366 notifyAccessibilityStateChanged();
4367 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004368 }
4369 }
4370
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004371 void ensureInputFocusOnFirstFocusable() {
4372 View root = getRootView();
4373 if (root != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004374 root.requestFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004375 }
4376 }
4377
4378 /**
4379 * Called internally by the view system when a new view is getting focus.
4380 * This is what clears the old focus.
4381 */
4382 void unFocus() {
4383 if (DBG) {
4384 System.out.println(this + " unFocus()");
4385 }
4386
Dianne Hackborn4702a852012-08-17 15:18:29 -07004387 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
4388 mPrivateFlags &= ~PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004389
4390 onFocusChanged(false, 0, null);
4391 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004392
4393 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4394 notifyAccessibilityStateChanged();
4395 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004396 }
4397 }
4398
4399 /**
4400 * Returns true if this view has focus iteself, or is the ancestor of the
4401 * view that has focus.
4402 *
4403 * @return True if this view has or contains focus, false otherwise.
4404 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004405 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004406 public boolean hasFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07004407 return (mPrivateFlags & PFLAG_FOCUSED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004408 }
4409
4410 /**
4411 * Returns true if this view is focusable or if it contains a reachable View
4412 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
4413 * is a View whose parents do not block descendants focus.
4414 *
4415 * Only {@link #VISIBLE} views are considered focusable.
4416 *
4417 * @return True if the view is focusable or if the view contains a focusable
4418 * View, false otherwise.
4419 *
4420 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
4421 */
4422 public boolean hasFocusable() {
4423 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4424 }
4425
4426 /**
4427 * Called by the view system when the focus state of this view changes.
4428 * When the focus change event is caused by directional navigation, direction
4429 * and previouslyFocusedRect provide insight into where the focus is coming from.
4430 * When overriding, be sure to call up through to the super class so that
4431 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004432 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004433 * @param gainFocus True if the View has focus; false otherwise.
4434 * @param direction The direction focus has moved when requestFocus()
4435 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004436 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4437 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4438 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004439 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4440 * system, of the previously focused view. If applicable, this will be
4441 * passed in as finer grained information about where the focus is coming
4442 * from (in addition to direction). Will be <code>null</code> otherwise.
4443 */
4444 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004445 if (gainFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004446 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4447 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004448 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004449 }
4450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004451 InputMethodManager imm = InputMethodManager.peekInstance();
4452 if (!gainFocus) {
4453 if (isPressed()) {
4454 setPressed(false);
4455 }
4456 if (imm != null && mAttachInfo != null
4457 && mAttachInfo.mHasWindowFocus) {
4458 imm.focusOut(this);
4459 }
Romain Guya2431d02009-04-30 16:30:00 -07004460 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004461 } else if (imm != null && mAttachInfo != null
4462 && mAttachInfo.mHasWindowFocus) {
4463 imm.focusIn(this);
4464 }
Romain Guy8506ab42009-06-11 17:35:47 -07004465
Romain Guy0fd89bf2011-01-26 15:41:30 -08004466 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004467 ListenerInfo li = mListenerInfo;
4468 if (li != null && li.mOnFocusChangeListener != null) {
4469 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004470 }
Joe Malin32736f02011-01-19 16:14:20 -08004471
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004472 if (mAttachInfo != null) {
4473 mAttachInfo.mKeyDispatchState.reset(this);
4474 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004475 }
4476
4477 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004478 * Sends an accessibility event of the given type. If accessiiblity is
4479 * not enabled this method has no effect. The default implementation calls
4480 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4481 * to populate information about the event source (this View), then calls
4482 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4483 * populate the text content of the event source including its descendants,
4484 * and last calls
4485 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4486 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004487 * <p>
4488 * If an {@link AccessibilityDelegate} has been specified via calling
4489 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4490 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4491 * responsible for handling this call.
4492 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004493 *
Scott Mainb303d832011-10-12 16:45:18 -07004494 * @param eventType The type of the event to send, as defined by several types from
4495 * {@link android.view.accessibility.AccessibilityEvent}, such as
4496 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4497 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004498 *
4499 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4500 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4501 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004502 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004503 */
4504 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004505 if (mAccessibilityDelegate != null) {
4506 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4507 } else {
4508 sendAccessibilityEventInternal(eventType);
4509 }
4510 }
4511
4512 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004513 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4514 * {@link AccessibilityEvent} to make an announcement which is related to some
4515 * sort of a context change for which none of the events representing UI transitions
4516 * is a good fit. For example, announcing a new page in a book. If accessibility
4517 * is not enabled this method does nothing.
4518 *
4519 * @param text The announcement text.
4520 */
4521 public void announceForAccessibility(CharSequence text) {
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004522 if (AccessibilityManager.getInstance(mContext).isEnabled() && mParent != null) {
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004523 AccessibilityEvent event = AccessibilityEvent.obtain(
4524 AccessibilityEvent.TYPE_ANNOUNCEMENT);
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004525 onInitializeAccessibilityEvent(event);
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004526 event.getText().add(text);
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004527 event.setContentDescription(null);
4528 mParent.requestSendAccessibilityEvent(this, event);
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004529 }
4530 }
4531
4532 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004533 * @see #sendAccessibilityEvent(int)
4534 *
4535 * Note: Called from the default {@link AccessibilityDelegate}.
4536 */
4537 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004538 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4539 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4540 }
4541 }
4542
4543 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004544 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4545 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004546 * perform a check whether accessibility is enabled.
4547 * <p>
4548 * If an {@link AccessibilityDelegate} has been specified via calling
4549 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4550 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4551 * is responsible for handling this call.
4552 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004553 *
4554 * @param event The event to send.
4555 *
4556 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004557 */
4558 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004559 if (mAccessibilityDelegate != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004560 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004561 } else {
4562 sendAccessibilityEventUncheckedInternal(event);
4563 }
4564 }
4565
4566 /**
4567 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4568 *
4569 * Note: Called from the default {@link AccessibilityDelegate}.
4570 */
4571 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004572 if (!isShown()) {
4573 return;
4574 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004575 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004576 // Only a subset of accessibility events populates text content.
4577 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4578 dispatchPopulateAccessibilityEvent(event);
4579 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004580 // In the beginning we called #isShown(), so we know that getParent() is not null.
4581 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004582 }
4583
4584 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004585 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4586 * to its children for adding their text content to the event. Note that the
4587 * event text is populated in a separate dispatch path since we add to the
4588 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004589 * A typical implementation will call
4590 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4591 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4592 * on each child. Override this method if custom population of the event text
4593 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004594 * <p>
4595 * If an {@link AccessibilityDelegate} has been specified via calling
4596 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4597 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4598 * is responsible for handling this call.
4599 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004600 * <p>
4601 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4602 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4603 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004604 *
4605 * @param event The event.
4606 *
4607 * @return True if the event population was completed.
4608 */
4609 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004610 if (mAccessibilityDelegate != null) {
4611 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4612 } else {
4613 return dispatchPopulateAccessibilityEventInternal(event);
4614 }
4615 }
4616
4617 /**
4618 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4619 *
4620 * Note: Called from the default {@link AccessibilityDelegate}.
4621 */
4622 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004623 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004624 return false;
4625 }
4626
4627 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004628 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004629 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004630 * text content. While this method is free to modify event
4631 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004632 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4633 * <p>
4634 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004635 * to the text added by the super implementation:
4636 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004637 * super.onPopulateAccessibilityEvent(event);
4638 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4639 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4640 * mCurrentDate.getTimeInMillis(), flags);
4641 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004642 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004643 * <p>
4644 * If an {@link AccessibilityDelegate} has been specified via calling
4645 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4646 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4647 * is responsible for handling this call.
4648 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004649 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4650 * information to the event, in case the default implementation has basic information to add.
4651 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004652 *
4653 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004654 *
4655 * @see #sendAccessibilityEvent(int)
4656 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004657 */
4658 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004659 if (mAccessibilityDelegate != null) {
4660 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4661 } else {
4662 onPopulateAccessibilityEventInternal(event);
4663 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004664 }
4665
4666 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004667 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4668 *
4669 * Note: Called from the default {@link AccessibilityDelegate}.
4670 */
4671 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4672
4673 }
4674
4675 /**
4676 * Initializes an {@link AccessibilityEvent} with information about
4677 * this View which is the event source. In other words, the source of
4678 * an accessibility event is the view whose state change triggered firing
4679 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004680 * <p>
4681 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004682 * to properties set by the super implementation:
4683 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4684 * super.onInitializeAccessibilityEvent(event);
4685 * event.setPassword(true);
4686 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004687 * <p>
4688 * If an {@link AccessibilityDelegate} has been specified via calling
4689 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4690 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4691 * is responsible for handling this call.
4692 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004693 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4694 * information to the event, in case the default implementation has basic information to add.
4695 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004696 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004697 *
4698 * @see #sendAccessibilityEvent(int)
4699 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4700 */
4701 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004702 if (mAccessibilityDelegate != null) {
4703 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4704 } else {
4705 onInitializeAccessibilityEventInternal(event);
4706 }
4707 }
4708
4709 /**
4710 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4711 *
4712 * Note: Called from the default {@link AccessibilityDelegate}.
4713 */
4714 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004715 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004716 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004717 event.setPackageName(getContext().getPackageName());
4718 event.setEnabled(isEnabled());
4719 event.setContentDescription(mContentDescription);
4720
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004721 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004722 ArrayList<View> focusablesTempList = mAttachInfo.mTempArrayList;
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004723 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4724 FOCUSABLES_ALL);
4725 event.setItemCount(focusablesTempList.size());
4726 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4727 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004728 }
4729 }
4730
4731 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004732 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4733 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4734 * This method is responsible for obtaining an accessibility node info from a
4735 * pool of reusable instances and calling
4736 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4737 * initialize the former.
4738 * <p>
4739 * Note: The client is responsible for recycling the obtained instance by calling
4740 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4741 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004742 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004743 * @return A populated {@link AccessibilityNodeInfo}.
4744 *
4745 * @see AccessibilityNodeInfo
4746 */
4747 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004748 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4749 if (provider != null) {
4750 return provider.createAccessibilityNodeInfo(View.NO_ID);
4751 } else {
4752 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4753 onInitializeAccessibilityNodeInfo(info);
4754 return info;
4755 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004756 }
4757
4758 /**
4759 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4760 * The base implementation sets:
4761 * <ul>
4762 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004763 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4764 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004765 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4766 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4767 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4768 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4769 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4770 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4771 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4772 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4773 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4774 * </ul>
4775 * <p>
4776 * Subclasses should override this method, call the super implementation,
4777 * and set additional attributes.
4778 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004779 * <p>
4780 * If an {@link AccessibilityDelegate} has been specified via calling
4781 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4782 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4783 * is responsible for handling this call.
4784 * </p>
4785 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004786 * @param info The instance to initialize.
4787 */
4788 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004789 if (mAccessibilityDelegate != null) {
4790 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4791 } else {
4792 onInitializeAccessibilityNodeInfoInternal(info);
4793 }
4794 }
4795
4796 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004797 * Gets the location of this view in screen coordintates.
4798 *
4799 * @param outRect The output location
4800 */
4801 private void getBoundsOnScreen(Rect outRect) {
4802 if (mAttachInfo == null) {
4803 return;
4804 }
4805
4806 RectF position = mAttachInfo.mTmpTransformRect;
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004807 position.set(0, 0, mRight - mLeft, mBottom - mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004808
4809 if (!hasIdentityMatrix()) {
4810 getMatrix().mapRect(position);
4811 }
4812
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004813 position.offset(mLeft, mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004814
4815 ViewParent parent = mParent;
4816 while (parent instanceof View) {
4817 View parentView = (View) parent;
4818
4819 position.offset(-parentView.mScrollX, -parentView.mScrollY);
4820
4821 if (!parentView.hasIdentityMatrix()) {
4822 parentView.getMatrix().mapRect(position);
4823 }
4824
4825 position.offset(parentView.mLeft, parentView.mTop);
4826
4827 parent = parentView.mParent;
4828 }
4829
4830 if (parent instanceof ViewRootImpl) {
4831 ViewRootImpl viewRootImpl = (ViewRootImpl) parent;
4832 position.offset(0, -viewRootImpl.mCurScrollY);
4833 }
4834
4835 position.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
4836
4837 outRect.set((int) (position.left + 0.5f), (int) (position.top + 0.5f),
4838 (int) (position.right + 0.5f), (int) (position.bottom + 0.5f));
4839 }
4840
4841 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004842 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4843 *
4844 * Note: Called from the default {@link AccessibilityDelegate}.
4845 */
4846 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004847 Rect bounds = mAttachInfo.mTmpInvalRect;
Svetoslav Ganov983119a2012-07-03 21:04:10 -07004848
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004849 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004850 info.setBoundsInParent(bounds);
4851
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004852 getBoundsOnScreen(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004853 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004854
Svetoslav Ganovc406be92012-05-11 16:12:32 -07004855 ViewParent parent = getParentForAccessibility();
4856 if (parent instanceof View) {
4857 info.setParent((View) parent);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004858 }
4859
Svetoslav Ganov33aef982012-09-13 12:49:03 -07004860 if (mID != View.NO_ID) {
4861 View rootView = getRootView();
4862 if (rootView == null) {
4863 rootView = this;
4864 }
4865 View label = rootView.findLabelForView(this, mID);
4866 if (label != null) {
4867 info.setLabeledBy(label);
4868 }
4869 }
4870
4871 if (mLabelForId != View.NO_ID) {
4872 View rootView = getRootView();
4873 if (rootView == null) {
4874 rootView = this;
4875 }
4876 View labeled = rootView.findViewInsideOutShouldExist(this, mLabelForId);
4877 if (labeled != null) {
4878 info.setLabelFor(labeled);
4879 }
4880 }
4881
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004882 info.setVisibleToUser(isVisibleToUser());
4883
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004884 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004885 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004886 info.setContentDescription(getContentDescription());
4887
4888 info.setEnabled(isEnabled());
4889 info.setClickable(isClickable());
4890 info.setFocusable(isFocusable());
4891 info.setFocused(isFocused());
Svetoslav Ganov42138042012-03-20 11:51:39 -07004892 info.setAccessibilityFocused(isAccessibilityFocused());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004893 info.setSelected(isSelected());
4894 info.setLongClickable(isLongClickable());
4895
4896 // TODO: These make sense only if we are in an AdapterView but all
4897 // views can be selected. Maybe from accessiiblity perspective
4898 // we should report as selectable view in an AdapterView.
4899 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4900 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4901
4902 if (isFocusable()) {
4903 if (isFocused()) {
4904 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4905 } else {
4906 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4907 }
4908 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004909
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07004910 if (!isAccessibilityFocused()) {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07004911 info.addAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07004912 } else {
4913 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
4914 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004915
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07004916 if (isClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004917 info.addAction(AccessibilityNodeInfo.ACTION_CLICK);
4918 }
4919
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07004920 if (isLongClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004921 info.addAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
4922 }
4923
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004924 if (mContentDescription != null && mContentDescription.length() > 0) {
Svetoslav Ganov2b435aa2012-05-04 17:16:37 -07004925 info.addAction(AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY);
4926 info.addAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY);
4927 info.setMovementGranularities(AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004928 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD
4929 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH);
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004930 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004931 }
4932
Svetoslav Ganov33aef982012-09-13 12:49:03 -07004933 private View findLabelForView(View view, int labeledId) {
4934 if (mMatchLabelForPredicate == null) {
4935 mMatchLabelForPredicate = new MatchLabelForPredicate();
4936 }
4937 mMatchLabelForPredicate.mLabeledId = labeledId;
4938 return findViewByPredicateInsideOut(view, mMatchLabelForPredicate);
4939 }
4940
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004941 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004942 * Computes whether this view is visible to the user. Such a view is
4943 * attached, visible, all its predecessors are visible, it is not clipped
4944 * entirely by its predecessors, and has an alpha greater than zero.
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004945 *
4946 * @return Whether the view is visible on the screen.
Guang Zhu0d607fb2012-05-11 19:34:56 -07004947 *
4948 * @hide
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004949 */
Guang Zhu0d607fb2012-05-11 19:34:56 -07004950 protected boolean isVisibleToUser() {
4951 return isVisibleToUser(null);
4952 }
4953
4954 /**
Romain Guyf0af1d52012-07-11 18:31:21 -07004955 * Computes whether the given portion of this view is visible to the user.
4956 * Such a view is attached, visible, all its predecessors are visible,
4957 * has an alpha greater than zero, and the specified portion is not
4958 * clipped entirely by its predecessors.
Guang Zhu0d607fb2012-05-11 19:34:56 -07004959 *
4960 * @param boundInView the portion of the view to test; coordinates should be relative; may be
4961 * <code>null</code>, and the entire view will be tested in this case.
4962 * When <code>true</code> is returned by the function, the actual visible
4963 * region will be stored in this parameter; that is, if boundInView is fully
4964 * contained within the view, no modification will be made, otherwise regions
4965 * outside of the visible area of the view will be clipped.
4966 *
4967 * @return Whether the specified portion of the view is visible on the screen.
4968 *
4969 * @hide
4970 */
4971 protected boolean isVisibleToUser(Rect boundInView) {
Romain Guyf0af1d52012-07-11 18:31:21 -07004972 if (mAttachInfo != null) {
4973 Rect visibleRect = mAttachInfo.mTmpInvalRect;
4974 Point offset = mAttachInfo.mPoint;
4975 // The first two checks are made also made by isShown() which
4976 // however traverses the tree up to the parent to catch that.
4977 // Therefore, we do some fail fast check to minimize the up
4978 // tree traversal.
4979 boolean isVisible = mAttachInfo.mWindowVisibility == View.VISIBLE
4980 && getAlpha() > 0
4981 && isShown()
4982 && getGlobalVisibleRect(visibleRect, offset);
Guang Zhu0d607fb2012-05-11 19:34:56 -07004983 if (isVisible && boundInView != null) {
4984 visibleRect.offset(-offset.x, -offset.y);
Romain Guyf0af1d52012-07-11 18:31:21 -07004985 // isVisible is always true here, use a simple assignment
4986 isVisible = boundInView.intersect(visibleRect);
Guang Zhu0d607fb2012-05-11 19:34:56 -07004987 }
4988 return isVisible;
Romain Guyf0af1d52012-07-11 18:31:21 -07004989 }
4990
4991 return false;
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004992 }
4993
4994 /**
alanvb72fe7a2012-08-27 16:44:25 -07004995 * Returns the delegate for implementing accessibility support via
4996 * composition. For more details see {@link AccessibilityDelegate}.
4997 *
4998 * @return The delegate, or null if none set.
4999 *
5000 * @hide
5001 */
5002 public AccessibilityDelegate getAccessibilityDelegate() {
5003 return mAccessibilityDelegate;
5004 }
5005
5006 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07005007 * Sets a delegate for implementing accessibility support via compositon as
5008 * opposed to inheritance. The delegate's primary use is for implementing
5009 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
5010 *
5011 * @param delegate The delegate instance.
5012 *
5013 * @see AccessibilityDelegate
5014 */
5015 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
5016 mAccessibilityDelegate = delegate;
5017 }
5018
5019 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07005020 * Gets the provider for managing a virtual view hierarchy rooted at this View
5021 * and reported to {@link android.accessibilityservice.AccessibilityService}s
5022 * that explore the window content.
5023 * <p>
5024 * If this method returns an instance, this instance is responsible for managing
5025 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
5026 * View including the one representing the View itself. Similarly the returned
5027 * instance is responsible for performing accessibility actions on any virtual
5028 * view or the root view itself.
5029 * </p>
5030 * <p>
5031 * If an {@link AccessibilityDelegate} has been specified via calling
5032 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
5033 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
5034 * is responsible for handling this call.
5035 * </p>
5036 *
5037 * @return The provider.
5038 *
5039 * @see AccessibilityNodeProvider
5040 */
5041 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
5042 if (mAccessibilityDelegate != null) {
5043 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
5044 } else {
5045 return null;
5046 }
5047 }
5048
5049 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005050 * Gets the unique identifier of this view on the screen for accessibility purposes.
5051 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
5052 *
5053 * @return The view accessibility id.
5054 *
5055 * @hide
5056 */
5057 public int getAccessibilityViewId() {
5058 if (mAccessibilityViewId == NO_ID) {
5059 mAccessibilityViewId = sNextAccessibilityViewId++;
5060 }
5061 return mAccessibilityViewId;
5062 }
5063
5064 /**
5065 * Gets the unique identifier of the window in which this View reseides.
5066 *
5067 * @return The window accessibility id.
5068 *
5069 * @hide
5070 */
5071 public int getAccessibilityWindowId() {
5072 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
5073 }
5074
5075 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07005076 * Gets the {@link View} description. It briefly describes the view and is
5077 * primarily used for accessibility support. Set this property to enable
5078 * better accessibility support for your application. This is especially
5079 * true for views that do not have textual representation (For example,
5080 * ImageButton).
5081 *
Svetoslav Ganov42138042012-03-20 11:51:39 -07005082 * @return The content description.
svetoslavganov75986cf2009-05-14 22:28:01 -07005083 *
5084 * @attr ref android.R.styleable#View_contentDescription
5085 */
Svetoslav Ganov42138042012-03-20 11:51:39 -07005086 @ViewDebug.ExportedProperty(category = "accessibility")
svetoslavganov75986cf2009-05-14 22:28:01 -07005087 public CharSequence getContentDescription() {
5088 return mContentDescription;
5089 }
5090
5091 /**
5092 * Sets the {@link View} description. It briefly describes the view and is
5093 * primarily used for accessibility support. Set this property to enable
5094 * better accessibility support for your application. This is especially
5095 * true for views that do not have textual representation (For example,
5096 * ImageButton).
5097 *
5098 * @param contentDescription The content description.
5099 *
5100 * @attr ref android.R.styleable#View_contentDescription
5101 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07005102 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07005103 public void setContentDescription(CharSequence contentDescription) {
5104 mContentDescription = contentDescription;
Svetoslav Ganove47957a2012-06-05 14:46:50 -07005105 final boolean nonEmptyDesc = contentDescription != null && contentDescription.length() > 0;
5106 if (nonEmptyDesc && getImportantForAccessibility() == IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
5107 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
5108 }
svetoslavganov75986cf2009-05-14 22:28:01 -07005109 }
5110
5111 /**
Svetoslav Ganov33aef982012-09-13 12:49:03 -07005112 * Gets the id of a view for which this view serves as a label for
5113 * accessibility purposes.
5114 *
5115 * @return The labeled view id.
5116 */
5117 @ViewDebug.ExportedProperty(category = "accessibility")
5118 public int getLabelFor() {
5119 return mLabelForId;
5120 }
5121
5122 /**
5123 * Sets the id of a view for which this view serves as a label for
5124 * accessibility purposes.
5125 *
5126 * @param id The labeled view id.
5127 */
5128 @RemotableViewMethod
5129 public void setLabelFor(int id) {
5130 mLabelForId = id;
5131 if (mLabelForId != View.NO_ID
5132 && mID == View.NO_ID) {
5133 mID = generateViewId();
5134 }
5135 }
5136
5137 /**
Romain Guya2431d02009-04-30 16:30:00 -07005138 * Invoked whenever this view loses focus, either by losing window focus or by losing
5139 * focus within its window. This method can be used to clear any state tied to the
5140 * focus. For instance, if a button is held pressed with the trackball and the window
5141 * loses focus, this method can be used to cancel the press.
5142 *
5143 * Subclasses of View overriding this method should always call super.onFocusLost().
5144 *
5145 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07005146 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07005147 *
5148 * @hide pending API council approval
5149 */
5150 protected void onFocusLost() {
5151 resetPressedState();
5152 }
5153
5154 private void resetPressedState() {
5155 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5156 return;
5157 }
5158
5159 if (isPressed()) {
5160 setPressed(false);
5161
5162 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05005163 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005164 }
5165 }
5166 }
5167
5168 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005169 * Returns true if this view has focus
5170 *
5171 * @return True if this view has focus, false otherwise.
5172 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005173 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005174 public boolean isFocused() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005175 return (mPrivateFlags & PFLAG_FOCUSED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005176 }
5177
5178 /**
5179 * Find the view in the hierarchy rooted at this view that currently has
5180 * focus.
5181 *
5182 * @return The view that currently has focus, or null if no focused view can
5183 * be found.
5184 */
5185 public View findFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005186 return (mPrivateFlags & PFLAG_FOCUSED) != 0 ? this : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005187 }
5188
5189 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07005190 * Indicates whether this view is one of the set of scrollable containers in
5191 * its window.
5192 *
5193 * @return whether this view is one of the set of scrollable containers in
5194 * its window
5195 *
5196 * @attr ref android.R.styleable#View_isScrollContainer
5197 */
5198 public boolean isScrollContainer() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005199 return (mPrivateFlags & PFLAG_SCROLL_CONTAINER_ADDED) != 0;
Philip Milne6c8ea062012-04-03 17:38:43 -07005200 }
5201
5202 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005203 * Change whether this view is one of the set of scrollable containers in
5204 * its window. This will be used to determine whether the window can
5205 * resize or must pan when a soft input area is open -- scrollable
5206 * containers allow the window to use resize mode since the container
5207 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07005208 *
5209 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005210 */
5211 public void setScrollContainer(boolean isScrollContainer) {
5212 if (isScrollContainer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005213 if (mAttachInfo != null && (mPrivateFlags&PFLAG_SCROLL_CONTAINER_ADDED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005214 mAttachInfo.mScrollContainers.add(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -07005215 mPrivateFlags |= PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005216 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005217 mPrivateFlags |= PFLAG_SCROLL_CONTAINER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005218 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005219 if ((mPrivateFlags&PFLAG_SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005220 mAttachInfo.mScrollContainers.remove(this);
5221 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005222 mPrivateFlags &= ~(PFLAG_SCROLL_CONTAINER|PFLAG_SCROLL_CONTAINER_ADDED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005223 }
5224 }
5225
5226 /**
5227 * Returns the quality of the drawing cache.
5228 *
5229 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5230 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5231 *
5232 * @see #setDrawingCacheQuality(int)
5233 * @see #setDrawingCacheEnabled(boolean)
5234 * @see #isDrawingCacheEnabled()
5235 *
5236 * @attr ref android.R.styleable#View_drawingCacheQuality
5237 */
5238 public int getDrawingCacheQuality() {
5239 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
5240 }
5241
5242 /**
5243 * Set the drawing cache quality of this view. This value is used only when the
5244 * drawing cache is enabled
5245 *
5246 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5247 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5248 *
5249 * @see #getDrawingCacheQuality()
5250 * @see #setDrawingCacheEnabled(boolean)
5251 * @see #isDrawingCacheEnabled()
5252 *
5253 * @attr ref android.R.styleable#View_drawingCacheQuality
5254 */
5255 public void setDrawingCacheQuality(int quality) {
5256 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
5257 }
5258
5259 /**
5260 * Returns whether the screen should remain on, corresponding to the current
5261 * value of {@link #KEEP_SCREEN_ON}.
5262 *
5263 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
5264 *
5265 * @see #setKeepScreenOn(boolean)
5266 *
5267 * @attr ref android.R.styleable#View_keepScreenOn
5268 */
5269 public boolean getKeepScreenOn() {
5270 return (mViewFlags & KEEP_SCREEN_ON) != 0;
5271 }
5272
5273 /**
5274 * Controls whether the screen should remain on, modifying the
5275 * value of {@link #KEEP_SCREEN_ON}.
5276 *
5277 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
5278 *
5279 * @see #getKeepScreenOn()
5280 *
5281 * @attr ref android.R.styleable#View_keepScreenOn
5282 */
5283 public void setKeepScreenOn(boolean keepScreenOn) {
5284 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
5285 }
5286
5287 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005288 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5289 * @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 -08005290 *
5291 * @attr ref android.R.styleable#View_nextFocusLeft
5292 */
5293 public int getNextFocusLeftId() {
5294 return mNextFocusLeftId;
5295 }
5296
5297 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005298 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5299 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
5300 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005301 *
5302 * @attr ref android.R.styleable#View_nextFocusLeft
5303 */
5304 public void setNextFocusLeftId(int nextFocusLeftId) {
5305 mNextFocusLeftId = nextFocusLeftId;
5306 }
5307
5308 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005309 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5310 * @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 -08005311 *
5312 * @attr ref android.R.styleable#View_nextFocusRight
5313 */
5314 public int getNextFocusRightId() {
5315 return mNextFocusRightId;
5316 }
5317
5318 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005319 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5320 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
5321 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005322 *
5323 * @attr ref android.R.styleable#View_nextFocusRight
5324 */
5325 public void setNextFocusRightId(int nextFocusRightId) {
5326 mNextFocusRightId = nextFocusRightId;
5327 }
5328
5329 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005330 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5331 * @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 -08005332 *
5333 * @attr ref android.R.styleable#View_nextFocusUp
5334 */
5335 public int getNextFocusUpId() {
5336 return mNextFocusUpId;
5337 }
5338
5339 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005340 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5341 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
5342 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005343 *
5344 * @attr ref android.R.styleable#View_nextFocusUp
5345 */
5346 public void setNextFocusUpId(int nextFocusUpId) {
5347 mNextFocusUpId = nextFocusUpId;
5348 }
5349
5350 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005351 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5352 * @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 -08005353 *
5354 * @attr ref android.R.styleable#View_nextFocusDown
5355 */
5356 public int getNextFocusDownId() {
5357 return mNextFocusDownId;
5358 }
5359
5360 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005361 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5362 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
5363 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005364 *
5365 * @attr ref android.R.styleable#View_nextFocusDown
5366 */
5367 public void setNextFocusDownId(int nextFocusDownId) {
5368 mNextFocusDownId = nextFocusDownId;
5369 }
5370
5371 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005372 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5373 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
5374 *
5375 * @attr ref android.R.styleable#View_nextFocusForward
5376 */
5377 public int getNextFocusForwardId() {
5378 return mNextFocusForwardId;
5379 }
5380
5381 /**
5382 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5383 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
5384 * decide automatically.
5385 *
5386 * @attr ref android.R.styleable#View_nextFocusForward
5387 */
5388 public void setNextFocusForwardId(int nextFocusForwardId) {
5389 mNextFocusForwardId = nextFocusForwardId;
5390 }
5391
5392 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005393 * Returns the visibility of this view and all of its ancestors
5394 *
5395 * @return True if this view and all of its ancestors are {@link #VISIBLE}
5396 */
5397 public boolean isShown() {
5398 View current = this;
5399 //noinspection ConstantConditions
5400 do {
5401 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5402 return false;
5403 }
5404 ViewParent parent = current.mParent;
5405 if (parent == null) {
5406 return false; // We are not attached to the view root
5407 }
5408 if (!(parent instanceof View)) {
5409 return true;
5410 }
5411 current = (View) parent;
5412 } while (current != null);
5413
5414 return false;
5415 }
5416
5417 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005418 * Called by the view hierarchy when the content insets for a window have
5419 * changed, to allow it to adjust its content to fit within those windows.
5420 * The content insets tell you the space that the status bar, input method,
5421 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005422 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005423 * <p>You do not normally need to deal with this function, since the default
5424 * window decoration given to applications takes care of applying it to the
5425 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
5426 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
5427 * and your content can be placed under those system elements. You can then
5428 * use this method within your view hierarchy if you have parts of your UI
5429 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005430 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005431 * <p>The default implementation of this method simply applies the content
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005432 * inset's to the view's padding, consuming that content (modifying the
5433 * insets to be 0), and returning true. This behavior is off by default, but can
5434 * be enabled through {@link #setFitsSystemWindows(boolean)}.
5435 *
5436 * <p>This function's traversal down the hierarchy is depth-first. The same content
5437 * insets object is propagated down the hierarchy, so any changes made to it will
5438 * be seen by all following views (including potentially ones above in
5439 * the hierarchy since this is a depth-first traversal). The first view
5440 * that returns true will abort the entire traversal.
5441 *
5442 * <p>The default implementation works well for a situation where it is
5443 * used with a container that covers the entire window, allowing it to
5444 * apply the appropriate insets to its content on all edges. If you need
5445 * a more complicated layout (such as two different views fitting system
5446 * windows, one on the top of the window, and one on the bottom),
5447 * you can override the method and handle the insets however you would like.
5448 * Note that the insets provided by the framework are always relative to the
5449 * far edges of the window, not accounting for the location of the called view
5450 * within that window. (In fact when this method is called you do not yet know
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005451 * where the layout will place the view, as it is done before layout happens.)
5452 *
5453 * <p>Note: unlike many View methods, there is no dispatch phase to this
5454 * call. If you are overriding it in a ViewGroup and want to allow the
5455 * call to continue to your children, you must be sure to call the super
5456 * implementation.
5457 *
Dianne Hackborncf675782012-05-10 15:07:24 -07005458 * <p>Here is a sample layout that makes use of fitting system windows
5459 * to have controls for a video view placed inside of the window decorations
5460 * that it hides and shows. This can be used with code like the second
5461 * sample (video player) shown in {@link #setSystemUiVisibility(int)}.
5462 *
5463 * {@sample development/samples/ApiDemos/res/layout/video_player.xml complete}
5464 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005465 * @param insets Current content insets of the window. Prior to
5466 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
5467 * the insets or else you and Android will be unhappy.
5468 *
5469 * @return Return true if this view applied the insets and it should not
5470 * continue propagating further down the hierarchy, false otherwise.
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005471 * @see #getFitsSystemWindows()
Romain Guyf0af1d52012-07-11 18:31:21 -07005472 * @see #setFitsSystemWindows(boolean)
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005473 * @see #setSystemUiVisibility(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005474 */
5475 protected boolean fitSystemWindows(Rect insets) {
5476 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07005477 mUserPaddingStart = UNDEFINED_PADDING;
5478 mUserPaddingEnd = UNDEFINED_PADDING;
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005479 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
5480 || mAttachInfo == null
5481 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
5482 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
5483 return true;
5484 } else {
5485 internalSetPadding(0, 0, 0, 0);
5486 return false;
5487 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005488 }
5489 return false;
5490 }
5491
5492 /**
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005493 * Sets whether or not this view should account for system screen decorations
5494 * such as the status bar and inset its content; that is, controlling whether
5495 * the default implementation of {@link #fitSystemWindows(Rect)} will be
5496 * executed. See that method for more details.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005497 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005498 * <p>Note that if you are providing your own implementation of
5499 * {@link #fitSystemWindows(Rect)}, then there is no need to set this
5500 * flag to true -- your implementation will be overriding the default
5501 * implementation that checks this flag.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005502 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005503 * @param fitSystemWindows If true, then the default implementation of
5504 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005505 *
5506 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005507 * @see #getFitsSystemWindows()
5508 * @see #fitSystemWindows(Rect)
5509 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005510 */
5511 public void setFitsSystemWindows(boolean fitSystemWindows) {
5512 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
5513 }
5514
5515 /**
Dianne Hackborncf675782012-05-10 15:07:24 -07005516 * Check for state of {@link #setFitsSystemWindows(boolean). If this method
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005517 * returns true, the default implementation of {@link #fitSystemWindows(Rect)}
5518 * will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005519 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005520 * @return Returns true if the default implementation of
5521 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005522 *
5523 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005524 * @see #setFitsSystemWindows()
5525 * @see #fitSystemWindows(Rect)
5526 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005527 */
Dianne Hackborncf675782012-05-10 15:07:24 -07005528 public boolean getFitsSystemWindows() {
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005529 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
5530 }
5531
Dianne Hackbornb1b55e62012-05-10 16:25:54 -07005532 /** @hide */
5533 public boolean fitsSystemWindows() {
5534 return getFitsSystemWindows();
5535 }
5536
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005537 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005538 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
5539 */
5540 public void requestFitSystemWindows() {
5541 if (mParent != null) {
5542 mParent.requestFitSystemWindows();
5543 }
5544 }
5545
5546 /**
5547 * For use by PhoneWindow to make its own system window fitting optional.
5548 * @hide
5549 */
5550 public void makeOptionalFitsSystemWindows() {
5551 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5552 }
5553
5554 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005555 * Returns the visibility status for this view.
5556 *
5557 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5558 * @attr ref android.R.styleable#View_visibility
5559 */
5560 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005561 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5562 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5563 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005564 })
5565 public int getVisibility() {
5566 return mViewFlags & VISIBILITY_MASK;
5567 }
5568
5569 /**
5570 * Set the enabled state of this view.
5571 *
5572 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5573 * @attr ref android.R.styleable#View_visibility
5574 */
5575 @RemotableViewMethod
5576 public void setVisibility(int visibility) {
5577 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005578 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005579 }
5580
5581 /**
5582 * Returns the enabled status for this view. The interpretation of the
5583 * enabled state varies by subclass.
5584 *
5585 * @return True if this view is enabled, false otherwise.
5586 */
5587 @ViewDebug.ExportedProperty
5588 public boolean isEnabled() {
5589 return (mViewFlags & ENABLED_MASK) == ENABLED;
5590 }
5591
5592 /**
5593 * Set the enabled state of this view. The interpretation of the enabled
5594 * state varies by subclass.
5595 *
5596 * @param enabled True if this view is enabled, false otherwise.
5597 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005598 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005599 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005600 if (enabled == isEnabled()) return;
5601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005602 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5603
5604 /*
5605 * The View most likely has to change its appearance, so refresh
5606 * the drawable state.
5607 */
5608 refreshDrawableState();
5609
5610 // Invalidate too, since the default behavior for views is to be
5611 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005612 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005613 }
5614
5615 /**
5616 * Set whether this view can receive the focus.
5617 *
5618 * Setting this to false will also ensure that this view is not focusable
5619 * in touch mode.
5620 *
5621 * @param focusable If true, this view can receive the focus.
5622 *
5623 * @see #setFocusableInTouchMode(boolean)
5624 * @attr ref android.R.styleable#View_focusable
5625 */
5626 public void setFocusable(boolean focusable) {
5627 if (!focusable) {
5628 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5629 }
5630 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5631 }
5632
5633 /**
5634 * Set whether this view can receive focus while in touch mode.
5635 *
5636 * Setting this to true will also ensure that this view is focusable.
5637 *
5638 * @param focusableInTouchMode If true, this view can receive the focus while
5639 * in touch mode.
5640 *
5641 * @see #setFocusable(boolean)
5642 * @attr ref android.R.styleable#View_focusableInTouchMode
5643 */
5644 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5645 // Focusable in touch mode should always be set before the focusable flag
5646 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5647 // which, in touch mode, will not successfully request focus on this view
5648 // because the focusable in touch mode flag is not set
5649 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5650 if (focusableInTouchMode) {
5651 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5652 }
5653 }
5654
5655 /**
5656 * Set whether this view should have sound effects enabled for events such as
5657 * clicking and touching.
5658 *
5659 * <p>You may wish to disable sound effects for a view if you already play sounds,
5660 * for instance, a dial key that plays dtmf tones.
5661 *
5662 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5663 * @see #isSoundEffectsEnabled()
5664 * @see #playSoundEffect(int)
5665 * @attr ref android.R.styleable#View_soundEffectsEnabled
5666 */
5667 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5668 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5669 }
5670
5671 /**
5672 * @return whether this view should have sound effects enabled for events such as
5673 * clicking and touching.
5674 *
5675 * @see #setSoundEffectsEnabled(boolean)
5676 * @see #playSoundEffect(int)
5677 * @attr ref android.R.styleable#View_soundEffectsEnabled
5678 */
5679 @ViewDebug.ExportedProperty
5680 public boolean isSoundEffectsEnabled() {
5681 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5682 }
5683
5684 /**
5685 * Set whether this view should have haptic feedback for events such as
5686 * long presses.
5687 *
5688 * <p>You may wish to disable haptic feedback if your view already controls
5689 * its own haptic feedback.
5690 *
5691 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5692 * @see #isHapticFeedbackEnabled()
5693 * @see #performHapticFeedback(int)
5694 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5695 */
5696 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5697 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5698 }
5699
5700 /**
5701 * @return whether this view should have haptic feedback enabled for events
5702 * long presses.
5703 *
5704 * @see #setHapticFeedbackEnabled(boolean)
5705 * @see #performHapticFeedback(int)
5706 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5707 */
5708 @ViewDebug.ExportedProperty
5709 public boolean isHapticFeedbackEnabled() {
5710 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5711 }
5712
5713 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005714 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005715 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005716 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5717 * {@link #LAYOUT_DIRECTION_RTL},
5718 * {@link #LAYOUT_DIRECTION_INHERIT} or
5719 * {@link #LAYOUT_DIRECTION_LOCALE}.
5720 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005721 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005722 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005723 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5724 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5725 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5726 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005727 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005728 public int getLayoutDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005729 return (mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_MASK) >> PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005730 }
5731
5732 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005733 * Set the layout direction for this view. This will propagate a reset of layout direction
5734 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005735 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005736 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
5737 * {@link #LAYOUT_DIRECTION_RTL},
5738 * {@link #LAYOUT_DIRECTION_INHERIT} or
5739 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005740 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005741 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005742 */
5743 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005744 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005745 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005746 // Reset the current layout direction and the resolved one
Dianne Hackborn4702a852012-08-17 15:18:29 -07005747 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07005748 resetResolvedLayoutDirection();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005749 // Reset padding resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -07005750 mPrivateFlags2 &= ~PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005751 // Set the new layout direction (filtered)
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005752 mPrivateFlags2 |=
Dianne Hackborn4702a852012-08-17 15:18:29 -07005753 ((layoutDirection << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) & PFLAG2_LAYOUT_DIRECTION_MASK);
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005754 resolveRtlProperties();
5755 // ... and ask for a layout pass
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005756 requestLayout();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005757 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005758 }
5759
5760 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005761 * Returns the resolved layout direction for this view.
5762 *
5763 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005764 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005765 */
5766 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005767 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5768 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005769 })
5770 public int getResolvedLayoutDirection() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005771 final int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion;
5772 if (targetSdkVersion < JELLY_BEAN_MR1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005773 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005774 return LAYOUT_DIRECTION_LTR;
5775 }
Fabrice Di Megliob93911f2012-06-26 19:43:15 -07005776 // The layout direction will be resolved only if needed
Dianne Hackborn4702a852012-08-17 15:18:29 -07005777 if ((mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_RESOLVED) != PFLAG2_LAYOUT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -07005778 resolveLayoutDirection();
5779 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005780 return ((mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL) == PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005781 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
5782 }
5783
5784 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005785 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5786 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005787 *
5788 * @return true if the layout is right-to-left.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005789 */
5790 @ViewDebug.ExportedProperty(category = "layout")
5791 public boolean isLayoutRtl() {
5792 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
5793 }
5794
5795 /**
Adam Powell539ee872012-02-03 19:00:49 -08005796 * Indicates whether the view is currently tracking transient state that the
5797 * app should not need to concern itself with saving and restoring, but that
5798 * the framework should take special note to preserve when possible.
5799 *
Adam Powell785c4472012-05-02 21:25:39 -07005800 * <p>A view with transient state cannot be trivially rebound from an external
5801 * data source, such as an adapter binding item views in a list. This may be
5802 * because the view is performing an animation, tracking user selection
5803 * of content, or similar.</p>
5804 *
Adam Powell539ee872012-02-03 19:00:49 -08005805 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005806 */
5807 @ViewDebug.ExportedProperty(category = "layout")
5808 public boolean hasTransientState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005809 return (mPrivateFlags2 & PFLAG2_HAS_TRANSIENT_STATE) == PFLAG2_HAS_TRANSIENT_STATE;
Adam Powell539ee872012-02-03 19:00:49 -08005810 }
5811
5812 /**
5813 * Set whether this view is currently tracking transient state that the
Chet Haase563d4f22012-04-18 16:20:08 -07005814 * framework should attempt to preserve when possible. This flag is reference counted,
5815 * so every call to setHasTransientState(true) should be paired with a later call
5816 * to setHasTransientState(false).
Adam Powell539ee872012-02-03 19:00:49 -08005817 *
Adam Powell785c4472012-05-02 21:25:39 -07005818 * <p>A view with transient state cannot be trivially rebound from an external
5819 * data source, such as an adapter binding item views in a list. This may be
5820 * because the view is performing an animation, tracking user selection
5821 * of content, or similar.</p>
5822 *
Adam Powell539ee872012-02-03 19:00:49 -08005823 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005824 */
5825 public void setHasTransientState(boolean hasTransientState) {
Chet Haase563d4f22012-04-18 16:20:08 -07005826 mTransientStateCount = hasTransientState ? mTransientStateCount + 1 :
5827 mTransientStateCount - 1;
5828 if (mTransientStateCount < 0) {
5829 mTransientStateCount = 0;
5830 Log.e(VIEW_LOG_TAG, "hasTransientState decremented below 0: " +
5831 "unmatched pair of setHasTransientState calls");
5832 }
5833 if ((hasTransientState && mTransientStateCount == 1) ||
Adam Powell057a5852012-05-11 10:28:38 -07005834 (!hasTransientState && mTransientStateCount == 0)) {
Chet Haase563d4f22012-04-18 16:20:08 -07005835 // update flag if we've just incremented up from 0 or decremented down to 0
Dianne Hackborn4702a852012-08-17 15:18:29 -07005836 mPrivateFlags2 = (mPrivateFlags2 & ~PFLAG2_HAS_TRANSIENT_STATE) |
5837 (hasTransientState ? PFLAG2_HAS_TRANSIENT_STATE : 0);
Chet Haase563d4f22012-04-18 16:20:08 -07005838 if (mParent != null) {
5839 try {
5840 mParent.childHasTransientStateChanged(this, hasTransientState);
5841 } catch (AbstractMethodError e) {
5842 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5843 " does not fully implement ViewParent", e);
5844 }
Adam Powell539ee872012-02-03 19:00:49 -08005845 }
5846 }
5847 }
5848
5849 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005850 * If this view doesn't do any drawing on its own, set this flag to
5851 * allow further optimizations. By default, this flag is not set on
5852 * View, but could be set on some View subclasses such as ViewGroup.
5853 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005854 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5855 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005856 *
5857 * @param willNotDraw whether or not this View draw on its own
5858 */
5859 public void setWillNotDraw(boolean willNotDraw) {
5860 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5861 }
5862
5863 /**
5864 * Returns whether or not this View draws on its own.
5865 *
5866 * @return true if this view has nothing to draw, false otherwise
5867 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005868 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005869 public boolean willNotDraw() {
5870 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
5871 }
5872
5873 /**
5874 * When a View's drawing cache is enabled, drawing is redirected to an
5875 * offscreen bitmap. Some views, like an ImageView, must be able to
5876 * bypass this mechanism if they already draw a single bitmap, to avoid
5877 * unnecessary usage of the memory.
5878 *
5879 * @param willNotCacheDrawing true if this view does not cache its
5880 * drawing, false otherwise
5881 */
5882 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
5883 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
5884 }
5885
5886 /**
5887 * Returns whether or not this View can cache its drawing or not.
5888 *
5889 * @return true if this view does not cache its drawing, false otherwise
5890 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005891 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005892 public boolean willNotCacheDrawing() {
5893 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5894 }
5895
5896 /**
5897 * Indicates whether this view reacts to click events or not.
5898 *
5899 * @return true if the view is clickable, false otherwise
5900 *
5901 * @see #setClickable(boolean)
5902 * @attr ref android.R.styleable#View_clickable
5903 */
5904 @ViewDebug.ExportedProperty
5905 public boolean isClickable() {
5906 return (mViewFlags & CLICKABLE) == CLICKABLE;
5907 }
5908
5909 /**
5910 * Enables or disables click events for this view. When a view
5911 * is clickable it will change its state to "pressed" on every click.
5912 * Subclasses should set the view clickable to visually react to
5913 * user's clicks.
5914 *
5915 * @param clickable true to make the view clickable, false otherwise
5916 *
5917 * @see #isClickable()
5918 * @attr ref android.R.styleable#View_clickable
5919 */
5920 public void setClickable(boolean clickable) {
5921 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5922 }
5923
5924 /**
5925 * Indicates whether this view reacts to long click events or not.
5926 *
5927 * @return true if the view is long clickable, false otherwise
5928 *
5929 * @see #setLongClickable(boolean)
5930 * @attr ref android.R.styleable#View_longClickable
5931 */
5932 public boolean isLongClickable() {
5933 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5934 }
5935
5936 /**
5937 * Enables or disables long click events for this view. When a view is long
5938 * clickable it reacts to the user holding down the button for a longer
5939 * duration than a tap. This event can either launch the listener or a
5940 * context menu.
5941 *
5942 * @param longClickable true to make the view long clickable, false otherwise
5943 * @see #isLongClickable()
5944 * @attr ref android.R.styleable#View_longClickable
5945 */
5946 public void setLongClickable(boolean longClickable) {
5947 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5948 }
5949
5950 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005951 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005952 *
5953 * @see #isClickable()
5954 * @see #setClickable(boolean)
5955 *
5956 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5957 * the View's internal state from a previously set "pressed" state.
5958 */
5959 public void setPressed(boolean pressed) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005960 final boolean needsRefresh = pressed != ((mPrivateFlags & PFLAG_PRESSED) == PFLAG_PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005962 if (pressed) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005963 mPrivateFlags |= PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005964 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005965 mPrivateFlags &= ~PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005966 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005967
5968 if (needsRefresh) {
5969 refreshDrawableState();
5970 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005971 dispatchSetPressed(pressed);
5972 }
5973
5974 /**
5975 * Dispatch setPressed to all of this View's children.
5976 *
5977 * @see #setPressed(boolean)
5978 *
5979 * @param pressed The new pressed state
5980 */
5981 protected void dispatchSetPressed(boolean pressed) {
5982 }
5983
5984 /**
5985 * Indicates whether the view is currently in pressed state. Unless
5986 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5987 * the pressed state.
5988 *
Philip Milne6c8ea062012-04-03 17:38:43 -07005989 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005990 * @see #isClickable()
5991 * @see #setClickable(boolean)
5992 *
5993 * @return true if the view is currently pressed, false otherwise
5994 */
5995 public boolean isPressed() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005996 return (mPrivateFlags & PFLAG_PRESSED) == PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005997 }
5998
5999 /**
6000 * Indicates whether this view will save its state (that is,
6001 * whether its {@link #onSaveInstanceState} method will be called).
6002 *
6003 * @return Returns true if the view state saving is enabled, else false.
6004 *
6005 * @see #setSaveEnabled(boolean)
6006 * @attr ref android.R.styleable#View_saveEnabled
6007 */
6008 public boolean isSaveEnabled() {
6009 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
6010 }
6011
6012 /**
6013 * Controls whether the saving of this view's state is
6014 * enabled (that is, whether its {@link #onSaveInstanceState} method
6015 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07006016 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006017 * for its state to be saved. This flag can only disable the
6018 * saving of this view; any child views may still have their state saved.
6019 *
6020 * @param enabled Set to false to <em>disable</em> state saving, or true
6021 * (the default) to allow it.
6022 *
6023 * @see #isSaveEnabled()
6024 * @see #setId(int)
6025 * @see #onSaveInstanceState()
6026 * @attr ref android.R.styleable#View_saveEnabled
6027 */
6028 public void setSaveEnabled(boolean enabled) {
6029 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
6030 }
6031
Jeff Brown85a31762010-09-01 17:01:00 -07006032 /**
6033 * Gets whether the framework should discard touches when the view's
6034 * window is obscured by another visible window.
6035 * Refer to the {@link View} security documentation for more details.
6036 *
6037 * @return True if touch filtering is enabled.
6038 *
6039 * @see #setFilterTouchesWhenObscured(boolean)
6040 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
6041 */
6042 @ViewDebug.ExportedProperty
6043 public boolean getFilterTouchesWhenObscured() {
6044 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
6045 }
6046
6047 /**
6048 * Sets whether the framework should discard touches when the view's
6049 * window is obscured by another visible window.
6050 * Refer to the {@link View} security documentation for more details.
6051 *
6052 * @param enabled True if touch filtering should be enabled.
6053 *
6054 * @see #getFilterTouchesWhenObscured
6055 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
6056 */
6057 public void setFilterTouchesWhenObscured(boolean enabled) {
6058 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
6059 FILTER_TOUCHES_WHEN_OBSCURED);
6060 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006061
6062 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07006063 * Indicates whether the entire hierarchy under this view will save its
6064 * state when a state saving traversal occurs from its parent. The default
6065 * is true; if false, these views will not be saved unless
6066 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
6067 *
6068 * @return Returns true if the view state saving from parent is enabled, else false.
6069 *
6070 * @see #setSaveFromParentEnabled(boolean)
6071 */
6072 public boolean isSaveFromParentEnabled() {
6073 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
6074 }
6075
6076 /**
6077 * Controls whether the entire hierarchy under this view will save its
6078 * state when a state saving traversal occurs from its parent. The default
6079 * is true; if false, these views will not be saved unless
6080 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
6081 *
6082 * @param enabled Set to false to <em>disable</em> state saving, or true
6083 * (the default) to allow it.
6084 *
6085 * @see #isSaveFromParentEnabled()
6086 * @see #setId(int)
6087 * @see #onSaveInstanceState()
6088 */
6089 public void setSaveFromParentEnabled(boolean enabled) {
6090 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
6091 }
6092
6093
6094 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006095 * Returns whether this View is able to take focus.
6096 *
6097 * @return True if this view can take focus, or false otherwise.
6098 * @attr ref android.R.styleable#View_focusable
6099 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006100 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006101 public final boolean isFocusable() {
6102 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
6103 }
6104
6105 /**
6106 * When a view is focusable, it may not want to take focus when in touch mode.
6107 * For example, a button would like focus when the user is navigating via a D-pad
6108 * so that the user can click on it, but once the user starts touching the screen,
6109 * the button shouldn't take focus
6110 * @return Whether the view is focusable in touch mode.
6111 * @attr ref android.R.styleable#View_focusableInTouchMode
6112 */
6113 @ViewDebug.ExportedProperty
6114 public final boolean isFocusableInTouchMode() {
6115 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
6116 }
6117
6118 /**
6119 * Find the nearest view in the specified direction that can take focus.
6120 * This does not actually give focus to that view.
6121 *
6122 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6123 *
6124 * @return The nearest focusable in the specified direction, or null if none
6125 * can be found.
6126 */
6127 public View focusSearch(int direction) {
6128 if (mParent != null) {
6129 return mParent.focusSearch(this, direction);
6130 } else {
6131 return null;
6132 }
6133 }
6134
6135 /**
6136 * This method is the last chance for the focused view and its ancestors to
6137 * respond to an arrow key. This is called when the focused view did not
6138 * consume the key internally, nor could the view system find a new view in
6139 * the requested direction to give focus to.
6140 *
6141 * @param focused The currently focused view.
6142 * @param direction The direction focus wants to move. One of FOCUS_UP,
6143 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
6144 * @return True if the this view consumed this unhandled move.
6145 */
6146 public boolean dispatchUnhandledMove(View focused, int direction) {
6147 return false;
6148 }
6149
6150 /**
6151 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08006152 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006153 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08006154 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
6155 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006156 * @return The user specified next view, or null if there is none.
6157 */
6158 View findUserSetNextFocus(View root, int direction) {
6159 switch (direction) {
6160 case FOCUS_LEFT:
6161 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006162 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006163 case FOCUS_RIGHT:
6164 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006165 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006166 case FOCUS_UP:
6167 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006168 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006169 case FOCUS_DOWN:
6170 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006171 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006172 case FOCUS_FORWARD:
6173 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006174 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006175 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08006176 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08006177 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006178 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08006179 @Override
6180 public boolean apply(View t) {
6181 return t.mNextFocusForwardId == id;
6182 }
6183 });
6184 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006185 }
6186 return null;
6187 }
6188
Svetoslav Ganov33aef982012-09-13 12:49:03 -07006189 private View findViewInsideOutShouldExist(View root, int id) {
6190 if (mMatchIdPredicate == null) {
6191 mMatchIdPredicate = new MatchIdPredicate();
6192 }
6193 mMatchIdPredicate.mId = id;
6194 View result = root.findViewByPredicateInsideOut(this, mMatchIdPredicate);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006195 if (result == null) {
Svetoslav Ganov33aef982012-09-13 12:49:03 -07006196 Log.w(VIEW_LOG_TAG, "couldn't find view with id " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006197 }
6198 return result;
6199 }
6200
6201 /**
6202 * Find and return all focusable views that are descendants of this view,
6203 * possibly including this view if it is focusable itself.
6204 *
6205 * @param direction The direction of the focus
6206 * @return A list of focusable views
6207 */
6208 public ArrayList<View> getFocusables(int direction) {
6209 ArrayList<View> result = new ArrayList<View>(24);
6210 addFocusables(result, direction);
6211 return result;
6212 }
6213
6214 /**
6215 * Add any focusable views that are descendants of this view (possibly
6216 * including this view if it is focusable itself) to views. If we are in touch mode,
6217 * only add views that are also focusable in touch mode.
6218 *
6219 * @param views Focusable views found so far
6220 * @param direction The direction of the focus
6221 */
6222 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006223 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
6224 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006225
svetoslavganov75986cf2009-05-14 22:28:01 -07006226 /**
6227 * Adds any focusable views that are descendants of this view (possibly
6228 * including this view if it is focusable itself) to views. This method
6229 * adds all focusable views regardless if we are in touch mode or
Svetoslav Ganov42138042012-03-20 11:51:39 -07006230 * only views focusable in touch mode if we are in touch mode or
6231 * only views that can take accessibility focus if accessibility is enabeld
6232 * depending on the focusable mode paramater.
svetoslavganov75986cf2009-05-14 22:28:01 -07006233 *
6234 * @param views Focusable views found so far or null if all we are interested is
6235 * the number of focusables.
6236 * @param direction The direction of the focus.
6237 * @param focusableMode The type of focusables to be added.
6238 *
6239 * @see #FOCUSABLES_ALL
6240 * @see #FOCUSABLES_TOUCH_MODE
6241 */
6242 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006243 if (views == null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006244 return;
6245 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006246 if (!isFocusable()) {
6247 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07006248 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006249 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE
6250 && isInTouchMode() && !isFocusableInTouchMode()) {
6251 return;
6252 }
6253 views.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006254 }
6255
6256 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006257 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006258 * The search is performed by either the text that the View renders or the content
6259 * description that describes the view for accessibility purposes and the view does
6260 * not render or both. Clients can specify how the search is to be performed via
6261 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
6262 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006263 *
6264 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006265 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07006266 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006267 * @see #FIND_VIEWS_WITH_TEXT
6268 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
6269 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006270 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006271 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07006272 if (getAccessibilityNodeProvider() != null) {
6273 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
6274 outViews.add(this);
6275 }
6276 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006277 && (searched != null && searched.length() > 0)
6278 && (mContentDescription != null && mContentDescription.length() > 0)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006279 String searchedLowerCase = searched.toString().toLowerCase();
6280 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
6281 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
6282 outViews.add(this);
6283 }
6284 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006285 }
6286
6287 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006288 * Find and return all touchable views that are descendants of this view,
6289 * possibly including this view if it is touchable itself.
6290 *
6291 * @return A list of touchable views
6292 */
6293 public ArrayList<View> getTouchables() {
6294 ArrayList<View> result = new ArrayList<View>();
6295 addTouchables(result);
6296 return result;
6297 }
6298
6299 /**
6300 * Add any touchable views that are descendants of this view (possibly
6301 * including this view if it is touchable itself) to views.
6302 *
6303 * @param views Touchable views found so far
6304 */
6305 public void addTouchables(ArrayList<View> views) {
6306 final int viewFlags = mViewFlags;
6307
6308 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
6309 && (viewFlags & ENABLED_MASK) == ENABLED) {
6310 views.add(this);
6311 }
6312 }
6313
6314 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006315 * Returns whether this View is accessibility focused.
6316 *
6317 * @return True if this View is accessibility focused.
6318 */
6319 boolean isAccessibilityFocused() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006320 return (mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006321 }
6322
6323 /**
6324 * Call this to try to give accessibility focus to this view.
6325 *
6326 * A view will not actually take focus if {@link AccessibilityManager#isEnabled()}
6327 * returns false or the view is no visible or the view already has accessibility
6328 * focus.
6329 *
6330 * See also {@link #focusSearch(int)}, which is what you call to say that you
6331 * have focus, and you want your parent to look for the next one.
6332 *
6333 * @return Whether this view actually took accessibility focus.
6334 *
6335 * @hide
6336 */
6337 public boolean requestAccessibilityFocus() {
Svetoslav Ganov07b726c2012-04-30 12:24:57 -07006338 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
6339 if (!manager.isEnabled() || !manager.isTouchExplorationEnabled()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006340 return false;
6341 }
6342 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6343 return false;
6344 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07006345 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) == 0) {
6346 mPrivateFlags2 |= PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006347 ViewRootImpl viewRootImpl = getViewRootImpl();
6348 if (viewRootImpl != null) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006349 viewRootImpl.setAccessibilityFocus(this, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07006350 }
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -07006351 if (mAttachInfo != null) {
6352 Rect rectangle = mAttachInfo.mTmpInvalRect;
6353 getDrawingRect(rectangle);
6354 requestRectangleOnScreen(rectangle);
6355 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006356 invalidate();
6357 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
6358 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006359 return true;
6360 }
6361 return false;
6362 }
6363
6364 /**
6365 * Call this to try to clear accessibility focus of this view.
6366 *
6367 * See also {@link #focusSearch(int)}, which is what you call to say that you
6368 * have focus, and you want your parent to look for the next one.
6369 *
6370 * @hide
6371 */
6372 public void clearAccessibilityFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006373 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0) {
6374 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006375 invalidate();
6376 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
6377 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006378 }
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006379 // Clear the global reference of accessibility focus if this
6380 // view or any of its descendants had accessibility focus.
6381 ViewRootImpl viewRootImpl = getViewRootImpl();
6382 if (viewRootImpl != null) {
6383 View focusHost = viewRootImpl.getAccessibilityFocusedHost();
6384 if (focusHost != null && ViewRootImpl.isViewDescendantOf(focusHost, this)) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006385 viewRootImpl.setAccessibilityFocus(null, null);
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006386 }
6387 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006388 }
6389
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07006390 private void sendAccessibilityHoverEvent(int eventType) {
6391 // Since we are not delivering to a client accessibility events from not
6392 // important views (unless the clinet request that) we need to fire the
6393 // event from the deepest view exposed to the client. As a consequence if
6394 // the user crosses a not exposed view the client will see enter and exit
6395 // of the exposed predecessor followed by and enter and exit of that same
6396 // predecessor when entering and exiting the not exposed descendant. This
6397 // is fine since the client has a clear idea which view is hovered at the
6398 // price of a couple more events being sent. This is a simple and
6399 // working solution.
6400 View source = this;
6401 while (true) {
6402 if (source.includeForAccessibility()) {
6403 source.sendAccessibilityEvent(eventType);
6404 return;
6405 }
6406 ViewParent parent = source.getParent();
6407 if (parent instanceof View) {
6408 source = (View) parent;
6409 } else {
6410 return;
6411 }
6412 }
6413 }
6414
Svetoslav Ganov42138042012-03-20 11:51:39 -07006415 /**
6416 * Clears accessibility focus without calling any callback methods
6417 * normally invoked in {@link #clearAccessibilityFocus()}. This method
6418 * is used for clearing accessibility focus when giving this focus to
6419 * another view.
6420 */
6421 void clearAccessibilityFocusNoCallbacks() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006422 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0) {
6423 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006424 invalidate();
6425 }
6426 }
6427
6428 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006429 * Call this to try to give focus to a specific view or to one of its
6430 * descendants.
6431 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006432 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6433 * false), or if it is focusable and it is not focusable in touch mode
6434 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006435 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006436 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006437 * have focus, and you want your parent to look for the next one.
6438 *
6439 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
6440 * {@link #FOCUS_DOWN} and <code>null</code>.
6441 *
6442 * @return Whether this view or one of its descendants actually took focus.
6443 */
6444 public final boolean requestFocus() {
6445 return requestFocus(View.FOCUS_DOWN);
6446 }
6447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006448 /**
6449 * Call this to try to give focus to a specific view or to one of its
6450 * descendants and give it a hint about what direction focus is heading.
6451 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006452 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6453 * false), or if it is focusable and it is not focusable in touch mode
6454 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006455 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006456 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006457 * have focus, and you want your parent to look for the next one.
6458 *
6459 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
6460 * <code>null</code> set for the previously focused rectangle.
6461 *
6462 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6463 * @return Whether this view or one of its descendants actually took focus.
6464 */
6465 public final boolean requestFocus(int direction) {
6466 return requestFocus(direction, null);
6467 }
6468
6469 /**
6470 * Call this to try to give focus to a specific view or to one of its descendants
6471 * and give it hints about the direction and a specific rectangle that the focus
6472 * is coming from. The rectangle can help give larger views a finer grained hint
6473 * about where focus is coming from, and therefore, where to show selection, or
6474 * forward focus change internally.
6475 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006476 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6477 * false), or if it is focusable and it is not focusable in touch mode
6478 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006479 *
6480 * A View will not take focus if it is not visible.
6481 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006482 * A View will not take focus if one of its parents has
6483 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
6484 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006485 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006486 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006487 * have focus, and you want your parent to look for the next one.
6488 *
6489 * You may wish to override this method if your custom {@link View} has an internal
6490 * {@link View} that it wishes to forward the request to.
6491 *
6492 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6493 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
6494 * to give a finer grained hint about where focus is coming from. May be null
6495 * if there is no hint.
6496 * @return Whether this view or one of its descendants actually took focus.
6497 */
6498 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006499 return requestFocusNoSearch(direction, previouslyFocusedRect);
6500 }
6501
6502 private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006503 // need to be focusable
6504 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
6505 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6506 return false;
6507 }
6508
6509 // need to be focusable in touch mode if in touch mode
6510 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006511 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
6512 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006513 }
6514
6515 // need to not have any parents blocking us
6516 if (hasAncestorThatBlocksDescendantFocus()) {
6517 return false;
6518 }
6519
6520 handleFocusGainInternal(direction, previouslyFocusedRect);
6521 return true;
6522 }
6523
6524 /**
6525 * Call this to try to give focus to a specific view or to one of its descendants. This is a
6526 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
6527 * touch mode to request focus when they are touched.
6528 *
6529 * @return Whether this view or one of its descendants actually took focus.
6530 *
6531 * @see #isInTouchMode()
6532 *
6533 */
6534 public final boolean requestFocusFromTouch() {
6535 // Leave touch mode if we need to
6536 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006537 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07006538 if (viewRoot != null) {
6539 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006540 }
6541 }
6542 return requestFocus(View.FOCUS_DOWN);
6543 }
6544
6545 /**
6546 * @return Whether any ancestor of this view blocks descendant focus.
6547 */
6548 private boolean hasAncestorThatBlocksDescendantFocus() {
6549 ViewParent ancestor = mParent;
6550 while (ancestor instanceof ViewGroup) {
6551 final ViewGroup vgAncestor = (ViewGroup) ancestor;
6552 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
6553 return true;
6554 } else {
6555 ancestor = vgAncestor.getParent();
6556 }
6557 }
6558 return false;
6559 }
6560
6561 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006562 * Gets the mode for determining whether this View is important for accessibility
6563 * which is if it fires accessibility events and if it is reported to
6564 * accessibility services that query the screen.
6565 *
6566 * @return The mode for determining whether a View is important for accessibility.
6567 *
6568 * @attr ref android.R.styleable#View_importantForAccessibility
6569 *
6570 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6571 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6572 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6573 */
6574 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07006575 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_AUTO, to = "auto"),
6576 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_YES, to = "yes"),
6577 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_NO, to = "no")
Svetoslav Ganov42138042012-03-20 11:51:39 -07006578 })
6579 public int getImportantForAccessibility() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006580 return (mPrivateFlags2 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK)
6581 >> PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006582 }
6583
6584 /**
6585 * Sets how to determine whether this view is important for accessibility
6586 * which is if it fires accessibility events and if it is reported to
6587 * accessibility services that query the screen.
6588 *
6589 * @param mode How to determine whether this view is important for accessibility.
6590 *
6591 * @attr ref android.R.styleable#View_importantForAccessibility
6592 *
6593 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6594 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6595 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6596 */
6597 public void setImportantForAccessibility(int mode) {
6598 if (mode != getImportantForAccessibility()) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006599 mPrivateFlags2 &= ~PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK;
6600 mPrivateFlags2 |= (mode << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT)
6601 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006602 notifyAccessibilityStateChanged();
6603 }
6604 }
6605
6606 /**
6607 * Gets whether this view should be exposed for accessibility.
6608 *
6609 * @return Whether the view is exposed for accessibility.
6610 *
6611 * @hide
6612 */
6613 public boolean isImportantForAccessibility() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006614 final int mode = (mPrivateFlags2 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK)
6615 >> PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006616 switch (mode) {
6617 case IMPORTANT_FOR_ACCESSIBILITY_YES:
6618 return true;
6619 case IMPORTANT_FOR_ACCESSIBILITY_NO:
6620 return false;
6621 case IMPORTANT_FOR_ACCESSIBILITY_AUTO:
Svetoslav Ganov34caec92012-07-19 18:07:58 -07006622 return isActionableForAccessibility() || hasListenersForAccessibility()
6623 || getAccessibilityNodeProvider() != null;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006624 default:
6625 throw new IllegalArgumentException("Unknow important for accessibility mode: "
6626 + mode);
6627 }
6628 }
6629
6630 /**
6631 * Gets the parent for accessibility purposes. Note that the parent for
6632 * accessibility is not necessary the immediate parent. It is the first
6633 * predecessor that is important for accessibility.
6634 *
6635 * @return The parent for accessibility purposes.
6636 */
6637 public ViewParent getParentForAccessibility() {
6638 if (mParent instanceof View) {
6639 View parentView = (View) mParent;
6640 if (parentView.includeForAccessibility()) {
6641 return mParent;
6642 } else {
6643 return mParent.getParentForAccessibility();
6644 }
6645 }
6646 return null;
6647 }
6648
6649 /**
6650 * Adds the children of a given View for accessibility. Since some Views are
6651 * not important for accessibility the children for accessibility are not
6652 * necessarily direct children of the riew, rather they are the first level of
6653 * descendants important for accessibility.
6654 *
6655 * @param children The list of children for accessibility.
6656 */
6657 public void addChildrenForAccessibility(ArrayList<View> children) {
6658 if (includeForAccessibility()) {
6659 children.add(this);
6660 }
6661 }
6662
6663 /**
6664 * Whether to regard this view for accessibility. A view is regarded for
6665 * accessibility if it is important for accessibility or the querying
6666 * accessibility service has explicitly requested that view not
6667 * important for accessibility are regarded.
6668 *
6669 * @return Whether to regard the view for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006670 *
6671 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006672 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006673 public boolean includeForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006674 if (mAttachInfo != null) {
Romain Guyf0af1d52012-07-11 18:31:21 -07006675 return mAttachInfo.mIncludeNotImportantViews || isImportantForAccessibility();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006676 }
6677 return false;
6678 }
6679
6680 /**
6681 * Returns whether the View is considered actionable from
6682 * accessibility perspective. Such view are important for
6683 * accessiiblity.
6684 *
6685 * @return True if the view is actionable for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006686 *
6687 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006688 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006689 public boolean isActionableForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006690 return (isClickable() || isLongClickable() || isFocusable());
6691 }
6692
6693 /**
6694 * Returns whether the View has registered callbacks wich makes it
6695 * important for accessiiblity.
6696 *
6697 * @return True if the view is actionable for accessibility.
6698 */
6699 private boolean hasListenersForAccessibility() {
6700 ListenerInfo info = getListenerInfo();
6701 return mTouchDelegate != null || info.mOnKeyListener != null
6702 || info.mOnTouchListener != null || info.mOnGenericMotionListener != null
6703 || info.mOnHoverListener != null || info.mOnDragListener != null;
6704 }
6705
6706 /**
6707 * Notifies accessibility services that some view's important for
6708 * accessibility state has changed. Note that such notifications
6709 * are made at most once every
6710 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}
6711 * to avoid unnecessary load to the system. Also once a view has
6712 * made a notifucation this method is a NOP until the notification has
6713 * been sent to clients.
6714 *
6715 * @hide
6716 *
6717 * TODO: Makse sure this method is called for any view state change
6718 * that is interesting for accessilility purposes.
6719 */
6720 public void notifyAccessibilityStateChanged() {
Svetoslav Ganovc406be92012-05-11 16:12:32 -07006721 if (!AccessibilityManager.getInstance(mContext).isEnabled()) {
6722 return;
6723 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07006724 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_STATE_CHANGED) == 0) {
6725 mPrivateFlags2 |= PFLAG2_ACCESSIBILITY_STATE_CHANGED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006726 if (mParent != null) {
6727 mParent.childAccessibilityStateChanged(this);
6728 }
6729 }
6730 }
6731
6732 /**
6733 * Reset the state indicating the this view has requested clients
6734 * interested in its accessiblity state to be notified.
6735 *
6736 * @hide
6737 */
6738 public void resetAccessibilityStateChanged() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006739 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_STATE_CHANGED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006740 }
6741
6742 /**
6743 * Performs the specified accessibility action on the view. For
6744 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
alanv8eeefef2012-05-07 16:57:53 -07006745 * <p>
6746 * If an {@link AccessibilityDelegate} has been specified via calling
6747 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
6748 * {@link AccessibilityDelegate#performAccessibilityAction(View, int, Bundle)}
6749 * is responsible for handling this call.
6750 * </p>
Svetoslav Ganov42138042012-03-20 11:51:39 -07006751 *
6752 * @param action The action to perform.
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006753 * @param arguments Optional action arguments.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006754 * @return Whether the action was performed.
6755 */
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006756 public boolean performAccessibilityAction(int action, Bundle arguments) {
alanv8eeefef2012-05-07 16:57:53 -07006757 if (mAccessibilityDelegate != null) {
6758 return mAccessibilityDelegate.performAccessibilityAction(this, action, arguments);
6759 } else {
6760 return performAccessibilityActionInternal(action, arguments);
6761 }
6762 }
6763
6764 /**
6765 * @see #performAccessibilityAction(int, Bundle)
6766 *
6767 * Note: Called from the default {@link AccessibilityDelegate}.
6768 */
6769 boolean performAccessibilityActionInternal(int action, Bundle arguments) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006770 switch (action) {
6771 case AccessibilityNodeInfo.ACTION_CLICK: {
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006772 if (isClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006773 return performClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006774 }
6775 } break;
6776 case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
6777 if (isLongClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006778 return performLongClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006779 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006780 } break;
6781 case AccessibilityNodeInfo.ACTION_FOCUS: {
6782 if (!hasFocus()) {
6783 // Get out of touch mode since accessibility
6784 // wants to move focus around.
6785 getViewRootImpl().ensureTouchMode(false);
6786 return requestFocus();
6787 }
6788 } break;
6789 case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS: {
6790 if (hasFocus()) {
6791 clearFocus();
6792 return !isFocused();
6793 }
6794 } break;
6795 case AccessibilityNodeInfo.ACTION_SELECT: {
6796 if (!isSelected()) {
6797 setSelected(true);
6798 return isSelected();
6799 }
6800 } break;
6801 case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
6802 if (isSelected()) {
6803 setSelected(false);
6804 return !isSelected();
6805 }
6806 } break;
6807 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07006808 if (!isAccessibilityFocused()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006809 return requestAccessibilityFocus();
6810 }
6811 } break;
6812 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
6813 if (isAccessibilityFocused()) {
6814 clearAccessibilityFocus();
6815 return true;
6816 }
6817 } break;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006818 case AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY: {
6819 if (arguments != null) {
6820 final int granularity = arguments.getInt(
6821 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6822 return nextAtGranularity(granularity);
6823 }
6824 } break;
6825 case AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY: {
6826 if (arguments != null) {
6827 final int granularity = arguments.getInt(
6828 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6829 return previousAtGranularity(granularity);
6830 }
6831 } break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006832 }
6833 return false;
6834 }
6835
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006836 private boolean nextAtGranularity(int granularity) {
6837 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006838 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006839 return false;
6840 }
6841 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6842 if (iterator == null) {
6843 return false;
6844 }
6845 final int current = getAccessibilityCursorPosition();
6846 final int[] range = iterator.following(current);
6847 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006848 return false;
6849 }
6850 final int start = range[0];
6851 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006852 setAccessibilityCursorPosition(end);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006853 sendViewTextTraversedAtGranularityEvent(
6854 AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY,
6855 granularity, start, end);
6856 return true;
6857 }
6858
6859 private boolean previousAtGranularity(int granularity) {
6860 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006861 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006862 return false;
6863 }
6864 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6865 if (iterator == null) {
6866 return false;
6867 }
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006868 int current = getAccessibilityCursorPosition();
6869 if (current == ACCESSIBILITY_CURSOR_POSITION_UNDEFINED) {
6870 current = text.length();
6871 } else if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
6872 // When traversing by character we always put the cursor after the character
6873 // to ease edit and have to compensate before asking the for previous segment.
6874 current--;
6875 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006876 final int[] range = iterator.preceding(current);
6877 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006878 return false;
6879 }
6880 final int start = range[0];
6881 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006882 // Always put the cursor after the character to ease edit.
6883 if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
6884 setAccessibilityCursorPosition(end);
6885 } else {
6886 setAccessibilityCursorPosition(start);
6887 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006888 sendViewTextTraversedAtGranularityEvent(
6889 AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
6890 granularity, start, end);
6891 return true;
6892 }
6893
6894 /**
6895 * Gets the text reported for accessibility purposes.
6896 *
6897 * @return The accessibility text.
6898 *
6899 * @hide
6900 */
6901 public CharSequence getIterableTextForAccessibility() {
Svetoslav Ganov05282aa2012-09-06 18:59:29 -07006902 return getContentDescription();
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006903 }
6904
6905 /**
6906 * @hide
6907 */
6908 public int getAccessibilityCursorPosition() {
6909 return mAccessibilityCursorPosition;
6910 }
6911
6912 /**
6913 * @hide
6914 */
6915 public void setAccessibilityCursorPosition(int position) {
6916 mAccessibilityCursorPosition = position;
6917 }
6918
6919 private void sendViewTextTraversedAtGranularityEvent(int action, int granularity,
6920 int fromIndex, int toIndex) {
6921 if (mParent == null) {
6922 return;
6923 }
6924 AccessibilityEvent event = AccessibilityEvent.obtain(
6925 AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY);
6926 onInitializeAccessibilityEvent(event);
6927 onPopulateAccessibilityEvent(event);
6928 event.setFromIndex(fromIndex);
6929 event.setToIndex(toIndex);
6930 event.setAction(action);
6931 event.setMovementGranularity(granularity);
6932 mParent.requestSendAccessibilityEvent(this, event);
6933 }
6934
6935 /**
6936 * @hide
6937 */
6938 public TextSegmentIterator getIteratorForGranularity(int granularity) {
6939 switch (granularity) {
6940 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER: {
6941 CharSequence text = getIterableTextForAccessibility();
6942 if (text != null && text.length() > 0) {
6943 CharacterTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07006944 CharacterTextSegmentIterator.getInstance(
6945 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006946 iterator.initialize(text.toString());
6947 return iterator;
6948 }
6949 } break;
6950 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD: {
6951 CharSequence text = getIterableTextForAccessibility();
6952 if (text != null && text.length() > 0) {
6953 WordTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07006954 WordTextSegmentIterator.getInstance(
6955 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006956 iterator.initialize(text.toString());
6957 return iterator;
6958 }
6959 } break;
6960 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH: {
6961 CharSequence text = getIterableTextForAccessibility();
6962 if (text != null && text.length() > 0) {
6963 ParagraphTextSegmentIterator iterator =
6964 ParagraphTextSegmentIterator.getInstance();
6965 iterator.initialize(text.toString());
6966 return iterator;
6967 }
6968 } break;
6969 }
6970 return null;
6971 }
6972
Svetoslav Ganov42138042012-03-20 11:51:39 -07006973 /**
Romain Guya440b002010-02-24 15:57:54 -08006974 * @hide
6975 */
6976 public void dispatchStartTemporaryDetach() {
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07006977 clearAccessibilityFocus();
Romain Guy38c2ece2012-05-24 14:20:56 -07006978 clearDisplayList();
6979
Romain Guya440b002010-02-24 15:57:54 -08006980 onStartTemporaryDetach();
6981 }
6982
6983 /**
6984 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006985 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
6986 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08006987 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006988 */
6989 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08006990 removeUnsetPressCallback();
Dianne Hackborn4702a852012-08-17 15:18:29 -07006991 mPrivateFlags |= PFLAG_CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08006992 }
6993
6994 /**
6995 * @hide
6996 */
6997 public void dispatchFinishTemporaryDetach() {
6998 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006999 }
Romain Guy8506ab42009-06-11 17:35:47 -07007000
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007001 /**
7002 * Called after {@link #onStartTemporaryDetach} when the container is done
7003 * changing the view.
7004 */
7005 public void onFinishTemporaryDetach() {
7006 }
Romain Guy8506ab42009-06-11 17:35:47 -07007007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007008 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007009 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
7010 * for this view's window. Returns null if the view is not currently attached
7011 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07007012 * just use the standard high-level event callbacks like
7013 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007014 */
7015 public KeyEvent.DispatcherState getKeyDispatcherState() {
7016 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
7017 }
Joe Malin32736f02011-01-19 16:14:20 -08007018
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007019 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007020 * Dispatch a key event before it is processed by any input method
7021 * associated with the view hierarchy. This can be used to intercept
7022 * key events in special situations before the IME consumes them; a
7023 * typical example would be handling the BACK key to update the application's
7024 * UI instead of allowing the IME to see it and close itself.
7025 *
7026 * @param event The key event to be dispatched.
7027 * @return True if the event was handled, false otherwise.
7028 */
7029 public boolean dispatchKeyEventPreIme(KeyEvent event) {
7030 return onKeyPreIme(event.getKeyCode(), event);
7031 }
7032
7033 /**
7034 * Dispatch a key event to the next view on the focus path. This path runs
7035 * from the top of the view tree down to the currently focused view. If this
7036 * view has focus, it will dispatch to itself. Otherwise it will dispatch
7037 * the next node down the focus path. This method also fires any key
7038 * listeners.
7039 *
7040 * @param event The key event to be dispatched.
7041 * @return True if the event was handled, false otherwise.
7042 */
7043 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007044 if (mInputEventConsistencyVerifier != null) {
7045 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
7046 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007047
Jeff Brown21bc5c92011-02-28 18:27:14 -08007048 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07007049 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007050 ListenerInfo li = mListenerInfo;
7051 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
7052 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007053 return true;
7054 }
7055
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007056 if (event.dispatch(this, mAttachInfo != null
7057 ? mAttachInfo.mKeyDispatchState : null, this)) {
7058 return true;
7059 }
7060
7061 if (mInputEventConsistencyVerifier != null) {
7062 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7063 }
7064 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007065 }
7066
7067 /**
7068 * Dispatches a key shortcut event.
7069 *
7070 * @param event The key event to be dispatched.
7071 * @return True if the event was handled by the view, false otherwise.
7072 */
7073 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
7074 return onKeyShortcut(event.getKeyCode(), event);
7075 }
7076
7077 /**
7078 * Pass the touch screen motion event down to the target view, or this
7079 * view if it is the target.
7080 *
7081 * @param event The motion event to be dispatched.
7082 * @return True if the event was handled by the view, false otherwise.
7083 */
7084 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007085 if (mInputEventConsistencyVerifier != null) {
7086 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
7087 }
7088
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007089 if (onFilterTouchEventForSecurity(event)) {
7090 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007091 ListenerInfo li = mListenerInfo;
7092 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
7093 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007094 return true;
7095 }
7096
7097 if (onTouchEvent(event)) {
7098 return true;
7099 }
Jeff Brown85a31762010-09-01 17:01:00 -07007100 }
7101
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007102 if (mInputEventConsistencyVerifier != null) {
7103 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007104 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007105 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007106 }
7107
7108 /**
Jeff Brown85a31762010-09-01 17:01:00 -07007109 * Filter the touch event to apply security policies.
7110 *
7111 * @param event The motion event to be filtered.
7112 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08007113 *
Jeff Brown85a31762010-09-01 17:01:00 -07007114 * @see #getFilterTouchesWhenObscured
7115 */
7116 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07007117 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07007118 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
7119 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
7120 // Window is obscured, drop this touch.
7121 return false;
7122 }
7123 return true;
7124 }
7125
7126 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007127 * Pass a trackball motion event down to the focused view.
7128 *
7129 * @param event The motion event to be dispatched.
7130 * @return True if the event was handled by the view, false otherwise.
7131 */
7132 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007133 if (mInputEventConsistencyVerifier != null) {
7134 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
7135 }
7136
Romain Guy02ccac62011-06-24 13:20:23 -07007137 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007138 }
7139
7140 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007141 * Dispatch a generic motion event.
7142 * <p>
7143 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7144 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08007145 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07007146 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007147 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08007148 *
7149 * @param event The motion event to be dispatched.
7150 * @return True if the event was handled by the view, false otherwise.
7151 */
7152 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007153 if (mInputEventConsistencyVerifier != null) {
7154 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
7155 }
7156
Jeff Browna032cc02011-03-07 16:56:21 -08007157 final int source = event.getSource();
7158 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
7159 final int action = event.getAction();
7160 if (action == MotionEvent.ACTION_HOVER_ENTER
7161 || action == MotionEvent.ACTION_HOVER_MOVE
7162 || action == MotionEvent.ACTION_HOVER_EXIT) {
7163 if (dispatchHoverEvent(event)) {
7164 return true;
7165 }
7166 } else if (dispatchGenericPointerEvent(event)) {
7167 return true;
7168 }
7169 } else if (dispatchGenericFocusedEvent(event)) {
7170 return true;
7171 }
7172
Jeff Brown10b62902011-06-20 16:40:37 -07007173 if (dispatchGenericMotionEventInternal(event)) {
7174 return true;
7175 }
7176
7177 if (mInputEventConsistencyVerifier != null) {
7178 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7179 }
7180 return false;
7181 }
7182
7183 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07007184 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007185 ListenerInfo li = mListenerInfo;
7186 if (li != null && li.mOnGenericMotionListener != null
7187 && (mViewFlags & ENABLED_MASK) == ENABLED
7188 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007189 return true;
7190 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007191
7192 if (onGenericMotionEvent(event)) {
7193 return true;
7194 }
7195
7196 if (mInputEventConsistencyVerifier != null) {
7197 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7198 }
7199 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08007200 }
7201
7202 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007203 * Dispatch a hover event.
7204 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007205 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07007206 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007207 * </p>
7208 *
7209 * @param event The motion event to be dispatched.
7210 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007211 */
7212 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07007213 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007214 ListenerInfo li = mListenerInfo;
7215 if (li != null && li.mOnHoverListener != null
7216 && (mViewFlags & ENABLED_MASK) == ENABLED
7217 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07007218 return true;
7219 }
7220
Jeff Browna032cc02011-03-07 16:56:21 -08007221 return onHoverEvent(event);
7222 }
7223
7224 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007225 * Returns true if the view has a child to which it has recently sent
7226 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
7227 * it does not have a hovered child, then it must be the innermost hovered view.
7228 * @hide
7229 */
7230 protected boolean hasHoveredChild() {
7231 return false;
7232 }
7233
7234 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007235 * Dispatch a generic motion event to the view under the first pointer.
7236 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007237 * Do not call this method directly.
7238 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007239 * </p>
7240 *
7241 * @param event The motion event to be dispatched.
7242 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007243 */
7244 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
7245 return false;
7246 }
7247
7248 /**
7249 * Dispatch a generic motion event to the currently focused view.
7250 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007251 * Do not call this method directly.
7252 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007253 * </p>
7254 *
7255 * @param event The motion event to be dispatched.
7256 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007257 */
7258 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
7259 return false;
7260 }
7261
7262 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007263 * Dispatch a pointer event.
7264 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007265 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
7266 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
7267 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08007268 * and should not be expected to handle other pointing device features.
7269 * </p>
7270 *
7271 * @param event The motion event to be dispatched.
7272 * @return True if the event was handled by the view, false otherwise.
7273 * @hide
7274 */
7275 public final boolean dispatchPointerEvent(MotionEvent event) {
7276 if (event.isTouchEvent()) {
7277 return dispatchTouchEvent(event);
7278 } else {
7279 return dispatchGenericMotionEvent(event);
7280 }
7281 }
7282
7283 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007284 * Called when the window containing this view gains or loses window focus.
7285 * ViewGroups should override to route to their children.
7286 *
7287 * @param hasFocus True if the window containing this view now has focus,
7288 * false otherwise.
7289 */
7290 public void dispatchWindowFocusChanged(boolean hasFocus) {
7291 onWindowFocusChanged(hasFocus);
7292 }
7293
7294 /**
7295 * Called when the window containing this view gains or loses focus. Note
7296 * that this is separate from view focus: to receive key events, both
7297 * your view and its window must have focus. If a window is displayed
7298 * on top of yours that takes input focus, then your own window will lose
7299 * focus but the view focus will remain unchanged.
7300 *
7301 * @param hasWindowFocus True if the window containing this view now has
7302 * focus, false otherwise.
7303 */
7304 public void onWindowFocusChanged(boolean hasWindowFocus) {
7305 InputMethodManager imm = InputMethodManager.peekInstance();
7306 if (!hasWindowFocus) {
7307 if (isPressed()) {
7308 setPressed(false);
7309 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07007310 if (imm != null && (mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007311 imm.focusOut(this);
7312 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007313 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08007314 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07007315 onFocusLost();
Dianne Hackborn4702a852012-08-17 15:18:29 -07007316 } else if (imm != null && (mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007317 imm.focusIn(this);
7318 }
7319 refreshDrawableState();
7320 }
7321
7322 /**
7323 * Returns true if this view is in a window that currently has window focus.
7324 * Note that this is not the same as the view itself having focus.
7325 *
7326 * @return True if this view is in a window that currently has window focus.
7327 */
7328 public boolean hasWindowFocus() {
7329 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
7330 }
7331
7332 /**
Adam Powell326d8082009-12-09 15:10:07 -08007333 * Dispatch a view visibility change down the view hierarchy.
7334 * ViewGroups should override to route to their children.
7335 * @param changedView The view whose visibility changed. Could be 'this' or
7336 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007337 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7338 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007339 */
7340 protected void dispatchVisibilityChanged(View changedView, int visibility) {
7341 onVisibilityChanged(changedView, visibility);
7342 }
7343
7344 /**
7345 * Called when the visibility of the view or an ancestor of the view is changed.
7346 * @param changedView The view whose visibility changed. Could be 'this' or
7347 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007348 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7349 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007350 */
7351 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007352 if (visibility == VISIBLE) {
7353 if (mAttachInfo != null) {
7354 initialAwakenScrollBars();
7355 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07007356 mPrivateFlags |= PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH;
Adam Powell8568c3a2010-04-19 14:26:11 -07007357 }
7358 }
Adam Powell326d8082009-12-09 15:10:07 -08007359 }
7360
7361 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08007362 * Dispatch a hint about whether this view is displayed. For instance, when
7363 * a View moves out of the screen, it might receives a display hint indicating
7364 * the view is not displayed. Applications should not <em>rely</em> on this hint
7365 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007366 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007367 * @param hint A hint about whether or not this view is displayed:
7368 * {@link #VISIBLE} or {@link #INVISIBLE}.
7369 */
7370 public void dispatchDisplayHint(int hint) {
7371 onDisplayHint(hint);
7372 }
7373
7374 /**
7375 * Gives this view a hint about whether is displayed or not. For instance, when
7376 * a View moves out of the screen, it might receives a display hint indicating
7377 * the view is not displayed. Applications should not <em>rely</em> on this hint
7378 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007379 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007380 * @param hint A hint about whether or not this view is displayed:
7381 * {@link #VISIBLE} or {@link #INVISIBLE}.
7382 */
7383 protected void onDisplayHint(int hint) {
7384 }
7385
7386 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007387 * Dispatch a window visibility change down the view hierarchy.
7388 * ViewGroups should override to route to their children.
7389 *
7390 * @param visibility The new visibility of the window.
7391 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007392 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007393 */
7394 public void dispatchWindowVisibilityChanged(int visibility) {
7395 onWindowVisibilityChanged(visibility);
7396 }
7397
7398 /**
7399 * Called when the window containing has change its visibility
7400 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
7401 * that this tells you whether or not your window is being made visible
7402 * to the window manager; this does <em>not</em> tell you whether or not
7403 * your window is obscured by other windows on the screen, even if it
7404 * is itself visible.
7405 *
7406 * @param visibility The new visibility of the window.
7407 */
7408 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007409 if (visibility == VISIBLE) {
7410 initialAwakenScrollBars();
7411 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007412 }
7413
7414 /**
7415 * Returns the current visibility of the window this view is attached to
7416 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
7417 *
7418 * @return Returns the current visibility of the view's window.
7419 */
7420 public int getWindowVisibility() {
7421 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
7422 }
7423
7424 /**
7425 * Retrieve the overall visible display size in which the window this view is
7426 * attached to has been positioned in. This takes into account screen
7427 * decorations above the window, for both cases where the window itself
7428 * is being position inside of them or the window is being placed under
7429 * then and covered insets are used for the window to position its content
7430 * inside. In effect, this tells you the available area where content can
7431 * be placed and remain visible to users.
7432 *
7433 * <p>This function requires an IPC back to the window manager to retrieve
7434 * the requested information, so should not be used in performance critical
7435 * code like drawing.
7436 *
7437 * @param outRect Filled in with the visible display frame. If the view
7438 * is not attached to a window, this is simply the raw display size.
7439 */
7440 public void getWindowVisibleDisplayFrame(Rect outRect) {
7441 if (mAttachInfo != null) {
7442 try {
7443 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
7444 } catch (RemoteException e) {
7445 return;
7446 }
7447 // XXX This is really broken, and probably all needs to be done
7448 // in the window manager, and we need to know more about whether
7449 // we want the area behind or in front of the IME.
7450 final Rect insets = mAttachInfo.mVisibleInsets;
7451 outRect.left += insets.left;
7452 outRect.top += insets.top;
7453 outRect.right -= insets.right;
7454 outRect.bottom -= insets.bottom;
7455 return;
7456 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07007457 // The view is not attached to a display so we don't have a context.
7458 // Make a best guess about the display size.
7459 Display d = DisplayManagerGlobal.getInstance().getRealDisplay(Display.DEFAULT_DISPLAY);
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07007460 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007461 }
7462
7463 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007464 * Dispatch a notification about a resource configuration change down
7465 * the view hierarchy.
7466 * ViewGroups should override to route to their children.
7467 *
7468 * @param newConfig The new resource configuration.
7469 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007470 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007471 */
7472 public void dispatchConfigurationChanged(Configuration newConfig) {
7473 onConfigurationChanged(newConfig);
7474 }
7475
7476 /**
7477 * Called when the current configuration of the resources being used
7478 * by the application have changed. You can use this to decide when
7479 * to reload resources that can changed based on orientation and other
7480 * configuration characterstics. You only need to use this if you are
7481 * not relying on the normal {@link android.app.Activity} mechanism of
7482 * recreating the activity instance upon a configuration change.
7483 *
7484 * @param newConfig The new resource configuration.
7485 */
7486 protected void onConfigurationChanged(Configuration newConfig) {
7487 }
7488
7489 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007490 * Private function to aggregate all per-view attributes in to the view
7491 * root.
7492 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007493 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7494 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007495 }
7496
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007497 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7498 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08007499 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007500 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007501 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007502 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007503 ListenerInfo li = mListenerInfo;
7504 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007505 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007506 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007507 }
7508 }
7509
7510 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08007511 final AttachInfo ai = mAttachInfo;
Craig Mautner7eac0f52012-09-13 13:14:14 -07007512 if (ai != null && !ai.mRecomputeGlobalAttributes) {
Joe Onorato664644d2011-01-23 17:53:23 -08007513 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
7514 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007515 ai.mRecomputeGlobalAttributes = true;
7516 }
7517 }
7518 }
7519
7520 /**
7521 * Returns whether the device is currently in touch mode. Touch mode is entered
7522 * once the user begins interacting with the device by touch, and affects various
7523 * things like whether focus is always visible to the user.
7524 *
7525 * @return Whether the device is in touch mode.
7526 */
7527 @ViewDebug.ExportedProperty
7528 public boolean isInTouchMode() {
7529 if (mAttachInfo != null) {
7530 return mAttachInfo.mInTouchMode;
7531 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07007532 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007533 }
7534 }
7535
7536 /**
7537 * Returns the context the view is running in, through which it can
7538 * access the current theme, resources, etc.
7539 *
7540 * @return The view's Context.
7541 */
7542 @ViewDebug.CapturedViewProperty
7543 public final Context getContext() {
7544 return mContext;
7545 }
7546
7547 /**
7548 * Handle a key event before it is processed by any input method
7549 * associated with the view hierarchy. This can be used to intercept
7550 * key events in special situations before the IME consumes them; a
7551 * typical example would be handling the BACK key to update the application's
7552 * UI instead of allowing the IME to see it and close itself.
7553 *
7554 * @param keyCode The value in event.getKeyCode().
7555 * @param event Description of the key event.
7556 * @return If you handled the event, return true. If you want to allow the
7557 * event to be handled by the next receiver, return false.
7558 */
7559 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7560 return false;
7561 }
7562
7563 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007564 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7565 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007566 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7567 * is released, if the view is enabled and clickable.
7568 *
Jean Chalard405bc512012-05-29 19:12:34 +09007569 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7570 * although some may elect to do so in some situations. Do not rely on this to
7571 * catch software key presses.
7572 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007573 * @param keyCode A key code that represents the button pressed, from
7574 * {@link android.view.KeyEvent}.
7575 * @param event The KeyEvent object that defines the button action.
7576 */
7577 public boolean onKeyDown(int keyCode, KeyEvent event) {
7578 boolean result = false;
7579
7580 switch (keyCode) {
7581 case KeyEvent.KEYCODE_DPAD_CENTER:
7582 case KeyEvent.KEYCODE_ENTER: {
7583 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7584 return true;
7585 }
7586 // Long clickable items don't necessarily have to be clickable
7587 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7588 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7589 (event.getRepeatCount() == 0)) {
7590 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007591 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007592 return true;
7593 }
7594 break;
7595 }
7596 }
7597 return result;
7598 }
7599
7600 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007601 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7602 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7603 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007604 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7605 * although some may elect to do so in some situations. Do not rely on this to
7606 * catch software key presses.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007607 */
7608 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7609 return false;
7610 }
7611
7612 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007613 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7614 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007615 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7616 * {@link KeyEvent#KEYCODE_ENTER} is released.
Jean Chalard405bc512012-05-29 19:12:34 +09007617 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7618 * although some may elect to do so in some situations. Do not rely on this to
7619 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007620 *
7621 * @param keyCode A key code that represents the button pressed, from
7622 * {@link android.view.KeyEvent}.
7623 * @param event The KeyEvent object that defines the button action.
7624 */
7625 public boolean onKeyUp(int keyCode, KeyEvent event) {
7626 boolean result = false;
7627
7628 switch (keyCode) {
7629 case KeyEvent.KEYCODE_DPAD_CENTER:
7630 case KeyEvent.KEYCODE_ENTER: {
7631 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7632 return true;
7633 }
7634 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7635 setPressed(false);
7636
7637 if (!mHasPerformedLongPress) {
7638 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007639 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007640
7641 result = performClick();
7642 }
7643 }
7644 break;
7645 }
7646 }
7647 return result;
7648 }
7649
7650 /**
7651 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7652 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7653 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007654 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7655 * although some may elect to do so in some situations. Do not rely on this to
7656 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007657 *
7658 * @param keyCode A key code that represents the button pressed, from
7659 * {@link android.view.KeyEvent}.
7660 * @param repeatCount The number of times the action was made.
7661 * @param event The KeyEvent object that defines the button action.
7662 */
7663 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7664 return false;
7665 }
7666
7667 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007668 * Called on the focused view when a key shortcut event is not handled.
7669 * Override this method to implement local key shortcuts for the View.
7670 * Key shortcuts can also be implemented by setting the
7671 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007672 *
7673 * @param keyCode The value in event.getKeyCode().
7674 * @param event Description of the key event.
7675 * @return If you handled the event, return true. If you want to allow the
7676 * event to be handled by the next receiver, return false.
7677 */
7678 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7679 return false;
7680 }
7681
7682 /**
7683 * Check whether the called view is a text editor, in which case it
7684 * would make sense to automatically display a soft input window for
7685 * it. Subclasses should override this if they implement
7686 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007687 * a call on that method would return a non-null InputConnection, and
7688 * they are really a first-class editor that the user would normally
7689 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007690 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007691 * <p>The default implementation always returns false. This does
7692 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7693 * will not be called or the user can not otherwise perform edits on your
7694 * view; it is just a hint to the system that this is not the primary
7695 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007696 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007697 * @return Returns true if this view is a text editor, else false.
7698 */
7699 public boolean onCheckIsTextEditor() {
7700 return false;
7701 }
Romain Guy8506ab42009-06-11 17:35:47 -07007702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007703 /**
7704 * Create a new InputConnection for an InputMethod to interact
7705 * with the view. The default implementation returns null, since it doesn't
7706 * support input methods. You can override this to implement such support.
7707 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007708 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007709 * <p>When implementing this, you probably also want to implement
7710 * {@link #onCheckIsTextEditor()} to indicate you will return a
7711 * non-null InputConnection.
7712 *
7713 * @param outAttrs Fill in with attribute information about the connection.
7714 */
7715 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7716 return null;
7717 }
7718
7719 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007720 * Called by the {@link android.view.inputmethod.InputMethodManager}
7721 * when a view who is not the current
7722 * input connection target is trying to make a call on the manager. The
7723 * default implementation returns false; you can override this to return
7724 * true for certain views if you are performing InputConnection proxying
7725 * to them.
7726 * @param view The View that is making the InputMethodManager call.
7727 * @return Return true to allow the call, false to reject.
7728 */
7729 public boolean checkInputConnectionProxy(View view) {
7730 return false;
7731 }
Romain Guy8506ab42009-06-11 17:35:47 -07007732
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007733 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007734 * Show the context menu for this view. It is not safe to hold on to the
7735 * menu after returning from this method.
7736 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007737 * You should normally not overload this method. Overload
7738 * {@link #onCreateContextMenu(ContextMenu)} or define an
7739 * {@link OnCreateContextMenuListener} to add items to the context menu.
7740 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007741 * @param menu The context menu to populate
7742 */
7743 public void createContextMenu(ContextMenu menu) {
7744 ContextMenuInfo menuInfo = getContextMenuInfo();
7745
7746 // Sets the current menu info so all items added to menu will have
7747 // my extra info set.
7748 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7749
7750 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007751 ListenerInfo li = mListenerInfo;
7752 if (li != null && li.mOnCreateContextMenuListener != null) {
7753 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007754 }
7755
7756 // Clear the extra information so subsequent items that aren't mine don't
7757 // have my extra info.
7758 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7759
7760 if (mParent != null) {
7761 mParent.createContextMenu(menu);
7762 }
7763 }
7764
7765 /**
7766 * Views should implement this if they have extra information to associate
7767 * with the context menu. The return result is supplied as a parameter to
7768 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7769 * callback.
7770 *
7771 * @return Extra information about the item for which the context menu
7772 * should be shown. This information will vary across different
7773 * subclasses of View.
7774 */
7775 protected ContextMenuInfo getContextMenuInfo() {
7776 return null;
7777 }
7778
7779 /**
7780 * Views should implement this if the view itself is going to add items to
7781 * the context menu.
7782 *
7783 * @param menu the context menu to populate
7784 */
7785 protected void onCreateContextMenu(ContextMenu menu) {
7786 }
7787
7788 /**
7789 * Implement this method to handle trackball motion events. The
7790 * <em>relative</em> movement of the trackball since the last event
7791 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
7792 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
7793 * that a movement of 1 corresponds to the user pressing one DPAD key (so
7794 * they will often be fractional values, representing the more fine-grained
7795 * movement information available from a trackball).
7796 *
7797 * @param event The motion event.
7798 * @return True if the event was handled, false otherwise.
7799 */
7800 public boolean onTrackballEvent(MotionEvent event) {
7801 return false;
7802 }
7803
7804 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08007805 * Implement this method to handle generic motion events.
7806 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08007807 * Generic motion events describe joystick movements, mouse hovers, track pad
7808 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08007809 * {@link MotionEvent#getSource() source} of the motion event specifies
7810 * the class of input that was received. Implementations of this method
7811 * must examine the bits in the source before processing the event.
7812 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007813 * </p><p>
7814 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7815 * are delivered to the view under the pointer. All other generic motion events are
7816 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08007817 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07007818 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08007819 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007820 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
7821 * // process the joystick movement...
7822 * return true;
7823 * }
7824 * }
7825 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
7826 * switch (event.getAction()) {
7827 * case MotionEvent.ACTION_HOVER_MOVE:
7828 * // process the mouse hover movement...
7829 * return true;
7830 * case MotionEvent.ACTION_SCROLL:
7831 * // process the scroll wheel movement...
7832 * return true;
7833 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08007834 * }
7835 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07007836 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08007837 *
7838 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08007839 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08007840 */
7841 public boolean onGenericMotionEvent(MotionEvent event) {
7842 return false;
7843 }
7844
7845 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007846 * Implement this method to handle hover events.
7847 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07007848 * This method is called whenever a pointer is hovering into, over, or out of the
7849 * bounds of a view and the view is not currently being touched.
7850 * Hover events are represented as pointer events with action
7851 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
7852 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
7853 * </p>
7854 * <ul>
7855 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
7856 * when the pointer enters the bounds of the view.</li>
7857 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
7858 * when the pointer has already entered the bounds of the view and has moved.</li>
7859 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
7860 * when the pointer has exited the bounds of the view or when the pointer is
7861 * about to go down due to a button click, tap, or similar user action that
7862 * causes the view to be touched.</li>
7863 * </ul>
7864 * <p>
7865 * The view should implement this method to return true to indicate that it is
7866 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08007867 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07007868 * The default implementation calls {@link #setHovered} to update the hovered state
7869 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07007870 * is enabled and is clickable. The default implementation also sends hover
7871 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08007872 * </p>
7873 *
7874 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07007875 * @return True if the view handled the hover event.
7876 *
7877 * @see #isHovered
7878 * @see #setHovered
7879 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007880 */
7881 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007882 // The root view may receive hover (or touch) events that are outside the bounds of
7883 // the window. This code ensures that we only send accessibility events for
7884 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07007885 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007886 if (!mSendingHoverAccessibilityEvents) {
7887 if ((action == MotionEvent.ACTION_HOVER_ENTER
7888 || action == MotionEvent.ACTION_HOVER_MOVE)
7889 && !hasHoveredChild()
7890 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07007891 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007892 mSendingHoverAccessibilityEvents = true;
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007893 }
7894 } else {
7895 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07007896 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007897 && !pointInView(event.getX(), event.getY()))) {
7898 mSendingHoverAccessibilityEvents = false;
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07007899 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007900 // If the window does not have input focus we take away accessibility
7901 // focus as soon as the user stop hovering over the view.
Jeff Brown59a422e2012-04-19 15:19:19 -07007902 if (mAttachInfo != null && !mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07007903 getViewRootImpl().setAccessibilityFocus(null, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007904 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007905 }
Jeff Browna1b24182011-07-28 13:38:24 -07007906 }
7907
Jeff Brown87b7f802011-06-21 18:35:45 -07007908 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007909 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07007910 case MotionEvent.ACTION_HOVER_ENTER:
7911 setHovered(true);
7912 break;
7913 case MotionEvent.ACTION_HOVER_EXIT:
7914 setHovered(false);
7915 break;
7916 }
Jeff Browna1b24182011-07-28 13:38:24 -07007917
7918 // Dispatch the event to onGenericMotionEvent before returning true.
7919 // This is to provide compatibility with existing applications that
7920 // handled HOVER_MOVE events in onGenericMotionEvent and that would
7921 // break because of the new default handling for hoverable views
7922 // in onHoverEvent.
7923 // Note that onGenericMotionEvent will be called by default when
7924 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
7925 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07007926 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08007927 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007928
Svetoslav Ganov736c2752011-04-22 18:30:36 -07007929 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08007930 }
7931
7932 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007933 * Returns true if the view should handle {@link #onHoverEvent}
7934 * by calling {@link #setHovered} to change its hovered state.
7935 *
7936 * @return True if the view is hoverable.
7937 */
7938 private boolean isHoverable() {
7939 final int viewFlags = mViewFlags;
7940 if ((viewFlags & ENABLED_MASK) == DISABLED) {
7941 return false;
7942 }
7943
7944 return (viewFlags & CLICKABLE) == CLICKABLE
7945 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
7946 }
7947
7948 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007949 * Returns true if the view is currently hovered.
7950 *
7951 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007952 *
7953 * @see #setHovered
7954 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007955 */
Jeff Brown10b62902011-06-20 16:40:37 -07007956 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08007957 public boolean isHovered() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07007958 return (mPrivateFlags & PFLAG_HOVERED) != 0;
Jeff Browna032cc02011-03-07 16:56:21 -08007959 }
7960
7961 /**
7962 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007963 * <p>
7964 * Calling this method also changes the drawable state of the view. This
7965 * enables the view to react to hover by using different drawable resources
7966 * to change its appearance.
7967 * </p><p>
7968 * The {@link #onHoverChanged} method is called when the hovered state changes.
7969 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08007970 *
7971 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007972 *
7973 * @see #isHovered
7974 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007975 */
7976 public void setHovered(boolean hovered) {
7977 if (hovered) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07007978 if ((mPrivateFlags & PFLAG_HOVERED) == 0) {
7979 mPrivateFlags |= PFLAG_HOVERED;
Jeff Browna032cc02011-03-07 16:56:21 -08007980 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007981 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08007982 }
7983 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07007984 if ((mPrivateFlags & PFLAG_HOVERED) != 0) {
7985 mPrivateFlags &= ~PFLAG_HOVERED;
Jeff Browna032cc02011-03-07 16:56:21 -08007986 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007987 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08007988 }
7989 }
7990 }
7991
7992 /**
Jeff Brown10b62902011-06-20 16:40:37 -07007993 * Implement this method to handle hover state changes.
7994 * <p>
7995 * This method is called whenever the hover state changes as a result of a
7996 * call to {@link #setHovered}.
7997 * </p>
7998 *
7999 * @param hovered The current hover state, as returned by {@link #isHovered}.
8000 *
8001 * @see #isHovered
8002 * @see #setHovered
8003 */
8004 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07008005 }
8006
8007 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008008 * Implement this method to handle touch screen motion events.
8009 *
8010 * @param event The motion event.
8011 * @return True if the event was handled, false otherwise.
8012 */
8013 public boolean onTouchEvent(MotionEvent event) {
8014 final int viewFlags = mViewFlags;
8015
8016 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008017 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PFLAG_PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08008018 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07008019 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008020 // A disabled view that is clickable still consumes the touch
8021 // events, it just doesn't respond to them.
8022 return (((viewFlags & CLICKABLE) == CLICKABLE ||
8023 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
8024 }
8025
8026 if (mTouchDelegate != null) {
8027 if (mTouchDelegate.onTouchEvent(event)) {
8028 return true;
8029 }
8030 }
8031
8032 if (((viewFlags & CLICKABLE) == CLICKABLE ||
8033 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
8034 switch (event.getAction()) {
8035 case MotionEvent.ACTION_UP:
Dianne Hackborn4702a852012-08-17 15:18:29 -07008036 boolean prepressed = (mPrivateFlags & PFLAG_PREPRESSED) != 0;
8037 if ((mPrivateFlags & PFLAG_PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008038 // take focus if we don't have it already and we should in
8039 // touch mode.
8040 boolean focusTaken = false;
8041 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
8042 focusTaken = requestFocus();
8043 }
8044
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08008045 if (prepressed) {
8046 // The button is being released before we actually
8047 // showed it as pressed. Make it show the pressed
8048 // state now (before scheduling the click) to ensure
8049 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08008050 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08008051 }
Joe Malin32736f02011-01-19 16:14:20 -08008052
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008053 if (!mHasPerformedLongPress) {
8054 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05008055 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008056
8057 // Only perform take click actions if we were in the pressed state
8058 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08008059 // Use a Runnable and post this rather than calling
8060 // performClick directly. This lets other visual state
8061 // of the view update before click actions start.
8062 if (mPerformClick == null) {
8063 mPerformClick = new PerformClick();
8064 }
8065 if (!post(mPerformClick)) {
8066 performClick();
8067 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008068 }
8069 }
8070
8071 if (mUnsetPressedState == null) {
8072 mUnsetPressedState = new UnsetPressedState();
8073 }
8074
Adam Powelle14579b2009-12-16 18:39:52 -08008075 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08008076 postDelayed(mUnsetPressedState,
8077 ViewConfiguration.getPressedStateDuration());
8078 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008079 // If the post failed, unpress right now
8080 mUnsetPressedState.run();
8081 }
Adam Powelle14579b2009-12-16 18:39:52 -08008082 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008083 }
8084 break;
8085
8086 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08008087 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008088
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07008089 if (performButtonActionOnTouchDown(event)) {
8090 break;
8091 }
8092
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008093 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07008094 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008095
8096 // For views inside a scrolling container, delay the pressed feedback for
8097 // a short period in case this is a scroll.
8098 if (isInScrollingContainer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008099 mPrivateFlags |= PFLAG_PREPRESSED;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008100 if (mPendingCheckForTap == null) {
8101 mPendingCheckForTap = new CheckForTap();
8102 }
8103 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
8104 } else {
8105 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08008106 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008107 checkForLongClick(0);
8108 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008109 break;
8110
8111 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08008112 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08008113 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008114 break;
8115
8116 case MotionEvent.ACTION_MOVE:
8117 final int x = (int) event.getX();
8118 final int y = (int) event.getY();
8119
8120 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07008121 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008122 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08008123 removeTapCallback();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008124 if ((mPrivateFlags & PFLAG_PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08008125 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05008126 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008127
Adam Powell4d6f0662012-02-21 15:11:11 -08008128 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008129 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008130 }
8131 break;
8132 }
8133 return true;
8134 }
8135
8136 return false;
8137 }
8138
8139 /**
Adam Powell10298662011-08-14 18:26:30 -07008140 * @hide
8141 */
8142 public boolean isInScrollingContainer() {
8143 ViewParent p = getParent();
8144 while (p != null && p instanceof ViewGroup) {
8145 if (((ViewGroup) p).shouldDelayChildPressedState()) {
8146 return true;
8147 }
8148 p = p.getParent();
8149 }
8150 return false;
8151 }
8152
8153 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05008154 * Remove the longpress detection timer.
8155 */
8156 private void removeLongPressCallback() {
8157 if (mPendingCheckForLongPress != null) {
8158 removeCallbacks(mPendingCheckForLongPress);
8159 }
8160 }
Adam Powell3cb8b632011-01-21 15:34:14 -08008161
8162 /**
8163 * Remove the pending click action
8164 */
8165 private void removePerformClickCallback() {
8166 if (mPerformClick != null) {
8167 removeCallbacks(mPerformClick);
8168 }
8169 }
8170
Adam Powelle14579b2009-12-16 18:39:52 -08008171 /**
Romain Guya440b002010-02-24 15:57:54 -08008172 * Remove the prepress detection timer.
8173 */
8174 private void removeUnsetPressCallback() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008175 if ((mPrivateFlags & PFLAG_PRESSED) != 0 && mUnsetPressedState != null) {
Romain Guya440b002010-02-24 15:57:54 -08008176 setPressed(false);
8177 removeCallbacks(mUnsetPressedState);
8178 }
8179 }
8180
8181 /**
Adam Powelle14579b2009-12-16 18:39:52 -08008182 * Remove the tap detection timer.
8183 */
8184 private void removeTapCallback() {
8185 if (mPendingCheckForTap != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008186 mPrivateFlags &= ~PFLAG_PREPRESSED;
Adam Powelle14579b2009-12-16 18:39:52 -08008187 removeCallbacks(mPendingCheckForTap);
8188 }
8189 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05008190
8191 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008192 * Cancels a pending long press. Your subclass can use this if you
8193 * want the context menu to come up if the user presses and holds
8194 * at the same place, but you don't want it to come up if they press
8195 * and then move around enough to cause scrolling.
8196 */
8197 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05008198 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08008199
8200 /*
8201 * The prepressed state handled by the tap callback is a display
8202 * construct, but the tap callback will post a long press callback
8203 * less its own timeout. Remove it here.
8204 */
8205 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008206 }
8207
8208 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008209 * Remove the pending callback for sending a
8210 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
8211 */
8212 private void removeSendViewScrolledAccessibilityEventCallback() {
8213 if (mSendViewScrolledAccessibilityEvent != null) {
8214 removeCallbacks(mSendViewScrolledAccessibilityEvent);
Svetoslav Ganov4a812ae2012-05-29 16:46:10 -07008215 mSendViewScrolledAccessibilityEvent.mIsPending = false;
Svetoslav Ganova0156172011-06-26 17:55:44 -07008216 }
8217 }
8218
8219 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008220 * Sets the TouchDelegate for this View.
8221 */
8222 public void setTouchDelegate(TouchDelegate delegate) {
8223 mTouchDelegate = delegate;
8224 }
8225
8226 /**
8227 * Gets the TouchDelegate for this View.
8228 */
8229 public TouchDelegate getTouchDelegate() {
8230 return mTouchDelegate;
8231 }
8232
8233 /**
8234 * Set flags controlling behavior of this view.
8235 *
8236 * @param flags Constant indicating the value which should be set
8237 * @param mask Constant indicating the bit range that should be changed
8238 */
8239 void setFlags(int flags, int mask) {
8240 int old = mViewFlags;
8241 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
8242
8243 int changed = mViewFlags ^ old;
8244 if (changed == 0) {
8245 return;
8246 }
8247 int privateFlags = mPrivateFlags;
8248
8249 /* Check if the FOCUSABLE bit has changed */
8250 if (((changed & FOCUSABLE_MASK) != 0) &&
Dianne Hackborn4702a852012-08-17 15:18:29 -07008251 ((privateFlags & PFLAG_HAS_BOUNDS) !=0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008252 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
Dianne Hackborn4702a852012-08-17 15:18:29 -07008253 && ((privateFlags & PFLAG_FOCUSED) != 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008254 /* Give up focus if we are no longer focusable */
8255 clearFocus();
8256 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
Dianne Hackborn4702a852012-08-17 15:18:29 -07008257 && ((privateFlags & PFLAG_FOCUSED) == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008258 /*
8259 * Tell the view system that we are now available to take focus
8260 * if no one else already has it.
8261 */
8262 if (mParent != null) mParent.focusableViewAvailable(this);
8263 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008264 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8265 notifyAccessibilityStateChanged();
8266 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008267 }
8268
8269 if ((flags & VISIBILITY_MASK) == VISIBLE) {
8270 if ((changed & VISIBILITY_MASK) != 0) {
8271 /*
Chet Haase4324ead2011-08-24 21:31:03 -07008272 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07008273 * it was not visible. Marking it drawn ensures that the invalidation will
8274 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008275 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07008276 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07008277 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008278
8279 needGlobalAttributesUpdate(true);
8280
8281 // a view becoming visible is worth notifying the parent
8282 // about in case nothing has focus. even if this specific view
8283 // isn't focusable, it may contain something that is, so let
8284 // the root view try to give this focus if nothing else does.
8285 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
8286 mParent.focusableViewAvailable(this);
8287 }
8288 }
8289 }
8290
8291 /* Check if the GONE bit has changed */
8292 if ((changed & GONE) != 0) {
8293 needGlobalAttributesUpdate(false);
8294 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008295
Romain Guyecd80ee2009-12-03 17:13:02 -08008296 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
8297 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008298 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08008299 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07008300 if (mParent instanceof View) {
8301 // GONE views noop invalidation, so invalidate the parent
8302 ((View) mParent).invalidate(true);
8303 }
8304 // Mark the view drawn to ensure that it gets invalidated properly the next
8305 // time it is visible and gets invalidated
Dianne Hackborn4702a852012-08-17 15:18:29 -07008306 mPrivateFlags |= PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008307 }
8308 if (mAttachInfo != null) {
8309 mAttachInfo.mViewVisibilityChanged = true;
8310 }
8311 }
8312
8313 /* Check if the VISIBLE bit has changed */
8314 if ((changed & INVISIBLE) != 0) {
8315 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07008316 /*
8317 * If this view is becoming invisible, set the DRAWN flag so that
8318 * the next invalidate() will not be skipped.
8319 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07008320 mPrivateFlags |= PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008321
8322 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008323 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008324 if (getRootView() != this) {
8325 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008326 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008327 }
8328 }
8329 if (mAttachInfo != null) {
8330 mAttachInfo.mViewVisibilityChanged = true;
8331 }
8332 }
8333
Adam Powell326d8082009-12-09 15:10:07 -08008334 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07008335 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08008336 ((ViewGroup) mParent).onChildVisibilityChanged(this,
8337 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08008338 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07008339 } else if (mParent != null) {
8340 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07008341 }
Adam Powell326d8082009-12-09 15:10:07 -08008342 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
8343 }
8344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008345 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
8346 destroyDrawingCache();
8347 }
8348
8349 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
8350 destroyDrawingCache();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008351 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008352 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008353 }
8354
8355 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
8356 destroyDrawingCache();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008357 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008358 }
8359
8360 if ((changed & DRAW_MASK) != 0) {
8361 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07008362 if (mBackground != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008363 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
8364 mPrivateFlags |= PFLAG_ONLY_DRAWS_BACKGROUND;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008365 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008366 mPrivateFlags |= PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008367 }
8368 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008369 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008370 }
8371 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08008372 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008373 }
8374
8375 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08008376 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008377 mParent.recomputeViewAttributes(this);
8378 }
8379 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008380
8381 if (AccessibilityManager.getInstance(mContext).isEnabled()
8382 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
8383 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
8384 notifyAccessibilityStateChanged();
8385 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008386 }
8387
8388 /**
8389 * Change the view's z order in the tree, so it's on top of other sibling
8390 * views
8391 */
8392 public void bringToFront() {
8393 if (mParent != null) {
8394 mParent.bringChildToFront(this);
8395 }
8396 }
8397
8398 /**
8399 * This is called in response to an internal scroll in this view (i.e., the
8400 * view scrolled its own contents). This is typically as a result of
8401 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
8402 * called.
8403 *
8404 * @param l Current horizontal scroll origin.
8405 * @param t Current vertical scroll origin.
8406 * @param oldl Previous horizontal scroll origin.
8407 * @param oldt Previous vertical scroll origin.
8408 */
8409 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07008410 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8411 postSendViewScrolledAccessibilityEventCallback();
8412 }
8413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008414 mBackgroundSizeChanged = true;
8415
8416 final AttachInfo ai = mAttachInfo;
8417 if (ai != null) {
8418 ai.mViewScrollChanged = true;
8419 }
8420 }
8421
8422 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008423 * Interface definition for a callback to be invoked when the layout bounds of a view
8424 * changes due to layout processing.
8425 */
8426 public interface OnLayoutChangeListener {
8427 /**
8428 * Called when the focus state of a view has changed.
8429 *
8430 * @param v The view whose state has changed.
8431 * @param left The new value of the view's left property.
8432 * @param top The new value of the view's top property.
8433 * @param right The new value of the view's right property.
8434 * @param bottom The new value of the view's bottom property.
8435 * @param oldLeft The previous value of the view's left property.
8436 * @param oldTop The previous value of the view's top property.
8437 * @param oldRight The previous value of the view's right property.
8438 * @param oldBottom The previous value of the view's bottom property.
8439 */
8440 void onLayoutChange(View v, int left, int top, int right, int bottom,
8441 int oldLeft, int oldTop, int oldRight, int oldBottom);
8442 }
8443
8444 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008445 * This is called during layout when the size of this view has changed. If
8446 * you were just added to the view hierarchy, you're called with the old
8447 * values of 0.
8448 *
8449 * @param w Current width of this view.
8450 * @param h Current height of this view.
8451 * @param oldw Old width of this view.
8452 * @param oldh Old height of this view.
8453 */
8454 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
8455 }
8456
8457 /**
8458 * Called by draw to draw the child views. This may be overridden
8459 * by derived classes to gain control just before its children are drawn
8460 * (but after its own view has been drawn).
8461 * @param canvas the canvas on which to draw the view
8462 */
8463 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008465 }
8466
8467 /**
8468 * Gets the parent of this view. Note that the parent is a
8469 * ViewParent and not necessarily a View.
8470 *
8471 * @return Parent of this view.
8472 */
8473 public final ViewParent getParent() {
8474 return mParent;
8475 }
8476
8477 /**
Chet Haasecca2c982011-05-20 14:34:18 -07008478 * Set the horizontal scrolled position of your view. This will cause a call to
8479 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8480 * invalidated.
8481 * @param value the x position to scroll to
8482 */
8483 public void setScrollX(int value) {
8484 scrollTo(value, mScrollY);
8485 }
8486
8487 /**
8488 * Set the vertical scrolled position of your view. This will cause a call to
8489 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8490 * invalidated.
8491 * @param value the y position to scroll to
8492 */
8493 public void setScrollY(int value) {
8494 scrollTo(mScrollX, value);
8495 }
8496
8497 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008498 * Return the scrolled left position of this view. This is the left edge of
8499 * the displayed part of your view. You do not need to draw any pixels
8500 * farther left, since those are outside of the frame of your view on
8501 * screen.
8502 *
8503 * @return The left edge of the displayed part of your view, in pixels.
8504 */
8505 public final int getScrollX() {
8506 return mScrollX;
8507 }
8508
8509 /**
8510 * Return the scrolled top position of this view. This is the top edge of
8511 * the displayed part of your view. You do not need to draw any pixels above
8512 * it, since those are outside of the frame of your view on screen.
8513 *
8514 * @return The top edge of the displayed part of your view, in pixels.
8515 */
8516 public final int getScrollY() {
8517 return mScrollY;
8518 }
8519
8520 /**
8521 * Return the width of the your view.
8522 *
8523 * @return The width of your view, in pixels.
8524 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008525 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008526 public final int getWidth() {
8527 return mRight - mLeft;
8528 }
8529
8530 /**
8531 * Return the height of your view.
8532 *
8533 * @return The height of your view, in pixels.
8534 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008535 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008536 public final int getHeight() {
8537 return mBottom - mTop;
8538 }
8539
8540 /**
8541 * Return the visible drawing bounds of your view. Fills in the output
8542 * rectangle with the values from getScrollX(), getScrollY(),
8543 * getWidth(), and getHeight().
8544 *
8545 * @param outRect The (scrolled) drawing bounds of the view.
8546 */
8547 public void getDrawingRect(Rect outRect) {
8548 outRect.left = mScrollX;
8549 outRect.top = mScrollY;
8550 outRect.right = mScrollX + (mRight - mLeft);
8551 outRect.bottom = mScrollY + (mBottom - mTop);
8552 }
8553
8554 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008555 * Like {@link #getMeasuredWidthAndState()}, but only returns the
8556 * raw width component (that is the result is masked by
8557 * {@link #MEASURED_SIZE_MASK}).
8558 *
8559 * @return The raw measured width of this view.
8560 */
8561 public final int getMeasuredWidth() {
8562 return mMeasuredWidth & MEASURED_SIZE_MASK;
8563 }
8564
8565 /**
8566 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008567 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008568 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008569 * This should be used during measurement and layout calculations only. Use
8570 * {@link #getWidth()} to see how wide a view is after layout.
8571 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008572 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008573 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008574 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008575 return mMeasuredWidth;
8576 }
8577
8578 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008579 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8580 * raw width component (that is the result is masked by
8581 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008582 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008583 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008584 */
8585 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008586 return mMeasuredHeight & MEASURED_SIZE_MASK;
8587 }
8588
8589 /**
8590 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008591 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008592 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8593 * This should be used during measurement and layout calculations only. Use
8594 * {@link #getHeight()} to see how wide a view is after layout.
8595 *
8596 * @return The measured width of this view as a bit mask.
8597 */
8598 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008599 return mMeasuredHeight;
8600 }
8601
8602 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008603 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8604 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8605 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8606 * and the height component is at the shifted bits
8607 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8608 */
8609 public final int getMeasuredState() {
8610 return (mMeasuredWidth&MEASURED_STATE_MASK)
8611 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8612 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8613 }
8614
8615 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008616 * The transform matrix of this view, which is calculated based on the current
8617 * roation, scale, and pivot properties.
8618 *
8619 * @see #getRotation()
8620 * @see #getScaleX()
8621 * @see #getScaleY()
8622 * @see #getPivotX()
8623 * @see #getPivotY()
8624 * @return The current transform matrix for the view
8625 */
8626 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008627 if (mTransformationInfo != null) {
8628 updateMatrix();
8629 return mTransformationInfo.mMatrix;
8630 }
8631 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008632 }
8633
8634 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008635 * Utility function to determine if the value is far enough away from zero to be
8636 * considered non-zero.
8637 * @param value A floating point value to check for zero-ness
8638 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8639 */
8640 private static boolean nonzero(float value) {
8641 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8642 }
8643
8644 /**
Jeff Brown86671742010-09-30 20:00:15 -07008645 * Returns true if the transform matrix is the identity matrix.
8646 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008647 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008648 * @return True if the transform matrix is the identity matrix, false otherwise.
8649 */
Jeff Brown86671742010-09-30 20:00:15 -07008650 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008651 if (mTransformationInfo != null) {
8652 updateMatrix();
8653 return mTransformationInfo.mMatrixIsIdentity;
8654 }
8655 return true;
8656 }
8657
8658 void ensureTransformationInfo() {
8659 if (mTransformationInfo == null) {
8660 mTransformationInfo = new TransformationInfo();
8661 }
Jeff Brown86671742010-09-30 20:00:15 -07008662 }
8663
8664 /**
8665 * Recomputes the transform matrix if necessary.
8666 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008667 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008668 final TransformationInfo info = mTransformationInfo;
8669 if (info == null) {
8670 return;
8671 }
8672 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008673 // transform-related properties have changed since the last time someone
8674 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008675
8676 // Figure out if we need to update the pivot point
Dianne Hackborn4702a852012-08-17 15:18:29 -07008677 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008678 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8679 info.mPrevWidth = mRight - mLeft;
8680 info.mPrevHeight = mBottom - mTop;
8681 info.mPivotX = info.mPrevWidth / 2f;
8682 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008683 }
8684 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008685 info.mMatrix.reset();
8686 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8687 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8688 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8689 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008690 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008691 if (info.mCamera == null) {
8692 info.mCamera = new Camera();
8693 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008694 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008695 info.mCamera.save();
8696 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8697 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8698 info.mCamera.getMatrix(info.matrix3D);
8699 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8700 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8701 info.mPivotY + info.mTranslationY);
8702 info.mMatrix.postConcat(info.matrix3D);
8703 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008704 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008705 info.mMatrixDirty = false;
8706 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8707 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008708 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008709 }
8710
8711 /**
Tobias Duboisdefdb1e2010-12-15 11:35:30 +01008712 * When searching for a view to focus this rectangle is used when considering if this view is
8713 * a good candidate for receiving focus.
8714 *
8715 * By default, the rectangle is the {@link #getDrawingRect}) of the view.
8716 *
8717 * @param r The rectangle to fill in, in this view's coordinates.
8718 */
8719 public void getFocusRect(Rect r) {
8720 getDrawingRect(r);
8721 }
8722
8723 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008724 * Utility method to retrieve the inverse of the current mMatrix property.
8725 * We cache the matrix to avoid recalculating it when transform properties
8726 * have not changed.
8727 *
8728 * @return The inverse of the current matrix of this view.
8729 */
Jeff Brown86671742010-09-30 20:00:15 -07008730 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008731 final TransformationInfo info = mTransformationInfo;
8732 if (info != null) {
8733 updateMatrix();
8734 if (info.mInverseMatrixDirty) {
8735 if (info.mInverseMatrix == null) {
8736 info.mInverseMatrix = new Matrix();
8737 }
8738 info.mMatrix.invert(info.mInverseMatrix);
8739 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008740 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008741 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008742 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008743 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008744 }
8745
8746 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008747 * Gets the distance along the Z axis from the camera to this view.
8748 *
8749 * @see #setCameraDistance(float)
8750 *
8751 * @return The distance along the Z axis.
8752 */
8753 public float getCameraDistance() {
8754 ensureTransformationInfo();
8755 final float dpi = mResources.getDisplayMetrics().densityDpi;
8756 final TransformationInfo info = mTransformationInfo;
8757 if (info.mCamera == null) {
8758 info.mCamera = new Camera();
8759 info.matrix3D = new Matrix();
8760 }
8761 return -(info.mCamera.getLocationZ() * dpi);
8762 }
8763
8764 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008765 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8766 * views are drawn) from the camera to this view. The camera's distance
8767 * affects 3D transformations, for instance rotations around the X and Y
8768 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008769 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008770 * use a camera distance that's greater than the height (X axis rotation) or
8771 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008772 *
Romain Guya5364ee2011-02-24 14:46:04 -08008773 * <p>The distance of the camera from the view plane can have an affect on the
8774 * perspective distortion of the view when it is rotated around the x or y axis.
8775 * For example, a large distance will result in a large viewing angle, and there
8776 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008777 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008778 * also result in some drawing artifacts if the rotated view ends up partially
8779 * behind the camera (which is why the recommendation is to use a distance at
8780 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008781 *
Romain Guya5364ee2011-02-24 14:46:04 -08008782 * <p>The distance is expressed in "depth pixels." The default distance depends
8783 * on the screen density. For instance, on a medium density display, the
8784 * default distance is 1280. On a high density display, the default distance
8785 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008786 *
Romain Guya5364ee2011-02-24 14:46:04 -08008787 * <p>If you want to specify a distance that leads to visually consistent
8788 * results across various densities, use the following formula:</p>
8789 * <pre>
8790 * float scale = context.getResources().getDisplayMetrics().density;
8791 * view.setCameraDistance(distance * scale);
8792 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07008793 *
Romain Guya5364ee2011-02-24 14:46:04 -08008794 * <p>The density scale factor of a high density display is 1.5,
8795 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008796 *
Romain Guya5364ee2011-02-24 14:46:04 -08008797 * @param distance The distance in "depth pixels", if negative the opposite
8798 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07008799 *
8800 * @see #setRotationX(float)
8801 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08008802 */
8803 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008804 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08008805
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008806 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08008807 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008808 final TransformationInfo info = mTransformationInfo;
8809 if (info.mCamera == null) {
8810 info.mCamera = new Camera();
8811 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08008812 }
8813
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008814 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
8815 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08008816
Chet Haase9d1992d2012-03-13 11:03:25 -07008817 invalidateViewProperty(false, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008818 if (mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07008819 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08008820 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008821 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008822 // View was rejected last time it was drawn by its parent; this may have changed
8823 invalidateParentIfNeeded();
8824 }
Romain Guya5364ee2011-02-24 14:46:04 -08008825 }
8826
8827 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008828 * The degrees that the view is rotated around the pivot point.
8829 *
Philip Milne6c8ea062012-04-03 17:38:43 -07008830 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07008831 * @see #getPivotX()
8832 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008833 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008834 * @return The degrees of rotation.
8835 */
Chet Haasea5531132012-02-02 13:41:44 -08008836 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008837 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008838 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008839 }
8840
8841 /**
Chet Haase897247b2010-09-09 14:54:47 -07008842 * Sets the degrees that the view is rotated around the pivot point. Increasing values
8843 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07008844 *
8845 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008846 *
8847 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07008848 * @see #getPivotX()
8849 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008850 * @see #setRotationX(float)
8851 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08008852 *
8853 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07008854 */
8855 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008856 ensureTransformationInfo();
8857 final TransformationInfo info = mTransformationInfo;
8858 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008859 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008860 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008861 info.mRotation = rotation;
8862 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008863 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008864 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008865 mDisplayList.setRotation(rotation);
8866 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008867 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008868 // View was rejected last time it was drawn by its parent; this may have changed
8869 invalidateParentIfNeeded();
8870 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008871 }
8872 }
8873
8874 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008875 * The degrees that the view is rotated around the vertical axis through the pivot point.
8876 *
8877 * @see #getPivotX()
8878 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008879 * @see #setRotationY(float)
8880 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008881 * @return The degrees of Y rotation.
8882 */
Chet Haasea5531132012-02-02 13:41:44 -08008883 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008884 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008885 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008886 }
8887
8888 /**
Chet Haase897247b2010-09-09 14:54:47 -07008889 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
8890 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
8891 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008892 *
Romain Guya5364ee2011-02-24 14:46:04 -08008893 * When rotating large views, it is recommended to adjust the camera distance
8894 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008895 *
8896 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008897 *
8898 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07008899 * @see #getPivotX()
8900 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008901 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008902 * @see #setRotationX(float)
8903 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008904 *
8905 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07008906 */
8907 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008908 ensureTransformationInfo();
8909 final TransformationInfo info = mTransformationInfo;
8910 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008911 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008912 info.mRotationY = rotationY;
8913 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008914 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008915 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008916 mDisplayList.setRotationY(rotationY);
8917 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008918 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008919 // View was rejected last time it was drawn by its parent; this may have changed
8920 invalidateParentIfNeeded();
8921 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008922 }
8923 }
8924
8925 /**
8926 * The degrees that the view is rotated around the horizontal axis through the pivot point.
8927 *
8928 * @see #getPivotX()
8929 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008930 * @see #setRotationX(float)
8931 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008932 * @return The degrees of X rotation.
8933 */
Chet Haasea5531132012-02-02 13:41:44 -08008934 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008935 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008936 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008937 }
8938
8939 /**
Chet Haase897247b2010-09-09 14:54:47 -07008940 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
8941 * Increasing values result in clockwise rotation from the viewpoint of looking down the
8942 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008943 *
Romain Guya5364ee2011-02-24 14:46:04 -08008944 * When rotating large views, it is recommended to adjust the camera distance
8945 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008946 *
8947 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008948 *
8949 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07008950 * @see #getPivotX()
8951 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008952 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008953 * @see #setRotationY(float)
8954 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008955 *
8956 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07008957 */
8958 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008959 ensureTransformationInfo();
8960 final TransformationInfo info = mTransformationInfo;
8961 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008962 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008963 info.mRotationX = rotationX;
8964 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008965 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008966 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008967 mDisplayList.setRotationX(rotationX);
8968 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008969 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008970 // View was rejected last time it was drawn by its parent; this may have changed
8971 invalidateParentIfNeeded();
8972 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008973 }
8974 }
8975
8976 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008977 * The amount that the view is scaled in x around the pivot point, as a proportion of
8978 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
8979 *
Joe Onorato93162322010-09-16 15:42:01 -04008980 * <p>By default, this is 1.0f.
8981 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008982 * @see #getPivotX()
8983 * @see #getPivotY()
8984 * @return The scaling factor.
8985 */
Chet Haasea5531132012-02-02 13:41:44 -08008986 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008987 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008988 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008989 }
8990
8991 /**
8992 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
8993 * the view's unscaled width. A value of 1 means that no scaling is applied.
8994 *
8995 * @param scaleX The scaling factor.
8996 * @see #getPivotX()
8997 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008998 *
8999 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07009000 */
9001 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009002 ensureTransformationInfo();
9003 final TransformationInfo info = mTransformationInfo;
9004 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009005 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009006 info.mScaleX = scaleX;
9007 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009008 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009009 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009010 mDisplayList.setScaleX(scaleX);
9011 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009012 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009013 // View was rejected last time it was drawn by its parent; this may have changed
9014 invalidateParentIfNeeded();
9015 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009016 }
9017 }
9018
9019 /**
9020 * The amount that the view is scaled in y around the pivot point, as a proportion of
9021 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
9022 *
Joe Onorato93162322010-09-16 15:42:01 -04009023 * <p>By default, this is 1.0f.
9024 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009025 * @see #getPivotX()
9026 * @see #getPivotY()
9027 * @return The scaling factor.
9028 */
Chet Haasea5531132012-02-02 13:41:44 -08009029 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009030 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009031 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009032 }
9033
9034 /**
9035 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
9036 * the view's unscaled width. A value of 1 means that no scaling is applied.
9037 *
9038 * @param scaleY The scaling factor.
9039 * @see #getPivotX()
9040 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009041 *
9042 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07009043 */
9044 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009045 ensureTransformationInfo();
9046 final TransformationInfo info = mTransformationInfo;
9047 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009048 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009049 info.mScaleY = scaleY;
9050 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009051 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009052 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009053 mDisplayList.setScaleY(scaleY);
9054 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009055 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009056 // View was rejected last time it was drawn by its parent; this may have changed
9057 invalidateParentIfNeeded();
9058 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009059 }
9060 }
9061
9062 /**
9063 * The x location of the point around which the view is {@link #setRotation(float) rotated}
9064 * and {@link #setScaleX(float) scaled}.
9065 *
9066 * @see #getRotation()
9067 * @see #getScaleX()
9068 * @see #getScaleY()
9069 * @see #getPivotY()
9070 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009071 *
9072 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07009073 */
Chet Haasea5531132012-02-02 13:41:44 -08009074 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009075 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009076 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009077 }
9078
9079 /**
9080 * Sets the x location of the point around which the view is
9081 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07009082 * By default, the pivot point is centered on the object.
9083 * Setting this property disables this behavior and causes the view to use only the
9084 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009085 *
9086 * @param pivotX The x location of the pivot point.
9087 * @see #getRotation()
9088 * @see #getScaleX()
9089 * @see #getScaleY()
9090 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009091 *
9092 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07009093 */
9094 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009095 ensureTransformationInfo();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009096 mPrivateFlags |= PFLAG_PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009097 final TransformationInfo info = mTransformationInfo;
9098 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009099 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009100 info.mPivotX = pivotX;
9101 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009102 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009103 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009104 mDisplayList.setPivotX(pivotX);
9105 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009106 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009107 // View was rejected last time it was drawn by its parent; this may have changed
9108 invalidateParentIfNeeded();
9109 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009110 }
9111 }
9112
9113 /**
9114 * The y location of the point around which the view is {@link #setRotation(float) rotated}
9115 * and {@link #setScaleY(float) scaled}.
9116 *
9117 * @see #getRotation()
9118 * @see #getScaleX()
9119 * @see #getScaleY()
9120 * @see #getPivotY()
9121 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009122 *
9123 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009124 */
Chet Haasea5531132012-02-02 13:41:44 -08009125 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009126 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009127 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009128 }
9129
9130 /**
9131 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07009132 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
9133 * Setting this property disables this behavior and causes the view to use only the
9134 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009135 *
9136 * @param pivotY The y location of the pivot point.
9137 * @see #getRotation()
9138 * @see #getScaleX()
9139 * @see #getScaleY()
9140 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009141 *
9142 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009143 */
9144 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009145 ensureTransformationInfo();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009146 mPrivateFlags |= PFLAG_PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009147 final TransformationInfo info = mTransformationInfo;
9148 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009149 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009150 info.mPivotY = pivotY;
9151 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009152 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009153 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009154 mDisplayList.setPivotY(pivotY);
9155 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009156 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009157 // View was rejected last time it was drawn by its parent; this may have changed
9158 invalidateParentIfNeeded();
9159 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009160 }
9161 }
9162
9163 /**
9164 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
9165 * completely transparent and 1 means the view is completely opaque.
9166 *
Joe Onorato93162322010-09-16 15:42:01 -04009167 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07009168 * @return The opacity of the view.
9169 */
Chet Haasea5531132012-02-02 13:41:44 -08009170 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009171 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009172 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009173 }
9174
9175 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07009176 * Returns whether this View has content which overlaps. This function, intended to be
9177 * overridden by specific View types, is an optimization when alpha is set on a view. If
9178 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
9179 * and then composited it into place, which can be expensive. If the view has no overlapping
9180 * rendering, the view can draw each primitive with the appropriate alpha value directly.
9181 * An example of overlapping rendering is a TextView with a background image, such as a
9182 * Button. An example of non-overlapping rendering is a TextView with no background, or
9183 * an ImageView with only the foreground image. The default implementation returns true;
9184 * subclasses should override if they have cases which can be optimized.
9185 *
9186 * @return true if the content in this view might overlap, false otherwise.
9187 */
9188 public boolean hasOverlappingRendering() {
9189 return true;
9190 }
9191
9192 /**
Romain Guy171c5922011-01-06 10:04:23 -08009193 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
9194 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009195 *
Romain Guy171c5922011-01-06 10:04:23 -08009196 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
9197 * responsible for applying the opacity itself. Otherwise, calling this method is
9198 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08009199 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07009200 *
Chet Haasea5531132012-02-02 13:41:44 -08009201 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
9202 * performance implications. It is generally best to use the alpha property sparingly and
9203 * transiently, as in the case of fading animations.</p>
9204 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009205 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08009206 *
Joe Malin32736f02011-01-19 16:14:20 -08009207 * @see #setLayerType(int, android.graphics.Paint)
9208 *
Chet Haase73066682010-11-29 15:55:32 -08009209 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07009210 */
9211 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009212 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009213 if (mTransformationInfo.mAlpha != alpha) {
9214 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009215 if (onSetAlpha((int) (alpha * 255))) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009216 mPrivateFlags |= PFLAG_ALPHA_SET;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009217 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07009218 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009219 invalidate(true);
9220 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009221 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07009222 invalidateViewProperty(true, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07009223 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009224 mDisplayList.setAlpha(alpha);
9225 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009226 }
Chet Haaseed032702010-10-01 14:05:54 -07009227 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009228 }
9229
9230 /**
Chet Haasea00f3862011-02-22 06:34:40 -08009231 * Faster version of setAlpha() which performs the same steps except there are
9232 * no calls to invalidate(). The caller of this function should perform proper invalidation
9233 * on the parent and this object. The return value indicates whether the subclass handles
9234 * alpha (the return value for onSetAlpha()).
9235 *
9236 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009237 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
9238 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08009239 */
9240 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009241 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009242 if (mTransformationInfo.mAlpha != alpha) {
9243 mTransformationInfo.mAlpha = alpha;
9244 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
9245 if (subclassHandlesAlpha) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009246 mPrivateFlags |= PFLAG_ALPHA_SET;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009247 return true;
9248 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009249 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase1271e2c2012-04-20 09:54:27 -07009250 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009251 mDisplayList.setAlpha(alpha);
9252 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009253 }
Chet Haasea00f3862011-02-22 06:34:40 -08009254 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009255 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08009256 }
9257
9258 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009259 * Top position of this view relative to its parent.
9260 *
9261 * @return The top of this view, in pixels.
9262 */
9263 @ViewDebug.CapturedViewProperty
9264 public final int getTop() {
9265 return mTop;
9266 }
9267
9268 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009269 * Sets the top position of this view relative to its parent. This method is meant to be called
9270 * by the layout system and should not generally be called otherwise, because the property
9271 * may be changed at any time by the layout.
9272 *
9273 * @param top The top of this view, in pixels.
9274 */
9275 public final void setTop(int top) {
9276 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07009277 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009278 final boolean matrixIsIdentity = mTransformationInfo == null
9279 || mTransformationInfo.mMatrixIsIdentity;
9280 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009281 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009282 int minTop;
9283 int yLoc;
9284 if (top < mTop) {
9285 minTop = top;
9286 yLoc = top - mTop;
9287 } else {
9288 minTop = mTop;
9289 yLoc = 0;
9290 }
Chet Haasee9140a72011-02-16 16:23:29 -08009291 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009292 }
9293 } else {
9294 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009295 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009296 }
9297
Chet Haaseed032702010-10-01 14:05:54 -07009298 int width = mRight - mLeft;
9299 int oldHeight = mBottom - mTop;
9300
Chet Haase21cd1382010-09-01 17:42:29 -07009301 mTop = top;
Chet Haase1271e2c2012-04-20 09:54:27 -07009302 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009303 mDisplayList.setTop(mTop);
9304 }
Chet Haase21cd1382010-09-01 17:42:29 -07009305
Chet Haaseed032702010-10-01 14:05:54 -07009306 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9307
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009308 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009309 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009310 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009311 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009312 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009313 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009314 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009315 }
Chet Haase55dbb652010-12-21 20:15:08 -08009316 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009317 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009318 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009319 // View was rejected last time it was drawn by its parent; this may have changed
9320 invalidateParentIfNeeded();
9321 }
Chet Haase21cd1382010-09-01 17:42:29 -07009322 }
9323 }
9324
9325 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009326 * Bottom position of this view relative to its parent.
9327 *
9328 * @return The bottom of this view, in pixels.
9329 */
9330 @ViewDebug.CapturedViewProperty
9331 public final int getBottom() {
9332 return mBottom;
9333 }
9334
9335 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08009336 * True if this view has changed since the last time being drawn.
9337 *
9338 * @return The dirty state of this view.
9339 */
9340 public boolean isDirty() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009341 return (mPrivateFlags & PFLAG_DIRTY_MASK) != 0;
Michael Jurkadab559a2011-01-04 20:31:51 -08009342 }
9343
9344 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009345 * Sets the bottom position of this view relative to its parent. This method is meant to be
9346 * called by the layout system and should not generally be called otherwise, because the
9347 * property may be changed at any time by the layout.
9348 *
9349 * @param bottom The bottom of this view, in pixels.
9350 */
9351 public final void setBottom(int bottom) {
9352 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07009353 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009354 final boolean matrixIsIdentity = mTransformationInfo == null
9355 || mTransformationInfo.mMatrixIsIdentity;
9356 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009357 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009358 int maxBottom;
9359 if (bottom < mBottom) {
9360 maxBottom = mBottom;
9361 } else {
9362 maxBottom = bottom;
9363 }
Chet Haasee9140a72011-02-16 16:23:29 -08009364 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009365 }
9366 } else {
9367 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009368 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009369 }
9370
Chet Haaseed032702010-10-01 14:05:54 -07009371 int width = mRight - mLeft;
9372 int oldHeight = mBottom - mTop;
9373
Chet Haase21cd1382010-09-01 17:42:29 -07009374 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -07009375 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009376 mDisplayList.setBottom(mBottom);
9377 }
Chet Haase21cd1382010-09-01 17:42:29 -07009378
Chet Haaseed032702010-10-01 14:05:54 -07009379 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9380
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009381 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009382 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009383 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009384 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009385 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009386 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009387 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009388 }
Chet Haase55dbb652010-12-21 20:15:08 -08009389 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009390 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009391 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009392 // View was rejected last time it was drawn by its parent; this may have changed
9393 invalidateParentIfNeeded();
9394 }
Chet Haase21cd1382010-09-01 17:42:29 -07009395 }
9396 }
9397
9398 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009399 * Left position of this view relative to its parent.
9400 *
9401 * @return The left edge of this view, in pixels.
9402 */
9403 @ViewDebug.CapturedViewProperty
9404 public final int getLeft() {
9405 return mLeft;
9406 }
9407
9408 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009409 * Sets the left position of this view relative to its parent. This method is meant to be called
9410 * by the layout system and should not generally be called otherwise, because the property
9411 * may be changed at any time by the layout.
9412 *
9413 * @param left The bottom of this view, in pixels.
9414 */
9415 public final void setLeft(int left) {
9416 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07009417 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009418 final boolean matrixIsIdentity = mTransformationInfo == null
9419 || mTransformationInfo.mMatrixIsIdentity;
9420 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009421 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009422 int minLeft;
9423 int xLoc;
9424 if (left < mLeft) {
9425 minLeft = left;
9426 xLoc = left - mLeft;
9427 } else {
9428 minLeft = mLeft;
9429 xLoc = 0;
9430 }
Chet Haasee9140a72011-02-16 16:23:29 -08009431 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009432 }
9433 } else {
9434 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009435 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009436 }
9437
Chet Haaseed032702010-10-01 14:05:54 -07009438 int oldWidth = mRight - mLeft;
9439 int height = mBottom - mTop;
9440
Chet Haase21cd1382010-09-01 17:42:29 -07009441 mLeft = left;
Chet Haase1271e2c2012-04-20 09:54:27 -07009442 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009443 mDisplayList.setLeft(left);
9444 }
Chet Haase21cd1382010-09-01 17:42:29 -07009445
Chet Haaseed032702010-10-01 14:05:54 -07009446 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9447
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009448 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009449 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009450 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009451 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009452 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009453 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009454 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009455 }
Chet Haase55dbb652010-12-21 20:15:08 -08009456 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009457 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009458 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009459 // View was rejected last time it was drawn by its parent; this may have changed
9460 invalidateParentIfNeeded();
9461 }
Chet Haase21cd1382010-09-01 17:42:29 -07009462 }
9463 }
9464
9465 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009466 * Right position of this view relative to its parent.
9467 *
9468 * @return The right edge of this view, in pixels.
9469 */
9470 @ViewDebug.CapturedViewProperty
9471 public final int getRight() {
9472 return mRight;
9473 }
9474
9475 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009476 * Sets the right position of this view relative to its parent. This method is meant to be called
9477 * by the layout system and should not generally be called otherwise, because the property
9478 * may be changed at any time by the layout.
9479 *
9480 * @param right The bottom of this view, in pixels.
9481 */
9482 public final void setRight(int right) {
9483 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07009484 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009485 final boolean matrixIsIdentity = mTransformationInfo == null
9486 || mTransformationInfo.mMatrixIsIdentity;
9487 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009488 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009489 int maxRight;
9490 if (right < mRight) {
9491 maxRight = mRight;
9492 } else {
9493 maxRight = right;
9494 }
Chet Haasee9140a72011-02-16 16:23:29 -08009495 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009496 }
9497 } else {
9498 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009499 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009500 }
9501
Chet Haaseed032702010-10-01 14:05:54 -07009502 int oldWidth = mRight - mLeft;
9503 int height = mBottom - mTop;
9504
Chet Haase21cd1382010-09-01 17:42:29 -07009505 mRight = right;
Chet Haase1271e2c2012-04-20 09:54:27 -07009506 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009507 mDisplayList.setRight(mRight);
9508 }
Chet Haase21cd1382010-09-01 17:42:29 -07009509
Chet Haaseed032702010-10-01 14:05:54 -07009510 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9511
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009512 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009513 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009514 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009515 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009516 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009517 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009518 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009519 }
Chet Haase55dbb652010-12-21 20:15:08 -08009520 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009521 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009522 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009523 // View was rejected last time it was drawn by its parent; this may have changed
9524 invalidateParentIfNeeded();
9525 }
Chet Haase21cd1382010-09-01 17:42:29 -07009526 }
9527 }
9528
9529 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009530 * The visual x position of this view, in pixels. This is equivalent to the
9531 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08009532 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07009533 *
Chet Haasedf030d22010-07-30 17:22:38 -07009534 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009535 */
Chet Haasea5531132012-02-02 13:41:44 -08009536 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009537 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009538 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009539 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009540
Chet Haasedf030d22010-07-30 17:22:38 -07009541 /**
9542 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
9543 * {@link #setTranslationX(float) translationX} property to be the difference between
9544 * the x value passed in and the current {@link #getLeft() left} property.
9545 *
9546 * @param x The visual x position of this view, in pixels.
9547 */
9548 public void setX(float x) {
9549 setTranslationX(x - mLeft);
9550 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009551
Chet Haasedf030d22010-07-30 17:22:38 -07009552 /**
9553 * The visual y position of this view, in pixels. This is equivalent to the
9554 * {@link #setTranslationY(float) translationY} property plus the current
9555 * {@link #getTop() top} property.
9556 *
9557 * @return The visual y position of this view, in pixels.
9558 */
Chet Haasea5531132012-02-02 13:41:44 -08009559 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009560 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009561 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009562 }
9563
9564 /**
9565 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
9566 * {@link #setTranslationY(float) translationY} property to be the difference between
9567 * the y value passed in and the current {@link #getTop() top} property.
9568 *
9569 * @param y The visual y position of this view, in pixels.
9570 */
9571 public void setY(float y) {
9572 setTranslationY(y - mTop);
9573 }
9574
9575
9576 /**
9577 * The horizontal location of this view relative to its {@link #getLeft() left} position.
9578 * This position is post-layout, in addition to wherever the object's
9579 * layout placed it.
9580 *
9581 * @return The horizontal position of this view relative to its left position, in pixels.
9582 */
Chet Haasea5531132012-02-02 13:41:44 -08009583 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009584 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009585 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07009586 }
9587
9588 /**
9589 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
9590 * This effectively positions the object post-layout, in addition to wherever the object's
9591 * layout placed it.
9592 *
9593 * @param translationX The horizontal position of this view relative to its left position,
9594 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009595 *
9596 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07009597 */
9598 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009599 ensureTransformationInfo();
9600 final TransformationInfo info = mTransformationInfo;
9601 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07009602 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009603 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009604 info.mTranslationX = translationX;
9605 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009606 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009607 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009608 mDisplayList.setTranslationX(translationX);
9609 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009610 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009611 // View was rejected last time it was drawn by its parent; this may have changed
9612 invalidateParentIfNeeded();
9613 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009614 }
9615 }
9616
9617 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009618 * The horizontal location of this view relative to its {@link #getTop() top} position.
9619 * This position is post-layout, in addition to wherever the object's
9620 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009621 *
Chet Haasedf030d22010-07-30 17:22:38 -07009622 * @return The vertical position of this view relative to its top position,
9623 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009624 */
Chet Haasea5531132012-02-02 13:41:44 -08009625 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009626 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009627 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009628 }
9629
9630 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009631 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9632 * This effectively positions the object post-layout, in addition to wherever the object's
9633 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009634 *
Chet Haasedf030d22010-07-30 17:22:38 -07009635 * @param translationY The vertical position of this view relative to its top position,
9636 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009637 *
9638 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009639 */
Chet Haasedf030d22010-07-30 17:22:38 -07009640 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009641 ensureTransformationInfo();
9642 final TransformationInfo info = mTransformationInfo;
9643 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009644 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009645 info.mTranslationY = translationY;
9646 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009647 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009648 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009649 mDisplayList.setTranslationY(translationY);
9650 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009651 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009652 // View was rejected last time it was drawn by its parent; this may have changed
9653 invalidateParentIfNeeded();
9654 }
Chet Haasedf030d22010-07-30 17:22:38 -07009655 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009656 }
9657
9658 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009659 * Hit rectangle in parent's coordinates
9660 *
9661 * @param outRect The hit rectangle of the view.
9662 */
9663 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009664 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009665 final TransformationInfo info = mTransformationInfo;
9666 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009667 outRect.set(mLeft, mTop, mRight, mBottom);
9668 } else {
9669 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009670 tmpRect.set(-info.mPivotX, -info.mPivotY,
9671 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9672 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009673 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9674 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009675 }
9676 }
9677
9678 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009679 * Determines whether the given point, in local coordinates is inside the view.
9680 */
9681 /*package*/ final boolean pointInView(float localX, float localY) {
9682 return localX >= 0 && localX < (mRight - mLeft)
9683 && localY >= 0 && localY < (mBottom - mTop);
9684 }
9685
9686 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009687 * Utility method to determine whether the given point, in local coordinates,
9688 * is inside the view, where the area of the view is expanded by the slop factor.
9689 * This method is called while processing touch-move events to determine if the event
9690 * is still within the view.
9691 */
9692 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009693 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009694 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009695 }
9696
9697 /**
9698 * When a view has focus and the user navigates away from it, the next view is searched for
9699 * starting from the rectangle filled in by this method.
9700 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009701 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
9702 * of the view. However, if your view maintains some idea of internal selection,
9703 * such as a cursor, or a selected row or column, you should override this method and
9704 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009705 *
9706 * @param r The rectangle to fill in, in this view's coordinates.
9707 */
9708 public void getFocusedRect(Rect r) {
9709 getDrawingRect(r);
9710 }
9711
9712 /**
9713 * If some part of this view is not clipped by any of its parents, then
9714 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009715 * coordinates (without taking possible View rotations into account), offset
9716 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9717 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009718 *
9719 * @param r If true is returned, r holds the global coordinates of the
9720 * visible portion of this view.
9721 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9722 * between this view and its root. globalOffet may be null.
9723 * @return true if r is non-empty (i.e. part of the view is visible at the
9724 * root level.
9725 */
9726 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9727 int width = mRight - mLeft;
9728 int height = mBottom - mTop;
9729 if (width > 0 && height > 0) {
9730 r.set(0, 0, width, height);
9731 if (globalOffset != null) {
9732 globalOffset.set(-mScrollX, -mScrollY);
9733 }
9734 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9735 }
9736 return false;
9737 }
9738
9739 public final boolean getGlobalVisibleRect(Rect r) {
9740 return getGlobalVisibleRect(r, null);
9741 }
9742
9743 public final boolean getLocalVisibleRect(Rect r) {
Romain Guyab4c4f4f2012-05-06 13:11:24 -07009744 final Point offset = mAttachInfo != null ? mAttachInfo.mPoint : new Point();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009745 if (getGlobalVisibleRect(r, offset)) {
9746 r.offset(-offset.x, -offset.y); // make r local
9747 return true;
9748 }
9749 return false;
9750 }
9751
9752 /**
9753 * Offset this view's vertical location by the specified number of pixels.
9754 *
9755 * @param offset the number of pixels to offset the view by
9756 */
9757 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009758 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009759 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009760 final boolean matrixIsIdentity = mTransformationInfo == null
9761 || mTransformationInfo.mMatrixIsIdentity;
9762 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009763 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009764 invalidateViewProperty(false, false);
9765 } else {
9766 final ViewParent p = mParent;
9767 if (p != null && mAttachInfo != null) {
9768 final Rect r = mAttachInfo.mTmpInvalRect;
9769 int minTop;
9770 int maxBottom;
9771 int yLoc;
9772 if (offset < 0) {
9773 minTop = mTop + offset;
9774 maxBottom = mBottom;
9775 yLoc = offset;
9776 } else {
9777 minTop = mTop;
9778 maxBottom = mBottom + offset;
9779 yLoc = 0;
9780 }
9781 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
9782 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009783 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009784 }
9785 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009786 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009787 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009788
Chet Haasec3aa3612010-06-17 08:50:37 -07009789 mTop += offset;
9790 mBottom += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009791 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009792 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009793 invalidateViewProperty(false, false);
9794 } else {
9795 if (!matrixIsIdentity) {
9796 invalidateViewProperty(false, true);
9797 }
9798 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009799 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009800 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009801 }
9802
9803 /**
9804 * Offset this view's horizontal location by the specified amount of pixels.
9805 *
9806 * @param offset the numer of pixels to offset the view by
9807 */
9808 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009809 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009810 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009811 final boolean matrixIsIdentity = mTransformationInfo == null
9812 || mTransformationInfo.mMatrixIsIdentity;
9813 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009814 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009815 invalidateViewProperty(false, false);
9816 } else {
9817 final ViewParent p = mParent;
9818 if (p != null && mAttachInfo != null) {
9819 final Rect r = mAttachInfo.mTmpInvalRect;
9820 int minLeft;
9821 int maxRight;
9822 if (offset < 0) {
9823 minLeft = mLeft + offset;
9824 maxRight = mRight;
9825 } else {
9826 minLeft = mLeft;
9827 maxRight = mRight + offset;
9828 }
9829 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
9830 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009831 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009832 }
9833 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009834 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009835 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009836
Chet Haasec3aa3612010-06-17 08:50:37 -07009837 mLeft += offset;
9838 mRight += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009839 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009840 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009841 invalidateViewProperty(false, false);
9842 } else {
9843 if (!matrixIsIdentity) {
9844 invalidateViewProperty(false, true);
9845 }
9846 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009847 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009848 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009849 }
9850
9851 /**
9852 * Get the LayoutParams associated with this view. All views should have
9853 * layout parameters. These supply parameters to the <i>parent</i> of this
9854 * view specifying how it should be arranged. There are many subclasses of
9855 * ViewGroup.LayoutParams, and these correspond to the different subclasses
9856 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08009857 *
9858 * This method may return null if this View is not attached to a parent
9859 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
9860 * was not invoked successfully. When a View is attached to a parent
9861 * ViewGroup, this method must not return null.
9862 *
9863 * @return The LayoutParams associated with this view, or null if no
9864 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009865 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07009866 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009867 public ViewGroup.LayoutParams getLayoutParams() {
9868 return mLayoutParams;
9869 }
9870
9871 /**
9872 * Set the layout parameters associated with this view. These supply
9873 * parameters to the <i>parent</i> of this view specifying how it should be
9874 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
9875 * correspond to the different subclasses of ViewGroup that are responsible
9876 * for arranging their children.
9877 *
Romain Guy01c174b2011-02-22 11:51:06 -08009878 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009879 */
9880 public void setLayoutParams(ViewGroup.LayoutParams params) {
9881 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08009882 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009883 }
9884 mLayoutParams = params;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07009885 resolveLayoutParams();
Philip Milned7dd8902012-01-26 16:55:30 -08009886 if (mParent instanceof ViewGroup) {
9887 ((ViewGroup) mParent).onSetLayoutParams(this, params);
9888 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009889 requestLayout();
9890 }
9891
9892 /**
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07009893 * Resolve the layout parameters depending on the resolved layout direction
9894 */
9895 private void resolveLayoutParams() {
9896 if (mLayoutParams != null) {
9897 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
9898 }
9899 }
9900
9901 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009902 * Set the scrolled position of your view. This will cause a call to
9903 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9904 * invalidated.
9905 * @param x the x position to scroll to
9906 * @param y the y position to scroll to
9907 */
9908 public void scrollTo(int x, int y) {
9909 if (mScrollX != x || mScrollY != y) {
9910 int oldX = mScrollX;
9911 int oldY = mScrollY;
9912 mScrollX = x;
9913 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009914 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009915 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07009916 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009917 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -07009918 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009919 }
9920 }
9921
9922 /**
9923 * Move the scrolled position of your view. This will cause a call to
9924 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9925 * invalidated.
9926 * @param x the amount of pixels to scroll by horizontally
9927 * @param y the amount of pixels to scroll by vertically
9928 */
9929 public void scrollBy(int x, int y) {
9930 scrollTo(mScrollX + x, mScrollY + y);
9931 }
9932
9933 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009934 * <p>Trigger the scrollbars to draw. When invoked this method starts an
9935 * animation to fade the scrollbars out after a default delay. If a subclass
9936 * provides animated scrolling, the start delay should equal the duration
9937 * of the scrolling animation.</p>
9938 *
9939 * <p>The animation starts only if at least one of the scrollbars is
9940 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
9941 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9942 * this method returns true, and false otherwise. If the animation is
9943 * started, this method calls {@link #invalidate()}; in that case the
9944 * caller should not call {@link #invalidate()}.</p>
9945 *
9946 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07009947 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07009948 *
9949 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
9950 * and {@link #scrollTo(int, int)}.</p>
9951 *
9952 * @return true if the animation is played, false otherwise
9953 *
9954 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07009955 * @see #scrollBy(int, int)
9956 * @see #scrollTo(int, int)
9957 * @see #isHorizontalScrollBarEnabled()
9958 * @see #isVerticalScrollBarEnabled()
9959 * @see #setHorizontalScrollBarEnabled(boolean)
9960 * @see #setVerticalScrollBarEnabled(boolean)
9961 */
9962 protected boolean awakenScrollBars() {
9963 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07009964 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07009965 }
9966
9967 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07009968 * Trigger the scrollbars to draw.
9969 * This method differs from awakenScrollBars() only in its default duration.
9970 * initialAwakenScrollBars() will show the scroll bars for longer than
9971 * usual to give the user more of a chance to notice them.
9972 *
9973 * @return true if the animation is played, false otherwise.
9974 */
9975 private boolean initialAwakenScrollBars() {
9976 return mScrollCache != null &&
9977 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
9978 }
9979
9980 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009981 * <p>
9982 * Trigger the scrollbars to draw. When invoked this method starts an
9983 * animation to fade the scrollbars out after a fixed delay. If a subclass
9984 * provides animated scrolling, the start delay should equal the duration of
9985 * the scrolling animation.
9986 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009987 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009988 * <p>
9989 * The animation starts only if at least one of the scrollbars is enabled,
9990 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9991 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9992 * this method returns true, and false otherwise. If the animation is
9993 * started, this method calls {@link #invalidate()}; in that case the caller
9994 * should not call {@link #invalidate()}.
9995 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009996 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009997 * <p>
9998 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07009999 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -070010000 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010001 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010002 * @param startDelay the delay, in milliseconds, after which the animation
10003 * should start; when the delay is 0, the animation starts
10004 * immediately
10005 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -080010006 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010007 * @see #scrollBy(int, int)
10008 * @see #scrollTo(int, int)
10009 * @see #isHorizontalScrollBarEnabled()
10010 * @see #isVerticalScrollBarEnabled()
10011 * @see #setHorizontalScrollBarEnabled(boolean)
10012 * @see #setVerticalScrollBarEnabled(boolean)
10013 */
10014 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -070010015 return awakenScrollBars(startDelay, true);
10016 }
Joe Malin32736f02011-01-19 16:14:20 -080010017
Mike Cleron290947b2009-09-29 18:34:32 -070010018 /**
10019 * <p>
10020 * Trigger the scrollbars to draw. When invoked this method starts an
10021 * animation to fade the scrollbars out after a fixed delay. If a subclass
10022 * provides animated scrolling, the start delay should equal the duration of
10023 * the scrolling animation.
10024 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010025 *
Mike Cleron290947b2009-09-29 18:34:32 -070010026 * <p>
10027 * The animation starts only if at least one of the scrollbars is enabled,
10028 * as specified by {@link #isHorizontalScrollBarEnabled()} and
10029 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
10030 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -080010031 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -070010032 * is set to true; in that case the caller
10033 * should not call {@link #invalidate()}.
10034 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010035 *
Mike Cleron290947b2009-09-29 18:34:32 -070010036 * <p>
10037 * This method should be invoked everytime a subclass directly updates the
10038 * scroll parameters.
10039 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010040 *
Mike Cleron290947b2009-09-29 18:34:32 -070010041 * @param startDelay the delay, in milliseconds, after which the animation
10042 * should start; when the delay is 0, the animation starts
10043 * immediately
Joe Malin32736f02011-01-19 16:14:20 -080010044 *
Mike Cleron290947b2009-09-29 18:34:32 -070010045 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -080010046 *
Mike Cleron290947b2009-09-29 18:34:32 -070010047 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -080010048 *
Mike Cleron290947b2009-09-29 18:34:32 -070010049 * @see #scrollBy(int, int)
10050 * @see #scrollTo(int, int)
10051 * @see #isHorizontalScrollBarEnabled()
10052 * @see #isVerticalScrollBarEnabled()
10053 * @see #setHorizontalScrollBarEnabled(boolean)
10054 * @see #setVerticalScrollBarEnabled(boolean)
10055 */
10056 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -070010057 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -080010058
Mike Cleronf116bf82009-09-27 19:14:12 -070010059 if (scrollCache == null || !scrollCache.fadeScrollBars) {
10060 return false;
10061 }
10062
10063 if (scrollCache.scrollBar == null) {
10064 scrollCache.scrollBar = new ScrollBarDrawable();
10065 }
10066
10067 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
10068
Mike Cleron290947b2009-09-29 18:34:32 -070010069 if (invalidate) {
10070 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -070010071 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -070010072 }
Mike Cleronf116bf82009-09-27 19:14:12 -070010073
10074 if (scrollCache.state == ScrollabilityCache.OFF) {
10075 // FIXME: this is copied from WindowManagerService.
10076 // We should get this value from the system when it
10077 // is possible to do so.
10078 final int KEY_REPEAT_FIRST_DELAY = 750;
10079 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
10080 }
10081
10082 // Tell mScrollCache when we should start fading. This may
10083 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -070010084 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -070010085 scrollCache.fadeStartTime = fadeStartTime;
10086 scrollCache.state = ScrollabilityCache.ON;
10087
10088 // Schedule our fader to run, unscheduling any old ones first
10089 if (mAttachInfo != null) {
10090 mAttachInfo.mHandler.removeCallbacks(scrollCache);
10091 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
10092 }
10093
10094 return true;
10095 }
10096
10097 return false;
10098 }
10099
10100 /**
Chet Haaseaceafe62011-08-26 15:44:33 -070010101 * Do not invalidate views which are not visible and which are not running an animation. They
10102 * will not get drawn and they should not set dirty flags as if they will be drawn
10103 */
10104 private boolean skipInvalidate() {
10105 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
10106 (!(mParent instanceof ViewGroup) ||
10107 !((ViewGroup) mParent).isViewTransitioning(this));
10108 }
10109 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010110 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010111 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
10112 * in the future. This must be called from a UI thread. To call from a non-UI
10113 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010114 *
10115 * WARNING: This method is destructive to dirty.
10116 * @param dirty the rectangle representing the bounds of the dirty region
10117 */
10118 public void invalidate(Rect dirty) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010119 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010120 return;
10121 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010122 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10123 (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID ||
10124 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED) {
10125 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
10126 mPrivateFlags |= PFLAG_INVALIDATED;
10127 mPrivateFlags |= PFLAG_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010128 final ViewParent p = mParent;
10129 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010130 //noinspection PointlessBooleanExpression,ConstantConditions
10131 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10132 if (p != null && ai != null && ai.mHardwareAccelerated) {
10133 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010134 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010135 p.invalidateChild(this, null);
10136 return;
10137 }
Romain Guyaf636eb2010-12-09 17:47:21 -080010138 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010139 if (p != null && ai != null) {
10140 final int scrollX = mScrollX;
10141 final int scrollY = mScrollY;
10142 final Rect r = ai.mTmpInvalRect;
10143 r.set(dirty.left - scrollX, dirty.top - scrollY,
10144 dirty.right - scrollX, dirty.bottom - scrollY);
10145 mParent.invalidateChild(this, r);
10146 }
10147 }
10148 }
10149
10150 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010151 * 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 -080010152 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -070010153 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
10154 * will be called at some point in the future. This must be called from
10155 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010156 * @param l the left position of the dirty region
10157 * @param t the top position of the dirty region
10158 * @param r the right position of the dirty region
10159 * @param b the bottom position of the dirty region
10160 */
10161 public void invalidate(int l, int t, int r, int b) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010162 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010163 return;
10164 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010165 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10166 (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID ||
10167 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED) {
10168 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
10169 mPrivateFlags |= PFLAG_INVALIDATED;
10170 mPrivateFlags |= PFLAG_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010171 final ViewParent p = mParent;
10172 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010173 //noinspection PointlessBooleanExpression,ConstantConditions
10174 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10175 if (p != null && ai != null && ai.mHardwareAccelerated) {
10176 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010177 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010178 p.invalidateChild(this, null);
10179 return;
10180 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010181 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010182 if (p != null && ai != null && l < r && t < b) {
10183 final int scrollX = mScrollX;
10184 final int scrollY = mScrollY;
10185 final Rect tmpr = ai.mTmpInvalRect;
10186 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
10187 p.invalidateChild(this, tmpr);
10188 }
10189 }
10190 }
10191
10192 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010193 * Invalidate the whole view. If the view is visible,
10194 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
10195 * the future. This must be called from a UI thread. To call from a non-UI thread,
10196 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010197 */
10198 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -070010199 invalidate(true);
10200 }
Joe Malin32736f02011-01-19 16:14:20 -080010201
Chet Haaseed032702010-10-01 14:05:54 -070010202 /**
10203 * This is where the invalidate() work actually happens. A full invalidate()
10204 * causes the drawing cache to be invalidated, but this function can be called with
10205 * invalidateCache set to false to skip that invalidation step for cases that do not
10206 * need it (for example, a component that remains at the same dimensions with the same
10207 * content).
10208 *
10209 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
10210 * well. This is usually true for a full invalidate, but may be set to false if the
10211 * View's contents or dimensions have not changed.
10212 */
Romain Guy849d0a32011-02-01 17:20:48 -080010213 void invalidate(boolean invalidateCache) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010214 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010215 return;
10216 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010217 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10218 (invalidateCache && (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID) ||
10219 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED || isOpaque() != mLastIsOpaque) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010220 mLastIsOpaque = isOpaque();
Dianne Hackborn4702a852012-08-17 15:18:29 -070010221 mPrivateFlags &= ~PFLAG_DRAWN;
10222 mPrivateFlags |= PFLAG_DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -070010223 if (invalidateCache) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010224 mPrivateFlags |= PFLAG_INVALIDATED;
10225 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Chet Haaseed032702010-10-01 14:05:54 -070010226 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010227 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -070010228 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -080010229 //noinspection PointlessBooleanExpression,ConstantConditions
10230 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10231 if (p != null && ai != null && ai.mHardwareAccelerated) {
10232 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010233 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010234 p.invalidateChild(this, null);
10235 return;
10236 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010237 }
Michael Jurkaebefea42010-11-15 16:04:01 -080010238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010239 if (p != null && ai != null) {
10240 final Rect r = ai.mTmpInvalRect;
10241 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10242 // Don't call invalidate -- we don't want to internally scroll
10243 // our own bounds
10244 p.invalidateChild(this, r);
10245 }
10246 }
10247 }
10248
10249 /**
Chet Haase9d1992d2012-03-13 11:03:25 -070010250 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
10251 * set any flags or handle all of the cases handled by the default invalidation methods.
10252 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
10253 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
10254 * walk up the hierarchy, transforming the dirty rect as necessary.
10255 *
10256 * The method also handles normal invalidation logic if display list properties are not
10257 * being used in this view. The invalidateParent and forceRedraw flags are used by that
10258 * backup approach, to handle these cases used in the various property-setting methods.
10259 *
10260 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
10261 * are not being used in this view
10262 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
10263 * list properties are not being used in this view
10264 */
10265 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010266 if (mDisplayList == null || (mPrivateFlags & PFLAG_DRAW_ANIMATION) == PFLAG_DRAW_ANIMATION) {
Chet Haase9d1992d2012-03-13 11:03:25 -070010267 if (invalidateParent) {
10268 invalidateParentCaches();
10269 }
10270 if (forceRedraw) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010271 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Chet Haase9d1992d2012-03-13 11:03:25 -070010272 }
10273 invalidate(false);
10274 } else {
10275 final AttachInfo ai = mAttachInfo;
10276 final ViewParent p = mParent;
10277 if (p != null && ai != null) {
10278 final Rect r = ai.mTmpInvalRect;
10279 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10280 if (mParent instanceof ViewGroup) {
10281 ((ViewGroup) mParent).invalidateChildFast(this, r);
10282 } else {
10283 mParent.invalidateChild(this, r);
10284 }
10285 }
10286 }
10287 }
10288
10289 /**
10290 * Utility method to transform a given Rect by the current matrix of this view.
10291 */
10292 void transformRect(final Rect rect) {
10293 if (!getMatrix().isIdentity()) {
10294 RectF boundingRect = mAttachInfo.mTmpTransformRect;
10295 boundingRect.set(rect);
10296 getMatrix().mapRect(boundingRect);
10297 rect.set((int) (boundingRect.left - 0.5f),
10298 (int) (boundingRect.top - 0.5f),
10299 (int) (boundingRect.right + 0.5f),
10300 (int) (boundingRect.bottom + 0.5f));
10301 }
10302 }
10303
10304 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -080010305 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -080010306 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10307 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -080010308 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
10309 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -080010310 *
10311 * @hide
10312 */
Romain Guy0fd89bf2011-01-26 15:41:30 -080010313 protected void invalidateParentCaches() {
10314 if (mParent instanceof View) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010315 ((View) mParent).mPrivateFlags |= PFLAG_INVALIDATED;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010316 }
10317 }
Joe Malin32736f02011-01-19 16:14:20 -080010318
Romain Guy0fd89bf2011-01-26 15:41:30 -080010319 /**
10320 * Used to indicate that the parent of this view should be invalidated. This functionality
10321 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10322 * which is necessary when various parent-managed properties of the view change, such as
10323 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
10324 * an invalidation event to the parent.
10325 *
10326 * @hide
10327 */
10328 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -080010329 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010330 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -080010331 }
10332 }
10333
10334 /**
Romain Guy24443ea2009-05-11 11:56:30 -070010335 * Indicates whether this View is opaque. An opaque View guarantees that it will
10336 * draw all the pixels overlapping its bounds using a fully opaque color.
10337 *
10338 * Subclasses of View should override this method whenever possible to indicate
10339 * whether an instance is opaque. Opaque Views are treated in a special way by
10340 * the View hierarchy, possibly allowing it to perform optimizations during
10341 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -070010342 *
Romain Guy24443ea2009-05-11 11:56:30 -070010343 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -070010344 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010345 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -070010346 public boolean isOpaque() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010347 return (mPrivateFlags & PFLAG_OPAQUE_MASK) == PFLAG_OPAQUE_MASK &&
Romain Guyf8773082012-07-12 18:01:00 -070010348 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1.0f) >= 1.0f);
Romain Guy8f1344f52009-05-15 16:03:59 -070010349 }
10350
Adam Powell20232d02010-12-08 21:08:53 -080010351 /**
10352 * @hide
10353 */
10354 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -070010355 // Opaque if:
10356 // - Has a background
10357 // - Background is opaque
10358 // - Doesn't have scrollbars or scrollbars are inside overlay
10359
Philip Milne6c8ea062012-04-03 17:38:43 -070010360 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010361 mPrivateFlags |= PFLAG_OPAQUE_BACKGROUND;
Romain Guy8f1344f52009-05-15 16:03:59 -070010362 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010363 mPrivateFlags &= ~PFLAG_OPAQUE_BACKGROUND;
Romain Guy8f1344f52009-05-15 16:03:59 -070010364 }
10365
10366 final int flags = mViewFlags;
10367 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
10368 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010369 mPrivateFlags |= PFLAG_OPAQUE_SCROLLBARS;
Romain Guy8f1344f52009-05-15 16:03:59 -070010370 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010371 mPrivateFlags &= ~PFLAG_OPAQUE_SCROLLBARS;
Romain Guy8f1344f52009-05-15 16:03:59 -070010372 }
10373 }
10374
10375 /**
10376 * @hide
10377 */
10378 protected boolean hasOpaqueScrollbars() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010379 return (mPrivateFlags & PFLAG_OPAQUE_SCROLLBARS) == PFLAG_OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -070010380 }
10381
10382 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010383 * @return A handler associated with the thread running the View. This
10384 * handler can be used to pump events in the UI events queue.
10385 */
10386 public Handler getHandler() {
10387 if (mAttachInfo != null) {
10388 return mAttachInfo.mHandler;
10389 }
10390 return null;
10391 }
10392
10393 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080010394 * Gets the view root associated with the View.
10395 * @return The view root, or null if none.
10396 * @hide
10397 */
10398 public ViewRootImpl getViewRootImpl() {
10399 if (mAttachInfo != null) {
10400 return mAttachInfo.mViewRootImpl;
10401 }
10402 return null;
10403 }
10404
10405 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010406 * <p>Causes the Runnable to be added to the message queue.
10407 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010408 *
Romain Guye63a4f32011-08-11 11:33:31 -070010409 * <p>This method can be invoked from outside of the UI thread
10410 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010411 *
10412 * @param action The Runnable that will be executed.
10413 *
10414 * @return Returns true if the Runnable was successfully placed in to the
10415 * message queue. Returns false on failure, usually because the
10416 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010417 *
10418 * @see #postDelayed
10419 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010420 */
10421 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010422 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010423 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010424 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010425 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010426 // Assume that post will succeed later
10427 ViewRootImpl.getRunQueue().post(action);
10428 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010429 }
10430
10431 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010432 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010433 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -070010434 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010435 *
Romain Guye63a4f32011-08-11 11:33:31 -070010436 * <p>This method can be invoked from outside of the UI thread
10437 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010438 *
10439 * @param action The Runnable that will be executed.
10440 * @param delayMillis The delay (in milliseconds) until the Runnable
10441 * will be executed.
10442 *
10443 * @return true if the Runnable was successfully placed in to the
10444 * message queue. Returns false on failure, usually because the
10445 * looper processing the message queue is exiting. Note that a
10446 * result of true does not mean the Runnable will be processed --
10447 * if the looper is quit before the delivery time of the message
10448 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010449 *
10450 * @see #post
10451 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010452 */
10453 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010454 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010455 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010456 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010457 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010458 // Assume that post will succeed later
10459 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10460 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010461 }
10462
10463 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010464 * <p>Causes the Runnable to execute on the next animation time step.
10465 * The runnable will be run on the user interface thread.</p>
10466 *
10467 * <p>This method can be invoked from outside of the UI thread
10468 * only when this View is attached to a window.</p>
10469 *
10470 * @param action The Runnable that will be executed.
10471 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010472 * @see #postOnAnimationDelayed
10473 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010474 */
10475 public void postOnAnimation(Runnable action) {
10476 final AttachInfo attachInfo = mAttachInfo;
10477 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010478 attachInfo.mViewRootImpl.mChoreographer.postCallback(
10479 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010480 } else {
10481 // Assume that post will succeed later
10482 ViewRootImpl.getRunQueue().post(action);
10483 }
10484 }
10485
10486 /**
10487 * <p>Causes the Runnable to execute on the next animation time step,
10488 * after the specified amount of time elapses.
10489 * The runnable will be run on the user interface thread.</p>
10490 *
10491 * <p>This method can be invoked from outside of the UI thread
10492 * only when this View is attached to a window.</p>
10493 *
10494 * @param action The Runnable that will be executed.
10495 * @param delayMillis The delay (in milliseconds) until the Runnable
10496 * will be executed.
10497 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010498 * @see #postOnAnimation
10499 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010500 */
10501 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
10502 final AttachInfo attachInfo = mAttachInfo;
10503 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010504 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
10505 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010506 } else {
10507 // Assume that post will succeed later
10508 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10509 }
10510 }
10511
10512 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010513 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010514 *
Romain Guye63a4f32011-08-11 11:33:31 -070010515 * <p>This method can be invoked from outside of the UI thread
10516 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010517 *
10518 * @param action The Runnable to remove from the message handling queue
10519 *
10520 * @return true if this view could ask the Handler to remove the Runnable,
10521 * false otherwise. When the returned value is true, the Runnable
10522 * may or may not have been actually removed from the message queue
10523 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010524 *
10525 * @see #post
10526 * @see #postDelayed
10527 * @see #postOnAnimation
10528 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010529 */
10530 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080010531 if (action != null) {
10532 final AttachInfo attachInfo = mAttachInfo;
10533 if (attachInfo != null) {
10534 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010535 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
10536 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -080010537 } else {
10538 // Assume that post will succeed later
10539 ViewRootImpl.getRunQueue().removeCallbacks(action);
10540 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010541 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010542 return true;
10543 }
10544
10545 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010546 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
10547 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010548 *
Romain Guye63a4f32011-08-11 11:33:31 -070010549 * <p>This method can be invoked from outside of the UI thread
10550 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010551 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010552 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010553 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010554 */
10555 public void postInvalidate() {
10556 postInvalidateDelayed(0);
10557 }
10558
10559 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010560 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10561 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010562 *
Romain Guye63a4f32011-08-11 11:33:31 -070010563 * <p>This method can be invoked from outside of the UI thread
10564 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010565 *
10566 * @param left The left coordinate of the rectangle to invalidate.
10567 * @param top The top coordinate of the rectangle to invalidate.
10568 * @param right The right coordinate of the rectangle to invalidate.
10569 * @param bottom The bottom coordinate of the rectangle to invalidate.
10570 *
10571 * @see #invalidate(int, int, int, int)
10572 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010573 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010574 */
10575 public void postInvalidate(int left, int top, int right, int bottom) {
10576 postInvalidateDelayed(0, left, top, right, bottom);
10577 }
10578
10579 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010580 * <p>Cause an invalidate to happen on a subsequent cycle through the event
10581 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010582 *
Romain Guye63a4f32011-08-11 11:33:31 -070010583 * <p>This method can be invoked from outside of the UI thread
10584 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010585 *
10586 * @param delayMilliseconds the duration in milliseconds to delay the
10587 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010588 *
10589 * @see #invalidate()
10590 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010591 */
10592 public void postInvalidateDelayed(long delayMilliseconds) {
10593 // We try only with the AttachInfo because there's no point in invalidating
10594 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010595 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010596 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010597 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010598 }
10599 }
10600
10601 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010602 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10603 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010604 *
Romain Guye63a4f32011-08-11 11:33:31 -070010605 * <p>This method can be invoked from outside of the UI thread
10606 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010607 *
10608 * @param delayMilliseconds the duration in milliseconds to delay the
10609 * invalidation by
10610 * @param left The left coordinate of the rectangle to invalidate.
10611 * @param top The top coordinate of the rectangle to invalidate.
10612 * @param right The right coordinate of the rectangle to invalidate.
10613 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010614 *
10615 * @see #invalidate(int, int, int, int)
10616 * @see #invalidate(Rect)
10617 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010618 */
10619 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
10620 int right, int bottom) {
10621
10622 // We try only with the AttachInfo because there's no point in invalidating
10623 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010624 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010625 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010626 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10627 info.target = this;
10628 info.left = left;
10629 info.top = top;
10630 info.right = right;
10631 info.bottom = bottom;
10632
Jeff Browna175a5b2012-02-15 19:18:31 -080010633 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010634 }
10635 }
10636
10637 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010638 * <p>Cause an invalidate to happen on the next animation time step, typically the
10639 * next display frame.</p>
10640 *
10641 * <p>This method can be invoked from outside of the UI thread
10642 * only when this View is attached to a window.</p>
10643 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010644 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010645 */
10646 public void postInvalidateOnAnimation() {
10647 // We try only with the AttachInfo because there's no point in invalidating
10648 // if we are not attached to our window
10649 final AttachInfo attachInfo = mAttachInfo;
10650 if (attachInfo != null) {
10651 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10652 }
10653 }
10654
10655 /**
10656 * <p>Cause an invalidate of the specified area to happen on the next animation
10657 * time step, typically the next display frame.</p>
10658 *
10659 * <p>This method can be invoked from outside of the UI thread
10660 * only when this View is attached to a window.</p>
10661 *
10662 * @param left The left coordinate of the rectangle to invalidate.
10663 * @param top The top coordinate of the rectangle to invalidate.
10664 * @param right The right coordinate of the rectangle to invalidate.
10665 * @param bottom The bottom coordinate of the rectangle to invalidate.
10666 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010667 * @see #invalidate(int, int, int, int)
10668 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010669 */
10670 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10671 // We try only with the AttachInfo because there's no point in invalidating
10672 // if we are not attached to our window
10673 final AttachInfo attachInfo = mAttachInfo;
10674 if (attachInfo != null) {
10675 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10676 info.target = this;
10677 info.left = left;
10678 info.top = top;
10679 info.right = right;
10680 info.bottom = bottom;
10681
10682 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10683 }
10684 }
10685
10686 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010687 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10688 * This event is sent at most once every
10689 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10690 */
10691 private void postSendViewScrolledAccessibilityEventCallback() {
10692 if (mSendViewScrolledAccessibilityEvent == null) {
10693 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10694 }
10695 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10696 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10697 postDelayed(mSendViewScrolledAccessibilityEvent,
10698 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10699 }
10700 }
10701
10702 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010703 * Called by a parent to request that a child update its values for mScrollX
10704 * and mScrollY if necessary. This will typically be done if the child is
10705 * animating a scroll using a {@link android.widget.Scroller Scroller}
10706 * object.
10707 */
10708 public void computeScroll() {
10709 }
10710
10711 /**
10712 * <p>Indicate whether the horizontal edges are faded when the view is
10713 * scrolled horizontally.</p>
10714 *
10715 * @return true if the horizontal edges should are faded on scroll, false
10716 * otherwise
10717 *
10718 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010719 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010720 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010721 */
10722 public boolean isHorizontalFadingEdgeEnabled() {
10723 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10724 }
10725
10726 /**
10727 * <p>Define whether the horizontal edges should be faded when this view
10728 * is scrolled horizontally.</p>
10729 *
10730 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10731 * be faded when the view is scrolled
10732 * horizontally
10733 *
10734 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010735 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010736 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010737 */
10738 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10739 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10740 if (horizontalFadingEdgeEnabled) {
10741 initScrollCache();
10742 }
10743
10744 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10745 }
10746 }
10747
10748 /**
10749 * <p>Indicate whether the vertical edges are faded when the view is
10750 * scrolled horizontally.</p>
10751 *
10752 * @return true if the vertical edges should are faded on scroll, false
10753 * otherwise
10754 *
10755 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010756 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010757 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010758 */
10759 public boolean isVerticalFadingEdgeEnabled() {
10760 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10761 }
10762
10763 /**
10764 * <p>Define whether the vertical edges should be faded when this view
10765 * is scrolled vertically.</p>
10766 *
10767 * @param verticalFadingEdgeEnabled true if the vertical edges should
10768 * be faded when the view is scrolled
10769 * vertically
10770 *
10771 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010772 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010773 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010774 */
10775 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10776 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10777 if (verticalFadingEdgeEnabled) {
10778 initScrollCache();
10779 }
10780
10781 mViewFlags ^= FADING_EDGE_VERTICAL;
10782 }
10783 }
10784
10785 /**
10786 * Returns the strength, or intensity, of the top faded edge. The strength is
10787 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10788 * returns 0.0 or 1.0 but no value in between.
10789 *
10790 * Subclasses should override this method to provide a smoother fade transition
10791 * when scrolling occurs.
10792 *
10793 * @return the intensity of the top fade as a float between 0.0f and 1.0f
10794 */
10795 protected float getTopFadingEdgeStrength() {
10796 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
10797 }
10798
10799 /**
10800 * Returns the strength, or intensity, of the bottom faded edge. The strength is
10801 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10802 * returns 0.0 or 1.0 but no value in between.
10803 *
10804 * Subclasses should override this method to provide a smoother fade transition
10805 * when scrolling occurs.
10806 *
10807 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
10808 */
10809 protected float getBottomFadingEdgeStrength() {
10810 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
10811 computeVerticalScrollRange() ? 1.0f : 0.0f;
10812 }
10813
10814 /**
10815 * Returns the strength, or intensity, of the left faded edge. The strength is
10816 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10817 * returns 0.0 or 1.0 but no value in between.
10818 *
10819 * Subclasses should override this method to provide a smoother fade transition
10820 * when scrolling occurs.
10821 *
10822 * @return the intensity of the left fade as a float between 0.0f and 1.0f
10823 */
10824 protected float getLeftFadingEdgeStrength() {
10825 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
10826 }
10827
10828 /**
10829 * Returns the strength, or intensity, of the right faded edge. The strength is
10830 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10831 * returns 0.0 or 1.0 but no value in between.
10832 *
10833 * Subclasses should override this method to provide a smoother fade transition
10834 * when scrolling occurs.
10835 *
10836 * @return the intensity of the right fade as a float between 0.0f and 1.0f
10837 */
10838 protected float getRightFadingEdgeStrength() {
10839 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
10840 computeHorizontalScrollRange() ? 1.0f : 0.0f;
10841 }
10842
10843 /**
10844 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
10845 * scrollbar is not drawn by default.</p>
10846 *
10847 * @return true if the horizontal scrollbar should be painted, false
10848 * otherwise
10849 *
10850 * @see #setHorizontalScrollBarEnabled(boolean)
10851 */
10852 public boolean isHorizontalScrollBarEnabled() {
10853 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10854 }
10855
10856 /**
10857 * <p>Define whether the horizontal scrollbar should be drawn or not. The
10858 * scrollbar is not drawn by default.</p>
10859 *
10860 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
10861 * be painted
10862 *
10863 * @see #isHorizontalScrollBarEnabled()
10864 */
10865 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
10866 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
10867 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010868 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010869 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010870 }
10871 }
10872
10873 /**
10874 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
10875 * scrollbar is not drawn by default.</p>
10876 *
10877 * @return true if the vertical scrollbar should be painted, false
10878 * otherwise
10879 *
10880 * @see #setVerticalScrollBarEnabled(boolean)
10881 */
10882 public boolean isVerticalScrollBarEnabled() {
10883 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
10884 }
10885
10886 /**
10887 * <p>Define whether the vertical scrollbar should be drawn or not. The
10888 * scrollbar is not drawn by default.</p>
10889 *
10890 * @param verticalScrollBarEnabled true if the vertical scrollbar should
10891 * be painted
10892 *
10893 * @see #isVerticalScrollBarEnabled()
10894 */
10895 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
10896 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
10897 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010898 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010899 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010900 }
10901 }
10902
Adam Powell20232d02010-12-08 21:08:53 -080010903 /**
10904 * @hide
10905 */
10906 protected void recomputePadding() {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070010907 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010908 }
Joe Malin32736f02011-01-19 16:14:20 -080010909
Mike Cleronfe81d382009-09-28 14:22:16 -070010910 /**
10911 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080010912 *
Mike Cleronfe81d382009-09-28 14:22:16 -070010913 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080010914 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010915 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070010916 */
10917 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
10918 initScrollCache();
10919 final ScrollabilityCache scrollabilityCache = mScrollCache;
10920 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010921 if (fadeScrollbars) {
10922 scrollabilityCache.state = ScrollabilityCache.OFF;
10923 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070010924 scrollabilityCache.state = ScrollabilityCache.ON;
10925 }
10926 }
Joe Malin32736f02011-01-19 16:14:20 -080010927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010928 /**
Joe Malin32736f02011-01-19 16:14:20 -080010929 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010930 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080010931 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010932 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070010933 *
10934 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070010935 */
10936 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080010937 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010938 }
Joe Malin32736f02011-01-19 16:14:20 -080010939
Mike Cleron52f0a642009-09-28 18:21:37 -070010940 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070010941 *
10942 * Returns the delay before scrollbars fade.
10943 *
10944 * @return the delay before scrollbars fade
10945 *
10946 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10947 */
10948 public int getScrollBarDefaultDelayBeforeFade() {
10949 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
10950 mScrollCache.scrollBarDefaultDelayBeforeFade;
10951 }
10952
10953 /**
10954 * Define the delay before scrollbars fade.
10955 *
10956 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
10957 *
10958 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10959 */
10960 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
10961 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
10962 }
10963
10964 /**
10965 *
10966 * Returns the scrollbar fade duration.
10967 *
10968 * @return the scrollbar fade duration
10969 *
10970 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10971 */
10972 public int getScrollBarFadeDuration() {
10973 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
10974 mScrollCache.scrollBarFadeDuration;
10975 }
10976
10977 /**
10978 * Define the scrollbar fade duration.
10979 *
10980 * @param scrollBarFadeDuration - the scrollbar fade duration
10981 *
10982 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10983 */
10984 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
10985 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
10986 }
10987
10988 /**
10989 *
10990 * Returns the scrollbar size.
10991 *
10992 * @return the scrollbar size
10993 *
10994 * @attr ref android.R.styleable#View_scrollbarSize
10995 */
10996 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070010997 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070010998 mScrollCache.scrollBarSize;
10999 }
11000
11001 /**
11002 * Define the scrollbar size.
11003 *
11004 * @param scrollBarSize - the scrollbar size
11005 *
11006 * @attr ref android.R.styleable#View_scrollbarSize
11007 */
11008 public void setScrollBarSize(int scrollBarSize) {
11009 getScrollCache().scrollBarSize = scrollBarSize;
11010 }
11011
11012 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011013 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
11014 * inset. When inset, they add to the padding of the view. And the scrollbars
11015 * can be drawn inside the padding area or on the edge of the view. For example,
11016 * if a view has a background drawable and you want to draw the scrollbars
11017 * inside the padding specified by the drawable, you can use
11018 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
11019 * appear at the edge of the view, ignoring the padding, then you can use
11020 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
11021 * @param style the style of the scrollbars. Should be one of
11022 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
11023 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
11024 * @see #SCROLLBARS_INSIDE_OVERLAY
11025 * @see #SCROLLBARS_INSIDE_INSET
11026 * @see #SCROLLBARS_OUTSIDE_OVERLAY
11027 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070011028 *
11029 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011030 */
11031 public void setScrollBarStyle(int style) {
11032 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
11033 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070011034 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011035 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011036 }
11037 }
11038
11039 /**
11040 * <p>Returns the current scrollbar style.</p>
11041 * @return the current scrollbar style
11042 * @see #SCROLLBARS_INSIDE_OVERLAY
11043 * @see #SCROLLBARS_INSIDE_INSET
11044 * @see #SCROLLBARS_OUTSIDE_OVERLAY
11045 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070011046 *
11047 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011048 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070011049 @ViewDebug.ExportedProperty(mapping = {
11050 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
11051 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
11052 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
11053 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
11054 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011055 public int getScrollBarStyle() {
11056 return mViewFlags & SCROLLBARS_STYLE_MASK;
11057 }
11058
11059 /**
11060 * <p>Compute the horizontal range that the horizontal scrollbar
11061 * represents.</p>
11062 *
11063 * <p>The range is expressed in arbitrary units that must be the same as the
11064 * units used by {@link #computeHorizontalScrollExtent()} and
11065 * {@link #computeHorizontalScrollOffset()}.</p>
11066 *
11067 * <p>The default range is the drawing width of this view.</p>
11068 *
11069 * @return the total horizontal range represented by the horizontal
11070 * scrollbar
11071 *
11072 * @see #computeHorizontalScrollExtent()
11073 * @see #computeHorizontalScrollOffset()
11074 * @see android.widget.ScrollBarDrawable
11075 */
11076 protected int computeHorizontalScrollRange() {
11077 return getWidth();
11078 }
11079
11080 /**
11081 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
11082 * within the horizontal range. This value is used to compute the position
11083 * of the thumb within the scrollbar's track.</p>
11084 *
11085 * <p>The range is expressed in arbitrary units that must be the same as the
11086 * units used by {@link #computeHorizontalScrollRange()} and
11087 * {@link #computeHorizontalScrollExtent()}.</p>
11088 *
11089 * <p>The default offset is the scroll offset of this view.</p>
11090 *
11091 * @return the horizontal offset of the scrollbar's thumb
11092 *
11093 * @see #computeHorizontalScrollRange()
11094 * @see #computeHorizontalScrollExtent()
11095 * @see android.widget.ScrollBarDrawable
11096 */
11097 protected int computeHorizontalScrollOffset() {
11098 return mScrollX;
11099 }
11100
11101 /**
11102 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
11103 * within the horizontal range. This value is used to compute the length
11104 * of the thumb within the scrollbar's track.</p>
11105 *
11106 * <p>The range is expressed in arbitrary units that must be the same as the
11107 * units used by {@link #computeHorizontalScrollRange()} and
11108 * {@link #computeHorizontalScrollOffset()}.</p>
11109 *
11110 * <p>The default extent is the drawing width of this view.</p>
11111 *
11112 * @return the horizontal extent of the scrollbar's thumb
11113 *
11114 * @see #computeHorizontalScrollRange()
11115 * @see #computeHorizontalScrollOffset()
11116 * @see android.widget.ScrollBarDrawable
11117 */
11118 protected int computeHorizontalScrollExtent() {
11119 return getWidth();
11120 }
11121
11122 /**
11123 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
11124 *
11125 * <p>The range is expressed in arbitrary units that must be the same as the
11126 * units used by {@link #computeVerticalScrollExtent()} and
11127 * {@link #computeVerticalScrollOffset()}.</p>
11128 *
11129 * @return the total vertical range represented by the vertical scrollbar
11130 *
11131 * <p>The default range is the drawing height of this view.</p>
11132 *
11133 * @see #computeVerticalScrollExtent()
11134 * @see #computeVerticalScrollOffset()
11135 * @see android.widget.ScrollBarDrawable
11136 */
11137 protected int computeVerticalScrollRange() {
11138 return getHeight();
11139 }
11140
11141 /**
11142 * <p>Compute the vertical offset of the vertical scrollbar's thumb
11143 * within the horizontal range. This value is used to compute the position
11144 * of the thumb within the scrollbar's track.</p>
11145 *
11146 * <p>The range is expressed in arbitrary units that must be the same as the
11147 * units used by {@link #computeVerticalScrollRange()} and
11148 * {@link #computeVerticalScrollExtent()}.</p>
11149 *
11150 * <p>The default offset is the scroll offset of this view.</p>
11151 *
11152 * @return the vertical offset of the scrollbar's thumb
11153 *
11154 * @see #computeVerticalScrollRange()
11155 * @see #computeVerticalScrollExtent()
11156 * @see android.widget.ScrollBarDrawable
11157 */
11158 protected int computeVerticalScrollOffset() {
11159 return mScrollY;
11160 }
11161
11162 /**
11163 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
11164 * within the vertical range. This value is used to compute the length
11165 * of the thumb within the scrollbar's track.</p>
11166 *
11167 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080011168 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011169 * {@link #computeVerticalScrollOffset()}.</p>
11170 *
11171 * <p>The default extent is the drawing height of this view.</p>
11172 *
11173 * @return the vertical extent of the scrollbar's thumb
11174 *
11175 * @see #computeVerticalScrollRange()
11176 * @see #computeVerticalScrollOffset()
11177 * @see android.widget.ScrollBarDrawable
11178 */
11179 protected int computeVerticalScrollExtent() {
11180 return getHeight();
11181 }
11182
11183 /**
Adam Powell69159442011-06-13 17:53:06 -070011184 * Check if this view can be scrolled horizontally in a certain direction.
11185 *
11186 * @param direction Negative to check scrolling left, positive to check scrolling right.
11187 * @return true if this view can be scrolled in the specified direction, false otherwise.
11188 */
11189 public boolean canScrollHorizontally(int direction) {
11190 final int offset = computeHorizontalScrollOffset();
11191 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
11192 if (range == 0) return false;
11193 if (direction < 0) {
11194 return offset > 0;
11195 } else {
11196 return offset < range - 1;
11197 }
11198 }
11199
11200 /**
11201 * Check if this view can be scrolled vertically in a certain direction.
11202 *
11203 * @param direction Negative to check scrolling up, positive to check scrolling down.
11204 * @return true if this view can be scrolled in the specified direction, false otherwise.
11205 */
11206 public boolean canScrollVertically(int direction) {
11207 final int offset = computeVerticalScrollOffset();
11208 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
11209 if (range == 0) return false;
11210 if (direction < 0) {
11211 return offset > 0;
11212 } else {
11213 return offset < range - 1;
11214 }
11215 }
11216
11217 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011218 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
11219 * scrollbars are painted only if they have been awakened first.</p>
11220 *
11221 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080011222 *
Mike Cleronf116bf82009-09-27 19:14:12 -070011223 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011224 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080011225 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011226 // scrollbars are drawn only when the animation is running
11227 final ScrollabilityCache cache = mScrollCache;
11228 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080011229
Mike Cleronf116bf82009-09-27 19:14:12 -070011230 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080011231
Mike Cleronf116bf82009-09-27 19:14:12 -070011232 if (state == ScrollabilityCache.OFF) {
11233 return;
11234 }
Joe Malin32736f02011-01-19 16:14:20 -080011235
Mike Cleronf116bf82009-09-27 19:14:12 -070011236 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080011237
Mike Cleronf116bf82009-09-27 19:14:12 -070011238 if (state == ScrollabilityCache.FADING) {
11239 // We're fading -- get our fade interpolation
11240 if (cache.interpolatorValues == null) {
11241 cache.interpolatorValues = new float[1];
11242 }
Joe Malin32736f02011-01-19 16:14:20 -080011243
Mike Cleronf116bf82009-09-27 19:14:12 -070011244 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080011245
Mike Cleronf116bf82009-09-27 19:14:12 -070011246 // Stops the animation if we're done
11247 if (cache.scrollBarInterpolator.timeToValues(values) ==
11248 Interpolator.Result.FREEZE_END) {
11249 cache.state = ScrollabilityCache.OFF;
11250 } else {
11251 cache.scrollBar.setAlpha(Math.round(values[0]));
11252 }
Joe Malin32736f02011-01-19 16:14:20 -080011253
11254 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070011255 // drawing. We only want this when we're fading so that
11256 // we prevent excessive redraws
11257 invalidate = true;
11258 } else {
11259 // We're just on -- but we may have been fading before so
11260 // reset alpha
11261 cache.scrollBar.setAlpha(255);
11262 }
11263
Joe Malin32736f02011-01-19 16:14:20 -080011264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011265 final int viewFlags = mViewFlags;
11266
11267 final boolean drawHorizontalScrollBar =
11268 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
11269 final boolean drawVerticalScrollBar =
11270 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
11271 && !isVerticalScrollBarHidden();
11272
11273 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
11274 final int width = mRight - mLeft;
11275 final int height = mBottom - mTop;
11276
11277 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011278
Mike Reede8853fc2009-09-04 14:01:48 -040011279 final int scrollX = mScrollX;
11280 final int scrollY = mScrollY;
11281 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
11282
Mike Cleronf116bf82009-09-27 19:14:12 -070011283 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080011284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011285 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011286 int size = scrollBar.getSize(false);
11287 if (size <= 0) {
11288 size = cache.scrollBarSize;
11289 }
11290
Mike Cleronf116bf82009-09-27 19:14:12 -070011291 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040011292 computeHorizontalScrollOffset(),
11293 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040011294 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070011295 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080011296 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070011297 left = scrollX + (mPaddingLeft & inside);
11298 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
11299 bottom = top + size;
11300 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
11301 if (invalidate) {
11302 invalidate(left, top, right, bottom);
11303 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011304 }
11305
11306 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011307 int size = scrollBar.getSize(true);
11308 if (size <= 0) {
11309 size = cache.scrollBarSize;
11310 }
11311
Mike Reede8853fc2009-09-04 14:01:48 -040011312 scrollBar.setParameters(computeVerticalScrollRange(),
11313 computeVerticalScrollOffset(),
11314 computeVerticalScrollExtent(), true);
Fabrice Di Meglioc91b6ca2012-06-22 14:51:15 -070011315 int verticalScrollbarPosition = mVerticalScrollbarPosition;
11316 if (verticalScrollbarPosition == SCROLLBAR_POSITION_DEFAULT) {
11317 verticalScrollbarPosition = isLayoutRtl() ?
11318 SCROLLBAR_POSITION_LEFT : SCROLLBAR_POSITION_RIGHT;
11319 }
11320 switch (verticalScrollbarPosition) {
Adam Powell20232d02010-12-08 21:08:53 -080011321 default:
Adam Powell20232d02010-12-08 21:08:53 -080011322 case SCROLLBAR_POSITION_RIGHT:
11323 left = scrollX + width - size - (mUserPaddingRight & inside);
11324 break;
11325 case SCROLLBAR_POSITION_LEFT:
11326 left = scrollX + (mUserPaddingLeft & inside);
11327 break;
11328 }
Mike Cleronf116bf82009-09-27 19:14:12 -070011329 top = scrollY + (mPaddingTop & inside);
11330 right = left + size;
11331 bottom = scrollY + height - (mUserPaddingBottom & inside);
11332 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
11333 if (invalidate) {
11334 invalidate(left, top, right, bottom);
11335 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011336 }
11337 }
11338 }
11339 }
Romain Guy8506ab42009-06-11 17:35:47 -070011340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011341 /**
Romain Guy8506ab42009-06-11 17:35:47 -070011342 * 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 -080011343 * FastScroller is visible.
11344 * @return whether to temporarily hide the vertical scrollbar
11345 * @hide
11346 */
11347 protected boolean isVerticalScrollBarHidden() {
11348 return false;
11349 }
11350
11351 /**
11352 * <p>Draw the horizontal scrollbar if
11353 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
11354 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011355 * @param canvas the canvas on which to draw the scrollbar
11356 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011357 *
11358 * @see #isHorizontalScrollBarEnabled()
11359 * @see #computeHorizontalScrollRange()
11360 * @see #computeHorizontalScrollExtent()
11361 * @see #computeHorizontalScrollOffset()
11362 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070011363 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011364 */
Romain Guy8fb95422010-08-17 18:38:51 -070011365 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
11366 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011367 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011368 scrollBar.draw(canvas);
11369 }
Mike Reede8853fc2009-09-04 14:01:48 -040011370
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011371 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011372 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
11373 * returns true.</p>
11374 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011375 * @param canvas the canvas on which to draw the scrollbar
11376 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011377 *
11378 * @see #isVerticalScrollBarEnabled()
11379 * @see #computeVerticalScrollRange()
11380 * @see #computeVerticalScrollExtent()
11381 * @see #computeVerticalScrollOffset()
11382 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040011383 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011384 */
Romain Guy8fb95422010-08-17 18:38:51 -070011385 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
11386 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040011387 scrollBar.setBounds(l, t, r, b);
11388 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011389 }
11390
11391 /**
11392 * Implement this to do your drawing.
11393 *
11394 * @param canvas the canvas on which the background will be drawn
11395 */
11396 protected void onDraw(Canvas canvas) {
11397 }
11398
11399 /*
11400 * Caller is responsible for calling requestLayout if necessary.
11401 * (This allows addViewInLayout to not request a new layout.)
11402 */
11403 void assignParent(ViewParent parent) {
11404 if (mParent == null) {
11405 mParent = parent;
11406 } else if (parent == null) {
11407 mParent = null;
11408 } else {
11409 throw new RuntimeException("view " + this + " being added, but"
11410 + " it already has a parent");
11411 }
11412 }
11413
11414 /**
11415 * This is called when the view is attached to a window. At this point it
11416 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011417 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
11418 * however it may be called any time before the first onDraw -- including
11419 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011420 *
11421 * @see #onDetachedFromWindow()
11422 */
11423 protected void onAttachedToWindow() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011424 if ((mPrivateFlags & PFLAG_REQUEST_TRANSPARENT_REGIONS) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011425 mParent.requestTransparentRegion(this);
11426 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011427
Dianne Hackborn4702a852012-08-17 15:18:29 -070011428 if ((mPrivateFlags & PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
Adam Powell8568c3a2010-04-19 14:26:11 -070011429 initialAwakenScrollBars();
Dianne Hackborn4702a852012-08-17 15:18:29 -070011430 mPrivateFlags &= ~PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH;
Adam Powell8568c3a2010-04-19 14:26:11 -070011431 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011432
Chet Haasea9b61ac2010-12-20 07:40:25 -080011433 jumpDrawablesToCurrentState();
Romain Guy2a0f2282012-05-08 14:43:12 -070011434
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011435 resolveRtlProperties();
Romain Guy2a0f2282012-05-08 14:43:12 -070011436
Svetoslav Ganov42138042012-03-20 11:51:39 -070011437 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070011438 if (isFocused()) {
11439 InputMethodManager imm = InputMethodManager.peekInstance();
11440 imm.focusIn(this);
11441 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011442
11443 if (mAttachInfo != null && mDisplayList != null) {
11444 mAttachInfo.mViewRootImpl.dequeueDisplayList(mDisplayList);
11445 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011446 }
Cibu Johny86666632010-02-22 13:01:02 -080011447
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011448 void resolveRtlProperties() {
11449 // Order is important here: LayoutDirection MUST be resolved first...
11450 resolveLayoutDirection();
11451 // ... then we can resolve the others properties depending on the resolved LayoutDirection.
11452 resolvePadding();
11453 resolveLayoutParams();
11454 resolveTextDirection();
11455 resolveTextAlignment();
Fabrice Di Meglio0af4b8b2012-06-11 18:30:05 -070011456 resolveDrawables();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011457 }
11458
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011459 /**
Romain Guybb9908b2012-03-08 11:14:07 -080011460 * @see #onScreenStateChanged(int)
11461 */
11462 void dispatchScreenStateChanged(int screenState) {
11463 onScreenStateChanged(screenState);
11464 }
11465
11466 /**
11467 * This method is called whenever the state of the screen this view is
11468 * attached to changes. A state change will usually occurs when the screen
11469 * turns on or off (whether it happens automatically or the user does it
11470 * manually.)
11471 *
11472 * @param screenState The new state of the screen. Can be either
11473 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
11474 */
11475 public void onScreenStateChanged(int screenState) {
11476 }
11477
11478 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011479 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
11480 */
11481 private boolean hasRtlSupport() {
11482 return mContext.getApplicationInfo().hasRtlSupport();
11483 }
11484
11485 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011486 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
11487 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011488 * Will call {@link View#onResolvedLayoutDirectionChanged} when resolution is done.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011489 */
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011490 public void resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011491 // Clear any previous layout direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070011492 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011493
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011494 if (hasRtlSupport()) {
11495 // Set resolved depending on layout direction
11496 switch (getLayoutDirection()) {
11497 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliob93911f2012-06-26 19:43:15 -070011498 // We cannot resolve yet. LTR is by default and let the resolution happen again
11499 // later to get the correct resolved value
11500 if (!canResolveLayoutDirection()) return;
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011501
Fabrice Di Megliob93911f2012-06-26 19:43:15 -070011502 ViewGroup viewGroup = ((ViewGroup) mParent);
11503
11504 // We cannot resolve yet on the parent too. LTR is by default and let the
11505 // resolution happen again later
11506 if (!viewGroup.canResolveLayoutDirection()) return;
11507
11508 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011509 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011510 }
11511 break;
11512 case LAYOUT_DIRECTION_RTL:
Dianne Hackborn4702a852012-08-17 15:18:29 -070011513 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011514 break;
11515 case LAYOUT_DIRECTION_LOCALE:
11516 if(isLayoutDirectionRtl(Locale.getDefault())) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011517 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011518 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011519 break;
11520 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011521 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011522 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011523 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011524
11525 // Set to resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070011526 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011527 onResolvedLayoutDirectionChanged();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011528 }
11529
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011530 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011531 * Called when layout direction has been resolved.
11532 *
11533 * The default implementation does nothing.
11534 */
11535 public void onResolvedLayoutDirectionChanged() {
11536 }
11537
11538 /**
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011539 * Return if padding has been resolved
11540 */
11541 boolean isPaddingResolved() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011542 return (mPrivateFlags2 & PFLAG2_PADDING_RESOLVED) != 0;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011543 }
11544
11545 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011546 * Resolve padding depending on layout direction.
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011547 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011548 public void resolvePadding() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011549 final int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion;
11550 if (targetSdkVersion < JELLY_BEAN_MR1 || !hasRtlSupport()) {
11551 // Pre Jelly Bean MR1 case (compatibility mode) OR no RTL support case:
11552 // left / right padding are used if defined. If they are not defined and start / end
11553 // padding are defined (e.g. in Frameworks resources), then we use start / end and
11554 // resolve them as left / right (layout direction is not taken into account).
11555 if (!mUserPaddingLeftDefined && mUserPaddingStart != UNDEFINED_PADDING) {
11556 mUserPaddingLeft = mUserPaddingStart;
11557 }
11558 if (!mUserPaddingRightDefined && mUserPaddingEnd != UNDEFINED_PADDING) {
11559 mUserPaddingRight = mUserPaddingEnd;
11560 }
11561
11562 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
11563
11564 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight,
11565 mUserPaddingBottom);
11566 } else {
11567 // Post Jelly Bean MR1 case: we need to take the resolved layout direction into account.
11568 // If start / end padding are defined, they will be resolved (hence overriding) to
11569 // left / right or right / left depending on the resolved layout direction.
11570 // If start / end padding are not defined, use the left / right ones.
11571 int resolvedLayoutDirection = getResolvedLayoutDirection();
11572 switch (resolvedLayoutDirection) {
11573 case LAYOUT_DIRECTION_RTL:
11574 if (mUserPaddingStart != UNDEFINED_PADDING) {
11575 mUserPaddingRight = mUserPaddingStart;
11576 }
11577 if (mUserPaddingEnd != UNDEFINED_PADDING) {
11578 mUserPaddingLeft = mUserPaddingEnd;
11579 }
11580 break;
11581 case LAYOUT_DIRECTION_LTR:
11582 default:
11583 if (mUserPaddingStart != UNDEFINED_PADDING) {
11584 mUserPaddingLeft = mUserPaddingStart;
11585 }
11586 if (mUserPaddingEnd != UNDEFINED_PADDING) {
11587 mUserPaddingRight = mUserPaddingEnd;
11588 }
11589 }
11590
11591 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
11592
11593 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight,
11594 mUserPaddingBottom);
11595 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011596 }
11597
Dianne Hackborn4702a852012-08-17 15:18:29 -070011598 mPrivateFlags2 |= PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011599 }
11600
11601 /**
11602 * Resolve padding depending on the layout direction. Subclasses that care about
11603 * padding resolution should override this method. The default implementation does
11604 * nothing.
11605 *
11606 * @param layoutDirection the direction of the layout
11607 *
Fabrice Di Meglioe8dc07d2012-03-09 17:10:19 -080011608 * @see {@link #LAYOUT_DIRECTION_LTR}
11609 * @see {@link #LAYOUT_DIRECTION_RTL}
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011610 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011611 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011612 }
11613
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011614 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011615 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011616 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011617 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011618 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011619 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011620 switch (getLayoutDirection()) {
11621 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011622 return (mParent != null) && (mParent instanceof ViewGroup);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011623 default:
11624 return true;
11625 }
11626 }
11627
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011628 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011629 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
11630 * when reset is done.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011631 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011632 public void resetResolvedLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070011633 // Reset the current resolved bits
Dianne Hackborn4702a852012-08-17 15:18:29 -070011634 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011635 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080011636 // Reset also the text direction
11637 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011638 }
11639
11640 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011641 * Called during reset of resolved layout direction.
11642 *
11643 * Subclasses need to override this method to clear cached information that depends on the
11644 * resolved layout direction, or to inform child views that inherit their layout direction.
11645 *
11646 * The default implementation does nothing.
11647 */
11648 public void onResolvedLayoutDirectionReset() {
11649 }
11650
11651 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011652 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011653 *
11654 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011655 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011656 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011657 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -080011658 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011659 }
11660
11661 /**
11662 * This is called when the view is detached from a window. At this point it
11663 * no longer has a surface for drawing.
11664 *
11665 * @see #onAttachedToWindow()
11666 */
11667 protected void onDetachedFromWindow() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011668 mPrivateFlags &= ~PFLAG_CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080011669
Romain Guya440b002010-02-24 15:57:54 -080011670 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050011671 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080011672 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070011673 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080011674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011675 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011676
Romain Guya998dff2012-03-23 18:58:36 -070011677 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011678
11679 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011680 if (mDisplayList != null) {
Romain Guy2a0f2282012-05-08 14:43:12 -070011681 mAttachInfo.mViewRootImpl.enqueueDisplayList(mDisplayList);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011682 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011683 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011684 } else {
Romain Guy38c2ece2012-05-24 14:20:56 -070011685 // Should never happen
11686 clearDisplayList();
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011687 }
11688
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011689 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011690
11691 resetResolvedLayoutDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011692 resetResolvedTextAlignment();
Svetoslav Ganov42138042012-03-20 11:51:39 -070011693 resetAccessibilityStateChanged();
Dianne Hackborn4702a852012-08-17 15:18:29 -070011694 mPrivateFlags2 &= ~PFLAG2_PADDING_RESOLVED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011695 }
11696
11697 /**
11698 * @return The number of times this view has been attached to a window
11699 */
11700 protected int getWindowAttachCount() {
11701 return mWindowAttachCount;
11702 }
11703
11704 /**
11705 * Retrieve a unique token identifying the window this view is attached to.
11706 * @return Return the window's token for use in
11707 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11708 */
11709 public IBinder getWindowToken() {
11710 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11711 }
11712
11713 /**
11714 * Retrieve a unique token identifying the top-level "real" window of
11715 * the window that this view is attached to. That is, this is like
11716 * {@link #getWindowToken}, except if the window this view in is a panel
11717 * window (attached to another containing window), then the token of
11718 * the containing window is returned instead.
11719 *
11720 * @return Returns the associated window token, either
11721 * {@link #getWindowToken()} or the containing window's token.
11722 */
11723 public IBinder getApplicationWindowToken() {
11724 AttachInfo ai = mAttachInfo;
11725 if (ai != null) {
11726 IBinder appWindowToken = ai.mPanelParentWindowToken;
11727 if (appWindowToken == null) {
11728 appWindowToken = ai.mWindowToken;
11729 }
11730 return appWindowToken;
11731 }
11732 return null;
11733 }
11734
11735 /**
Jeff Brown98365d72012-08-19 20:30:52 -070011736 * Gets the logical display to which the view's window has been attached.
11737 *
11738 * @return The logical display, or null if the view is not currently attached to a window.
11739 */
11740 public Display getDisplay() {
11741 return mAttachInfo != null ? mAttachInfo.mDisplay : null;
11742 }
11743
11744 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011745 * Retrieve private session object this view hierarchy is using to
11746 * communicate with the window manager.
11747 * @return the session object to communicate with the window manager
11748 */
11749 /*package*/ IWindowSession getWindowSession() {
11750 return mAttachInfo != null ? mAttachInfo.mSession : null;
11751 }
11752
11753 /**
11754 * @param info the {@link android.view.View.AttachInfo} to associated with
11755 * this view
11756 */
11757 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
11758 //System.out.println("Attached! " + this);
11759 mAttachInfo = info;
11760 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011761 // We will need to evaluate the drawable state at least once.
Dianne Hackborn4702a852012-08-17 15:18:29 -070011762 mPrivateFlags |= PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011763 if (mFloatingTreeObserver != null) {
11764 info.mTreeObserver.merge(mFloatingTreeObserver);
11765 mFloatingTreeObserver = null;
11766 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070011767 if ((mPrivateFlags&PFLAG_SCROLL_CONTAINER) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011768 mAttachInfo.mScrollContainers.add(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -070011769 mPrivateFlags |= PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011770 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070011771 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011772 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080011773
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011774 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011775 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011776 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011777 if (listeners != null && listeners.size() > 0) {
11778 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11779 // perform the dispatching. The iterator is a safe guard against listeners that
11780 // could mutate the list by calling the various add/remove methods. This prevents
11781 // the array from being modified while we iterate it.
11782 for (OnAttachStateChangeListener listener : listeners) {
11783 listener.onViewAttachedToWindow(this);
11784 }
11785 }
11786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011787 int vis = info.mWindowVisibility;
11788 if (vis != GONE) {
11789 onWindowVisibilityChanged(vis);
11790 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070011791 if ((mPrivateFlags&PFLAG_DRAWABLE_STATE_DIRTY) != 0) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011792 // If nobody has evaluated the drawable state yet, then do it now.
11793 refreshDrawableState();
11794 }
Craig Mautner7eac0f52012-09-13 13:14:14 -070011795 needGlobalAttributesUpdate(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011796 }
11797
11798 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011799 AttachInfo info = mAttachInfo;
11800 if (info != null) {
11801 int vis = info.mWindowVisibility;
11802 if (vis != GONE) {
11803 onWindowVisibilityChanged(GONE);
11804 }
11805 }
11806
11807 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080011808
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011809 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011810 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011811 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011812 if (listeners != null && listeners.size() > 0) {
11813 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11814 // perform the dispatching. The iterator is a safe guard against listeners that
11815 // could mutate the list by calling the various add/remove methods. This prevents
11816 // the array from being modified while we iterate it.
11817 for (OnAttachStateChangeListener listener : listeners) {
11818 listener.onViewDetachedFromWindow(this);
11819 }
11820 }
11821
Dianne Hackborn4702a852012-08-17 15:18:29 -070011822 if ((mPrivateFlags & PFLAG_SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011823 mAttachInfo.mScrollContainers.remove(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -070011824 mPrivateFlags &= ~PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011825 }
Romain Guy01d5edc2011-01-28 11:28:53 -080011826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011827 mAttachInfo = null;
11828 }
11829
11830 /**
11831 * Store this view hierarchy's frozen state into the given container.
11832 *
11833 * @param container The SparseArray in which to save the view's state.
11834 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011835 * @see #restoreHierarchyState(android.util.SparseArray)
11836 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11837 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011838 */
11839 public void saveHierarchyState(SparseArray<Parcelable> container) {
11840 dispatchSaveInstanceState(container);
11841 }
11842
11843 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011844 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
11845 * this view and its children. May be overridden to modify how freezing happens to a
11846 * 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 -080011847 *
11848 * @param container The SparseArray in which to save the view's state.
11849 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011850 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11851 * @see #saveHierarchyState(android.util.SparseArray)
11852 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011853 */
11854 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
11855 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011856 mPrivateFlags &= ~PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011857 Parcelable state = onSaveInstanceState();
Dianne Hackborn4702a852012-08-17 15:18:29 -070011858 if ((mPrivateFlags & PFLAG_SAVE_STATE_CALLED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011859 throw new IllegalStateException(
11860 "Derived class did not call super.onSaveInstanceState()");
11861 }
11862 if (state != null) {
11863 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
11864 // + ": " + state);
11865 container.put(mID, state);
11866 }
11867 }
11868 }
11869
11870 /**
11871 * Hook allowing a view to generate a representation of its internal state
11872 * that can later be used to create a new instance with that same state.
11873 * This state should only contain information that is not persistent or can
11874 * not be reconstructed later. For example, you will never store your
11875 * current position on screen because that will be computed again when a
11876 * new instance of the view is placed in its view hierarchy.
11877 * <p>
11878 * Some examples of things you may store here: the current cursor position
11879 * in a text view (but usually not the text itself since that is stored in a
11880 * content provider or other persistent storage), the currently selected
11881 * item in a list view.
11882 *
11883 * @return Returns a Parcelable object containing the view's current dynamic
11884 * state, or null if there is nothing interesting to save. The
11885 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070011886 * @see #onRestoreInstanceState(android.os.Parcelable)
11887 * @see #saveHierarchyState(android.util.SparseArray)
11888 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011889 * @see #setSaveEnabled(boolean)
11890 */
11891 protected Parcelable onSaveInstanceState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011892 mPrivateFlags |= PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011893 return BaseSavedState.EMPTY_STATE;
11894 }
11895
11896 /**
11897 * Restore this view hierarchy's frozen state from the given container.
11898 *
11899 * @param container The SparseArray which holds previously frozen states.
11900 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011901 * @see #saveHierarchyState(android.util.SparseArray)
11902 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11903 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011904 */
11905 public void restoreHierarchyState(SparseArray<Parcelable> container) {
11906 dispatchRestoreInstanceState(container);
11907 }
11908
11909 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011910 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
11911 * state for this view and its children. May be overridden to modify how restoring
11912 * happens to a view's children; for example, some views may want to not store state
11913 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011914 *
11915 * @param container The SparseArray which holds previously saved state.
11916 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011917 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11918 * @see #restoreHierarchyState(android.util.SparseArray)
11919 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011920 */
11921 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
11922 if (mID != NO_ID) {
11923 Parcelable state = container.get(mID);
11924 if (state != null) {
11925 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
11926 // + ": " + state);
Dianne Hackborn4702a852012-08-17 15:18:29 -070011927 mPrivateFlags &= ~PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011928 onRestoreInstanceState(state);
Dianne Hackborn4702a852012-08-17 15:18:29 -070011929 if ((mPrivateFlags & PFLAG_SAVE_STATE_CALLED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011930 throw new IllegalStateException(
11931 "Derived class did not call super.onRestoreInstanceState()");
11932 }
11933 }
11934 }
11935 }
11936
11937 /**
11938 * Hook allowing a view to re-apply a representation of its internal state that had previously
11939 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
11940 * null state.
11941 *
11942 * @param state The frozen state that had previously been returned by
11943 * {@link #onSaveInstanceState}.
11944 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011945 * @see #onSaveInstanceState()
11946 * @see #restoreHierarchyState(android.util.SparseArray)
11947 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011948 */
11949 protected void onRestoreInstanceState(Parcelable state) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011950 mPrivateFlags |= PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011951 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080011952 throw new IllegalArgumentException("Wrong state class, expecting View State but "
11953 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080011954 + "when two views of different type have the same id in the same hierarchy. "
11955 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080011956 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011957 }
11958 }
11959
11960 /**
11961 * <p>Return the time at which the drawing of the view hierarchy started.</p>
11962 *
11963 * @return the drawing start time in milliseconds
11964 */
11965 public long getDrawingTime() {
11966 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
11967 }
11968
11969 /**
11970 * <p>Enables or disables the duplication of the parent's state into this view. When
11971 * duplication is enabled, this view gets its drawable state from its parent rather
11972 * than from its own internal properties.</p>
11973 *
11974 * <p>Note: in the current implementation, setting this property to true after the
11975 * view was added to a ViewGroup might have no effect at all. This property should
11976 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
11977 *
11978 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
11979 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011980 *
Gilles Debunnefb817032011-01-13 13:52:49 -080011981 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
11982 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011983 *
11984 * @param enabled True to enable duplication of the parent's drawable state, false
11985 * to disable it.
11986 *
11987 * @see #getDrawableState()
11988 * @see #isDuplicateParentStateEnabled()
11989 */
11990 public void setDuplicateParentStateEnabled(boolean enabled) {
11991 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
11992 }
11993
11994 /**
11995 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
11996 *
11997 * @return True if this view's drawable state is duplicated from the parent,
11998 * false otherwise
11999 *
12000 * @see #getDrawableState()
12001 * @see #setDuplicateParentStateEnabled(boolean)
12002 */
12003 public boolean isDuplicateParentStateEnabled() {
12004 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
12005 }
12006
12007 /**
Romain Guy171c5922011-01-06 10:04:23 -080012008 * <p>Specifies the type of layer backing this view. The layer can be
12009 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
12010 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012011 *
Romain Guy171c5922011-01-06 10:04:23 -080012012 * <p>A layer is associated with an optional {@link android.graphics.Paint}
12013 * instance that controls how the layer is composed on screen. The following
12014 * properties of the paint are taken into account when composing the layer:</p>
12015 * <ul>
12016 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
12017 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
12018 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
12019 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080012020 *
Romain Guy171c5922011-01-06 10:04:23 -080012021 * <p>If this view has an alpha value set to < 1.0 by calling
12022 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
12023 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
12024 * equivalent to setting a hardware layer on this view and providing a paint with
Chet Haased15ebf22012-09-05 11:40:29 -070012025 * the desired alpha value.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012026 *
Romain Guy171c5922011-01-06 10:04:23 -080012027 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
12028 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
12029 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012030 *
Chet Haased15ebf22012-09-05 11:40:29 -070012031 * @param layerType The type of layer to use with this view, must be one of
Romain Guy171c5922011-01-06 10:04:23 -080012032 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
12033 * {@link #LAYER_TYPE_HARDWARE}
12034 * @param paint The paint used to compose the layer. This argument is optional
12035 * and can be null. It is ignored when the layer type is
12036 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080012037 *
12038 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080012039 * @see #LAYER_TYPE_NONE
12040 * @see #LAYER_TYPE_SOFTWARE
12041 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080012042 * @see #setAlpha(float)
12043 *
Romain Guy171c5922011-01-06 10:04:23 -080012044 * @attr ref android.R.styleable#View_layerType
12045 */
12046 public void setLayerType(int layerType, Paint paint) {
12047 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080012048 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080012049 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
12050 }
Chet Haasedaf98e92011-01-10 14:10:36 -080012051
Romain Guyd6cd5722011-01-17 14:42:41 -080012052 if (layerType == mLayerType) {
12053 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
12054 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012055 invalidateParentCaches();
12056 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080012057 }
12058 return;
12059 }
Romain Guy171c5922011-01-06 10:04:23 -080012060
12061 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080012062 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070012063 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070012064 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012065 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080012066 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070012067 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080012068 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080012069 default:
12070 break;
Romain Guy171c5922011-01-06 10:04:23 -080012071 }
12072
12073 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080012074 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
12075 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
12076 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080012077
Romain Guy0fd89bf2011-01-26 15:41:30 -080012078 invalidateParentCaches();
12079 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080012080 }
12081
12082 /**
Chet Haased15ebf22012-09-05 11:40:29 -070012083 * Updates the {@link Paint} object used with the current layer (used only if the current
12084 * layer type is not set to {@link #LAYER_TYPE_NONE}). Changed properties of the Paint
12085 * provided to {@link #setLayerType(int, android.graphics.Paint)} will be used the next time
12086 * the View is redrawn, but {@link #setLayerPaint(android.graphics.Paint)} must be called to
12087 * ensure that the view gets redrawn immediately.
12088 *
12089 * <p>A layer is associated with an optional {@link android.graphics.Paint}
12090 * instance that controls how the layer is composed on screen. The following
12091 * properties of the paint are taken into account when composing the layer:</p>
12092 * <ul>
12093 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
12094 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
12095 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
12096 * </ul>
12097 *
12098 * <p>If this view has an alpha value set to < 1.0 by calling
12099 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
12100 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
12101 * equivalent to setting a hardware layer on this view and providing a paint with
12102 * the desired alpha value.</p>
12103 *
12104 * @param paint The paint used to compose the layer. This argument is optional
12105 * and can be null. It is ignored when the layer type is
12106 * {@link #LAYER_TYPE_NONE}
12107 *
12108 * @see #setLayerType(int, android.graphics.Paint)
12109 */
12110 public void setLayerPaint(Paint paint) {
12111 int layerType = getLayerType();
12112 if (layerType != LAYER_TYPE_NONE) {
12113 mLayerPaint = paint == null ? new Paint() : paint;
12114 if (layerType == LAYER_TYPE_HARDWARE) {
12115 HardwareLayer layer = getHardwareLayer();
12116 if (layer != null) {
12117 layer.setLayerPaint(paint);
12118 }
12119 invalidateViewProperty(false, false);
12120 } else {
12121 invalidate();
12122 }
12123 }
12124 }
12125
12126 /**
Romain Guy59c7f802011-09-29 17:21:45 -070012127 * Indicates whether this view has a static layer. A view with layer type
12128 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
12129 * dynamic.
12130 */
12131 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080012132 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070012133 }
12134
12135 /**
Romain Guy171c5922011-01-06 10:04:23 -080012136 * Indicates what type of layer is currently associated with this view. By default
12137 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
12138 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
12139 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080012140 *
Romain Guy171c5922011-01-06 10:04:23 -080012141 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
12142 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080012143 *
12144 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070012145 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080012146 * @see #LAYER_TYPE_NONE
12147 * @see #LAYER_TYPE_SOFTWARE
12148 * @see #LAYER_TYPE_HARDWARE
12149 */
12150 public int getLayerType() {
12151 return mLayerType;
12152 }
Joe Malin32736f02011-01-19 16:14:20 -080012153
Romain Guy6c319ca2011-01-11 14:29:25 -080012154 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080012155 * Forces this view's layer to be created and this view to be rendered
12156 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
12157 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070012158 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012159 * This method can for instance be used to render a view into its layer before
12160 * starting an animation. If this view is complex, rendering into the layer
12161 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070012162 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012163 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070012164 *
12165 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080012166 */
12167 public void buildLayer() {
12168 if (mLayerType == LAYER_TYPE_NONE) return;
12169
12170 if (mAttachInfo == null) {
12171 throw new IllegalStateException("This view must be attached to a window first");
12172 }
12173
12174 switch (mLayerType) {
12175 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080012176 if (mAttachInfo.mHardwareRenderer != null &&
12177 mAttachInfo.mHardwareRenderer.isEnabled() &&
12178 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080012179 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080012180 }
Romain Guyf1ae1062011-03-02 18:16:04 -080012181 break;
12182 case LAYER_TYPE_SOFTWARE:
12183 buildDrawingCache(true);
12184 break;
12185 }
12186 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012187
Romain Guyf1ae1062011-03-02 18:16:04 -080012188 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080012189 * <p>Returns a hardware layer that can be used to draw this view again
12190 * without executing its draw method.</p>
12191 *
12192 * @return A HardwareLayer ready to render, or null if an error occurred.
12193 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080012194 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070012195 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
12196 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080012197 return null;
12198 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012199
Romain Guy9c4b79a2011-11-10 19:23:58 -080012200 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080012201
12202 final int width = mRight - mLeft;
12203 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080012204
Romain Guy6c319ca2011-01-11 14:29:25 -080012205 if (width == 0 || height == 0) {
12206 return null;
12207 }
12208
Dianne Hackborn4702a852012-08-17 15:18:29 -070012209 if ((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
Romain Guy6c319ca2011-01-11 14:29:25 -080012210 if (mHardwareLayer == null) {
12211 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
12212 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070012213 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy846a5332012-07-11 17:44:57 -070012214 } else {
12215 if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
Chet Haase603f6de2012-09-14 15:31:25 -070012216 if (mHardwareLayer.resize(width, height)) {
12217 mLocalDirtyRect.set(0, 0, width, height);
12218 }
Romain Guy846a5332012-07-11 17:44:57 -070012219 }
12220
12221 // This should not be necessary but applications that change
12222 // the parameters of their background drawable without calling
12223 // this.setBackground(Drawable) can leave the view in a bad state
12224 // (for instance isOpaque() returns true, but the background is
12225 // not opaque.)
12226 computeOpaqueFlags();
12227
12228 final boolean opaque = isOpaque();
Chet Haase603f6de2012-09-14 15:31:25 -070012229 if (mHardwareLayer.isValid() && mHardwareLayer.isOpaque() != opaque) {
Romain Guy846a5332012-07-11 17:44:57 -070012230 mHardwareLayer.setOpaque(opaque);
12231 mLocalDirtyRect.set(0, 0, width, height);
12232 }
Romain Guy6c319ca2011-01-11 14:29:25 -080012233 }
12234
Romain Guy5cd5c3f2011-10-17 17:10:02 -070012235 // The layer is not valid if the underlying GPU resources cannot be allocated
12236 if (!mHardwareLayer.isValid()) {
12237 return null;
12238 }
Chet Haased15ebf22012-09-05 11:40:29 -070012239 mHardwareLayer.setLayerPaint(mLayerPaint);
Romain Guy5cd5c3f2011-10-17 17:10:02 -070012240
Chet Haasea1cff502012-02-21 13:43:44 -080012241 mHardwareLayer.redraw(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
Michael Jurka7e52caf2012-03-06 15:57:06 -080012242 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080012243 }
12244
12245 return mHardwareLayer;
12246 }
Romain Guy171c5922011-01-06 10:04:23 -080012247
Romain Guy589b0bb2011-10-10 13:57:47 -070012248 /**
12249 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070012250 *
Romain Guy589b0bb2011-10-10 13:57:47 -070012251 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070012252 *
12253 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070012254 * @see #LAYER_TYPE_HARDWARE
12255 */
Romain Guya998dff2012-03-23 18:58:36 -070012256 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070012257 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012258 AttachInfo info = mAttachInfo;
12259 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070012260 info.mHardwareRenderer.isEnabled() &&
12261 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012262 mHardwareLayer.destroy();
12263 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080012264
Romain Guy9c4b79a2011-11-10 19:23:58 -080012265 invalidate(true);
12266 invalidateParentCaches();
12267 }
Romain Guy65b345f2011-07-27 18:51:50 -070012268 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070012269 }
Romain Guy65b345f2011-07-27 18:51:50 -070012270 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070012271 }
12272
Romain Guy171c5922011-01-06 10:04:23 -080012273 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080012274 * Destroys all hardware rendering resources. This method is invoked
12275 * when the system needs to reclaim resources. Upon execution of this
12276 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070012277 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012278 * Note: you <strong>must</strong> call
12279 * <code>super.destroyHardwareResources()</code> when overriding
12280 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070012281 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012282 * @hide
12283 */
12284 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070012285 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012286 }
12287
12288 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012289 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
12290 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
12291 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
12292 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
12293 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
12294 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012295 *
Romain Guy171c5922011-01-06 10:04:23 -080012296 * <p>Enabling the drawing cache is similar to
12297 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080012298 * acceleration is turned off. When hardware acceleration is turned on, enabling the
12299 * drawing cache has no effect on rendering because the system uses a different mechanism
12300 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
12301 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
12302 * for information on how to enable software and hardware layers.</p>
12303 *
12304 * <p>This API can be used to manually generate
12305 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
12306 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012307 *
12308 * @param enabled true to enable the drawing cache, false otherwise
12309 *
12310 * @see #isDrawingCacheEnabled()
12311 * @see #getDrawingCache()
12312 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080012313 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012314 */
12315 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012316 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012317 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
12318 }
12319
12320 /**
12321 * <p>Indicates whether the drawing cache is enabled for this view.</p>
12322 *
12323 * @return true if the drawing cache is enabled
12324 *
12325 * @see #setDrawingCacheEnabled(boolean)
12326 * @see #getDrawingCache()
12327 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012328 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012329 public boolean isDrawingCacheEnabled() {
12330 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
12331 }
12332
12333 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080012334 * Debugging utility which recursively outputs the dirty state of a view and its
12335 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080012336 *
Chet Haasedaf98e92011-01-10 14:10:36 -080012337 * @hide
12338 */
Romain Guy676b1732011-02-14 14:45:33 -080012339 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080012340 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012341 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.PFLAG_DIRTY_MASK) +
12342 ") DRAWN(" + (mPrivateFlags & PFLAG_DRAWN) + ")" + " CACHE_VALID(" +
12343 (mPrivateFlags & View.PFLAG_DRAWING_CACHE_VALID) +
12344 ") INVALIDATED(" + (mPrivateFlags & PFLAG_INVALIDATED) + ")");
Chet Haasedaf98e92011-01-10 14:10:36 -080012345 if (clear) {
12346 mPrivateFlags &= clearMask;
12347 }
12348 if (this instanceof ViewGroup) {
12349 ViewGroup parent = (ViewGroup) this;
12350 final int count = parent.getChildCount();
12351 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080012352 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080012353 child.outputDirtyFlags(indent + " ", clear, clearMask);
12354 }
12355 }
12356 }
12357
12358 /**
12359 * This method is used by ViewGroup to cause its children to restore or recreate their
12360 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
12361 * to recreate its own display list, which would happen if it went through the normal
12362 * draw/dispatchDraw mechanisms.
12363 *
12364 * @hide
12365 */
12366 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080012367
12368 /**
12369 * A view that is not attached or hardware accelerated cannot create a display list.
12370 * This method checks these conditions and returns the appropriate result.
12371 *
12372 * @return true if view has the ability to create a display list, false otherwise.
12373 *
12374 * @hide
12375 */
12376 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080012377 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080012378 }
Joe Malin32736f02011-01-19 16:14:20 -080012379
Chet Haasedaf98e92011-01-10 14:10:36 -080012380 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080012381 * @return The HardwareRenderer associated with that view or null if hardware rendering
12382 * is not supported or this this has not been attached to a window.
12383 *
12384 * @hide
12385 */
12386 public HardwareRenderer getHardwareRenderer() {
12387 if (mAttachInfo != null) {
12388 return mAttachInfo.mHardwareRenderer;
12389 }
12390 return null;
12391 }
12392
12393 /**
Chet Haasea1cff502012-02-21 13:43:44 -080012394 * Returns a DisplayList. If the incoming displayList is null, one will be created.
12395 * Otherwise, the same display list will be returned (after having been rendered into
12396 * along the way, depending on the invalidation state of the view).
12397 *
12398 * @param displayList The previous version of this displayList, could be null.
12399 * @param isLayer Whether the requester of the display list is a layer. If so,
12400 * the view will avoid creating a layer inside the resulting display list.
12401 * @return A new or reused DisplayList object.
12402 */
12403 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
12404 if (!canHaveDisplayList()) {
12405 return null;
12406 }
12407
Dianne Hackborn4702a852012-08-17 15:18:29 -070012408 if (((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 ||
Chet Haasea1cff502012-02-21 13:43:44 -080012409 displayList == null || !displayList.isValid() ||
12410 (!isLayer && mRecreateDisplayList))) {
12411 // Don't need to recreate the display list, just need to tell our
12412 // children to restore/recreate theirs
12413 if (displayList != null && displayList.isValid() &&
12414 !isLayer && !mRecreateDisplayList) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012415 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12416 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012417 dispatchGetDisplayList();
12418
12419 return displayList;
12420 }
12421
12422 if (!isLayer) {
12423 // If we got here, we're recreating it. Mark it as such to ensure that
12424 // we copy in child display lists into ours in drawChild()
12425 mRecreateDisplayList = true;
12426 }
12427 if (displayList == null) {
12428 final String name = getClass().getSimpleName();
12429 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
12430 // If we're creating a new display list, make sure our parent gets invalidated
12431 // since they will need to recreate their display list to account for this
12432 // new child display list.
12433 invalidateParentCaches();
12434 }
12435
12436 boolean caching = false;
12437 final HardwareCanvas canvas = displayList.start();
Chet Haasea1cff502012-02-21 13:43:44 -080012438 int width = mRight - mLeft;
12439 int height = mBottom - mTop;
12440
12441 try {
12442 canvas.setViewport(width, height);
12443 // The dirty rect should always be null for a display list
12444 canvas.onPreDraw(null);
Michael Jurkaba649742012-06-28 19:12:58 -070012445 int layerType = getLayerType();
Chet Haase1271e2c2012-04-20 09:54:27 -070012446 if (!isLayer && layerType != LAYER_TYPE_NONE) {
Chet Haaseb85967b2012-03-26 14:37:51 -070012447 if (layerType == LAYER_TYPE_HARDWARE) {
12448 final HardwareLayer layer = getHardwareLayer();
12449 if (layer != null && layer.isValid()) {
12450 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
12451 } else {
12452 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
12453 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
12454 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12455 }
12456 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080012457 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070012458 buildDrawingCache(true);
12459 Bitmap cache = getDrawingCache(true);
12460 if (cache != null) {
12461 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
12462 caching = true;
12463 }
Chet Haasea1cff502012-02-21 13:43:44 -080012464 }
Chet Haasea1cff502012-02-21 13:43:44 -080012465 } else {
12466
12467 computeScroll();
12468
Chet Haasea1cff502012-02-21 13:43:44 -080012469 canvas.translate(-mScrollX, -mScrollY);
12470 if (!isLayer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012471 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12472 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012473 }
12474
12475 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070012476 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
Chet Haasea1cff502012-02-21 13:43:44 -080012477 dispatchDraw(canvas);
12478 } else {
12479 draw(canvas);
12480 }
12481 }
12482 } finally {
Chet Haasea1cff502012-02-21 13:43:44 -080012483 canvas.onPostDraw();
12484
12485 displayList.end();
Chet Haase1271e2c2012-04-20 09:54:27 -070012486 displayList.setCaching(caching);
12487 if (isLayer) {
Chet Haasea1cff502012-02-21 13:43:44 -080012488 displayList.setLeftTopRightBottom(0, 0, width, height);
12489 } else {
12490 setDisplayListProperties(displayList);
12491 }
12492 }
12493 } else if (!isLayer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012494 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12495 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012496 }
12497
12498 return displayList;
12499 }
12500
12501 /**
12502 * Get the DisplayList for the HardwareLayer
12503 *
12504 * @param layer The HardwareLayer whose DisplayList we want
12505 * @return A DisplayList fopr the specified HardwareLayer
12506 */
12507 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
12508 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
12509 layer.setDisplayList(displayList);
12510 return displayList;
12511 }
12512
12513
12514 /**
Romain Guyb051e892010-09-28 19:09:36 -070012515 * <p>Returns a display list that can be used to draw this view again
12516 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012517 *
Romain Guyb051e892010-09-28 19:09:36 -070012518 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080012519 *
12520 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070012521 */
Chet Haasedaf98e92011-01-10 14:10:36 -080012522 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080012523 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070012524 return mDisplayList;
12525 }
12526
Romain Guy38c2ece2012-05-24 14:20:56 -070012527 private void clearDisplayList() {
12528 if (mDisplayList != null) {
12529 mDisplayList.invalidate();
12530 mDisplayList.clear();
12531 }
12532 }
12533
Romain Guyb051e892010-09-28 19:09:36 -070012534 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012535 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012536 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012537 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012538 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012539 * @see #getDrawingCache(boolean)
12540 */
12541 public Bitmap getDrawingCache() {
12542 return getDrawingCache(false);
12543 }
12544
12545 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012546 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
12547 * is null when caching is disabled. If caching is enabled and the cache is not ready,
12548 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
12549 * draw from the cache when the cache is enabled. To benefit from the cache, you must
12550 * request the drawing cache by calling this method and draw it on screen if the
12551 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012552 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012553 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12554 * this method will create a bitmap of the same size as this view. Because this bitmap
12555 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12556 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12557 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12558 * size than the view. This implies that your application must be able to handle this
12559 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012560 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012561 * @param autoScale Indicates whether the generated bitmap should be scaled based on
12562 * the current density of the screen when the application is in compatibility
12563 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012564 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012565 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012566 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012567 * @see #setDrawingCacheEnabled(boolean)
12568 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070012569 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012570 * @see #destroyDrawingCache()
12571 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012572 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012573 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
12574 return null;
12575 }
12576 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012577 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012578 }
Romain Guy02890fd2010-08-06 17:58:44 -070012579 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012580 }
12581
12582 /**
12583 * <p>Frees the resources used by the drawing cache. If you call
12584 * {@link #buildDrawingCache()} manually without calling
12585 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12586 * should cleanup the cache with this method afterwards.</p>
12587 *
12588 * @see #setDrawingCacheEnabled(boolean)
12589 * @see #buildDrawingCache()
12590 * @see #getDrawingCache()
12591 */
12592 public void destroyDrawingCache() {
12593 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012594 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012595 mDrawingCache = null;
12596 }
Romain Guyfbd8f692009-06-26 14:51:58 -070012597 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012598 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070012599 mUnscaledDrawingCache = null;
12600 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012601 }
12602
12603 /**
12604 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070012605 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012606 * view will always be drawn on top of a solid color.
12607 *
12608 * @param color The background color to use for the drawing cache's bitmap
12609 *
12610 * @see #setDrawingCacheEnabled(boolean)
12611 * @see #buildDrawingCache()
12612 * @see #getDrawingCache()
12613 */
12614 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080012615 if (color != mDrawingCacheBackgroundColor) {
12616 mDrawingCacheBackgroundColor = color;
Dianne Hackborn4702a852012-08-17 15:18:29 -070012617 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Romain Guy52e2ef82010-01-14 12:11:48 -080012618 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012619 }
12620
12621 /**
12622 * @see #setDrawingCacheBackgroundColor(int)
12623 *
12624 * @return The background color to used for the drawing cache's bitmap
12625 */
12626 public int getDrawingCacheBackgroundColor() {
12627 return mDrawingCacheBackgroundColor;
12628 }
12629
12630 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012631 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012632 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012633 * @see #buildDrawingCache(boolean)
12634 */
12635 public void buildDrawingCache() {
12636 buildDrawingCache(false);
12637 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080012638
Romain Guyfbd8f692009-06-26 14:51:58 -070012639 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012640 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
12641 *
12642 * <p>If you call {@link #buildDrawingCache()} manually without calling
12643 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12644 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012645 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012646 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12647 * this method will create a bitmap of the same size as this view. Because this bitmap
12648 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12649 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12650 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12651 * size than the view. This implies that your application must be able to handle this
12652 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012653 *
Romain Guy0d9275e2010-10-26 14:22:30 -070012654 * <p>You should avoid calling this method when hardware acceleration is enabled. If
12655 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080012656 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070012657 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012658 *
12659 * @see #getDrawingCache()
12660 * @see #destroyDrawingCache()
12661 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012662 public void buildDrawingCache(boolean autoScale) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012663 if ((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070012664 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012665 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012666
Romain Guy8506ab42009-06-11 17:35:47 -070012667 int width = mRight - mLeft;
12668 int height = mBottom - mTop;
12669
12670 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070012671 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070012672
Romain Guye1123222009-06-29 14:24:56 -070012673 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012674 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
12675 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070012676 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012677
12678 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070012679 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080012680 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012681
Chris Craik10e9d1d2012-09-06 14:42:44 -070012682 final int projectedBitmapSize = width * height * (opaque && !use32BitCache ? 2 : 4);
12683 final int drawingCacheSize =
12684 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize();
12685 if (width <= 0 || height <= 0 || projectedBitmapSize > drawingCacheSize) {
Chris Craik3667aa32012-09-06 14:56:39 -070012686 if (width > 0 && height > 0) {
12687 Log.w(VIEW_LOG_TAG, "View too large to fit into drawing cache, needs "
12688 + projectedBitmapSize + " bytes, only "
12689 + drawingCacheSize + " available");
12690 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012691 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080012692 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012693 return;
12694 }
12695
12696 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070012697 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012698
12699 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012700 Bitmap.Config quality;
12701 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080012702 // Never pick ARGB_4444 because it looks awful
12703 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012704 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
12705 case DRAWING_CACHE_QUALITY_AUTO:
12706 quality = Bitmap.Config.ARGB_8888;
12707 break;
12708 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080012709 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012710 break;
12711 case DRAWING_CACHE_QUALITY_HIGH:
12712 quality = Bitmap.Config.ARGB_8888;
12713 break;
12714 default:
12715 quality = Bitmap.Config.ARGB_8888;
12716 break;
12717 }
12718 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070012719 // Optimization for translucent windows
12720 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080012721 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012722 }
12723
12724 // Try to cleanup memory
12725 if (bitmap != null) bitmap.recycle();
12726
12727 try {
Dianne Hackborndde331c2012-08-03 14:01:57 -070012728 bitmap = Bitmap.createBitmap(mResources.getDisplayMetrics(),
12729 width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070012730 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070012731 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070012732 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012733 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070012734 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012735 }
Adam Powell26153a32010-11-08 15:22:27 -080012736 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012737 } catch (OutOfMemoryError e) {
12738 // If there is not enough memory to create the bitmap cache, just
12739 // ignore the issue as bitmap caches are not required to draw the
12740 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012741 if (autoScale) {
12742 mDrawingCache = null;
12743 } else {
12744 mUnscaledDrawingCache = null;
12745 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012746 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012747 return;
12748 }
12749
12750 clear = drawingCacheBackgroundColor != 0;
12751 }
12752
12753 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012754 if (attachInfo != null) {
12755 canvas = attachInfo.mCanvas;
12756 if (canvas == null) {
12757 canvas = new Canvas();
12758 }
12759 canvas.setBitmap(bitmap);
12760 // Temporarily clobber the cached Canvas in case one of our children
12761 // is also using a drawing cache. Without this, the children would
12762 // steal the canvas by attaching their own bitmap to it and bad, bad
12763 // thing would happen (invisible views, corrupted drawings, etc.)
12764 attachInfo.mCanvas = null;
12765 } else {
12766 // This case should hopefully never or seldom happen
12767 canvas = new Canvas(bitmap);
12768 }
12769
12770 if (clear) {
12771 bitmap.eraseColor(drawingCacheBackgroundColor);
12772 }
12773
12774 computeScroll();
12775 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080012776
Romain Guye1123222009-06-29 14:24:56 -070012777 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012778 final float scale = attachInfo.mApplicationScale;
12779 canvas.scale(scale, scale);
12780 }
Joe Malin32736f02011-01-19 16:14:20 -080012781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012782 canvas.translate(-mScrollX, -mScrollY);
12783
Dianne Hackborn4702a852012-08-17 15:18:29 -070012784 mPrivateFlags |= PFLAG_DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080012785 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
12786 mLayerType != LAYER_TYPE_NONE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012787 mPrivateFlags |= PFLAG_DRAWING_CACHE_VALID;
Romain Guy0d9275e2010-10-26 14:22:30 -070012788 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012789
12790 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070012791 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
12792 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012793 dispatchDraw(canvas);
12794 } else {
12795 draw(canvas);
12796 }
12797
12798 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012799 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012800
12801 if (attachInfo != null) {
12802 // Restore the cached Canvas for our siblings
12803 attachInfo.mCanvas = canvas;
12804 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012805 }
12806 }
12807
12808 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012809 * Create a snapshot of the view into a bitmap. We should probably make
12810 * some form of this public, but should think about the API.
12811 */
Romain Guy223ff5c2010-03-02 17:07:47 -080012812 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012813 int width = mRight - mLeft;
12814 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012815
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012816 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070012817 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012818 width = (int) ((width * scale) + 0.5f);
12819 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080012820
Dianne Hackborndde331c2012-08-03 14:01:57 -070012821 Bitmap bitmap = Bitmap.createBitmap(mResources.getDisplayMetrics(),
12822 width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012823 if (bitmap == null) {
12824 throw new OutOfMemoryError();
12825 }
12826
Romain Guyc529d8d2011-09-06 15:01:39 -070012827 Resources resources = getResources();
12828 if (resources != null) {
12829 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
12830 }
Joe Malin32736f02011-01-19 16:14:20 -080012831
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012832 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012833 if (attachInfo != null) {
12834 canvas = attachInfo.mCanvas;
12835 if (canvas == null) {
12836 canvas = new Canvas();
12837 }
12838 canvas.setBitmap(bitmap);
12839 // Temporarily clobber the cached Canvas in case one of our children
12840 // is also using a drawing cache. Without this, the children would
12841 // steal the canvas by attaching their own bitmap to it and bad, bad
12842 // things would happen (invisible views, corrupted drawings, etc.)
12843 attachInfo.mCanvas = null;
12844 } else {
12845 // This case should hopefully never or seldom happen
12846 canvas = new Canvas(bitmap);
12847 }
12848
Romain Guy5bcdff42009-05-14 21:27:18 -070012849 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012850 bitmap.eraseColor(backgroundColor);
12851 }
12852
12853 computeScroll();
12854 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012855 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012856 canvas.translate(-mScrollX, -mScrollY);
12857
Romain Guy5bcdff42009-05-14 21:27:18 -070012858 // Temporarily remove the dirty mask
12859 int flags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070012860 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Romain Guy5bcdff42009-05-14 21:27:18 -070012861
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012862 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070012863 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012864 dispatchDraw(canvas);
12865 } else {
12866 draw(canvas);
12867 }
12868
Romain Guy5bcdff42009-05-14 21:27:18 -070012869 mPrivateFlags = flags;
12870
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012871 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012872 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012873
12874 if (attachInfo != null) {
12875 // Restore the cached Canvas for our siblings
12876 attachInfo.mCanvas = canvas;
12877 }
Romain Guy8506ab42009-06-11 17:35:47 -070012878
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012879 return bitmap;
12880 }
12881
12882 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012883 * Indicates whether this View is currently in edit mode. A View is usually
12884 * in edit mode when displayed within a developer tool. For instance, if
12885 * this View is being drawn by a visual user interface builder, this method
12886 * should return true.
12887 *
12888 * Subclasses should check the return value of this method to provide
12889 * different behaviors if their normal behavior might interfere with the
12890 * host environment. For instance: the class spawns a thread in its
12891 * constructor, the drawing code relies on device-specific features, etc.
12892 *
12893 * This method is usually checked in the drawing code of custom widgets.
12894 *
12895 * @return True if this View is in edit mode, false otherwise.
12896 */
12897 public boolean isInEditMode() {
12898 return false;
12899 }
12900
12901 /**
12902 * If the View draws content inside its padding and enables fading edges,
12903 * it needs to support padding offsets. Padding offsets are added to the
12904 * fading edges to extend the length of the fade so that it covers pixels
12905 * drawn inside the padding.
12906 *
12907 * Subclasses of this class should override this method if they need
12908 * to draw content inside the padding.
12909 *
12910 * @return True if padding offset must be applied, false otherwise.
12911 *
12912 * @see #getLeftPaddingOffset()
12913 * @see #getRightPaddingOffset()
12914 * @see #getTopPaddingOffset()
12915 * @see #getBottomPaddingOffset()
12916 *
12917 * @since CURRENT
12918 */
12919 protected boolean isPaddingOffsetRequired() {
12920 return false;
12921 }
12922
12923 /**
12924 * Amount by which to extend the left fading region. Called only when
12925 * {@link #isPaddingOffsetRequired()} returns true.
12926 *
12927 * @return The left padding offset in pixels.
12928 *
12929 * @see #isPaddingOffsetRequired()
12930 *
12931 * @since CURRENT
12932 */
12933 protected int getLeftPaddingOffset() {
12934 return 0;
12935 }
12936
12937 /**
12938 * Amount by which to extend the right fading region. Called only when
12939 * {@link #isPaddingOffsetRequired()} returns true.
12940 *
12941 * @return The right padding offset in pixels.
12942 *
12943 * @see #isPaddingOffsetRequired()
12944 *
12945 * @since CURRENT
12946 */
12947 protected int getRightPaddingOffset() {
12948 return 0;
12949 }
12950
12951 /**
12952 * Amount by which to extend the top fading region. Called only when
12953 * {@link #isPaddingOffsetRequired()} returns true.
12954 *
12955 * @return The top padding offset in pixels.
12956 *
12957 * @see #isPaddingOffsetRequired()
12958 *
12959 * @since CURRENT
12960 */
12961 protected int getTopPaddingOffset() {
12962 return 0;
12963 }
12964
12965 /**
12966 * Amount by which to extend the bottom fading region. Called only when
12967 * {@link #isPaddingOffsetRequired()} returns true.
12968 *
12969 * @return The bottom padding offset in pixels.
12970 *
12971 * @see #isPaddingOffsetRequired()
12972 *
12973 * @since CURRENT
12974 */
12975 protected int getBottomPaddingOffset() {
12976 return 0;
12977 }
12978
12979 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070012980 * @hide
12981 * @param offsetRequired
12982 */
12983 protected int getFadeTop(boolean offsetRequired) {
12984 int top = mPaddingTop;
12985 if (offsetRequired) top += getTopPaddingOffset();
12986 return top;
12987 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012988
Romain Guyf2fc4602011-07-19 15:20:03 -070012989 /**
12990 * @hide
12991 * @param offsetRequired
12992 */
12993 protected int getFadeHeight(boolean offsetRequired) {
12994 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070012995 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070012996 return mBottom - mTop - mPaddingBottom - padding;
12997 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012998
Romain Guyf2fc4602011-07-19 15:20:03 -070012999 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090013000 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070013001 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080013002 *
Romain Guy2bffd262010-09-12 17:40:02 -070013003 * <p>Even if this method returns true, it does not mean that every call
13004 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
13005 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090013006 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070013007 * window is hardware accelerated,
13008 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
13009 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080013010 *
Romain Guy2bffd262010-09-12 17:40:02 -070013011 * @return True if the view is attached to a window and the window is
13012 * hardware accelerated; false in any other case.
13013 */
13014 public boolean isHardwareAccelerated() {
13015 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
13016 }
Joe Malin32736f02011-01-19 16:14:20 -080013017
Romain Guy2bffd262010-09-12 17:40:02 -070013018 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080013019 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
13020 * case of an active Animation being run on the view.
13021 */
13022 private boolean drawAnimation(ViewGroup parent, long drawingTime,
13023 Animation a, boolean scalingRequired) {
13024 Transformation invalidationTransform;
13025 final int flags = parent.mGroupFlags;
13026 final boolean initialized = a.isInitialized();
13027 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070013028 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080013029 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
Romain Guy393a52c2012-05-22 20:21:08 -070013030 if (mAttachInfo != null) a.setListenerHandler(mAttachInfo.mHandler);
Chet Haasebcca79a2012-02-14 08:45:14 -080013031 onAnimationStart();
13032 }
13033
13034 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
13035 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
13036 if (parent.mInvalidationTransformation == null) {
13037 parent.mInvalidationTransformation = new Transformation();
13038 }
13039 invalidationTransform = parent.mInvalidationTransformation;
13040 a.getTransformation(drawingTime, invalidationTransform, 1f);
13041 } else {
13042 invalidationTransform = parent.mChildTransformation;
13043 }
Romain Guy393a52c2012-05-22 20:21:08 -070013044
Chet Haasebcca79a2012-02-14 08:45:14 -080013045 if (more) {
13046 if (!a.willChangeBounds()) {
Romain Guyf0af1d52012-07-11 18:31:21 -070013047 if ((flags & (ViewGroup.FLAG_OPTIMIZE_INVALIDATE | ViewGroup.FLAG_ANIMATION_DONE)) ==
13048 ViewGroup.FLAG_OPTIMIZE_INVALIDATE) {
13049 parent.mGroupFlags |= ViewGroup.FLAG_INVALIDATE_REQUIRED;
13050 } else if ((flags & ViewGroup.FLAG_INVALIDATE_REQUIRED) == 0) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013051 // The child need to draw an animation, potentially offscreen, so
13052 // make sure we do not cancel invalidate requests
Dianne Hackborn4702a852012-08-17 15:18:29 -070013053 parent.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
Chet Haasebcca79a2012-02-14 08:45:14 -080013054 parent.invalidate(mLeft, mTop, mRight, mBottom);
13055 }
13056 } else {
13057 if (parent.mInvalidateRegion == null) {
13058 parent.mInvalidateRegion = new RectF();
13059 }
13060 final RectF region = parent.mInvalidateRegion;
13061 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
13062 invalidationTransform);
13063
13064 // The child need to draw an animation, potentially offscreen, so
13065 // make sure we do not cancel invalidate requests
Dianne Hackborn4702a852012-08-17 15:18:29 -070013066 parent.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
Chet Haasebcca79a2012-02-14 08:45:14 -080013067
13068 final int left = mLeft + (int) region.left;
13069 final int top = mTop + (int) region.top;
13070 parent.invalidate(left, top, left + (int) (region.width() + .5f),
13071 top + (int) (region.height() + .5f));
13072 }
13073 }
13074 return more;
13075 }
13076
Chet Haasea1cff502012-02-21 13:43:44 -080013077 /**
13078 * This method is called by getDisplayList() when a display list is created or re-rendered.
13079 * It sets or resets the current value of all properties on that display list (resetting is
13080 * necessary when a display list is being re-created, because we need to make sure that
13081 * previously-set transform values
13082 */
13083 void setDisplayListProperties(DisplayList displayList) {
Chet Haase1271e2c2012-04-20 09:54:27 -070013084 if (displayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013085 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070013086 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080013087 if (mParent instanceof ViewGroup) {
13088 displayList.setClipChildren(
13089 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
13090 }
Chet Haase9420abd2012-03-29 16:28:32 -070013091 float alpha = 1;
13092 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
13093 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
13094 ViewGroup parentVG = (ViewGroup) mParent;
13095 final boolean hasTransform =
13096 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
13097 if (hasTransform) {
13098 Transformation transform = parentVG.mChildTransformation;
13099 final int transformType = parentVG.mChildTransformation.getTransformationType();
13100 if (transformType != Transformation.TYPE_IDENTITY) {
13101 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
13102 alpha = transform.getAlpha();
13103 }
13104 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
13105 displayList.setStaticMatrix(transform.getMatrix());
13106 }
13107 }
13108 }
Chet Haasea1cff502012-02-21 13:43:44 -080013109 }
13110 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070013111 alpha *= mTransformationInfo.mAlpha;
13112 if (alpha < 1) {
13113 final int multipliedAlpha = (int) (255 * alpha);
13114 if (onSetAlpha(multipliedAlpha)) {
13115 alpha = 1;
13116 }
13117 }
13118 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080013119 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
13120 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
13121 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
13122 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070013123 if (mTransformationInfo.mCamera == null) {
13124 mTransformationInfo.mCamera = new Camera();
13125 mTransformationInfo.matrix3D = new Matrix();
13126 }
13127 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Dianne Hackborn4702a852012-08-17 15:18:29 -070013128 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == PFLAG_PIVOT_EXPLICITLY_SET) {
Chet Haasea1cff502012-02-21 13:43:44 -080013129 displayList.setPivotX(getPivotX());
13130 displayList.setPivotY(getPivotY());
13131 }
Chet Haase9420abd2012-03-29 16:28:32 -070013132 } else if (alpha < 1) {
13133 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080013134 }
13135 }
13136 }
13137
Chet Haasebcca79a2012-02-14 08:45:14 -080013138 /**
Chet Haase64a48c12012-02-13 16:33:29 -080013139 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
13140 * This draw() method is an implementation detail and is not intended to be overridden or
13141 * to be called from anywhere else other than ViewGroup.drawChild().
13142 */
13143 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haase1271e2c2012-04-20 09:54:27 -070013144 boolean useDisplayListProperties = mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080013145 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080013146 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080013147 final int flags = parent.mGroupFlags;
13148
Chet Haasea1cff502012-02-21 13:43:44 -080013149 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080013150 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080013151 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013152 }
13153
13154 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080013155 boolean concatMatrix = false;
13156
13157 boolean scalingRequired = false;
13158 boolean caching;
Michael Jurkaba649742012-06-28 19:12:58 -070013159 int layerType = getLayerType();
Chet Haase64a48c12012-02-13 16:33:29 -080013160
13161 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080013162 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
13163 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013164 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070013165 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080013166 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
13167 } else {
13168 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
13169 }
13170
Chet Haasebcca79a2012-02-14 08:45:14 -080013171 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080013172 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013173 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080013174 concatMatrix = a.willChangeTransformationMatrix();
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013175 if (concatMatrix) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013176 mPrivateFlags3 |= PFLAG3_VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013177 }
Chet Haasebcca79a2012-02-14 08:45:14 -080013178 transformToApply = parent.mChildTransformation;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013179 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013180 if ((mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_TRANSFORM) == PFLAG3_VIEW_IS_ANIMATING_TRANSFORM &&
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013181 mDisplayList != null) {
13182 // No longer animating: clear out old animation matrix
13183 mDisplayList.setAnimationMatrix(null);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013184 mPrivateFlags3 &= ~PFLAG3_VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013185 }
13186 if (!useDisplayListProperties &&
13187 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
13188 final boolean hasTransform =
13189 parent.getChildStaticTransformation(this, parent.mChildTransformation);
13190 if (hasTransform) {
13191 final int transformType = parent.mChildTransformation.getTransformationType();
13192 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
13193 parent.mChildTransformation : null;
13194 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
13195 }
Chet Haase64a48c12012-02-13 16:33:29 -080013196 }
13197 }
13198
13199 concatMatrix |= !childHasIdentityMatrix;
13200
13201 // Sets the flag as early as possible to allow draw() implementations
13202 // to call invalidate() successfully when doing animations
Dianne Hackborn4702a852012-08-17 15:18:29 -070013203 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase64a48c12012-02-13 16:33:29 -080013204
Romain Guyfbb43212012-08-30 15:19:27 -070013205 if (!concatMatrix &&
13206 (flags & (ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS |
13207 ViewGroup.FLAG_CLIP_CHILDREN)) == ViewGroup.FLAG_CLIP_CHILDREN &&
Chet Haase599913d2012-07-23 16:22:05 -070013208 canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Dianne Hackborn4702a852012-08-17 15:18:29 -070013209 (mPrivateFlags & PFLAG_DRAW_ANIMATION) == 0) {
13210 mPrivateFlags2 |= PFLAG2_VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013211 return more;
13212 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013213 mPrivateFlags2 &= ~PFLAG2_VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013214
13215 if (hardwareAccelerated) {
13216 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
13217 // retain the flag's value temporarily in the mRecreateDisplayList flag
Dianne Hackborn4702a852012-08-17 15:18:29 -070013218 mRecreateDisplayList = (mPrivateFlags & PFLAG_INVALIDATED) == PFLAG_INVALIDATED;
13219 mPrivateFlags &= ~PFLAG_INVALIDATED;
Chet Haase64a48c12012-02-13 16:33:29 -080013220 }
13221
Chet Haase64a48c12012-02-13 16:33:29 -080013222 DisplayList displayList = null;
13223 Bitmap cache = null;
13224 boolean hasDisplayList = false;
13225 if (caching) {
13226 if (!hardwareAccelerated) {
13227 if (layerType != LAYER_TYPE_NONE) {
13228 layerType = LAYER_TYPE_SOFTWARE;
13229 buildDrawingCache(true);
13230 }
13231 cache = getDrawingCache(true);
13232 } else {
13233 switch (layerType) {
13234 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070013235 if (useDisplayListProperties) {
13236 hasDisplayList = canHaveDisplayList();
13237 } else {
13238 buildDrawingCache(true);
13239 cache = getDrawingCache(true);
13240 }
Chet Haase64a48c12012-02-13 16:33:29 -080013241 break;
Chet Haasea1cff502012-02-21 13:43:44 -080013242 case LAYER_TYPE_HARDWARE:
13243 if (useDisplayListProperties) {
13244 hasDisplayList = canHaveDisplayList();
13245 }
13246 break;
Chet Haase64a48c12012-02-13 16:33:29 -080013247 case LAYER_TYPE_NONE:
13248 // Delay getting the display list until animation-driven alpha values are
13249 // set up and possibly passed on to the view
13250 hasDisplayList = canHaveDisplayList();
13251 break;
13252 }
13253 }
13254 }
Chet Haasea1cff502012-02-21 13:43:44 -080013255 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070013256 if (useDisplayListProperties) {
13257 displayList = getDisplayList();
13258 if (!displayList.isValid()) {
13259 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13260 // to getDisplayList(), the display list will be marked invalid and we should not
13261 // try to use it again.
13262 displayList = null;
13263 hasDisplayList = false;
13264 useDisplayListProperties = false;
13265 }
13266 }
Chet Haase64a48c12012-02-13 16:33:29 -080013267
Chet Haase526057b2012-07-12 17:50:41 -070013268 int sx = 0;
13269 int sy = 0;
13270 if (!hasDisplayList) {
13271 computeScroll();
13272 sx = mScrollX;
13273 sy = mScrollY;
13274 }
13275
Chet Haase64a48c12012-02-13 16:33:29 -080013276 final boolean hasNoCache = cache == null || hasDisplayList;
13277 final boolean offsetForScroll = cache == null && !hasDisplayList &&
13278 layerType != LAYER_TYPE_HARDWARE;
13279
Chet Haasea1cff502012-02-21 13:43:44 -080013280 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070013281 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013282 restoreTo = canvas.save();
13283 }
Chet Haase64a48c12012-02-13 16:33:29 -080013284 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013285 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080013286 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080013287 if (!useDisplayListProperties) {
13288 canvas.translate(mLeft, mTop);
13289 }
Chet Haase64a48c12012-02-13 16:33:29 -080013290 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080013291 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070013292 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080013293 restoreTo = canvas.save();
13294 }
Chet Haase64a48c12012-02-13 16:33:29 -080013295 // mAttachInfo cannot be null, otherwise scalingRequired == false
13296 final float scale = 1.0f / mAttachInfo.mApplicationScale;
13297 canvas.scale(scale, scale);
13298 }
13299 }
13300
Chet Haasea1cff502012-02-21 13:43:44 -080013301 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase21433372012-06-05 07:54:09 -070013302 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix() ||
Dianne Hackborn4702a852012-08-17 15:18:29 -070013303 (mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_ALPHA) == PFLAG3_VIEW_IS_ANIMATING_ALPHA) {
Chet Haase64a48c12012-02-13 16:33:29 -080013304 if (transformToApply != null || !childHasIdentityMatrix) {
13305 int transX = 0;
13306 int transY = 0;
13307
13308 if (offsetForScroll) {
13309 transX = -sx;
13310 transY = -sy;
13311 }
13312
13313 if (transformToApply != null) {
13314 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070013315 if (useDisplayListProperties) {
13316 displayList.setAnimationMatrix(transformToApply.getMatrix());
13317 } else {
13318 // Undo the scroll translation, apply the transformation matrix,
13319 // then redo the scroll translate to get the correct result.
13320 canvas.translate(-transX, -transY);
13321 canvas.concat(transformToApply.getMatrix());
13322 canvas.translate(transX, transY);
13323 }
Chet Haasea1cff502012-02-21 13:43:44 -080013324 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013325 }
13326
13327 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070013328 if (transformAlpha < 1) {
Chet Haase21433372012-06-05 07:54:09 -070013329 alpha *= transformAlpha;
Chet Haasea1cff502012-02-21 13:43:44 -080013330 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013331 }
13332 }
13333
Chet Haasea1cff502012-02-21 13:43:44 -080013334 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013335 canvas.translate(-transX, -transY);
13336 canvas.concat(getMatrix());
13337 canvas.translate(transX, transY);
13338 }
13339 }
13340
Chet Haase21433372012-06-05 07:54:09 -070013341 // Deal with alpha if it is or used to be <1
13342 if (alpha < 1 ||
Dianne Hackborn4702a852012-08-17 15:18:29 -070013343 (mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_ALPHA) == PFLAG3_VIEW_IS_ANIMATING_ALPHA) {
Chet Haase21433372012-06-05 07:54:09 -070013344 if (alpha < 1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013345 mPrivateFlags3 |= PFLAG3_VIEW_IS_ANIMATING_ALPHA;
Chet Haase21433372012-06-05 07:54:09 -070013346 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013347 mPrivateFlags3 &= ~PFLAG3_VIEW_IS_ANIMATING_ALPHA;
Chet Haase21433372012-06-05 07:54:09 -070013348 }
Chet Haasea1cff502012-02-21 13:43:44 -080013349 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013350 if (hasNoCache) {
13351 final int multipliedAlpha = (int) (255 * alpha);
13352 if (!onSetAlpha(multipliedAlpha)) {
13353 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080013354 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080013355 layerType != LAYER_TYPE_NONE) {
13356 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
13357 }
Chet Haase9420abd2012-03-29 16:28:32 -070013358 if (useDisplayListProperties) {
13359 displayList.setAlpha(alpha * getAlpha());
13360 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070013361 final int scrollX = hasDisplayList ? 0 : sx;
13362 final int scrollY = hasDisplayList ? 0 : sy;
13363 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
13364 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080013365 }
13366 } else {
13367 // Alpha is handled by the child directly, clobber the layer's alpha
Dianne Hackborn4702a852012-08-17 15:18:29 -070013368 mPrivateFlags |= PFLAG_ALPHA_SET;
Chet Haase64a48c12012-02-13 16:33:29 -080013369 }
13370 }
13371 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013372 } else if ((mPrivateFlags & PFLAG_ALPHA_SET) == PFLAG_ALPHA_SET) {
Chet Haase64a48c12012-02-13 16:33:29 -080013373 onSetAlpha(255);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013374 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase64a48c12012-02-13 16:33:29 -080013375 }
13376
Chet Haasea1cff502012-02-21 13:43:44 -080013377 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
13378 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013379 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013380 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080013381 } else {
13382 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013383 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080013384 } else {
13385 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
13386 }
13387 }
13388 }
13389
Chet Haase9420abd2012-03-29 16:28:32 -070013390 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080013391 displayList = getDisplayList();
13392 if (!displayList.isValid()) {
13393 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13394 // to getDisplayList(), the display list will be marked invalid and we should not
13395 // try to use it again.
13396 displayList = null;
13397 hasDisplayList = false;
13398 }
13399 }
13400
13401 if (hasNoCache) {
13402 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080013403 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080013404 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080013405 if (layer != null && layer.isValid()) {
13406 mLayerPaint.setAlpha((int) (alpha * 255));
13407 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
13408 layerRendered = true;
13409 } else {
13410 final int scrollX = hasDisplayList ? 0 : sx;
13411 final int scrollY = hasDisplayList ? 0 : sy;
13412 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080013413 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080013414 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
13415 }
13416 }
13417
13418 if (!layerRendered) {
13419 if (!hasDisplayList) {
13420 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070013421 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
13422 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase64a48c12012-02-13 16:33:29 -080013423 dispatchDraw(canvas);
13424 } else {
13425 draw(canvas);
13426 }
13427 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013428 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase1271e2c2012-04-20 09:54:27 -070013429 ((HardwareCanvas) canvas).drawDisplayList(displayList, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080013430 }
13431 }
13432 } else if (cache != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013433 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase64a48c12012-02-13 16:33:29 -080013434 Paint cachePaint;
13435
13436 if (layerType == LAYER_TYPE_NONE) {
13437 cachePaint = parent.mCachePaint;
13438 if (cachePaint == null) {
13439 cachePaint = new Paint();
13440 cachePaint.setDither(false);
13441 parent.mCachePaint = cachePaint;
13442 }
Chet Haase9420abd2012-03-29 16:28:32 -070013443 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080013444 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080013445 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
13446 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013447 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080013448 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080013449 }
13450 } else {
13451 cachePaint = mLayerPaint;
13452 cachePaint.setAlpha((int) (alpha * 255));
13453 }
13454 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
13455 }
13456
Chet Haasea1cff502012-02-21 13:43:44 -080013457 if (restoreTo >= 0) {
13458 canvas.restoreToCount(restoreTo);
13459 }
Chet Haase64a48c12012-02-13 16:33:29 -080013460
13461 if (a != null && !more) {
13462 if (!hardwareAccelerated && !a.getFillAfter()) {
13463 onSetAlpha(255);
13464 }
13465 parent.finishAnimatingView(this, a);
13466 }
13467
13468 if (more && hardwareAccelerated) {
13469 // invalidation is the trigger to recreate display lists, so if we're using
13470 // display lists to render, force an invalidate to allow the animation to
13471 // continue drawing another frame
13472 parent.invalidate(true);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013473 if (a.hasAlpha() && (mPrivateFlags & PFLAG_ALPHA_SET) == PFLAG_ALPHA_SET) {
Chet Haase64a48c12012-02-13 16:33:29 -080013474 // alpha animations should cause the child to recreate its display list
13475 invalidate(true);
13476 }
13477 }
13478
13479 mRecreateDisplayList = false;
13480
13481 return more;
13482 }
13483
13484 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013485 * Manually render this view (and all of its children) to the given Canvas.
13486 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070013487 * called. When implementing a view, implement
13488 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
13489 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013490 *
13491 * @param canvas The Canvas to which the View is rendered.
13492 */
13493 public void draw(Canvas canvas) {
Romain Guy5bcdff42009-05-14 21:27:18 -070013494 final int privateFlags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070013495 final boolean dirtyOpaque = (privateFlags & PFLAG_DIRTY_MASK) == PFLAG_DIRTY_OPAQUE &&
Romain Guy5bcdff42009-05-14 21:27:18 -070013496 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013497 mPrivateFlags = (privateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070013498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013499 /*
13500 * Draw traversal performs several drawing steps which must be executed
13501 * in the appropriate order:
13502 *
13503 * 1. Draw the background
13504 * 2. If necessary, save the canvas' layers to prepare for fading
13505 * 3. Draw view's content
13506 * 4. Draw children
13507 * 5. If necessary, draw the fading edges and restore layers
13508 * 6. Draw decorations (scrollbars for instance)
13509 */
13510
13511 // Step 1, draw the background, if needed
13512 int saveCount;
13513
Romain Guy24443ea2009-05-11 11:56:30 -070013514 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013515 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070013516 if (background != null) {
13517 final int scrollX = mScrollX;
13518 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013519
Romain Guy24443ea2009-05-11 11:56:30 -070013520 if (mBackgroundSizeChanged) {
13521 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
13522 mBackgroundSizeChanged = false;
13523 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013524
Romain Guy24443ea2009-05-11 11:56:30 -070013525 if ((scrollX | scrollY) == 0) {
13526 background.draw(canvas);
13527 } else {
13528 canvas.translate(scrollX, scrollY);
13529 background.draw(canvas);
13530 canvas.translate(-scrollX, -scrollY);
13531 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013532 }
13533 }
13534
13535 // skip step 2 & 5 if possible (common case)
13536 final int viewFlags = mViewFlags;
13537 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
13538 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
13539 if (!verticalEdges && !horizontalEdges) {
13540 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013541 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013542
13543 // Step 4, draw the children
13544 dispatchDraw(canvas);
13545
13546 // Step 6, draw decorations (scrollbars)
13547 onDrawScrollBars(canvas);
13548
13549 // we're done...
13550 return;
13551 }
13552
13553 /*
13554 * Here we do the full fledged routine...
13555 * (this is an uncommon case where speed matters less,
13556 * this is why we repeat some of the tests that have been
13557 * done above)
13558 */
13559
13560 boolean drawTop = false;
13561 boolean drawBottom = false;
13562 boolean drawLeft = false;
13563 boolean drawRight = false;
13564
13565 float topFadeStrength = 0.0f;
13566 float bottomFadeStrength = 0.0f;
13567 float leftFadeStrength = 0.0f;
13568 float rightFadeStrength = 0.0f;
13569
13570 // Step 2, save the canvas' layers
13571 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013572
13573 final boolean offsetRequired = isPaddingOffsetRequired();
13574 if (offsetRequired) {
13575 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013576 }
13577
13578 int left = mScrollX + paddingLeft;
13579 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070013580 int top = mScrollY + getFadeTop(offsetRequired);
13581 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013582
13583 if (offsetRequired) {
13584 right += getRightPaddingOffset();
13585 bottom += getBottomPaddingOffset();
13586 }
13587
13588 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070013589 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013590 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013591
13592 // clip the fade length if top and bottom fades overlap
13593 // overlapping fades produce odd-looking artifacts
13594 if (verticalEdges && (top + length > bottom - length)) {
13595 length = (bottom - top) / 2;
13596 }
13597
13598 // also clip horizontal fades if necessary
13599 if (horizontalEdges && (left + length > right - length)) {
13600 length = (right - left) / 2;
13601 }
13602
13603 if (verticalEdges) {
13604 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013605 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013606 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013607 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013608 }
13609
13610 if (horizontalEdges) {
13611 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013612 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013613 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013614 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013615 }
13616
13617 saveCount = canvas.getSaveCount();
13618
13619 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070013620 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013621 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
13622
13623 if (drawTop) {
13624 canvas.saveLayer(left, top, right, top + length, null, flags);
13625 }
13626
13627 if (drawBottom) {
13628 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
13629 }
13630
13631 if (drawLeft) {
13632 canvas.saveLayer(left, top, left + length, bottom, null, flags);
13633 }
13634
13635 if (drawRight) {
13636 canvas.saveLayer(right - length, top, right, bottom, null, flags);
13637 }
13638 } else {
13639 scrollabilityCache.setFadeColor(solidColor);
13640 }
13641
13642 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013643 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013644
13645 // Step 4, draw the children
13646 dispatchDraw(canvas);
13647
13648 // Step 5, draw the fade effect and restore layers
13649 final Paint p = scrollabilityCache.paint;
13650 final Matrix matrix = scrollabilityCache.matrix;
13651 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013652
13653 if (drawTop) {
13654 matrix.setScale(1, fadeHeight * topFadeStrength);
13655 matrix.postTranslate(left, top);
13656 fade.setLocalMatrix(matrix);
13657 canvas.drawRect(left, top, right, top + length, p);
13658 }
13659
13660 if (drawBottom) {
13661 matrix.setScale(1, fadeHeight * bottomFadeStrength);
13662 matrix.postRotate(180);
13663 matrix.postTranslate(left, bottom);
13664 fade.setLocalMatrix(matrix);
13665 canvas.drawRect(left, bottom - length, right, bottom, p);
13666 }
13667
13668 if (drawLeft) {
13669 matrix.setScale(1, fadeHeight * leftFadeStrength);
13670 matrix.postRotate(-90);
13671 matrix.postTranslate(left, top);
13672 fade.setLocalMatrix(matrix);
13673 canvas.drawRect(left, top, left + length, bottom, p);
13674 }
13675
13676 if (drawRight) {
13677 matrix.setScale(1, fadeHeight * rightFadeStrength);
13678 matrix.postRotate(90);
13679 matrix.postTranslate(right, top);
13680 fade.setLocalMatrix(matrix);
13681 canvas.drawRect(right - length, top, right, bottom, p);
13682 }
13683
13684 canvas.restoreToCount(saveCount);
13685
13686 // Step 6, draw decorations (scrollbars)
13687 onDrawScrollBars(canvas);
13688 }
13689
13690 /**
13691 * Override this if your view is known to always be drawn on top of a solid color background,
13692 * and needs to draw fading edges. Returning a non-zero color enables the view system to
13693 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
13694 * should be set to 0xFF.
13695 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013696 * @see #setVerticalFadingEdgeEnabled(boolean)
13697 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013698 *
13699 * @return The known solid color background for this view, or 0 if the color may vary
13700 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013701 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013702 public int getSolidColor() {
13703 return 0;
13704 }
13705
13706 /**
13707 * Build a human readable string representation of the specified view flags.
13708 *
13709 * @param flags the view flags to convert to a string
13710 * @return a String representing the supplied flags
13711 */
13712 private static String printFlags(int flags) {
13713 String output = "";
13714 int numFlags = 0;
13715 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
13716 output += "TAKES_FOCUS";
13717 numFlags++;
13718 }
13719
13720 switch (flags & VISIBILITY_MASK) {
13721 case INVISIBLE:
13722 if (numFlags > 0) {
13723 output += " ";
13724 }
13725 output += "INVISIBLE";
13726 // USELESS HERE numFlags++;
13727 break;
13728 case GONE:
13729 if (numFlags > 0) {
13730 output += " ";
13731 }
13732 output += "GONE";
13733 // USELESS HERE numFlags++;
13734 break;
13735 default:
13736 break;
13737 }
13738 return output;
13739 }
13740
13741 /**
13742 * Build a human readable string representation of the specified private
13743 * view flags.
13744 *
13745 * @param privateFlags the private view flags to convert to a string
13746 * @return a String representing the supplied flags
13747 */
13748 private static String printPrivateFlags(int privateFlags) {
13749 String output = "";
13750 int numFlags = 0;
13751
Dianne Hackborn4702a852012-08-17 15:18:29 -070013752 if ((privateFlags & PFLAG_WANTS_FOCUS) == PFLAG_WANTS_FOCUS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013753 output += "WANTS_FOCUS";
13754 numFlags++;
13755 }
13756
Dianne Hackborn4702a852012-08-17 15:18:29 -070013757 if ((privateFlags & PFLAG_FOCUSED) == PFLAG_FOCUSED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013758 if (numFlags > 0) {
13759 output += " ";
13760 }
13761 output += "FOCUSED";
13762 numFlags++;
13763 }
13764
Dianne Hackborn4702a852012-08-17 15:18:29 -070013765 if ((privateFlags & PFLAG_SELECTED) == PFLAG_SELECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013766 if (numFlags > 0) {
13767 output += " ";
13768 }
13769 output += "SELECTED";
13770 numFlags++;
13771 }
13772
Dianne Hackborn4702a852012-08-17 15:18:29 -070013773 if ((privateFlags & PFLAG_IS_ROOT_NAMESPACE) == PFLAG_IS_ROOT_NAMESPACE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013774 if (numFlags > 0) {
13775 output += " ";
13776 }
13777 output += "IS_ROOT_NAMESPACE";
13778 numFlags++;
13779 }
13780
Dianne Hackborn4702a852012-08-17 15:18:29 -070013781 if ((privateFlags & PFLAG_HAS_BOUNDS) == PFLAG_HAS_BOUNDS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013782 if (numFlags > 0) {
13783 output += " ";
13784 }
13785 output += "HAS_BOUNDS";
13786 numFlags++;
13787 }
13788
Dianne Hackborn4702a852012-08-17 15:18:29 -070013789 if ((privateFlags & PFLAG_DRAWN) == PFLAG_DRAWN) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013790 if (numFlags > 0) {
13791 output += " ";
13792 }
13793 output += "DRAWN";
13794 // USELESS HERE numFlags++;
13795 }
13796 return output;
13797 }
13798
13799 /**
13800 * <p>Indicates whether or not this view's layout will be requested during
13801 * the next hierarchy layout pass.</p>
13802 *
13803 * @return true if the layout will be forced during next layout pass
13804 */
13805 public boolean isLayoutRequested() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013806 return (mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013807 }
13808
13809 /**
13810 * Assign a size and position to a view and all of its
13811 * descendants
13812 *
13813 * <p>This is the second phase of the layout mechanism.
13814 * (The first is measuring). In this phase, each parent calls
13815 * layout on all of its children to position them.
13816 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080013817 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013818 *
Chet Haase9c087442011-01-12 16:20:16 -080013819 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013820 * Derived classes with children should override
13821 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080013822 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013823 *
13824 * @param l Left position, relative to parent
13825 * @param t Top position, relative to parent
13826 * @param r Right position, relative to parent
13827 * @param b Bottom position, relative to parent
13828 */
Romain Guy5429e1d2010-09-07 12:38:00 -070013829 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080013830 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070013831 int oldL = mLeft;
13832 int oldT = mTop;
13833 int oldB = mBottom;
13834 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013835 boolean changed = setFrame(l, t, r, b);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013836 if (changed || (mPrivateFlags & PFLAG_LAYOUT_REQUIRED) == PFLAG_LAYOUT_REQUIRED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013837 onLayout(changed, l, t, r, b);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013838 mPrivateFlags &= ~PFLAG_LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070013839
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013840 ListenerInfo li = mListenerInfo;
13841 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070013842 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013843 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070013844 int numListeners = listenersCopy.size();
13845 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070013846 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070013847 }
13848 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013849 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013850 mPrivateFlags &= ~PFLAG_FORCE_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013851 }
13852
13853 /**
13854 * Called from layout when this view should
13855 * assign a size and position to each of its children.
13856 *
13857 * Derived classes with children should override
13858 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070013859 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013860 * @param changed This is a new size or position for this view
13861 * @param left Left position, relative to parent
13862 * @param top Top position, relative to parent
13863 * @param right Right position, relative to parent
13864 * @param bottom Bottom position, relative to parent
13865 */
13866 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
13867 }
13868
13869 /**
13870 * Assign a size and position to this view.
13871 *
13872 * This is called from layout.
13873 *
13874 * @param left Left position, relative to parent
13875 * @param top Top position, relative to parent
13876 * @param right Right position, relative to parent
13877 * @param bottom Bottom position, relative to parent
13878 * @return true if the new size and position are different than the
13879 * previous ones
13880 * {@hide}
13881 */
13882 protected boolean setFrame(int left, int top, int right, int bottom) {
13883 boolean changed = false;
13884
13885 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070013886 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013887 + right + "," + bottom + ")");
13888 }
13889
13890 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
13891 changed = true;
13892
13893 // Remember our drawn bit
Dianne Hackborn4702a852012-08-17 15:18:29 -070013894 int drawn = mPrivateFlags & PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013896 int oldWidth = mRight - mLeft;
13897 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070013898 int newWidth = right - left;
13899 int newHeight = bottom - top;
13900 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
13901
13902 // Invalidate our old position
13903 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013904
13905 mLeft = left;
13906 mTop = top;
13907 mRight = right;
13908 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -070013909 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013910 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
13911 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013912
Dianne Hackborn4702a852012-08-17 15:18:29 -070013913 mPrivateFlags |= PFLAG_HAS_BOUNDS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013915
Chet Haase75755e22011-07-18 17:48:25 -070013916 if (sizeChanged) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013917 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013918 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013919 if (mTransformationInfo != null) {
13920 mTransformationInfo.mMatrixDirty = true;
13921 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013922 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013923 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
13924 }
13925
13926 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
13927 // If we are visible, force the DRAWN bit to on so that
13928 // this invalidate will go through (at least to our parent).
13929 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013930 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013931 // the DRAWN bit.
Dianne Hackborn4702a852012-08-17 15:18:29 -070013932 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070013933 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080013934 // parent display list may need to be recreated based on a change in the bounds
13935 // of any child
13936 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013937 }
13938
13939 // Reset drawn bit to original value (invalidate turns it off)
13940 mPrivateFlags |= drawn;
13941
13942 mBackgroundSizeChanged = true;
13943 }
13944 return changed;
13945 }
13946
13947 /**
13948 * Finalize inflating a view from XML. This is called as the last phase
13949 * of inflation, after all child views have been added.
13950 *
13951 * <p>Even if the subclass overrides onFinishInflate, they should always be
13952 * sure to call the super method, so that we get called.
13953 */
13954 protected void onFinishInflate() {
13955 }
13956
13957 /**
13958 * Returns the resources associated with this view.
13959 *
13960 * @return Resources object.
13961 */
13962 public Resources getResources() {
13963 return mResources;
13964 }
13965
13966 /**
13967 * Invalidates the specified Drawable.
13968 *
13969 * @param drawable the drawable to invalidate
13970 */
13971 public void invalidateDrawable(Drawable drawable) {
13972 if (verifyDrawable(drawable)) {
13973 final Rect dirty = drawable.getBounds();
13974 final int scrollX = mScrollX;
13975 final int scrollY = mScrollY;
13976
13977 invalidate(dirty.left + scrollX, dirty.top + scrollY,
13978 dirty.right + scrollX, dirty.bottom + scrollY);
13979 }
13980 }
13981
13982 /**
13983 * Schedules an action on a drawable to occur at a specified time.
13984 *
13985 * @param who the recipient of the action
13986 * @param what the action to run on the drawable
13987 * @param when the time at which the action must occur. Uses the
13988 * {@link SystemClock#uptimeMillis} timebase.
13989 */
13990 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080013991 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013992 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080013993 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013994 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
13995 Choreographer.CALLBACK_ANIMATION, what, who,
13996 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080013997 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013998 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080013999 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014000 }
14001 }
14002
14003 /**
14004 * Cancels a scheduled action on a drawable.
14005 *
14006 * @param who the recipient of the action
14007 * @param what the action to cancel
14008 */
14009 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080014010 if (verifyDrawable(who) && what != null) {
14011 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070014012 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
14013 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080014014 } else {
14015 ViewRootImpl.getRunQueue().removeCallbacks(what);
14016 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014017 }
14018 }
14019
14020 /**
14021 * Unschedule any events associated with the given Drawable. This can be
14022 * used when selecting a new Drawable into a view, so that the previous
14023 * one is completely unscheduled.
14024 *
14025 * @param who The Drawable to unschedule.
14026 *
14027 * @see #drawableStateChanged
14028 */
14029 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080014030 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070014031 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
14032 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014033 }
14034 }
14035
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070014036 /**
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014037 * Resolve the Drawables depending on the layout direction. This is implicitly supposing
14038 * that the View directionality can and will be resolved before its Drawables.
14039 *
14040 * Will call {@link View#onResolveDrawables} when resolution is done.
14041 */
14042 public void resolveDrawables() {
14043 if (mBackground != null) {
14044 mBackground.setLayoutDirection(getResolvedLayoutDirection());
14045 }
14046 onResolveDrawables(getResolvedLayoutDirection());
14047 }
14048
14049 /**
14050 * Called when layout direction has been resolved.
14051 *
14052 * The default implementation does nothing.
14053 *
14054 * @param layoutDirection The resolved layout direction.
14055 *
14056 * @see {@link #LAYOUT_DIRECTION_LTR}
14057 * @see {@link #LAYOUT_DIRECTION_RTL}
14058 */
14059 public void onResolveDrawables(int layoutDirection) {
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070014060 }
14061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014062 /**
14063 * If your view subclass is displaying its own Drawable objects, it should
14064 * override this function and return true for any Drawable it is
14065 * displaying. This allows animations for those drawables to be
14066 * scheduled.
14067 *
14068 * <p>Be sure to call through to the super class when overriding this
14069 * function.
14070 *
14071 * @param who The Drawable to verify. Return true if it is one you are
14072 * displaying, else return the result of calling through to the
14073 * super class.
14074 *
14075 * @return boolean If true than the Drawable is being displayed in the
14076 * view; else false and it is not allowed to animate.
14077 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014078 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
14079 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014080 */
14081 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070014082 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014083 }
14084
14085 /**
14086 * This function is called whenever the state of the view changes in such
14087 * a way that it impacts the state of drawables being shown.
14088 *
14089 * <p>Be sure to call through to the superclass when overriding this
14090 * function.
14091 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014092 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014093 */
14094 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014095 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014096 if (d != null && d.isStateful()) {
14097 d.setState(getDrawableState());
14098 }
14099 }
14100
14101 /**
14102 * Call this to force a view to update its drawable state. This will cause
14103 * drawableStateChanged to be called on this view. Views that are interested
14104 * in the new state should call getDrawableState.
14105 *
14106 * @see #drawableStateChanged
14107 * @see #getDrawableState
14108 */
14109 public void refreshDrawableState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014110 mPrivateFlags |= PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014111 drawableStateChanged();
14112
14113 ViewParent parent = mParent;
14114 if (parent != null) {
14115 parent.childDrawableStateChanged(this);
14116 }
14117 }
14118
14119 /**
14120 * Return an array of resource IDs of the drawable states representing the
14121 * current state of the view.
14122 *
14123 * @return The current drawable state
14124 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014125 * @see Drawable#setState(int[])
14126 * @see #drawableStateChanged()
14127 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014128 */
14129 public final int[] getDrawableState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014130 if ((mDrawableState != null) && ((mPrivateFlags & PFLAG_DRAWABLE_STATE_DIRTY) == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014131 return mDrawableState;
14132 } else {
14133 mDrawableState = onCreateDrawableState(0);
Dianne Hackborn4702a852012-08-17 15:18:29 -070014134 mPrivateFlags &= ~PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014135 return mDrawableState;
14136 }
14137 }
14138
14139 /**
14140 * Generate the new {@link android.graphics.drawable.Drawable} state for
14141 * this view. This is called by the view
14142 * system when the cached Drawable state is determined to be invalid. To
14143 * retrieve the current state, you should use {@link #getDrawableState}.
14144 *
14145 * @param extraSpace if non-zero, this is the number of extra entries you
14146 * would like in the returned array in which you can place your own
14147 * states.
14148 *
14149 * @return Returns an array holding the current {@link Drawable} state of
14150 * the view.
14151 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014152 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014153 */
14154 protected int[] onCreateDrawableState(int extraSpace) {
14155 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
14156 mParent instanceof View) {
14157 return ((View) mParent).onCreateDrawableState(extraSpace);
14158 }
14159
14160 int[] drawableState;
14161
14162 int privateFlags = mPrivateFlags;
14163
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014164 int viewStateIndex = 0;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014165 if ((privateFlags & PFLAG_PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014166 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
14167 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014168 if ((privateFlags & PFLAG_SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014169 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014170 if ((privateFlags & PFLAG_ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070014171 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
14172 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080014173 // This is set if HW acceleration is requested, even if the current
14174 // process doesn't allow it. This is just to allow app preview
14175 // windows to better match their app.
14176 viewStateIndex |= VIEW_STATE_ACCELERATED;
14177 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070014178 if ((privateFlags & PFLAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014179
Christopher Tate3d4bf172011-03-28 16:16:46 -070014180 final int privateFlags2 = mPrivateFlags2;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014181 if ((privateFlags2 & PFLAG2_DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
14182 if ((privateFlags2 & PFLAG2_DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
Christopher Tate3d4bf172011-03-28 16:16:46 -070014183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014184 drawableState = VIEW_STATE_SETS[viewStateIndex];
14185
14186 //noinspection ConstantIfStatement
14187 if (false) {
14188 Log.i("View", "drawableStateIndex=" + viewStateIndex);
14189 Log.i("View", toString()
Dianne Hackborn4702a852012-08-17 15:18:29 -070014190 + " pressed=" + ((privateFlags & PFLAG_PRESSED) != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014191 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
14192 + " fo=" + hasFocus()
Dianne Hackborn4702a852012-08-17 15:18:29 -070014193 + " sl=" + ((privateFlags & PFLAG_SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014194 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014195 + ": " + Arrays.toString(drawableState));
14196 }
14197
14198 if (extraSpace == 0) {
14199 return drawableState;
14200 }
14201
14202 final int[] fullState;
14203 if (drawableState != null) {
14204 fullState = new int[drawableState.length + extraSpace];
14205 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
14206 } else {
14207 fullState = new int[extraSpace];
14208 }
14209
14210 return fullState;
14211 }
14212
14213 /**
14214 * Merge your own state values in <var>additionalState</var> into the base
14215 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014216 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014217 *
14218 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014219 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014220 * own additional state values.
14221 *
14222 * @param additionalState The additional state values you would like
14223 * added to <var>baseState</var>; this array is not modified.
14224 *
14225 * @return As a convenience, the <var>baseState</var> array you originally
14226 * passed into the function is returned.
14227 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014228 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014229 */
14230 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
14231 final int N = baseState.length;
14232 int i = N - 1;
14233 while (i >= 0 && baseState[i] == 0) {
14234 i--;
14235 }
14236 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
14237 return baseState;
14238 }
14239
14240 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070014241 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
14242 * on all Drawable objects associated with this view.
14243 */
14244 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014245 if (mBackground != null) {
14246 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070014247 }
14248 }
14249
14250 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014251 * Sets the background color for this view.
14252 * @param color the color of the background
14253 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014254 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014255 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070014256 if (mBackground instanceof ColorDrawable) {
Romain Guy5f49c302012-09-06 16:33:31 -070014257 ((ColorDrawable) mBackground.mutate()).setColor(color);
Romain Guy7d3082a2012-07-11 17:52:54 -070014258 computeOpaqueFlags();
Chet Haase70d4ba12010-10-06 09:46:45 -070014259 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070014260 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070014261 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014262 }
14263
14264 /**
14265 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070014266 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014267 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070014268 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014269 * @attr ref android.R.styleable#View_background
14270 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014271 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014272 public void setBackgroundResource(int resid) {
14273 if (resid != 0 && resid == mBackgroundResource) {
14274 return;
14275 }
14276
14277 Drawable d= null;
14278 if (resid != 0) {
14279 d = mResources.getDrawable(resid);
14280 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014281 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014282
14283 mBackgroundResource = resid;
14284 }
14285
14286 /**
14287 * Set the background to a given Drawable, or remove the background. If the
14288 * background has padding, this View's padding is set to the background's
14289 * padding. However, when a background is removed, this View's padding isn't
14290 * touched. If setting the padding is desired, please use
14291 * {@link #setPadding(int, int, int, int)}.
14292 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014293 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014294 * background
14295 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014296 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070014297 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070014298 setBackgroundDrawable(background);
14299 }
14300
14301 /**
14302 * @deprecated use {@link #setBackground(Drawable)} instead
14303 */
14304 @Deprecated
14305 public void setBackgroundDrawable(Drawable background) {
Romain Guy846a5332012-07-11 17:44:57 -070014306 computeOpaqueFlags();
14307
Philip Milne6c8ea062012-04-03 17:38:43 -070014308 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070014309 return;
14310 }
14311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014312 boolean requestLayout = false;
14313
14314 mBackgroundResource = 0;
14315
14316 /*
14317 * Regardless of whether we're setting a new background or not, we want
14318 * to clear the previous drawable.
14319 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014320 if (mBackground != null) {
14321 mBackground.setCallback(null);
14322 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014323 }
14324
Philip Milne6c8ea062012-04-03 17:38:43 -070014325 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014326 Rect padding = sThreadLocal.get();
14327 if (padding == null) {
14328 padding = new Rect();
14329 sThreadLocal.set(padding);
14330 }
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014331 background.setLayoutDirection(getResolvedLayoutDirection());
Philip Milne6c8ea062012-04-03 17:38:43 -070014332 if (background.getPadding(padding)) {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014333 // Reset padding resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070014334 mPrivateFlags2 &= ~PFLAG2_PADDING_RESOLVED;
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014335 switch (background.getLayoutDirection()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014336 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014337 internalSetPadding(padding.right, padding.top, padding.left, padding.bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014338 break;
14339 case LAYOUT_DIRECTION_LTR:
14340 default:
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014341 internalSetPadding(padding.left, padding.top, padding.right, padding.bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014342 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014343 }
14344
14345 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
14346 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070014347 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
14348 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014349 requestLayout = true;
14350 }
14351
Philip Milne6c8ea062012-04-03 17:38:43 -070014352 background.setCallback(this);
14353 if (background.isStateful()) {
14354 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014355 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014356 background.setVisible(getVisibility() == VISIBLE, false);
14357 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014358
Dianne Hackborn4702a852012-08-17 15:18:29 -070014359 if ((mPrivateFlags & PFLAG_SKIP_DRAW) != 0) {
14360 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
14361 mPrivateFlags |= PFLAG_ONLY_DRAWS_BACKGROUND;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014362 requestLayout = true;
14363 }
14364 } else {
14365 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070014366 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014367
Dianne Hackborn4702a852012-08-17 15:18:29 -070014368 if ((mPrivateFlags & PFLAG_ONLY_DRAWS_BACKGROUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014369 /*
14370 * This view ONLY drew the background before and we're removing
14371 * the background, so now it won't draw anything
14372 * (hence we SKIP_DRAW)
14373 */
Dianne Hackborn4702a852012-08-17 15:18:29 -070014374 mPrivateFlags &= ~PFLAG_ONLY_DRAWS_BACKGROUND;
14375 mPrivateFlags |= PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014376 }
14377
14378 /*
14379 * When the background is set, we try to apply its padding to this
14380 * View. When the background is removed, we don't touch this View's
14381 * padding. This is noted in the Javadocs. Hence, we don't need to
14382 * requestLayout(), the invalidate() below is sufficient.
14383 */
14384
14385 // The old background's minimum size could have affected this
14386 // View's layout, so let's requestLayout
14387 requestLayout = true;
14388 }
14389
Romain Guy8f1344f52009-05-15 16:03:59 -070014390 computeOpaqueFlags();
14391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014392 if (requestLayout) {
14393 requestLayout();
14394 }
14395
14396 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014397 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014398 }
14399
14400 /**
14401 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070014402 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014403 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070014404 *
14405 * @see #setBackground(Drawable)
14406 *
14407 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014408 */
14409 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014410 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014411 }
14412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014413 /**
14414 * Sets the padding. The view may add on the space required to display
14415 * the scrollbars, depending on the style and visibility of the scrollbars.
14416 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
14417 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
14418 * from the values set in this call.
14419 *
14420 * @attr ref android.R.styleable#View_padding
14421 * @attr ref android.R.styleable#View_paddingBottom
14422 * @attr ref android.R.styleable#View_paddingLeft
14423 * @attr ref android.R.styleable#View_paddingRight
14424 * @attr ref android.R.styleable#View_paddingTop
14425 * @param left the left padding in pixels
14426 * @param top the top padding in pixels
14427 * @param right the right padding in pixels
14428 * @param bottom the bottom padding in pixels
14429 */
14430 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014431 // Reset padding resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070014432 mPrivateFlags2 &= ~PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014433
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014434 mUserPaddingStart = UNDEFINED_PADDING;
14435 mUserPaddingEnd = UNDEFINED_PADDING;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014436
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014437 internalSetPadding(left, top, right, bottom);
14438 }
14439
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014440 /**
14441 * @hide
14442 */
14443 protected void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080014444 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014445 mUserPaddingRight = right;
14446 mUserPaddingBottom = bottom;
14447
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014448 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014449 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070014450
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014451 // Common case is there are no scroll bars.
14452 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014453 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080014454 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014455 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080014456 switch (mVerticalScrollbarPosition) {
14457 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Meglioc91b6ca2012-06-22 14:51:15 -070014458 if (isLayoutRtl()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014459 left += offset;
14460 } else {
14461 right += offset;
14462 }
14463 break;
Adam Powell20232d02010-12-08 21:08:53 -080014464 case SCROLLBAR_POSITION_RIGHT:
14465 right += offset;
14466 break;
14467 case SCROLLBAR_POSITION_LEFT:
14468 left += offset;
14469 break;
14470 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014471 }
Adam Powell20232d02010-12-08 21:08:53 -080014472 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014473 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
14474 ? 0 : getHorizontalScrollbarHeight();
14475 }
14476 }
Romain Guy8506ab42009-06-11 17:35:47 -070014477
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014478 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014479 changed = true;
14480 mPaddingLeft = left;
14481 }
14482 if (mPaddingTop != top) {
14483 changed = true;
14484 mPaddingTop = top;
14485 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014486 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014487 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014488 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014489 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014490 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014491 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014492 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014493 }
14494
14495 if (changed) {
14496 requestLayout();
14497 }
14498 }
14499
14500 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014501 * Sets the relative padding. The view may add on the space required to display
14502 * the scrollbars, depending on the style and visibility of the scrollbars.
14503 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
14504 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
14505 * from the values set in this call.
14506 *
14507 * @attr ref android.R.styleable#View_padding
14508 * @attr ref android.R.styleable#View_paddingBottom
14509 * @attr ref android.R.styleable#View_paddingStart
14510 * @attr ref android.R.styleable#View_paddingEnd
14511 * @attr ref android.R.styleable#View_paddingTop
14512 * @param start the start padding in pixels
14513 * @param top the top padding in pixels
14514 * @param end the end padding in pixels
14515 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014516 */
14517 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014518 // Reset padding resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070014519 mPrivateFlags2 &= ~PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014520
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014521 mUserPaddingStart = start;
14522 mUserPaddingEnd = end;
14523
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014524 switch(getResolvedLayoutDirection()) {
14525 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014526 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014527 break;
14528 case LAYOUT_DIRECTION_LTR:
14529 default:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014530 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014531 }
14532 }
14533
14534 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014535 * Returns the top padding of this view.
14536 *
14537 * @return the top padding in pixels
14538 */
14539 public int getPaddingTop() {
14540 return mPaddingTop;
14541 }
14542
14543 /**
14544 * Returns the bottom padding of this view. If there are inset and enabled
14545 * scrollbars, this value may include the space required to display the
14546 * scrollbars as well.
14547 *
14548 * @return the bottom padding in pixels
14549 */
14550 public int getPaddingBottom() {
14551 return mPaddingBottom;
14552 }
14553
14554 /**
14555 * Returns the left padding of this view. If there are inset and enabled
14556 * scrollbars, this value may include the space required to display the
14557 * scrollbars as well.
14558 *
14559 * @return the left padding in pixels
14560 */
14561 public int getPaddingLeft() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014562 if (!isPaddingResolved()) {
14563 resolvePadding();
14564 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014565 return mPaddingLeft;
14566 }
14567
14568 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014569 * Returns the start padding of this view depending on its resolved layout direction.
14570 * If there are inset and enabled scrollbars, this value may include the space
14571 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014572 *
14573 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014574 */
14575 public int getPaddingStart() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014576 if (!isPaddingResolved()) {
14577 resolvePadding();
14578 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014579 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14580 mPaddingRight : mPaddingLeft;
14581 }
14582
14583 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014584 * Returns the right padding of this view. If there are inset and enabled
14585 * scrollbars, this value may include the space required to display the
14586 * scrollbars as well.
14587 *
14588 * @return the right padding in pixels
14589 */
14590 public int getPaddingRight() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014591 if (!isPaddingResolved()) {
14592 resolvePadding();
14593 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014594 return mPaddingRight;
14595 }
14596
14597 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014598 * Returns the end padding of this view depending on its resolved layout direction.
14599 * If there are inset and enabled scrollbars, this value may include the space
14600 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014601 *
14602 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014603 */
14604 public int getPaddingEnd() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014605 if (!isPaddingResolved()) {
14606 resolvePadding();
14607 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014608 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14609 mPaddingLeft : mPaddingRight;
14610 }
14611
14612 /**
14613 * Return if the padding as been set thru relative values
14614 * {@link #setPaddingRelative(int, int, int, int)} or thru
14615 * @attr ref android.R.styleable#View_paddingStart or
14616 * @attr ref android.R.styleable#View_paddingEnd
14617 *
14618 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014619 */
14620 public boolean isPaddingRelative() {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014621 return (mUserPaddingStart != UNDEFINED_PADDING || mUserPaddingEnd != UNDEFINED_PADDING);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014622 }
14623
14624 /**
Philip Milne1557fd72012-04-04 23:41:34 -070014625 * @hide
14626 */
Philip Milne7a23b492012-04-24 22:12:36 -070014627 public Insets getOpticalInsets() {
Philip Milne1557fd72012-04-04 23:41:34 -070014628 if (mLayoutInsets == null) {
Philip Milnebbd51f12012-04-18 15:09:05 -070014629 mLayoutInsets = (mBackground == null) ? Insets.NONE : mBackground.getLayoutInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070014630 }
14631 return mLayoutInsets;
14632 }
14633
14634 /**
14635 * @hide
14636 */
14637 public void setLayoutInsets(Insets layoutInsets) {
14638 mLayoutInsets = layoutInsets;
14639 }
14640
14641 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014642 * Changes the selection state of this view. A view can be selected or not.
14643 * Note that selection is not the same as focus. Views are typically
14644 * selected in the context of an AdapterView like ListView or GridView;
14645 * the selected view is the view that is highlighted.
14646 *
14647 * @param selected true if the view must be selected, false otherwise
14648 */
14649 public void setSelected(boolean selected) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014650 if (((mPrivateFlags & PFLAG_SELECTED) != 0) != selected) {
14651 mPrivateFlags = (mPrivateFlags & ~PFLAG_SELECTED) | (selected ? PFLAG_SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070014652 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080014653 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014654 refreshDrawableState();
14655 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070014656 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
14657 notifyAccessibilityStateChanged();
14658 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014659 }
14660 }
14661
14662 /**
14663 * Dispatch setSelected to all of this View's children.
14664 *
14665 * @see #setSelected(boolean)
14666 *
14667 * @param selected The new selected state
14668 */
14669 protected void dispatchSetSelected(boolean selected) {
14670 }
14671
14672 /**
14673 * Indicates the selection state of this view.
14674 *
14675 * @return true if the view is selected, false otherwise
14676 */
14677 @ViewDebug.ExportedProperty
14678 public boolean isSelected() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014679 return (mPrivateFlags & PFLAG_SELECTED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014680 }
14681
14682 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014683 * Changes the activated state of this view. A view can be activated or not.
14684 * Note that activation is not the same as selection. Selection is
14685 * a transient property, representing the view (hierarchy) the user is
14686 * currently interacting with. Activation is a longer-term state that the
14687 * user can move views in and out of. For example, in a list view with
14688 * single or multiple selection enabled, the views in the current selection
14689 * set are activated. (Um, yeah, we are deeply sorry about the terminology
14690 * here.) The activated state is propagated down to children of the view it
14691 * is set on.
14692 *
14693 * @param activated true if the view must be activated, false otherwise
14694 */
14695 public void setActivated(boolean activated) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014696 if (((mPrivateFlags & PFLAG_ACTIVATED) != 0) != activated) {
14697 mPrivateFlags = (mPrivateFlags & ~PFLAG_ACTIVATED) | (activated ? PFLAG_ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014698 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014699 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070014700 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014701 }
14702 }
14703
14704 /**
14705 * Dispatch setActivated to all of this View's children.
14706 *
14707 * @see #setActivated(boolean)
14708 *
14709 * @param activated The new activated state
14710 */
14711 protected void dispatchSetActivated(boolean activated) {
14712 }
14713
14714 /**
14715 * Indicates the activation state of this view.
14716 *
14717 * @return true if the view is activated, false otherwise
14718 */
14719 @ViewDebug.ExportedProperty
14720 public boolean isActivated() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014721 return (mPrivateFlags & PFLAG_ACTIVATED) != 0;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014722 }
14723
14724 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014725 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
14726 * observer can be used to get notifications when global events, like
14727 * layout, happen.
14728 *
14729 * The returned ViewTreeObserver observer is not guaranteed to remain
14730 * valid for the lifetime of this View. If the caller of this method keeps
14731 * a long-lived reference to ViewTreeObserver, it should always check for
14732 * the return value of {@link ViewTreeObserver#isAlive()}.
14733 *
14734 * @return The ViewTreeObserver for this view's hierarchy.
14735 */
14736 public ViewTreeObserver getViewTreeObserver() {
14737 if (mAttachInfo != null) {
14738 return mAttachInfo.mTreeObserver;
14739 }
14740 if (mFloatingTreeObserver == null) {
14741 mFloatingTreeObserver = new ViewTreeObserver();
14742 }
14743 return mFloatingTreeObserver;
14744 }
14745
14746 /**
14747 * <p>Finds the topmost view in the current view hierarchy.</p>
14748 *
14749 * @return the topmost view containing this view
14750 */
14751 public View getRootView() {
14752 if (mAttachInfo != null) {
14753 final View v = mAttachInfo.mRootView;
14754 if (v != null) {
14755 return v;
14756 }
14757 }
Romain Guy8506ab42009-06-11 17:35:47 -070014758
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014759 View parent = this;
14760
14761 while (parent.mParent != null && parent.mParent instanceof View) {
14762 parent = (View) parent.mParent;
14763 }
14764
14765 return parent;
14766 }
14767
14768 /**
14769 * <p>Computes the coordinates of this view on the screen. The argument
14770 * must be an array of two integers. After the method returns, the array
14771 * contains the x and y location in that order.</p>
14772 *
14773 * @param location an array of two integers in which to hold the coordinates
14774 */
14775 public void getLocationOnScreen(int[] location) {
14776 getLocationInWindow(location);
14777
14778 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070014779 if (info != null) {
14780 location[0] += info.mWindowLeft;
14781 location[1] += info.mWindowTop;
14782 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014783 }
14784
14785 /**
14786 * <p>Computes the coordinates of this view in its window. The argument
14787 * must be an array of two integers. After the method returns, the array
14788 * contains the x and y location in that order.</p>
14789 *
14790 * @param location an array of two integers in which to hold the coordinates
14791 */
14792 public void getLocationInWindow(int[] location) {
14793 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014794 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014795 }
14796
Gilles Debunne6583ce52011-12-06 18:09:02 -080014797 if (mAttachInfo == null) {
14798 // When the view is not attached to a window, this method does not make sense
14799 location[0] = location[1] = 0;
14800 return;
14801 }
14802
Gilles Debunnecea45132011-11-24 02:19:27 +010014803 float[] position = mAttachInfo.mTmpTransformLocation;
14804 position[0] = position[1] = 0.0f;
14805
14806 if (!hasIdentityMatrix()) {
14807 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014808 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014809
Gilles Debunnecea45132011-11-24 02:19:27 +010014810 position[0] += mLeft;
14811 position[1] += mTop;
14812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014813 ViewParent viewParent = mParent;
14814 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014815 final View view = (View) viewParent;
14816
14817 position[0] -= view.mScrollX;
14818 position[1] -= view.mScrollY;
14819
14820 if (!view.hasIdentityMatrix()) {
14821 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014822 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014823
14824 position[0] += view.mLeft;
14825 position[1] += view.mTop;
14826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014827 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070014828 }
Romain Guy8506ab42009-06-11 17:35:47 -070014829
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014830 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014831 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010014832 final ViewRootImpl vr = (ViewRootImpl) viewParent;
14833 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014834 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014835
14836 location[0] = (int) (position[0] + 0.5f);
14837 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014838 }
14839
14840 /**
14841 * {@hide}
14842 * @param id the id of the view to be found
14843 * @return the view of the specified id, null if cannot be found
14844 */
14845 protected View findViewTraversal(int id) {
14846 if (id == mID) {
14847 return this;
14848 }
14849 return null;
14850 }
14851
14852 /**
14853 * {@hide}
14854 * @param tag the tag of the view to be found
14855 * @return the view of specified tag, null if cannot be found
14856 */
14857 protected View findViewWithTagTraversal(Object tag) {
14858 if (tag != null && tag.equals(mTag)) {
14859 return this;
14860 }
14861 return null;
14862 }
14863
14864 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014865 * {@hide}
14866 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070014867 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080014868 * @return The first view that matches the predicate or null.
14869 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070014870 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080014871 if (predicate.apply(this)) {
14872 return this;
14873 }
14874 return null;
14875 }
14876
14877 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014878 * Look for a child view with the given id. If this view has the given
14879 * id, return this view.
14880 *
14881 * @param id The id to search for.
14882 * @return The view that has the given id in the hierarchy or null
14883 */
14884 public final View findViewById(int id) {
14885 if (id < 0) {
14886 return null;
14887 }
14888 return findViewTraversal(id);
14889 }
14890
14891 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070014892 * Finds a view by its unuque and stable accessibility id.
14893 *
14894 * @param accessibilityId The searched accessibility id.
14895 * @return The found view.
14896 */
14897 final View findViewByAccessibilityId(int accessibilityId) {
14898 if (accessibilityId < 0) {
14899 return null;
14900 }
14901 return findViewByAccessibilityIdTraversal(accessibilityId);
14902 }
14903
14904 /**
14905 * Performs the traversal to find a view by its unuque and stable accessibility id.
14906 *
14907 * <strong>Note:</strong>This method does not stop at the root namespace
14908 * boundary since the user can touch the screen at an arbitrary location
14909 * potentially crossing the root namespace bounday which will send an
14910 * accessibility event to accessibility services and they should be able
14911 * to obtain the event source. Also accessibility ids are guaranteed to be
14912 * unique in the window.
14913 *
14914 * @param accessibilityId The accessibility id.
14915 * @return The found view.
14916 */
14917 View findViewByAccessibilityIdTraversal(int accessibilityId) {
14918 if (getAccessibilityViewId() == accessibilityId) {
14919 return this;
14920 }
14921 return null;
14922 }
14923
14924 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014925 * Look for a child view with the given tag. If this view has the given
14926 * tag, return this view.
14927 *
14928 * @param tag The tag to search for, using "tag.equals(getTag())".
14929 * @return The View that has the given tag in the hierarchy or null
14930 */
14931 public final View findViewWithTag(Object tag) {
14932 if (tag == null) {
14933 return null;
14934 }
14935 return findViewWithTagTraversal(tag);
14936 }
14937
14938 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014939 * {@hide}
14940 * Look for a child view that matches the specified predicate.
14941 * If this view matches the predicate, return this view.
14942 *
14943 * @param predicate The predicate to evaluate.
14944 * @return The first view that matches the predicate or null.
14945 */
14946 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070014947 return findViewByPredicateTraversal(predicate, null);
14948 }
14949
14950 /**
14951 * {@hide}
14952 * Look for a child view that matches the specified predicate,
14953 * starting with the specified view and its descendents and then
14954 * recusively searching the ancestors and siblings of that view
14955 * until this view is reached.
14956 *
14957 * This method is useful in cases where the predicate does not match
14958 * a single unique view (perhaps multiple views use the same id)
14959 * and we are trying to find the view that is "closest" in scope to the
14960 * starting view.
14961 *
14962 * @param start The view to start from.
14963 * @param predicate The predicate to evaluate.
14964 * @return The first view that matches the predicate or null.
14965 */
14966 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
14967 View childToSkip = null;
14968 for (;;) {
14969 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
14970 if (view != null || start == this) {
14971 return view;
14972 }
14973
14974 ViewParent parent = start.getParent();
14975 if (parent == null || !(parent instanceof View)) {
14976 return null;
14977 }
14978
14979 childToSkip = start;
14980 start = (View) parent;
14981 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080014982 }
14983
14984 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014985 * Sets the identifier for this view. The identifier does not have to be
14986 * unique in this view's hierarchy. The identifier should be a positive
14987 * number.
14988 *
14989 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070014990 * @see #getId()
14991 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014992 *
14993 * @param id a number used to identify the view
14994 *
14995 * @attr ref android.R.styleable#View_id
14996 */
14997 public void setId(int id) {
14998 mID = id;
Svetoslav Ganov33aef982012-09-13 12:49:03 -070014999 if (mID == View.NO_ID && mLabelForId != View.NO_ID) {
15000 mID = generateViewId();
15001 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015002 }
15003
15004 /**
15005 * {@hide}
15006 *
15007 * @param isRoot true if the view belongs to the root namespace, false
15008 * otherwise
15009 */
15010 public void setIsRootNamespace(boolean isRoot) {
15011 if (isRoot) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015012 mPrivateFlags |= PFLAG_IS_ROOT_NAMESPACE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015013 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015014 mPrivateFlags &= ~PFLAG_IS_ROOT_NAMESPACE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015015 }
15016 }
15017
15018 /**
15019 * {@hide}
15020 *
15021 * @return true if the view belongs to the root namespace, false otherwise
15022 */
15023 public boolean isRootNamespace() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015024 return (mPrivateFlags&PFLAG_IS_ROOT_NAMESPACE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015025 }
15026
15027 /**
15028 * Returns this view's identifier.
15029 *
15030 * @return a positive integer used to identify the view or {@link #NO_ID}
15031 * if the view has no ID
15032 *
Philip Milne6c8ea062012-04-03 17:38:43 -070015033 * @see #setId(int)
15034 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015035 * @attr ref android.R.styleable#View_id
15036 */
15037 @ViewDebug.CapturedViewProperty
15038 public int getId() {
15039 return mID;
15040 }
15041
15042 /**
15043 * Returns this view's tag.
15044 *
15045 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070015046 *
15047 * @see #setTag(Object)
15048 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015049 */
15050 @ViewDebug.ExportedProperty
15051 public Object getTag() {
15052 return mTag;
15053 }
15054
15055 /**
15056 * Sets the tag associated with this view. A tag can be used to mark
15057 * a view in its hierarchy and does not have to be unique within the
15058 * hierarchy. Tags can also be used to store data within a view without
15059 * resorting to another data structure.
15060 *
15061 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070015062 *
15063 * @see #getTag()
15064 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015065 */
15066 public void setTag(final Object tag) {
15067 mTag = tag;
15068 }
15069
15070 /**
Romain Guyd90a3312009-05-06 14:54:28 -070015071 * Returns the tag associated with this view and the specified key.
15072 *
15073 * @param key The key identifying the tag
15074 *
15075 * @return the Object stored in this view as a tag
15076 *
15077 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070015078 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070015079 */
15080 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070015081 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070015082 return null;
15083 }
15084
15085 /**
15086 * Sets a tag associated with this view and a key. A tag can be used
15087 * to mark a view in its hierarchy and does not have to be unique within
15088 * the hierarchy. Tags can also be used to store data within a view
15089 * without resorting to another data structure.
15090 *
15091 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070015092 * application to ensure it is unique (see the <a
15093 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
15094 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070015095 * the Android framework or not associated with any package will cause
15096 * an {@link IllegalArgumentException} to be thrown.
15097 *
15098 * @param key The key identifying the tag
15099 * @param tag An Object to tag the view with
15100 *
15101 * @throws IllegalArgumentException If they specified key is not valid
15102 *
15103 * @see #setTag(Object)
15104 * @see #getTag(int)
15105 */
15106 public void setTag(int key, final Object tag) {
15107 // If the package id is 0x00 or 0x01, it's either an undefined package
15108 // or a framework id
15109 if ((key >>> 24) < 2) {
15110 throw new IllegalArgumentException("The key must be an application-specific "
15111 + "resource id.");
15112 }
15113
Adam Powell2b2f6d62011-09-23 11:15:39 -070015114 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015115 }
15116
15117 /**
15118 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
15119 * framework id.
15120 *
15121 * @hide
15122 */
15123 public void setTagInternal(int key, Object tag) {
15124 if ((key >>> 24) != 0x1) {
15125 throw new IllegalArgumentException("The key must be a framework-specific "
15126 + "resource id.");
15127 }
15128
Adam Powell2b2f6d62011-09-23 11:15:39 -070015129 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015130 }
15131
Adam Powell2b2f6d62011-09-23 11:15:39 -070015132 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070015133 if (mKeyedTags == null) {
15134 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070015135 }
15136
Adam Powell7db82ac2011-09-22 19:44:04 -070015137 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015138 }
15139
15140 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015141 * Prints information about this view in the log output, with the tag
15142 * {@link #VIEW_LOG_TAG}.
15143 *
15144 * @hide
15145 */
15146 public void debug() {
15147 debug(0);
15148 }
15149
15150 /**
15151 * Prints information about this view in the log output, with the tag
15152 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
15153 * indentation defined by the <code>depth</code>.
15154 *
15155 * @param depth the indentation level
15156 *
15157 * @hide
15158 */
15159 protected void debug(int depth) {
15160 String output = debugIndent(depth - 1);
15161
15162 output += "+ " + this;
15163 int id = getId();
15164 if (id != -1) {
15165 output += " (id=" + id + ")";
15166 }
15167 Object tag = getTag();
15168 if (tag != null) {
15169 output += " (tag=" + tag + ")";
15170 }
15171 Log.d(VIEW_LOG_TAG, output);
15172
Dianne Hackborn4702a852012-08-17 15:18:29 -070015173 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015174 output = debugIndent(depth) + " FOCUSED";
15175 Log.d(VIEW_LOG_TAG, output);
15176 }
15177
15178 output = debugIndent(depth);
15179 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
15180 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
15181 + "} ";
15182 Log.d(VIEW_LOG_TAG, output);
15183
15184 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
15185 || mPaddingBottom != 0) {
15186 output = debugIndent(depth);
15187 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
15188 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
15189 Log.d(VIEW_LOG_TAG, output);
15190 }
15191
15192 output = debugIndent(depth);
15193 output += "mMeasureWidth=" + mMeasuredWidth +
15194 " mMeasureHeight=" + mMeasuredHeight;
15195 Log.d(VIEW_LOG_TAG, output);
15196
15197 output = debugIndent(depth);
15198 if (mLayoutParams == null) {
15199 output += "BAD! no layout params";
15200 } else {
15201 output = mLayoutParams.debug(output);
15202 }
15203 Log.d(VIEW_LOG_TAG, output);
15204
15205 output = debugIndent(depth);
15206 output += "flags={";
15207 output += View.printFlags(mViewFlags);
15208 output += "}";
15209 Log.d(VIEW_LOG_TAG, output);
15210
15211 output = debugIndent(depth);
15212 output += "privateFlags={";
15213 output += View.printPrivateFlags(mPrivateFlags);
15214 output += "}";
15215 Log.d(VIEW_LOG_TAG, output);
15216 }
15217
15218 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090015219 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015220 *
15221 * @param depth the indentation level
15222 * @return a String containing (depth * 2 + 3) * 2 white spaces
15223 *
15224 * @hide
15225 */
15226 protected static String debugIndent(int depth) {
15227 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
15228 for (int i = 0; i < (depth * 2) + 3; i++) {
15229 spaces.append(' ').append(' ');
15230 }
15231 return spaces.toString();
15232 }
15233
15234 /**
15235 * <p>Return the offset of the widget's text baseline from the widget's top
15236 * boundary. If this widget does not support baseline alignment, this
15237 * method returns -1. </p>
15238 *
15239 * @return the offset of the baseline within the widget's bounds or -1
15240 * if baseline alignment is not supported
15241 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070015242 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015243 public int getBaseline() {
15244 return -1;
15245 }
15246
15247 /**
15248 * Call this when something has changed which has invalidated the
15249 * layout of this view. This will schedule a layout pass of the view
15250 * tree.
15251 */
15252 public void requestLayout() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015253 mPrivateFlags |= PFLAG_FORCE_LAYOUT;
15254 mPrivateFlags |= PFLAG_INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015255
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070015256 if (mParent != null && !mParent.isLayoutRequested()) {
15257 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015258 }
15259 }
15260
15261 /**
15262 * Forces this view to be laid out during the next layout pass.
15263 * This method does not call requestLayout() or forceLayout()
15264 * on the parent.
15265 */
15266 public void forceLayout() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015267 mPrivateFlags |= PFLAG_FORCE_LAYOUT;
15268 mPrivateFlags |= PFLAG_INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015269 }
15270
15271 /**
15272 * <p>
15273 * This is called to find out how big a view should be. The parent
15274 * supplies constraint information in the width and height parameters.
15275 * </p>
15276 *
15277 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080015278 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015279 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080015280 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015281 * </p>
15282 *
15283 *
15284 * @param widthMeasureSpec Horizontal space requirements as imposed by the
15285 * parent
15286 * @param heightMeasureSpec Vertical space requirements as imposed by the
15287 * parent
15288 *
15289 * @see #onMeasure(int, int)
15290 */
15291 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015292 if ((mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015293 widthMeasureSpec != mOldWidthMeasureSpec ||
15294 heightMeasureSpec != mOldHeightMeasureSpec) {
15295
15296 // first clears the measured dimension flag
Dianne Hackborn4702a852012-08-17 15:18:29 -070015297 mPrivateFlags &= ~PFLAG_MEASURED_DIMENSION_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015298
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070015299 if (!isPaddingResolved()) {
Adam Powell0090f202012-08-07 17:15:30 -070015300 resolvePadding();
15301 }
15302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015303 // measure ourselves, this should set the measured dimension flag back
15304 onMeasure(widthMeasureSpec, heightMeasureSpec);
15305
15306 // flag not set, setMeasuredDimension() was not invoked, we raise
15307 // an exception to warn the developer
Dianne Hackborn4702a852012-08-17 15:18:29 -070015308 if ((mPrivateFlags & PFLAG_MEASURED_DIMENSION_SET) != PFLAG_MEASURED_DIMENSION_SET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015309 throw new IllegalStateException("onMeasure() did not set the"
15310 + " measured dimension by calling"
15311 + " setMeasuredDimension()");
15312 }
15313
Dianne Hackborn4702a852012-08-17 15:18:29 -070015314 mPrivateFlags |= PFLAG_LAYOUT_REQUIRED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015315 }
15316
15317 mOldWidthMeasureSpec = widthMeasureSpec;
15318 mOldHeightMeasureSpec = heightMeasureSpec;
15319 }
15320
15321 /**
15322 * <p>
15323 * Measure the view and its content to determine the measured width and the
15324 * measured height. This method is invoked by {@link #measure(int, int)} and
15325 * should be overriden by subclasses to provide accurate and efficient
15326 * measurement of their contents.
15327 * </p>
15328 *
15329 * <p>
15330 * <strong>CONTRACT:</strong> When overriding this method, you
15331 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
15332 * measured width and height of this view. Failure to do so will trigger an
15333 * <code>IllegalStateException</code>, thrown by
15334 * {@link #measure(int, int)}. Calling the superclass'
15335 * {@link #onMeasure(int, int)} is a valid use.
15336 * </p>
15337 *
15338 * <p>
15339 * The base class implementation of measure defaults to the background size,
15340 * unless a larger size is allowed by the MeasureSpec. Subclasses should
15341 * override {@link #onMeasure(int, int)} to provide better measurements of
15342 * their content.
15343 * </p>
15344 *
15345 * <p>
15346 * If this method is overridden, it is the subclass's responsibility to make
15347 * sure the measured height and width are at least the view's minimum height
15348 * and width ({@link #getSuggestedMinimumHeight()} and
15349 * {@link #getSuggestedMinimumWidth()}).
15350 * </p>
15351 *
15352 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
15353 * The requirements are encoded with
15354 * {@link android.view.View.MeasureSpec}.
15355 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
15356 * The requirements are encoded with
15357 * {@link android.view.View.MeasureSpec}.
15358 *
15359 * @see #getMeasuredWidth()
15360 * @see #getMeasuredHeight()
15361 * @see #setMeasuredDimension(int, int)
15362 * @see #getSuggestedMinimumHeight()
15363 * @see #getSuggestedMinimumWidth()
15364 * @see android.view.View.MeasureSpec#getMode(int)
15365 * @see android.view.View.MeasureSpec#getSize(int)
15366 */
15367 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
15368 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
15369 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
15370 }
15371
15372 /**
15373 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
15374 * measured width and measured height. Failing to do so will trigger an
15375 * exception at measurement time.</p>
15376 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080015377 * @param measuredWidth The measured width of this view. May be a complex
15378 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15379 * {@link #MEASURED_STATE_TOO_SMALL}.
15380 * @param measuredHeight The measured height of this view. May be a complex
15381 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15382 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015383 */
15384 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
15385 mMeasuredWidth = measuredWidth;
15386 mMeasuredHeight = measuredHeight;
15387
Dianne Hackborn4702a852012-08-17 15:18:29 -070015388 mPrivateFlags |= PFLAG_MEASURED_DIMENSION_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015389 }
15390
15391 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080015392 * Merge two states as returned by {@link #getMeasuredState()}.
15393 * @param curState The current state as returned from a view or the result
15394 * of combining multiple views.
15395 * @param newState The new view state to combine.
15396 * @return Returns a new integer reflecting the combination of the two
15397 * states.
15398 */
15399 public static int combineMeasuredStates(int curState, int newState) {
15400 return curState | newState;
15401 }
15402
15403 /**
15404 * Version of {@link #resolveSizeAndState(int, int, int)}
15405 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
15406 */
15407 public static int resolveSize(int size, int measureSpec) {
15408 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
15409 }
15410
15411 /**
15412 * Utility to reconcile a desired size and state, with constraints imposed
15413 * by a MeasureSpec. Will take the desired size, unless a different size
15414 * is imposed by the constraints. The returned value is a compound integer,
15415 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
15416 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
15417 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015418 *
15419 * @param size How big the view wants to be
15420 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080015421 * @return Size information bit mask as defined by
15422 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015423 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080015424 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015425 int result = size;
15426 int specMode = MeasureSpec.getMode(measureSpec);
15427 int specSize = MeasureSpec.getSize(measureSpec);
15428 switch (specMode) {
15429 case MeasureSpec.UNSPECIFIED:
15430 result = size;
15431 break;
15432 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080015433 if (specSize < size) {
15434 result = specSize | MEASURED_STATE_TOO_SMALL;
15435 } else {
15436 result = size;
15437 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015438 break;
15439 case MeasureSpec.EXACTLY:
15440 result = specSize;
15441 break;
15442 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080015443 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015444 }
15445
15446 /**
15447 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070015448 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015449 * by the MeasureSpec.
15450 *
15451 * @param size Default size for this view
15452 * @param measureSpec Constraints imposed by the parent
15453 * @return The size this view should be.
15454 */
15455 public static int getDefaultSize(int size, int measureSpec) {
15456 int result = size;
15457 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070015458 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015459
15460 switch (specMode) {
15461 case MeasureSpec.UNSPECIFIED:
15462 result = size;
15463 break;
15464 case MeasureSpec.AT_MOST:
15465 case MeasureSpec.EXACTLY:
15466 result = specSize;
15467 break;
15468 }
15469 return result;
15470 }
15471
15472 /**
15473 * Returns the suggested minimum height that the view should use. This
15474 * returns the maximum of the view's minimum height
15475 * and the background's minimum height
15476 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
15477 * <p>
15478 * When being used in {@link #onMeasure(int, int)}, the caller should still
15479 * ensure the returned height is within the requirements of the parent.
15480 *
15481 * @return The suggested minimum height of the view.
15482 */
15483 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015484 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015486 }
15487
15488 /**
15489 * Returns the suggested minimum width that the view should use. This
15490 * returns the maximum of the view's minimum width)
15491 * and the background's minimum width
15492 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
15493 * <p>
15494 * When being used in {@link #onMeasure(int, int)}, the caller should still
15495 * ensure the returned width is within the requirements of the parent.
15496 *
15497 * @return The suggested minimum width of the view.
15498 */
15499 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015500 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
15501 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015502
Philip Milne6c8ea062012-04-03 17:38:43 -070015503 /**
15504 * Returns the minimum height of the view.
15505 *
15506 * @return the minimum height the view will try to be.
15507 *
15508 * @see #setMinimumHeight(int)
15509 *
15510 * @attr ref android.R.styleable#View_minHeight
15511 */
15512 public int getMinimumHeight() {
15513 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015514 }
15515
15516 /**
15517 * Sets the minimum height of the view. It is not guaranteed the view will
15518 * be able to achieve this minimum height (for example, if its parent layout
15519 * constrains it with less available height).
15520 *
15521 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015522 *
15523 * @see #getMinimumHeight()
15524 *
15525 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015526 */
15527 public void setMinimumHeight(int minHeight) {
15528 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070015529 requestLayout();
15530 }
15531
15532 /**
15533 * Returns the minimum width of the view.
15534 *
15535 * @return the minimum width the view will try to be.
15536 *
15537 * @see #setMinimumWidth(int)
15538 *
15539 * @attr ref android.R.styleable#View_minWidth
15540 */
15541 public int getMinimumWidth() {
15542 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015543 }
15544
15545 /**
15546 * Sets the minimum width of the view. It is not guaranteed the view will
15547 * be able to achieve this minimum width (for example, if its parent layout
15548 * constrains it with less available width).
15549 *
15550 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015551 *
15552 * @see #getMinimumWidth()
15553 *
15554 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015555 */
15556 public void setMinimumWidth(int minWidth) {
15557 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070015558 requestLayout();
15559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015560 }
15561
15562 /**
15563 * Get the animation currently associated with this view.
15564 *
15565 * @return The animation that is currently playing or
15566 * scheduled to play for this view.
15567 */
15568 public Animation getAnimation() {
15569 return mCurrentAnimation;
15570 }
15571
15572 /**
15573 * Start the specified animation now.
15574 *
15575 * @param animation the animation to start now
15576 */
15577 public void startAnimation(Animation animation) {
15578 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
15579 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080015580 invalidateParentCaches();
15581 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015582 }
15583
15584 /**
15585 * Cancels any animations for this view.
15586 */
15587 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080015588 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080015589 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080015590 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015591 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080015592 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015593 }
15594
15595 /**
15596 * Sets the next animation to play for this view.
15597 * If you want the animation to play immediately, use
Chet Haase42428932012-05-11 15:39:07 -070015598 * {@link #startAnimation(android.view.animation.Animation)} instead.
15599 * This method provides allows fine-grained
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015600 * control over the start time and invalidation, but you
15601 * must make sure that 1) the animation has a start time set, and
Chet Haase42428932012-05-11 15:39:07 -070015602 * 2) the view's parent (which controls animations on its children)
15603 * will be invalidated when the animation is supposed to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015604 * start.
15605 *
15606 * @param animation The next animation, or null.
15607 */
15608 public void setAnimation(Animation animation) {
15609 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070015610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015611 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070015612 // If the screen is off assume the animation start time is now instead of
15613 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
15614 // would cause the animation to start when the screen turns back on
15615 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
15616 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
15617 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
15618 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015619 animation.reset();
15620 }
15621 }
15622
15623 /**
15624 * Invoked by a parent ViewGroup to notify the start of the animation
15625 * currently associated with this view. If you override this method,
15626 * always call super.onAnimationStart();
15627 *
15628 * @see #setAnimation(android.view.animation.Animation)
15629 * @see #getAnimation()
15630 */
15631 protected void onAnimationStart() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015632 mPrivateFlags |= PFLAG_ANIMATION_STARTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015633 }
15634
15635 /**
15636 * Invoked by a parent ViewGroup to notify the end of the animation
15637 * currently associated with this view. If you override this method,
15638 * always call super.onAnimationEnd();
15639 *
15640 * @see #setAnimation(android.view.animation.Animation)
15641 * @see #getAnimation()
15642 */
15643 protected void onAnimationEnd() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015644 mPrivateFlags &= ~PFLAG_ANIMATION_STARTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015645 }
15646
15647 /**
15648 * Invoked if there is a Transform that involves alpha. Subclass that can
15649 * draw themselves with the specified alpha should return true, and then
15650 * respect that alpha when their onDraw() is called. If this returns false
15651 * then the view may be redirected to draw into an offscreen buffer to
15652 * fulfill the request, which will look fine, but may be slower than if the
15653 * subclass handles it internally. The default implementation returns false.
15654 *
15655 * @param alpha The alpha (0..255) to apply to the view's drawing
15656 * @return true if the view can draw with the specified alpha.
15657 */
15658 protected boolean onSetAlpha(int alpha) {
15659 return false;
15660 }
15661
15662 /**
15663 * This is used by the RootView to perform an optimization when
15664 * the view hierarchy contains one or several SurfaceView.
15665 * SurfaceView is always considered transparent, but its children are not,
15666 * therefore all View objects remove themselves from the global transparent
15667 * region (passed as a parameter to this function).
15668 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015669 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015670 *
15671 * @return Returns true if the effective visibility of the view at this
15672 * point is opaque, regardless of the transparent region; returns false
15673 * if it is possible for underlying windows to be seen behind the view.
15674 *
15675 * {@hide}
15676 */
15677 public boolean gatherTransparentRegion(Region region) {
15678 final AttachInfo attachInfo = mAttachInfo;
15679 if (region != null && attachInfo != null) {
15680 final int pflags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070015681 if ((pflags & PFLAG_SKIP_DRAW) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015682 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
15683 // remove it from the transparent region.
15684 final int[] location = attachInfo.mTransparentLocation;
15685 getLocationInWindow(location);
15686 region.op(location[0], location[1], location[0] + mRight - mLeft,
15687 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Dianne Hackborn4702a852012-08-17 15:18:29 -070015688 } else if ((pflags & PFLAG_ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015689 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
15690 // exists, so we remove the background drawable's non-transparent
15691 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070015692 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015693 }
15694 }
15695 return true;
15696 }
15697
15698 /**
15699 * Play a sound effect for this view.
15700 *
15701 * <p>The framework will play sound effects for some built in actions, such as
15702 * clicking, but you may wish to play these effects in your widget,
15703 * for instance, for internal navigation.
15704 *
15705 * <p>The sound effect will only be played if sound effects are enabled by the user, and
15706 * {@link #isSoundEffectsEnabled()} is true.
15707 *
15708 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
15709 */
15710 public void playSoundEffect(int soundConstant) {
15711 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
15712 return;
15713 }
15714 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
15715 }
15716
15717 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015718 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015719 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015720 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015721 *
15722 * <p>The framework will provide haptic feedback for some built in actions,
15723 * such as long presses, but you may wish to provide feedback for your
15724 * own widget.
15725 *
15726 * <p>The feedback will only be performed if
15727 * {@link #isHapticFeedbackEnabled()} is true.
15728 *
15729 * @param feedbackConstant One of the constants defined in
15730 * {@link HapticFeedbackConstants}
15731 */
15732 public boolean performHapticFeedback(int feedbackConstant) {
15733 return performHapticFeedback(feedbackConstant, 0);
15734 }
15735
15736 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015737 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015738 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015739 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015740 *
15741 * @param feedbackConstant One of the constants defined in
15742 * {@link HapticFeedbackConstants}
15743 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
15744 */
15745 public boolean performHapticFeedback(int feedbackConstant, int flags) {
15746 if (mAttachInfo == null) {
15747 return false;
15748 }
Romain Guyf607bdc2010-09-10 19:20:06 -070015749 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070015750 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015751 && !isHapticFeedbackEnabled()) {
15752 return false;
15753 }
Romain Guy812ccbe2010-06-01 14:07:24 -070015754 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
15755 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015756 }
15757
15758 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015759 * Request that the visibility of the status bar or other screen/window
15760 * decorations be changed.
15761 *
15762 * <p>This method is used to put the over device UI into temporary modes
15763 * where the user's attention is focused more on the application content,
15764 * by dimming or hiding surrounding system affordances. This is typically
15765 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
15766 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
15767 * to be placed behind the action bar (and with these flags other system
15768 * affordances) so that smooth transitions between hiding and showing them
15769 * can be done.
15770 *
15771 * <p>Two representative examples of the use of system UI visibility is
15772 * implementing a content browsing application (like a magazine reader)
15773 * and a video playing application.
15774 *
15775 * <p>The first code shows a typical implementation of a View in a content
15776 * browsing application. In this implementation, the application goes
15777 * into a content-oriented mode by hiding the status bar and action bar,
15778 * and putting the navigation elements into lights out mode. The user can
15779 * then interact with content while in this mode. Such an application should
15780 * provide an easy way for the user to toggle out of the mode (such as to
15781 * check information in the status bar or access notifications). In the
15782 * implementation here, this is done simply by tapping on the content.
15783 *
15784 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
15785 * content}
15786 *
15787 * <p>This second code sample shows a typical implementation of a View
15788 * in a video playing application. In this situation, while the video is
15789 * playing the application would like to go into a complete full-screen mode,
15790 * to use as much of the display as possible for the video. When in this state
15791 * the user can not interact with the application; the system intercepts
Dianne Hackborncf675782012-05-10 15:07:24 -070015792 * touching on the screen to pop the UI out of full screen mode. See
15793 * {@link #fitSystemWindows(Rect)} for a sample layout that goes with this code.
Dianne Hackborn98014352012-04-05 18:31:41 -070015794 *
15795 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
15796 * content}
15797 *
15798 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15799 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15800 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15801 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015802 */
15803 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040015804 if (visibility != mSystemUiVisibility) {
15805 mSystemUiVisibility = visibility;
15806 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15807 mParent.recomputeViewAttributes(this);
15808 }
Joe Onorato664644d2011-01-23 17:53:23 -080015809 }
15810 }
15811
15812 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015813 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
15814 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15815 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15816 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15817 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015818 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080015819 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080015820 return mSystemUiVisibility;
15821 }
15822
Scott Mainec6331b2011-05-24 16:55:56 -070015823 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070015824 * Returns the current system UI visibility that is currently set for
15825 * the entire window. This is the combination of the
15826 * {@link #setSystemUiVisibility(int)} values supplied by all of the
15827 * views in the window.
15828 */
15829 public int getWindowSystemUiVisibility() {
15830 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
15831 }
15832
15833 /**
15834 * Override to find out when the window's requested system UI visibility
15835 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
15836 * This is different from the callbacks recieved through
15837 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
15838 * in that this is only telling you about the local request of the window,
15839 * not the actual values applied by the system.
15840 */
15841 public void onWindowSystemUiVisibilityChanged(int visible) {
15842 }
15843
15844 /**
15845 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
15846 * the view hierarchy.
15847 */
15848 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
15849 onWindowSystemUiVisibilityChanged(visible);
15850 }
15851
15852 /**
Scott Mainec6331b2011-05-24 16:55:56 -070015853 * Set a listener to receive callbacks when the visibility of the system bar changes.
15854 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
15855 */
Joe Onorato664644d2011-01-23 17:53:23 -080015856 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015857 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080015858 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15859 mParent.recomputeViewAttributes(this);
15860 }
15861 }
15862
15863 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015864 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
15865 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080015866 */
15867 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015868 ListenerInfo li = mListenerInfo;
15869 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
15870 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080015871 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080015872 }
15873 }
15874
Dianne Hackborncf675782012-05-10 15:07:24 -070015875 boolean updateLocalSystemUiVisibility(int localValue, int localChanges) {
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015876 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
15877 if (val != mSystemUiVisibility) {
15878 setSystemUiVisibility(val);
Dianne Hackborncf675782012-05-10 15:07:24 -070015879 return true;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015880 }
Dianne Hackborncf675782012-05-10 15:07:24 -070015881 return false;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015882 }
15883
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070015884 /** @hide */
15885 public void setDisabledSystemUiVisibility(int flags) {
15886 if (mAttachInfo != null) {
15887 if (mAttachInfo.mDisabledSystemUiVisibility != flags) {
15888 mAttachInfo.mDisabledSystemUiVisibility = flags;
15889 if (mParent != null) {
15890 mParent.recomputeViewAttributes(this);
15891 }
15892 }
15893 }
15894 }
15895
Joe Onorato664644d2011-01-23 17:53:23 -080015896 /**
Joe Malin32736f02011-01-19 16:14:20 -080015897 * Creates an image that the system displays during the drag and drop
15898 * operation. This is called a &quot;drag shadow&quot;. The default implementation
15899 * for a DragShadowBuilder based on a View returns an image that has exactly the same
15900 * appearance as the given View. The default also positions the center of the drag shadow
15901 * directly under the touch point. If no View is provided (the constructor with no parameters
15902 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
15903 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
15904 * default is an invisible drag shadow.
15905 * <p>
15906 * You are not required to use the View you provide to the constructor as the basis of the
15907 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
15908 * anything you want as the drag shadow.
15909 * </p>
15910 * <p>
15911 * You pass a DragShadowBuilder object to the system when you start the drag. The system
15912 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
15913 * size and position of the drag shadow. It uses this data to construct a
15914 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
15915 * so that your application can draw the shadow image in the Canvas.
15916 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070015917 *
15918 * <div class="special reference">
15919 * <h3>Developer Guides</h3>
15920 * <p>For a guide to implementing drag and drop features, read the
15921 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
15922 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070015923 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015924 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070015925 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070015926
15927 /**
Joe Malin32736f02011-01-19 16:14:20 -080015928 * Constructs a shadow image builder based on a View. By default, the resulting drag
15929 * shadow will have the same appearance and dimensions as the View, with the touch point
15930 * over the center of the View.
15931 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070015932 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015933 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070015934 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070015935 }
15936
Christopher Tate17ed60c2011-01-18 12:50:26 -080015937 /**
15938 * Construct a shadow builder object with no associated View. This
15939 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
15940 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
15941 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080015942 * reference to any View object. If they are not overridden, then the result is an
15943 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080015944 */
15945 public DragShadowBuilder() {
15946 mView = new WeakReference<View>(null);
15947 }
15948
15949 /**
15950 * Returns the View object that had been passed to the
15951 * {@link #View.DragShadowBuilder(View)}
15952 * constructor. If that View parameter was {@code null} or if the
15953 * {@link #View.DragShadowBuilder()}
15954 * constructor was used to instantiate the builder object, this method will return
15955 * null.
15956 *
15957 * @return The View object associate with this builder object.
15958 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070015959 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070015960 final public View getView() {
15961 return mView.get();
15962 }
15963
Christopher Tate2c095f32010-10-04 14:13:40 -070015964 /**
Joe Malin32736f02011-01-19 16:14:20 -080015965 * Provides the metrics for the shadow image. These include the dimensions of
15966 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070015967 * be centered under the touch location while dragging.
15968 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015969 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080015970 * same as the dimensions of the View itself and centers the shadow under
15971 * the touch point.
15972 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070015973 *
Joe Malin32736f02011-01-19 16:14:20 -080015974 * @param shadowSize A {@link android.graphics.Point} containing the width and height
15975 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
15976 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
15977 * image.
15978 *
15979 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
15980 * shadow image that should be underneath the touch point during the drag and drop
15981 * operation. Your application must set {@link android.graphics.Point#x} to the
15982 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070015983 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015984 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070015985 final View view = mView.get();
15986 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015987 shadowSize.set(view.getWidth(), view.getHeight());
15988 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070015989 } else {
15990 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
15991 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015992 }
15993
15994 /**
Joe Malin32736f02011-01-19 16:14:20 -080015995 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
15996 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015997 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070015998 *
Joe Malin32736f02011-01-19 16:14:20 -080015999 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070016000 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016001 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070016002 final View view = mView.get();
16003 if (view != null) {
16004 view.draw(canvas);
16005 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016006 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070016007 }
Christopher Tate2c095f32010-10-04 14:13:40 -070016008 }
16009 }
16010
16011 /**
Joe Malin32736f02011-01-19 16:14:20 -080016012 * Starts a drag and drop operation. When your application calls this method, it passes a
16013 * {@link android.view.View.DragShadowBuilder} object to the system. The
16014 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
16015 * to get metrics for the drag shadow, and then calls the object's
16016 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
16017 * <p>
16018 * Once the system has the drag shadow, it begins the drag and drop operation by sending
16019 * drag events to all the View objects in your application that are currently visible. It does
16020 * this either by calling the View object's drag listener (an implementation of
16021 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
16022 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
16023 * Both are passed a {@link android.view.DragEvent} object that has a
16024 * {@link android.view.DragEvent#getAction()} value of
16025 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
16026 * </p>
16027 * <p>
16028 * Your application can invoke startDrag() on any attached View object. The View object does not
16029 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
16030 * be related to the View the user selected for dragging.
16031 * </p>
16032 * @param data A {@link android.content.ClipData} object pointing to the data to be
16033 * transferred by the drag and drop operation.
16034 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
16035 * drag shadow.
16036 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
16037 * drop operation. This Object is put into every DragEvent object sent by the system during the
16038 * current drag.
16039 * <p>
16040 * myLocalState is a lightweight mechanism for the sending information from the dragged View
16041 * to the target Views. For example, it can contain flags that differentiate between a
16042 * a copy operation and a move operation.
16043 * </p>
16044 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
16045 * so the parameter should be set to 0.
16046 * @return {@code true} if the method completes successfully, or
16047 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
16048 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070016049 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016050 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080016051 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070016052 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080016053 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070016054 }
16055 boolean okay = false;
16056
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016057 Point shadowSize = new Point();
16058 Point shadowTouchPoint = new Point();
16059 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070016060
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016061 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
16062 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
16063 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070016064 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016065
Chris Tatea32dcf72010-10-14 12:13:50 -070016066 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016067 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
16068 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070016069 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016070 Surface surface = new Surface();
16071 try {
16072 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080016073 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070016074 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070016075 + " surface=" + surface);
16076 if (token != null) {
16077 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070016078 try {
Chris Tate6b391282010-10-14 15:48:59 -070016079 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016080 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070016081 } finally {
16082 surface.unlockCanvasAndPost(canvas);
16083 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016084
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070016085 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080016086
16087 // Cache the local state object for delivery with DragEvents
16088 root.setLocalDragState(myLocalState);
16089
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016090 // repurpose 'shadowSize' for the last touch point
16091 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070016092
Christopher Tatea53146c2010-09-07 11:57:52 -070016093 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016094 shadowSize.x, shadowSize.y,
16095 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070016096 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070016097
16098 // Off and running! Release our local surface instance; the drag
16099 // shadow surface is now managed by the system process.
16100 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070016101 }
16102 } catch (Exception e) {
16103 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
16104 surface.destroy();
16105 }
16106
16107 return okay;
16108 }
16109
Christopher Tatea53146c2010-09-07 11:57:52 -070016110 /**
Joe Malin32736f02011-01-19 16:14:20 -080016111 * Handles drag events sent by the system following a call to
16112 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
16113 *<p>
16114 * When the system calls this method, it passes a
16115 * {@link android.view.DragEvent} object. A call to
16116 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
16117 * in DragEvent. The method uses these to determine what is happening in the drag and drop
16118 * operation.
16119 * @param event The {@link android.view.DragEvent} sent by the system.
16120 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
16121 * in DragEvent, indicating the type of drag event represented by this object.
16122 * @return {@code true} if the method was successful, otherwise {@code false}.
16123 * <p>
16124 * The method should return {@code true} in response to an action type of
16125 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
16126 * operation.
16127 * </p>
16128 * <p>
16129 * The method should also return {@code true} in response to an action type of
16130 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
16131 * {@code false} if it didn't.
16132 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070016133 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070016134 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070016135 return false;
16136 }
16137
16138 /**
Joe Malin32736f02011-01-19 16:14:20 -080016139 * Detects if this View is enabled and has a drag event listener.
16140 * If both are true, then it calls the drag event listener with the
16141 * {@link android.view.DragEvent} it received. If the drag event listener returns
16142 * {@code true}, then dispatchDragEvent() returns {@code true}.
16143 * <p>
16144 * For all other cases, the method calls the
16145 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
16146 * method and returns its result.
16147 * </p>
16148 * <p>
16149 * This ensures that a drag event is always consumed, even if the View does not have a drag
16150 * event listener. However, if the View has a listener and the listener returns true, then
16151 * onDragEvent() is not called.
16152 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070016153 */
16154 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080016155 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070016156 ListenerInfo li = mListenerInfo;
16157 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
16158 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070016159 return true;
16160 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016161 return onDragEvent(event);
16162 }
16163
Christopher Tate3d4bf172011-03-28 16:16:46 -070016164 boolean canAcceptDrag() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016165 return (mPrivateFlags2 & PFLAG2_DRAG_CAN_ACCEPT) != 0;
Christopher Tate3d4bf172011-03-28 16:16:46 -070016166 }
16167
Christopher Tatea53146c2010-09-07 11:57:52 -070016168 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070016169 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
16170 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070016171 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070016172 */
16173 public void onCloseSystemDialogs(String reason) {
16174 }
Joe Malin32736f02011-01-19 16:14:20 -080016175
Dianne Hackbornffa42482009-09-23 22:20:11 -070016176 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016177 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070016178 * update a Region being computed for
16179 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016180 * that any non-transparent parts of the Drawable are removed from the
16181 * given transparent region.
16182 *
16183 * @param dr The Drawable whose transparency is to be applied to the region.
16184 * @param region A Region holding the current transparency information,
16185 * where any parts of the region that are set are considered to be
16186 * transparent. On return, this region will be modified to have the
16187 * transparency information reduced by the corresponding parts of the
16188 * Drawable that are not transparent.
16189 * {@hide}
16190 */
16191 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
16192 if (DBG) {
16193 Log.i("View", "Getting transparent region for: " + this);
16194 }
16195 final Region r = dr.getTransparentRegion();
16196 final Rect db = dr.getBounds();
16197 final AttachInfo attachInfo = mAttachInfo;
16198 if (r != null && attachInfo != null) {
16199 final int w = getRight()-getLeft();
16200 final int h = getBottom()-getTop();
16201 if (db.left > 0) {
16202 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
16203 r.op(0, 0, db.left, h, Region.Op.UNION);
16204 }
16205 if (db.right < w) {
16206 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
16207 r.op(db.right, 0, w, h, Region.Op.UNION);
16208 }
16209 if (db.top > 0) {
16210 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
16211 r.op(0, 0, w, db.top, Region.Op.UNION);
16212 }
16213 if (db.bottom < h) {
16214 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
16215 r.op(0, db.bottom, w, h, Region.Op.UNION);
16216 }
16217 final int[] location = attachInfo.mTransparentLocation;
16218 getLocationInWindow(location);
16219 r.translate(location[0], location[1]);
16220 region.op(r, Region.Op.INTERSECT);
16221 } else {
16222 region.op(db, Region.Op.DIFFERENCE);
16223 }
16224 }
16225
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016226 private void checkForLongClick(int delayOffset) {
16227 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
16228 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016229
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016230 if (mPendingCheckForLongPress == null) {
16231 mPendingCheckForLongPress = new CheckForLongPress();
16232 }
16233 mPendingCheckForLongPress.rememberWindowAttachCount();
16234 postDelayed(mPendingCheckForLongPress,
16235 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016236 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016237 }
16238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016239 /**
16240 * Inflate a view from an XML resource. This convenience method wraps the {@link
16241 * LayoutInflater} class, which provides a full range of options for view inflation.
16242 *
16243 * @param context The Context object for your activity or application.
16244 * @param resource The resource ID to inflate
16245 * @param root A view group that will be the parent. Used to properly inflate the
16246 * layout_* parameters.
16247 * @see LayoutInflater
16248 */
16249 public static View inflate(Context context, int resource, ViewGroup root) {
16250 LayoutInflater factory = LayoutInflater.from(context);
16251 return factory.inflate(resource, root);
16252 }
Romain Guy33e72ae2010-07-17 12:40:29 -070016253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016254 /**
Adam Powell637d3372010-08-25 14:37:03 -070016255 * Scroll the view with standard behavior for scrolling beyond the normal
16256 * content boundaries. Views that call this method should override
16257 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
16258 * results of an over-scroll operation.
16259 *
16260 * Views can use this method to handle any touch or fling-based scrolling.
16261 *
16262 * @param deltaX Change in X in pixels
16263 * @param deltaY Change in Y in pixels
16264 * @param scrollX Current X scroll value in pixels before applying deltaX
16265 * @param scrollY Current Y scroll value in pixels before applying deltaY
16266 * @param scrollRangeX Maximum content scroll range along the X axis
16267 * @param scrollRangeY Maximum content scroll range along the Y axis
16268 * @param maxOverScrollX Number of pixels to overscroll by in either direction
16269 * along the X axis.
16270 * @param maxOverScrollY Number of pixels to overscroll by in either direction
16271 * along the Y axis.
16272 * @param isTouchEvent true if this scroll operation is the result of a touch event.
16273 * @return true if scrolling was clamped to an over-scroll boundary along either
16274 * axis, false otherwise.
16275 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070016276 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070016277 protected boolean overScrollBy(int deltaX, int deltaY,
16278 int scrollX, int scrollY,
16279 int scrollRangeX, int scrollRangeY,
16280 int maxOverScrollX, int maxOverScrollY,
16281 boolean isTouchEvent) {
16282 final int overScrollMode = mOverScrollMode;
16283 final boolean canScrollHorizontal =
16284 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
16285 final boolean canScrollVertical =
16286 computeVerticalScrollRange() > computeVerticalScrollExtent();
16287 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
16288 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
16289 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
16290 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
16291
16292 int newScrollX = scrollX + deltaX;
16293 if (!overScrollHorizontal) {
16294 maxOverScrollX = 0;
16295 }
16296
16297 int newScrollY = scrollY + deltaY;
16298 if (!overScrollVertical) {
16299 maxOverScrollY = 0;
16300 }
16301
16302 // Clamp values if at the limits and record
16303 final int left = -maxOverScrollX;
16304 final int right = maxOverScrollX + scrollRangeX;
16305 final int top = -maxOverScrollY;
16306 final int bottom = maxOverScrollY + scrollRangeY;
16307
16308 boolean clampedX = false;
16309 if (newScrollX > right) {
16310 newScrollX = right;
16311 clampedX = true;
16312 } else if (newScrollX < left) {
16313 newScrollX = left;
16314 clampedX = true;
16315 }
16316
16317 boolean clampedY = false;
16318 if (newScrollY > bottom) {
16319 newScrollY = bottom;
16320 clampedY = true;
16321 } else if (newScrollY < top) {
16322 newScrollY = top;
16323 clampedY = true;
16324 }
16325
16326 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
16327
16328 return clampedX || clampedY;
16329 }
16330
16331 /**
16332 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
16333 * respond to the results of an over-scroll operation.
16334 *
16335 * @param scrollX New X scroll value in pixels
16336 * @param scrollY New Y scroll value in pixels
16337 * @param clampedX True if scrollX was clamped to an over-scroll boundary
16338 * @param clampedY True if scrollY was clamped to an over-scroll boundary
16339 */
16340 protected void onOverScrolled(int scrollX, int scrollY,
16341 boolean clampedX, boolean clampedY) {
16342 // Intentionally empty.
16343 }
16344
16345 /**
16346 * Returns the over-scroll mode for this view. The result will be
16347 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16348 * (allow over-scrolling only if the view content is larger than the container),
16349 * or {@link #OVER_SCROLL_NEVER}.
16350 *
16351 * @return This view's over-scroll mode.
16352 */
16353 public int getOverScrollMode() {
16354 return mOverScrollMode;
16355 }
16356
16357 /**
16358 * Set the over-scroll mode for this view. Valid over-scroll modes are
16359 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16360 * (allow over-scrolling only if the view content is larger than the container),
16361 * or {@link #OVER_SCROLL_NEVER}.
16362 *
16363 * Setting the over-scroll mode of a view will have an effect only if the
16364 * view is capable of scrolling.
16365 *
16366 * @param overScrollMode The new over-scroll mode for this view.
16367 */
16368 public void setOverScrollMode(int overScrollMode) {
16369 if (overScrollMode != OVER_SCROLL_ALWAYS &&
16370 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
16371 overScrollMode != OVER_SCROLL_NEVER) {
16372 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
16373 }
16374 mOverScrollMode = overScrollMode;
16375 }
16376
16377 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016378 * Gets a scale factor that determines the distance the view should scroll
16379 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
16380 * @return The vertical scroll scale factor.
16381 * @hide
16382 */
16383 protected float getVerticalScrollFactor() {
16384 if (mVerticalScrollFactor == 0) {
16385 TypedValue outValue = new TypedValue();
16386 if (!mContext.getTheme().resolveAttribute(
16387 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
16388 throw new IllegalStateException(
16389 "Expected theme to define listPreferredItemHeight.");
16390 }
16391 mVerticalScrollFactor = outValue.getDimension(
16392 mContext.getResources().getDisplayMetrics());
16393 }
16394 return mVerticalScrollFactor;
16395 }
16396
16397 /**
16398 * Gets a scale factor that determines the distance the view should scroll
16399 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
16400 * @return The horizontal scroll scale factor.
16401 * @hide
16402 */
16403 protected float getHorizontalScrollFactor() {
16404 // TODO: Should use something else.
16405 return getVerticalScrollFactor();
16406 }
16407
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016408 /**
16409 * Return the value specifying the text direction or policy that was set with
16410 * {@link #setTextDirection(int)}.
16411 *
16412 * @return the defined text direction. It can be one of:
16413 *
16414 * {@link #TEXT_DIRECTION_INHERIT},
16415 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16416 * {@link #TEXT_DIRECTION_ANY_RTL},
16417 * {@link #TEXT_DIRECTION_LTR},
16418 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016419 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016420 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016421 @ViewDebug.ExportedProperty(category = "text", mapping = {
16422 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
16423 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
16424 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
16425 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
16426 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
16427 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
16428 })
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016429 public int getTextDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016430 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_MASK) >> PFLAG2_TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016431 }
16432
16433 /**
16434 * Set the text direction.
16435 *
16436 * @param textDirection the direction to set. Should be one of:
16437 *
16438 * {@link #TEXT_DIRECTION_INHERIT},
16439 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16440 * {@link #TEXT_DIRECTION_ANY_RTL},
16441 * {@link #TEXT_DIRECTION_LTR},
16442 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016443 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016444 */
16445 public void setTextDirection(int textDirection) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016446 if (getTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016447 // Reset the current text direction and the resolved one
Dianne Hackborn4702a852012-08-17 15:18:29 -070016448 mPrivateFlags2 &= ~PFLAG2_TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016449 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016450 // Set the new text direction
Dianne Hackborn4702a852012-08-17 15:18:29 -070016451 mPrivateFlags2 |= ((textDirection << PFLAG2_TEXT_DIRECTION_MASK_SHIFT) & PFLAG2_TEXT_DIRECTION_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016452 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016453 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016454 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016455 }
16456 }
16457
16458 /**
16459 * Return the resolved text direction.
16460 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016461 * This needs resolution if the value is TEXT_DIRECTION_INHERIT. The resolution matches
16462 * {@link #getTextDirection()}if it is not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds
16463 * up the parent chain of the view. if there is no parent, then it will return the default
16464 * {@link #TEXT_DIRECTION_FIRST_STRONG}.
16465 *
16466 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016467 *
Doug Feltcb3791202011-07-07 11:57:48 -070016468 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16469 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016470 * {@link #TEXT_DIRECTION_LTR},
16471 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016472 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016473 */
16474 public int getResolvedTextDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070016475 // The text direction will be resolved only if needed
Dianne Hackborn4702a852012-08-17 15:18:29 -070016476 if ((mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_RESOLVED) != PFLAG2_TEXT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016477 resolveTextDirection();
16478 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070016479 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_RESOLVED_MASK) >> PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016480 }
16481
16482 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016483 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
16484 * resolution is done.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016485 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016486 public void resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016487 // Reset any previous text direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016488 mPrivateFlags2 &= ~(PFLAG2_TEXT_DIRECTION_RESOLVED | PFLAG2_TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016489
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016490 if (hasRtlSupport()) {
16491 // Set resolved text direction flag depending on text direction flag
16492 final int textDirection = getTextDirection();
16493 switch(textDirection) {
16494 case TEXT_DIRECTION_INHERIT:
16495 if (canResolveTextDirection()) {
16496 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016497
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016498 // Set current resolved direction to the same value as the parent's one
16499 final int parentResolvedDirection = viewGroup.getResolvedTextDirection();
16500 switch (parentResolvedDirection) {
16501 case TEXT_DIRECTION_FIRST_STRONG:
16502 case TEXT_DIRECTION_ANY_RTL:
16503 case TEXT_DIRECTION_LTR:
16504 case TEXT_DIRECTION_RTL:
16505 case TEXT_DIRECTION_LOCALE:
16506 mPrivateFlags2 |=
Dianne Hackborn4702a852012-08-17 15:18:29 -070016507 (parentResolvedDirection << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016508 break;
16509 default:
16510 // Default resolved direction is "first strong" heuristic
Dianne Hackborn4702a852012-08-17 15:18:29 -070016511 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016512 }
16513 } else {
16514 // We cannot do the resolution if there is no parent, so use the default one
Dianne Hackborn4702a852012-08-17 15:18:29 -070016515 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016516 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016517 break;
16518 case TEXT_DIRECTION_FIRST_STRONG:
16519 case TEXT_DIRECTION_ANY_RTL:
16520 case TEXT_DIRECTION_LTR:
16521 case TEXT_DIRECTION_RTL:
16522 case TEXT_DIRECTION_LOCALE:
16523 // Resolved direction is the same as text direction
Dianne Hackborn4702a852012-08-17 15:18:29 -070016524 mPrivateFlags2 |= (textDirection << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016525 break;
16526 default:
16527 // Default resolved direction is "first strong" heuristic
Dianne Hackborn4702a852012-08-17 15:18:29 -070016528 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016529 }
16530 } else {
16531 // Default resolved direction is "first strong" heuristic
Dianne Hackborn4702a852012-08-17 15:18:29 -070016532 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016533 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016534
16535 // Set to resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070016536 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016537 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016538 }
16539
16540 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016541 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016542 * resolution should override this method.
16543 *
16544 * The default implementation does nothing.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016545 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016546 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016547 }
16548
16549 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016550 * Check if text direction resolution can be done.
16551 *
16552 * @return true if text direction resolution can be done otherwise return false.
16553 */
16554 public boolean canResolveTextDirection() {
16555 switch (getTextDirection()) {
16556 case TEXT_DIRECTION_INHERIT:
16557 return (mParent != null) && (mParent instanceof ViewGroup);
16558 default:
16559 return true;
16560 }
16561 }
16562
16563 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016564 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016565 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016566 * reset is done.
16567 */
16568 public void resetResolvedTextDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016569 mPrivateFlags2 &= ~(PFLAG2_TEXT_DIRECTION_RESOLVED | PFLAG2_TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016570 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016571 }
16572
16573 /**
16574 * Called when text direction is reset. Subclasses that care about text direction reset should
16575 * override this method and do a reset of the text direction of their children. The default
16576 * implementation does nothing.
16577 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016578 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016579 }
16580
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016581 /**
16582 * Return the value specifying the text alignment or policy that was set with
16583 * {@link #setTextAlignment(int)}.
16584 *
16585 * @return the defined text alignment. It can be one of:
16586 *
16587 * {@link #TEXT_ALIGNMENT_INHERIT},
16588 * {@link #TEXT_ALIGNMENT_GRAVITY},
16589 * {@link #TEXT_ALIGNMENT_CENTER},
16590 * {@link #TEXT_ALIGNMENT_TEXT_START},
16591 * {@link #TEXT_ALIGNMENT_TEXT_END},
16592 * {@link #TEXT_ALIGNMENT_VIEW_START},
16593 * {@link #TEXT_ALIGNMENT_VIEW_END}
16594 */
16595 @ViewDebug.ExportedProperty(category = "text", mapping = {
16596 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16597 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16598 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16599 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16600 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16601 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16602 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16603 })
16604 public int getTextAlignment() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016605 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_MASK) >> PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016606 }
16607
16608 /**
16609 * Set the text alignment.
16610 *
16611 * @param textAlignment The text alignment to set. Should be one of
16612 *
16613 * {@link #TEXT_ALIGNMENT_INHERIT},
16614 * {@link #TEXT_ALIGNMENT_GRAVITY},
16615 * {@link #TEXT_ALIGNMENT_CENTER},
16616 * {@link #TEXT_ALIGNMENT_TEXT_START},
16617 * {@link #TEXT_ALIGNMENT_TEXT_END},
16618 * {@link #TEXT_ALIGNMENT_VIEW_START},
16619 * {@link #TEXT_ALIGNMENT_VIEW_END}
16620 *
16621 * @attr ref android.R.styleable#View_textAlignment
16622 */
16623 public void setTextAlignment(int textAlignment) {
16624 if (textAlignment != getTextAlignment()) {
16625 // Reset the current and resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016626 mPrivateFlags2 &= ~PFLAG2_TEXT_ALIGNMENT_MASK;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016627 resetResolvedTextAlignment();
16628 // Set the new text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016629 mPrivateFlags2 |= ((textAlignment << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT) & PFLAG2_TEXT_ALIGNMENT_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016630 // Refresh
16631 requestLayout();
16632 invalidate(true);
16633 }
16634 }
16635
16636 /**
16637 * Return the resolved text alignment.
16638 *
16639 * The resolved text alignment. This needs resolution if the value is
16640 * TEXT_ALIGNMENT_INHERIT. The resolution matches {@link #setTextAlignment(int)} if it is
16641 * not TEXT_ALIGNMENT_INHERIT, otherwise resolution proceeds up the parent chain of the view.
16642 *
16643 * @return the resolved text alignment. Returns one of:
16644 *
16645 * {@link #TEXT_ALIGNMENT_GRAVITY},
16646 * {@link #TEXT_ALIGNMENT_CENTER},
16647 * {@link #TEXT_ALIGNMENT_TEXT_START},
16648 * {@link #TEXT_ALIGNMENT_TEXT_END},
16649 * {@link #TEXT_ALIGNMENT_VIEW_START},
16650 * {@link #TEXT_ALIGNMENT_VIEW_END}
16651 */
16652 @ViewDebug.ExportedProperty(category = "text", mapping = {
16653 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16654 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16655 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16656 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16657 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16658 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16659 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16660 })
16661 public int getResolvedTextAlignment() {
16662 // If text alignment is not resolved, then resolve it
Dianne Hackborn4702a852012-08-17 15:18:29 -070016663 if ((mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_RESOLVED) != PFLAG2_TEXT_ALIGNMENT_RESOLVED) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016664 resolveTextAlignment();
16665 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070016666 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK) >> PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016667 }
16668
16669 /**
16670 * Resolve the text alignment. Will call {@link View#onResolvedTextAlignmentChanged} when
16671 * resolution is done.
16672 */
16673 public void resolveTextAlignment() {
16674 // Reset any previous text alignment resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016675 mPrivateFlags2 &= ~(PFLAG2_TEXT_ALIGNMENT_RESOLVED | PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016676
16677 if (hasRtlSupport()) {
16678 // Set resolved text alignment flag depending on text alignment flag
16679 final int textAlignment = getTextAlignment();
16680 switch (textAlignment) {
16681 case TEXT_ALIGNMENT_INHERIT:
16682 // Check if we can resolve the text alignment
16683 if (canResolveLayoutDirection() && mParent instanceof View) {
16684 View view = (View) mParent;
16685
16686 final int parentResolvedTextAlignment = view.getResolvedTextAlignment();
16687 switch (parentResolvedTextAlignment) {
16688 case TEXT_ALIGNMENT_GRAVITY:
16689 case TEXT_ALIGNMENT_TEXT_START:
16690 case TEXT_ALIGNMENT_TEXT_END:
16691 case TEXT_ALIGNMENT_CENTER:
16692 case TEXT_ALIGNMENT_VIEW_START:
16693 case TEXT_ALIGNMENT_VIEW_END:
16694 // Resolved text alignment is the same as the parent resolved
16695 // text alignment
16696 mPrivateFlags2 |=
Dianne Hackborn4702a852012-08-17 15:18:29 -070016697 (parentResolvedTextAlignment << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016698 break;
16699 default:
16700 // Use default resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016701 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016702 }
16703 }
16704 else {
16705 // We cannot do the resolution if there is no parent so use the default
Dianne Hackborn4702a852012-08-17 15:18:29 -070016706 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016707 }
16708 break;
16709 case TEXT_ALIGNMENT_GRAVITY:
16710 case TEXT_ALIGNMENT_TEXT_START:
16711 case TEXT_ALIGNMENT_TEXT_END:
16712 case TEXT_ALIGNMENT_CENTER:
16713 case TEXT_ALIGNMENT_VIEW_START:
16714 case TEXT_ALIGNMENT_VIEW_END:
16715 // Resolved text alignment is the same as text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016716 mPrivateFlags2 |= (textAlignment << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016717 break;
16718 default:
16719 // Use default resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016720 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016721 }
16722 } else {
16723 // Use default resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016724 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016725 }
16726
16727 // Set the resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070016728 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016729 onResolvedTextAlignmentChanged();
16730 }
16731
16732 /**
16733 * Check if text alignment resolution can be done.
16734 *
16735 * @return true if text alignment resolution can be done otherwise return false.
16736 */
16737 public boolean canResolveTextAlignment() {
16738 switch (getTextAlignment()) {
16739 case TEXT_DIRECTION_INHERIT:
16740 return (mParent != null);
16741 default:
16742 return true;
16743 }
16744 }
16745
16746 /**
16747 * Called when text alignment has been resolved. Subclasses that care about text alignment
16748 * resolution should override this method.
16749 *
16750 * The default implementation does nothing.
16751 */
16752 public void onResolvedTextAlignmentChanged() {
16753 }
16754
16755 /**
16756 * Reset resolved text alignment. Text alignment can be resolved with a call to
16757 * getResolvedTextAlignment(). Will call {@link View#onResolvedTextAlignmentReset} when
16758 * reset is done.
16759 */
16760 public void resetResolvedTextAlignment() {
16761 // Reset any previous text alignment resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016762 mPrivateFlags2 &= ~(PFLAG2_TEXT_ALIGNMENT_RESOLVED | PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016763 onResolvedTextAlignmentReset();
16764 }
16765
16766 /**
16767 * Called when text alignment is reset. Subclasses that care about text alignment reset should
16768 * override this method and do a reset of the text alignment of their children. The default
16769 * implementation does nothing.
16770 */
16771 public void onResolvedTextAlignmentReset() {
16772 }
16773
Adam Powella9108a22012-07-18 11:18:09 -070016774 /**
16775 * Generate a value suitable for use in {@link #setId(int)}.
16776 * This value will not collide with ID values generated at build time by aapt for R.id.
16777 *
16778 * @return a generated ID value
16779 */
16780 public static int generateViewId() {
16781 for (;;) {
16782 final int result = sNextGeneratedId.get();
16783 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
16784 int newValue = result + 1;
16785 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
16786 if (sNextGeneratedId.compareAndSet(result, newValue)) {
16787 return result;
16788 }
16789 }
16790 }
16791
Chet Haaseb39f0512011-05-24 14:36:40 -070016792 //
16793 // Properties
16794 //
16795 /**
16796 * A Property wrapper around the <code>alpha</code> functionality handled by the
16797 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
16798 */
Chet Haased47f1532011-12-16 11:18:52 -080016799 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016800 @Override
16801 public void setValue(View object, float value) {
16802 object.setAlpha(value);
16803 }
16804
16805 @Override
16806 public Float get(View object) {
16807 return object.getAlpha();
16808 }
16809 };
16810
16811 /**
16812 * A Property wrapper around the <code>translationX</code> functionality handled by the
16813 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
16814 */
Chet Haased47f1532011-12-16 11:18:52 -080016815 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016816 @Override
16817 public void setValue(View object, float value) {
16818 object.setTranslationX(value);
16819 }
16820
16821 @Override
16822 public Float get(View object) {
16823 return object.getTranslationX();
16824 }
16825 };
16826
16827 /**
16828 * A Property wrapper around the <code>translationY</code> functionality handled by the
16829 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
16830 */
Chet Haased47f1532011-12-16 11:18:52 -080016831 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016832 @Override
16833 public void setValue(View object, float value) {
16834 object.setTranslationY(value);
16835 }
16836
16837 @Override
16838 public Float get(View object) {
16839 return object.getTranslationY();
16840 }
16841 };
16842
16843 /**
16844 * A Property wrapper around the <code>x</code> functionality handled by the
16845 * {@link View#setX(float)} and {@link View#getX()} methods.
16846 */
Chet Haased47f1532011-12-16 11:18:52 -080016847 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016848 @Override
16849 public void setValue(View object, float value) {
16850 object.setX(value);
16851 }
16852
16853 @Override
16854 public Float get(View object) {
16855 return object.getX();
16856 }
16857 };
16858
16859 /**
16860 * A Property wrapper around the <code>y</code> functionality handled by the
16861 * {@link View#setY(float)} and {@link View#getY()} methods.
16862 */
Chet Haased47f1532011-12-16 11:18:52 -080016863 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016864 @Override
16865 public void setValue(View object, float value) {
16866 object.setY(value);
16867 }
16868
16869 @Override
16870 public Float get(View object) {
16871 return object.getY();
16872 }
16873 };
16874
16875 /**
16876 * A Property wrapper around the <code>rotation</code> functionality handled by the
16877 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
16878 */
Chet Haased47f1532011-12-16 11:18:52 -080016879 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016880 @Override
16881 public void setValue(View object, float value) {
16882 object.setRotation(value);
16883 }
16884
16885 @Override
16886 public Float get(View object) {
16887 return object.getRotation();
16888 }
16889 };
16890
16891 /**
16892 * A Property wrapper around the <code>rotationX</code> functionality handled by the
16893 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
16894 */
Chet Haased47f1532011-12-16 11:18:52 -080016895 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016896 @Override
16897 public void setValue(View object, float value) {
16898 object.setRotationX(value);
16899 }
16900
16901 @Override
16902 public Float get(View object) {
16903 return object.getRotationX();
16904 }
16905 };
16906
16907 /**
16908 * A Property wrapper around the <code>rotationY</code> functionality handled by the
16909 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
16910 */
Chet Haased47f1532011-12-16 11:18:52 -080016911 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016912 @Override
16913 public void setValue(View object, float value) {
16914 object.setRotationY(value);
16915 }
16916
16917 @Override
16918 public Float get(View object) {
16919 return object.getRotationY();
16920 }
16921 };
16922
16923 /**
16924 * A Property wrapper around the <code>scaleX</code> functionality handled by the
16925 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
16926 */
Chet Haased47f1532011-12-16 11:18:52 -080016927 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016928 @Override
16929 public void setValue(View object, float value) {
16930 object.setScaleX(value);
16931 }
16932
16933 @Override
16934 public Float get(View object) {
16935 return object.getScaleX();
16936 }
16937 };
16938
16939 /**
16940 * A Property wrapper around the <code>scaleY</code> functionality handled by the
16941 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
16942 */
Chet Haased47f1532011-12-16 11:18:52 -080016943 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016944 @Override
16945 public void setValue(View object, float value) {
16946 object.setScaleY(value);
16947 }
16948
16949 @Override
16950 public Float get(View object) {
16951 return object.getScaleY();
16952 }
16953 };
16954
Jeff Brown33bbfd22011-02-24 20:55:35 -080016955 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016956 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
16957 * Each MeasureSpec represents a requirement for either the width or the height.
16958 * A MeasureSpec is comprised of a size and a mode. There are three possible
16959 * modes:
16960 * <dl>
16961 * <dt>UNSPECIFIED</dt>
16962 * <dd>
16963 * The parent has not imposed any constraint on the child. It can be whatever size
16964 * it wants.
16965 * </dd>
16966 *
16967 * <dt>EXACTLY</dt>
16968 * <dd>
16969 * The parent has determined an exact size for the child. The child is going to be
16970 * given those bounds regardless of how big it wants to be.
16971 * </dd>
16972 *
16973 * <dt>AT_MOST</dt>
16974 * <dd>
16975 * The child can be as large as it wants up to the specified size.
16976 * </dd>
16977 * </dl>
16978 *
16979 * MeasureSpecs are implemented as ints to reduce object allocation. This class
16980 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
16981 */
16982 public static class MeasureSpec {
16983 private static final int MODE_SHIFT = 30;
16984 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
16985
16986 /**
16987 * Measure specification mode: The parent has not imposed any constraint
16988 * on the child. It can be whatever size it wants.
16989 */
16990 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
16991
16992 /**
16993 * Measure specification mode: The parent has determined an exact size
16994 * for the child. The child is going to be given those bounds regardless
16995 * of how big it wants to be.
16996 */
16997 public static final int EXACTLY = 1 << MODE_SHIFT;
16998
16999 /**
17000 * Measure specification mode: The child can be as large as it wants up
17001 * to the specified size.
17002 */
17003 public static final int AT_MOST = 2 << MODE_SHIFT;
17004
17005 /**
17006 * Creates a measure specification based on the supplied size and mode.
17007 *
17008 * The mode must always be one of the following:
17009 * <ul>
17010 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
17011 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
17012 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
17013 * </ul>
17014 *
17015 * @param size the size of the measure specification
17016 * @param mode the mode of the measure specification
17017 * @return the measure specification based on size and mode
17018 */
17019 public static int makeMeasureSpec(int size, int mode) {
17020 return size + mode;
17021 }
17022
17023 /**
17024 * Extracts the mode from the supplied measure specification.
17025 *
17026 * @param measureSpec the measure specification to extract the mode from
17027 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
17028 * {@link android.view.View.MeasureSpec#AT_MOST} or
17029 * {@link android.view.View.MeasureSpec#EXACTLY}
17030 */
17031 public static int getMode(int measureSpec) {
17032 return (measureSpec & MODE_MASK);
17033 }
17034
17035 /**
17036 * Extracts the size from the supplied measure specification.
17037 *
17038 * @param measureSpec the measure specification to extract the size from
17039 * @return the size in pixels defined in the supplied measure specification
17040 */
17041 public static int getSize(int measureSpec) {
17042 return (measureSpec & ~MODE_MASK);
17043 }
17044
17045 /**
17046 * Returns a String representation of the specified measure
17047 * specification.
17048 *
17049 * @param measureSpec the measure specification to convert to a String
17050 * @return a String with the following format: "MeasureSpec: MODE SIZE"
17051 */
17052 public static String toString(int measureSpec) {
17053 int mode = getMode(measureSpec);
17054 int size = getSize(measureSpec);
17055
17056 StringBuilder sb = new StringBuilder("MeasureSpec: ");
17057
17058 if (mode == UNSPECIFIED)
17059 sb.append("UNSPECIFIED ");
17060 else if (mode == EXACTLY)
17061 sb.append("EXACTLY ");
17062 else if (mode == AT_MOST)
17063 sb.append("AT_MOST ");
17064 else
17065 sb.append(mode).append(" ");
17066
17067 sb.append(size);
17068 return sb.toString();
17069 }
17070 }
17071
17072 class CheckForLongPress implements Runnable {
17073
17074 private int mOriginalWindowAttachCount;
17075
17076 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070017077 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017078 && mOriginalWindowAttachCount == mWindowAttachCount) {
17079 if (performLongClick()) {
17080 mHasPerformedLongPress = true;
17081 }
17082 }
17083 }
17084
17085 public void rememberWindowAttachCount() {
17086 mOriginalWindowAttachCount = mWindowAttachCount;
17087 }
17088 }
Joe Malin32736f02011-01-19 16:14:20 -080017089
Adam Powelle14579b2009-12-16 18:39:52 -080017090 private final class CheckForTap implements Runnable {
17091 public void run() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070017092 mPrivateFlags &= ~PFLAG_PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080017093 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070017094 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080017095 }
17096 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017097
Adam Powella35d7682010-03-12 14:48:13 -080017098 private final class PerformClick implements Runnable {
17099 public void run() {
17100 performClick();
17101 }
17102 }
17103
Dianne Hackborn63042d62011-01-26 18:56:29 -080017104 /** @hide */
17105 public void hackTurnOffWindowResizeAnim(boolean off) {
17106 mAttachInfo.mTurnOffWindowResizeAnim = off;
17107 }
Joe Malin32736f02011-01-19 16:14:20 -080017108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017109 /**
Chet Haasea00f3862011-02-22 06:34:40 -080017110 * This method returns a ViewPropertyAnimator object, which can be used to animate
17111 * specific properties on this View.
17112 *
17113 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
17114 */
17115 public ViewPropertyAnimator animate() {
17116 if (mAnimator == null) {
17117 mAnimator = new ViewPropertyAnimator(this);
17118 }
17119 return mAnimator;
17120 }
17121
17122 /**
Jean Chalard405bc512012-05-29 19:12:34 +090017123 * Interface definition for a callback to be invoked when a hardware key event is
17124 * dispatched to this view. The callback will be invoked before the key event is
17125 * given to the view. This is only useful for hardware keyboards; a software input
17126 * method has no obligation to trigger this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017127 */
17128 public interface OnKeyListener {
17129 /**
Jean Chalard405bc512012-05-29 19:12:34 +090017130 * Called when a hardware key is dispatched to a view. This allows listeners to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017131 * get a chance to respond before the target view.
Jean Chalard405bc512012-05-29 19:12:34 +090017132 * <p>Key presses in software keyboards will generally NOT trigger this method,
17133 * although some may elect to do so in some situations. Do not assume a
17134 * software input method has to be key-based; even if it is, it may use key presses
17135 * in a different way than you expect, so there is no way to reliably catch soft
17136 * input key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017137 *
17138 * @param v The view the key has been dispatched to.
17139 * @param keyCode The code for the physical key that was pressed
17140 * @param event The KeyEvent object containing full information about
17141 * the event.
17142 * @return True if the listener has consumed the event, false otherwise.
17143 */
17144 boolean onKey(View v, int keyCode, KeyEvent event);
17145 }
17146
17147 /**
17148 * Interface definition for a callback to be invoked when a touch event is
17149 * dispatched to this view. The callback will be invoked before the touch
17150 * event is given to the view.
17151 */
17152 public interface OnTouchListener {
17153 /**
17154 * Called when a touch event is dispatched to a view. This allows listeners to
17155 * get a chance to respond before the target view.
17156 *
17157 * @param v The view the touch event has been dispatched to.
17158 * @param event The MotionEvent object containing full information about
17159 * the event.
17160 * @return True if the listener has consumed the event, false otherwise.
17161 */
17162 boolean onTouch(View v, MotionEvent event);
17163 }
17164
17165 /**
Jeff Brown10b62902011-06-20 16:40:37 -070017166 * Interface definition for a callback to be invoked when a hover event is
17167 * dispatched to this view. The callback will be invoked before the hover
17168 * event is given to the view.
17169 */
17170 public interface OnHoverListener {
17171 /**
17172 * Called when a hover event is dispatched to a view. This allows listeners to
17173 * get a chance to respond before the target view.
17174 *
17175 * @param v The view the hover event has been dispatched to.
17176 * @param event The MotionEvent object containing full information about
17177 * the event.
17178 * @return True if the listener has consumed the event, false otherwise.
17179 */
17180 boolean onHover(View v, MotionEvent event);
17181 }
17182
17183 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080017184 * Interface definition for a callback to be invoked when a generic motion event is
17185 * dispatched to this view. The callback will be invoked before the generic motion
17186 * event is given to the view.
17187 */
17188 public interface OnGenericMotionListener {
17189 /**
17190 * Called when a generic motion event is dispatched to a view. This allows listeners to
17191 * get a chance to respond before the target view.
17192 *
17193 * @param v The view the generic motion event has been dispatched to.
17194 * @param event The MotionEvent object containing full information about
17195 * the event.
17196 * @return True if the listener has consumed the event, false otherwise.
17197 */
17198 boolean onGenericMotion(View v, MotionEvent event);
17199 }
17200
17201 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017202 * Interface definition for a callback to be invoked when a view has been clicked and held.
17203 */
17204 public interface OnLongClickListener {
17205 /**
17206 * Called when a view has been clicked and held.
17207 *
17208 * @param v The view that was clicked and held.
17209 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080017210 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017211 */
17212 boolean onLongClick(View v);
17213 }
17214
17215 /**
Chris Tate32affef2010-10-18 15:29:21 -070017216 * Interface definition for a callback to be invoked when a drag is being dispatched
17217 * to this view. The callback will be invoked before the hosting view's own
17218 * onDrag(event) method. If the listener wants to fall back to the hosting view's
17219 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070017220 *
17221 * <div class="special reference">
17222 * <h3>Developer Guides</h3>
17223 * <p>For a guide to implementing drag and drop features, read the
17224 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
17225 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070017226 */
17227 public interface OnDragListener {
17228 /**
17229 * Called when a drag event is dispatched to a view. This allows listeners
17230 * to get a chance to override base View behavior.
17231 *
Joe Malin32736f02011-01-19 16:14:20 -080017232 * @param v The View that received the drag event.
17233 * @param event The {@link android.view.DragEvent} object for the drag event.
17234 * @return {@code true} if the drag event was handled successfully, or {@code false}
17235 * if the drag event was not handled. Note that {@code false} will trigger the View
17236 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070017237 */
17238 boolean onDrag(View v, DragEvent event);
17239 }
17240
17241 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017242 * Interface definition for a callback to be invoked when the focus state of
17243 * a view changed.
17244 */
17245 public interface OnFocusChangeListener {
17246 /**
17247 * Called when the focus state of a view has changed.
17248 *
17249 * @param v The view whose state has changed.
17250 * @param hasFocus The new focus state of v.
17251 */
17252 void onFocusChange(View v, boolean hasFocus);
17253 }
17254
17255 /**
17256 * Interface definition for a callback to be invoked when a view is clicked.
17257 */
17258 public interface OnClickListener {
17259 /**
17260 * Called when a view has been clicked.
17261 *
17262 * @param v The view that was clicked.
17263 */
17264 void onClick(View v);
17265 }
17266
17267 /**
17268 * Interface definition for a callback to be invoked when the context menu
17269 * for this view is being built.
17270 */
17271 public interface OnCreateContextMenuListener {
17272 /**
17273 * Called when the context menu for this view is being built. It is not
17274 * safe to hold onto the menu after this method returns.
17275 *
17276 * @param menu The context menu that is being built
17277 * @param v The view for which the context menu is being built
17278 * @param menuInfo Extra information about the item for which the
17279 * context menu should be shown. This information will vary
17280 * depending on the class of v.
17281 */
17282 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
17283 }
17284
Joe Onorato664644d2011-01-23 17:53:23 -080017285 /**
17286 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017287 * visibility. This reports <strong>global</strong> changes to the system UI
Dianne Hackborncf675782012-05-10 15:07:24 -070017288 * state, not what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080017289 *
Philip Milne6c8ea062012-04-03 17:38:43 -070017290 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080017291 */
17292 public interface OnSystemUiVisibilityChangeListener {
17293 /**
17294 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070017295 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080017296 *
Dianne Hackborncf675782012-05-10 15:07:24 -070017297 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
17298 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, and {@link #SYSTEM_UI_FLAG_FULLSCREEN}.
17299 * This tells you the <strong>global</strong> state of these UI visibility
17300 * flags, not what your app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080017301 */
17302 public void onSystemUiVisibilityChange(int visibility);
17303 }
17304
Adam Powell4afd62b2011-02-18 15:02:18 -080017305 /**
17306 * Interface definition for a callback to be invoked when this view is attached
17307 * or detached from its window.
17308 */
17309 public interface OnAttachStateChangeListener {
17310 /**
17311 * Called when the view is attached to a window.
17312 * @param v The view that was attached
17313 */
17314 public void onViewAttachedToWindow(View v);
17315 /**
17316 * Called when the view is detached from a window.
17317 * @param v The view that was detached
17318 */
17319 public void onViewDetachedFromWindow(View v);
17320 }
17321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017322 private final class UnsetPressedState implements Runnable {
17323 public void run() {
17324 setPressed(false);
17325 }
17326 }
17327
17328 /**
17329 * Base class for derived classes that want to save and restore their own
17330 * state in {@link android.view.View#onSaveInstanceState()}.
17331 */
17332 public static class BaseSavedState extends AbsSavedState {
17333 /**
17334 * Constructor used when reading from a parcel. Reads the state of the superclass.
17335 *
17336 * @param source
17337 */
17338 public BaseSavedState(Parcel source) {
17339 super(source);
17340 }
17341
17342 /**
17343 * Constructor called by derived classes when creating their SavedState objects
17344 *
17345 * @param superState The state of the superclass of this view
17346 */
17347 public BaseSavedState(Parcelable superState) {
17348 super(superState);
17349 }
17350
17351 public static final Parcelable.Creator<BaseSavedState> CREATOR =
17352 new Parcelable.Creator<BaseSavedState>() {
17353 public BaseSavedState createFromParcel(Parcel in) {
17354 return new BaseSavedState(in);
17355 }
17356
17357 public BaseSavedState[] newArray(int size) {
17358 return new BaseSavedState[size];
17359 }
17360 };
17361 }
17362
17363 /**
17364 * A set of information given to a view when it is attached to its parent
17365 * window.
17366 */
17367 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017368 interface Callbacks {
17369 void playSoundEffect(int effectId);
17370 boolean performHapticFeedback(int effectId, boolean always);
17371 }
17372
17373 /**
17374 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
17375 * to a Handler. This class contains the target (View) to invalidate and
17376 * the coordinates of the dirty rectangle.
17377 *
17378 * For performance purposes, this class also implements a pool of up to
17379 * POOL_LIMIT objects that get reused. This reduces memory allocations
17380 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017381 */
Romain Guyd928d682009-03-31 17:52:16 -070017382 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017383 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070017384 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
17385 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070017386 public InvalidateInfo newInstance() {
17387 return new InvalidateInfo();
17388 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017389
Romain Guyd928d682009-03-31 17:52:16 -070017390 public void onAcquired(InvalidateInfo element) {
17391 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017392
Romain Guyd928d682009-03-31 17:52:16 -070017393 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070017394 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070017395 }
17396 }, POOL_LIMIT)
17397 );
17398
17399 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017400 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017401
17402 View target;
17403
17404 int left;
17405 int top;
17406 int right;
17407 int bottom;
17408
Romain Guyd928d682009-03-31 17:52:16 -070017409 public void setNextPoolable(InvalidateInfo element) {
17410 mNext = element;
17411 }
17412
17413 public InvalidateInfo getNextPoolable() {
17414 return mNext;
17415 }
17416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017417 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070017418 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017419 }
17420
17421 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070017422 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017423 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017424
17425 public boolean isPooled() {
17426 return mIsPooled;
17427 }
17428
17429 public void setPooled(boolean isPooled) {
17430 mIsPooled = isPooled;
17431 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017432 }
17433
17434 final IWindowSession mSession;
17435
17436 final IWindow mWindow;
17437
17438 final IBinder mWindowToken;
17439
Jeff Brown98365d72012-08-19 20:30:52 -070017440 final Display mDisplay;
17441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017442 final Callbacks mRootCallbacks;
17443
Romain Guy59a12ca2011-06-09 17:48:21 -070017444 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080017445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017446 /**
17447 * The top view of the hierarchy.
17448 */
17449 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070017450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017451 IBinder mPanelParentWindowToken;
17452 Surface mSurface;
17453
Romain Guyb051e892010-09-28 19:09:36 -070017454 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080017455 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070017456 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080017457
Romain Guy7e4e5612012-03-05 14:37:29 -080017458 boolean mScreenOn;
17459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017460 /**
Romain Guy8506ab42009-06-11 17:35:47 -070017461 * Scale factor used by the compatibility mode
17462 */
17463 float mApplicationScale;
17464
17465 /**
17466 * Indicates whether the application is in compatibility mode
17467 */
17468 boolean mScalingRequired;
17469
17470 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017471 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080017472 */
17473 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080017474
Dianne Hackborn63042d62011-01-26 18:56:29 -080017475 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017476 * Left position of this view's window
17477 */
17478 int mWindowLeft;
17479
17480 /**
17481 * Top position of this view's window
17482 */
17483 int mWindowTop;
17484
17485 /**
Adam Powell26153a32010-11-08 15:22:27 -080017486 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070017487 */
Adam Powell26153a32010-11-08 15:22:27 -080017488 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070017489
17490 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017491 * For windows that are full-screen but using insets to layout inside
17492 * of the screen decorations, these are the current insets for the
17493 * content of the window.
17494 */
17495 final Rect mContentInsets = new Rect();
17496
17497 /**
17498 * For windows that are full-screen but using insets to layout inside
17499 * of the screen decorations, these are the current insets for the
17500 * actual visible parts of the window.
17501 */
17502 final Rect mVisibleInsets = new Rect();
17503
17504 /**
17505 * The internal insets given by this window. This value is
17506 * supplied by the client (through
17507 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
17508 * be given to the window manager when changed to be used in laying
17509 * out windows behind it.
17510 */
17511 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
17512 = new ViewTreeObserver.InternalInsetsInfo();
17513
17514 /**
17515 * All views in the window's hierarchy that serve as scroll containers,
17516 * used to determine if the window can be resized or must be panned
17517 * to adjust for a soft input area.
17518 */
17519 final ArrayList<View> mScrollContainers = new ArrayList<View>();
17520
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070017521 final KeyEvent.DispatcherState mKeyDispatchState
17522 = new KeyEvent.DispatcherState();
17523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017524 /**
17525 * Indicates whether the view's window currently has the focus.
17526 */
17527 boolean mHasWindowFocus;
17528
17529 /**
17530 * The current visibility of the window.
17531 */
17532 int mWindowVisibility;
17533
17534 /**
17535 * Indicates the time at which drawing started to occur.
17536 */
17537 long mDrawingTime;
17538
17539 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070017540 * Indicates whether or not ignoring the DIRTY_MASK flags.
17541 */
17542 boolean mIgnoreDirtyState;
17543
17544 /**
Romain Guy02ccac62011-06-24 13:20:23 -070017545 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
17546 * to avoid clearing that flag prematurely.
17547 */
17548 boolean mSetIgnoreDirtyState = false;
17549
17550 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017551 * Indicates whether the view's window is currently in touch mode.
17552 */
17553 boolean mInTouchMode;
17554
17555 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017556 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017557 * the next time it performs a traversal
17558 */
17559 boolean mRecomputeGlobalAttributes;
17560
17561 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017562 * Always report new attributes at next traversal.
17563 */
17564 boolean mForceReportNewAttributes;
17565
17566 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017567 * Set during a traveral if any views want to keep the screen on.
17568 */
17569 boolean mKeepScreenOn;
17570
17571 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017572 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
17573 */
17574 int mSystemUiVisibility;
17575
17576 /**
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070017577 * Hack to force certain system UI visibility flags to be cleared.
17578 */
17579 int mDisabledSystemUiVisibility;
17580
17581 /**
Dianne Hackborncf675782012-05-10 15:07:24 -070017582 * Last global system UI visibility reported by the window manager.
17583 */
17584 int mGlobalSystemUiVisibility;
17585
17586 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017587 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
17588 * attached.
17589 */
17590 boolean mHasSystemUiListeners;
17591
17592 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017593 * Set if the visibility of any views has changed.
17594 */
17595 boolean mViewVisibilityChanged;
17596
17597 /**
17598 * Set to true if a view has been scrolled.
17599 */
17600 boolean mViewScrollChanged;
17601
17602 /**
17603 * Global to the view hierarchy used as a temporary for dealing with
17604 * x/y points in the transparent region computations.
17605 */
17606 final int[] mTransparentLocation = new int[2];
17607
17608 /**
17609 * Global to the view hierarchy used as a temporary for dealing with
17610 * x/y points in the ViewGroup.invalidateChild implementation.
17611 */
17612 final int[] mInvalidateChildLocation = new int[2];
17613
Chet Haasec3aa3612010-06-17 08:50:37 -070017614
17615 /**
17616 * Global to the view hierarchy used as a temporary for dealing with
17617 * x/y location when view is transformed.
17618 */
17619 final float[] mTmpTransformLocation = new float[2];
17620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017621 /**
17622 * The view tree observer used to dispatch global events like
17623 * layout, pre-draw, touch mode change, etc.
17624 */
17625 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
17626
17627 /**
17628 * A Canvas used by the view hierarchy to perform bitmap caching.
17629 */
17630 Canvas mCanvas;
17631
17632 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080017633 * The view root impl.
17634 */
17635 final ViewRootImpl mViewRootImpl;
17636
17637 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070017638 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017639 * handler can be used to pump events in the UI events queue.
17640 */
17641 final Handler mHandler;
17642
17643 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017644 * Temporary for use in computing invalidate rectangles while
17645 * calling up the hierarchy.
17646 */
17647 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070017648
17649 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070017650 * Temporary for use in computing hit areas with transformed views
17651 */
17652 final RectF mTmpTransformRect = new RectF();
17653
17654 /**
Chet Haase599913d2012-07-23 16:22:05 -070017655 * Temporary for use in transforming invalidation rect
17656 */
17657 final Matrix mTmpMatrix = new Matrix();
17658
17659 /**
17660 * Temporary for use in transforming invalidation rect
17661 */
17662 final Transformation mTmpTransformation = new Transformation();
17663
17664 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070017665 * Temporary list for use in collecting focusable descendents of a view.
17666 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070017667 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070017668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017669 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017670 * The id of the window for accessibility purposes.
17671 */
17672 int mAccessibilityWindowId = View.NO_ID;
17673
17674 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -070017675 * Whether to ingore not exposed for accessibility Views when
17676 * reporting the view tree to accessibility services.
17677 */
17678 boolean mIncludeNotImportantViews;
17679
17680 /**
17681 * The drawable for highlighting accessibility focus.
17682 */
17683 Drawable mAccessibilityFocusDrawable;
17684
17685 /**
Philip Milne10ca24a2012-04-23 15:38:27 -070017686 * Show where the margins, bounds and layout bounds are for each view.
17687 */
Dianne Hackborna53de062012-05-08 18:53:51 -070017688 boolean mDebugLayout = SystemProperties.getBoolean(DEBUG_LAYOUT_PROPERTY, false);
Philip Milne10ca24a2012-04-23 15:38:27 -070017689
17690 /**
Romain Guyab4c4f4f2012-05-06 13:11:24 -070017691 * Point used to compute visible regions.
17692 */
17693 final Point mPoint = new Point();
17694
17695 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017696 * Creates a new set of attachment information with the specified
17697 * events handler and thread.
17698 *
17699 * @param handler the events handler the view must use
17700 */
Jeff Brown98365d72012-08-19 20:30:52 -070017701 AttachInfo(IWindowSession session, IWindow window, Display display,
Jeff Browna175a5b2012-02-15 19:18:31 -080017702 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017703 mSession = session;
17704 mWindow = window;
17705 mWindowToken = window.asBinder();
Jeff Brown98365d72012-08-19 20:30:52 -070017706 mDisplay = display;
Jeff Browna175a5b2012-02-15 19:18:31 -080017707 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017708 mHandler = handler;
17709 mRootCallbacks = effectPlayer;
17710 }
17711 }
17712
17713 /**
17714 * <p>ScrollabilityCache holds various fields used by a View when scrolling
17715 * is supported. This avoids keeping too many unused fields in most
17716 * instances of View.</p>
17717 */
Mike Cleronf116bf82009-09-27 19:14:12 -070017718 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080017719
Mike Cleronf116bf82009-09-27 19:14:12 -070017720 /**
17721 * Scrollbars are not visible
17722 */
17723 public static final int OFF = 0;
17724
17725 /**
17726 * Scrollbars are visible
17727 */
17728 public static final int ON = 1;
17729
17730 /**
17731 * Scrollbars are fading away
17732 */
17733 public static final int FADING = 2;
17734
17735 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080017736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017737 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070017738 public int scrollBarDefaultDelayBeforeFade;
17739 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017740
17741 public int scrollBarSize;
17742 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070017743 public float[] interpolatorValues;
17744 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017745
17746 public final Paint paint;
17747 public final Matrix matrix;
17748 public Shader shader;
17749
Mike Cleronf116bf82009-09-27 19:14:12 -070017750 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
17751
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017752 private static final float[] OPAQUE = { 255 };
17753 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080017754
Mike Cleronf116bf82009-09-27 19:14:12 -070017755 /**
17756 * When fading should start. This time moves into the future every time
17757 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
17758 */
17759 public long fadeStartTime;
17760
17761
17762 /**
17763 * The current state of the scrollbars: ON, OFF, or FADING
17764 */
17765 public int state = OFF;
17766
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017767 private int mLastColor;
17768
Mike Cleronf116bf82009-09-27 19:14:12 -070017769 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017770 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
17771 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070017772 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
17773 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017774
17775 paint = new Paint();
17776 matrix = new Matrix();
17777 // use use a height of 1, and then wack the matrix each time we
17778 // actually use it.
17779 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017780 paint.setShader(shader);
17781 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Romain Guyd679b572012-08-29 21:49:00 -070017782
Mike Cleronf116bf82009-09-27 19:14:12 -070017783 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017784 }
Romain Guy8506ab42009-06-11 17:35:47 -070017785
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017786 public void setFadeColor(int color) {
Romain Guyd679b572012-08-29 21:49:00 -070017787 if (color != mLastColor) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017788 mLastColor = color;
Romain Guy8506ab42009-06-11 17:35:47 -070017789
Romain Guyd679b572012-08-29 21:49:00 -070017790 if (color != 0) {
17791 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
17792 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
17793 paint.setShader(shader);
17794 // Restore the default transfer mode (src_over)
17795 paint.setXfermode(null);
17796 } else {
17797 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
17798 paint.setShader(shader);
17799 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
17800 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017801 }
17802 }
Joe Malin32736f02011-01-19 16:14:20 -080017803
Mike Cleronf116bf82009-09-27 19:14:12 -070017804 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070017805 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070017806 if (now >= fadeStartTime) {
17807
17808 // the animation fades the scrollbars out by changing
17809 // the opacity (alpha) from fully opaque to fully
17810 // transparent
17811 int nextFrame = (int) now;
17812 int framesCount = 0;
17813
17814 Interpolator interpolator = scrollBarInterpolator;
17815
17816 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017817 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070017818
17819 // End transparent
17820 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017821 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070017822
17823 state = FADING;
17824
17825 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080017826 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070017827 }
17828 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070017829 }
Mike Cleronf116bf82009-09-27 19:14:12 -070017830
Svetoslav Ganova0156172011-06-26 17:55:44 -070017831 /**
17832 * Resuable callback for sending
17833 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
17834 */
17835 private class SendViewScrolledAccessibilityEvent implements Runnable {
17836 public volatile boolean mIsPending;
17837
17838 public void run() {
17839 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
17840 mIsPending = false;
17841 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017842 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017843
17844 /**
17845 * <p>
17846 * This class represents a delegate that can be registered in a {@link View}
17847 * to enhance accessibility support via composition rather via inheritance.
17848 * It is specifically targeted to widget developers that extend basic View
17849 * classes i.e. classes in package android.view, that would like their
17850 * applications to be backwards compatible.
17851 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080017852 * <div class="special reference">
17853 * <h3>Developer Guides</h3>
17854 * <p>For more information about making applications accessible, read the
17855 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
17856 * developer guide.</p>
17857 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017858 * <p>
17859 * A scenario in which a developer would like to use an accessibility delegate
17860 * is overriding a method introduced in a later API version then the minimal API
17861 * version supported by the application. For example, the method
17862 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
17863 * in API version 4 when the accessibility APIs were first introduced. If a
17864 * developer would like his application to run on API version 4 devices (assuming
17865 * all other APIs used by the application are version 4 or lower) and take advantage
17866 * of this method, instead of overriding the method which would break the application's
17867 * backwards compatibility, he can override the corresponding method in this
17868 * delegate and register the delegate in the target View if the API version of
17869 * the system is high enough i.e. the API version is same or higher to the API
17870 * version that introduced
17871 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
17872 * </p>
17873 * <p>
17874 * Here is an example implementation:
17875 * </p>
17876 * <code><pre><p>
17877 * if (Build.VERSION.SDK_INT >= 14) {
17878 * // If the API version is equal of higher than the version in
17879 * // which onInitializeAccessibilityNodeInfo was introduced we
17880 * // register a delegate with a customized implementation.
17881 * View view = findViewById(R.id.view_id);
17882 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
17883 * public void onInitializeAccessibilityNodeInfo(View host,
17884 * AccessibilityNodeInfo info) {
17885 * // Let the default implementation populate the info.
17886 * super.onInitializeAccessibilityNodeInfo(host, info);
17887 * // Set some other information.
17888 * info.setEnabled(host.isEnabled());
17889 * }
17890 * });
17891 * }
17892 * </code></pre></p>
17893 * <p>
17894 * This delegate contains methods that correspond to the accessibility methods
17895 * in View. If a delegate has been specified the implementation in View hands
17896 * off handling to the corresponding method in this delegate. The default
17897 * implementation the delegate methods behaves exactly as the corresponding
17898 * method in View for the case of no accessibility delegate been set. Hence,
17899 * to customize the behavior of a View method, clients can override only the
17900 * corresponding delegate method without altering the behavior of the rest
17901 * accessibility related methods of the host view.
17902 * </p>
17903 */
17904 public static class AccessibilityDelegate {
17905
17906 /**
17907 * Sends an accessibility event of the given type. If accessibility is not
17908 * enabled this method has no effect.
17909 * <p>
17910 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
17911 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
17912 * been set.
17913 * </p>
17914 *
17915 * @param host The View hosting the delegate.
17916 * @param eventType The type of the event to send.
17917 *
17918 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
17919 */
17920 public void sendAccessibilityEvent(View host, int eventType) {
17921 host.sendAccessibilityEventInternal(eventType);
17922 }
17923
17924 /**
alanv8eeefef2012-05-07 16:57:53 -070017925 * Performs the specified accessibility action on the view. For
17926 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
17927 * <p>
17928 * The default implementation behaves as
17929 * {@link View#performAccessibilityAction(int, Bundle)
17930 * View#performAccessibilityAction(int, Bundle)} for the case of
17931 * no accessibility delegate been set.
17932 * </p>
17933 *
17934 * @param action The action to perform.
17935 * @return Whether the action was performed.
17936 *
17937 * @see View#performAccessibilityAction(int, Bundle)
17938 * View#performAccessibilityAction(int, Bundle)
17939 */
17940 public boolean performAccessibilityAction(View host, int action, Bundle args) {
17941 return host.performAccessibilityActionInternal(action, args);
17942 }
17943
17944 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017945 * Sends an accessibility event. This method behaves exactly as
17946 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
17947 * empty {@link AccessibilityEvent} and does not perform a check whether
17948 * accessibility is enabled.
17949 * <p>
17950 * The default implementation behaves as
17951 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17952 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
17953 * the case of no accessibility delegate been set.
17954 * </p>
17955 *
17956 * @param host The View hosting the delegate.
17957 * @param event The event to send.
17958 *
17959 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17960 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17961 */
17962 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
17963 host.sendAccessibilityEventUncheckedInternal(event);
17964 }
17965
17966 /**
17967 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
17968 * to its children for adding their text content to the event.
17969 * <p>
17970 * The default implementation behaves as
17971 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17972 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
17973 * the case of no accessibility delegate been set.
17974 * </p>
17975 *
17976 * @param host The View hosting the delegate.
17977 * @param event The event.
17978 * @return True if the event population was completed.
17979 *
17980 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17981 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17982 */
17983 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17984 return host.dispatchPopulateAccessibilityEventInternal(event);
17985 }
17986
17987 /**
17988 * Gives a chance to the host View to populate the accessibility event with its
17989 * text content.
17990 * <p>
17991 * The default implementation behaves as
17992 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
17993 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
17994 * the case of no accessibility delegate been set.
17995 * </p>
17996 *
17997 * @param host The View hosting the delegate.
17998 * @param event The accessibility event which to populate.
17999 *
18000 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
18001 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
18002 */
18003 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
18004 host.onPopulateAccessibilityEventInternal(event);
18005 }
18006
18007 /**
18008 * Initializes an {@link AccessibilityEvent} with information about the
18009 * the host View which is the event source.
18010 * <p>
18011 * The default implementation behaves as
18012 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
18013 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
18014 * the case of no accessibility delegate been set.
18015 * </p>
18016 *
18017 * @param host The View hosting the delegate.
18018 * @param event The event to initialize.
18019 *
18020 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
18021 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
18022 */
18023 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
18024 host.onInitializeAccessibilityEventInternal(event);
18025 }
18026
18027 /**
18028 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
18029 * <p>
18030 * The default implementation behaves as
18031 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
18032 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
18033 * the case of no accessibility delegate been set.
18034 * </p>
18035 *
18036 * @param host The View hosting the delegate.
18037 * @param info The instance to initialize.
18038 *
18039 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
18040 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
18041 */
18042 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
18043 host.onInitializeAccessibilityNodeInfoInternal(info);
18044 }
18045
18046 /**
18047 * Called when a child of the host View has requested sending an
18048 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
18049 * to augment the event.
18050 * <p>
18051 * The default implementation behaves as
18052 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
18053 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
18054 * the case of no accessibility delegate been set.
18055 * </p>
18056 *
18057 * @param host The View hosting the delegate.
18058 * @param child The child which requests sending the event.
18059 * @param event The event to be sent.
18060 * @return True if the event should be sent
18061 *
18062 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
18063 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
18064 */
18065 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
18066 AccessibilityEvent event) {
18067 return host.onRequestSendAccessibilityEventInternal(child, event);
18068 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070018069
18070 /**
18071 * Gets the provider for managing a virtual view hierarchy rooted at this View
18072 * and reported to {@link android.accessibilityservice.AccessibilityService}s
18073 * that explore the window content.
18074 * <p>
18075 * The default implementation behaves as
18076 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
18077 * the case of no accessibility delegate been set.
18078 * </p>
18079 *
18080 * @return The provider.
18081 *
18082 * @see AccessibilityNodeProvider
18083 */
18084 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
18085 return null;
18086 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018087 }
Svetoslav Ganov33aef982012-09-13 12:49:03 -070018088
18089 private class MatchIdPredicate implements Predicate<View> {
18090 public int mId;
18091
18092 @Override
18093 public boolean apply(View view) {
18094 return (view.mID == mId);
18095 }
18096 }
18097
18098 private class MatchLabelForPredicate implements Predicate<View> {
18099 private int mLabeledId;
18100
18101 @Override
18102 public boolean apply(View view) {
18103 return (view.mLabelForId == mLabeledId);
18104 }
18105 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018106}