blob: 44f39de314978f478643e597764e02135f85d159 [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 Brown98365d72012-08-19 20:30:52 -070042import android.hardware.display.DisplayManager;
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 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002762 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002763 *
2764 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002765 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002766 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002767 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002768
2769 /**
2770 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002771 *
2772 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002773 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002774 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002775 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002776
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002777 /**
Adam Powell20232d02010-12-08 21:08:53 -08002778 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002779 *
2780 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002781 */
2782 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002783 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002784
2785 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002786 * Cache the paddingStart set by the user to append to the scrollbar's size.
2787 *
2788 */
2789 @ViewDebug.ExportedProperty(category = "padding")
2790 int mUserPaddingStart;
2791
2792 /**
2793 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2794 *
2795 */
2796 @ViewDebug.ExportedProperty(category = "padding")
2797 int mUserPaddingEnd;
2798
2799 /**
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07002800 * Whether a left padding has been defined during layout inflation.
2801 *
2802 * @hide
2803 */
2804 boolean mUserPaddingLeftDefined = false;
2805
2806 /**
2807 * Whether a right padding has been defined during layout inflation.
2808 *
2809 * @hide
2810 */
2811 boolean mUserPaddingRightDefined = false;
2812
2813 /**
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07002814 * Default undefined padding
2815 */
2816 private static final int UNDEFINED_PADDING = Integer.MIN_VALUE;
2817
2818 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002819 * @hide
2820 */
2821 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2822 /**
2823 * @hide
2824 */
2825 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002826
Philip Milne6c8ea062012-04-03 17:38:43 -07002827 private Drawable mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002828
2829 private int mBackgroundResource;
2830 private boolean mBackgroundSizeChanged;
2831
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002832 static class ListenerInfo {
2833 /**
2834 * Listener used to dispatch focus change events.
2835 * This field should be made private, so it is hidden from the SDK.
2836 * {@hide}
2837 */
2838 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002839
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002840 /**
2841 * Listeners for layout change events.
2842 */
2843 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002844
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002845 /**
2846 * Listeners for attach events.
2847 */
2848 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002849
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002850 /**
2851 * Listener used to dispatch click events.
2852 * This field should be made private, so it is hidden from the SDK.
2853 * {@hide}
2854 */
2855 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002856
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002857 /**
2858 * Listener used to dispatch long click events.
2859 * This field should be made private, so it is hidden from the SDK.
2860 * {@hide}
2861 */
2862 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002863
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002864 /**
2865 * Listener used to build the context menu.
2866 * This field should be made private, so it is hidden from the SDK.
2867 * {@hide}
2868 */
2869 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002870
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002871 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002872
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002873 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002874
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002875 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002876
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002877 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002878
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002879 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002880
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002881 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2882 }
2883
2884 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002886 /**
2887 * The application environment this view lives in.
2888 * This field should be made private, so it is hidden from the SDK.
2889 * {@hide}
2890 */
2891 protected Context mContext;
2892
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002893 private final Resources mResources;
2894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002895 private ScrollabilityCache mScrollCache;
2896
2897 private int[] mDrawableState = null;
2898
Romain Guy0211a0a2011-02-14 16:34:59 -08002899 /**
2900 * Set to true when drawing cache is enabled and cannot be created.
Philip Milne6c8ea062012-04-03 17:38:43 -07002901 *
Romain Guy0211a0a2011-02-14 16:34:59 -08002902 * @hide
2903 */
2904 public boolean mCachingFailed;
2905
Romain Guy02890fd2010-08-06 17:58:44 -07002906 private Bitmap mDrawingCache;
2907 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002908 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002909 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002910
2911 /**
2912 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2913 * the user may specify which view to go to next.
2914 */
2915 private int mNextFocusLeftId = View.NO_ID;
2916
2917 /**
2918 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2919 * the user may specify which view to go to next.
2920 */
2921 private int mNextFocusRightId = View.NO_ID;
2922
2923 /**
2924 * When this view has focus and the next focus is {@link #FOCUS_UP},
2925 * the user may specify which view to go to next.
2926 */
2927 private int mNextFocusUpId = View.NO_ID;
2928
2929 /**
2930 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2931 * the user may specify which view to go to next.
2932 */
2933 private int mNextFocusDownId = View.NO_ID;
2934
Jeff Brown4e6319b2010-12-13 10:36:51 -08002935 /**
2936 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2937 * the user may specify which view to go to next.
2938 */
2939 int mNextFocusForwardId = View.NO_ID;
2940
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002941 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002942 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002943 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002944 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002946 private UnsetPressedState mUnsetPressedState;
2947
2948 /**
2949 * Whether the long press's action has been invoked. The tap's action is invoked on the
2950 * up event while a long press is invoked as soon as the long press duration is reached, so
2951 * a long press could be performed before the tap is checked, in which case the tap's action
2952 * should not be invoked.
2953 */
2954 private boolean mHasPerformedLongPress;
2955
2956 /**
2957 * The minimum height of the view. We'll try our best to have the height
2958 * of this view to at least this amount.
2959 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002960 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002961 private int mMinHeight;
2962
2963 /**
2964 * The minimum width of the view. We'll try our best to have the width
2965 * of this view to at least this amount.
2966 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002967 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002968 private int mMinWidth;
2969
2970 /**
2971 * The delegate to handle touch events that are physically in this view
2972 * but should be handled by another view.
2973 */
2974 private TouchDelegate mTouchDelegate = null;
2975
2976 /**
2977 * Solid color to use as a background when creating the drawing cache. Enables
2978 * the cache to use 16 bit bitmaps instead of 32 bit.
2979 */
2980 private int mDrawingCacheBackgroundColor = 0;
2981
2982 /**
2983 * Special tree observer used when mAttachInfo is null.
2984 */
2985 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002986
Adam Powelle14579b2009-12-16 18:39:52 -08002987 /**
2988 * Cache the touch slop from the context that created the view.
2989 */
2990 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002992 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002993 * Object that handles automatic animation of view properties.
2994 */
2995 private ViewPropertyAnimator mAnimator = null;
2996
2997 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002998 * Flag indicating that a drag can cross window boundaries. When
2999 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
3000 * with this flag set, all visible applications will be able to participate
3001 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08003002 *
3003 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08003004 */
3005 public static final int DRAG_FLAG_GLOBAL = 1;
3006
3007 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003008 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
3009 */
3010 private float mVerticalScrollFactor;
3011
3012 /**
Adam Powell20232d02010-12-08 21:08:53 -08003013 * Position of the vertical scroll bar.
3014 */
3015 private int mVerticalScrollbarPosition;
3016
3017 /**
3018 * Position the scroll bar at the default position as determined by the system.
3019 */
3020 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
3021
3022 /**
3023 * Position the scroll bar along the left edge.
3024 */
3025 public static final int SCROLLBAR_POSITION_LEFT = 1;
3026
3027 /**
3028 * Position the scroll bar along the right edge.
3029 */
3030 public static final int SCROLLBAR_POSITION_RIGHT = 2;
3031
3032 /**
Romain Guy171c5922011-01-06 10:04:23 -08003033 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08003034 *
3035 * @see #getLayerType()
3036 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003037 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08003038 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003039 */
3040 public static final int LAYER_TYPE_NONE = 0;
3041
3042 /**
3043 * <p>Indicates that the view has a software layer. A software layer is backed
3044 * by a bitmap and causes the view to be rendered using Android's software
3045 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003046 *
Romain Guy171c5922011-01-06 10:04:23 -08003047 * <p>Software layers have various usages:</p>
3048 * <p>When the application is not using hardware acceleration, a software layer
3049 * is useful to apply a specific color filter and/or blending mode and/or
3050 * translucency to a view and all its children.</p>
3051 * <p>When the application is using hardware acceleration, a software layer
3052 * is useful to render drawing primitives not supported by the hardware
3053 * accelerated pipeline. It can also be used to cache a complex view tree
3054 * into a texture and reduce the complexity of drawing operations. For instance,
3055 * when animating a complex view tree with a translation, a software layer can
3056 * be used to render the view tree only once.</p>
3057 * <p>Software layers should be avoided when the affected view tree updates
3058 * often. Every update will require to re-render the software layer, which can
3059 * potentially be slow (particularly when hardware acceleration is turned on
3060 * since the layer will have to be uploaded into a hardware texture after every
3061 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08003062 *
3063 * @see #getLayerType()
3064 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003065 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08003066 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003067 */
3068 public static final int LAYER_TYPE_SOFTWARE = 1;
3069
3070 /**
3071 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
3072 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
3073 * OpenGL hardware) and causes the view to be rendered using Android's hardware
3074 * rendering pipeline, but only if hardware acceleration is turned on for the
3075 * view hierarchy. When hardware acceleration is turned off, hardware layers
3076 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003077 *
Romain Guy171c5922011-01-06 10:04:23 -08003078 * <p>A hardware layer is useful to apply a specific color filter and/or
3079 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08003080 * <p>A hardware layer can be used to cache a complex view tree into a
3081 * texture and reduce the complexity of drawing operations. For instance,
3082 * when animating a complex view tree with a translation, a hardware layer can
3083 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08003084 * <p>A hardware layer can also be used to increase the rendering quality when
3085 * rotation transformations are applied on a view. It can also be used to
3086 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003087 *
3088 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08003089 * @see #setLayerType(int, android.graphics.Paint)
3090 * @see #LAYER_TYPE_NONE
3091 * @see #LAYER_TYPE_SOFTWARE
3092 */
3093 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08003094
Romain Guy3aaff3a2011-01-12 14:18:47 -08003095 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
3096 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
3097 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
3098 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
3099 })
Romain Guy171c5922011-01-06 10:04:23 -08003100 int mLayerType = LAYER_TYPE_NONE;
3101 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08003102 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08003103
3104 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07003105 * Set to true when the view is sending hover accessibility events because it
3106 * is the innermost hovered view.
3107 */
3108 private boolean mSendingHoverAccessibilityEvents;
3109
Dianne Hackborn4702a852012-08-17 15:18:29 -07003110 /**
3111 * Delegate for injecting accessiblity functionality.
3112 */
3113 AccessibilityDelegate mAccessibilityDelegate;
3114
3115 /**
3116 * Consistency verifier for debugging purposes.
3117 * @hide
3118 */
3119 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
3120 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
3121 new InputEventConsistencyVerifier(this, 0) : null;
3122
Adam Powella9108a22012-07-18 11:18:09 -07003123 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
3124
Jeff Brown87b7f802011-06-21 18:35:45 -07003125 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003126 * Simple constructor to use when creating a view from code.
3127 *
3128 * @param context The Context the view is running in, through which it can
3129 * access the current theme, resources, etc.
3130 */
3131 public View(Context context) {
3132 mContext = context;
3133 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003134 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003135 // Set layout and text direction defaults
Dianne Hackborn4702a852012-08-17 15:18:29 -07003136 mPrivateFlags2 = (LAYOUT_DIRECTION_DEFAULT << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) |
3137 (TEXT_DIRECTION_DEFAULT << PFLAG2_TEXT_DIRECTION_MASK_SHIFT) |
3138 (TEXT_ALIGNMENT_DEFAULT << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT) |
3139 (IMPORTANT_FOR_ACCESSIBILITY_DEFAULT << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT);
Adam Powelle14579b2009-12-16 18:39:52 -08003140 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07003141 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003142 mUserPaddingStart = UNDEFINED_PADDING;
3143 mUserPaddingEnd = UNDEFINED_PADDING;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003144 }
3145
3146 /**
3147 * Constructor that is called when inflating a view from XML. This is called
3148 * when a view is being constructed from an XML file, supplying attributes
3149 * that were specified in the XML file. This version uses a default style of
3150 * 0, so the only attribute values applied are those in the Context's Theme
3151 * and the given AttributeSet.
3152 *
3153 * <p>
3154 * The method onFinishInflate() will be called after all children have been
3155 * added.
3156 *
3157 * @param context The Context the view is running in, through which it can
3158 * access the current theme, resources, etc.
3159 * @param attrs The attributes of the XML tag that is inflating the view.
3160 * @see #View(Context, AttributeSet, int)
3161 */
3162 public View(Context context, AttributeSet attrs) {
3163 this(context, attrs, 0);
3164 }
3165
3166 /**
3167 * Perform inflation from XML and apply a class-specific base style. This
3168 * constructor of View allows subclasses to use their own base style when
3169 * they are inflating. For example, a Button class's constructor would call
3170 * this version of the super class constructor and supply
3171 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
3172 * the theme's button style to modify all of the base view attributes (in
3173 * particular its background) as well as the Button class's attributes.
3174 *
3175 * @param context The Context the view is running in, through which it can
3176 * access the current theme, resources, etc.
3177 * @param attrs The attributes of the XML tag that is inflating the view.
3178 * @param defStyle The default style to apply to this view. If 0, no style
3179 * will be applied (beyond what is included in the theme). This may
3180 * either be an attribute resource, whose value will be retrieved
3181 * from the current theme, or an explicit style resource.
3182 * @see #View(Context, AttributeSet)
3183 */
3184 public View(Context context, AttributeSet attrs, int defStyle) {
3185 this(context);
3186
3187 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
3188 defStyle, 0);
3189
3190 Drawable background = null;
3191
3192 int leftPadding = -1;
3193 int topPadding = -1;
3194 int rightPadding = -1;
3195 int bottomPadding = -1;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003196 int startPadding = UNDEFINED_PADDING;
3197 int endPadding = UNDEFINED_PADDING;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003198
3199 int padding = -1;
3200
3201 int viewFlagValues = 0;
3202 int viewFlagMasks = 0;
3203
3204 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07003205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003206 int x = 0;
3207 int y = 0;
3208
Chet Haase73066682010-11-29 15:55:32 -08003209 float tx = 0;
3210 float ty = 0;
3211 float rotation = 0;
3212 float rotationX = 0;
3213 float rotationY = 0;
3214 float sx = 1f;
3215 float sy = 1f;
3216 boolean transformSet = false;
3217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003218 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
Adam Powell637d3372010-08-25 14:37:03 -07003219 int overScrollMode = mOverScrollMode;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003220 boolean initializeScrollbars = false;
3221
3222 final int targetSdkVersion = context.getApplicationInfo().targetSdkVersion;
3223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003224 final int N = a.getIndexCount();
3225 for (int i = 0; i < N; i++) {
3226 int attr = a.getIndex(i);
3227 switch (attr) {
3228 case com.android.internal.R.styleable.View_background:
3229 background = a.getDrawable(attr);
3230 break;
3231 case com.android.internal.R.styleable.View_padding:
3232 padding = a.getDimensionPixelSize(attr, -1);
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003233 mUserPaddingLeftDefined = true;
3234 mUserPaddingRightDefined = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003235 break;
3236 case com.android.internal.R.styleable.View_paddingLeft:
3237 leftPadding = a.getDimensionPixelSize(attr, -1);
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003238 mUserPaddingLeftDefined = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003239 break;
3240 case com.android.internal.R.styleable.View_paddingTop:
3241 topPadding = a.getDimensionPixelSize(attr, -1);
3242 break;
3243 case com.android.internal.R.styleable.View_paddingRight:
3244 rightPadding = a.getDimensionPixelSize(attr, -1);
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003245 mUserPaddingRightDefined = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003246 break;
3247 case com.android.internal.R.styleable.View_paddingBottom:
3248 bottomPadding = a.getDimensionPixelSize(attr, -1);
3249 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003250 case com.android.internal.R.styleable.View_paddingStart:
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003251 startPadding = a.getDimensionPixelSize(attr, UNDEFINED_PADDING);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003252 break;
3253 case com.android.internal.R.styleable.View_paddingEnd:
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003254 endPadding = a.getDimensionPixelSize(attr, UNDEFINED_PADDING);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003255 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003256 case com.android.internal.R.styleable.View_scrollX:
3257 x = a.getDimensionPixelOffset(attr, 0);
3258 break;
3259 case com.android.internal.R.styleable.View_scrollY:
3260 y = a.getDimensionPixelOffset(attr, 0);
3261 break;
Chet Haase73066682010-11-29 15:55:32 -08003262 case com.android.internal.R.styleable.View_alpha:
3263 setAlpha(a.getFloat(attr, 1f));
3264 break;
3265 case com.android.internal.R.styleable.View_transformPivotX:
3266 setPivotX(a.getDimensionPixelOffset(attr, 0));
3267 break;
3268 case com.android.internal.R.styleable.View_transformPivotY:
3269 setPivotY(a.getDimensionPixelOffset(attr, 0));
3270 break;
3271 case com.android.internal.R.styleable.View_translationX:
3272 tx = a.getDimensionPixelOffset(attr, 0);
3273 transformSet = true;
3274 break;
3275 case com.android.internal.R.styleable.View_translationY:
3276 ty = a.getDimensionPixelOffset(attr, 0);
3277 transformSet = true;
3278 break;
3279 case com.android.internal.R.styleable.View_rotation:
3280 rotation = a.getFloat(attr, 0);
3281 transformSet = true;
3282 break;
3283 case com.android.internal.R.styleable.View_rotationX:
3284 rotationX = a.getFloat(attr, 0);
3285 transformSet = true;
3286 break;
3287 case com.android.internal.R.styleable.View_rotationY:
3288 rotationY = a.getFloat(attr, 0);
3289 transformSet = true;
3290 break;
3291 case com.android.internal.R.styleable.View_scaleX:
3292 sx = a.getFloat(attr, 1f);
3293 transformSet = true;
3294 break;
3295 case com.android.internal.R.styleable.View_scaleY:
3296 sy = a.getFloat(attr, 1f);
3297 transformSet = true;
3298 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003299 case com.android.internal.R.styleable.View_id:
3300 mID = a.getResourceId(attr, NO_ID);
3301 break;
3302 case com.android.internal.R.styleable.View_tag:
3303 mTag = a.getText(attr);
3304 break;
3305 case com.android.internal.R.styleable.View_fitsSystemWindows:
3306 if (a.getBoolean(attr, false)) {
3307 viewFlagValues |= FITS_SYSTEM_WINDOWS;
3308 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
3309 }
3310 break;
3311 case com.android.internal.R.styleable.View_focusable:
3312 if (a.getBoolean(attr, false)) {
3313 viewFlagValues |= FOCUSABLE;
3314 viewFlagMasks |= FOCUSABLE_MASK;
3315 }
3316 break;
3317 case com.android.internal.R.styleable.View_focusableInTouchMode:
3318 if (a.getBoolean(attr, false)) {
3319 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
3320 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
3321 }
3322 break;
3323 case com.android.internal.R.styleable.View_clickable:
3324 if (a.getBoolean(attr, false)) {
3325 viewFlagValues |= CLICKABLE;
3326 viewFlagMasks |= CLICKABLE;
3327 }
3328 break;
3329 case com.android.internal.R.styleable.View_longClickable:
3330 if (a.getBoolean(attr, false)) {
3331 viewFlagValues |= LONG_CLICKABLE;
3332 viewFlagMasks |= LONG_CLICKABLE;
3333 }
3334 break;
3335 case com.android.internal.R.styleable.View_saveEnabled:
3336 if (!a.getBoolean(attr, true)) {
3337 viewFlagValues |= SAVE_DISABLED;
3338 viewFlagMasks |= SAVE_DISABLED_MASK;
3339 }
3340 break;
3341 case com.android.internal.R.styleable.View_duplicateParentState:
3342 if (a.getBoolean(attr, false)) {
3343 viewFlagValues |= DUPLICATE_PARENT_STATE;
3344 viewFlagMasks |= DUPLICATE_PARENT_STATE;
3345 }
3346 break;
3347 case com.android.internal.R.styleable.View_visibility:
3348 final int visibility = a.getInt(attr, 0);
3349 if (visibility != 0) {
3350 viewFlagValues |= VISIBILITY_FLAGS[visibility];
3351 viewFlagMasks |= VISIBILITY_MASK;
3352 }
3353 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003354 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003355 // Clear any layout direction flags (included resolved bits) already set
Dianne Hackborn4702a852012-08-17 15:18:29 -07003356 mPrivateFlags2 &= ~(PFLAG2_LAYOUT_DIRECTION_MASK | PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003357 // Set the layout direction flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003358 final int layoutDirection = a.getInt(attr, -1);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003359 final int value = (layoutDirection != -1) ?
3360 LAYOUT_DIRECTION_FLAGS[layoutDirection] : LAYOUT_DIRECTION_DEFAULT;
Dianne Hackborn4702a852012-08-17 15:18:29 -07003361 mPrivateFlags2 |= (value << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT);
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07003362 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003363 case com.android.internal.R.styleable.View_drawingCacheQuality:
3364 final int cacheQuality = a.getInt(attr, 0);
3365 if (cacheQuality != 0) {
3366 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
3367 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
3368 }
3369 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07003370 case com.android.internal.R.styleable.View_contentDescription:
Svetoslav Ganove47957a2012-06-05 14:46:50 -07003371 setContentDescription(a.getString(attr));
svetoslavganov75986cf2009-05-14 22:28:01 -07003372 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003373 case com.android.internal.R.styleable.View_soundEffectsEnabled:
3374 if (!a.getBoolean(attr, true)) {
3375 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
3376 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
3377 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003378 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003379 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
3380 if (!a.getBoolean(attr, true)) {
3381 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
3382 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
3383 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003384 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003385 case R.styleable.View_scrollbars:
3386 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
3387 if (scrollbars != SCROLLBARS_NONE) {
3388 viewFlagValues |= scrollbars;
3389 viewFlagMasks |= SCROLLBARS_MASK;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003390 initializeScrollbars = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003391 }
3392 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07003393 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003394 case R.styleable.View_fadingEdge:
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003395 if (targetSdkVersion >= ICE_CREAM_SANDWICH) {
Romain Guy1ef3fdb2011-09-09 15:30:30 -07003396 // Ignore the attribute starting with ICS
3397 break;
3398 }
3399 // With builds < ICS, fall through and apply fading edges
3400 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003401 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
3402 if (fadingEdge != FADING_EDGE_NONE) {
3403 viewFlagValues |= fadingEdge;
3404 viewFlagMasks |= FADING_EDGE_MASK;
3405 initializeFadingEdge(a);
3406 }
3407 break;
3408 case R.styleable.View_scrollbarStyle:
3409 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3410 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3411 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3412 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3413 }
3414 break;
3415 case R.styleable.View_isScrollContainer:
3416 setScrollContainer = true;
3417 if (a.getBoolean(attr, false)) {
3418 setScrollContainer(true);
3419 }
3420 break;
3421 case com.android.internal.R.styleable.View_keepScreenOn:
3422 if (a.getBoolean(attr, false)) {
3423 viewFlagValues |= KEEP_SCREEN_ON;
3424 viewFlagMasks |= KEEP_SCREEN_ON;
3425 }
3426 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003427 case R.styleable.View_filterTouchesWhenObscured:
3428 if (a.getBoolean(attr, false)) {
3429 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3430 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3431 }
3432 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003433 case R.styleable.View_nextFocusLeft:
3434 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3435 break;
3436 case R.styleable.View_nextFocusRight:
3437 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3438 break;
3439 case R.styleable.View_nextFocusUp:
3440 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3441 break;
3442 case R.styleable.View_nextFocusDown:
3443 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3444 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003445 case R.styleable.View_nextFocusForward:
3446 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3447 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003448 case R.styleable.View_minWidth:
3449 mMinWidth = a.getDimensionPixelSize(attr, 0);
3450 break;
3451 case R.styleable.View_minHeight:
3452 mMinHeight = a.getDimensionPixelSize(attr, 0);
3453 break;
Romain Guy9a817362009-05-01 10:57:14 -07003454 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003455 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003456 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003457 + "be used within a restricted context");
3458 }
3459
Romain Guy9a817362009-05-01 10:57:14 -07003460 final String handlerName = a.getString(attr);
3461 if (handlerName != null) {
3462 setOnClickListener(new OnClickListener() {
3463 private Method mHandler;
3464
3465 public void onClick(View v) {
3466 if (mHandler == null) {
3467 try {
3468 mHandler = getContext().getClass().getMethod(handlerName,
3469 View.class);
3470 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003471 int id = getId();
3472 String idText = id == NO_ID ? "" : " with id '"
3473 + getContext().getResources().getResourceEntryName(
3474 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003475 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003476 handlerName + "(View) in the activity "
3477 + getContext().getClass() + " for onClick handler"
3478 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003479 }
3480 }
3481
3482 try {
3483 mHandler.invoke(getContext(), View.this);
3484 } catch (IllegalAccessException e) {
3485 throw new IllegalStateException("Could not execute non "
3486 + "public method of the activity", e);
3487 } catch (InvocationTargetException e) {
3488 throw new IllegalStateException("Could not execute "
3489 + "method of the activity", e);
3490 }
3491 }
3492 });
3493 }
3494 break;
Adam Powell637d3372010-08-25 14:37:03 -07003495 case R.styleable.View_overScrollMode:
3496 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3497 break;
Adam Powell20232d02010-12-08 21:08:53 -08003498 case R.styleable.View_verticalScrollbarPosition:
3499 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3500 break;
Romain Guy171c5922011-01-06 10:04:23 -08003501 case R.styleable.View_layerType:
3502 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3503 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003504 case R.styleable.View_textDirection:
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003505 // Clear any text direction flag already set
Dianne Hackborn4702a852012-08-17 15:18:29 -07003506 mPrivateFlags2 &= ~PFLAG2_TEXT_DIRECTION_MASK;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003507 // Set the text direction flags depending on the value of the attribute
3508 final int textDirection = a.getInt(attr, -1);
3509 if (textDirection != -1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07003510 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_FLAGS[textDirection];
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003511 }
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003512 break;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003513 case R.styleable.View_textAlignment:
3514 // Clear any text alignment flag already set
Dianne Hackborn4702a852012-08-17 15:18:29 -07003515 mPrivateFlags2 &= ~PFLAG2_TEXT_ALIGNMENT_MASK;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003516 // Set the text alignment flag depending on the value of the attribute
3517 final int textAlignment = a.getInt(attr, TEXT_ALIGNMENT_DEFAULT);
Dianne Hackborn4702a852012-08-17 15:18:29 -07003518 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_FLAGS[textAlignment];
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003519 break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07003520 case R.styleable.View_importantForAccessibility:
3521 setImportantForAccessibility(a.getInt(attr,
3522 IMPORTANT_FOR_ACCESSIBILITY_DEFAULT));
Svetoslav Ganov86783472012-06-06 21:12:20 -07003523 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003524 }
3525 }
3526
Adam Powell637d3372010-08-25 14:37:03 -07003527 setOverScrollMode(overScrollMode);
3528
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003529 // Cache start/end user padding as we cannot fully resolve padding here (we dont have yet
3530 // the resolved layout direction). Those cached values will be used later during padding
3531 // resolution.
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003532 mUserPaddingStart = startPadding;
3533 mUserPaddingEnd = endPadding;
3534
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003535 if (background != null) {
3536 setBackground(background);
3537 }
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003539 if (padding >= 0) {
3540 leftPadding = padding;
3541 topPadding = padding;
3542 rightPadding = padding;
3543 bottomPadding = padding;
3544 }
3545
3546 // If the user specified the padding (either with android:padding or
3547 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3548 // use the default padding or the padding from the background drawable
3549 // (stored at this point in mPadding*)
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003550 internalSetPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003551 topPadding >= 0 ? topPadding : mPaddingTop,
3552 rightPadding >= 0 ? rightPadding : mPaddingRight,
3553 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3554
3555 if (viewFlagMasks != 0) {
3556 setFlags(viewFlagValues, viewFlagMasks);
3557 }
3558
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003559 if (initializeScrollbars) {
3560 initializeScrollbars(a);
3561 }
3562
3563 a.recycle();
3564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003565 // Needs to be called after mViewFlags is set
3566 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3567 recomputePadding();
3568 }
3569
3570 if (x != 0 || y != 0) {
3571 scrollTo(x, y);
3572 }
3573
Chet Haase73066682010-11-29 15:55:32 -08003574 if (transformSet) {
3575 setTranslationX(tx);
3576 setTranslationY(ty);
3577 setRotation(rotation);
3578 setRotationX(rotationX);
3579 setRotationY(rotationY);
3580 setScaleX(sx);
3581 setScaleY(sy);
3582 }
3583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003584 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3585 setScrollContainer(true);
3586 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003587
3588 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003589 }
3590
3591 /**
3592 * Non-public constructor for use in testing
3593 */
3594 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003595 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003596 }
3597
Dianne Hackborn4702a852012-08-17 15:18:29 -07003598 public String toString() {
3599 StringBuilder out = new StringBuilder(128);
3600 out.append(getClass().getName());
3601 out.append('{');
3602 out.append(Integer.toHexString(System.identityHashCode(this)));
3603 out.append(' ');
3604 switch (mViewFlags&VISIBILITY_MASK) {
3605 case VISIBLE: out.append('V'); break;
3606 case INVISIBLE: out.append('I'); break;
3607 case GONE: out.append('G'); break;
3608 default: out.append('.'); break;
3609 }
3610 out.append((mViewFlags&FOCUSABLE_MASK) == FOCUSABLE ? 'F' : '.');
3611 out.append((mViewFlags&ENABLED_MASK) == ENABLED ? 'E' : '.');
3612 out.append((mViewFlags&DRAW_MASK) == WILL_NOT_DRAW ? '.' : 'D');
3613 out.append((mViewFlags&SCROLLBARS_HORIZONTAL) != 0 ? 'H' : '.');
3614 out.append((mViewFlags&SCROLLBARS_VERTICAL) != 0 ? 'V' : '.');
3615 out.append((mViewFlags&CLICKABLE) != 0 ? 'C' : '.');
3616 out.append((mViewFlags&LONG_CLICKABLE) != 0 ? 'L' : '.');
3617 out.append(' ');
3618 out.append((mPrivateFlags&PFLAG_IS_ROOT_NAMESPACE) != 0 ? 'R' : '.');
3619 out.append((mPrivateFlags&PFLAG_FOCUSED) != 0 ? 'F' : '.');
3620 out.append((mPrivateFlags&PFLAG_SELECTED) != 0 ? 'S' : '.');
3621 if ((mPrivateFlags&PFLAG_PREPRESSED) != 0) {
3622 out.append('p');
3623 } else {
3624 out.append((mPrivateFlags&PFLAG_PRESSED) != 0 ? 'P' : '.');
3625 }
3626 out.append((mPrivateFlags&PFLAG_HOVERED) != 0 ? 'H' : '.');
3627 out.append((mPrivateFlags&PFLAG_ACTIVATED) != 0 ? 'A' : '.');
3628 out.append((mPrivateFlags&PFLAG_INVALIDATED) != 0 ? 'I' : '.');
3629 out.append((mPrivateFlags&PFLAG_DIRTY_MASK) != 0 ? 'D' : '.');
3630 out.append(' ');
3631 out.append(mLeft);
3632 out.append(',');
3633 out.append(mTop);
3634 out.append('-');
3635 out.append(mRight);
3636 out.append(',');
3637 out.append(mBottom);
3638 final int id = getId();
3639 if (id != NO_ID) {
3640 out.append(" #");
3641 out.append(Integer.toHexString(id));
3642 final Resources r = mResources;
3643 if (id != 0 && r != null) {
3644 try {
3645 String pkgname;
3646 switch (id&0xff000000) {
3647 case 0x7f000000:
3648 pkgname="app";
3649 break;
3650 case 0x01000000:
3651 pkgname="android";
3652 break;
3653 default:
3654 pkgname = r.getResourcePackageName(id);
3655 break;
3656 }
3657 String typename = r.getResourceTypeName(id);
3658 String entryname = r.getResourceEntryName(id);
3659 out.append(" ");
3660 out.append(pkgname);
3661 out.append(":");
3662 out.append(typename);
3663 out.append("/");
3664 out.append(entryname);
3665 } catch (Resources.NotFoundException e) {
3666 }
3667 }
3668 }
3669 out.append("}");
3670 return out.toString();
3671 }
3672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003673 /**
3674 * <p>
3675 * Initializes the fading edges from a given set of styled attributes. This
3676 * method should be called by subclasses that need fading edges and when an
3677 * instance of these subclasses is created programmatically rather than
3678 * being inflated from XML. This method is automatically called when the XML
3679 * is inflated.
3680 * </p>
3681 *
3682 * @param a the styled attributes set to initialize the fading edges from
3683 */
3684 protected void initializeFadingEdge(TypedArray a) {
3685 initScrollCache();
3686
3687 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3688 R.styleable.View_fadingEdgeLength,
3689 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3690 }
3691
3692 /**
3693 * Returns the size of the vertical faded edges used to indicate that more
3694 * content in this view is visible.
3695 *
3696 * @return The size in pixels of the vertical faded edge or 0 if vertical
3697 * faded edges are not enabled for this view.
3698 * @attr ref android.R.styleable#View_fadingEdgeLength
3699 */
3700 public int getVerticalFadingEdgeLength() {
3701 if (isVerticalFadingEdgeEnabled()) {
3702 ScrollabilityCache cache = mScrollCache;
3703 if (cache != null) {
3704 return cache.fadingEdgeLength;
3705 }
3706 }
3707 return 0;
3708 }
3709
3710 /**
3711 * Set the size of the faded edge used to indicate that more content in this
3712 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003713 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3714 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3715 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003716 *
3717 * @param length The size in pixels of the faded edge used to indicate that more
3718 * content in this view is visible.
3719 */
3720 public void setFadingEdgeLength(int length) {
3721 initScrollCache();
3722 mScrollCache.fadingEdgeLength = length;
3723 }
3724
3725 /**
3726 * Returns the size of the horizontal faded edges used to indicate that more
3727 * content in this view is visible.
3728 *
3729 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3730 * faded edges are not enabled for this view.
3731 * @attr ref android.R.styleable#View_fadingEdgeLength
3732 */
3733 public int getHorizontalFadingEdgeLength() {
3734 if (isHorizontalFadingEdgeEnabled()) {
3735 ScrollabilityCache cache = mScrollCache;
3736 if (cache != null) {
3737 return cache.fadingEdgeLength;
3738 }
3739 }
3740 return 0;
3741 }
3742
3743 /**
3744 * Returns the width of the vertical scrollbar.
3745 *
3746 * @return The width in pixels of the vertical scrollbar or 0 if there
3747 * is no vertical scrollbar.
3748 */
3749 public int getVerticalScrollbarWidth() {
3750 ScrollabilityCache cache = mScrollCache;
3751 if (cache != null) {
3752 ScrollBarDrawable scrollBar = cache.scrollBar;
3753 if (scrollBar != null) {
3754 int size = scrollBar.getSize(true);
3755 if (size <= 0) {
3756 size = cache.scrollBarSize;
3757 }
3758 return size;
3759 }
3760 return 0;
3761 }
3762 return 0;
3763 }
3764
3765 /**
3766 * Returns the height of the horizontal scrollbar.
3767 *
3768 * @return The height in pixels of the horizontal scrollbar or 0 if
3769 * there is no horizontal scrollbar.
3770 */
3771 protected int getHorizontalScrollbarHeight() {
3772 ScrollabilityCache cache = mScrollCache;
3773 if (cache != null) {
3774 ScrollBarDrawable scrollBar = cache.scrollBar;
3775 if (scrollBar != null) {
3776 int size = scrollBar.getSize(false);
3777 if (size <= 0) {
3778 size = cache.scrollBarSize;
3779 }
3780 return size;
3781 }
3782 return 0;
3783 }
3784 return 0;
3785 }
3786
3787 /**
3788 * <p>
3789 * Initializes the scrollbars from a given set of styled attributes. This
3790 * method should be called by subclasses that need scrollbars and when an
3791 * instance of these subclasses is created programmatically rather than
3792 * being inflated from XML. This method is automatically called when the XML
3793 * is inflated.
3794 * </p>
3795 *
3796 * @param a the styled attributes set to initialize the scrollbars from
3797 */
3798 protected void initializeScrollbars(TypedArray a) {
3799 initScrollCache();
3800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003801 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003802
Mike Cleronf116bf82009-09-27 19:14:12 -07003803 if (scrollabilityCache.scrollBar == null) {
3804 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3805 }
Joe Malin32736f02011-01-19 16:14:20 -08003806
Romain Guy8bda2482010-03-02 11:42:11 -08003807 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003808
Mike Cleronf116bf82009-09-27 19:14:12 -07003809 if (!fadeScrollbars) {
3810 scrollabilityCache.state = ScrollabilityCache.ON;
3811 }
3812 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003813
3814
Mike Cleronf116bf82009-09-27 19:14:12 -07003815 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3816 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3817 .getScrollBarFadeDuration());
3818 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3819 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3820 ViewConfiguration.getScrollDefaultDelay());
3821
Joe Malin32736f02011-01-19 16:14:20 -08003822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003823 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3824 com.android.internal.R.styleable.View_scrollbarSize,
3825 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3826
3827 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3828 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3829
3830 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3831 if (thumb != null) {
3832 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3833 }
3834
3835 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3836 false);
3837 if (alwaysDraw) {
3838 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3839 }
3840
3841 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3842 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3843
3844 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3845 if (thumb != null) {
3846 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3847 }
3848
3849 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3850 false);
3851 if (alwaysDraw) {
3852 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3853 }
3854
Fabrice Di Megliob03b4342012-06-04 12:55:30 -07003855 // Apply layout direction to the new Drawables if needed
3856 final int layoutDirection = getResolvedLayoutDirection();
3857 if (track != null) {
3858 track.setLayoutDirection(layoutDirection);
3859 }
3860 if (thumb != null) {
3861 thumb.setLayoutDirection(layoutDirection);
3862 }
3863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003864 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003865 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003866 }
3867
3868 /**
3869 * <p>
3870 * Initalizes the scrollability cache if necessary.
3871 * </p>
3872 */
3873 private void initScrollCache() {
3874 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003875 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003876 }
3877 }
3878
Philip Milne6c8ea062012-04-03 17:38:43 -07003879 private ScrollabilityCache getScrollCache() {
3880 initScrollCache();
3881 return mScrollCache;
3882 }
3883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003884 /**
Adam Powell20232d02010-12-08 21:08:53 -08003885 * Set the position of the vertical scroll bar. Should be one of
3886 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3887 * {@link #SCROLLBAR_POSITION_RIGHT}.
3888 *
3889 * @param position Where the vertical scroll bar should be positioned.
3890 */
3891 public void setVerticalScrollbarPosition(int position) {
3892 if (mVerticalScrollbarPosition != position) {
3893 mVerticalScrollbarPosition = position;
3894 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003895 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003896 }
3897 }
3898
3899 /**
3900 * @return The position where the vertical scroll bar will show, if applicable.
3901 * @see #setVerticalScrollbarPosition(int)
3902 */
3903 public int getVerticalScrollbarPosition() {
3904 return mVerticalScrollbarPosition;
3905 }
3906
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003907 ListenerInfo getListenerInfo() {
3908 if (mListenerInfo != null) {
3909 return mListenerInfo;
3910 }
3911 mListenerInfo = new ListenerInfo();
3912 return mListenerInfo;
3913 }
3914
Adam Powell20232d02010-12-08 21:08:53 -08003915 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003916 * Register a callback to be invoked when focus of this view changed.
3917 *
3918 * @param l The callback that will run.
3919 */
3920 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003921 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003922 }
3923
3924 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003925 * Add a listener that will be called when the bounds of the view change due to
3926 * layout processing.
3927 *
3928 * @param listener The listener that will be called when layout bounds change.
3929 */
3930 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003931 ListenerInfo li = getListenerInfo();
3932 if (li.mOnLayoutChangeListeners == null) {
3933 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003934 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003935 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3936 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003937 }
Chet Haase21cd1382010-09-01 17:42:29 -07003938 }
3939
3940 /**
3941 * Remove a listener for layout changes.
3942 *
3943 * @param listener The listener for layout bounds change.
3944 */
3945 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003946 ListenerInfo li = mListenerInfo;
3947 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003948 return;
3949 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003950 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003951 }
3952
3953 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003954 * Add a listener for attach state changes.
3955 *
3956 * This listener will be called whenever this view is attached or detached
3957 * from a window. Remove the listener using
3958 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3959 *
3960 * @param listener Listener to attach
3961 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3962 */
3963 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003964 ListenerInfo li = getListenerInfo();
3965 if (li.mOnAttachStateChangeListeners == null) {
3966 li.mOnAttachStateChangeListeners
3967 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003968 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003969 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003970 }
3971
3972 /**
3973 * Remove a listener for attach state changes. The listener will receive no further
3974 * notification of window attach/detach events.
3975 *
3976 * @param listener Listener to remove
3977 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3978 */
3979 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003980 ListenerInfo li = mListenerInfo;
3981 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003982 return;
3983 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003984 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003985 }
3986
3987 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003988 * Returns the focus-change callback registered for this view.
3989 *
3990 * @return The callback, or null if one is not registered.
3991 */
3992 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003993 ListenerInfo li = mListenerInfo;
3994 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003995 }
3996
3997 /**
3998 * Register a callback to be invoked when this view is clicked. If this view is not
3999 * clickable, it becomes clickable.
4000 *
4001 * @param l The callback that will run
4002 *
4003 * @see #setClickable(boolean)
4004 */
4005 public void setOnClickListener(OnClickListener l) {
4006 if (!isClickable()) {
4007 setClickable(true);
4008 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004009 getListenerInfo().mOnClickListener = l;
4010 }
4011
4012 /**
4013 * Return whether this view has an attached OnClickListener. Returns
4014 * true if there is a listener, false if there is none.
4015 */
4016 public boolean hasOnClickListeners() {
4017 ListenerInfo li = mListenerInfo;
4018 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004019 }
4020
4021 /**
4022 * Register a callback to be invoked when this view is clicked and held. If this view is not
4023 * long clickable, it becomes long clickable.
4024 *
4025 * @param l The callback that will run
4026 *
4027 * @see #setLongClickable(boolean)
4028 */
4029 public void setOnLongClickListener(OnLongClickListener l) {
4030 if (!isLongClickable()) {
4031 setLongClickable(true);
4032 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004033 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004034 }
4035
4036 /**
4037 * Register a callback to be invoked when the context menu for this view is
4038 * being built. If this view is not long clickable, it becomes long clickable.
4039 *
4040 * @param l The callback that will run
4041 *
4042 */
4043 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
4044 if (!isLongClickable()) {
4045 setLongClickable(true);
4046 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004047 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004048 }
4049
4050 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004051 * Call this view's OnClickListener, if it is defined. Performs all normal
4052 * actions associated with clicking: reporting accessibility event, playing
4053 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004054 *
4055 * @return True there was an assigned OnClickListener that was called, false
4056 * otherwise is returned.
4057 */
4058 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07004059 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
4060
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004061 ListenerInfo li = mListenerInfo;
4062 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004063 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004064 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004065 return true;
4066 }
4067
4068 return false;
4069 }
4070
4071 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004072 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
4073 * this only calls the listener, and does not do any associated clicking
4074 * actions like reporting an accessibility event.
4075 *
4076 * @return True there was an assigned OnClickListener that was called, false
4077 * otherwise is returned.
4078 */
4079 public boolean callOnClick() {
4080 ListenerInfo li = mListenerInfo;
4081 if (li != null && li.mOnClickListener != null) {
4082 li.mOnClickListener.onClick(this);
4083 return true;
4084 }
4085 return false;
4086 }
4087
4088 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004089 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
4090 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004091 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004092 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004093 */
4094 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07004095 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
4096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004097 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004098 ListenerInfo li = mListenerInfo;
4099 if (li != null && li.mOnLongClickListener != null) {
4100 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004101 }
4102 if (!handled) {
4103 handled = showContextMenu();
4104 }
4105 if (handled) {
4106 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
4107 }
4108 return handled;
4109 }
4110
4111 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004112 * Performs button-related actions during a touch down event.
4113 *
4114 * @param event The event.
4115 * @return True if the down was consumed.
4116 *
4117 * @hide
4118 */
4119 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
4120 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
4121 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
4122 return true;
4123 }
4124 }
4125 return false;
4126 }
4127
4128 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004129 * Bring up the context menu for this view.
4130 *
4131 * @return Whether a context menu was displayed.
4132 */
4133 public boolean showContextMenu() {
4134 return getParent().showContextMenuForChild(this);
4135 }
4136
4137 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004138 * Bring up the context menu for this view, referring to the item under the specified point.
4139 *
4140 * @param x The referenced x coordinate.
4141 * @param y The referenced y coordinate.
4142 * @param metaState The keyboard modifiers that were pressed.
4143 * @return Whether a context menu was displayed.
4144 *
4145 * @hide
4146 */
4147 public boolean showContextMenu(float x, float y, int metaState) {
4148 return showContextMenu();
4149 }
4150
4151 /**
Adam Powell6e346362010-07-23 10:18:23 -07004152 * Start an action mode.
4153 *
4154 * @param callback Callback that will control the lifecycle of the action mode
4155 * @return The new action mode if it is started, null otherwise
4156 *
4157 * @see ActionMode
4158 */
4159 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08004160 ViewParent parent = getParent();
4161 if (parent == null) return null;
4162 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07004163 }
4164
4165 /**
Jean Chalard405bc512012-05-29 19:12:34 +09004166 * Register a callback to be invoked when a hardware key is pressed in this view.
4167 * Key presses in software input methods will generally not trigger the methods of
4168 * this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004169 * @param l the key listener to attach to this view
4170 */
4171 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004172 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004173 }
4174
4175 /**
4176 * Register a callback to be invoked when a touch event is sent to this view.
4177 * @param l the touch listener to attach to this view
4178 */
4179 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004180 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004181 }
4182
4183 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004184 * Register a callback to be invoked when a generic motion event is sent to this view.
4185 * @param l the generic motion listener to attach to this view
4186 */
4187 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004188 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08004189 }
4190
4191 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07004192 * Register a callback to be invoked when a hover event is sent to this view.
4193 * @param l the hover listener to attach to this view
4194 */
4195 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004196 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07004197 }
4198
4199 /**
Joe Malin32736f02011-01-19 16:14:20 -08004200 * Register a drag event listener callback object for this View. The parameter is
4201 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
4202 * View, the system calls the
4203 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
4204 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07004205 */
4206 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004207 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07004208 }
4209
4210 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07004211 * Give this view focus. This will cause
4212 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004213 *
4214 * Note: this does not check whether this {@link View} should get focus, it just
4215 * gives it focus no matter what. It should only be called internally by framework
4216 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
4217 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004218 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
4219 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004220 * focus moved when requestFocus() is called. It may not always
4221 * apply, in which case use the default View.FOCUS_DOWN.
4222 * @param previouslyFocusedRect The rectangle of the view that had focus
4223 * prior in this View's coordinate system.
4224 */
4225 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
4226 if (DBG) {
4227 System.out.println(this + " requestFocus()");
4228 }
4229
Dianne Hackborn4702a852012-08-17 15:18:29 -07004230 if ((mPrivateFlags & PFLAG_FOCUSED) == 0) {
4231 mPrivateFlags |= PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004232
4233 if (mParent != null) {
4234 mParent.requestChildFocus(this, this);
4235 }
4236
4237 onFocusChanged(true, direction, previouslyFocusedRect);
4238 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004239
4240 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4241 notifyAccessibilityStateChanged();
4242 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004243 }
4244 }
4245
4246 /**
4247 * Request that a rectangle of this view be visible on the screen,
4248 * scrolling if necessary just enough.
4249 *
4250 * <p>A View should call this if it maintains some notion of which part
4251 * of its content is interesting. For example, a text editing view
4252 * should call this when its cursor moves.
4253 *
4254 * @param rectangle The rectangle.
4255 * @return Whether any parent scrolled.
4256 */
4257 public boolean requestRectangleOnScreen(Rect rectangle) {
4258 return requestRectangleOnScreen(rectangle, false);
4259 }
4260
4261 /**
4262 * Request that a rectangle of this view be visible on the screen,
4263 * scrolling if necessary just enough.
4264 *
4265 * <p>A View should call this if it maintains some notion of which part
4266 * of its content is interesting. For example, a text editing view
4267 * should call this when its cursor moves.
4268 *
4269 * <p>When <code>immediate</code> is set to true, scrolling will not be
4270 * animated.
4271 *
4272 * @param rectangle The rectangle.
4273 * @param immediate True to forbid animated scrolling, false otherwise
4274 * @return Whether any parent scrolled.
4275 */
4276 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
4277 View child = this;
4278 ViewParent parent = mParent;
4279 boolean scrolled = false;
4280 while (parent != null) {
4281 scrolled |= parent.requestChildRectangleOnScreen(child,
4282 rectangle, immediate);
4283
4284 // offset rect so next call has the rectangle in the
4285 // coordinate system of its direct child.
4286 rectangle.offset(child.getLeft(), child.getTop());
4287 rectangle.offset(-child.getScrollX(), -child.getScrollY());
4288
4289 if (!(parent instanceof View)) {
4290 break;
4291 }
Romain Guy8506ab42009-06-11 17:35:47 -07004292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004293 child = (View) parent;
4294 parent = child.getParent();
4295 }
4296 return scrolled;
4297 }
4298
4299 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004300 * Called when this view wants to give up focus. If focus is cleared
4301 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
4302 * <p>
4303 * <strong>Note:</strong> When a View clears focus the framework is trying
4304 * to give focus to the first focusable View from the top. Hence, if this
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004305 * View is the first from the top that can take focus, then all callbacks
4306 * related to clearing focus will be invoked after wich the framework will
4307 * give focus to this view.
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004308 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004309 */
4310 public void clearFocus() {
4311 if (DBG) {
4312 System.out.println(this + " clearFocus()");
4313 }
4314
Dianne Hackborn4702a852012-08-17 15:18:29 -07004315 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
4316 mPrivateFlags &= ~PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004317
4318 if (mParent != null) {
4319 mParent.clearChildFocus(this);
4320 }
4321
4322 onFocusChanged(false, 0, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004324 refreshDrawableState();
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004325
4326 ensureInputFocusOnFirstFocusable();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004327
4328 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4329 notifyAccessibilityStateChanged();
4330 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004331 }
4332 }
4333
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004334 void ensureInputFocusOnFirstFocusable() {
4335 View root = getRootView();
4336 if (root != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004337 root.requestFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004338 }
4339 }
4340
4341 /**
4342 * Called internally by the view system when a new view is getting focus.
4343 * This is what clears the old focus.
4344 */
4345 void unFocus() {
4346 if (DBG) {
4347 System.out.println(this + " unFocus()");
4348 }
4349
Dianne Hackborn4702a852012-08-17 15:18:29 -07004350 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
4351 mPrivateFlags &= ~PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004352
4353 onFocusChanged(false, 0, null);
4354 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004355
4356 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4357 notifyAccessibilityStateChanged();
4358 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004359 }
4360 }
4361
4362 /**
4363 * Returns true if this view has focus iteself, or is the ancestor of the
4364 * view that has focus.
4365 *
4366 * @return True if this view has or contains focus, false otherwise.
4367 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004368 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004369 public boolean hasFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07004370 return (mPrivateFlags & PFLAG_FOCUSED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004371 }
4372
4373 /**
4374 * Returns true if this view is focusable or if it contains a reachable View
4375 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
4376 * is a View whose parents do not block descendants focus.
4377 *
4378 * Only {@link #VISIBLE} views are considered focusable.
4379 *
4380 * @return True if the view is focusable or if the view contains a focusable
4381 * View, false otherwise.
4382 *
4383 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
4384 */
4385 public boolean hasFocusable() {
4386 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4387 }
4388
4389 /**
4390 * Called by the view system when the focus state of this view changes.
4391 * When the focus change event is caused by directional navigation, direction
4392 * and previouslyFocusedRect provide insight into where the focus is coming from.
4393 * When overriding, be sure to call up through to the super class so that
4394 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004395 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004396 * @param gainFocus True if the View has focus; false otherwise.
4397 * @param direction The direction focus has moved when requestFocus()
4398 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004399 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4400 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4401 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004402 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4403 * system, of the previously focused view. If applicable, this will be
4404 * passed in as finer grained information about where the focus is coming
4405 * from (in addition to direction). Will be <code>null</code> otherwise.
4406 */
4407 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004408 if (gainFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004409 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4410 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004411 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004412 }
4413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004414 InputMethodManager imm = InputMethodManager.peekInstance();
4415 if (!gainFocus) {
4416 if (isPressed()) {
4417 setPressed(false);
4418 }
4419 if (imm != null && mAttachInfo != null
4420 && mAttachInfo.mHasWindowFocus) {
4421 imm.focusOut(this);
4422 }
Romain Guya2431d02009-04-30 16:30:00 -07004423 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004424 } else if (imm != null && mAttachInfo != null
4425 && mAttachInfo.mHasWindowFocus) {
4426 imm.focusIn(this);
4427 }
Romain Guy8506ab42009-06-11 17:35:47 -07004428
Romain Guy0fd89bf2011-01-26 15:41:30 -08004429 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004430 ListenerInfo li = mListenerInfo;
4431 if (li != null && li.mOnFocusChangeListener != null) {
4432 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004433 }
Joe Malin32736f02011-01-19 16:14:20 -08004434
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004435 if (mAttachInfo != null) {
4436 mAttachInfo.mKeyDispatchState.reset(this);
4437 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004438 }
4439
4440 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004441 * Sends an accessibility event of the given type. If accessiiblity is
4442 * not enabled this method has no effect. The default implementation calls
4443 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4444 * to populate information about the event source (this View), then calls
4445 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4446 * populate the text content of the event source including its descendants,
4447 * and last calls
4448 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4449 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004450 * <p>
4451 * If an {@link AccessibilityDelegate} has been specified via calling
4452 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4453 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4454 * responsible for handling this call.
4455 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004456 *
Scott Mainb303d832011-10-12 16:45:18 -07004457 * @param eventType The type of the event to send, as defined by several types from
4458 * {@link android.view.accessibility.AccessibilityEvent}, such as
4459 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4460 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004461 *
4462 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4463 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4464 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004465 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004466 */
4467 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004468 if (mAccessibilityDelegate != null) {
4469 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4470 } else {
4471 sendAccessibilityEventInternal(eventType);
4472 }
4473 }
4474
4475 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004476 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4477 * {@link AccessibilityEvent} to make an announcement which is related to some
4478 * sort of a context change for which none of the events representing UI transitions
4479 * is a good fit. For example, announcing a new page in a book. If accessibility
4480 * is not enabled this method does nothing.
4481 *
4482 * @param text The announcement text.
4483 */
4484 public void announceForAccessibility(CharSequence text) {
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004485 if (AccessibilityManager.getInstance(mContext).isEnabled() && mParent != null) {
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004486 AccessibilityEvent event = AccessibilityEvent.obtain(
4487 AccessibilityEvent.TYPE_ANNOUNCEMENT);
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004488 onInitializeAccessibilityEvent(event);
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004489 event.getText().add(text);
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004490 event.setContentDescription(null);
4491 mParent.requestSendAccessibilityEvent(this, event);
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004492 }
4493 }
4494
4495 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004496 * @see #sendAccessibilityEvent(int)
4497 *
4498 * Note: Called from the default {@link AccessibilityDelegate}.
4499 */
4500 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004501 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4502 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4503 }
4504 }
4505
4506 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004507 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4508 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004509 * perform a check whether accessibility is enabled.
4510 * <p>
4511 * If an {@link AccessibilityDelegate} has been specified via calling
4512 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4513 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4514 * is responsible for handling this call.
4515 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004516 *
4517 * @param event The event to send.
4518 *
4519 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004520 */
4521 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004522 if (mAccessibilityDelegate != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004523 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004524 } else {
4525 sendAccessibilityEventUncheckedInternal(event);
4526 }
4527 }
4528
4529 /**
4530 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4531 *
4532 * Note: Called from the default {@link AccessibilityDelegate}.
4533 */
4534 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004535 if (!isShown()) {
4536 return;
4537 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004538 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004539 // Only a subset of accessibility events populates text content.
4540 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4541 dispatchPopulateAccessibilityEvent(event);
4542 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004543 // In the beginning we called #isShown(), so we know that getParent() is not null.
4544 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004545 }
4546
4547 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004548 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4549 * to its children for adding their text content to the event. Note that the
4550 * event text is populated in a separate dispatch path since we add to the
4551 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004552 * A typical implementation will call
4553 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4554 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4555 * on each child. Override this method if custom population of the event text
4556 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004557 * <p>
4558 * If an {@link AccessibilityDelegate} has been specified via calling
4559 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4560 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4561 * is responsible for handling this call.
4562 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004563 * <p>
4564 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4565 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4566 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004567 *
4568 * @param event The event.
4569 *
4570 * @return True if the event population was completed.
4571 */
4572 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004573 if (mAccessibilityDelegate != null) {
4574 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4575 } else {
4576 return dispatchPopulateAccessibilityEventInternal(event);
4577 }
4578 }
4579
4580 /**
4581 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4582 *
4583 * Note: Called from the default {@link AccessibilityDelegate}.
4584 */
4585 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004586 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004587 return false;
4588 }
4589
4590 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004591 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004592 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004593 * text content. While this method is free to modify event
4594 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004595 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4596 * <p>
4597 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004598 * to the text added by the super implementation:
4599 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004600 * super.onPopulateAccessibilityEvent(event);
4601 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4602 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4603 * mCurrentDate.getTimeInMillis(), flags);
4604 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004605 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004606 * <p>
4607 * If an {@link AccessibilityDelegate} has been specified via calling
4608 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4609 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4610 * is responsible for handling this call.
4611 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004612 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4613 * information to the event, in case the default implementation has basic information to add.
4614 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004615 *
4616 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004617 *
4618 * @see #sendAccessibilityEvent(int)
4619 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004620 */
4621 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004622 if (mAccessibilityDelegate != null) {
4623 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4624 } else {
4625 onPopulateAccessibilityEventInternal(event);
4626 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004627 }
4628
4629 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004630 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4631 *
4632 * Note: Called from the default {@link AccessibilityDelegate}.
4633 */
4634 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4635
4636 }
4637
4638 /**
4639 * Initializes an {@link AccessibilityEvent} with information about
4640 * this View which is the event source. In other words, the source of
4641 * an accessibility event is the view whose state change triggered firing
4642 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004643 * <p>
4644 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004645 * to properties set by the super implementation:
4646 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4647 * super.onInitializeAccessibilityEvent(event);
4648 * event.setPassword(true);
4649 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004650 * <p>
4651 * If an {@link AccessibilityDelegate} has been specified via calling
4652 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4653 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4654 * is responsible for handling this call.
4655 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004656 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4657 * information to the event, in case the default implementation has basic information to add.
4658 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004659 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004660 *
4661 * @see #sendAccessibilityEvent(int)
4662 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4663 */
4664 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004665 if (mAccessibilityDelegate != null) {
4666 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4667 } else {
4668 onInitializeAccessibilityEventInternal(event);
4669 }
4670 }
4671
4672 /**
4673 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4674 *
4675 * Note: Called from the default {@link AccessibilityDelegate}.
4676 */
4677 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004678 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004679 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004680 event.setPackageName(getContext().getPackageName());
4681 event.setEnabled(isEnabled());
4682 event.setContentDescription(mContentDescription);
4683
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004684 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004685 ArrayList<View> focusablesTempList = mAttachInfo.mTempArrayList;
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004686 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4687 FOCUSABLES_ALL);
4688 event.setItemCount(focusablesTempList.size());
4689 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4690 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004691 }
4692 }
4693
4694 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004695 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4696 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4697 * This method is responsible for obtaining an accessibility node info from a
4698 * pool of reusable instances and calling
4699 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4700 * initialize the former.
4701 * <p>
4702 * Note: The client is responsible for recycling the obtained instance by calling
4703 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4704 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004705 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004706 * @return A populated {@link AccessibilityNodeInfo}.
4707 *
4708 * @see AccessibilityNodeInfo
4709 */
4710 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004711 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4712 if (provider != null) {
4713 return provider.createAccessibilityNodeInfo(View.NO_ID);
4714 } else {
4715 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4716 onInitializeAccessibilityNodeInfo(info);
4717 return info;
4718 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004719 }
4720
4721 /**
4722 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4723 * The base implementation sets:
4724 * <ul>
4725 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004726 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4727 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004728 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4729 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4730 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4731 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4732 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4733 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4734 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4735 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4736 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4737 * </ul>
4738 * <p>
4739 * Subclasses should override this method, call the super implementation,
4740 * and set additional attributes.
4741 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004742 * <p>
4743 * If an {@link AccessibilityDelegate} has been specified via calling
4744 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4745 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4746 * is responsible for handling this call.
4747 * </p>
4748 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004749 * @param info The instance to initialize.
4750 */
4751 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004752 if (mAccessibilityDelegate != null) {
4753 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4754 } else {
4755 onInitializeAccessibilityNodeInfoInternal(info);
4756 }
4757 }
4758
4759 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004760 * Gets the location of this view in screen coordintates.
4761 *
4762 * @param outRect The output location
4763 */
4764 private void getBoundsOnScreen(Rect outRect) {
4765 if (mAttachInfo == null) {
4766 return;
4767 }
4768
4769 RectF position = mAttachInfo.mTmpTransformRect;
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004770 position.set(0, 0, mRight - mLeft, mBottom - mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004771
4772 if (!hasIdentityMatrix()) {
4773 getMatrix().mapRect(position);
4774 }
4775
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004776 position.offset(mLeft, mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004777
4778 ViewParent parent = mParent;
4779 while (parent instanceof View) {
4780 View parentView = (View) parent;
4781
4782 position.offset(-parentView.mScrollX, -parentView.mScrollY);
4783
4784 if (!parentView.hasIdentityMatrix()) {
4785 parentView.getMatrix().mapRect(position);
4786 }
4787
4788 position.offset(parentView.mLeft, parentView.mTop);
4789
4790 parent = parentView.mParent;
4791 }
4792
4793 if (parent instanceof ViewRootImpl) {
4794 ViewRootImpl viewRootImpl = (ViewRootImpl) parent;
4795 position.offset(0, -viewRootImpl.mCurScrollY);
4796 }
4797
4798 position.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
4799
4800 outRect.set((int) (position.left + 0.5f), (int) (position.top + 0.5f),
4801 (int) (position.right + 0.5f), (int) (position.bottom + 0.5f));
4802 }
4803
4804 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004805 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4806 *
4807 * Note: Called from the default {@link AccessibilityDelegate}.
4808 */
4809 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004810 Rect bounds = mAttachInfo.mTmpInvalRect;
Svetoslav Ganov983119a2012-07-03 21:04:10 -07004811
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004812 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004813 info.setBoundsInParent(bounds);
4814
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004815 getBoundsOnScreen(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004816 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004817
Svetoslav Ganovc406be92012-05-11 16:12:32 -07004818 ViewParent parent = getParentForAccessibility();
4819 if (parent instanceof View) {
4820 info.setParent((View) parent);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004821 }
4822
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004823 info.setVisibleToUser(isVisibleToUser());
4824
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004825 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004826 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004827 info.setContentDescription(getContentDescription());
4828
4829 info.setEnabled(isEnabled());
4830 info.setClickable(isClickable());
4831 info.setFocusable(isFocusable());
4832 info.setFocused(isFocused());
Svetoslav Ganov42138042012-03-20 11:51:39 -07004833 info.setAccessibilityFocused(isAccessibilityFocused());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004834 info.setSelected(isSelected());
4835 info.setLongClickable(isLongClickable());
4836
4837 // TODO: These make sense only if we are in an AdapterView but all
4838 // views can be selected. Maybe from accessiiblity perspective
4839 // we should report as selectable view in an AdapterView.
4840 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4841 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4842
4843 if (isFocusable()) {
4844 if (isFocused()) {
4845 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4846 } else {
4847 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4848 }
4849 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004850
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07004851 if (!isAccessibilityFocused()) {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07004852 info.addAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07004853 } else {
4854 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
4855 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004856
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07004857 if (isClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004858 info.addAction(AccessibilityNodeInfo.ACTION_CLICK);
4859 }
4860
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07004861 if (isLongClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004862 info.addAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
4863 }
4864
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004865 if (mContentDescription != null && mContentDescription.length() > 0) {
Svetoslav Ganov2b435aa2012-05-04 17:16:37 -07004866 info.addAction(AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY);
4867 info.addAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY);
4868 info.setMovementGranularities(AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004869 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD
4870 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH);
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004871 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004872 }
4873
4874 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004875 * Computes whether this view is visible to the user. Such a view is
4876 * attached, visible, all its predecessors are visible, it is not clipped
4877 * entirely by its predecessors, and has an alpha greater than zero.
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004878 *
4879 * @return Whether the view is visible on the screen.
Guang Zhu0d607fb2012-05-11 19:34:56 -07004880 *
4881 * @hide
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004882 */
Guang Zhu0d607fb2012-05-11 19:34:56 -07004883 protected boolean isVisibleToUser() {
4884 return isVisibleToUser(null);
4885 }
4886
4887 /**
Romain Guyf0af1d52012-07-11 18:31:21 -07004888 * Computes whether the given portion of this view is visible to the user.
4889 * Such a view is attached, visible, all its predecessors are visible,
4890 * has an alpha greater than zero, and the specified portion is not
4891 * clipped entirely by its predecessors.
Guang Zhu0d607fb2012-05-11 19:34:56 -07004892 *
4893 * @param boundInView the portion of the view to test; coordinates should be relative; may be
4894 * <code>null</code>, and the entire view will be tested in this case.
4895 * When <code>true</code> is returned by the function, the actual visible
4896 * region will be stored in this parameter; that is, if boundInView is fully
4897 * contained within the view, no modification will be made, otherwise regions
4898 * outside of the visible area of the view will be clipped.
4899 *
4900 * @return Whether the specified portion of the view is visible on the screen.
4901 *
4902 * @hide
4903 */
4904 protected boolean isVisibleToUser(Rect boundInView) {
Romain Guyf0af1d52012-07-11 18:31:21 -07004905 if (mAttachInfo != null) {
4906 Rect visibleRect = mAttachInfo.mTmpInvalRect;
4907 Point offset = mAttachInfo.mPoint;
4908 // The first two checks are made also made by isShown() which
4909 // however traverses the tree up to the parent to catch that.
4910 // Therefore, we do some fail fast check to minimize the up
4911 // tree traversal.
4912 boolean isVisible = mAttachInfo.mWindowVisibility == View.VISIBLE
4913 && getAlpha() > 0
4914 && isShown()
4915 && getGlobalVisibleRect(visibleRect, offset);
Guang Zhu0d607fb2012-05-11 19:34:56 -07004916 if (isVisible && boundInView != null) {
4917 visibleRect.offset(-offset.x, -offset.y);
Romain Guyf0af1d52012-07-11 18:31:21 -07004918 // isVisible is always true here, use a simple assignment
4919 isVisible = boundInView.intersect(visibleRect);
Guang Zhu0d607fb2012-05-11 19:34:56 -07004920 }
4921 return isVisible;
Romain Guyf0af1d52012-07-11 18:31:21 -07004922 }
4923
4924 return false;
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004925 }
4926
4927 /**
alanvb72fe7a2012-08-27 16:44:25 -07004928 * Returns the delegate for implementing accessibility support via
4929 * composition. For more details see {@link AccessibilityDelegate}.
4930 *
4931 * @return The delegate, or null if none set.
4932 *
4933 * @hide
4934 */
4935 public AccessibilityDelegate getAccessibilityDelegate() {
4936 return mAccessibilityDelegate;
4937 }
4938
4939 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004940 * Sets a delegate for implementing accessibility support via compositon as
4941 * opposed to inheritance. The delegate's primary use is for implementing
4942 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4943 *
4944 * @param delegate The delegate instance.
4945 *
4946 * @see AccessibilityDelegate
4947 */
4948 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4949 mAccessibilityDelegate = delegate;
4950 }
4951
4952 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004953 * Gets the provider for managing a virtual view hierarchy rooted at this View
4954 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4955 * that explore the window content.
4956 * <p>
4957 * If this method returns an instance, this instance is responsible for managing
4958 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4959 * View including the one representing the View itself. Similarly the returned
4960 * instance is responsible for performing accessibility actions on any virtual
4961 * view or the root view itself.
4962 * </p>
4963 * <p>
4964 * If an {@link AccessibilityDelegate} has been specified via calling
4965 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4966 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4967 * is responsible for handling this call.
4968 * </p>
4969 *
4970 * @return The provider.
4971 *
4972 * @see AccessibilityNodeProvider
4973 */
4974 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4975 if (mAccessibilityDelegate != null) {
4976 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4977 } else {
4978 return null;
4979 }
4980 }
4981
4982 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004983 * Gets the unique identifier of this view on the screen for accessibility purposes.
4984 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4985 *
4986 * @return The view accessibility id.
4987 *
4988 * @hide
4989 */
4990 public int getAccessibilityViewId() {
4991 if (mAccessibilityViewId == NO_ID) {
4992 mAccessibilityViewId = sNextAccessibilityViewId++;
4993 }
4994 return mAccessibilityViewId;
4995 }
4996
4997 /**
4998 * Gets the unique identifier of the window in which this View reseides.
4999 *
5000 * @return The window accessibility id.
5001 *
5002 * @hide
5003 */
5004 public int getAccessibilityWindowId() {
5005 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
5006 }
5007
5008 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07005009 * Gets the {@link View} description. It briefly describes the view and is
5010 * primarily used for accessibility support. Set this property to enable
5011 * better accessibility support for your application. This is especially
5012 * true for views that do not have textual representation (For example,
5013 * ImageButton).
5014 *
Svetoslav Ganov42138042012-03-20 11:51:39 -07005015 * @return The content description.
svetoslavganov75986cf2009-05-14 22:28:01 -07005016 *
5017 * @attr ref android.R.styleable#View_contentDescription
5018 */
Svetoslav Ganov42138042012-03-20 11:51:39 -07005019 @ViewDebug.ExportedProperty(category = "accessibility")
svetoslavganov75986cf2009-05-14 22:28:01 -07005020 public CharSequence getContentDescription() {
5021 return mContentDescription;
5022 }
5023
5024 /**
5025 * Sets the {@link View} description. It briefly describes the view and is
5026 * primarily used for accessibility support. Set this property to enable
5027 * better accessibility support for your application. This is especially
5028 * true for views that do not have textual representation (For example,
5029 * ImageButton).
5030 *
5031 * @param contentDescription The content description.
5032 *
5033 * @attr ref android.R.styleable#View_contentDescription
5034 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07005035 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07005036 public void setContentDescription(CharSequence contentDescription) {
5037 mContentDescription = contentDescription;
Svetoslav Ganove47957a2012-06-05 14:46:50 -07005038 final boolean nonEmptyDesc = contentDescription != null && contentDescription.length() > 0;
5039 if (nonEmptyDesc && getImportantForAccessibility() == IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
5040 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
5041 }
svetoslavganov75986cf2009-05-14 22:28:01 -07005042 }
5043
5044 /**
Romain Guya2431d02009-04-30 16:30:00 -07005045 * Invoked whenever this view loses focus, either by losing window focus or by losing
5046 * focus within its window. This method can be used to clear any state tied to the
5047 * focus. For instance, if a button is held pressed with the trackball and the window
5048 * loses focus, this method can be used to cancel the press.
5049 *
5050 * Subclasses of View overriding this method should always call super.onFocusLost().
5051 *
5052 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07005053 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07005054 *
5055 * @hide pending API council approval
5056 */
5057 protected void onFocusLost() {
5058 resetPressedState();
5059 }
5060
5061 private void resetPressedState() {
5062 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5063 return;
5064 }
5065
5066 if (isPressed()) {
5067 setPressed(false);
5068
5069 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05005070 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005071 }
5072 }
5073 }
5074
5075 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005076 * Returns true if this view has focus
5077 *
5078 * @return True if this view has focus, false otherwise.
5079 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005080 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005081 public boolean isFocused() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005082 return (mPrivateFlags & PFLAG_FOCUSED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005083 }
5084
5085 /**
5086 * Find the view in the hierarchy rooted at this view that currently has
5087 * focus.
5088 *
5089 * @return The view that currently has focus, or null if no focused view can
5090 * be found.
5091 */
5092 public View findFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005093 return (mPrivateFlags & PFLAG_FOCUSED) != 0 ? this : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005094 }
5095
5096 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07005097 * Indicates whether this view is one of the set of scrollable containers in
5098 * its window.
5099 *
5100 * @return whether this view is one of the set of scrollable containers in
5101 * its window
5102 *
5103 * @attr ref android.R.styleable#View_isScrollContainer
5104 */
5105 public boolean isScrollContainer() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005106 return (mPrivateFlags & PFLAG_SCROLL_CONTAINER_ADDED) != 0;
Philip Milne6c8ea062012-04-03 17:38:43 -07005107 }
5108
5109 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005110 * Change whether this view is one of the set of scrollable containers in
5111 * its window. This will be used to determine whether the window can
5112 * resize or must pan when a soft input area is open -- scrollable
5113 * containers allow the window to use resize mode since the container
5114 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07005115 *
5116 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005117 */
5118 public void setScrollContainer(boolean isScrollContainer) {
5119 if (isScrollContainer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005120 if (mAttachInfo != null && (mPrivateFlags&PFLAG_SCROLL_CONTAINER_ADDED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005121 mAttachInfo.mScrollContainers.add(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -07005122 mPrivateFlags |= PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005123 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005124 mPrivateFlags |= PFLAG_SCROLL_CONTAINER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005125 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005126 if ((mPrivateFlags&PFLAG_SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005127 mAttachInfo.mScrollContainers.remove(this);
5128 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005129 mPrivateFlags &= ~(PFLAG_SCROLL_CONTAINER|PFLAG_SCROLL_CONTAINER_ADDED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005130 }
5131 }
5132
5133 /**
5134 * Returns the quality of the drawing cache.
5135 *
5136 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5137 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5138 *
5139 * @see #setDrawingCacheQuality(int)
5140 * @see #setDrawingCacheEnabled(boolean)
5141 * @see #isDrawingCacheEnabled()
5142 *
5143 * @attr ref android.R.styleable#View_drawingCacheQuality
5144 */
5145 public int getDrawingCacheQuality() {
5146 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
5147 }
5148
5149 /**
5150 * Set the drawing cache quality of this view. This value is used only when the
5151 * drawing cache is enabled
5152 *
5153 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5154 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5155 *
5156 * @see #getDrawingCacheQuality()
5157 * @see #setDrawingCacheEnabled(boolean)
5158 * @see #isDrawingCacheEnabled()
5159 *
5160 * @attr ref android.R.styleable#View_drawingCacheQuality
5161 */
5162 public void setDrawingCacheQuality(int quality) {
5163 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
5164 }
5165
5166 /**
5167 * Returns whether the screen should remain on, corresponding to the current
5168 * value of {@link #KEEP_SCREEN_ON}.
5169 *
5170 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
5171 *
5172 * @see #setKeepScreenOn(boolean)
5173 *
5174 * @attr ref android.R.styleable#View_keepScreenOn
5175 */
5176 public boolean getKeepScreenOn() {
5177 return (mViewFlags & KEEP_SCREEN_ON) != 0;
5178 }
5179
5180 /**
5181 * Controls whether the screen should remain on, modifying the
5182 * value of {@link #KEEP_SCREEN_ON}.
5183 *
5184 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
5185 *
5186 * @see #getKeepScreenOn()
5187 *
5188 * @attr ref android.R.styleable#View_keepScreenOn
5189 */
5190 public void setKeepScreenOn(boolean keepScreenOn) {
5191 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
5192 }
5193
5194 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005195 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5196 * @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 -08005197 *
5198 * @attr ref android.R.styleable#View_nextFocusLeft
5199 */
5200 public int getNextFocusLeftId() {
5201 return mNextFocusLeftId;
5202 }
5203
5204 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005205 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5206 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
5207 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005208 *
5209 * @attr ref android.R.styleable#View_nextFocusLeft
5210 */
5211 public void setNextFocusLeftId(int nextFocusLeftId) {
5212 mNextFocusLeftId = nextFocusLeftId;
5213 }
5214
5215 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005216 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5217 * @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 -08005218 *
5219 * @attr ref android.R.styleable#View_nextFocusRight
5220 */
5221 public int getNextFocusRightId() {
5222 return mNextFocusRightId;
5223 }
5224
5225 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005226 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5227 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
5228 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005229 *
5230 * @attr ref android.R.styleable#View_nextFocusRight
5231 */
5232 public void setNextFocusRightId(int nextFocusRightId) {
5233 mNextFocusRightId = nextFocusRightId;
5234 }
5235
5236 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005237 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5238 * @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 -08005239 *
5240 * @attr ref android.R.styleable#View_nextFocusUp
5241 */
5242 public int getNextFocusUpId() {
5243 return mNextFocusUpId;
5244 }
5245
5246 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005247 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5248 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
5249 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005250 *
5251 * @attr ref android.R.styleable#View_nextFocusUp
5252 */
5253 public void setNextFocusUpId(int nextFocusUpId) {
5254 mNextFocusUpId = nextFocusUpId;
5255 }
5256
5257 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005258 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5259 * @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 -08005260 *
5261 * @attr ref android.R.styleable#View_nextFocusDown
5262 */
5263 public int getNextFocusDownId() {
5264 return mNextFocusDownId;
5265 }
5266
5267 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005268 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5269 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
5270 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005271 *
5272 * @attr ref android.R.styleable#View_nextFocusDown
5273 */
5274 public void setNextFocusDownId(int nextFocusDownId) {
5275 mNextFocusDownId = nextFocusDownId;
5276 }
5277
5278 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005279 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5280 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
5281 *
5282 * @attr ref android.R.styleable#View_nextFocusForward
5283 */
5284 public int getNextFocusForwardId() {
5285 return mNextFocusForwardId;
5286 }
5287
5288 /**
5289 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5290 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
5291 * decide automatically.
5292 *
5293 * @attr ref android.R.styleable#View_nextFocusForward
5294 */
5295 public void setNextFocusForwardId(int nextFocusForwardId) {
5296 mNextFocusForwardId = nextFocusForwardId;
5297 }
5298
5299 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005300 * Returns the visibility of this view and all of its ancestors
5301 *
5302 * @return True if this view and all of its ancestors are {@link #VISIBLE}
5303 */
5304 public boolean isShown() {
5305 View current = this;
5306 //noinspection ConstantConditions
5307 do {
5308 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5309 return false;
5310 }
5311 ViewParent parent = current.mParent;
5312 if (parent == null) {
5313 return false; // We are not attached to the view root
5314 }
5315 if (!(parent instanceof View)) {
5316 return true;
5317 }
5318 current = (View) parent;
5319 } while (current != null);
5320
5321 return false;
5322 }
5323
5324 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005325 * Called by the view hierarchy when the content insets for a window have
5326 * changed, to allow it to adjust its content to fit within those windows.
5327 * The content insets tell you the space that the status bar, input method,
5328 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005329 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005330 * <p>You do not normally need to deal with this function, since the default
5331 * window decoration given to applications takes care of applying it to the
5332 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
5333 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
5334 * and your content can be placed under those system elements. You can then
5335 * use this method within your view hierarchy if you have parts of your UI
5336 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005337 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005338 * <p>The default implementation of this method simply applies the content
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005339 * inset's to the view's padding, consuming that content (modifying the
5340 * insets to be 0), and returning true. This behavior is off by default, but can
5341 * be enabled through {@link #setFitsSystemWindows(boolean)}.
5342 *
5343 * <p>This function's traversal down the hierarchy is depth-first. The same content
5344 * insets object is propagated down the hierarchy, so any changes made to it will
5345 * be seen by all following views (including potentially ones above in
5346 * the hierarchy since this is a depth-first traversal). The first view
5347 * that returns true will abort the entire traversal.
5348 *
5349 * <p>The default implementation works well for a situation where it is
5350 * used with a container that covers the entire window, allowing it to
5351 * apply the appropriate insets to its content on all edges. If you need
5352 * a more complicated layout (such as two different views fitting system
5353 * windows, one on the top of the window, and one on the bottom),
5354 * you can override the method and handle the insets however you would like.
5355 * Note that the insets provided by the framework are always relative to the
5356 * far edges of the window, not accounting for the location of the called view
5357 * within that window. (In fact when this method is called you do not yet know
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005358 * where the layout will place the view, as it is done before layout happens.)
5359 *
5360 * <p>Note: unlike many View methods, there is no dispatch phase to this
5361 * call. If you are overriding it in a ViewGroup and want to allow the
5362 * call to continue to your children, you must be sure to call the super
5363 * implementation.
5364 *
Dianne Hackborncf675782012-05-10 15:07:24 -07005365 * <p>Here is a sample layout that makes use of fitting system windows
5366 * to have controls for a video view placed inside of the window decorations
5367 * that it hides and shows. This can be used with code like the second
5368 * sample (video player) shown in {@link #setSystemUiVisibility(int)}.
5369 *
5370 * {@sample development/samples/ApiDemos/res/layout/video_player.xml complete}
5371 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005372 * @param insets Current content insets of the window. Prior to
5373 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
5374 * the insets or else you and Android will be unhappy.
5375 *
5376 * @return Return true if this view applied the insets and it should not
5377 * continue propagating further down the hierarchy, false otherwise.
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005378 * @see #getFitsSystemWindows()
Romain Guyf0af1d52012-07-11 18:31:21 -07005379 * @see #setFitsSystemWindows(boolean)
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005380 * @see #setSystemUiVisibility(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005381 */
5382 protected boolean fitSystemWindows(Rect insets) {
5383 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07005384 mUserPaddingStart = UNDEFINED_PADDING;
5385 mUserPaddingEnd = UNDEFINED_PADDING;
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005386 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
5387 || mAttachInfo == null
5388 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
5389 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
5390 return true;
5391 } else {
5392 internalSetPadding(0, 0, 0, 0);
5393 return false;
5394 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005395 }
5396 return false;
5397 }
5398
5399 /**
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005400 * Sets whether or not this view should account for system screen decorations
5401 * such as the status bar and inset its content; that is, controlling whether
5402 * the default implementation of {@link #fitSystemWindows(Rect)} will be
5403 * executed. See that method for more details.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005404 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005405 * <p>Note that if you are providing your own implementation of
5406 * {@link #fitSystemWindows(Rect)}, then there is no need to set this
5407 * flag to true -- your implementation will be overriding the default
5408 * implementation that checks this flag.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005409 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005410 * @param fitSystemWindows If true, then the default implementation of
5411 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005412 *
5413 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005414 * @see #getFitsSystemWindows()
5415 * @see #fitSystemWindows(Rect)
5416 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005417 */
5418 public void setFitsSystemWindows(boolean fitSystemWindows) {
5419 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
5420 }
5421
5422 /**
Dianne Hackborncf675782012-05-10 15:07:24 -07005423 * Check for state of {@link #setFitsSystemWindows(boolean). If this method
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005424 * returns true, the default implementation of {@link #fitSystemWindows(Rect)}
5425 * will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005426 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005427 * @return Returns true if the default implementation of
5428 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005429 *
5430 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005431 * @see #setFitsSystemWindows()
5432 * @see #fitSystemWindows(Rect)
5433 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005434 */
Dianne Hackborncf675782012-05-10 15:07:24 -07005435 public boolean getFitsSystemWindows() {
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005436 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
5437 }
5438
Dianne Hackbornb1b55e62012-05-10 16:25:54 -07005439 /** @hide */
5440 public boolean fitsSystemWindows() {
5441 return getFitsSystemWindows();
5442 }
5443
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005444 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005445 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
5446 */
5447 public void requestFitSystemWindows() {
5448 if (mParent != null) {
5449 mParent.requestFitSystemWindows();
5450 }
5451 }
5452
5453 /**
5454 * For use by PhoneWindow to make its own system window fitting optional.
5455 * @hide
5456 */
5457 public void makeOptionalFitsSystemWindows() {
5458 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5459 }
5460
5461 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005462 * Returns the visibility status for this view.
5463 *
5464 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5465 * @attr ref android.R.styleable#View_visibility
5466 */
5467 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005468 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5469 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5470 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005471 })
5472 public int getVisibility() {
5473 return mViewFlags & VISIBILITY_MASK;
5474 }
5475
5476 /**
5477 * Set the enabled state of this view.
5478 *
5479 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5480 * @attr ref android.R.styleable#View_visibility
5481 */
5482 @RemotableViewMethod
5483 public void setVisibility(int visibility) {
5484 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005485 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005486 }
5487
5488 /**
5489 * Returns the enabled status for this view. The interpretation of the
5490 * enabled state varies by subclass.
5491 *
5492 * @return True if this view is enabled, false otherwise.
5493 */
5494 @ViewDebug.ExportedProperty
5495 public boolean isEnabled() {
5496 return (mViewFlags & ENABLED_MASK) == ENABLED;
5497 }
5498
5499 /**
5500 * Set the enabled state of this view. The interpretation of the enabled
5501 * state varies by subclass.
5502 *
5503 * @param enabled True if this view is enabled, false otherwise.
5504 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005505 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005506 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005507 if (enabled == isEnabled()) return;
5508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005509 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5510
5511 /*
5512 * The View most likely has to change its appearance, so refresh
5513 * the drawable state.
5514 */
5515 refreshDrawableState();
5516
5517 // Invalidate too, since the default behavior for views is to be
5518 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005519 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005520 }
5521
5522 /**
5523 * Set whether this view can receive the focus.
5524 *
5525 * Setting this to false will also ensure that this view is not focusable
5526 * in touch mode.
5527 *
5528 * @param focusable If true, this view can receive the focus.
5529 *
5530 * @see #setFocusableInTouchMode(boolean)
5531 * @attr ref android.R.styleable#View_focusable
5532 */
5533 public void setFocusable(boolean focusable) {
5534 if (!focusable) {
5535 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5536 }
5537 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5538 }
5539
5540 /**
5541 * Set whether this view can receive focus while in touch mode.
5542 *
5543 * Setting this to true will also ensure that this view is focusable.
5544 *
5545 * @param focusableInTouchMode If true, this view can receive the focus while
5546 * in touch mode.
5547 *
5548 * @see #setFocusable(boolean)
5549 * @attr ref android.R.styleable#View_focusableInTouchMode
5550 */
5551 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5552 // Focusable in touch mode should always be set before the focusable flag
5553 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5554 // which, in touch mode, will not successfully request focus on this view
5555 // because the focusable in touch mode flag is not set
5556 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5557 if (focusableInTouchMode) {
5558 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5559 }
5560 }
5561
5562 /**
5563 * Set whether this view should have sound effects enabled for events such as
5564 * clicking and touching.
5565 *
5566 * <p>You may wish to disable sound effects for a view if you already play sounds,
5567 * for instance, a dial key that plays dtmf tones.
5568 *
5569 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5570 * @see #isSoundEffectsEnabled()
5571 * @see #playSoundEffect(int)
5572 * @attr ref android.R.styleable#View_soundEffectsEnabled
5573 */
5574 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5575 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5576 }
5577
5578 /**
5579 * @return whether this view should have sound effects enabled for events such as
5580 * clicking and touching.
5581 *
5582 * @see #setSoundEffectsEnabled(boolean)
5583 * @see #playSoundEffect(int)
5584 * @attr ref android.R.styleable#View_soundEffectsEnabled
5585 */
5586 @ViewDebug.ExportedProperty
5587 public boolean isSoundEffectsEnabled() {
5588 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5589 }
5590
5591 /**
5592 * Set whether this view should have haptic feedback for events such as
5593 * long presses.
5594 *
5595 * <p>You may wish to disable haptic feedback if your view already controls
5596 * its own haptic feedback.
5597 *
5598 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5599 * @see #isHapticFeedbackEnabled()
5600 * @see #performHapticFeedback(int)
5601 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5602 */
5603 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5604 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5605 }
5606
5607 /**
5608 * @return whether this view should have haptic feedback enabled for events
5609 * long presses.
5610 *
5611 * @see #setHapticFeedbackEnabled(boolean)
5612 * @see #performHapticFeedback(int)
5613 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5614 */
5615 @ViewDebug.ExportedProperty
5616 public boolean isHapticFeedbackEnabled() {
5617 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5618 }
5619
5620 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005621 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005622 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005623 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5624 * {@link #LAYOUT_DIRECTION_RTL},
5625 * {@link #LAYOUT_DIRECTION_INHERIT} or
5626 * {@link #LAYOUT_DIRECTION_LOCALE}.
5627 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005628 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005629 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005630 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5631 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5632 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5633 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005634 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005635 public int getLayoutDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005636 return (mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_MASK) >> PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005637 }
5638
5639 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005640 * Set the layout direction for this view. This will propagate a reset of layout direction
5641 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005642 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005643 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
5644 * {@link #LAYOUT_DIRECTION_RTL},
5645 * {@link #LAYOUT_DIRECTION_INHERIT} or
5646 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005647 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005648 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005649 */
5650 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005651 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005652 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005653 // Reset the current layout direction and the resolved one
Dianne Hackborn4702a852012-08-17 15:18:29 -07005654 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07005655 resetResolvedLayoutDirection();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005656 // Reset padding resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -07005657 mPrivateFlags2 &= ~PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005658 // Set the new layout direction (filtered)
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005659 mPrivateFlags2 |=
Dianne Hackborn4702a852012-08-17 15:18:29 -07005660 ((layoutDirection << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) & PFLAG2_LAYOUT_DIRECTION_MASK);
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005661 resolveRtlProperties();
5662 // ... and ask for a layout pass
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005663 requestLayout();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005664 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005665 }
5666
5667 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005668 * Returns the resolved layout direction for this view.
5669 *
5670 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005671 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005672 */
5673 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005674 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5675 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005676 })
5677 public int getResolvedLayoutDirection() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005678 final int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion;
5679 if (targetSdkVersion < JELLY_BEAN_MR1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005680 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005681 return LAYOUT_DIRECTION_LTR;
5682 }
Fabrice Di Megliob93911f2012-06-26 19:43:15 -07005683 // The layout direction will be resolved only if needed
Dianne Hackborn4702a852012-08-17 15:18:29 -07005684 if ((mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_RESOLVED) != PFLAG2_LAYOUT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -07005685 resolveLayoutDirection();
5686 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005687 return ((mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL) == PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005688 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
5689 }
5690
5691 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005692 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5693 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005694 *
5695 * @return true if the layout is right-to-left.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005696 */
5697 @ViewDebug.ExportedProperty(category = "layout")
5698 public boolean isLayoutRtl() {
5699 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
5700 }
5701
5702 /**
Adam Powell539ee872012-02-03 19:00:49 -08005703 * Indicates whether the view is currently tracking transient state that the
5704 * app should not need to concern itself with saving and restoring, but that
5705 * the framework should take special note to preserve when possible.
5706 *
Adam Powell785c4472012-05-02 21:25:39 -07005707 * <p>A view with transient state cannot be trivially rebound from an external
5708 * data source, such as an adapter binding item views in a list. This may be
5709 * because the view is performing an animation, tracking user selection
5710 * of content, or similar.</p>
5711 *
Adam Powell539ee872012-02-03 19:00:49 -08005712 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005713 */
5714 @ViewDebug.ExportedProperty(category = "layout")
5715 public boolean hasTransientState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005716 return (mPrivateFlags2 & PFLAG2_HAS_TRANSIENT_STATE) == PFLAG2_HAS_TRANSIENT_STATE;
Adam Powell539ee872012-02-03 19:00:49 -08005717 }
5718
5719 /**
5720 * Set whether this view is currently tracking transient state that the
Chet Haase563d4f22012-04-18 16:20:08 -07005721 * framework should attempt to preserve when possible. This flag is reference counted,
5722 * so every call to setHasTransientState(true) should be paired with a later call
5723 * to setHasTransientState(false).
Adam Powell539ee872012-02-03 19:00:49 -08005724 *
Adam Powell785c4472012-05-02 21:25:39 -07005725 * <p>A view with transient state cannot be trivially rebound from an external
5726 * data source, such as an adapter binding item views in a list. This may be
5727 * because the view is performing an animation, tracking user selection
5728 * of content, or similar.</p>
5729 *
Adam Powell539ee872012-02-03 19:00:49 -08005730 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005731 */
5732 public void setHasTransientState(boolean hasTransientState) {
Chet Haase563d4f22012-04-18 16:20:08 -07005733 mTransientStateCount = hasTransientState ? mTransientStateCount + 1 :
5734 mTransientStateCount - 1;
5735 if (mTransientStateCount < 0) {
5736 mTransientStateCount = 0;
5737 Log.e(VIEW_LOG_TAG, "hasTransientState decremented below 0: " +
5738 "unmatched pair of setHasTransientState calls");
5739 }
5740 if ((hasTransientState && mTransientStateCount == 1) ||
Adam Powell057a5852012-05-11 10:28:38 -07005741 (!hasTransientState && mTransientStateCount == 0)) {
Chet Haase563d4f22012-04-18 16:20:08 -07005742 // update flag if we've just incremented up from 0 or decremented down to 0
Dianne Hackborn4702a852012-08-17 15:18:29 -07005743 mPrivateFlags2 = (mPrivateFlags2 & ~PFLAG2_HAS_TRANSIENT_STATE) |
5744 (hasTransientState ? PFLAG2_HAS_TRANSIENT_STATE : 0);
Chet Haase563d4f22012-04-18 16:20:08 -07005745 if (mParent != null) {
5746 try {
5747 mParent.childHasTransientStateChanged(this, hasTransientState);
5748 } catch (AbstractMethodError e) {
5749 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5750 " does not fully implement ViewParent", e);
5751 }
Adam Powell539ee872012-02-03 19:00:49 -08005752 }
5753 }
5754 }
5755
5756 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005757 * If this view doesn't do any drawing on its own, set this flag to
5758 * allow further optimizations. By default, this flag is not set on
5759 * View, but could be set on some View subclasses such as ViewGroup.
5760 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005761 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5762 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005763 *
5764 * @param willNotDraw whether or not this View draw on its own
5765 */
5766 public void setWillNotDraw(boolean willNotDraw) {
5767 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5768 }
5769
5770 /**
5771 * Returns whether or not this View draws on its own.
5772 *
5773 * @return true if this view has nothing to draw, false otherwise
5774 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005775 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005776 public boolean willNotDraw() {
5777 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
5778 }
5779
5780 /**
5781 * When a View's drawing cache is enabled, drawing is redirected to an
5782 * offscreen bitmap. Some views, like an ImageView, must be able to
5783 * bypass this mechanism if they already draw a single bitmap, to avoid
5784 * unnecessary usage of the memory.
5785 *
5786 * @param willNotCacheDrawing true if this view does not cache its
5787 * drawing, false otherwise
5788 */
5789 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
5790 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
5791 }
5792
5793 /**
5794 * Returns whether or not this View can cache its drawing or not.
5795 *
5796 * @return true if this view does not cache its drawing, false otherwise
5797 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005798 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005799 public boolean willNotCacheDrawing() {
5800 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5801 }
5802
5803 /**
5804 * Indicates whether this view reacts to click events or not.
5805 *
5806 * @return true if the view is clickable, false otherwise
5807 *
5808 * @see #setClickable(boolean)
5809 * @attr ref android.R.styleable#View_clickable
5810 */
5811 @ViewDebug.ExportedProperty
5812 public boolean isClickable() {
5813 return (mViewFlags & CLICKABLE) == CLICKABLE;
5814 }
5815
5816 /**
5817 * Enables or disables click events for this view. When a view
5818 * is clickable it will change its state to "pressed" on every click.
5819 * Subclasses should set the view clickable to visually react to
5820 * user's clicks.
5821 *
5822 * @param clickable true to make the view clickable, false otherwise
5823 *
5824 * @see #isClickable()
5825 * @attr ref android.R.styleable#View_clickable
5826 */
5827 public void setClickable(boolean clickable) {
5828 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5829 }
5830
5831 /**
5832 * Indicates whether this view reacts to long click events or not.
5833 *
5834 * @return true if the view is long clickable, false otherwise
5835 *
5836 * @see #setLongClickable(boolean)
5837 * @attr ref android.R.styleable#View_longClickable
5838 */
5839 public boolean isLongClickable() {
5840 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5841 }
5842
5843 /**
5844 * Enables or disables long click events for this view. When a view is long
5845 * clickable it reacts to the user holding down the button for a longer
5846 * duration than a tap. This event can either launch the listener or a
5847 * context menu.
5848 *
5849 * @param longClickable true to make the view long clickable, false otherwise
5850 * @see #isLongClickable()
5851 * @attr ref android.R.styleable#View_longClickable
5852 */
5853 public void setLongClickable(boolean longClickable) {
5854 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5855 }
5856
5857 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005858 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005859 *
5860 * @see #isClickable()
5861 * @see #setClickable(boolean)
5862 *
5863 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5864 * the View's internal state from a previously set "pressed" state.
5865 */
5866 public void setPressed(boolean pressed) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005867 final boolean needsRefresh = pressed != ((mPrivateFlags & PFLAG_PRESSED) == PFLAG_PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005869 if (pressed) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005870 mPrivateFlags |= PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005871 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005872 mPrivateFlags &= ~PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005873 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005874
5875 if (needsRefresh) {
5876 refreshDrawableState();
5877 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005878 dispatchSetPressed(pressed);
5879 }
5880
5881 /**
5882 * Dispatch setPressed to all of this View's children.
5883 *
5884 * @see #setPressed(boolean)
5885 *
5886 * @param pressed The new pressed state
5887 */
5888 protected void dispatchSetPressed(boolean pressed) {
5889 }
5890
5891 /**
5892 * Indicates whether the view is currently in pressed state. Unless
5893 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5894 * the pressed state.
5895 *
Philip Milne6c8ea062012-04-03 17:38:43 -07005896 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005897 * @see #isClickable()
5898 * @see #setClickable(boolean)
5899 *
5900 * @return true if the view is currently pressed, false otherwise
5901 */
5902 public boolean isPressed() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005903 return (mPrivateFlags & PFLAG_PRESSED) == PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005904 }
5905
5906 /**
5907 * Indicates whether this view will save its state (that is,
5908 * whether its {@link #onSaveInstanceState} method will be called).
5909 *
5910 * @return Returns true if the view state saving is enabled, else false.
5911 *
5912 * @see #setSaveEnabled(boolean)
5913 * @attr ref android.R.styleable#View_saveEnabled
5914 */
5915 public boolean isSaveEnabled() {
5916 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5917 }
5918
5919 /**
5920 * Controls whether the saving of this view's state is
5921 * enabled (that is, whether its {@link #onSaveInstanceState} method
5922 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005923 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005924 * for its state to be saved. This flag can only disable the
5925 * saving of this view; any child views may still have their state saved.
5926 *
5927 * @param enabled Set to false to <em>disable</em> state saving, or true
5928 * (the default) to allow it.
5929 *
5930 * @see #isSaveEnabled()
5931 * @see #setId(int)
5932 * @see #onSaveInstanceState()
5933 * @attr ref android.R.styleable#View_saveEnabled
5934 */
5935 public void setSaveEnabled(boolean enabled) {
5936 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5937 }
5938
Jeff Brown85a31762010-09-01 17:01:00 -07005939 /**
5940 * Gets whether the framework should discard touches when the view's
5941 * window is obscured by another visible window.
5942 * Refer to the {@link View} security documentation for more details.
5943 *
5944 * @return True if touch filtering is enabled.
5945 *
5946 * @see #setFilterTouchesWhenObscured(boolean)
5947 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5948 */
5949 @ViewDebug.ExportedProperty
5950 public boolean getFilterTouchesWhenObscured() {
5951 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5952 }
5953
5954 /**
5955 * Sets whether the framework should discard touches when the view's
5956 * window is obscured by another visible window.
5957 * Refer to the {@link View} security documentation for more details.
5958 *
5959 * @param enabled True if touch filtering should be enabled.
5960 *
5961 * @see #getFilterTouchesWhenObscured
5962 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5963 */
5964 public void setFilterTouchesWhenObscured(boolean enabled) {
5965 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5966 FILTER_TOUCHES_WHEN_OBSCURED);
5967 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005968
5969 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005970 * Indicates whether the entire hierarchy under this view will save its
5971 * state when a state saving traversal occurs from its parent. The default
5972 * is true; if false, these views will not be saved unless
5973 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5974 *
5975 * @return Returns true if the view state saving from parent is enabled, else false.
5976 *
5977 * @see #setSaveFromParentEnabled(boolean)
5978 */
5979 public boolean isSaveFromParentEnabled() {
5980 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5981 }
5982
5983 /**
5984 * Controls whether the entire hierarchy under this view will save its
5985 * state when a state saving traversal occurs from its parent. The default
5986 * is true; if false, these views will not be saved unless
5987 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5988 *
5989 * @param enabled Set to false to <em>disable</em> state saving, or true
5990 * (the default) to allow it.
5991 *
5992 * @see #isSaveFromParentEnabled()
5993 * @see #setId(int)
5994 * @see #onSaveInstanceState()
5995 */
5996 public void setSaveFromParentEnabled(boolean enabled) {
5997 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5998 }
5999
6000
6001 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006002 * Returns whether this View is able to take focus.
6003 *
6004 * @return True if this view can take focus, or false otherwise.
6005 * @attr ref android.R.styleable#View_focusable
6006 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006007 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006008 public final boolean isFocusable() {
6009 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
6010 }
6011
6012 /**
6013 * When a view is focusable, it may not want to take focus when in touch mode.
6014 * For example, a button would like focus when the user is navigating via a D-pad
6015 * so that the user can click on it, but once the user starts touching the screen,
6016 * the button shouldn't take focus
6017 * @return Whether the view is focusable in touch mode.
6018 * @attr ref android.R.styleable#View_focusableInTouchMode
6019 */
6020 @ViewDebug.ExportedProperty
6021 public final boolean isFocusableInTouchMode() {
6022 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
6023 }
6024
6025 /**
6026 * Find the nearest view in the specified direction that can take focus.
6027 * This does not actually give focus to that view.
6028 *
6029 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6030 *
6031 * @return The nearest focusable in the specified direction, or null if none
6032 * can be found.
6033 */
6034 public View focusSearch(int direction) {
6035 if (mParent != null) {
6036 return mParent.focusSearch(this, direction);
6037 } else {
6038 return null;
6039 }
6040 }
6041
6042 /**
6043 * This method is the last chance for the focused view and its ancestors to
6044 * respond to an arrow key. This is called when the focused view did not
6045 * consume the key internally, nor could the view system find a new view in
6046 * the requested direction to give focus to.
6047 *
6048 * @param focused The currently focused view.
6049 * @param direction The direction focus wants to move. One of FOCUS_UP,
6050 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
6051 * @return True if the this view consumed this unhandled move.
6052 */
6053 public boolean dispatchUnhandledMove(View focused, int direction) {
6054 return false;
6055 }
6056
6057 /**
6058 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08006059 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006060 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08006061 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
6062 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006063 * @return The user specified next view, or null if there is none.
6064 */
6065 View findUserSetNextFocus(View root, int direction) {
6066 switch (direction) {
6067 case FOCUS_LEFT:
6068 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006069 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006070 case FOCUS_RIGHT:
6071 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006072 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006073 case FOCUS_UP:
6074 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006075 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006076 case FOCUS_DOWN:
6077 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006078 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006079 case FOCUS_FORWARD:
6080 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006081 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006082 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08006083 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08006084 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006085 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08006086 @Override
6087 public boolean apply(View t) {
6088 return t.mNextFocusForwardId == id;
6089 }
6090 });
6091 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006092 }
6093 return null;
6094 }
6095
Jeff Brown4dfbec22011-08-15 14:55:37 -07006096 private View findViewInsideOutShouldExist(View root, final int childViewId) {
6097 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
6098 @Override
6099 public boolean apply(View t) {
6100 return t.mID == childViewId;
6101 }
6102 });
6103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006104 if (result == null) {
6105 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
6106 + "by user for id " + childViewId);
6107 }
6108 return result;
6109 }
6110
6111 /**
6112 * Find and return all focusable views that are descendants of this view,
6113 * possibly including this view if it is focusable itself.
6114 *
6115 * @param direction The direction of the focus
6116 * @return A list of focusable views
6117 */
6118 public ArrayList<View> getFocusables(int direction) {
6119 ArrayList<View> result = new ArrayList<View>(24);
6120 addFocusables(result, direction);
6121 return result;
6122 }
6123
6124 /**
6125 * Add any focusable views that are descendants of this view (possibly
6126 * including this view if it is focusable itself) to views. If we are in touch mode,
6127 * only add views that are also focusable in touch mode.
6128 *
6129 * @param views Focusable views found so far
6130 * @param direction The direction of the focus
6131 */
6132 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006133 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
6134 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006135
svetoslavganov75986cf2009-05-14 22:28:01 -07006136 /**
6137 * Adds any focusable views that are descendants of this view (possibly
6138 * including this view if it is focusable itself) to views. This method
6139 * adds all focusable views regardless if we are in touch mode or
Svetoslav Ganov42138042012-03-20 11:51:39 -07006140 * only views focusable in touch mode if we are in touch mode or
6141 * only views that can take accessibility focus if accessibility is enabeld
6142 * depending on the focusable mode paramater.
svetoslavganov75986cf2009-05-14 22:28:01 -07006143 *
6144 * @param views Focusable views found so far or null if all we are interested is
6145 * the number of focusables.
6146 * @param direction The direction of the focus.
6147 * @param focusableMode The type of focusables to be added.
6148 *
6149 * @see #FOCUSABLES_ALL
6150 * @see #FOCUSABLES_TOUCH_MODE
6151 */
6152 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006153 if (views == null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006154 return;
6155 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006156 if (!isFocusable()) {
6157 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07006158 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006159 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE
6160 && isInTouchMode() && !isFocusableInTouchMode()) {
6161 return;
6162 }
6163 views.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006164 }
6165
6166 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006167 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006168 * The search is performed by either the text that the View renders or the content
6169 * description that describes the view for accessibility purposes and the view does
6170 * not render or both. Clients can specify how the search is to be performed via
6171 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
6172 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006173 *
6174 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006175 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07006176 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006177 * @see #FIND_VIEWS_WITH_TEXT
6178 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
6179 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006180 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006181 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07006182 if (getAccessibilityNodeProvider() != null) {
6183 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
6184 outViews.add(this);
6185 }
6186 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006187 && (searched != null && searched.length() > 0)
6188 && (mContentDescription != null && mContentDescription.length() > 0)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006189 String searchedLowerCase = searched.toString().toLowerCase();
6190 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
6191 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
6192 outViews.add(this);
6193 }
6194 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006195 }
6196
6197 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006198 * Find and return all touchable views that are descendants of this view,
6199 * possibly including this view if it is touchable itself.
6200 *
6201 * @return A list of touchable views
6202 */
6203 public ArrayList<View> getTouchables() {
6204 ArrayList<View> result = new ArrayList<View>();
6205 addTouchables(result);
6206 return result;
6207 }
6208
6209 /**
6210 * Add any touchable views that are descendants of this view (possibly
6211 * including this view if it is touchable itself) to views.
6212 *
6213 * @param views Touchable views found so far
6214 */
6215 public void addTouchables(ArrayList<View> views) {
6216 final int viewFlags = mViewFlags;
6217
6218 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
6219 && (viewFlags & ENABLED_MASK) == ENABLED) {
6220 views.add(this);
6221 }
6222 }
6223
6224 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006225 * Returns whether this View is accessibility focused.
6226 *
6227 * @return True if this View is accessibility focused.
6228 */
6229 boolean isAccessibilityFocused() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006230 return (mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006231 }
6232
6233 /**
6234 * Call this to try to give accessibility focus to this view.
6235 *
6236 * A view will not actually take focus if {@link AccessibilityManager#isEnabled()}
6237 * returns false or the view is no visible or the view already has accessibility
6238 * focus.
6239 *
6240 * See also {@link #focusSearch(int)}, which is what you call to say that you
6241 * have focus, and you want your parent to look for the next one.
6242 *
6243 * @return Whether this view actually took accessibility focus.
6244 *
6245 * @hide
6246 */
6247 public boolean requestAccessibilityFocus() {
Svetoslav Ganov07b726c2012-04-30 12:24:57 -07006248 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
6249 if (!manager.isEnabled() || !manager.isTouchExplorationEnabled()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006250 return false;
6251 }
6252 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6253 return false;
6254 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07006255 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) == 0) {
6256 mPrivateFlags2 |= PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006257 ViewRootImpl viewRootImpl = getViewRootImpl();
6258 if (viewRootImpl != null) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006259 viewRootImpl.setAccessibilityFocus(this, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07006260 }
6261 invalidate();
6262 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
6263 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006264 return true;
6265 }
6266 return false;
6267 }
6268
6269 /**
6270 * Call this to try to clear accessibility focus of this view.
6271 *
6272 * See also {@link #focusSearch(int)}, which is what you call to say that you
6273 * have focus, and you want your parent to look for the next one.
6274 *
6275 * @hide
6276 */
6277 public void clearAccessibilityFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006278 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0) {
6279 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006280 invalidate();
6281 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
6282 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006283 }
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006284 // Clear the global reference of accessibility focus if this
6285 // view or any of its descendants had accessibility focus.
6286 ViewRootImpl viewRootImpl = getViewRootImpl();
6287 if (viewRootImpl != null) {
6288 View focusHost = viewRootImpl.getAccessibilityFocusedHost();
6289 if (focusHost != null && ViewRootImpl.isViewDescendantOf(focusHost, this)) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006290 viewRootImpl.setAccessibilityFocus(null, null);
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006291 }
6292 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006293 }
6294
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07006295 private void sendAccessibilityHoverEvent(int eventType) {
6296 // Since we are not delivering to a client accessibility events from not
6297 // important views (unless the clinet request that) we need to fire the
6298 // event from the deepest view exposed to the client. As a consequence if
6299 // the user crosses a not exposed view the client will see enter and exit
6300 // of the exposed predecessor followed by and enter and exit of that same
6301 // predecessor when entering and exiting the not exposed descendant. This
6302 // is fine since the client has a clear idea which view is hovered at the
6303 // price of a couple more events being sent. This is a simple and
6304 // working solution.
6305 View source = this;
6306 while (true) {
6307 if (source.includeForAccessibility()) {
6308 source.sendAccessibilityEvent(eventType);
6309 return;
6310 }
6311 ViewParent parent = source.getParent();
6312 if (parent instanceof View) {
6313 source = (View) parent;
6314 } else {
6315 return;
6316 }
6317 }
6318 }
6319
Svetoslav Ganov42138042012-03-20 11:51:39 -07006320 /**
6321 * Clears accessibility focus without calling any callback methods
6322 * normally invoked in {@link #clearAccessibilityFocus()}. This method
6323 * is used for clearing accessibility focus when giving this focus to
6324 * another view.
6325 */
6326 void clearAccessibilityFocusNoCallbacks() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006327 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0) {
6328 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006329 invalidate();
6330 }
6331 }
6332
6333 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006334 * Call this to try to give focus to a specific view or to one of its
6335 * descendants.
6336 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006337 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6338 * false), or if it is focusable and it is not focusable in touch mode
6339 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006340 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006341 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006342 * have focus, and you want your parent to look for the next one.
6343 *
6344 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
6345 * {@link #FOCUS_DOWN} and <code>null</code>.
6346 *
6347 * @return Whether this view or one of its descendants actually took focus.
6348 */
6349 public final boolean requestFocus() {
6350 return requestFocus(View.FOCUS_DOWN);
6351 }
6352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006353 /**
6354 * Call this to try to give focus to a specific view or to one of its
6355 * descendants and give it a hint about what direction focus is heading.
6356 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006357 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6358 * false), or if it is focusable and it is not focusable in touch mode
6359 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006360 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006361 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006362 * have focus, and you want your parent to look for the next one.
6363 *
6364 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
6365 * <code>null</code> set for the previously focused rectangle.
6366 *
6367 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6368 * @return Whether this view or one of its descendants actually took focus.
6369 */
6370 public final boolean requestFocus(int direction) {
6371 return requestFocus(direction, null);
6372 }
6373
6374 /**
6375 * Call this to try to give focus to a specific view or to one of its descendants
6376 * and give it hints about the direction and a specific rectangle that the focus
6377 * is coming from. The rectangle can help give larger views a finer grained hint
6378 * about where focus is coming from, and therefore, where to show selection, or
6379 * forward focus change internally.
6380 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006381 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6382 * false), or if it is focusable and it is not focusable in touch mode
6383 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006384 *
6385 * A View will not take focus if it is not visible.
6386 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006387 * A View will not take focus if one of its parents has
6388 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
6389 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006390 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006391 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006392 * have focus, and you want your parent to look for the next one.
6393 *
6394 * You may wish to override this method if your custom {@link View} has an internal
6395 * {@link View} that it wishes to forward the request to.
6396 *
6397 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6398 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
6399 * to give a finer grained hint about where focus is coming from. May be null
6400 * if there is no hint.
6401 * @return Whether this view or one of its descendants actually took focus.
6402 */
6403 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006404 return requestFocusNoSearch(direction, previouslyFocusedRect);
6405 }
6406
6407 private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006408 // need to be focusable
6409 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
6410 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6411 return false;
6412 }
6413
6414 // need to be focusable in touch mode if in touch mode
6415 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006416 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
6417 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006418 }
6419
6420 // need to not have any parents blocking us
6421 if (hasAncestorThatBlocksDescendantFocus()) {
6422 return false;
6423 }
6424
6425 handleFocusGainInternal(direction, previouslyFocusedRect);
6426 return true;
6427 }
6428
6429 /**
6430 * Call this to try to give focus to a specific view or to one of its descendants. This is a
6431 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
6432 * touch mode to request focus when they are touched.
6433 *
6434 * @return Whether this view or one of its descendants actually took focus.
6435 *
6436 * @see #isInTouchMode()
6437 *
6438 */
6439 public final boolean requestFocusFromTouch() {
6440 // Leave touch mode if we need to
6441 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006442 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07006443 if (viewRoot != null) {
6444 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006445 }
6446 }
6447 return requestFocus(View.FOCUS_DOWN);
6448 }
6449
6450 /**
6451 * @return Whether any ancestor of this view blocks descendant focus.
6452 */
6453 private boolean hasAncestorThatBlocksDescendantFocus() {
6454 ViewParent ancestor = mParent;
6455 while (ancestor instanceof ViewGroup) {
6456 final ViewGroup vgAncestor = (ViewGroup) ancestor;
6457 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
6458 return true;
6459 } else {
6460 ancestor = vgAncestor.getParent();
6461 }
6462 }
6463 return false;
6464 }
6465
6466 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006467 * Gets the mode for determining whether this View is important for accessibility
6468 * which is if it fires accessibility events and if it is reported to
6469 * accessibility services that query the screen.
6470 *
6471 * @return The mode for determining whether a View is important for accessibility.
6472 *
6473 * @attr ref android.R.styleable#View_importantForAccessibility
6474 *
6475 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6476 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6477 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6478 */
6479 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07006480 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_AUTO, to = "auto"),
6481 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_YES, to = "yes"),
6482 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_NO, to = "no")
Svetoslav Ganov42138042012-03-20 11:51:39 -07006483 })
6484 public int getImportantForAccessibility() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006485 return (mPrivateFlags2 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK)
6486 >> PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006487 }
6488
6489 /**
6490 * Sets how to determine whether this view is important for accessibility
6491 * which is if it fires accessibility events and if it is reported to
6492 * accessibility services that query the screen.
6493 *
6494 * @param mode How to determine whether this view is important for accessibility.
6495 *
6496 * @attr ref android.R.styleable#View_importantForAccessibility
6497 *
6498 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6499 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6500 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6501 */
6502 public void setImportantForAccessibility(int mode) {
6503 if (mode != getImportantForAccessibility()) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006504 mPrivateFlags2 &= ~PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK;
6505 mPrivateFlags2 |= (mode << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT)
6506 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006507 notifyAccessibilityStateChanged();
6508 }
6509 }
6510
6511 /**
6512 * Gets whether this view should be exposed for accessibility.
6513 *
6514 * @return Whether the view is exposed for accessibility.
6515 *
6516 * @hide
6517 */
6518 public boolean isImportantForAccessibility() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006519 final int mode = (mPrivateFlags2 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK)
6520 >> PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006521 switch (mode) {
6522 case IMPORTANT_FOR_ACCESSIBILITY_YES:
6523 return true;
6524 case IMPORTANT_FOR_ACCESSIBILITY_NO:
6525 return false;
6526 case IMPORTANT_FOR_ACCESSIBILITY_AUTO:
Svetoslav Ganov34caec92012-07-19 18:07:58 -07006527 return isActionableForAccessibility() || hasListenersForAccessibility()
6528 || getAccessibilityNodeProvider() != null;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006529 default:
6530 throw new IllegalArgumentException("Unknow important for accessibility mode: "
6531 + mode);
6532 }
6533 }
6534
6535 /**
6536 * Gets the parent for accessibility purposes. Note that the parent for
6537 * accessibility is not necessary the immediate parent. It is the first
6538 * predecessor that is important for accessibility.
6539 *
6540 * @return The parent for accessibility purposes.
6541 */
6542 public ViewParent getParentForAccessibility() {
6543 if (mParent instanceof View) {
6544 View parentView = (View) mParent;
6545 if (parentView.includeForAccessibility()) {
6546 return mParent;
6547 } else {
6548 return mParent.getParentForAccessibility();
6549 }
6550 }
6551 return null;
6552 }
6553
6554 /**
6555 * Adds the children of a given View for accessibility. Since some Views are
6556 * not important for accessibility the children for accessibility are not
6557 * necessarily direct children of the riew, rather they are the first level of
6558 * descendants important for accessibility.
6559 *
6560 * @param children The list of children for accessibility.
6561 */
6562 public void addChildrenForAccessibility(ArrayList<View> children) {
6563 if (includeForAccessibility()) {
6564 children.add(this);
6565 }
6566 }
6567
6568 /**
6569 * Whether to regard this view for accessibility. A view is regarded for
6570 * accessibility if it is important for accessibility or the querying
6571 * accessibility service has explicitly requested that view not
6572 * important for accessibility are regarded.
6573 *
6574 * @return Whether to regard the view for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006575 *
6576 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006577 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006578 public boolean includeForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006579 if (mAttachInfo != null) {
Romain Guyf0af1d52012-07-11 18:31:21 -07006580 return mAttachInfo.mIncludeNotImportantViews || isImportantForAccessibility();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006581 }
6582 return false;
6583 }
6584
6585 /**
6586 * Returns whether the View is considered actionable from
6587 * accessibility perspective. Such view are important for
6588 * accessiiblity.
6589 *
6590 * @return True if the view is actionable for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006591 *
6592 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006593 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006594 public boolean isActionableForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006595 return (isClickable() || isLongClickable() || isFocusable());
6596 }
6597
6598 /**
6599 * Returns whether the View has registered callbacks wich makes it
6600 * important for accessiiblity.
6601 *
6602 * @return True if the view is actionable for accessibility.
6603 */
6604 private boolean hasListenersForAccessibility() {
6605 ListenerInfo info = getListenerInfo();
6606 return mTouchDelegate != null || info.mOnKeyListener != null
6607 || info.mOnTouchListener != null || info.mOnGenericMotionListener != null
6608 || info.mOnHoverListener != null || info.mOnDragListener != null;
6609 }
6610
6611 /**
6612 * Notifies accessibility services that some view's important for
6613 * accessibility state has changed. Note that such notifications
6614 * are made at most once every
6615 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}
6616 * to avoid unnecessary load to the system. Also once a view has
6617 * made a notifucation this method is a NOP until the notification has
6618 * been sent to clients.
6619 *
6620 * @hide
6621 *
6622 * TODO: Makse sure this method is called for any view state change
6623 * that is interesting for accessilility purposes.
6624 */
6625 public void notifyAccessibilityStateChanged() {
Svetoslav Ganovc406be92012-05-11 16:12:32 -07006626 if (!AccessibilityManager.getInstance(mContext).isEnabled()) {
6627 return;
6628 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07006629 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_STATE_CHANGED) == 0) {
6630 mPrivateFlags2 |= PFLAG2_ACCESSIBILITY_STATE_CHANGED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006631 if (mParent != null) {
6632 mParent.childAccessibilityStateChanged(this);
6633 }
6634 }
6635 }
6636
6637 /**
6638 * Reset the state indicating the this view has requested clients
6639 * interested in its accessiblity state to be notified.
6640 *
6641 * @hide
6642 */
6643 public void resetAccessibilityStateChanged() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006644 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_STATE_CHANGED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006645 }
6646
6647 /**
6648 * Performs the specified accessibility action on the view. For
6649 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
alanv8eeefef2012-05-07 16:57:53 -07006650 * <p>
6651 * If an {@link AccessibilityDelegate} has been specified via calling
6652 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
6653 * {@link AccessibilityDelegate#performAccessibilityAction(View, int, Bundle)}
6654 * is responsible for handling this call.
6655 * </p>
Svetoslav Ganov42138042012-03-20 11:51:39 -07006656 *
6657 * @param action The action to perform.
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006658 * @param arguments Optional action arguments.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006659 * @return Whether the action was performed.
6660 */
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006661 public boolean performAccessibilityAction(int action, Bundle arguments) {
alanv8eeefef2012-05-07 16:57:53 -07006662 if (mAccessibilityDelegate != null) {
6663 return mAccessibilityDelegate.performAccessibilityAction(this, action, arguments);
6664 } else {
6665 return performAccessibilityActionInternal(action, arguments);
6666 }
6667 }
6668
6669 /**
6670 * @see #performAccessibilityAction(int, Bundle)
6671 *
6672 * Note: Called from the default {@link AccessibilityDelegate}.
6673 */
6674 boolean performAccessibilityActionInternal(int action, Bundle arguments) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006675 switch (action) {
6676 case AccessibilityNodeInfo.ACTION_CLICK: {
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006677 if (isClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006678 return performClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006679 }
6680 } break;
6681 case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
6682 if (isLongClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006683 return performLongClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006684 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006685 } break;
6686 case AccessibilityNodeInfo.ACTION_FOCUS: {
6687 if (!hasFocus()) {
6688 // Get out of touch mode since accessibility
6689 // wants to move focus around.
6690 getViewRootImpl().ensureTouchMode(false);
6691 return requestFocus();
6692 }
6693 } break;
6694 case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS: {
6695 if (hasFocus()) {
6696 clearFocus();
6697 return !isFocused();
6698 }
6699 } break;
6700 case AccessibilityNodeInfo.ACTION_SELECT: {
6701 if (!isSelected()) {
6702 setSelected(true);
6703 return isSelected();
6704 }
6705 } break;
6706 case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
6707 if (isSelected()) {
6708 setSelected(false);
6709 return !isSelected();
6710 }
6711 } break;
6712 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07006713 if (!isAccessibilityFocused()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006714 return requestAccessibilityFocus();
6715 }
6716 } break;
6717 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
6718 if (isAccessibilityFocused()) {
6719 clearAccessibilityFocus();
6720 return true;
6721 }
6722 } break;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006723 case AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY: {
6724 if (arguments != null) {
6725 final int granularity = arguments.getInt(
6726 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6727 return nextAtGranularity(granularity);
6728 }
6729 } break;
6730 case AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY: {
6731 if (arguments != null) {
6732 final int granularity = arguments.getInt(
6733 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6734 return previousAtGranularity(granularity);
6735 }
6736 } break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006737 }
6738 return false;
6739 }
6740
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006741 private boolean nextAtGranularity(int granularity) {
6742 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006743 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006744 return false;
6745 }
6746 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6747 if (iterator == null) {
6748 return false;
6749 }
6750 final int current = getAccessibilityCursorPosition();
6751 final int[] range = iterator.following(current);
6752 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006753 return false;
6754 }
6755 final int start = range[0];
6756 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006757 setAccessibilityCursorPosition(end);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006758 sendViewTextTraversedAtGranularityEvent(
6759 AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY,
6760 granularity, start, end);
6761 return true;
6762 }
6763
6764 private boolean previousAtGranularity(int granularity) {
6765 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006766 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006767 return false;
6768 }
6769 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6770 if (iterator == null) {
6771 return false;
6772 }
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006773 int current = getAccessibilityCursorPosition();
6774 if (current == ACCESSIBILITY_CURSOR_POSITION_UNDEFINED) {
6775 current = text.length();
6776 } else if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
6777 // When traversing by character we always put the cursor after the character
6778 // to ease edit and have to compensate before asking the for previous segment.
6779 current--;
6780 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006781 final int[] range = iterator.preceding(current);
6782 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006783 return false;
6784 }
6785 final int start = range[0];
6786 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006787 // Always put the cursor after the character to ease edit.
6788 if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
6789 setAccessibilityCursorPosition(end);
6790 } else {
6791 setAccessibilityCursorPosition(start);
6792 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006793 sendViewTextTraversedAtGranularityEvent(
6794 AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
6795 granularity, start, end);
6796 return true;
6797 }
6798
6799 /**
6800 * Gets the text reported for accessibility purposes.
6801 *
6802 * @return The accessibility text.
6803 *
6804 * @hide
6805 */
6806 public CharSequence getIterableTextForAccessibility() {
6807 return mContentDescription;
6808 }
6809
6810 /**
6811 * @hide
6812 */
6813 public int getAccessibilityCursorPosition() {
6814 return mAccessibilityCursorPosition;
6815 }
6816
6817 /**
6818 * @hide
6819 */
6820 public void setAccessibilityCursorPosition(int position) {
6821 mAccessibilityCursorPosition = position;
6822 }
6823
6824 private void sendViewTextTraversedAtGranularityEvent(int action, int granularity,
6825 int fromIndex, int toIndex) {
6826 if (mParent == null) {
6827 return;
6828 }
6829 AccessibilityEvent event = AccessibilityEvent.obtain(
6830 AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY);
6831 onInitializeAccessibilityEvent(event);
6832 onPopulateAccessibilityEvent(event);
6833 event.setFromIndex(fromIndex);
6834 event.setToIndex(toIndex);
6835 event.setAction(action);
6836 event.setMovementGranularity(granularity);
6837 mParent.requestSendAccessibilityEvent(this, event);
6838 }
6839
6840 /**
6841 * @hide
6842 */
6843 public TextSegmentIterator getIteratorForGranularity(int granularity) {
6844 switch (granularity) {
6845 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER: {
6846 CharSequence text = getIterableTextForAccessibility();
6847 if (text != null && text.length() > 0) {
6848 CharacterTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07006849 CharacterTextSegmentIterator.getInstance(
6850 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006851 iterator.initialize(text.toString());
6852 return iterator;
6853 }
6854 } break;
6855 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD: {
6856 CharSequence text = getIterableTextForAccessibility();
6857 if (text != null && text.length() > 0) {
6858 WordTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07006859 WordTextSegmentIterator.getInstance(
6860 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006861 iterator.initialize(text.toString());
6862 return iterator;
6863 }
6864 } break;
6865 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH: {
6866 CharSequence text = getIterableTextForAccessibility();
6867 if (text != null && text.length() > 0) {
6868 ParagraphTextSegmentIterator iterator =
6869 ParagraphTextSegmentIterator.getInstance();
6870 iterator.initialize(text.toString());
6871 return iterator;
6872 }
6873 } break;
6874 }
6875 return null;
6876 }
6877
Svetoslav Ganov42138042012-03-20 11:51:39 -07006878 /**
Romain Guya440b002010-02-24 15:57:54 -08006879 * @hide
6880 */
6881 public void dispatchStartTemporaryDetach() {
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07006882 clearAccessibilityFocus();
Romain Guy38c2ece2012-05-24 14:20:56 -07006883 clearDisplayList();
6884
Romain Guya440b002010-02-24 15:57:54 -08006885 onStartTemporaryDetach();
6886 }
6887
6888 /**
6889 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006890 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
6891 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08006892 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006893 */
6894 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08006895 removeUnsetPressCallback();
Dianne Hackborn4702a852012-08-17 15:18:29 -07006896 mPrivateFlags |= PFLAG_CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08006897 }
6898
6899 /**
6900 * @hide
6901 */
6902 public void dispatchFinishTemporaryDetach() {
6903 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006904 }
Romain Guy8506ab42009-06-11 17:35:47 -07006905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006906 /**
6907 * Called after {@link #onStartTemporaryDetach} when the container is done
6908 * changing the view.
6909 */
6910 public void onFinishTemporaryDetach() {
6911 }
Romain Guy8506ab42009-06-11 17:35:47 -07006912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006913 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006914 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
6915 * for this view's window. Returns null if the view is not currently attached
6916 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07006917 * just use the standard high-level event callbacks like
6918 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006919 */
6920 public KeyEvent.DispatcherState getKeyDispatcherState() {
6921 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
6922 }
Joe Malin32736f02011-01-19 16:14:20 -08006923
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006924 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006925 * Dispatch a key event before it is processed by any input method
6926 * associated with the view hierarchy. This can be used to intercept
6927 * key events in special situations before the IME consumes them; a
6928 * typical example would be handling the BACK key to update the application's
6929 * UI instead of allowing the IME to see it and close itself.
6930 *
6931 * @param event The key event to be dispatched.
6932 * @return True if the event was handled, false otherwise.
6933 */
6934 public boolean dispatchKeyEventPreIme(KeyEvent event) {
6935 return onKeyPreIme(event.getKeyCode(), event);
6936 }
6937
6938 /**
6939 * Dispatch a key event to the next view on the focus path. This path runs
6940 * from the top of the view tree down to the currently focused view. If this
6941 * view has focus, it will dispatch to itself. Otherwise it will dispatch
6942 * the next node down the focus path. This method also fires any key
6943 * listeners.
6944 *
6945 * @param event The key event to be dispatched.
6946 * @return True if the event was handled, false otherwise.
6947 */
6948 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006949 if (mInputEventConsistencyVerifier != null) {
6950 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
6951 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006952
Jeff Brown21bc5c92011-02-28 18:27:14 -08006953 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07006954 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006955 ListenerInfo li = mListenerInfo;
6956 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6957 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006958 return true;
6959 }
6960
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006961 if (event.dispatch(this, mAttachInfo != null
6962 ? mAttachInfo.mKeyDispatchState : null, this)) {
6963 return true;
6964 }
6965
6966 if (mInputEventConsistencyVerifier != null) {
6967 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6968 }
6969 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006970 }
6971
6972 /**
6973 * Dispatches a key shortcut event.
6974 *
6975 * @param event The key event to be dispatched.
6976 * @return True if the event was handled by the view, false otherwise.
6977 */
6978 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
6979 return onKeyShortcut(event.getKeyCode(), event);
6980 }
6981
6982 /**
6983 * Pass the touch screen motion event down to the target view, or this
6984 * view if it is the target.
6985 *
6986 * @param event The motion event to be dispatched.
6987 * @return True if the event was handled by the view, false otherwise.
6988 */
6989 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006990 if (mInputEventConsistencyVerifier != null) {
6991 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
6992 }
6993
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006994 if (onFilterTouchEventForSecurity(event)) {
6995 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006996 ListenerInfo li = mListenerInfo;
6997 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6998 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006999 return true;
7000 }
7001
7002 if (onTouchEvent(event)) {
7003 return true;
7004 }
Jeff Brown85a31762010-09-01 17:01:00 -07007005 }
7006
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007007 if (mInputEventConsistencyVerifier != null) {
7008 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007009 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007010 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007011 }
7012
7013 /**
Jeff Brown85a31762010-09-01 17:01:00 -07007014 * Filter the touch event to apply security policies.
7015 *
7016 * @param event The motion event to be filtered.
7017 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08007018 *
Jeff Brown85a31762010-09-01 17:01:00 -07007019 * @see #getFilterTouchesWhenObscured
7020 */
7021 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07007022 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07007023 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
7024 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
7025 // Window is obscured, drop this touch.
7026 return false;
7027 }
7028 return true;
7029 }
7030
7031 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007032 * Pass a trackball motion event down to the focused view.
7033 *
7034 * @param event The motion event to be dispatched.
7035 * @return True if the event was handled by the view, false otherwise.
7036 */
7037 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007038 if (mInputEventConsistencyVerifier != null) {
7039 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
7040 }
7041
Romain Guy02ccac62011-06-24 13:20:23 -07007042 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007043 }
7044
7045 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007046 * Dispatch a generic motion event.
7047 * <p>
7048 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7049 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08007050 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07007051 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007052 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08007053 *
7054 * @param event The motion event to be dispatched.
7055 * @return True if the event was handled by the view, false otherwise.
7056 */
7057 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007058 if (mInputEventConsistencyVerifier != null) {
7059 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
7060 }
7061
Jeff Browna032cc02011-03-07 16:56:21 -08007062 final int source = event.getSource();
7063 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
7064 final int action = event.getAction();
7065 if (action == MotionEvent.ACTION_HOVER_ENTER
7066 || action == MotionEvent.ACTION_HOVER_MOVE
7067 || action == MotionEvent.ACTION_HOVER_EXIT) {
7068 if (dispatchHoverEvent(event)) {
7069 return true;
7070 }
7071 } else if (dispatchGenericPointerEvent(event)) {
7072 return true;
7073 }
7074 } else if (dispatchGenericFocusedEvent(event)) {
7075 return true;
7076 }
7077
Jeff Brown10b62902011-06-20 16:40:37 -07007078 if (dispatchGenericMotionEventInternal(event)) {
7079 return true;
7080 }
7081
7082 if (mInputEventConsistencyVerifier != null) {
7083 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7084 }
7085 return false;
7086 }
7087
7088 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07007089 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007090 ListenerInfo li = mListenerInfo;
7091 if (li != null && li.mOnGenericMotionListener != null
7092 && (mViewFlags & ENABLED_MASK) == ENABLED
7093 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007094 return true;
7095 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007096
7097 if (onGenericMotionEvent(event)) {
7098 return true;
7099 }
7100
7101 if (mInputEventConsistencyVerifier != null) {
7102 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7103 }
7104 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08007105 }
7106
7107 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007108 * Dispatch a hover event.
7109 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007110 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07007111 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007112 * </p>
7113 *
7114 * @param event The motion event to be dispatched.
7115 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007116 */
7117 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07007118 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007119 ListenerInfo li = mListenerInfo;
7120 if (li != null && li.mOnHoverListener != null
7121 && (mViewFlags & ENABLED_MASK) == ENABLED
7122 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07007123 return true;
7124 }
7125
Jeff Browna032cc02011-03-07 16:56:21 -08007126 return onHoverEvent(event);
7127 }
7128
7129 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007130 * Returns true if the view has a child to which it has recently sent
7131 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
7132 * it does not have a hovered child, then it must be the innermost hovered view.
7133 * @hide
7134 */
7135 protected boolean hasHoveredChild() {
7136 return false;
7137 }
7138
7139 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007140 * Dispatch a generic motion event to the view under the first pointer.
7141 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007142 * Do not call this method directly.
7143 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007144 * </p>
7145 *
7146 * @param event The motion event to be dispatched.
7147 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007148 */
7149 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
7150 return false;
7151 }
7152
7153 /**
7154 * Dispatch a generic motion event to the currently focused view.
7155 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007156 * Do not call this method directly.
7157 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007158 * </p>
7159 *
7160 * @param event The motion event to be dispatched.
7161 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007162 */
7163 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
7164 return false;
7165 }
7166
7167 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007168 * Dispatch a pointer event.
7169 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007170 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
7171 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
7172 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08007173 * and should not be expected to handle other pointing device features.
7174 * </p>
7175 *
7176 * @param event The motion event to be dispatched.
7177 * @return True if the event was handled by the view, false otherwise.
7178 * @hide
7179 */
7180 public final boolean dispatchPointerEvent(MotionEvent event) {
7181 if (event.isTouchEvent()) {
7182 return dispatchTouchEvent(event);
7183 } else {
7184 return dispatchGenericMotionEvent(event);
7185 }
7186 }
7187
7188 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007189 * Called when the window containing this view gains or loses window focus.
7190 * ViewGroups should override to route to their children.
7191 *
7192 * @param hasFocus True if the window containing this view now has focus,
7193 * false otherwise.
7194 */
7195 public void dispatchWindowFocusChanged(boolean hasFocus) {
7196 onWindowFocusChanged(hasFocus);
7197 }
7198
7199 /**
7200 * Called when the window containing this view gains or loses focus. Note
7201 * that this is separate from view focus: to receive key events, both
7202 * your view and its window must have focus. If a window is displayed
7203 * on top of yours that takes input focus, then your own window will lose
7204 * focus but the view focus will remain unchanged.
7205 *
7206 * @param hasWindowFocus True if the window containing this view now has
7207 * focus, false otherwise.
7208 */
7209 public void onWindowFocusChanged(boolean hasWindowFocus) {
7210 InputMethodManager imm = InputMethodManager.peekInstance();
7211 if (!hasWindowFocus) {
7212 if (isPressed()) {
7213 setPressed(false);
7214 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07007215 if (imm != null && (mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007216 imm.focusOut(this);
7217 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007218 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08007219 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07007220 onFocusLost();
Dianne Hackborn4702a852012-08-17 15:18:29 -07007221 } else if (imm != null && (mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007222 imm.focusIn(this);
7223 }
7224 refreshDrawableState();
7225 }
7226
7227 /**
7228 * Returns true if this view is in a window that currently has window focus.
7229 * Note that this is not the same as the view itself having focus.
7230 *
7231 * @return True if this view is in a window that currently has window focus.
7232 */
7233 public boolean hasWindowFocus() {
7234 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
7235 }
7236
7237 /**
Adam Powell326d8082009-12-09 15:10:07 -08007238 * Dispatch a view visibility change down the view hierarchy.
7239 * ViewGroups should override to route to their children.
7240 * @param changedView The view whose visibility changed. Could be 'this' or
7241 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007242 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7243 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007244 */
7245 protected void dispatchVisibilityChanged(View changedView, int visibility) {
7246 onVisibilityChanged(changedView, visibility);
7247 }
7248
7249 /**
7250 * Called when the visibility of the view or an ancestor of the view is changed.
7251 * @param changedView The view whose visibility changed. Could be 'this' or
7252 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007253 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7254 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007255 */
7256 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007257 if (visibility == VISIBLE) {
7258 if (mAttachInfo != null) {
7259 initialAwakenScrollBars();
7260 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07007261 mPrivateFlags |= PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH;
Adam Powell8568c3a2010-04-19 14:26:11 -07007262 }
7263 }
Adam Powell326d8082009-12-09 15:10:07 -08007264 }
7265
7266 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08007267 * Dispatch a hint about whether this view is displayed. For instance, when
7268 * a View moves out of the screen, it might receives a display hint indicating
7269 * the view is not displayed. Applications should not <em>rely</em> on this hint
7270 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007271 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007272 * @param hint A hint about whether or not this view is displayed:
7273 * {@link #VISIBLE} or {@link #INVISIBLE}.
7274 */
7275 public void dispatchDisplayHint(int hint) {
7276 onDisplayHint(hint);
7277 }
7278
7279 /**
7280 * Gives this view a hint about whether is displayed or not. For instance, when
7281 * a View moves out of the screen, it might receives a display hint indicating
7282 * the view is not displayed. Applications should not <em>rely</em> on this hint
7283 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007284 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007285 * @param hint A hint about whether or not this view is displayed:
7286 * {@link #VISIBLE} or {@link #INVISIBLE}.
7287 */
7288 protected void onDisplayHint(int hint) {
7289 }
7290
7291 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007292 * Dispatch a window visibility change down the view hierarchy.
7293 * ViewGroups should override to route to their children.
7294 *
7295 * @param visibility The new visibility of the window.
7296 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007297 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007298 */
7299 public void dispatchWindowVisibilityChanged(int visibility) {
7300 onWindowVisibilityChanged(visibility);
7301 }
7302
7303 /**
7304 * Called when the window containing has change its visibility
7305 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
7306 * that this tells you whether or not your window is being made visible
7307 * to the window manager; this does <em>not</em> tell you whether or not
7308 * your window is obscured by other windows on the screen, even if it
7309 * is itself visible.
7310 *
7311 * @param visibility The new visibility of the window.
7312 */
7313 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007314 if (visibility == VISIBLE) {
7315 initialAwakenScrollBars();
7316 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007317 }
7318
7319 /**
7320 * Returns the current visibility of the window this view is attached to
7321 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
7322 *
7323 * @return Returns the current visibility of the view's window.
7324 */
7325 public int getWindowVisibility() {
7326 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
7327 }
7328
7329 /**
7330 * Retrieve the overall visible display size in which the window this view is
7331 * attached to has been positioned in. This takes into account screen
7332 * decorations above the window, for both cases where the window itself
7333 * is being position inside of them or the window is being placed under
7334 * then and covered insets are used for the window to position its content
7335 * inside. In effect, this tells you the available area where content can
7336 * be placed and remain visible to users.
7337 *
7338 * <p>This function requires an IPC back to the window manager to retrieve
7339 * the requested information, so should not be used in performance critical
7340 * code like drawing.
7341 *
7342 * @param outRect Filled in with the visible display frame. If the view
7343 * is not attached to a window, this is simply the raw display size.
7344 */
7345 public void getWindowVisibleDisplayFrame(Rect outRect) {
7346 if (mAttachInfo != null) {
7347 try {
7348 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
7349 } catch (RemoteException e) {
7350 return;
7351 }
7352 // XXX This is really broken, and probably all needs to be done
7353 // in the window manager, and we need to know more about whether
7354 // we want the area behind or in front of the IME.
7355 final Rect insets = mAttachInfo.mVisibleInsets;
7356 outRect.left += insets.left;
7357 outRect.top += insets.top;
7358 outRect.right -= insets.right;
7359 outRect.bottom -= insets.bottom;
7360 return;
7361 }
Jeff Brown98365d72012-08-19 20:30:52 -07007362 Display d = DisplayManager.getInstance().getRealDisplay(Display.DEFAULT_DISPLAY);
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07007363 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007364 }
7365
7366 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007367 * Dispatch a notification about a resource configuration change down
7368 * the view hierarchy.
7369 * ViewGroups should override to route to their children.
7370 *
7371 * @param newConfig The new resource configuration.
7372 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007373 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007374 */
7375 public void dispatchConfigurationChanged(Configuration newConfig) {
7376 onConfigurationChanged(newConfig);
7377 }
7378
7379 /**
7380 * Called when the current configuration of the resources being used
7381 * by the application have changed. You can use this to decide when
7382 * to reload resources that can changed based on orientation and other
7383 * configuration characterstics. You only need to use this if you are
7384 * not relying on the normal {@link android.app.Activity} mechanism of
7385 * recreating the activity instance upon a configuration change.
7386 *
7387 * @param newConfig The new resource configuration.
7388 */
7389 protected void onConfigurationChanged(Configuration newConfig) {
7390 }
7391
7392 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007393 * Private function to aggregate all per-view attributes in to the view
7394 * root.
7395 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007396 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7397 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007398 }
7399
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007400 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7401 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08007402 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007403 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007404 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007405 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007406 ListenerInfo li = mListenerInfo;
7407 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007408 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007409 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007410 }
7411 }
7412
7413 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08007414 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007415 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08007416 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
7417 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007418 ai.mRecomputeGlobalAttributes = true;
7419 }
7420 }
7421 }
7422
7423 /**
7424 * Returns whether the device is currently in touch mode. Touch mode is entered
7425 * once the user begins interacting with the device by touch, and affects various
7426 * things like whether focus is always visible to the user.
7427 *
7428 * @return Whether the device is in touch mode.
7429 */
7430 @ViewDebug.ExportedProperty
7431 public boolean isInTouchMode() {
7432 if (mAttachInfo != null) {
7433 return mAttachInfo.mInTouchMode;
7434 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07007435 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007436 }
7437 }
7438
7439 /**
7440 * Returns the context the view is running in, through which it can
7441 * access the current theme, resources, etc.
7442 *
7443 * @return The view's Context.
7444 */
7445 @ViewDebug.CapturedViewProperty
7446 public final Context getContext() {
7447 return mContext;
7448 }
7449
7450 /**
7451 * Handle a key event before it is processed by any input method
7452 * associated with the view hierarchy. This can be used to intercept
7453 * key events in special situations before the IME consumes them; a
7454 * typical example would be handling the BACK key to update the application's
7455 * UI instead of allowing the IME to see it and close itself.
7456 *
7457 * @param keyCode The value in event.getKeyCode().
7458 * @param event Description of the key event.
7459 * @return If you handled the event, return true. If you want to allow the
7460 * event to be handled by the next receiver, return false.
7461 */
7462 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7463 return false;
7464 }
7465
7466 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007467 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7468 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007469 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7470 * is released, if the view is enabled and clickable.
7471 *
Jean Chalard405bc512012-05-29 19:12:34 +09007472 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7473 * although some may elect to do so in some situations. Do not rely on this to
7474 * catch software key presses.
7475 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007476 * @param keyCode A key code that represents the button pressed, from
7477 * {@link android.view.KeyEvent}.
7478 * @param event The KeyEvent object that defines the button action.
7479 */
7480 public boolean onKeyDown(int keyCode, KeyEvent event) {
7481 boolean result = false;
7482
7483 switch (keyCode) {
7484 case KeyEvent.KEYCODE_DPAD_CENTER:
7485 case KeyEvent.KEYCODE_ENTER: {
7486 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7487 return true;
7488 }
7489 // Long clickable items don't necessarily have to be clickable
7490 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7491 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7492 (event.getRepeatCount() == 0)) {
7493 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007494 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007495 return true;
7496 }
7497 break;
7498 }
7499 }
7500 return result;
7501 }
7502
7503 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007504 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7505 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7506 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007507 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7508 * although some may elect to do so in some situations. Do not rely on this to
7509 * catch software key presses.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007510 */
7511 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7512 return false;
7513 }
7514
7515 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007516 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7517 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007518 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7519 * {@link KeyEvent#KEYCODE_ENTER} is released.
Jean Chalard405bc512012-05-29 19:12:34 +09007520 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7521 * although some may elect to do so in some situations. Do not rely on this to
7522 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007523 *
7524 * @param keyCode A key code that represents the button pressed, from
7525 * {@link android.view.KeyEvent}.
7526 * @param event The KeyEvent object that defines the button action.
7527 */
7528 public boolean onKeyUp(int keyCode, KeyEvent event) {
7529 boolean result = false;
7530
7531 switch (keyCode) {
7532 case KeyEvent.KEYCODE_DPAD_CENTER:
7533 case KeyEvent.KEYCODE_ENTER: {
7534 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7535 return true;
7536 }
7537 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7538 setPressed(false);
7539
7540 if (!mHasPerformedLongPress) {
7541 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007542 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007543
7544 result = performClick();
7545 }
7546 }
7547 break;
7548 }
7549 }
7550 return result;
7551 }
7552
7553 /**
7554 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7555 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7556 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007557 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7558 * although some may elect to do so in some situations. Do not rely on this to
7559 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007560 *
7561 * @param keyCode A key code that represents the button pressed, from
7562 * {@link android.view.KeyEvent}.
7563 * @param repeatCount The number of times the action was made.
7564 * @param event The KeyEvent object that defines the button action.
7565 */
7566 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7567 return false;
7568 }
7569
7570 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007571 * Called on the focused view when a key shortcut event is not handled.
7572 * Override this method to implement local key shortcuts for the View.
7573 * Key shortcuts can also be implemented by setting the
7574 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007575 *
7576 * @param keyCode The value in event.getKeyCode().
7577 * @param event Description of the key event.
7578 * @return If you handled the event, return true. If you want to allow the
7579 * event to be handled by the next receiver, return false.
7580 */
7581 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7582 return false;
7583 }
7584
7585 /**
7586 * Check whether the called view is a text editor, in which case it
7587 * would make sense to automatically display a soft input window for
7588 * it. Subclasses should override this if they implement
7589 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007590 * a call on that method would return a non-null InputConnection, and
7591 * they are really a first-class editor that the user would normally
7592 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007593 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007594 * <p>The default implementation always returns false. This does
7595 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7596 * will not be called or the user can not otherwise perform edits on your
7597 * view; it is just a hint to the system that this is not the primary
7598 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007599 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007600 * @return Returns true if this view is a text editor, else false.
7601 */
7602 public boolean onCheckIsTextEditor() {
7603 return false;
7604 }
Romain Guy8506ab42009-06-11 17:35:47 -07007605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007606 /**
7607 * Create a new InputConnection for an InputMethod to interact
7608 * with the view. The default implementation returns null, since it doesn't
7609 * support input methods. You can override this to implement such support.
7610 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007611 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007612 * <p>When implementing this, you probably also want to implement
7613 * {@link #onCheckIsTextEditor()} to indicate you will return a
7614 * non-null InputConnection.
7615 *
7616 * @param outAttrs Fill in with attribute information about the connection.
7617 */
7618 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7619 return null;
7620 }
7621
7622 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007623 * Called by the {@link android.view.inputmethod.InputMethodManager}
7624 * when a view who is not the current
7625 * input connection target is trying to make a call on the manager. The
7626 * default implementation returns false; you can override this to return
7627 * true for certain views if you are performing InputConnection proxying
7628 * to them.
7629 * @param view The View that is making the InputMethodManager call.
7630 * @return Return true to allow the call, false to reject.
7631 */
7632 public boolean checkInputConnectionProxy(View view) {
7633 return false;
7634 }
Romain Guy8506ab42009-06-11 17:35:47 -07007635
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007636 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007637 * Show the context menu for this view. It is not safe to hold on to the
7638 * menu after returning from this method.
7639 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007640 * You should normally not overload this method. Overload
7641 * {@link #onCreateContextMenu(ContextMenu)} or define an
7642 * {@link OnCreateContextMenuListener} to add items to the context menu.
7643 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007644 * @param menu The context menu to populate
7645 */
7646 public void createContextMenu(ContextMenu menu) {
7647 ContextMenuInfo menuInfo = getContextMenuInfo();
7648
7649 // Sets the current menu info so all items added to menu will have
7650 // my extra info set.
7651 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7652
7653 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007654 ListenerInfo li = mListenerInfo;
7655 if (li != null && li.mOnCreateContextMenuListener != null) {
7656 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007657 }
7658
7659 // Clear the extra information so subsequent items that aren't mine don't
7660 // have my extra info.
7661 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7662
7663 if (mParent != null) {
7664 mParent.createContextMenu(menu);
7665 }
7666 }
7667
7668 /**
7669 * Views should implement this if they have extra information to associate
7670 * with the context menu. The return result is supplied as a parameter to
7671 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7672 * callback.
7673 *
7674 * @return Extra information about the item for which the context menu
7675 * should be shown. This information will vary across different
7676 * subclasses of View.
7677 */
7678 protected ContextMenuInfo getContextMenuInfo() {
7679 return null;
7680 }
7681
7682 /**
7683 * Views should implement this if the view itself is going to add items to
7684 * the context menu.
7685 *
7686 * @param menu the context menu to populate
7687 */
7688 protected void onCreateContextMenu(ContextMenu menu) {
7689 }
7690
7691 /**
7692 * Implement this method to handle trackball motion events. The
7693 * <em>relative</em> movement of the trackball since the last event
7694 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
7695 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
7696 * that a movement of 1 corresponds to the user pressing one DPAD key (so
7697 * they will often be fractional values, representing the more fine-grained
7698 * movement information available from a trackball).
7699 *
7700 * @param event The motion event.
7701 * @return True if the event was handled, false otherwise.
7702 */
7703 public boolean onTrackballEvent(MotionEvent event) {
7704 return false;
7705 }
7706
7707 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08007708 * Implement this method to handle generic motion events.
7709 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08007710 * Generic motion events describe joystick movements, mouse hovers, track pad
7711 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08007712 * {@link MotionEvent#getSource() source} of the motion event specifies
7713 * the class of input that was received. Implementations of this method
7714 * must examine the bits in the source before processing the event.
7715 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007716 * </p><p>
7717 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7718 * are delivered to the view under the pointer. All other generic motion events are
7719 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08007720 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07007721 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08007722 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007723 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
7724 * // process the joystick movement...
7725 * return true;
7726 * }
7727 * }
7728 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
7729 * switch (event.getAction()) {
7730 * case MotionEvent.ACTION_HOVER_MOVE:
7731 * // process the mouse hover movement...
7732 * return true;
7733 * case MotionEvent.ACTION_SCROLL:
7734 * // process the scroll wheel movement...
7735 * return true;
7736 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08007737 * }
7738 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07007739 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08007740 *
7741 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08007742 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08007743 */
7744 public boolean onGenericMotionEvent(MotionEvent event) {
7745 return false;
7746 }
7747
7748 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007749 * Implement this method to handle hover events.
7750 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07007751 * This method is called whenever a pointer is hovering into, over, or out of the
7752 * bounds of a view and the view is not currently being touched.
7753 * Hover events are represented as pointer events with action
7754 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
7755 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
7756 * </p>
7757 * <ul>
7758 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
7759 * when the pointer enters the bounds of the view.</li>
7760 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
7761 * when the pointer has already entered the bounds of the view and has moved.</li>
7762 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
7763 * when the pointer has exited the bounds of the view or when the pointer is
7764 * about to go down due to a button click, tap, or similar user action that
7765 * causes the view to be touched.</li>
7766 * </ul>
7767 * <p>
7768 * The view should implement this method to return true to indicate that it is
7769 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08007770 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07007771 * The default implementation calls {@link #setHovered} to update the hovered state
7772 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07007773 * is enabled and is clickable. The default implementation also sends hover
7774 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08007775 * </p>
7776 *
7777 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07007778 * @return True if the view handled the hover event.
7779 *
7780 * @see #isHovered
7781 * @see #setHovered
7782 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007783 */
7784 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007785 // The root view may receive hover (or touch) events that are outside the bounds of
7786 // the window. This code ensures that we only send accessibility events for
7787 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07007788 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007789 if (!mSendingHoverAccessibilityEvents) {
7790 if ((action == MotionEvent.ACTION_HOVER_ENTER
7791 || action == MotionEvent.ACTION_HOVER_MOVE)
7792 && !hasHoveredChild()
7793 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07007794 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007795 mSendingHoverAccessibilityEvents = true;
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007796 }
7797 } else {
7798 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07007799 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007800 && !pointInView(event.getX(), event.getY()))) {
7801 mSendingHoverAccessibilityEvents = false;
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07007802 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007803 // If the window does not have input focus we take away accessibility
7804 // focus as soon as the user stop hovering over the view.
Jeff Brown59a422e2012-04-19 15:19:19 -07007805 if (mAttachInfo != null && !mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07007806 getViewRootImpl().setAccessibilityFocus(null, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007807 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007808 }
Jeff Browna1b24182011-07-28 13:38:24 -07007809 }
7810
Jeff Brown87b7f802011-06-21 18:35:45 -07007811 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007812 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07007813 case MotionEvent.ACTION_HOVER_ENTER:
7814 setHovered(true);
7815 break;
7816 case MotionEvent.ACTION_HOVER_EXIT:
7817 setHovered(false);
7818 break;
7819 }
Jeff Browna1b24182011-07-28 13:38:24 -07007820
7821 // Dispatch the event to onGenericMotionEvent before returning true.
7822 // This is to provide compatibility with existing applications that
7823 // handled HOVER_MOVE events in onGenericMotionEvent and that would
7824 // break because of the new default handling for hoverable views
7825 // in onHoverEvent.
7826 // Note that onGenericMotionEvent will be called by default when
7827 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
7828 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07007829 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08007830 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007831
Svetoslav Ganov736c2752011-04-22 18:30:36 -07007832 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08007833 }
7834
7835 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007836 * Returns true if the view should handle {@link #onHoverEvent}
7837 * by calling {@link #setHovered} to change its hovered state.
7838 *
7839 * @return True if the view is hoverable.
7840 */
7841 private boolean isHoverable() {
7842 final int viewFlags = mViewFlags;
7843 if ((viewFlags & ENABLED_MASK) == DISABLED) {
7844 return false;
7845 }
7846
7847 return (viewFlags & CLICKABLE) == CLICKABLE
7848 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
7849 }
7850
7851 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007852 * Returns true if the view is currently hovered.
7853 *
7854 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007855 *
7856 * @see #setHovered
7857 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007858 */
Jeff Brown10b62902011-06-20 16:40:37 -07007859 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08007860 public boolean isHovered() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07007861 return (mPrivateFlags & PFLAG_HOVERED) != 0;
Jeff Browna032cc02011-03-07 16:56:21 -08007862 }
7863
7864 /**
7865 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007866 * <p>
7867 * Calling this method also changes the drawable state of the view. This
7868 * enables the view to react to hover by using different drawable resources
7869 * to change its appearance.
7870 * </p><p>
7871 * The {@link #onHoverChanged} method is called when the hovered state changes.
7872 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08007873 *
7874 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007875 *
7876 * @see #isHovered
7877 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007878 */
7879 public void setHovered(boolean hovered) {
7880 if (hovered) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07007881 if ((mPrivateFlags & PFLAG_HOVERED) == 0) {
7882 mPrivateFlags |= PFLAG_HOVERED;
Jeff Browna032cc02011-03-07 16:56:21 -08007883 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007884 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08007885 }
7886 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07007887 if ((mPrivateFlags & PFLAG_HOVERED) != 0) {
7888 mPrivateFlags &= ~PFLAG_HOVERED;
Jeff Browna032cc02011-03-07 16:56:21 -08007889 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007890 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08007891 }
7892 }
7893 }
7894
7895 /**
Jeff Brown10b62902011-06-20 16:40:37 -07007896 * Implement this method to handle hover state changes.
7897 * <p>
7898 * This method is called whenever the hover state changes as a result of a
7899 * call to {@link #setHovered}.
7900 * </p>
7901 *
7902 * @param hovered The current hover state, as returned by {@link #isHovered}.
7903 *
7904 * @see #isHovered
7905 * @see #setHovered
7906 */
7907 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07007908 }
7909
7910 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007911 * Implement this method to handle touch screen motion events.
7912 *
7913 * @param event The motion event.
7914 * @return True if the event was handled, false otherwise.
7915 */
7916 public boolean onTouchEvent(MotionEvent event) {
7917 final int viewFlags = mViewFlags;
7918
7919 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07007920 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PFLAG_PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08007921 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007922 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007923 // A disabled view that is clickable still consumes the touch
7924 // events, it just doesn't respond to them.
7925 return (((viewFlags & CLICKABLE) == CLICKABLE ||
7926 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
7927 }
7928
7929 if (mTouchDelegate != null) {
7930 if (mTouchDelegate.onTouchEvent(event)) {
7931 return true;
7932 }
7933 }
7934
7935 if (((viewFlags & CLICKABLE) == CLICKABLE ||
7936 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
7937 switch (event.getAction()) {
7938 case MotionEvent.ACTION_UP:
Dianne Hackborn4702a852012-08-17 15:18:29 -07007939 boolean prepressed = (mPrivateFlags & PFLAG_PREPRESSED) != 0;
7940 if ((mPrivateFlags & PFLAG_PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007941 // take focus if we don't have it already and we should in
7942 // touch mode.
7943 boolean focusTaken = false;
7944 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
7945 focusTaken = requestFocus();
7946 }
7947
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007948 if (prepressed) {
7949 // The button is being released before we actually
7950 // showed it as pressed. Make it show the pressed
7951 // state now (before scheduling the click) to ensure
7952 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08007953 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007954 }
Joe Malin32736f02011-01-19 16:14:20 -08007955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007956 if (!mHasPerformedLongPress) {
7957 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007958 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007959
7960 // Only perform take click actions if we were in the pressed state
7961 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08007962 // Use a Runnable and post this rather than calling
7963 // performClick directly. This lets other visual state
7964 // of the view update before click actions start.
7965 if (mPerformClick == null) {
7966 mPerformClick = new PerformClick();
7967 }
7968 if (!post(mPerformClick)) {
7969 performClick();
7970 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007971 }
7972 }
7973
7974 if (mUnsetPressedState == null) {
7975 mUnsetPressedState = new UnsetPressedState();
7976 }
7977
Adam Powelle14579b2009-12-16 18:39:52 -08007978 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08007979 postDelayed(mUnsetPressedState,
7980 ViewConfiguration.getPressedStateDuration());
7981 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007982 // If the post failed, unpress right now
7983 mUnsetPressedState.run();
7984 }
Adam Powelle14579b2009-12-16 18:39:52 -08007985 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007986 }
7987 break;
7988
7989 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08007990 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007991
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07007992 if (performButtonActionOnTouchDown(event)) {
7993 break;
7994 }
7995
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007996 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07007997 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007998
7999 // For views inside a scrolling container, delay the pressed feedback for
8000 // a short period in case this is a scroll.
8001 if (isInScrollingContainer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008002 mPrivateFlags |= PFLAG_PREPRESSED;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008003 if (mPendingCheckForTap == null) {
8004 mPendingCheckForTap = new CheckForTap();
8005 }
8006 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
8007 } else {
8008 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08008009 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008010 checkForLongClick(0);
8011 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008012 break;
8013
8014 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08008015 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08008016 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008017 break;
8018
8019 case MotionEvent.ACTION_MOVE:
8020 final int x = (int) event.getX();
8021 final int y = (int) event.getY();
8022
8023 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07008024 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008025 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08008026 removeTapCallback();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008027 if ((mPrivateFlags & PFLAG_PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08008028 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05008029 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008030
Adam Powell4d6f0662012-02-21 15:11:11 -08008031 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008032 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008033 }
8034 break;
8035 }
8036 return true;
8037 }
8038
8039 return false;
8040 }
8041
8042 /**
Adam Powell10298662011-08-14 18:26:30 -07008043 * @hide
8044 */
8045 public boolean isInScrollingContainer() {
8046 ViewParent p = getParent();
8047 while (p != null && p instanceof ViewGroup) {
8048 if (((ViewGroup) p).shouldDelayChildPressedState()) {
8049 return true;
8050 }
8051 p = p.getParent();
8052 }
8053 return false;
8054 }
8055
8056 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05008057 * Remove the longpress detection timer.
8058 */
8059 private void removeLongPressCallback() {
8060 if (mPendingCheckForLongPress != null) {
8061 removeCallbacks(mPendingCheckForLongPress);
8062 }
8063 }
Adam Powell3cb8b632011-01-21 15:34:14 -08008064
8065 /**
8066 * Remove the pending click action
8067 */
8068 private void removePerformClickCallback() {
8069 if (mPerformClick != null) {
8070 removeCallbacks(mPerformClick);
8071 }
8072 }
8073
Adam Powelle14579b2009-12-16 18:39:52 -08008074 /**
Romain Guya440b002010-02-24 15:57:54 -08008075 * Remove the prepress detection timer.
8076 */
8077 private void removeUnsetPressCallback() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008078 if ((mPrivateFlags & PFLAG_PRESSED) != 0 && mUnsetPressedState != null) {
Romain Guya440b002010-02-24 15:57:54 -08008079 setPressed(false);
8080 removeCallbacks(mUnsetPressedState);
8081 }
8082 }
8083
8084 /**
Adam Powelle14579b2009-12-16 18:39:52 -08008085 * Remove the tap detection timer.
8086 */
8087 private void removeTapCallback() {
8088 if (mPendingCheckForTap != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008089 mPrivateFlags &= ~PFLAG_PREPRESSED;
Adam Powelle14579b2009-12-16 18:39:52 -08008090 removeCallbacks(mPendingCheckForTap);
8091 }
8092 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05008093
8094 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008095 * Cancels a pending long press. Your subclass can use this if you
8096 * want the context menu to come up if the user presses and holds
8097 * at the same place, but you don't want it to come up if they press
8098 * and then move around enough to cause scrolling.
8099 */
8100 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05008101 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08008102
8103 /*
8104 * The prepressed state handled by the tap callback is a display
8105 * construct, but the tap callback will post a long press callback
8106 * less its own timeout. Remove it here.
8107 */
8108 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008109 }
8110
8111 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008112 * Remove the pending callback for sending a
8113 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
8114 */
8115 private void removeSendViewScrolledAccessibilityEventCallback() {
8116 if (mSendViewScrolledAccessibilityEvent != null) {
8117 removeCallbacks(mSendViewScrolledAccessibilityEvent);
Svetoslav Ganov4a812ae2012-05-29 16:46:10 -07008118 mSendViewScrolledAccessibilityEvent.mIsPending = false;
Svetoslav Ganova0156172011-06-26 17:55:44 -07008119 }
8120 }
8121
8122 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008123 * Sets the TouchDelegate for this View.
8124 */
8125 public void setTouchDelegate(TouchDelegate delegate) {
8126 mTouchDelegate = delegate;
8127 }
8128
8129 /**
8130 * Gets the TouchDelegate for this View.
8131 */
8132 public TouchDelegate getTouchDelegate() {
8133 return mTouchDelegate;
8134 }
8135
8136 /**
8137 * Set flags controlling behavior of this view.
8138 *
8139 * @param flags Constant indicating the value which should be set
8140 * @param mask Constant indicating the bit range that should be changed
8141 */
8142 void setFlags(int flags, int mask) {
8143 int old = mViewFlags;
8144 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
8145
8146 int changed = mViewFlags ^ old;
8147 if (changed == 0) {
8148 return;
8149 }
8150 int privateFlags = mPrivateFlags;
8151
8152 /* Check if the FOCUSABLE bit has changed */
8153 if (((changed & FOCUSABLE_MASK) != 0) &&
Dianne Hackborn4702a852012-08-17 15:18:29 -07008154 ((privateFlags & PFLAG_HAS_BOUNDS) !=0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008155 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
Dianne Hackborn4702a852012-08-17 15:18:29 -07008156 && ((privateFlags & PFLAG_FOCUSED) != 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008157 /* Give up focus if we are no longer focusable */
8158 clearFocus();
8159 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
Dianne Hackborn4702a852012-08-17 15:18:29 -07008160 && ((privateFlags & PFLAG_FOCUSED) == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008161 /*
8162 * Tell the view system that we are now available to take focus
8163 * if no one else already has it.
8164 */
8165 if (mParent != null) mParent.focusableViewAvailable(this);
8166 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008167 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8168 notifyAccessibilityStateChanged();
8169 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008170 }
8171
8172 if ((flags & VISIBILITY_MASK) == VISIBLE) {
8173 if ((changed & VISIBILITY_MASK) != 0) {
8174 /*
Chet Haase4324ead2011-08-24 21:31:03 -07008175 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07008176 * it was not visible. Marking it drawn ensures that the invalidation will
8177 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008178 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07008179 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07008180 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008181
8182 needGlobalAttributesUpdate(true);
8183
8184 // a view becoming visible is worth notifying the parent
8185 // about in case nothing has focus. even if this specific view
8186 // isn't focusable, it may contain something that is, so let
8187 // the root view try to give this focus if nothing else does.
8188 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
8189 mParent.focusableViewAvailable(this);
8190 }
8191 }
8192 }
8193
8194 /* Check if the GONE bit has changed */
8195 if ((changed & GONE) != 0) {
8196 needGlobalAttributesUpdate(false);
8197 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008198
Romain Guyecd80ee2009-12-03 17:13:02 -08008199 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
8200 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008201 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08008202 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07008203 if (mParent instanceof View) {
8204 // GONE views noop invalidation, so invalidate the parent
8205 ((View) mParent).invalidate(true);
8206 }
8207 // Mark the view drawn to ensure that it gets invalidated properly the next
8208 // time it is visible and gets invalidated
Dianne Hackborn4702a852012-08-17 15:18:29 -07008209 mPrivateFlags |= PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008210 }
8211 if (mAttachInfo != null) {
8212 mAttachInfo.mViewVisibilityChanged = true;
8213 }
8214 }
8215
8216 /* Check if the VISIBLE bit has changed */
8217 if ((changed & INVISIBLE) != 0) {
8218 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07008219 /*
8220 * If this view is becoming invisible, set the DRAWN flag so that
8221 * the next invalidate() will not be skipped.
8222 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07008223 mPrivateFlags |= PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008224
8225 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008226 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008227 if (getRootView() != this) {
8228 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008229 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008230 }
8231 }
8232 if (mAttachInfo != null) {
8233 mAttachInfo.mViewVisibilityChanged = true;
8234 }
8235 }
8236
Adam Powell326d8082009-12-09 15:10:07 -08008237 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07008238 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08008239 ((ViewGroup) mParent).onChildVisibilityChanged(this,
8240 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08008241 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07008242 } else if (mParent != null) {
8243 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07008244 }
Adam Powell326d8082009-12-09 15:10:07 -08008245 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
8246 }
8247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008248 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
8249 destroyDrawingCache();
8250 }
8251
8252 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
8253 destroyDrawingCache();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008254 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008255 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008256 }
8257
8258 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
8259 destroyDrawingCache();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008260 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008261 }
8262
8263 if ((changed & DRAW_MASK) != 0) {
8264 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07008265 if (mBackground != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008266 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
8267 mPrivateFlags |= PFLAG_ONLY_DRAWS_BACKGROUND;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008268 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008269 mPrivateFlags |= PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008270 }
8271 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008272 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008273 }
8274 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08008275 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008276 }
8277
8278 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08008279 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008280 mParent.recomputeViewAttributes(this);
8281 }
8282 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008283
8284 if (AccessibilityManager.getInstance(mContext).isEnabled()
8285 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
8286 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
8287 notifyAccessibilityStateChanged();
8288 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008289 }
8290
8291 /**
8292 * Change the view's z order in the tree, so it's on top of other sibling
8293 * views
8294 */
8295 public void bringToFront() {
8296 if (mParent != null) {
8297 mParent.bringChildToFront(this);
8298 }
8299 }
8300
8301 /**
8302 * This is called in response to an internal scroll in this view (i.e., the
8303 * view scrolled its own contents). This is typically as a result of
8304 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
8305 * called.
8306 *
8307 * @param l Current horizontal scroll origin.
8308 * @param t Current vertical scroll origin.
8309 * @param oldl Previous horizontal scroll origin.
8310 * @param oldt Previous vertical scroll origin.
8311 */
8312 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07008313 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8314 postSendViewScrolledAccessibilityEventCallback();
8315 }
8316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008317 mBackgroundSizeChanged = true;
8318
8319 final AttachInfo ai = mAttachInfo;
8320 if (ai != null) {
8321 ai.mViewScrollChanged = true;
8322 }
8323 }
8324
8325 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008326 * Interface definition for a callback to be invoked when the layout bounds of a view
8327 * changes due to layout processing.
8328 */
8329 public interface OnLayoutChangeListener {
8330 /**
8331 * Called when the focus state of a view has changed.
8332 *
8333 * @param v The view whose state has changed.
8334 * @param left The new value of the view's left property.
8335 * @param top The new value of the view's top property.
8336 * @param right The new value of the view's right property.
8337 * @param bottom The new value of the view's bottom property.
8338 * @param oldLeft The previous value of the view's left property.
8339 * @param oldTop The previous value of the view's top property.
8340 * @param oldRight The previous value of the view's right property.
8341 * @param oldBottom The previous value of the view's bottom property.
8342 */
8343 void onLayoutChange(View v, int left, int top, int right, int bottom,
8344 int oldLeft, int oldTop, int oldRight, int oldBottom);
8345 }
8346
8347 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008348 * This is called during layout when the size of this view has changed. If
8349 * you were just added to the view hierarchy, you're called with the old
8350 * values of 0.
8351 *
8352 * @param w Current width of this view.
8353 * @param h Current height of this view.
8354 * @param oldw Old width of this view.
8355 * @param oldh Old height of this view.
8356 */
8357 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
8358 }
8359
8360 /**
8361 * Called by draw to draw the child views. This may be overridden
8362 * by derived classes to gain control just before its children are drawn
8363 * (but after its own view has been drawn).
8364 * @param canvas the canvas on which to draw the view
8365 */
8366 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008368 }
8369
8370 /**
8371 * Gets the parent of this view. Note that the parent is a
8372 * ViewParent and not necessarily a View.
8373 *
8374 * @return Parent of this view.
8375 */
8376 public final ViewParent getParent() {
8377 return mParent;
8378 }
8379
8380 /**
Chet Haasecca2c982011-05-20 14:34:18 -07008381 * Set the horizontal scrolled position of your view. This will cause a call to
8382 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8383 * invalidated.
8384 * @param value the x position to scroll to
8385 */
8386 public void setScrollX(int value) {
8387 scrollTo(value, mScrollY);
8388 }
8389
8390 /**
8391 * Set the vertical scrolled position of your view. This will cause a call to
8392 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8393 * invalidated.
8394 * @param value the y position to scroll to
8395 */
8396 public void setScrollY(int value) {
8397 scrollTo(mScrollX, value);
8398 }
8399
8400 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008401 * Return the scrolled left position of this view. This is the left edge of
8402 * the displayed part of your view. You do not need to draw any pixels
8403 * farther left, since those are outside of the frame of your view on
8404 * screen.
8405 *
8406 * @return The left edge of the displayed part of your view, in pixels.
8407 */
8408 public final int getScrollX() {
8409 return mScrollX;
8410 }
8411
8412 /**
8413 * Return the scrolled top position of this view. This is the top edge of
8414 * the displayed part of your view. You do not need to draw any pixels above
8415 * it, since those are outside of the frame of your view on screen.
8416 *
8417 * @return The top edge of the displayed part of your view, in pixels.
8418 */
8419 public final int getScrollY() {
8420 return mScrollY;
8421 }
8422
8423 /**
8424 * Return the width of the your view.
8425 *
8426 * @return The width of your view, in pixels.
8427 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008428 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008429 public final int getWidth() {
8430 return mRight - mLeft;
8431 }
8432
8433 /**
8434 * Return the height of your view.
8435 *
8436 * @return The height of your view, in pixels.
8437 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008438 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008439 public final int getHeight() {
8440 return mBottom - mTop;
8441 }
8442
8443 /**
8444 * Return the visible drawing bounds of your view. Fills in the output
8445 * rectangle with the values from getScrollX(), getScrollY(),
8446 * getWidth(), and getHeight().
8447 *
8448 * @param outRect The (scrolled) drawing bounds of the view.
8449 */
8450 public void getDrawingRect(Rect outRect) {
8451 outRect.left = mScrollX;
8452 outRect.top = mScrollY;
8453 outRect.right = mScrollX + (mRight - mLeft);
8454 outRect.bottom = mScrollY + (mBottom - mTop);
8455 }
8456
8457 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008458 * Like {@link #getMeasuredWidthAndState()}, but only returns the
8459 * raw width component (that is the result is masked by
8460 * {@link #MEASURED_SIZE_MASK}).
8461 *
8462 * @return The raw measured width of this view.
8463 */
8464 public final int getMeasuredWidth() {
8465 return mMeasuredWidth & MEASURED_SIZE_MASK;
8466 }
8467
8468 /**
8469 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008470 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008471 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008472 * This should be used during measurement and layout calculations only. Use
8473 * {@link #getWidth()} to see how wide a view is after layout.
8474 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008475 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008476 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008477 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008478 return mMeasuredWidth;
8479 }
8480
8481 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008482 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8483 * raw width component (that is the result is masked by
8484 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008485 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008486 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008487 */
8488 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008489 return mMeasuredHeight & MEASURED_SIZE_MASK;
8490 }
8491
8492 /**
8493 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008494 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008495 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8496 * This should be used during measurement and layout calculations only. Use
8497 * {@link #getHeight()} to see how wide a view is after layout.
8498 *
8499 * @return The measured width of this view as a bit mask.
8500 */
8501 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008502 return mMeasuredHeight;
8503 }
8504
8505 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008506 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8507 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8508 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8509 * and the height component is at the shifted bits
8510 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8511 */
8512 public final int getMeasuredState() {
8513 return (mMeasuredWidth&MEASURED_STATE_MASK)
8514 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8515 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8516 }
8517
8518 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008519 * The transform matrix of this view, which is calculated based on the current
8520 * roation, scale, and pivot properties.
8521 *
8522 * @see #getRotation()
8523 * @see #getScaleX()
8524 * @see #getScaleY()
8525 * @see #getPivotX()
8526 * @see #getPivotY()
8527 * @return The current transform matrix for the view
8528 */
8529 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008530 if (mTransformationInfo != null) {
8531 updateMatrix();
8532 return mTransformationInfo.mMatrix;
8533 }
8534 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008535 }
8536
8537 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008538 * Utility function to determine if the value is far enough away from zero to be
8539 * considered non-zero.
8540 * @param value A floating point value to check for zero-ness
8541 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8542 */
8543 private static boolean nonzero(float value) {
8544 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8545 }
8546
8547 /**
Jeff Brown86671742010-09-30 20:00:15 -07008548 * Returns true if the transform matrix is the identity matrix.
8549 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008550 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008551 * @return True if the transform matrix is the identity matrix, false otherwise.
8552 */
Jeff Brown86671742010-09-30 20:00:15 -07008553 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008554 if (mTransformationInfo != null) {
8555 updateMatrix();
8556 return mTransformationInfo.mMatrixIsIdentity;
8557 }
8558 return true;
8559 }
8560
8561 void ensureTransformationInfo() {
8562 if (mTransformationInfo == null) {
8563 mTransformationInfo = new TransformationInfo();
8564 }
Jeff Brown86671742010-09-30 20:00:15 -07008565 }
8566
8567 /**
8568 * Recomputes the transform matrix if necessary.
8569 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008570 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008571 final TransformationInfo info = mTransformationInfo;
8572 if (info == null) {
8573 return;
8574 }
8575 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008576 // transform-related properties have changed since the last time someone
8577 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008578
8579 // Figure out if we need to update the pivot point
Dianne Hackborn4702a852012-08-17 15:18:29 -07008580 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008581 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8582 info.mPrevWidth = mRight - mLeft;
8583 info.mPrevHeight = mBottom - mTop;
8584 info.mPivotX = info.mPrevWidth / 2f;
8585 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008586 }
8587 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008588 info.mMatrix.reset();
8589 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8590 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8591 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8592 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008593 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008594 if (info.mCamera == null) {
8595 info.mCamera = new Camera();
8596 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008597 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008598 info.mCamera.save();
8599 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8600 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8601 info.mCamera.getMatrix(info.matrix3D);
8602 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8603 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8604 info.mPivotY + info.mTranslationY);
8605 info.mMatrix.postConcat(info.matrix3D);
8606 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008607 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008608 info.mMatrixDirty = false;
8609 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8610 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008611 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008612 }
8613
8614 /**
Tobias Duboisdefdb1e2010-12-15 11:35:30 +01008615 * When searching for a view to focus this rectangle is used when considering if this view is
8616 * a good candidate for receiving focus.
8617 *
8618 * By default, the rectangle is the {@link #getDrawingRect}) of the view.
8619 *
8620 * @param r The rectangle to fill in, in this view's coordinates.
8621 */
8622 public void getFocusRect(Rect r) {
8623 getDrawingRect(r);
8624 }
8625
8626 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008627 * Utility method to retrieve the inverse of the current mMatrix property.
8628 * We cache the matrix to avoid recalculating it when transform properties
8629 * have not changed.
8630 *
8631 * @return The inverse of the current matrix of this view.
8632 */
Jeff Brown86671742010-09-30 20:00:15 -07008633 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008634 final TransformationInfo info = mTransformationInfo;
8635 if (info != null) {
8636 updateMatrix();
8637 if (info.mInverseMatrixDirty) {
8638 if (info.mInverseMatrix == null) {
8639 info.mInverseMatrix = new Matrix();
8640 }
8641 info.mMatrix.invert(info.mInverseMatrix);
8642 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008643 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008644 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008645 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008646 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008647 }
8648
8649 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008650 * Gets the distance along the Z axis from the camera to this view.
8651 *
8652 * @see #setCameraDistance(float)
8653 *
8654 * @return The distance along the Z axis.
8655 */
8656 public float getCameraDistance() {
8657 ensureTransformationInfo();
8658 final float dpi = mResources.getDisplayMetrics().densityDpi;
8659 final TransformationInfo info = mTransformationInfo;
8660 if (info.mCamera == null) {
8661 info.mCamera = new Camera();
8662 info.matrix3D = new Matrix();
8663 }
8664 return -(info.mCamera.getLocationZ() * dpi);
8665 }
8666
8667 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008668 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8669 * views are drawn) from the camera to this view. The camera's distance
8670 * affects 3D transformations, for instance rotations around the X and Y
8671 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008672 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008673 * use a camera distance that's greater than the height (X axis rotation) or
8674 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008675 *
Romain Guya5364ee2011-02-24 14:46:04 -08008676 * <p>The distance of the camera from the view plane can have an affect on the
8677 * perspective distortion of the view when it is rotated around the x or y axis.
8678 * For example, a large distance will result in a large viewing angle, and there
8679 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008680 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008681 * also result in some drawing artifacts if the rotated view ends up partially
8682 * behind the camera (which is why the recommendation is to use a distance at
8683 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008684 *
Romain Guya5364ee2011-02-24 14:46:04 -08008685 * <p>The distance is expressed in "depth pixels." The default distance depends
8686 * on the screen density. For instance, on a medium density display, the
8687 * default distance is 1280. On a high density display, the default distance
8688 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008689 *
Romain Guya5364ee2011-02-24 14:46:04 -08008690 * <p>If you want to specify a distance that leads to visually consistent
8691 * results across various densities, use the following formula:</p>
8692 * <pre>
8693 * float scale = context.getResources().getDisplayMetrics().density;
8694 * view.setCameraDistance(distance * scale);
8695 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07008696 *
Romain Guya5364ee2011-02-24 14:46:04 -08008697 * <p>The density scale factor of a high density display is 1.5,
8698 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008699 *
Romain Guya5364ee2011-02-24 14:46:04 -08008700 * @param distance The distance in "depth pixels", if negative the opposite
8701 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07008702 *
8703 * @see #setRotationX(float)
8704 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08008705 */
8706 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008707 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08008708
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008709 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08008710 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008711 final TransformationInfo info = mTransformationInfo;
8712 if (info.mCamera == null) {
8713 info.mCamera = new Camera();
8714 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08008715 }
8716
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008717 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
8718 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08008719
Chet Haase9d1992d2012-03-13 11:03:25 -07008720 invalidateViewProperty(false, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008721 if (mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07008722 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08008723 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008724 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008725 // View was rejected last time it was drawn by its parent; this may have changed
8726 invalidateParentIfNeeded();
8727 }
Romain Guya5364ee2011-02-24 14:46:04 -08008728 }
8729
8730 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008731 * The degrees that the view is rotated around the pivot point.
8732 *
Philip Milne6c8ea062012-04-03 17:38:43 -07008733 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07008734 * @see #getPivotX()
8735 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008736 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008737 * @return The degrees of rotation.
8738 */
Chet Haasea5531132012-02-02 13:41:44 -08008739 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008740 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008741 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008742 }
8743
8744 /**
Chet Haase897247b2010-09-09 14:54:47 -07008745 * Sets the degrees that the view is rotated around the pivot point. Increasing values
8746 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07008747 *
8748 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008749 *
8750 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07008751 * @see #getPivotX()
8752 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008753 * @see #setRotationX(float)
8754 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08008755 *
8756 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07008757 */
8758 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008759 ensureTransformationInfo();
8760 final TransformationInfo info = mTransformationInfo;
8761 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008762 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008763 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008764 info.mRotation = rotation;
8765 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008766 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008767 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008768 mDisplayList.setRotation(rotation);
8769 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008770 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008771 // View was rejected last time it was drawn by its parent; this may have changed
8772 invalidateParentIfNeeded();
8773 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008774 }
8775 }
8776
8777 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008778 * The degrees that the view is rotated around the vertical axis through the pivot point.
8779 *
8780 * @see #getPivotX()
8781 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008782 * @see #setRotationY(float)
8783 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008784 * @return The degrees of Y rotation.
8785 */
Chet Haasea5531132012-02-02 13:41:44 -08008786 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008787 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008788 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008789 }
8790
8791 /**
Chet Haase897247b2010-09-09 14:54:47 -07008792 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
8793 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
8794 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008795 *
Romain Guya5364ee2011-02-24 14:46:04 -08008796 * When rotating large views, it is recommended to adjust the camera distance
8797 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008798 *
8799 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008800 *
8801 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07008802 * @see #getPivotX()
8803 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008804 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008805 * @see #setRotationX(float)
8806 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008807 *
8808 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07008809 */
8810 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008811 ensureTransformationInfo();
8812 final TransformationInfo info = mTransformationInfo;
8813 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008814 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008815 info.mRotationY = rotationY;
8816 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008817 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008818 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008819 mDisplayList.setRotationY(rotationY);
8820 }
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 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008825 }
8826 }
8827
8828 /**
8829 * The degrees that the view is rotated around the horizontal axis through the pivot point.
8830 *
8831 * @see #getPivotX()
8832 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008833 * @see #setRotationX(float)
8834 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008835 * @return The degrees of X rotation.
8836 */
Chet Haasea5531132012-02-02 13:41:44 -08008837 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008838 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008839 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008840 }
8841
8842 /**
Chet Haase897247b2010-09-09 14:54:47 -07008843 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
8844 * Increasing values result in clockwise rotation from the viewpoint of looking down the
8845 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008846 *
Romain Guya5364ee2011-02-24 14:46:04 -08008847 * When rotating large views, it is recommended to adjust the camera distance
8848 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008849 *
8850 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008851 *
8852 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07008853 * @see #getPivotX()
8854 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008855 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008856 * @see #setRotationY(float)
8857 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008858 *
8859 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07008860 */
8861 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008862 ensureTransformationInfo();
8863 final TransformationInfo info = mTransformationInfo;
8864 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008865 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008866 info.mRotationX = rotationX;
8867 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008868 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008869 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008870 mDisplayList.setRotationX(rotationX);
8871 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008872 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008873 // View was rejected last time it was drawn by its parent; this may have changed
8874 invalidateParentIfNeeded();
8875 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008876 }
8877 }
8878
8879 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008880 * The amount that the view is scaled in x around the pivot point, as a proportion of
8881 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
8882 *
Joe Onorato93162322010-09-16 15:42:01 -04008883 * <p>By default, this is 1.0f.
8884 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008885 * @see #getPivotX()
8886 * @see #getPivotY()
8887 * @return The scaling factor.
8888 */
Chet Haasea5531132012-02-02 13:41:44 -08008889 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008890 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008891 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008892 }
8893
8894 /**
8895 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
8896 * the view's unscaled width. A value of 1 means that no scaling is applied.
8897 *
8898 * @param scaleX The scaling factor.
8899 * @see #getPivotX()
8900 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008901 *
8902 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07008903 */
8904 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008905 ensureTransformationInfo();
8906 final TransformationInfo info = mTransformationInfo;
8907 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008908 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008909 info.mScaleX = scaleX;
8910 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008911 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008912 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008913 mDisplayList.setScaleX(scaleX);
8914 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008915 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008916 // View was rejected last time it was drawn by its parent; this may have changed
8917 invalidateParentIfNeeded();
8918 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008919 }
8920 }
8921
8922 /**
8923 * The amount that the view is scaled in y around the pivot point, as a proportion of
8924 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
8925 *
Joe Onorato93162322010-09-16 15:42:01 -04008926 * <p>By default, this is 1.0f.
8927 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008928 * @see #getPivotX()
8929 * @see #getPivotY()
8930 * @return The scaling factor.
8931 */
Chet Haasea5531132012-02-02 13:41:44 -08008932 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008933 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008934 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008935 }
8936
8937 /**
8938 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
8939 * the view's unscaled width. A value of 1 means that no scaling is applied.
8940 *
8941 * @param scaleY The scaling factor.
8942 * @see #getPivotX()
8943 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008944 *
8945 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07008946 */
8947 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008948 ensureTransformationInfo();
8949 final TransformationInfo info = mTransformationInfo;
8950 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008951 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008952 info.mScaleY = scaleY;
8953 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008954 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008955 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008956 mDisplayList.setScaleY(scaleY);
8957 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008958 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008959 // View was rejected last time it was drawn by its parent; this may have changed
8960 invalidateParentIfNeeded();
8961 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008962 }
8963 }
8964
8965 /**
8966 * The x location of the point around which the view is {@link #setRotation(float) rotated}
8967 * and {@link #setScaleX(float) scaled}.
8968 *
8969 * @see #getRotation()
8970 * @see #getScaleX()
8971 * @see #getScaleY()
8972 * @see #getPivotY()
8973 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008974 *
8975 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008976 */
Chet Haasea5531132012-02-02 13:41:44 -08008977 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008978 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008979 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008980 }
8981
8982 /**
8983 * Sets the x location of the point around which the view is
8984 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07008985 * By default, the pivot point is centered on the object.
8986 * Setting this property disables this behavior and causes the view to use only the
8987 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008988 *
8989 * @param pivotX The x location of the pivot point.
8990 * @see #getRotation()
8991 * @see #getScaleX()
8992 * @see #getScaleY()
8993 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008994 *
8995 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008996 */
8997 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008998 ensureTransformationInfo();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008999 mPrivateFlags |= PFLAG_PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009000 final TransformationInfo info = mTransformationInfo;
9001 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009002 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009003 info.mPivotX = pivotX;
9004 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009005 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009006 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009007 mDisplayList.setPivotX(pivotX);
9008 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009009 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009010 // View was rejected last time it was drawn by its parent; this may have changed
9011 invalidateParentIfNeeded();
9012 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009013 }
9014 }
9015
9016 /**
9017 * The y location of the point around which the view is {@link #setRotation(float) rotated}
9018 * and {@link #setScaleY(float) scaled}.
9019 *
9020 * @see #getRotation()
9021 * @see #getScaleX()
9022 * @see #getScaleY()
9023 * @see #getPivotY()
9024 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009025 *
9026 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009027 */
Chet Haasea5531132012-02-02 13:41:44 -08009028 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009029 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009030 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009031 }
9032
9033 /**
9034 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07009035 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
9036 * Setting this property disables this behavior and causes the view to use only the
9037 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009038 *
9039 * @param pivotY The y location of the pivot point.
9040 * @see #getRotation()
9041 * @see #getScaleX()
9042 * @see #getScaleY()
9043 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009044 *
9045 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009046 */
9047 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009048 ensureTransformationInfo();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009049 mPrivateFlags |= PFLAG_PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009050 final TransformationInfo info = mTransformationInfo;
9051 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009052 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009053 info.mPivotY = pivotY;
9054 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009055 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009056 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009057 mDisplayList.setPivotY(pivotY);
9058 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009059 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009060 // View was rejected last time it was drawn by its parent; this may have changed
9061 invalidateParentIfNeeded();
9062 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009063 }
9064 }
9065
9066 /**
9067 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
9068 * completely transparent and 1 means the view is completely opaque.
9069 *
Joe Onorato93162322010-09-16 15:42:01 -04009070 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07009071 * @return The opacity of the view.
9072 */
Chet Haasea5531132012-02-02 13:41:44 -08009073 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009074 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009075 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009076 }
9077
9078 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07009079 * Returns whether this View has content which overlaps. This function, intended to be
9080 * overridden by specific View types, is an optimization when alpha is set on a view. If
9081 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
9082 * and then composited it into place, which can be expensive. If the view has no overlapping
9083 * rendering, the view can draw each primitive with the appropriate alpha value directly.
9084 * An example of overlapping rendering is a TextView with a background image, such as a
9085 * Button. An example of non-overlapping rendering is a TextView with no background, or
9086 * an ImageView with only the foreground image. The default implementation returns true;
9087 * subclasses should override if they have cases which can be optimized.
9088 *
9089 * @return true if the content in this view might overlap, false otherwise.
9090 */
9091 public boolean hasOverlappingRendering() {
9092 return true;
9093 }
9094
9095 /**
Romain Guy171c5922011-01-06 10:04:23 -08009096 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
9097 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009098 *
Romain Guy171c5922011-01-06 10:04:23 -08009099 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
9100 * responsible for applying the opacity itself. Otherwise, calling this method is
9101 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08009102 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07009103 *
Chet Haasea5531132012-02-02 13:41:44 -08009104 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
9105 * performance implications. It is generally best to use the alpha property sparingly and
9106 * transiently, as in the case of fading animations.</p>
9107 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009108 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08009109 *
Joe Malin32736f02011-01-19 16:14:20 -08009110 * @see #setLayerType(int, android.graphics.Paint)
9111 *
Chet Haase73066682010-11-29 15:55:32 -08009112 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07009113 */
9114 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009115 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009116 if (mTransformationInfo.mAlpha != alpha) {
9117 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009118 if (onSetAlpha((int) (alpha * 255))) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009119 mPrivateFlags |= PFLAG_ALPHA_SET;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009120 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07009121 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009122 invalidate(true);
9123 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009124 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07009125 invalidateViewProperty(true, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07009126 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009127 mDisplayList.setAlpha(alpha);
9128 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009129 }
Chet Haaseed032702010-10-01 14:05:54 -07009130 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009131 }
9132
9133 /**
Chet Haasea00f3862011-02-22 06:34:40 -08009134 * Faster version of setAlpha() which performs the same steps except there are
9135 * no calls to invalidate(). The caller of this function should perform proper invalidation
9136 * on the parent and this object. The return value indicates whether the subclass handles
9137 * alpha (the return value for onSetAlpha()).
9138 *
9139 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009140 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
9141 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08009142 */
9143 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009144 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009145 if (mTransformationInfo.mAlpha != alpha) {
9146 mTransformationInfo.mAlpha = alpha;
9147 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
9148 if (subclassHandlesAlpha) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009149 mPrivateFlags |= PFLAG_ALPHA_SET;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009150 return true;
9151 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009152 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase1271e2c2012-04-20 09:54:27 -07009153 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009154 mDisplayList.setAlpha(alpha);
9155 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009156 }
Chet Haasea00f3862011-02-22 06:34:40 -08009157 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009158 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08009159 }
9160
9161 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009162 * Top position of this view relative to its parent.
9163 *
9164 * @return The top of this view, in pixels.
9165 */
9166 @ViewDebug.CapturedViewProperty
9167 public final int getTop() {
9168 return mTop;
9169 }
9170
9171 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009172 * Sets the top position of this view relative to its parent. This method is meant to be called
9173 * by the layout system and should not generally be called otherwise, because the property
9174 * may be changed at any time by the layout.
9175 *
9176 * @param top The top of this view, in pixels.
9177 */
9178 public final void setTop(int top) {
9179 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07009180 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009181 final boolean matrixIsIdentity = mTransformationInfo == null
9182 || mTransformationInfo.mMatrixIsIdentity;
9183 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009184 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009185 int minTop;
9186 int yLoc;
9187 if (top < mTop) {
9188 minTop = top;
9189 yLoc = top - mTop;
9190 } else {
9191 minTop = mTop;
9192 yLoc = 0;
9193 }
Chet Haasee9140a72011-02-16 16:23:29 -08009194 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009195 }
9196 } else {
9197 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009198 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009199 }
9200
Chet Haaseed032702010-10-01 14:05:54 -07009201 int width = mRight - mLeft;
9202 int oldHeight = mBottom - mTop;
9203
Chet Haase21cd1382010-09-01 17:42:29 -07009204 mTop = top;
Chet Haase1271e2c2012-04-20 09:54:27 -07009205 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009206 mDisplayList.setTop(mTop);
9207 }
Chet Haase21cd1382010-09-01 17:42:29 -07009208
Chet Haaseed032702010-10-01 14:05:54 -07009209 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9210
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009211 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009212 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009213 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009214 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009215 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009216 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009217 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009218 }
Chet Haase55dbb652010-12-21 20:15:08 -08009219 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009220 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009221 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009222 // View was rejected last time it was drawn by its parent; this may have changed
9223 invalidateParentIfNeeded();
9224 }
Chet Haase21cd1382010-09-01 17:42:29 -07009225 }
9226 }
9227
9228 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009229 * Bottom position of this view relative to its parent.
9230 *
9231 * @return The bottom of this view, in pixels.
9232 */
9233 @ViewDebug.CapturedViewProperty
9234 public final int getBottom() {
9235 return mBottom;
9236 }
9237
9238 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08009239 * True if this view has changed since the last time being drawn.
9240 *
9241 * @return The dirty state of this view.
9242 */
9243 public boolean isDirty() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009244 return (mPrivateFlags & PFLAG_DIRTY_MASK) != 0;
Michael Jurkadab559a2011-01-04 20:31:51 -08009245 }
9246
9247 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009248 * Sets the bottom position of this view relative to its parent. This method is meant to be
9249 * called by the layout system and should not generally be called otherwise, because the
9250 * property may be changed at any time by the layout.
9251 *
9252 * @param bottom The bottom of this view, in pixels.
9253 */
9254 public final void setBottom(int bottom) {
9255 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07009256 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009257 final boolean matrixIsIdentity = mTransformationInfo == null
9258 || mTransformationInfo.mMatrixIsIdentity;
9259 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009260 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009261 int maxBottom;
9262 if (bottom < mBottom) {
9263 maxBottom = mBottom;
9264 } else {
9265 maxBottom = bottom;
9266 }
Chet Haasee9140a72011-02-16 16:23:29 -08009267 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009268 }
9269 } else {
9270 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009271 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009272 }
9273
Chet Haaseed032702010-10-01 14:05:54 -07009274 int width = mRight - mLeft;
9275 int oldHeight = mBottom - mTop;
9276
Chet Haase21cd1382010-09-01 17:42:29 -07009277 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -07009278 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009279 mDisplayList.setBottom(mBottom);
9280 }
Chet Haase21cd1382010-09-01 17:42:29 -07009281
Chet Haaseed032702010-10-01 14:05:54 -07009282 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9283
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009284 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009285 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009286 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009287 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009288 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009289 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009290 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009291 }
Chet Haase55dbb652010-12-21 20:15:08 -08009292 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009293 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009294 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009295 // View was rejected last time it was drawn by its parent; this may have changed
9296 invalidateParentIfNeeded();
9297 }
Chet Haase21cd1382010-09-01 17:42:29 -07009298 }
9299 }
9300
9301 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009302 * Left position of this view relative to its parent.
9303 *
9304 * @return The left edge of this view, in pixels.
9305 */
9306 @ViewDebug.CapturedViewProperty
9307 public final int getLeft() {
9308 return mLeft;
9309 }
9310
9311 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009312 * Sets the left position of this view relative to its parent. This method is meant to be called
9313 * by the layout system and should not generally be called otherwise, because the property
9314 * may be changed at any time by the layout.
9315 *
9316 * @param left The bottom of this view, in pixels.
9317 */
9318 public final void setLeft(int left) {
9319 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07009320 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009321 final boolean matrixIsIdentity = mTransformationInfo == null
9322 || mTransformationInfo.mMatrixIsIdentity;
9323 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009324 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009325 int minLeft;
9326 int xLoc;
9327 if (left < mLeft) {
9328 minLeft = left;
9329 xLoc = left - mLeft;
9330 } else {
9331 minLeft = mLeft;
9332 xLoc = 0;
9333 }
Chet Haasee9140a72011-02-16 16:23:29 -08009334 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009335 }
9336 } else {
9337 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009338 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009339 }
9340
Chet Haaseed032702010-10-01 14:05:54 -07009341 int oldWidth = mRight - mLeft;
9342 int height = mBottom - mTop;
9343
Chet Haase21cd1382010-09-01 17:42:29 -07009344 mLeft = left;
Chet Haase1271e2c2012-04-20 09:54:27 -07009345 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009346 mDisplayList.setLeft(left);
9347 }
Chet Haase21cd1382010-09-01 17:42:29 -07009348
Chet Haaseed032702010-10-01 14:05:54 -07009349 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9350
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009351 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009352 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009353 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009354 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009355 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009356 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009357 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009358 }
Chet Haase55dbb652010-12-21 20:15:08 -08009359 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009360 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009361 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009362 // View was rejected last time it was drawn by its parent; this may have changed
9363 invalidateParentIfNeeded();
9364 }
Chet Haase21cd1382010-09-01 17:42:29 -07009365 }
9366 }
9367
9368 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009369 * Right position of this view relative to its parent.
9370 *
9371 * @return The right edge of this view, in pixels.
9372 */
9373 @ViewDebug.CapturedViewProperty
9374 public final int getRight() {
9375 return mRight;
9376 }
9377
9378 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009379 * Sets the right position of this view relative to its parent. This method is meant to be called
9380 * by the layout system and should not generally be called otherwise, because the property
9381 * may be changed at any time by the layout.
9382 *
9383 * @param right The bottom of this view, in pixels.
9384 */
9385 public final void setRight(int right) {
9386 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07009387 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009388 final boolean matrixIsIdentity = mTransformationInfo == null
9389 || mTransformationInfo.mMatrixIsIdentity;
9390 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009391 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009392 int maxRight;
9393 if (right < mRight) {
9394 maxRight = mRight;
9395 } else {
9396 maxRight = right;
9397 }
Chet Haasee9140a72011-02-16 16:23:29 -08009398 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009399 }
9400 } else {
9401 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009402 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009403 }
9404
Chet Haaseed032702010-10-01 14:05:54 -07009405 int oldWidth = mRight - mLeft;
9406 int height = mBottom - mTop;
9407
Chet Haase21cd1382010-09-01 17:42:29 -07009408 mRight = right;
Chet Haase1271e2c2012-04-20 09:54:27 -07009409 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009410 mDisplayList.setRight(mRight);
9411 }
Chet Haase21cd1382010-09-01 17:42:29 -07009412
Chet Haaseed032702010-10-01 14:05:54 -07009413 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9414
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009415 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009416 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009417 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009418 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009419 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009420 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009421 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009422 }
Chet Haase55dbb652010-12-21 20:15:08 -08009423 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009424 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009425 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009426 // View was rejected last time it was drawn by its parent; this may have changed
9427 invalidateParentIfNeeded();
9428 }
Chet Haase21cd1382010-09-01 17:42:29 -07009429 }
9430 }
9431
9432 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009433 * The visual x position of this view, in pixels. This is equivalent to the
9434 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08009435 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07009436 *
Chet Haasedf030d22010-07-30 17:22:38 -07009437 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009438 */
Chet Haasea5531132012-02-02 13:41:44 -08009439 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009440 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009441 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009442 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009443
Chet Haasedf030d22010-07-30 17:22:38 -07009444 /**
9445 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
9446 * {@link #setTranslationX(float) translationX} property to be the difference between
9447 * the x value passed in and the current {@link #getLeft() left} property.
9448 *
9449 * @param x The visual x position of this view, in pixels.
9450 */
9451 public void setX(float x) {
9452 setTranslationX(x - mLeft);
9453 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009454
Chet Haasedf030d22010-07-30 17:22:38 -07009455 /**
9456 * The visual y position of this view, in pixels. This is equivalent to the
9457 * {@link #setTranslationY(float) translationY} property plus the current
9458 * {@link #getTop() top} property.
9459 *
9460 * @return The visual y position of this view, in pixels.
9461 */
Chet Haasea5531132012-02-02 13:41:44 -08009462 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009463 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009464 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009465 }
9466
9467 /**
9468 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
9469 * {@link #setTranslationY(float) translationY} property to be the difference between
9470 * the y value passed in and the current {@link #getTop() top} property.
9471 *
9472 * @param y The visual y position of this view, in pixels.
9473 */
9474 public void setY(float y) {
9475 setTranslationY(y - mTop);
9476 }
9477
9478
9479 /**
9480 * The horizontal location of this view relative to its {@link #getLeft() left} position.
9481 * This position is post-layout, in addition to wherever the object's
9482 * layout placed it.
9483 *
9484 * @return The horizontal position of this view relative to its left position, in pixels.
9485 */
Chet Haasea5531132012-02-02 13:41:44 -08009486 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009487 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009488 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07009489 }
9490
9491 /**
9492 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
9493 * This effectively positions the object post-layout, in addition to wherever the object's
9494 * layout placed it.
9495 *
9496 * @param translationX The horizontal position of this view relative to its left position,
9497 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009498 *
9499 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07009500 */
9501 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009502 ensureTransformationInfo();
9503 final TransformationInfo info = mTransformationInfo;
9504 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07009505 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009506 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009507 info.mTranslationX = translationX;
9508 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009509 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009510 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009511 mDisplayList.setTranslationX(translationX);
9512 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009513 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009514 // View was rejected last time it was drawn by its parent; this may have changed
9515 invalidateParentIfNeeded();
9516 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009517 }
9518 }
9519
9520 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009521 * The horizontal location of this view relative to its {@link #getTop() top} position.
9522 * This position is post-layout, in addition to wherever the object's
9523 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009524 *
Chet Haasedf030d22010-07-30 17:22:38 -07009525 * @return The vertical position of this view relative to its top position,
9526 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009527 */
Chet Haasea5531132012-02-02 13:41:44 -08009528 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009529 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009530 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009531 }
9532
9533 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009534 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9535 * This effectively positions the object post-layout, in addition to wherever the object's
9536 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009537 *
Chet Haasedf030d22010-07-30 17:22:38 -07009538 * @param translationY The vertical position of this view relative to its top position,
9539 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009540 *
9541 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009542 */
Chet Haasedf030d22010-07-30 17:22:38 -07009543 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009544 ensureTransformationInfo();
9545 final TransformationInfo info = mTransformationInfo;
9546 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009547 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009548 info.mTranslationY = translationY;
9549 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009550 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009551 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009552 mDisplayList.setTranslationY(translationY);
9553 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009554 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009555 // View was rejected last time it was drawn by its parent; this may have changed
9556 invalidateParentIfNeeded();
9557 }
Chet Haasedf030d22010-07-30 17:22:38 -07009558 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009559 }
9560
9561 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009562 * Hit rectangle in parent's coordinates
9563 *
9564 * @param outRect The hit rectangle of the view.
9565 */
9566 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009567 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009568 final TransformationInfo info = mTransformationInfo;
9569 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009570 outRect.set(mLeft, mTop, mRight, mBottom);
9571 } else {
9572 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009573 tmpRect.set(-info.mPivotX, -info.mPivotY,
9574 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9575 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009576 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9577 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009578 }
9579 }
9580
9581 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009582 * Determines whether the given point, in local coordinates is inside the view.
9583 */
9584 /*package*/ final boolean pointInView(float localX, float localY) {
9585 return localX >= 0 && localX < (mRight - mLeft)
9586 && localY >= 0 && localY < (mBottom - mTop);
9587 }
9588
9589 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009590 * Utility method to determine whether the given point, in local coordinates,
9591 * is inside the view, where the area of the view is expanded by the slop factor.
9592 * This method is called while processing touch-move events to determine if the event
9593 * is still within the view.
9594 */
9595 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009596 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009597 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009598 }
9599
9600 /**
9601 * When a view has focus and the user navigates away from it, the next view is searched for
9602 * starting from the rectangle filled in by this method.
9603 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009604 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
9605 * of the view. However, if your view maintains some idea of internal selection,
9606 * such as a cursor, or a selected row or column, you should override this method and
9607 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009608 *
9609 * @param r The rectangle to fill in, in this view's coordinates.
9610 */
9611 public void getFocusedRect(Rect r) {
9612 getDrawingRect(r);
9613 }
9614
9615 /**
9616 * If some part of this view is not clipped by any of its parents, then
9617 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009618 * coordinates (without taking possible View rotations into account), offset
9619 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9620 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009621 *
9622 * @param r If true is returned, r holds the global coordinates of the
9623 * visible portion of this view.
9624 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9625 * between this view and its root. globalOffet may be null.
9626 * @return true if r is non-empty (i.e. part of the view is visible at the
9627 * root level.
9628 */
9629 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9630 int width = mRight - mLeft;
9631 int height = mBottom - mTop;
9632 if (width > 0 && height > 0) {
9633 r.set(0, 0, width, height);
9634 if (globalOffset != null) {
9635 globalOffset.set(-mScrollX, -mScrollY);
9636 }
9637 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9638 }
9639 return false;
9640 }
9641
9642 public final boolean getGlobalVisibleRect(Rect r) {
9643 return getGlobalVisibleRect(r, null);
9644 }
9645
9646 public final boolean getLocalVisibleRect(Rect r) {
Romain Guyab4c4f4f2012-05-06 13:11:24 -07009647 final Point offset = mAttachInfo != null ? mAttachInfo.mPoint : new Point();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009648 if (getGlobalVisibleRect(r, offset)) {
9649 r.offset(-offset.x, -offset.y); // make r local
9650 return true;
9651 }
9652 return false;
9653 }
9654
9655 /**
9656 * Offset this view's vertical location by the specified number of pixels.
9657 *
9658 * @param offset the number of pixels to offset the view by
9659 */
9660 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009661 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009662 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009663 final boolean matrixIsIdentity = mTransformationInfo == null
9664 || mTransformationInfo.mMatrixIsIdentity;
9665 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009666 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009667 invalidateViewProperty(false, false);
9668 } else {
9669 final ViewParent p = mParent;
9670 if (p != null && mAttachInfo != null) {
9671 final Rect r = mAttachInfo.mTmpInvalRect;
9672 int minTop;
9673 int maxBottom;
9674 int yLoc;
9675 if (offset < 0) {
9676 minTop = mTop + offset;
9677 maxBottom = mBottom;
9678 yLoc = offset;
9679 } else {
9680 minTop = mTop;
9681 maxBottom = mBottom + offset;
9682 yLoc = 0;
9683 }
9684 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
9685 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009686 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009687 }
9688 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009689 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009690 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009691
Chet Haasec3aa3612010-06-17 08:50:37 -07009692 mTop += offset;
9693 mBottom += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009694 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009695 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009696 invalidateViewProperty(false, false);
9697 } else {
9698 if (!matrixIsIdentity) {
9699 invalidateViewProperty(false, true);
9700 }
9701 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009702 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009703 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009704 }
9705
9706 /**
9707 * Offset this view's horizontal location by the specified amount of pixels.
9708 *
9709 * @param offset the numer of pixels to offset the view by
9710 */
9711 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009712 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009713 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009714 final boolean matrixIsIdentity = mTransformationInfo == null
9715 || mTransformationInfo.mMatrixIsIdentity;
9716 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009717 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009718 invalidateViewProperty(false, false);
9719 } else {
9720 final ViewParent p = mParent;
9721 if (p != null && mAttachInfo != null) {
9722 final Rect r = mAttachInfo.mTmpInvalRect;
9723 int minLeft;
9724 int maxRight;
9725 if (offset < 0) {
9726 minLeft = mLeft + offset;
9727 maxRight = mRight;
9728 } else {
9729 minLeft = mLeft;
9730 maxRight = mRight + offset;
9731 }
9732 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
9733 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009734 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009735 }
9736 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009737 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009738 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009739
Chet Haasec3aa3612010-06-17 08:50:37 -07009740 mLeft += offset;
9741 mRight += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009742 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009743 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009744 invalidateViewProperty(false, false);
9745 } else {
9746 if (!matrixIsIdentity) {
9747 invalidateViewProperty(false, true);
9748 }
9749 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009750 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009751 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009752 }
9753
9754 /**
9755 * Get the LayoutParams associated with this view. All views should have
9756 * layout parameters. These supply parameters to the <i>parent</i> of this
9757 * view specifying how it should be arranged. There are many subclasses of
9758 * ViewGroup.LayoutParams, and these correspond to the different subclasses
9759 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08009760 *
9761 * This method may return null if this View is not attached to a parent
9762 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
9763 * was not invoked successfully. When a View is attached to a parent
9764 * ViewGroup, this method must not return null.
9765 *
9766 * @return The LayoutParams associated with this view, or null if no
9767 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009768 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07009769 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009770 public ViewGroup.LayoutParams getLayoutParams() {
9771 return mLayoutParams;
9772 }
9773
9774 /**
9775 * Set the layout parameters associated with this view. These supply
9776 * parameters to the <i>parent</i> of this view specifying how it should be
9777 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
9778 * correspond to the different subclasses of ViewGroup that are responsible
9779 * for arranging their children.
9780 *
Romain Guy01c174b2011-02-22 11:51:06 -08009781 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009782 */
9783 public void setLayoutParams(ViewGroup.LayoutParams params) {
9784 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08009785 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009786 }
9787 mLayoutParams = params;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07009788 resolveLayoutParams();
Philip Milned7dd8902012-01-26 16:55:30 -08009789 if (mParent instanceof ViewGroup) {
9790 ((ViewGroup) mParent).onSetLayoutParams(this, params);
9791 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009792 requestLayout();
9793 }
9794
9795 /**
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07009796 * Resolve the layout parameters depending on the resolved layout direction
9797 */
9798 private void resolveLayoutParams() {
9799 if (mLayoutParams != null) {
9800 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
9801 }
9802 }
9803
9804 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009805 * Set the scrolled position of your view. This will cause a call to
9806 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9807 * invalidated.
9808 * @param x the x position to scroll to
9809 * @param y the y position to scroll to
9810 */
9811 public void scrollTo(int x, int y) {
9812 if (mScrollX != x || mScrollY != y) {
9813 int oldX = mScrollX;
9814 int oldY = mScrollY;
9815 mScrollX = x;
9816 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009817 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009818 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07009819 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009820 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -07009821 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009822 }
9823 }
9824
9825 /**
9826 * Move the scrolled position of your view. This will cause a call to
9827 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9828 * invalidated.
9829 * @param x the amount of pixels to scroll by horizontally
9830 * @param y the amount of pixels to scroll by vertically
9831 */
9832 public void scrollBy(int x, int y) {
9833 scrollTo(mScrollX + x, mScrollY + y);
9834 }
9835
9836 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009837 * <p>Trigger the scrollbars to draw. When invoked this method starts an
9838 * animation to fade the scrollbars out after a default delay. If a subclass
9839 * provides animated scrolling, the start delay should equal the duration
9840 * of the scrolling animation.</p>
9841 *
9842 * <p>The animation starts only if at least one of the scrollbars is
9843 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
9844 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9845 * this method returns true, and false otherwise. If the animation is
9846 * started, this method calls {@link #invalidate()}; in that case the
9847 * caller should not call {@link #invalidate()}.</p>
9848 *
9849 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07009850 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07009851 *
9852 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
9853 * and {@link #scrollTo(int, int)}.</p>
9854 *
9855 * @return true if the animation is played, false otherwise
9856 *
9857 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07009858 * @see #scrollBy(int, int)
9859 * @see #scrollTo(int, int)
9860 * @see #isHorizontalScrollBarEnabled()
9861 * @see #isVerticalScrollBarEnabled()
9862 * @see #setHorizontalScrollBarEnabled(boolean)
9863 * @see #setVerticalScrollBarEnabled(boolean)
9864 */
9865 protected boolean awakenScrollBars() {
9866 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07009867 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07009868 }
9869
9870 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07009871 * Trigger the scrollbars to draw.
9872 * This method differs from awakenScrollBars() only in its default duration.
9873 * initialAwakenScrollBars() will show the scroll bars for longer than
9874 * usual to give the user more of a chance to notice them.
9875 *
9876 * @return true if the animation is played, false otherwise.
9877 */
9878 private boolean initialAwakenScrollBars() {
9879 return mScrollCache != null &&
9880 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
9881 }
9882
9883 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009884 * <p>
9885 * Trigger the scrollbars to draw. When invoked this method starts an
9886 * animation to fade the scrollbars out after a fixed delay. If a subclass
9887 * provides animated scrolling, the start delay should equal the duration of
9888 * the scrolling animation.
9889 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009890 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009891 * <p>
9892 * The animation starts only if at least one of the scrollbars is enabled,
9893 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9894 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9895 * this method returns true, and false otherwise. If the animation is
9896 * started, this method calls {@link #invalidate()}; in that case the caller
9897 * should not call {@link #invalidate()}.
9898 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009899 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009900 * <p>
9901 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07009902 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07009903 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009904 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009905 * @param startDelay the delay, in milliseconds, after which the animation
9906 * should start; when the delay is 0, the animation starts
9907 * immediately
9908 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009909 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009910 * @see #scrollBy(int, int)
9911 * @see #scrollTo(int, int)
9912 * @see #isHorizontalScrollBarEnabled()
9913 * @see #isVerticalScrollBarEnabled()
9914 * @see #setHorizontalScrollBarEnabled(boolean)
9915 * @see #setVerticalScrollBarEnabled(boolean)
9916 */
9917 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07009918 return awakenScrollBars(startDelay, true);
9919 }
Joe Malin32736f02011-01-19 16:14:20 -08009920
Mike Cleron290947b2009-09-29 18:34:32 -07009921 /**
9922 * <p>
9923 * Trigger the scrollbars to draw. When invoked this method starts an
9924 * animation to fade the scrollbars out after a fixed delay. If a subclass
9925 * provides animated scrolling, the start delay should equal the duration of
9926 * the scrolling animation.
9927 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009928 *
Mike Cleron290947b2009-09-29 18:34:32 -07009929 * <p>
9930 * The animation starts only if at least one of the scrollbars is enabled,
9931 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9932 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9933 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08009934 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07009935 * is set to true; in that case the caller
9936 * should not call {@link #invalidate()}.
9937 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009938 *
Mike Cleron290947b2009-09-29 18:34:32 -07009939 * <p>
9940 * This method should be invoked everytime a subclass directly updates the
9941 * scroll parameters.
9942 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009943 *
Mike Cleron290947b2009-09-29 18:34:32 -07009944 * @param startDelay the delay, in milliseconds, after which the animation
9945 * should start; when the delay is 0, the animation starts
9946 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08009947 *
Mike Cleron290947b2009-09-29 18:34:32 -07009948 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08009949 *
Mike Cleron290947b2009-09-29 18:34:32 -07009950 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009951 *
Mike Cleron290947b2009-09-29 18:34:32 -07009952 * @see #scrollBy(int, int)
9953 * @see #scrollTo(int, int)
9954 * @see #isHorizontalScrollBarEnabled()
9955 * @see #isVerticalScrollBarEnabled()
9956 * @see #setHorizontalScrollBarEnabled(boolean)
9957 * @see #setVerticalScrollBarEnabled(boolean)
9958 */
9959 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07009960 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08009961
Mike Cleronf116bf82009-09-27 19:14:12 -07009962 if (scrollCache == null || !scrollCache.fadeScrollBars) {
9963 return false;
9964 }
9965
9966 if (scrollCache.scrollBar == null) {
9967 scrollCache.scrollBar = new ScrollBarDrawable();
9968 }
9969
9970 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
9971
Mike Cleron290947b2009-09-29 18:34:32 -07009972 if (invalidate) {
9973 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009974 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -07009975 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009976
9977 if (scrollCache.state == ScrollabilityCache.OFF) {
9978 // FIXME: this is copied from WindowManagerService.
9979 // We should get this value from the system when it
9980 // is possible to do so.
9981 final int KEY_REPEAT_FIRST_DELAY = 750;
9982 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
9983 }
9984
9985 // Tell mScrollCache when we should start fading. This may
9986 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07009987 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07009988 scrollCache.fadeStartTime = fadeStartTime;
9989 scrollCache.state = ScrollabilityCache.ON;
9990
9991 // Schedule our fader to run, unscheduling any old ones first
9992 if (mAttachInfo != null) {
9993 mAttachInfo.mHandler.removeCallbacks(scrollCache);
9994 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
9995 }
9996
9997 return true;
9998 }
9999
10000 return false;
10001 }
10002
10003 /**
Chet Haaseaceafe62011-08-26 15:44:33 -070010004 * Do not invalidate views which are not visible and which are not running an animation. They
10005 * will not get drawn and they should not set dirty flags as if they will be drawn
10006 */
10007 private boolean skipInvalidate() {
10008 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
10009 (!(mParent instanceof ViewGroup) ||
10010 !((ViewGroup) mParent).isViewTransitioning(this));
10011 }
10012 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010013 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010014 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
10015 * in the future. This must be called from a UI thread. To call from a non-UI
10016 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010017 *
10018 * WARNING: This method is destructive to dirty.
10019 * @param dirty the rectangle representing the bounds of the dirty region
10020 */
10021 public void invalidate(Rect dirty) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010022 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010023 return;
10024 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010025 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10026 (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID ||
10027 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED) {
10028 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
10029 mPrivateFlags |= PFLAG_INVALIDATED;
10030 mPrivateFlags |= PFLAG_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010031 final ViewParent p = mParent;
10032 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010033 //noinspection PointlessBooleanExpression,ConstantConditions
10034 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10035 if (p != null && ai != null && ai.mHardwareAccelerated) {
10036 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010037 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010038 p.invalidateChild(this, null);
10039 return;
10040 }
Romain Guyaf636eb2010-12-09 17:47:21 -080010041 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010042 if (p != null && ai != null) {
10043 final int scrollX = mScrollX;
10044 final int scrollY = mScrollY;
10045 final Rect r = ai.mTmpInvalRect;
10046 r.set(dirty.left - scrollX, dirty.top - scrollY,
10047 dirty.right - scrollX, dirty.bottom - scrollY);
10048 mParent.invalidateChild(this, r);
10049 }
10050 }
10051 }
10052
10053 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010054 * 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 -080010055 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -070010056 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
10057 * will be called at some point in the future. This must be called from
10058 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010059 * @param l the left position of the dirty region
10060 * @param t the top position of the dirty region
10061 * @param r the right position of the dirty region
10062 * @param b the bottom position of the dirty region
10063 */
10064 public void invalidate(int l, int t, int r, int b) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010065 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010066 return;
10067 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010068 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10069 (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID ||
10070 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED) {
10071 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
10072 mPrivateFlags |= PFLAG_INVALIDATED;
10073 mPrivateFlags |= PFLAG_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010074 final ViewParent p = mParent;
10075 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010076 //noinspection PointlessBooleanExpression,ConstantConditions
10077 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10078 if (p != null && ai != null && ai.mHardwareAccelerated) {
10079 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010080 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010081 p.invalidateChild(this, null);
10082 return;
10083 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010084 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010085 if (p != null && ai != null && l < r && t < b) {
10086 final int scrollX = mScrollX;
10087 final int scrollY = mScrollY;
10088 final Rect tmpr = ai.mTmpInvalRect;
10089 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
10090 p.invalidateChild(this, tmpr);
10091 }
10092 }
10093 }
10094
10095 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010096 * Invalidate the whole view. If the view is visible,
10097 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
10098 * the future. This must be called from a UI thread. To call from a non-UI thread,
10099 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010100 */
10101 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -070010102 invalidate(true);
10103 }
Joe Malin32736f02011-01-19 16:14:20 -080010104
Chet Haaseed032702010-10-01 14:05:54 -070010105 /**
10106 * This is where the invalidate() work actually happens. A full invalidate()
10107 * causes the drawing cache to be invalidated, but this function can be called with
10108 * invalidateCache set to false to skip that invalidation step for cases that do not
10109 * need it (for example, a component that remains at the same dimensions with the same
10110 * content).
10111 *
10112 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
10113 * well. This is usually true for a full invalidate, but may be set to false if the
10114 * View's contents or dimensions have not changed.
10115 */
Romain Guy849d0a32011-02-01 17:20:48 -080010116 void invalidate(boolean invalidateCache) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010117 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010118 return;
10119 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010120 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10121 (invalidateCache && (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID) ||
10122 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED || isOpaque() != mLastIsOpaque) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010123 mLastIsOpaque = isOpaque();
Dianne Hackborn4702a852012-08-17 15:18:29 -070010124 mPrivateFlags &= ~PFLAG_DRAWN;
10125 mPrivateFlags |= PFLAG_DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -070010126 if (invalidateCache) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010127 mPrivateFlags |= PFLAG_INVALIDATED;
10128 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Chet Haaseed032702010-10-01 14:05:54 -070010129 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010130 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -070010131 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -080010132 //noinspection PointlessBooleanExpression,ConstantConditions
10133 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10134 if (p != null && ai != null && ai.mHardwareAccelerated) {
10135 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010136 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010137 p.invalidateChild(this, null);
10138 return;
10139 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010140 }
Michael Jurkaebefea42010-11-15 16:04:01 -080010141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010142 if (p != null && ai != null) {
10143 final Rect r = ai.mTmpInvalRect;
10144 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10145 // Don't call invalidate -- we don't want to internally scroll
10146 // our own bounds
10147 p.invalidateChild(this, r);
10148 }
10149 }
10150 }
10151
10152 /**
Chet Haase9d1992d2012-03-13 11:03:25 -070010153 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
10154 * set any flags or handle all of the cases handled by the default invalidation methods.
10155 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
10156 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
10157 * walk up the hierarchy, transforming the dirty rect as necessary.
10158 *
10159 * The method also handles normal invalidation logic if display list properties are not
10160 * being used in this view. The invalidateParent and forceRedraw flags are used by that
10161 * backup approach, to handle these cases used in the various property-setting methods.
10162 *
10163 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
10164 * are not being used in this view
10165 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
10166 * list properties are not being used in this view
10167 */
10168 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010169 if (mDisplayList == null || (mPrivateFlags & PFLAG_DRAW_ANIMATION) == PFLAG_DRAW_ANIMATION) {
Chet Haase9d1992d2012-03-13 11:03:25 -070010170 if (invalidateParent) {
10171 invalidateParentCaches();
10172 }
10173 if (forceRedraw) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010174 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Chet Haase9d1992d2012-03-13 11:03:25 -070010175 }
10176 invalidate(false);
10177 } else {
10178 final AttachInfo ai = mAttachInfo;
10179 final ViewParent p = mParent;
10180 if (p != null && ai != null) {
10181 final Rect r = ai.mTmpInvalRect;
10182 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10183 if (mParent instanceof ViewGroup) {
10184 ((ViewGroup) mParent).invalidateChildFast(this, r);
10185 } else {
10186 mParent.invalidateChild(this, r);
10187 }
10188 }
10189 }
10190 }
10191
10192 /**
10193 * Utility method to transform a given Rect by the current matrix of this view.
10194 */
10195 void transformRect(final Rect rect) {
10196 if (!getMatrix().isIdentity()) {
10197 RectF boundingRect = mAttachInfo.mTmpTransformRect;
10198 boundingRect.set(rect);
10199 getMatrix().mapRect(boundingRect);
10200 rect.set((int) (boundingRect.left - 0.5f),
10201 (int) (boundingRect.top - 0.5f),
10202 (int) (boundingRect.right + 0.5f),
10203 (int) (boundingRect.bottom + 0.5f));
10204 }
10205 }
10206
10207 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -080010208 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -080010209 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10210 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -080010211 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
10212 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -080010213 *
10214 * @hide
10215 */
Romain Guy0fd89bf2011-01-26 15:41:30 -080010216 protected void invalidateParentCaches() {
10217 if (mParent instanceof View) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010218 ((View) mParent).mPrivateFlags |= PFLAG_INVALIDATED;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010219 }
10220 }
Joe Malin32736f02011-01-19 16:14:20 -080010221
Romain Guy0fd89bf2011-01-26 15:41:30 -080010222 /**
10223 * Used to indicate that the parent of this view should be invalidated. This functionality
10224 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10225 * which is necessary when various parent-managed properties of the view change, such as
10226 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
10227 * an invalidation event to the parent.
10228 *
10229 * @hide
10230 */
10231 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -080010232 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010233 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -080010234 }
10235 }
10236
10237 /**
Romain Guy24443ea2009-05-11 11:56:30 -070010238 * Indicates whether this View is opaque. An opaque View guarantees that it will
10239 * draw all the pixels overlapping its bounds using a fully opaque color.
10240 *
10241 * Subclasses of View should override this method whenever possible to indicate
10242 * whether an instance is opaque. Opaque Views are treated in a special way by
10243 * the View hierarchy, possibly allowing it to perform optimizations during
10244 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -070010245 *
Romain Guy24443ea2009-05-11 11:56:30 -070010246 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -070010247 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010248 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -070010249 public boolean isOpaque() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010250 return (mPrivateFlags & PFLAG_OPAQUE_MASK) == PFLAG_OPAQUE_MASK &&
Romain Guyf8773082012-07-12 18:01:00 -070010251 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1.0f) >= 1.0f);
Romain Guy8f1344f52009-05-15 16:03:59 -070010252 }
10253
Adam Powell20232d02010-12-08 21:08:53 -080010254 /**
10255 * @hide
10256 */
10257 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -070010258 // Opaque if:
10259 // - Has a background
10260 // - Background is opaque
10261 // - Doesn't have scrollbars or scrollbars are inside overlay
10262
Philip Milne6c8ea062012-04-03 17:38:43 -070010263 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010264 mPrivateFlags |= PFLAG_OPAQUE_BACKGROUND;
Romain Guy8f1344f52009-05-15 16:03:59 -070010265 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010266 mPrivateFlags &= ~PFLAG_OPAQUE_BACKGROUND;
Romain Guy8f1344f52009-05-15 16:03:59 -070010267 }
10268
10269 final int flags = mViewFlags;
10270 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
10271 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010272 mPrivateFlags |= PFLAG_OPAQUE_SCROLLBARS;
Romain Guy8f1344f52009-05-15 16:03:59 -070010273 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010274 mPrivateFlags &= ~PFLAG_OPAQUE_SCROLLBARS;
Romain Guy8f1344f52009-05-15 16:03:59 -070010275 }
10276 }
10277
10278 /**
10279 * @hide
10280 */
10281 protected boolean hasOpaqueScrollbars() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010282 return (mPrivateFlags & PFLAG_OPAQUE_SCROLLBARS) == PFLAG_OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -070010283 }
10284
10285 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010286 * @return A handler associated with the thread running the View. This
10287 * handler can be used to pump events in the UI events queue.
10288 */
10289 public Handler getHandler() {
10290 if (mAttachInfo != null) {
10291 return mAttachInfo.mHandler;
10292 }
10293 return null;
10294 }
10295
10296 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080010297 * Gets the view root associated with the View.
10298 * @return The view root, or null if none.
10299 * @hide
10300 */
10301 public ViewRootImpl getViewRootImpl() {
10302 if (mAttachInfo != null) {
10303 return mAttachInfo.mViewRootImpl;
10304 }
10305 return null;
10306 }
10307
10308 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010309 * <p>Causes the Runnable to be added to the message queue.
10310 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010311 *
Romain Guye63a4f32011-08-11 11:33:31 -070010312 * <p>This method can be invoked from outside of the UI thread
10313 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010314 *
10315 * @param action The Runnable that will be executed.
10316 *
10317 * @return Returns true if the Runnable was successfully placed in to the
10318 * message queue. Returns false on failure, usually because the
10319 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010320 *
10321 * @see #postDelayed
10322 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010323 */
10324 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010325 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010326 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010327 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010328 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010329 // Assume that post will succeed later
10330 ViewRootImpl.getRunQueue().post(action);
10331 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010332 }
10333
10334 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010335 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010336 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -070010337 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010338 *
Romain Guye63a4f32011-08-11 11:33:31 -070010339 * <p>This method can be invoked from outside of the UI thread
10340 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010341 *
10342 * @param action The Runnable that will be executed.
10343 * @param delayMillis The delay (in milliseconds) until the Runnable
10344 * will be executed.
10345 *
10346 * @return true if the Runnable was successfully placed in to the
10347 * message queue. Returns false on failure, usually because the
10348 * looper processing the message queue is exiting. Note that a
10349 * result of true does not mean the Runnable will be processed --
10350 * if the looper is quit before the delivery time of the message
10351 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010352 *
10353 * @see #post
10354 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010355 */
10356 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010357 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010358 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010359 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010360 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010361 // Assume that post will succeed later
10362 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10363 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010364 }
10365
10366 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010367 * <p>Causes the Runnable to execute on the next animation time step.
10368 * The runnable will be run on the user interface thread.</p>
10369 *
10370 * <p>This method can be invoked from outside of the UI thread
10371 * only when this View is attached to a window.</p>
10372 *
10373 * @param action The Runnable that will be executed.
10374 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010375 * @see #postOnAnimationDelayed
10376 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010377 */
10378 public void postOnAnimation(Runnable action) {
10379 final AttachInfo attachInfo = mAttachInfo;
10380 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010381 attachInfo.mViewRootImpl.mChoreographer.postCallback(
10382 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010383 } else {
10384 // Assume that post will succeed later
10385 ViewRootImpl.getRunQueue().post(action);
10386 }
10387 }
10388
10389 /**
10390 * <p>Causes the Runnable to execute on the next animation time step,
10391 * after the specified amount of time elapses.
10392 * The runnable will be run on the user interface thread.</p>
10393 *
10394 * <p>This method can be invoked from outside of the UI thread
10395 * only when this View is attached to a window.</p>
10396 *
10397 * @param action The Runnable that will be executed.
10398 * @param delayMillis The delay (in milliseconds) until the Runnable
10399 * will be executed.
10400 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010401 * @see #postOnAnimation
10402 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010403 */
10404 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
10405 final AttachInfo attachInfo = mAttachInfo;
10406 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010407 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
10408 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010409 } else {
10410 // Assume that post will succeed later
10411 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10412 }
10413 }
10414
10415 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010416 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010417 *
Romain Guye63a4f32011-08-11 11:33:31 -070010418 * <p>This method can be invoked from outside of the UI thread
10419 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010420 *
10421 * @param action The Runnable to remove from the message handling queue
10422 *
10423 * @return true if this view could ask the Handler to remove the Runnable,
10424 * false otherwise. When the returned value is true, the Runnable
10425 * may or may not have been actually removed from the message queue
10426 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010427 *
10428 * @see #post
10429 * @see #postDelayed
10430 * @see #postOnAnimation
10431 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010432 */
10433 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080010434 if (action != null) {
10435 final AttachInfo attachInfo = mAttachInfo;
10436 if (attachInfo != null) {
10437 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010438 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
10439 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -080010440 } else {
10441 // Assume that post will succeed later
10442 ViewRootImpl.getRunQueue().removeCallbacks(action);
10443 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010444 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010445 return true;
10446 }
10447
10448 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010449 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
10450 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010451 *
Romain Guye63a4f32011-08-11 11:33:31 -070010452 * <p>This method can be invoked from outside of the UI thread
10453 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010454 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010455 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010456 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010457 */
10458 public void postInvalidate() {
10459 postInvalidateDelayed(0);
10460 }
10461
10462 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010463 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10464 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010465 *
Romain Guye63a4f32011-08-11 11:33:31 -070010466 * <p>This method can be invoked from outside of the UI thread
10467 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010468 *
10469 * @param left The left coordinate of the rectangle to invalidate.
10470 * @param top The top coordinate of the rectangle to invalidate.
10471 * @param right The right coordinate of the rectangle to invalidate.
10472 * @param bottom The bottom coordinate of the rectangle to invalidate.
10473 *
10474 * @see #invalidate(int, int, int, int)
10475 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010476 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010477 */
10478 public void postInvalidate(int left, int top, int right, int bottom) {
10479 postInvalidateDelayed(0, left, top, right, bottom);
10480 }
10481
10482 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010483 * <p>Cause an invalidate to happen on a subsequent cycle through the event
10484 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010485 *
Romain Guye63a4f32011-08-11 11:33:31 -070010486 * <p>This method can be invoked from outside of the UI thread
10487 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010488 *
10489 * @param delayMilliseconds the duration in milliseconds to delay the
10490 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010491 *
10492 * @see #invalidate()
10493 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010494 */
10495 public void postInvalidateDelayed(long delayMilliseconds) {
10496 // We try only with the AttachInfo because there's no point in invalidating
10497 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010498 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010499 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010500 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010501 }
10502 }
10503
10504 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010505 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10506 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010507 *
Romain Guye63a4f32011-08-11 11:33:31 -070010508 * <p>This method can be invoked from outside of the UI thread
10509 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010510 *
10511 * @param delayMilliseconds the duration in milliseconds to delay the
10512 * invalidation by
10513 * @param left The left coordinate of the rectangle to invalidate.
10514 * @param top The top coordinate of the rectangle to invalidate.
10515 * @param right The right coordinate of the rectangle to invalidate.
10516 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010517 *
10518 * @see #invalidate(int, int, int, int)
10519 * @see #invalidate(Rect)
10520 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010521 */
10522 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
10523 int right, int bottom) {
10524
10525 // We try only with the AttachInfo because there's no point in invalidating
10526 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010527 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010528 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010529 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10530 info.target = this;
10531 info.left = left;
10532 info.top = top;
10533 info.right = right;
10534 info.bottom = bottom;
10535
Jeff Browna175a5b2012-02-15 19:18:31 -080010536 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010537 }
10538 }
10539
10540 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010541 * <p>Cause an invalidate to happen on the next animation time step, typically the
10542 * next display frame.</p>
10543 *
10544 * <p>This method can be invoked from outside of the UI thread
10545 * only when this View is attached to a window.</p>
10546 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010547 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010548 */
10549 public void postInvalidateOnAnimation() {
10550 // We try only with the AttachInfo because there's no point in invalidating
10551 // if we are not attached to our window
10552 final AttachInfo attachInfo = mAttachInfo;
10553 if (attachInfo != null) {
10554 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10555 }
10556 }
10557
10558 /**
10559 * <p>Cause an invalidate of the specified area to happen on the next animation
10560 * time step, typically the next display frame.</p>
10561 *
10562 * <p>This method can be invoked from outside of the UI thread
10563 * only when this View is attached to a window.</p>
10564 *
10565 * @param left The left coordinate of the rectangle to invalidate.
10566 * @param top The top coordinate of the rectangle to invalidate.
10567 * @param right The right coordinate of the rectangle to invalidate.
10568 * @param bottom The bottom coordinate of the rectangle to invalidate.
10569 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010570 * @see #invalidate(int, int, int, int)
10571 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010572 */
10573 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10574 // We try only with the AttachInfo because there's no point in invalidating
10575 // if we are not attached to our window
10576 final AttachInfo attachInfo = mAttachInfo;
10577 if (attachInfo != null) {
10578 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10579 info.target = this;
10580 info.left = left;
10581 info.top = top;
10582 info.right = right;
10583 info.bottom = bottom;
10584
10585 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10586 }
10587 }
10588
10589 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010590 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10591 * This event is sent at most once every
10592 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10593 */
10594 private void postSendViewScrolledAccessibilityEventCallback() {
10595 if (mSendViewScrolledAccessibilityEvent == null) {
10596 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10597 }
10598 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10599 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10600 postDelayed(mSendViewScrolledAccessibilityEvent,
10601 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10602 }
10603 }
10604
10605 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010606 * Called by a parent to request that a child update its values for mScrollX
10607 * and mScrollY if necessary. This will typically be done if the child is
10608 * animating a scroll using a {@link android.widget.Scroller Scroller}
10609 * object.
10610 */
10611 public void computeScroll() {
10612 }
10613
10614 /**
10615 * <p>Indicate whether the horizontal edges are faded when the view is
10616 * scrolled horizontally.</p>
10617 *
10618 * @return true if the horizontal edges should are faded on scroll, false
10619 * otherwise
10620 *
10621 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010622 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010623 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010624 */
10625 public boolean isHorizontalFadingEdgeEnabled() {
10626 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10627 }
10628
10629 /**
10630 * <p>Define whether the horizontal edges should be faded when this view
10631 * is scrolled horizontally.</p>
10632 *
10633 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10634 * be faded when the view is scrolled
10635 * horizontally
10636 *
10637 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010638 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010639 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010640 */
10641 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10642 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10643 if (horizontalFadingEdgeEnabled) {
10644 initScrollCache();
10645 }
10646
10647 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10648 }
10649 }
10650
10651 /**
10652 * <p>Indicate whether the vertical edges are faded when the view is
10653 * scrolled horizontally.</p>
10654 *
10655 * @return true if the vertical edges should are faded on scroll, false
10656 * otherwise
10657 *
10658 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010659 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010660 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010661 */
10662 public boolean isVerticalFadingEdgeEnabled() {
10663 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10664 }
10665
10666 /**
10667 * <p>Define whether the vertical edges should be faded when this view
10668 * is scrolled vertically.</p>
10669 *
10670 * @param verticalFadingEdgeEnabled true if the vertical edges should
10671 * be faded when the view is scrolled
10672 * vertically
10673 *
10674 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010675 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010676 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010677 */
10678 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10679 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10680 if (verticalFadingEdgeEnabled) {
10681 initScrollCache();
10682 }
10683
10684 mViewFlags ^= FADING_EDGE_VERTICAL;
10685 }
10686 }
10687
10688 /**
10689 * Returns the strength, or intensity, of the top faded edge. The strength is
10690 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10691 * returns 0.0 or 1.0 but no value in between.
10692 *
10693 * Subclasses should override this method to provide a smoother fade transition
10694 * when scrolling occurs.
10695 *
10696 * @return the intensity of the top fade as a float between 0.0f and 1.0f
10697 */
10698 protected float getTopFadingEdgeStrength() {
10699 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
10700 }
10701
10702 /**
10703 * Returns the strength, or intensity, of the bottom faded edge. The strength is
10704 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10705 * returns 0.0 or 1.0 but no value in between.
10706 *
10707 * Subclasses should override this method to provide a smoother fade transition
10708 * when scrolling occurs.
10709 *
10710 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
10711 */
10712 protected float getBottomFadingEdgeStrength() {
10713 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
10714 computeVerticalScrollRange() ? 1.0f : 0.0f;
10715 }
10716
10717 /**
10718 * Returns the strength, or intensity, of the left faded edge. The strength is
10719 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10720 * returns 0.0 or 1.0 but no value in between.
10721 *
10722 * Subclasses should override this method to provide a smoother fade transition
10723 * when scrolling occurs.
10724 *
10725 * @return the intensity of the left fade as a float between 0.0f and 1.0f
10726 */
10727 protected float getLeftFadingEdgeStrength() {
10728 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
10729 }
10730
10731 /**
10732 * Returns the strength, or intensity, of the right faded edge. The strength is
10733 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10734 * returns 0.0 or 1.0 but no value in between.
10735 *
10736 * Subclasses should override this method to provide a smoother fade transition
10737 * when scrolling occurs.
10738 *
10739 * @return the intensity of the right fade as a float between 0.0f and 1.0f
10740 */
10741 protected float getRightFadingEdgeStrength() {
10742 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
10743 computeHorizontalScrollRange() ? 1.0f : 0.0f;
10744 }
10745
10746 /**
10747 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
10748 * scrollbar is not drawn by default.</p>
10749 *
10750 * @return true if the horizontal scrollbar should be painted, false
10751 * otherwise
10752 *
10753 * @see #setHorizontalScrollBarEnabled(boolean)
10754 */
10755 public boolean isHorizontalScrollBarEnabled() {
10756 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10757 }
10758
10759 /**
10760 * <p>Define whether the horizontal scrollbar should be drawn or not. The
10761 * scrollbar is not drawn by default.</p>
10762 *
10763 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
10764 * be painted
10765 *
10766 * @see #isHorizontalScrollBarEnabled()
10767 */
10768 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
10769 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
10770 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010771 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010772 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010773 }
10774 }
10775
10776 /**
10777 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
10778 * scrollbar is not drawn by default.</p>
10779 *
10780 * @return true if the vertical scrollbar should be painted, false
10781 * otherwise
10782 *
10783 * @see #setVerticalScrollBarEnabled(boolean)
10784 */
10785 public boolean isVerticalScrollBarEnabled() {
10786 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
10787 }
10788
10789 /**
10790 * <p>Define whether the vertical scrollbar should be drawn or not. The
10791 * scrollbar is not drawn by default.</p>
10792 *
10793 * @param verticalScrollBarEnabled true if the vertical scrollbar should
10794 * be painted
10795 *
10796 * @see #isVerticalScrollBarEnabled()
10797 */
10798 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
10799 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
10800 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010801 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010802 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010803 }
10804 }
10805
Adam Powell20232d02010-12-08 21:08:53 -080010806 /**
10807 * @hide
10808 */
10809 protected void recomputePadding() {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070010810 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010811 }
Joe Malin32736f02011-01-19 16:14:20 -080010812
Mike Cleronfe81d382009-09-28 14:22:16 -070010813 /**
10814 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080010815 *
Mike Cleronfe81d382009-09-28 14:22:16 -070010816 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080010817 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010818 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070010819 */
10820 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
10821 initScrollCache();
10822 final ScrollabilityCache scrollabilityCache = mScrollCache;
10823 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010824 if (fadeScrollbars) {
10825 scrollabilityCache.state = ScrollabilityCache.OFF;
10826 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070010827 scrollabilityCache.state = ScrollabilityCache.ON;
10828 }
10829 }
Joe Malin32736f02011-01-19 16:14:20 -080010830
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010831 /**
Joe Malin32736f02011-01-19 16:14:20 -080010832 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010833 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080010834 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010835 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070010836 *
10837 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070010838 */
10839 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080010840 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010841 }
Joe Malin32736f02011-01-19 16:14:20 -080010842
Mike Cleron52f0a642009-09-28 18:21:37 -070010843 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070010844 *
10845 * Returns the delay before scrollbars fade.
10846 *
10847 * @return the delay before scrollbars fade
10848 *
10849 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10850 */
10851 public int getScrollBarDefaultDelayBeforeFade() {
10852 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
10853 mScrollCache.scrollBarDefaultDelayBeforeFade;
10854 }
10855
10856 /**
10857 * Define the delay before scrollbars fade.
10858 *
10859 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
10860 *
10861 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10862 */
10863 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
10864 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
10865 }
10866
10867 /**
10868 *
10869 * Returns the scrollbar fade duration.
10870 *
10871 * @return the scrollbar fade duration
10872 *
10873 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10874 */
10875 public int getScrollBarFadeDuration() {
10876 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
10877 mScrollCache.scrollBarFadeDuration;
10878 }
10879
10880 /**
10881 * Define the scrollbar fade duration.
10882 *
10883 * @param scrollBarFadeDuration - the scrollbar fade duration
10884 *
10885 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10886 */
10887 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
10888 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
10889 }
10890
10891 /**
10892 *
10893 * Returns the scrollbar size.
10894 *
10895 * @return the scrollbar size
10896 *
10897 * @attr ref android.R.styleable#View_scrollbarSize
10898 */
10899 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070010900 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070010901 mScrollCache.scrollBarSize;
10902 }
10903
10904 /**
10905 * Define the scrollbar size.
10906 *
10907 * @param scrollBarSize - the scrollbar size
10908 *
10909 * @attr ref android.R.styleable#View_scrollbarSize
10910 */
10911 public void setScrollBarSize(int scrollBarSize) {
10912 getScrollCache().scrollBarSize = scrollBarSize;
10913 }
10914
10915 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010916 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
10917 * inset. When inset, they add to the padding of the view. And the scrollbars
10918 * can be drawn inside the padding area or on the edge of the view. For example,
10919 * if a view has a background drawable and you want to draw the scrollbars
10920 * inside the padding specified by the drawable, you can use
10921 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
10922 * appear at the edge of the view, ignoring the padding, then you can use
10923 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
10924 * @param style the style of the scrollbars. Should be one of
10925 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
10926 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
10927 * @see #SCROLLBARS_INSIDE_OVERLAY
10928 * @see #SCROLLBARS_INSIDE_INSET
10929 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10930 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010931 *
10932 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010933 */
10934 public void setScrollBarStyle(int style) {
10935 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
10936 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070010937 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010938 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010939 }
10940 }
10941
10942 /**
10943 * <p>Returns the current scrollbar style.</p>
10944 * @return the current scrollbar style
10945 * @see #SCROLLBARS_INSIDE_OVERLAY
10946 * @see #SCROLLBARS_INSIDE_INSET
10947 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10948 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010949 *
10950 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010951 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070010952 @ViewDebug.ExportedProperty(mapping = {
10953 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
10954 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
10955 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
10956 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
10957 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010958 public int getScrollBarStyle() {
10959 return mViewFlags & SCROLLBARS_STYLE_MASK;
10960 }
10961
10962 /**
10963 * <p>Compute the horizontal range that the horizontal scrollbar
10964 * represents.</p>
10965 *
10966 * <p>The range is expressed in arbitrary units that must be the same as the
10967 * units used by {@link #computeHorizontalScrollExtent()} and
10968 * {@link #computeHorizontalScrollOffset()}.</p>
10969 *
10970 * <p>The default range is the drawing width of this view.</p>
10971 *
10972 * @return the total horizontal range represented by the horizontal
10973 * scrollbar
10974 *
10975 * @see #computeHorizontalScrollExtent()
10976 * @see #computeHorizontalScrollOffset()
10977 * @see android.widget.ScrollBarDrawable
10978 */
10979 protected int computeHorizontalScrollRange() {
10980 return getWidth();
10981 }
10982
10983 /**
10984 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
10985 * within the horizontal range. This value is used to compute the position
10986 * of the thumb within the scrollbar's track.</p>
10987 *
10988 * <p>The range is expressed in arbitrary units that must be the same as the
10989 * units used by {@link #computeHorizontalScrollRange()} and
10990 * {@link #computeHorizontalScrollExtent()}.</p>
10991 *
10992 * <p>The default offset is the scroll offset of this view.</p>
10993 *
10994 * @return the horizontal offset of the scrollbar's thumb
10995 *
10996 * @see #computeHorizontalScrollRange()
10997 * @see #computeHorizontalScrollExtent()
10998 * @see android.widget.ScrollBarDrawable
10999 */
11000 protected int computeHorizontalScrollOffset() {
11001 return mScrollX;
11002 }
11003
11004 /**
11005 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
11006 * within the horizontal range. This value is used to compute the length
11007 * of the thumb within the scrollbar's track.</p>
11008 *
11009 * <p>The range is expressed in arbitrary units that must be the same as the
11010 * units used by {@link #computeHorizontalScrollRange()} and
11011 * {@link #computeHorizontalScrollOffset()}.</p>
11012 *
11013 * <p>The default extent is the drawing width of this view.</p>
11014 *
11015 * @return the horizontal extent of the scrollbar's thumb
11016 *
11017 * @see #computeHorizontalScrollRange()
11018 * @see #computeHorizontalScrollOffset()
11019 * @see android.widget.ScrollBarDrawable
11020 */
11021 protected int computeHorizontalScrollExtent() {
11022 return getWidth();
11023 }
11024
11025 /**
11026 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
11027 *
11028 * <p>The range is expressed in arbitrary units that must be the same as the
11029 * units used by {@link #computeVerticalScrollExtent()} and
11030 * {@link #computeVerticalScrollOffset()}.</p>
11031 *
11032 * @return the total vertical range represented by the vertical scrollbar
11033 *
11034 * <p>The default range is the drawing height of this view.</p>
11035 *
11036 * @see #computeVerticalScrollExtent()
11037 * @see #computeVerticalScrollOffset()
11038 * @see android.widget.ScrollBarDrawable
11039 */
11040 protected int computeVerticalScrollRange() {
11041 return getHeight();
11042 }
11043
11044 /**
11045 * <p>Compute the vertical offset of the vertical scrollbar's thumb
11046 * within the horizontal range. This value is used to compute the position
11047 * of the thumb within the scrollbar's track.</p>
11048 *
11049 * <p>The range is expressed in arbitrary units that must be the same as the
11050 * units used by {@link #computeVerticalScrollRange()} and
11051 * {@link #computeVerticalScrollExtent()}.</p>
11052 *
11053 * <p>The default offset is the scroll offset of this view.</p>
11054 *
11055 * @return the vertical offset of the scrollbar's thumb
11056 *
11057 * @see #computeVerticalScrollRange()
11058 * @see #computeVerticalScrollExtent()
11059 * @see android.widget.ScrollBarDrawable
11060 */
11061 protected int computeVerticalScrollOffset() {
11062 return mScrollY;
11063 }
11064
11065 /**
11066 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
11067 * within the vertical range. This value is used to compute the length
11068 * of the thumb within the scrollbar's track.</p>
11069 *
11070 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080011071 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011072 * {@link #computeVerticalScrollOffset()}.</p>
11073 *
11074 * <p>The default extent is the drawing height of this view.</p>
11075 *
11076 * @return the vertical extent of the scrollbar's thumb
11077 *
11078 * @see #computeVerticalScrollRange()
11079 * @see #computeVerticalScrollOffset()
11080 * @see android.widget.ScrollBarDrawable
11081 */
11082 protected int computeVerticalScrollExtent() {
11083 return getHeight();
11084 }
11085
11086 /**
Adam Powell69159442011-06-13 17:53:06 -070011087 * Check if this view can be scrolled horizontally in a certain direction.
11088 *
11089 * @param direction Negative to check scrolling left, positive to check scrolling right.
11090 * @return true if this view can be scrolled in the specified direction, false otherwise.
11091 */
11092 public boolean canScrollHorizontally(int direction) {
11093 final int offset = computeHorizontalScrollOffset();
11094 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
11095 if (range == 0) return false;
11096 if (direction < 0) {
11097 return offset > 0;
11098 } else {
11099 return offset < range - 1;
11100 }
11101 }
11102
11103 /**
11104 * Check if this view can be scrolled vertically in a certain direction.
11105 *
11106 * @param direction Negative to check scrolling up, positive to check scrolling down.
11107 * @return true if this view can be scrolled in the specified direction, false otherwise.
11108 */
11109 public boolean canScrollVertically(int direction) {
11110 final int offset = computeVerticalScrollOffset();
11111 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
11112 if (range == 0) return false;
11113 if (direction < 0) {
11114 return offset > 0;
11115 } else {
11116 return offset < range - 1;
11117 }
11118 }
11119
11120 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011121 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
11122 * scrollbars are painted only if they have been awakened first.</p>
11123 *
11124 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080011125 *
Mike Cleronf116bf82009-09-27 19:14:12 -070011126 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011127 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080011128 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011129 // scrollbars are drawn only when the animation is running
11130 final ScrollabilityCache cache = mScrollCache;
11131 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080011132
Mike Cleronf116bf82009-09-27 19:14:12 -070011133 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080011134
Mike Cleronf116bf82009-09-27 19:14:12 -070011135 if (state == ScrollabilityCache.OFF) {
11136 return;
11137 }
Joe Malin32736f02011-01-19 16:14:20 -080011138
Mike Cleronf116bf82009-09-27 19:14:12 -070011139 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080011140
Mike Cleronf116bf82009-09-27 19:14:12 -070011141 if (state == ScrollabilityCache.FADING) {
11142 // We're fading -- get our fade interpolation
11143 if (cache.interpolatorValues == null) {
11144 cache.interpolatorValues = new float[1];
11145 }
Joe Malin32736f02011-01-19 16:14:20 -080011146
Mike Cleronf116bf82009-09-27 19:14:12 -070011147 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080011148
Mike Cleronf116bf82009-09-27 19:14:12 -070011149 // Stops the animation if we're done
11150 if (cache.scrollBarInterpolator.timeToValues(values) ==
11151 Interpolator.Result.FREEZE_END) {
11152 cache.state = ScrollabilityCache.OFF;
11153 } else {
11154 cache.scrollBar.setAlpha(Math.round(values[0]));
11155 }
Joe Malin32736f02011-01-19 16:14:20 -080011156
11157 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070011158 // drawing. We only want this when we're fading so that
11159 // we prevent excessive redraws
11160 invalidate = true;
11161 } else {
11162 // We're just on -- but we may have been fading before so
11163 // reset alpha
11164 cache.scrollBar.setAlpha(255);
11165 }
11166
Joe Malin32736f02011-01-19 16:14:20 -080011167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011168 final int viewFlags = mViewFlags;
11169
11170 final boolean drawHorizontalScrollBar =
11171 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
11172 final boolean drawVerticalScrollBar =
11173 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
11174 && !isVerticalScrollBarHidden();
11175
11176 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
11177 final int width = mRight - mLeft;
11178 final int height = mBottom - mTop;
11179
11180 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011181
Mike Reede8853fc2009-09-04 14:01:48 -040011182 final int scrollX = mScrollX;
11183 final int scrollY = mScrollY;
11184 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
11185
Mike Cleronf116bf82009-09-27 19:14:12 -070011186 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080011187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011188 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011189 int size = scrollBar.getSize(false);
11190 if (size <= 0) {
11191 size = cache.scrollBarSize;
11192 }
11193
Mike Cleronf116bf82009-09-27 19:14:12 -070011194 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040011195 computeHorizontalScrollOffset(),
11196 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040011197 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070011198 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080011199 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070011200 left = scrollX + (mPaddingLeft & inside);
11201 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
11202 bottom = top + size;
11203 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
11204 if (invalidate) {
11205 invalidate(left, top, right, bottom);
11206 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011207 }
11208
11209 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011210 int size = scrollBar.getSize(true);
11211 if (size <= 0) {
11212 size = cache.scrollBarSize;
11213 }
11214
Mike Reede8853fc2009-09-04 14:01:48 -040011215 scrollBar.setParameters(computeVerticalScrollRange(),
11216 computeVerticalScrollOffset(),
11217 computeVerticalScrollExtent(), true);
Fabrice Di Meglioc91b6ca2012-06-22 14:51:15 -070011218 int verticalScrollbarPosition = mVerticalScrollbarPosition;
11219 if (verticalScrollbarPosition == SCROLLBAR_POSITION_DEFAULT) {
11220 verticalScrollbarPosition = isLayoutRtl() ?
11221 SCROLLBAR_POSITION_LEFT : SCROLLBAR_POSITION_RIGHT;
11222 }
11223 switch (verticalScrollbarPosition) {
Adam Powell20232d02010-12-08 21:08:53 -080011224 default:
Adam Powell20232d02010-12-08 21:08:53 -080011225 case SCROLLBAR_POSITION_RIGHT:
11226 left = scrollX + width - size - (mUserPaddingRight & inside);
11227 break;
11228 case SCROLLBAR_POSITION_LEFT:
11229 left = scrollX + (mUserPaddingLeft & inside);
11230 break;
11231 }
Mike Cleronf116bf82009-09-27 19:14:12 -070011232 top = scrollY + (mPaddingTop & inside);
11233 right = left + size;
11234 bottom = scrollY + height - (mUserPaddingBottom & inside);
11235 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
11236 if (invalidate) {
11237 invalidate(left, top, right, bottom);
11238 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011239 }
11240 }
11241 }
11242 }
Romain Guy8506ab42009-06-11 17:35:47 -070011243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011244 /**
Romain Guy8506ab42009-06-11 17:35:47 -070011245 * 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 -080011246 * FastScroller is visible.
11247 * @return whether to temporarily hide the vertical scrollbar
11248 * @hide
11249 */
11250 protected boolean isVerticalScrollBarHidden() {
11251 return false;
11252 }
11253
11254 /**
11255 * <p>Draw the horizontal scrollbar if
11256 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
11257 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011258 * @param canvas the canvas on which to draw the scrollbar
11259 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011260 *
11261 * @see #isHorizontalScrollBarEnabled()
11262 * @see #computeHorizontalScrollRange()
11263 * @see #computeHorizontalScrollExtent()
11264 * @see #computeHorizontalScrollOffset()
11265 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070011266 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011267 */
Romain Guy8fb95422010-08-17 18:38:51 -070011268 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
11269 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011270 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011271 scrollBar.draw(canvas);
11272 }
Mike Reede8853fc2009-09-04 14:01:48 -040011273
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011274 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011275 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
11276 * returns true.</p>
11277 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011278 * @param canvas the canvas on which to draw the scrollbar
11279 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011280 *
11281 * @see #isVerticalScrollBarEnabled()
11282 * @see #computeVerticalScrollRange()
11283 * @see #computeVerticalScrollExtent()
11284 * @see #computeVerticalScrollOffset()
11285 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040011286 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011287 */
Romain Guy8fb95422010-08-17 18:38:51 -070011288 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
11289 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040011290 scrollBar.setBounds(l, t, r, b);
11291 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011292 }
11293
11294 /**
11295 * Implement this to do your drawing.
11296 *
11297 * @param canvas the canvas on which the background will be drawn
11298 */
11299 protected void onDraw(Canvas canvas) {
11300 }
11301
11302 /*
11303 * Caller is responsible for calling requestLayout if necessary.
11304 * (This allows addViewInLayout to not request a new layout.)
11305 */
11306 void assignParent(ViewParent parent) {
11307 if (mParent == null) {
11308 mParent = parent;
11309 } else if (parent == null) {
11310 mParent = null;
11311 } else {
11312 throw new RuntimeException("view " + this + " being added, but"
11313 + " it already has a parent");
11314 }
11315 }
11316
11317 /**
11318 * This is called when the view is attached to a window. At this point it
11319 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011320 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
11321 * however it may be called any time before the first onDraw -- including
11322 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011323 *
11324 * @see #onDetachedFromWindow()
11325 */
11326 protected void onAttachedToWindow() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011327 if ((mPrivateFlags & PFLAG_REQUEST_TRANSPARENT_REGIONS) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011328 mParent.requestTransparentRegion(this);
11329 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011330
Dianne Hackborn4702a852012-08-17 15:18:29 -070011331 if ((mPrivateFlags & PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
Adam Powell8568c3a2010-04-19 14:26:11 -070011332 initialAwakenScrollBars();
Dianne Hackborn4702a852012-08-17 15:18:29 -070011333 mPrivateFlags &= ~PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH;
Adam Powell8568c3a2010-04-19 14:26:11 -070011334 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011335
Chet Haasea9b61ac2010-12-20 07:40:25 -080011336 jumpDrawablesToCurrentState();
Romain Guy2a0f2282012-05-08 14:43:12 -070011337
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011338 resolveRtlProperties();
Romain Guy2a0f2282012-05-08 14:43:12 -070011339
Svetoslav Ganov42138042012-03-20 11:51:39 -070011340 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070011341 if (isFocused()) {
11342 InputMethodManager imm = InputMethodManager.peekInstance();
11343 imm.focusIn(this);
11344 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011345
11346 if (mAttachInfo != null && mDisplayList != null) {
11347 mAttachInfo.mViewRootImpl.dequeueDisplayList(mDisplayList);
11348 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011349 }
Cibu Johny86666632010-02-22 13:01:02 -080011350
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011351 void resolveRtlProperties() {
11352 // Order is important here: LayoutDirection MUST be resolved first...
11353 resolveLayoutDirection();
11354 // ... then we can resolve the others properties depending on the resolved LayoutDirection.
11355 resolvePadding();
11356 resolveLayoutParams();
11357 resolveTextDirection();
11358 resolveTextAlignment();
11359 }
11360
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011361 /**
Romain Guybb9908b2012-03-08 11:14:07 -080011362 * @see #onScreenStateChanged(int)
11363 */
11364 void dispatchScreenStateChanged(int screenState) {
11365 onScreenStateChanged(screenState);
11366 }
11367
11368 /**
11369 * This method is called whenever the state of the screen this view is
11370 * attached to changes. A state change will usually occurs when the screen
11371 * turns on or off (whether it happens automatically or the user does it
11372 * manually.)
11373 *
11374 * @param screenState The new state of the screen. Can be either
11375 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
11376 */
11377 public void onScreenStateChanged(int screenState) {
11378 }
11379
11380 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011381 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
11382 */
11383 private boolean hasRtlSupport() {
11384 return mContext.getApplicationInfo().hasRtlSupport();
11385 }
11386
11387 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011388 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
11389 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011390 * Will call {@link View#onResolvedLayoutDirectionChanged} when resolution is done.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011391 */
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011392 public void resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011393 // Clear any previous layout direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070011394 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011395
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011396 if (hasRtlSupport()) {
11397 // Set resolved depending on layout direction
11398 switch (getLayoutDirection()) {
11399 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliob93911f2012-06-26 19:43:15 -070011400 // We cannot resolve yet. LTR is by default and let the resolution happen again
11401 // later to get the correct resolved value
11402 if (!canResolveLayoutDirection()) return;
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011403
Fabrice Di Megliob93911f2012-06-26 19:43:15 -070011404 ViewGroup viewGroup = ((ViewGroup) mParent);
11405
11406 // We cannot resolve yet on the parent too. LTR is by default and let the
11407 // resolution happen again later
11408 if (!viewGroup.canResolveLayoutDirection()) return;
11409
11410 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011411 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011412 }
11413 break;
11414 case LAYOUT_DIRECTION_RTL:
Dianne Hackborn4702a852012-08-17 15:18:29 -070011415 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011416 break;
11417 case LAYOUT_DIRECTION_LOCALE:
11418 if(isLayoutDirectionRtl(Locale.getDefault())) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011419 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011420 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011421 break;
11422 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011423 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011424 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011425 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011426
11427 // Set to resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070011428 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011429 onResolvedLayoutDirectionChanged();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011430 }
11431
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011432 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011433 * Called when layout direction has been resolved.
11434 *
11435 * The default implementation does nothing.
11436 */
11437 public void onResolvedLayoutDirectionChanged() {
11438 }
11439
11440 /**
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011441 * Return if padding has been resolved
11442 */
11443 boolean isPaddingResolved() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011444 return (mPrivateFlags2 & PFLAG2_PADDING_RESOLVED) != 0;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011445 }
11446
11447 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011448 * Resolve padding depending on layout direction.
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011449 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011450 public void resolvePadding() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011451 final int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion;
11452 if (targetSdkVersion < JELLY_BEAN_MR1 || !hasRtlSupport()) {
11453 // Pre Jelly Bean MR1 case (compatibility mode) OR no RTL support case:
11454 // left / right padding are used if defined. If they are not defined and start / end
11455 // padding are defined (e.g. in Frameworks resources), then we use start / end and
11456 // resolve them as left / right (layout direction is not taken into account).
11457 if (!mUserPaddingLeftDefined && mUserPaddingStart != UNDEFINED_PADDING) {
11458 mUserPaddingLeft = mUserPaddingStart;
11459 }
11460 if (!mUserPaddingRightDefined && mUserPaddingEnd != UNDEFINED_PADDING) {
11461 mUserPaddingRight = mUserPaddingEnd;
11462 }
11463
11464 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
11465
11466 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight,
11467 mUserPaddingBottom);
11468 } else {
11469 // Post Jelly Bean MR1 case: we need to take the resolved layout direction into account.
11470 // If start / end padding are defined, they will be resolved (hence overriding) to
11471 // left / right or right / left depending on the resolved layout direction.
11472 // If start / end padding are not defined, use the left / right ones.
11473 int resolvedLayoutDirection = getResolvedLayoutDirection();
11474 switch (resolvedLayoutDirection) {
11475 case LAYOUT_DIRECTION_RTL:
11476 if (mUserPaddingStart != UNDEFINED_PADDING) {
11477 mUserPaddingRight = mUserPaddingStart;
11478 }
11479 if (mUserPaddingEnd != UNDEFINED_PADDING) {
11480 mUserPaddingLeft = mUserPaddingEnd;
11481 }
11482 break;
11483 case LAYOUT_DIRECTION_LTR:
11484 default:
11485 if (mUserPaddingStart != UNDEFINED_PADDING) {
11486 mUserPaddingLeft = mUserPaddingStart;
11487 }
11488 if (mUserPaddingEnd != UNDEFINED_PADDING) {
11489 mUserPaddingRight = mUserPaddingEnd;
11490 }
11491 }
11492
11493 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
11494
11495 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight,
11496 mUserPaddingBottom);
11497 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011498 }
11499
Dianne Hackborn4702a852012-08-17 15:18:29 -070011500 mPrivateFlags2 |= PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011501 }
11502
11503 /**
11504 * Resolve padding depending on the layout direction. Subclasses that care about
11505 * padding resolution should override this method. The default implementation does
11506 * nothing.
11507 *
11508 * @param layoutDirection the direction of the layout
11509 *
Fabrice Di Meglioe8dc07d2012-03-09 17:10:19 -080011510 * @see {@link #LAYOUT_DIRECTION_LTR}
11511 * @see {@link #LAYOUT_DIRECTION_RTL}
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011512 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011513 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011514 }
11515
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011516 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011517 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011518 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011519 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011520 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011521 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011522 switch (getLayoutDirection()) {
11523 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011524 return (mParent != null) && (mParent instanceof ViewGroup);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011525 default:
11526 return true;
11527 }
11528 }
11529
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011530 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011531 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
11532 * when reset is done.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011533 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011534 public void resetResolvedLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070011535 // Reset the current resolved bits
Dianne Hackborn4702a852012-08-17 15:18:29 -070011536 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011537 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080011538 // Reset also the text direction
11539 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011540 }
11541
11542 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011543 * Called during reset of resolved layout direction.
11544 *
11545 * Subclasses need to override this method to clear cached information that depends on the
11546 * resolved layout direction, or to inform child views that inherit their layout direction.
11547 *
11548 * The default implementation does nothing.
11549 */
11550 public void onResolvedLayoutDirectionReset() {
11551 }
11552
11553 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011554 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011555 *
11556 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011557 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011558 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011559 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -080011560 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011561 }
11562
11563 /**
11564 * This is called when the view is detached from a window. At this point it
11565 * no longer has a surface for drawing.
11566 *
11567 * @see #onAttachedToWindow()
11568 */
11569 protected void onDetachedFromWindow() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011570 mPrivateFlags &= ~PFLAG_CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080011571
Romain Guya440b002010-02-24 15:57:54 -080011572 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050011573 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080011574 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070011575 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080011576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011577 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011578
Romain Guya998dff2012-03-23 18:58:36 -070011579 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011580
11581 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011582 if (mDisplayList != null) {
Romain Guy2a0f2282012-05-08 14:43:12 -070011583 mAttachInfo.mViewRootImpl.enqueueDisplayList(mDisplayList);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011584 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011585 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011586 } else {
Romain Guy38c2ece2012-05-24 14:20:56 -070011587 // Should never happen
11588 clearDisplayList();
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011589 }
11590
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011591 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011592
11593 resetResolvedLayoutDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011594 resetResolvedTextAlignment();
Svetoslav Ganov42138042012-03-20 11:51:39 -070011595 resetAccessibilityStateChanged();
Dianne Hackborn4702a852012-08-17 15:18:29 -070011596 mPrivateFlags2 &= ~PFLAG2_PADDING_RESOLVED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011597 }
11598
11599 /**
11600 * @return The number of times this view has been attached to a window
11601 */
11602 protected int getWindowAttachCount() {
11603 return mWindowAttachCount;
11604 }
11605
11606 /**
11607 * Retrieve a unique token identifying the window this view is attached to.
11608 * @return Return the window's token for use in
11609 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11610 */
11611 public IBinder getWindowToken() {
11612 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11613 }
11614
11615 /**
11616 * Retrieve a unique token identifying the top-level "real" window of
11617 * the window that this view is attached to. That is, this is like
11618 * {@link #getWindowToken}, except if the window this view in is a panel
11619 * window (attached to another containing window), then the token of
11620 * the containing window is returned instead.
11621 *
11622 * @return Returns the associated window token, either
11623 * {@link #getWindowToken()} or the containing window's token.
11624 */
11625 public IBinder getApplicationWindowToken() {
11626 AttachInfo ai = mAttachInfo;
11627 if (ai != null) {
11628 IBinder appWindowToken = ai.mPanelParentWindowToken;
11629 if (appWindowToken == null) {
11630 appWindowToken = ai.mWindowToken;
11631 }
11632 return appWindowToken;
11633 }
11634 return null;
11635 }
11636
11637 /**
Jeff Brown98365d72012-08-19 20:30:52 -070011638 * Gets the logical display to which the view's window has been attached.
11639 *
11640 * @return The logical display, or null if the view is not currently attached to a window.
11641 */
11642 public Display getDisplay() {
11643 return mAttachInfo != null ? mAttachInfo.mDisplay : null;
11644 }
11645
11646 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011647 * Retrieve private session object this view hierarchy is using to
11648 * communicate with the window manager.
11649 * @return the session object to communicate with the window manager
11650 */
11651 /*package*/ IWindowSession getWindowSession() {
11652 return mAttachInfo != null ? mAttachInfo.mSession : null;
11653 }
11654
11655 /**
11656 * @param info the {@link android.view.View.AttachInfo} to associated with
11657 * this view
11658 */
11659 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
11660 //System.out.println("Attached! " + this);
11661 mAttachInfo = info;
11662 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011663 // We will need to evaluate the drawable state at least once.
Dianne Hackborn4702a852012-08-17 15:18:29 -070011664 mPrivateFlags |= PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011665 if (mFloatingTreeObserver != null) {
11666 info.mTreeObserver.merge(mFloatingTreeObserver);
11667 mFloatingTreeObserver = null;
11668 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070011669 if ((mPrivateFlags&PFLAG_SCROLL_CONTAINER) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011670 mAttachInfo.mScrollContainers.add(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -070011671 mPrivateFlags |= PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011672 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070011673 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011674 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080011675
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011676 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011677 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011678 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011679 if (listeners != null && listeners.size() > 0) {
11680 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11681 // perform the dispatching. The iterator is a safe guard against listeners that
11682 // could mutate the list by calling the various add/remove methods. This prevents
11683 // the array from being modified while we iterate it.
11684 for (OnAttachStateChangeListener listener : listeners) {
11685 listener.onViewAttachedToWindow(this);
11686 }
11687 }
11688
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011689 int vis = info.mWindowVisibility;
11690 if (vis != GONE) {
11691 onWindowVisibilityChanged(vis);
11692 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070011693 if ((mPrivateFlags&PFLAG_DRAWABLE_STATE_DIRTY) != 0) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011694 // If nobody has evaluated the drawable state yet, then do it now.
11695 refreshDrawableState();
11696 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011697 }
11698
11699 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011700 AttachInfo info = mAttachInfo;
11701 if (info != null) {
11702 int vis = info.mWindowVisibility;
11703 if (vis != GONE) {
11704 onWindowVisibilityChanged(GONE);
11705 }
11706 }
11707
11708 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080011709
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011710 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011711 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011712 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011713 if (listeners != null && listeners.size() > 0) {
11714 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11715 // perform the dispatching. The iterator is a safe guard against listeners that
11716 // could mutate the list by calling the various add/remove methods. This prevents
11717 // the array from being modified while we iterate it.
11718 for (OnAttachStateChangeListener listener : listeners) {
11719 listener.onViewDetachedFromWindow(this);
11720 }
11721 }
11722
Dianne Hackborn4702a852012-08-17 15:18:29 -070011723 if ((mPrivateFlags & PFLAG_SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011724 mAttachInfo.mScrollContainers.remove(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -070011725 mPrivateFlags &= ~PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011726 }
Romain Guy01d5edc2011-01-28 11:28:53 -080011727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011728 mAttachInfo = null;
11729 }
11730
11731 /**
11732 * Store this view hierarchy's frozen state into the given container.
11733 *
11734 * @param container The SparseArray in which to save the view's state.
11735 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011736 * @see #restoreHierarchyState(android.util.SparseArray)
11737 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11738 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011739 */
11740 public void saveHierarchyState(SparseArray<Parcelable> container) {
11741 dispatchSaveInstanceState(container);
11742 }
11743
11744 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011745 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
11746 * this view and its children. May be overridden to modify how freezing happens to a
11747 * 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 -080011748 *
11749 * @param container The SparseArray in which to save the view's state.
11750 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011751 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11752 * @see #saveHierarchyState(android.util.SparseArray)
11753 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011754 */
11755 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
11756 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011757 mPrivateFlags &= ~PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011758 Parcelable state = onSaveInstanceState();
Dianne Hackborn4702a852012-08-17 15:18:29 -070011759 if ((mPrivateFlags & PFLAG_SAVE_STATE_CALLED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011760 throw new IllegalStateException(
11761 "Derived class did not call super.onSaveInstanceState()");
11762 }
11763 if (state != null) {
11764 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
11765 // + ": " + state);
11766 container.put(mID, state);
11767 }
11768 }
11769 }
11770
11771 /**
11772 * Hook allowing a view to generate a representation of its internal state
11773 * that can later be used to create a new instance with that same state.
11774 * This state should only contain information that is not persistent or can
11775 * not be reconstructed later. For example, you will never store your
11776 * current position on screen because that will be computed again when a
11777 * new instance of the view is placed in its view hierarchy.
11778 * <p>
11779 * Some examples of things you may store here: the current cursor position
11780 * in a text view (but usually not the text itself since that is stored in a
11781 * content provider or other persistent storage), the currently selected
11782 * item in a list view.
11783 *
11784 * @return Returns a Parcelable object containing the view's current dynamic
11785 * state, or null if there is nothing interesting to save. The
11786 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070011787 * @see #onRestoreInstanceState(android.os.Parcelable)
11788 * @see #saveHierarchyState(android.util.SparseArray)
11789 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011790 * @see #setSaveEnabled(boolean)
11791 */
11792 protected Parcelable onSaveInstanceState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011793 mPrivateFlags |= PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011794 return BaseSavedState.EMPTY_STATE;
11795 }
11796
11797 /**
11798 * Restore this view hierarchy's frozen state from the given container.
11799 *
11800 * @param container The SparseArray which holds previously frozen states.
11801 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011802 * @see #saveHierarchyState(android.util.SparseArray)
11803 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11804 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011805 */
11806 public void restoreHierarchyState(SparseArray<Parcelable> container) {
11807 dispatchRestoreInstanceState(container);
11808 }
11809
11810 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011811 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
11812 * state for this view and its children. May be overridden to modify how restoring
11813 * happens to a view's children; for example, some views may want to not store state
11814 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011815 *
11816 * @param container The SparseArray which holds previously saved state.
11817 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011818 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11819 * @see #restoreHierarchyState(android.util.SparseArray)
11820 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011821 */
11822 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
11823 if (mID != NO_ID) {
11824 Parcelable state = container.get(mID);
11825 if (state != null) {
11826 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
11827 // + ": " + state);
Dianne Hackborn4702a852012-08-17 15:18:29 -070011828 mPrivateFlags &= ~PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011829 onRestoreInstanceState(state);
Dianne Hackborn4702a852012-08-17 15:18:29 -070011830 if ((mPrivateFlags & PFLAG_SAVE_STATE_CALLED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011831 throw new IllegalStateException(
11832 "Derived class did not call super.onRestoreInstanceState()");
11833 }
11834 }
11835 }
11836 }
11837
11838 /**
11839 * Hook allowing a view to re-apply a representation of its internal state that had previously
11840 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
11841 * null state.
11842 *
11843 * @param state The frozen state that had previously been returned by
11844 * {@link #onSaveInstanceState}.
11845 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011846 * @see #onSaveInstanceState()
11847 * @see #restoreHierarchyState(android.util.SparseArray)
11848 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011849 */
11850 protected void onRestoreInstanceState(Parcelable state) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011851 mPrivateFlags |= PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011852 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080011853 throw new IllegalArgumentException("Wrong state class, expecting View State but "
11854 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080011855 + "when two views of different type have the same id in the same hierarchy. "
11856 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080011857 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011858 }
11859 }
11860
11861 /**
11862 * <p>Return the time at which the drawing of the view hierarchy started.</p>
11863 *
11864 * @return the drawing start time in milliseconds
11865 */
11866 public long getDrawingTime() {
11867 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
11868 }
11869
11870 /**
11871 * <p>Enables or disables the duplication of the parent's state into this view. When
11872 * duplication is enabled, this view gets its drawable state from its parent rather
11873 * than from its own internal properties.</p>
11874 *
11875 * <p>Note: in the current implementation, setting this property to true after the
11876 * view was added to a ViewGroup might have no effect at all. This property should
11877 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
11878 *
11879 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
11880 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011881 *
Gilles Debunnefb817032011-01-13 13:52:49 -080011882 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
11883 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011884 *
11885 * @param enabled True to enable duplication of the parent's drawable state, false
11886 * to disable it.
11887 *
11888 * @see #getDrawableState()
11889 * @see #isDuplicateParentStateEnabled()
11890 */
11891 public void setDuplicateParentStateEnabled(boolean enabled) {
11892 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
11893 }
11894
11895 /**
11896 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
11897 *
11898 * @return True if this view's drawable state is duplicated from the parent,
11899 * false otherwise
11900 *
11901 * @see #getDrawableState()
11902 * @see #setDuplicateParentStateEnabled(boolean)
11903 */
11904 public boolean isDuplicateParentStateEnabled() {
11905 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
11906 }
11907
11908 /**
Romain Guy171c5922011-01-06 10:04:23 -080011909 * <p>Specifies the type of layer backing this view. The layer can be
11910 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
11911 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011912 *
Romain Guy171c5922011-01-06 10:04:23 -080011913 * <p>A layer is associated with an optional {@link android.graphics.Paint}
11914 * instance that controls how the layer is composed on screen. The following
11915 * properties of the paint are taken into account when composing the layer:</p>
11916 * <ul>
11917 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
11918 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
11919 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
11920 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080011921 *
Romain Guy171c5922011-01-06 10:04:23 -080011922 * <p>If this view has an alpha value set to < 1.0 by calling
11923 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
11924 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
11925 * equivalent to setting a hardware layer on this view and providing a paint with
11926 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080011927 *
Romain Guy171c5922011-01-06 10:04:23 -080011928 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
11929 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
11930 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011931 *
Romain Guy171c5922011-01-06 10:04:23 -080011932 * @param layerType The ype of layer to use with this view, must be one of
11933 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11934 * {@link #LAYER_TYPE_HARDWARE}
11935 * @param paint The paint used to compose the layer. This argument is optional
11936 * and can be null. It is ignored when the layer type is
11937 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080011938 *
11939 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080011940 * @see #LAYER_TYPE_NONE
11941 * @see #LAYER_TYPE_SOFTWARE
11942 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080011943 * @see #setAlpha(float)
11944 *
Romain Guy171c5922011-01-06 10:04:23 -080011945 * @attr ref android.R.styleable#View_layerType
11946 */
11947 public void setLayerType(int layerType, Paint paint) {
11948 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080011949 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080011950 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
11951 }
Chet Haasedaf98e92011-01-10 14:10:36 -080011952
Romain Guyd6cd5722011-01-17 14:42:41 -080011953 if (layerType == mLayerType) {
11954 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
11955 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011956 invalidateParentCaches();
11957 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080011958 }
11959 return;
11960 }
Romain Guy171c5922011-01-06 10:04:23 -080011961
11962 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080011963 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070011964 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070011965 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011966 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080011967 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070011968 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011969 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080011970 default:
11971 break;
Romain Guy171c5922011-01-06 10:04:23 -080011972 }
11973
11974 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080011975 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
11976 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
11977 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080011978
Romain Guy0fd89bf2011-01-26 15:41:30 -080011979 invalidateParentCaches();
11980 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080011981 }
11982
11983 /**
Romain Guy59c7f802011-09-29 17:21:45 -070011984 * Indicates whether this view has a static layer. A view with layer type
11985 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
11986 * dynamic.
11987 */
11988 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080011989 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070011990 }
11991
11992 /**
Romain Guy171c5922011-01-06 10:04:23 -080011993 * Indicates what type of layer is currently associated with this view. By default
11994 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
11995 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
11996 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080011997 *
Romain Guy171c5922011-01-06 10:04:23 -080011998 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11999 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080012000 *
12001 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070012002 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080012003 * @see #LAYER_TYPE_NONE
12004 * @see #LAYER_TYPE_SOFTWARE
12005 * @see #LAYER_TYPE_HARDWARE
12006 */
12007 public int getLayerType() {
12008 return mLayerType;
12009 }
Joe Malin32736f02011-01-19 16:14:20 -080012010
Romain Guy6c319ca2011-01-11 14:29:25 -080012011 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080012012 * Forces this view's layer to be created and this view to be rendered
12013 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
12014 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070012015 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012016 * This method can for instance be used to render a view into its layer before
12017 * starting an animation. If this view is complex, rendering into the layer
12018 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070012019 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012020 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070012021 *
12022 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080012023 */
12024 public void buildLayer() {
12025 if (mLayerType == LAYER_TYPE_NONE) return;
12026
12027 if (mAttachInfo == null) {
12028 throw new IllegalStateException("This view must be attached to a window first");
12029 }
12030
12031 switch (mLayerType) {
12032 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080012033 if (mAttachInfo.mHardwareRenderer != null &&
12034 mAttachInfo.mHardwareRenderer.isEnabled() &&
12035 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080012036 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080012037 }
Romain Guyf1ae1062011-03-02 18:16:04 -080012038 break;
12039 case LAYER_TYPE_SOFTWARE:
12040 buildDrawingCache(true);
12041 break;
12042 }
12043 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012044
Romain Guyf1ae1062011-03-02 18:16:04 -080012045 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080012046 * <p>Returns a hardware layer that can be used to draw this view again
12047 * without executing its draw method.</p>
12048 *
12049 * @return A HardwareLayer ready to render, or null if an error occurred.
12050 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080012051 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070012052 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
12053 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080012054 return null;
12055 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012056
Romain Guy9c4b79a2011-11-10 19:23:58 -080012057 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080012058
12059 final int width = mRight - mLeft;
12060 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080012061
Romain Guy6c319ca2011-01-11 14:29:25 -080012062 if (width == 0 || height == 0) {
12063 return null;
12064 }
12065
Dianne Hackborn4702a852012-08-17 15:18:29 -070012066 if ((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
Romain Guy6c319ca2011-01-11 14:29:25 -080012067 if (mHardwareLayer == null) {
12068 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
12069 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070012070 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy846a5332012-07-11 17:44:57 -070012071 } else {
12072 if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
12073 mHardwareLayer.resize(width, height);
12074 mLocalDirtyRect.set(0, 0, width, height);
12075 }
12076
12077 // This should not be necessary but applications that change
12078 // the parameters of their background drawable without calling
12079 // this.setBackground(Drawable) can leave the view in a bad state
12080 // (for instance isOpaque() returns true, but the background is
12081 // not opaque.)
12082 computeOpaqueFlags();
12083
12084 final boolean opaque = isOpaque();
12085 if (mHardwareLayer.isOpaque() != opaque) {
12086 mHardwareLayer.setOpaque(opaque);
12087 mLocalDirtyRect.set(0, 0, width, height);
12088 }
Romain Guy6c319ca2011-01-11 14:29:25 -080012089 }
12090
Romain Guy5cd5c3f2011-10-17 17:10:02 -070012091 // The layer is not valid if the underlying GPU resources cannot be allocated
12092 if (!mHardwareLayer.isValid()) {
12093 return null;
12094 }
12095
Chet Haasea1cff502012-02-21 13:43:44 -080012096 mHardwareLayer.redraw(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
Michael Jurka7e52caf2012-03-06 15:57:06 -080012097 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080012098 }
12099
12100 return mHardwareLayer;
12101 }
Romain Guy171c5922011-01-06 10:04:23 -080012102
Romain Guy589b0bb2011-10-10 13:57:47 -070012103 /**
12104 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070012105 *
Romain Guy589b0bb2011-10-10 13:57:47 -070012106 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070012107 *
12108 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070012109 * @see #LAYER_TYPE_HARDWARE
12110 */
Romain Guya998dff2012-03-23 18:58:36 -070012111 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070012112 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012113 AttachInfo info = mAttachInfo;
12114 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070012115 info.mHardwareRenderer.isEnabled() &&
12116 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012117 mHardwareLayer.destroy();
12118 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080012119
Romain Guy9c4b79a2011-11-10 19:23:58 -080012120 invalidate(true);
12121 invalidateParentCaches();
12122 }
Romain Guy65b345f2011-07-27 18:51:50 -070012123 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070012124 }
Romain Guy65b345f2011-07-27 18:51:50 -070012125 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070012126 }
12127
Romain Guy171c5922011-01-06 10:04:23 -080012128 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080012129 * Destroys all hardware rendering resources. This method is invoked
12130 * when the system needs to reclaim resources. Upon execution of this
12131 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070012132 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012133 * Note: you <strong>must</strong> call
12134 * <code>super.destroyHardwareResources()</code> when overriding
12135 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070012136 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012137 * @hide
12138 */
12139 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070012140 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012141 }
12142
12143 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012144 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
12145 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
12146 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
12147 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
12148 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
12149 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012150 *
Romain Guy171c5922011-01-06 10:04:23 -080012151 * <p>Enabling the drawing cache is similar to
12152 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080012153 * acceleration is turned off. When hardware acceleration is turned on, enabling the
12154 * drawing cache has no effect on rendering because the system uses a different mechanism
12155 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
12156 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
12157 * for information on how to enable software and hardware layers.</p>
12158 *
12159 * <p>This API can be used to manually generate
12160 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
12161 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012162 *
12163 * @param enabled true to enable the drawing cache, false otherwise
12164 *
12165 * @see #isDrawingCacheEnabled()
12166 * @see #getDrawingCache()
12167 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080012168 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012169 */
12170 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012171 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012172 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
12173 }
12174
12175 /**
12176 * <p>Indicates whether the drawing cache is enabled for this view.</p>
12177 *
12178 * @return true if the drawing cache is enabled
12179 *
12180 * @see #setDrawingCacheEnabled(boolean)
12181 * @see #getDrawingCache()
12182 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012183 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012184 public boolean isDrawingCacheEnabled() {
12185 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
12186 }
12187
12188 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080012189 * Debugging utility which recursively outputs the dirty state of a view and its
12190 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080012191 *
Chet Haasedaf98e92011-01-10 14:10:36 -080012192 * @hide
12193 */
Romain Guy676b1732011-02-14 14:45:33 -080012194 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080012195 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012196 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.PFLAG_DIRTY_MASK) +
12197 ") DRAWN(" + (mPrivateFlags & PFLAG_DRAWN) + ")" + " CACHE_VALID(" +
12198 (mPrivateFlags & View.PFLAG_DRAWING_CACHE_VALID) +
12199 ") INVALIDATED(" + (mPrivateFlags & PFLAG_INVALIDATED) + ")");
Chet Haasedaf98e92011-01-10 14:10:36 -080012200 if (clear) {
12201 mPrivateFlags &= clearMask;
12202 }
12203 if (this instanceof ViewGroup) {
12204 ViewGroup parent = (ViewGroup) this;
12205 final int count = parent.getChildCount();
12206 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080012207 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080012208 child.outputDirtyFlags(indent + " ", clear, clearMask);
12209 }
12210 }
12211 }
12212
12213 /**
12214 * This method is used by ViewGroup to cause its children to restore or recreate their
12215 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
12216 * to recreate its own display list, which would happen if it went through the normal
12217 * draw/dispatchDraw mechanisms.
12218 *
12219 * @hide
12220 */
12221 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080012222
12223 /**
12224 * A view that is not attached or hardware accelerated cannot create a display list.
12225 * This method checks these conditions and returns the appropriate result.
12226 *
12227 * @return true if view has the ability to create a display list, false otherwise.
12228 *
12229 * @hide
12230 */
12231 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080012232 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080012233 }
Joe Malin32736f02011-01-19 16:14:20 -080012234
Chet Haasedaf98e92011-01-10 14:10:36 -080012235 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080012236 * @return The HardwareRenderer associated with that view or null if hardware rendering
12237 * is not supported or this this has not been attached to a window.
12238 *
12239 * @hide
12240 */
12241 public HardwareRenderer getHardwareRenderer() {
12242 if (mAttachInfo != null) {
12243 return mAttachInfo.mHardwareRenderer;
12244 }
12245 return null;
12246 }
12247
12248 /**
Chet Haasea1cff502012-02-21 13:43:44 -080012249 * Returns a DisplayList. If the incoming displayList is null, one will be created.
12250 * Otherwise, the same display list will be returned (after having been rendered into
12251 * along the way, depending on the invalidation state of the view).
12252 *
12253 * @param displayList The previous version of this displayList, could be null.
12254 * @param isLayer Whether the requester of the display list is a layer. If so,
12255 * the view will avoid creating a layer inside the resulting display list.
12256 * @return A new or reused DisplayList object.
12257 */
12258 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
12259 if (!canHaveDisplayList()) {
12260 return null;
12261 }
12262
Dianne Hackborn4702a852012-08-17 15:18:29 -070012263 if (((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 ||
Chet Haasea1cff502012-02-21 13:43:44 -080012264 displayList == null || !displayList.isValid() ||
12265 (!isLayer && mRecreateDisplayList))) {
12266 // Don't need to recreate the display list, just need to tell our
12267 // children to restore/recreate theirs
12268 if (displayList != null && displayList.isValid() &&
12269 !isLayer && !mRecreateDisplayList) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012270 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12271 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012272 dispatchGetDisplayList();
12273
12274 return displayList;
12275 }
12276
12277 if (!isLayer) {
12278 // If we got here, we're recreating it. Mark it as such to ensure that
12279 // we copy in child display lists into ours in drawChild()
12280 mRecreateDisplayList = true;
12281 }
12282 if (displayList == null) {
12283 final String name = getClass().getSimpleName();
12284 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
12285 // If we're creating a new display list, make sure our parent gets invalidated
12286 // since they will need to recreate their display list to account for this
12287 // new child display list.
12288 invalidateParentCaches();
12289 }
12290
12291 boolean caching = false;
12292 final HardwareCanvas canvas = displayList.start();
Chet Haasea1cff502012-02-21 13:43:44 -080012293 int width = mRight - mLeft;
12294 int height = mBottom - mTop;
12295
12296 try {
12297 canvas.setViewport(width, height);
12298 // The dirty rect should always be null for a display list
12299 canvas.onPreDraw(null);
Michael Jurkaba649742012-06-28 19:12:58 -070012300 int layerType = getLayerType();
Chet Haase1271e2c2012-04-20 09:54:27 -070012301 if (!isLayer && layerType != LAYER_TYPE_NONE) {
Chet Haaseb85967b2012-03-26 14:37:51 -070012302 if (layerType == LAYER_TYPE_HARDWARE) {
12303 final HardwareLayer layer = getHardwareLayer();
12304 if (layer != null && layer.isValid()) {
12305 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
12306 } else {
12307 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
12308 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
12309 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12310 }
12311 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080012312 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070012313 buildDrawingCache(true);
12314 Bitmap cache = getDrawingCache(true);
12315 if (cache != null) {
12316 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
12317 caching = true;
12318 }
Chet Haasea1cff502012-02-21 13:43:44 -080012319 }
Chet Haasea1cff502012-02-21 13:43:44 -080012320 } else {
12321
12322 computeScroll();
12323
Chet Haasea1cff502012-02-21 13:43:44 -080012324 canvas.translate(-mScrollX, -mScrollY);
12325 if (!isLayer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012326 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12327 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012328 }
12329
12330 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070012331 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
Chet Haasea1cff502012-02-21 13:43:44 -080012332 dispatchDraw(canvas);
12333 } else {
12334 draw(canvas);
12335 }
12336 }
12337 } finally {
Chet Haasea1cff502012-02-21 13:43:44 -080012338 canvas.onPostDraw();
12339
12340 displayList.end();
Chet Haase1271e2c2012-04-20 09:54:27 -070012341 displayList.setCaching(caching);
12342 if (isLayer) {
Chet Haasea1cff502012-02-21 13:43:44 -080012343 displayList.setLeftTopRightBottom(0, 0, width, height);
12344 } else {
12345 setDisplayListProperties(displayList);
12346 }
12347 }
12348 } else if (!isLayer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012349 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12350 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012351 }
12352
12353 return displayList;
12354 }
12355
12356 /**
12357 * Get the DisplayList for the HardwareLayer
12358 *
12359 * @param layer The HardwareLayer whose DisplayList we want
12360 * @return A DisplayList fopr the specified HardwareLayer
12361 */
12362 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
12363 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
12364 layer.setDisplayList(displayList);
12365 return displayList;
12366 }
12367
12368
12369 /**
Romain Guyb051e892010-09-28 19:09:36 -070012370 * <p>Returns a display list that can be used to draw this view again
12371 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012372 *
Romain Guyb051e892010-09-28 19:09:36 -070012373 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080012374 *
12375 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070012376 */
Chet Haasedaf98e92011-01-10 14:10:36 -080012377 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080012378 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070012379 return mDisplayList;
12380 }
12381
Romain Guy38c2ece2012-05-24 14:20:56 -070012382 private void clearDisplayList() {
12383 if (mDisplayList != null) {
12384 mDisplayList.invalidate();
12385 mDisplayList.clear();
12386 }
12387 }
12388
Romain Guyb051e892010-09-28 19:09:36 -070012389 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012390 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012391 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012392 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012393 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012394 * @see #getDrawingCache(boolean)
12395 */
12396 public Bitmap getDrawingCache() {
12397 return getDrawingCache(false);
12398 }
12399
12400 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012401 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
12402 * is null when caching is disabled. If caching is enabled and the cache is not ready,
12403 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
12404 * draw from the cache when the cache is enabled. To benefit from the cache, you must
12405 * request the drawing cache by calling this method and draw it on screen if the
12406 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012407 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012408 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12409 * this method will create a bitmap of the same size as this view. Because this bitmap
12410 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12411 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12412 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12413 * size than the view. This implies that your application must be able to handle this
12414 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012415 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012416 * @param autoScale Indicates whether the generated bitmap should be scaled based on
12417 * the current density of the screen when the application is in compatibility
12418 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012419 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012420 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012421 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012422 * @see #setDrawingCacheEnabled(boolean)
12423 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070012424 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012425 * @see #destroyDrawingCache()
12426 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012427 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012428 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
12429 return null;
12430 }
12431 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012432 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012433 }
Romain Guy02890fd2010-08-06 17:58:44 -070012434 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012435 }
12436
12437 /**
12438 * <p>Frees the resources used by the drawing cache. If you call
12439 * {@link #buildDrawingCache()} manually without calling
12440 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12441 * should cleanup the cache with this method afterwards.</p>
12442 *
12443 * @see #setDrawingCacheEnabled(boolean)
12444 * @see #buildDrawingCache()
12445 * @see #getDrawingCache()
12446 */
12447 public void destroyDrawingCache() {
12448 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012449 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012450 mDrawingCache = null;
12451 }
Romain Guyfbd8f692009-06-26 14:51:58 -070012452 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012453 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070012454 mUnscaledDrawingCache = null;
12455 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012456 }
12457
12458 /**
12459 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070012460 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012461 * view will always be drawn on top of a solid color.
12462 *
12463 * @param color The background color to use for the drawing cache's bitmap
12464 *
12465 * @see #setDrawingCacheEnabled(boolean)
12466 * @see #buildDrawingCache()
12467 * @see #getDrawingCache()
12468 */
12469 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080012470 if (color != mDrawingCacheBackgroundColor) {
12471 mDrawingCacheBackgroundColor = color;
Dianne Hackborn4702a852012-08-17 15:18:29 -070012472 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Romain Guy52e2ef82010-01-14 12:11:48 -080012473 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012474 }
12475
12476 /**
12477 * @see #setDrawingCacheBackgroundColor(int)
12478 *
12479 * @return The background color to used for the drawing cache's bitmap
12480 */
12481 public int getDrawingCacheBackgroundColor() {
12482 return mDrawingCacheBackgroundColor;
12483 }
12484
12485 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012486 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012487 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012488 * @see #buildDrawingCache(boolean)
12489 */
12490 public void buildDrawingCache() {
12491 buildDrawingCache(false);
12492 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080012493
Romain Guyfbd8f692009-06-26 14:51:58 -070012494 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012495 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
12496 *
12497 * <p>If you call {@link #buildDrawingCache()} manually without calling
12498 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12499 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012500 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012501 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12502 * this method will create a bitmap of the same size as this view. Because this bitmap
12503 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12504 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12505 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12506 * size than the view. This implies that your application must be able to handle this
12507 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012508 *
Romain Guy0d9275e2010-10-26 14:22:30 -070012509 * <p>You should avoid calling this method when hardware acceleration is enabled. If
12510 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080012511 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070012512 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012513 *
12514 * @see #getDrawingCache()
12515 * @see #destroyDrawingCache()
12516 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012517 public void buildDrawingCache(boolean autoScale) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012518 if ((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070012519 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012520 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012521
Romain Guy8506ab42009-06-11 17:35:47 -070012522 int width = mRight - mLeft;
12523 int height = mBottom - mTop;
12524
12525 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070012526 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070012527
Romain Guye1123222009-06-29 14:24:56 -070012528 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012529 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
12530 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070012531 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012532
12533 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070012534 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080012535 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012536
12537 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070012538 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080012539 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012540 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
12541 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080012542 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012543 return;
12544 }
12545
12546 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070012547 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012548
12549 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012550 Bitmap.Config quality;
12551 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080012552 // Never pick ARGB_4444 because it looks awful
12553 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012554 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
12555 case DRAWING_CACHE_QUALITY_AUTO:
12556 quality = Bitmap.Config.ARGB_8888;
12557 break;
12558 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080012559 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012560 break;
12561 case DRAWING_CACHE_QUALITY_HIGH:
12562 quality = Bitmap.Config.ARGB_8888;
12563 break;
12564 default:
12565 quality = Bitmap.Config.ARGB_8888;
12566 break;
12567 }
12568 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070012569 // Optimization for translucent windows
12570 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080012571 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012572 }
12573
12574 // Try to cleanup memory
12575 if (bitmap != null) bitmap.recycle();
12576
12577 try {
Dianne Hackborndde331c2012-08-03 14:01:57 -070012578 bitmap = Bitmap.createBitmap(mResources.getDisplayMetrics(),
12579 width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070012580 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070012581 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070012582 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012583 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070012584 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012585 }
Adam Powell26153a32010-11-08 15:22:27 -080012586 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012587 } catch (OutOfMemoryError e) {
12588 // If there is not enough memory to create the bitmap cache, just
12589 // ignore the issue as bitmap caches are not required to draw the
12590 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012591 if (autoScale) {
12592 mDrawingCache = null;
12593 } else {
12594 mUnscaledDrawingCache = null;
12595 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012596 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012597 return;
12598 }
12599
12600 clear = drawingCacheBackgroundColor != 0;
12601 }
12602
12603 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012604 if (attachInfo != null) {
12605 canvas = attachInfo.mCanvas;
12606 if (canvas == null) {
12607 canvas = new Canvas();
12608 }
12609 canvas.setBitmap(bitmap);
12610 // Temporarily clobber the cached Canvas in case one of our children
12611 // is also using a drawing cache. Without this, the children would
12612 // steal the canvas by attaching their own bitmap to it and bad, bad
12613 // thing would happen (invisible views, corrupted drawings, etc.)
12614 attachInfo.mCanvas = null;
12615 } else {
12616 // This case should hopefully never or seldom happen
12617 canvas = new Canvas(bitmap);
12618 }
12619
12620 if (clear) {
12621 bitmap.eraseColor(drawingCacheBackgroundColor);
12622 }
12623
12624 computeScroll();
12625 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080012626
Romain Guye1123222009-06-29 14:24:56 -070012627 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012628 final float scale = attachInfo.mApplicationScale;
12629 canvas.scale(scale, scale);
12630 }
Joe Malin32736f02011-01-19 16:14:20 -080012631
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012632 canvas.translate(-mScrollX, -mScrollY);
12633
Dianne Hackborn4702a852012-08-17 15:18:29 -070012634 mPrivateFlags |= PFLAG_DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080012635 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
12636 mLayerType != LAYER_TYPE_NONE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012637 mPrivateFlags |= PFLAG_DRAWING_CACHE_VALID;
Romain Guy0d9275e2010-10-26 14:22:30 -070012638 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012639
12640 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070012641 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
12642 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012643 dispatchDraw(canvas);
12644 } else {
12645 draw(canvas);
12646 }
12647
12648 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012649 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012650
12651 if (attachInfo != null) {
12652 // Restore the cached Canvas for our siblings
12653 attachInfo.mCanvas = canvas;
12654 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012655 }
12656 }
12657
12658 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012659 * Create a snapshot of the view into a bitmap. We should probably make
12660 * some form of this public, but should think about the API.
12661 */
Romain Guy223ff5c2010-03-02 17:07:47 -080012662 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012663 int width = mRight - mLeft;
12664 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012665
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012666 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070012667 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012668 width = (int) ((width * scale) + 0.5f);
12669 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080012670
Dianne Hackborndde331c2012-08-03 14:01:57 -070012671 Bitmap bitmap = Bitmap.createBitmap(mResources.getDisplayMetrics(),
12672 width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012673 if (bitmap == null) {
12674 throw new OutOfMemoryError();
12675 }
12676
Romain Guyc529d8d2011-09-06 15:01:39 -070012677 Resources resources = getResources();
12678 if (resources != null) {
12679 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
12680 }
Joe Malin32736f02011-01-19 16:14:20 -080012681
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012682 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012683 if (attachInfo != null) {
12684 canvas = attachInfo.mCanvas;
12685 if (canvas == null) {
12686 canvas = new Canvas();
12687 }
12688 canvas.setBitmap(bitmap);
12689 // Temporarily clobber the cached Canvas in case one of our children
12690 // is also using a drawing cache. Without this, the children would
12691 // steal the canvas by attaching their own bitmap to it and bad, bad
12692 // things would happen (invisible views, corrupted drawings, etc.)
12693 attachInfo.mCanvas = null;
12694 } else {
12695 // This case should hopefully never or seldom happen
12696 canvas = new Canvas(bitmap);
12697 }
12698
Romain Guy5bcdff42009-05-14 21:27:18 -070012699 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012700 bitmap.eraseColor(backgroundColor);
12701 }
12702
12703 computeScroll();
12704 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012705 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012706 canvas.translate(-mScrollX, -mScrollY);
12707
Romain Guy5bcdff42009-05-14 21:27:18 -070012708 // Temporarily remove the dirty mask
12709 int flags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070012710 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Romain Guy5bcdff42009-05-14 21:27:18 -070012711
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012712 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070012713 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012714 dispatchDraw(canvas);
12715 } else {
12716 draw(canvas);
12717 }
12718
Romain Guy5bcdff42009-05-14 21:27:18 -070012719 mPrivateFlags = flags;
12720
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012721 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012722 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012723
12724 if (attachInfo != null) {
12725 // Restore the cached Canvas for our siblings
12726 attachInfo.mCanvas = canvas;
12727 }
Romain Guy8506ab42009-06-11 17:35:47 -070012728
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012729 return bitmap;
12730 }
12731
12732 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012733 * Indicates whether this View is currently in edit mode. A View is usually
12734 * in edit mode when displayed within a developer tool. For instance, if
12735 * this View is being drawn by a visual user interface builder, this method
12736 * should return true.
12737 *
12738 * Subclasses should check the return value of this method to provide
12739 * different behaviors if their normal behavior might interfere with the
12740 * host environment. For instance: the class spawns a thread in its
12741 * constructor, the drawing code relies on device-specific features, etc.
12742 *
12743 * This method is usually checked in the drawing code of custom widgets.
12744 *
12745 * @return True if this View is in edit mode, false otherwise.
12746 */
12747 public boolean isInEditMode() {
12748 return false;
12749 }
12750
12751 /**
12752 * If the View draws content inside its padding and enables fading edges,
12753 * it needs to support padding offsets. Padding offsets are added to the
12754 * fading edges to extend the length of the fade so that it covers pixels
12755 * drawn inside the padding.
12756 *
12757 * Subclasses of this class should override this method if they need
12758 * to draw content inside the padding.
12759 *
12760 * @return True if padding offset must be applied, false otherwise.
12761 *
12762 * @see #getLeftPaddingOffset()
12763 * @see #getRightPaddingOffset()
12764 * @see #getTopPaddingOffset()
12765 * @see #getBottomPaddingOffset()
12766 *
12767 * @since CURRENT
12768 */
12769 protected boolean isPaddingOffsetRequired() {
12770 return false;
12771 }
12772
12773 /**
12774 * Amount by which to extend the left fading region. Called only when
12775 * {@link #isPaddingOffsetRequired()} returns true.
12776 *
12777 * @return The left padding offset in pixels.
12778 *
12779 * @see #isPaddingOffsetRequired()
12780 *
12781 * @since CURRENT
12782 */
12783 protected int getLeftPaddingOffset() {
12784 return 0;
12785 }
12786
12787 /**
12788 * Amount by which to extend the right fading region. Called only when
12789 * {@link #isPaddingOffsetRequired()} returns true.
12790 *
12791 * @return The right padding offset in pixels.
12792 *
12793 * @see #isPaddingOffsetRequired()
12794 *
12795 * @since CURRENT
12796 */
12797 protected int getRightPaddingOffset() {
12798 return 0;
12799 }
12800
12801 /**
12802 * Amount by which to extend the top fading region. Called only when
12803 * {@link #isPaddingOffsetRequired()} returns true.
12804 *
12805 * @return The top padding offset in pixels.
12806 *
12807 * @see #isPaddingOffsetRequired()
12808 *
12809 * @since CURRENT
12810 */
12811 protected int getTopPaddingOffset() {
12812 return 0;
12813 }
12814
12815 /**
12816 * Amount by which to extend the bottom fading region. Called only when
12817 * {@link #isPaddingOffsetRequired()} returns true.
12818 *
12819 * @return The bottom padding offset in pixels.
12820 *
12821 * @see #isPaddingOffsetRequired()
12822 *
12823 * @since CURRENT
12824 */
12825 protected int getBottomPaddingOffset() {
12826 return 0;
12827 }
12828
12829 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070012830 * @hide
12831 * @param offsetRequired
12832 */
12833 protected int getFadeTop(boolean offsetRequired) {
12834 int top = mPaddingTop;
12835 if (offsetRequired) top += getTopPaddingOffset();
12836 return top;
12837 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012838
Romain Guyf2fc4602011-07-19 15:20:03 -070012839 /**
12840 * @hide
12841 * @param offsetRequired
12842 */
12843 protected int getFadeHeight(boolean offsetRequired) {
12844 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070012845 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070012846 return mBottom - mTop - mPaddingBottom - padding;
12847 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012848
Romain Guyf2fc4602011-07-19 15:20:03 -070012849 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012850 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070012851 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012852 *
Romain Guy2bffd262010-09-12 17:40:02 -070012853 * <p>Even if this method returns true, it does not mean that every call
12854 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
12855 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012856 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070012857 * window is hardware accelerated,
12858 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
12859 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012860 *
Romain Guy2bffd262010-09-12 17:40:02 -070012861 * @return True if the view is attached to a window and the window is
12862 * hardware accelerated; false in any other case.
12863 */
12864 public boolean isHardwareAccelerated() {
12865 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
12866 }
Joe Malin32736f02011-01-19 16:14:20 -080012867
Romain Guy2bffd262010-09-12 17:40:02 -070012868 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080012869 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
12870 * case of an active Animation being run on the view.
12871 */
12872 private boolean drawAnimation(ViewGroup parent, long drawingTime,
12873 Animation a, boolean scalingRequired) {
12874 Transformation invalidationTransform;
12875 final int flags = parent.mGroupFlags;
12876 final boolean initialized = a.isInitialized();
12877 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070012878 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080012879 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
Romain Guy393a52c2012-05-22 20:21:08 -070012880 if (mAttachInfo != null) a.setListenerHandler(mAttachInfo.mHandler);
Chet Haasebcca79a2012-02-14 08:45:14 -080012881 onAnimationStart();
12882 }
12883
12884 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
12885 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
12886 if (parent.mInvalidationTransformation == null) {
12887 parent.mInvalidationTransformation = new Transformation();
12888 }
12889 invalidationTransform = parent.mInvalidationTransformation;
12890 a.getTransformation(drawingTime, invalidationTransform, 1f);
12891 } else {
12892 invalidationTransform = parent.mChildTransformation;
12893 }
Romain Guy393a52c2012-05-22 20:21:08 -070012894
Chet Haasebcca79a2012-02-14 08:45:14 -080012895 if (more) {
12896 if (!a.willChangeBounds()) {
Romain Guyf0af1d52012-07-11 18:31:21 -070012897 if ((flags & (ViewGroup.FLAG_OPTIMIZE_INVALIDATE | ViewGroup.FLAG_ANIMATION_DONE)) ==
12898 ViewGroup.FLAG_OPTIMIZE_INVALIDATE) {
12899 parent.mGroupFlags |= ViewGroup.FLAG_INVALIDATE_REQUIRED;
12900 } else if ((flags & ViewGroup.FLAG_INVALIDATE_REQUIRED) == 0) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012901 // The child need to draw an animation, potentially offscreen, so
12902 // make sure we do not cancel invalidate requests
Dianne Hackborn4702a852012-08-17 15:18:29 -070012903 parent.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
Chet Haasebcca79a2012-02-14 08:45:14 -080012904 parent.invalidate(mLeft, mTop, mRight, mBottom);
12905 }
12906 } else {
12907 if (parent.mInvalidateRegion == null) {
12908 parent.mInvalidateRegion = new RectF();
12909 }
12910 final RectF region = parent.mInvalidateRegion;
12911 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
12912 invalidationTransform);
12913
12914 // The child need to draw an animation, potentially offscreen, so
12915 // make sure we do not cancel invalidate requests
Dianne Hackborn4702a852012-08-17 15:18:29 -070012916 parent.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
Chet Haasebcca79a2012-02-14 08:45:14 -080012917
12918 final int left = mLeft + (int) region.left;
12919 final int top = mTop + (int) region.top;
12920 parent.invalidate(left, top, left + (int) (region.width() + .5f),
12921 top + (int) (region.height() + .5f));
12922 }
12923 }
12924 return more;
12925 }
12926
Chet Haasea1cff502012-02-21 13:43:44 -080012927 /**
12928 * This method is called by getDisplayList() when a display list is created or re-rendered.
12929 * It sets or resets the current value of all properties on that display list (resetting is
12930 * necessary when a display list is being re-created, because we need to make sure that
12931 * previously-set transform values
12932 */
12933 void setDisplayListProperties(DisplayList displayList) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012934 if (displayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012935 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070012936 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080012937 if (mParent instanceof ViewGroup) {
12938 displayList.setClipChildren(
12939 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
12940 }
Chet Haase9420abd2012-03-29 16:28:32 -070012941 float alpha = 1;
12942 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
12943 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
12944 ViewGroup parentVG = (ViewGroup) mParent;
12945 final boolean hasTransform =
12946 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
12947 if (hasTransform) {
12948 Transformation transform = parentVG.mChildTransformation;
12949 final int transformType = parentVG.mChildTransformation.getTransformationType();
12950 if (transformType != Transformation.TYPE_IDENTITY) {
12951 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
12952 alpha = transform.getAlpha();
12953 }
12954 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
12955 displayList.setStaticMatrix(transform.getMatrix());
12956 }
12957 }
12958 }
Chet Haasea1cff502012-02-21 13:43:44 -080012959 }
12960 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070012961 alpha *= mTransformationInfo.mAlpha;
12962 if (alpha < 1) {
12963 final int multipliedAlpha = (int) (255 * alpha);
12964 if (onSetAlpha(multipliedAlpha)) {
12965 alpha = 1;
12966 }
12967 }
12968 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080012969 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
12970 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
12971 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
12972 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070012973 if (mTransformationInfo.mCamera == null) {
12974 mTransformationInfo.mCamera = new Camera();
12975 mTransformationInfo.matrix3D = new Matrix();
12976 }
12977 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Dianne Hackborn4702a852012-08-17 15:18:29 -070012978 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == PFLAG_PIVOT_EXPLICITLY_SET) {
Chet Haasea1cff502012-02-21 13:43:44 -080012979 displayList.setPivotX(getPivotX());
12980 displayList.setPivotY(getPivotY());
12981 }
Chet Haase9420abd2012-03-29 16:28:32 -070012982 } else if (alpha < 1) {
12983 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080012984 }
12985 }
12986 }
12987
Chet Haasebcca79a2012-02-14 08:45:14 -080012988 /**
Chet Haase64a48c12012-02-13 16:33:29 -080012989 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
12990 * This draw() method is an implementation detail and is not intended to be overridden or
12991 * to be called from anywhere else other than ViewGroup.drawChild().
12992 */
12993 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012994 boolean useDisplayListProperties = mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080012995 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080012996 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080012997 final int flags = parent.mGroupFlags;
12998
Chet Haasea1cff502012-02-21 13:43:44 -080012999 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080013000 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080013001 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013002 }
13003
13004 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080013005 boolean concatMatrix = false;
13006
13007 boolean scalingRequired = false;
13008 boolean caching;
Michael Jurkaba649742012-06-28 19:12:58 -070013009 int layerType = getLayerType();
Chet Haase64a48c12012-02-13 16:33:29 -080013010
13011 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080013012 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
13013 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013014 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070013015 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080013016 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
13017 } else {
13018 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
13019 }
13020
Chet Haasebcca79a2012-02-14 08:45:14 -080013021 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080013022 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013023 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080013024 concatMatrix = a.willChangeTransformationMatrix();
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013025 if (concatMatrix) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013026 mPrivateFlags3 |= PFLAG3_VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013027 }
Chet Haasebcca79a2012-02-14 08:45:14 -080013028 transformToApply = parent.mChildTransformation;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013029 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013030 if ((mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_TRANSFORM) == PFLAG3_VIEW_IS_ANIMATING_TRANSFORM &&
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013031 mDisplayList != null) {
13032 // No longer animating: clear out old animation matrix
13033 mDisplayList.setAnimationMatrix(null);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013034 mPrivateFlags3 &= ~PFLAG3_VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013035 }
13036 if (!useDisplayListProperties &&
13037 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
13038 final boolean hasTransform =
13039 parent.getChildStaticTransformation(this, parent.mChildTransformation);
13040 if (hasTransform) {
13041 final int transformType = parent.mChildTransformation.getTransformationType();
13042 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
13043 parent.mChildTransformation : null;
13044 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
13045 }
Chet Haase64a48c12012-02-13 16:33:29 -080013046 }
13047 }
13048
13049 concatMatrix |= !childHasIdentityMatrix;
13050
13051 // Sets the flag as early as possible to allow draw() implementations
13052 // to call invalidate() successfully when doing animations
Dianne Hackborn4702a852012-08-17 15:18:29 -070013053 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase64a48c12012-02-13 16:33:29 -080013054
Chet Haase599913d2012-07-23 16:22:05 -070013055 if (!concatMatrix && (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) == 0 &&
13056 canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Dianne Hackborn4702a852012-08-17 15:18:29 -070013057 (mPrivateFlags & PFLAG_DRAW_ANIMATION) == 0) {
13058 mPrivateFlags2 |= PFLAG2_VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013059 return more;
13060 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013061 mPrivateFlags2 &= ~PFLAG2_VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013062
13063 if (hardwareAccelerated) {
13064 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
13065 // retain the flag's value temporarily in the mRecreateDisplayList flag
Dianne Hackborn4702a852012-08-17 15:18:29 -070013066 mRecreateDisplayList = (mPrivateFlags & PFLAG_INVALIDATED) == PFLAG_INVALIDATED;
13067 mPrivateFlags &= ~PFLAG_INVALIDATED;
Chet Haase64a48c12012-02-13 16:33:29 -080013068 }
13069
Chet Haase64a48c12012-02-13 16:33:29 -080013070 DisplayList displayList = null;
13071 Bitmap cache = null;
13072 boolean hasDisplayList = false;
13073 if (caching) {
13074 if (!hardwareAccelerated) {
13075 if (layerType != LAYER_TYPE_NONE) {
13076 layerType = LAYER_TYPE_SOFTWARE;
13077 buildDrawingCache(true);
13078 }
13079 cache = getDrawingCache(true);
13080 } else {
13081 switch (layerType) {
13082 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070013083 if (useDisplayListProperties) {
13084 hasDisplayList = canHaveDisplayList();
13085 } else {
13086 buildDrawingCache(true);
13087 cache = getDrawingCache(true);
13088 }
Chet Haase64a48c12012-02-13 16:33:29 -080013089 break;
Chet Haasea1cff502012-02-21 13:43:44 -080013090 case LAYER_TYPE_HARDWARE:
13091 if (useDisplayListProperties) {
13092 hasDisplayList = canHaveDisplayList();
13093 }
13094 break;
Chet Haase64a48c12012-02-13 16:33:29 -080013095 case LAYER_TYPE_NONE:
13096 // Delay getting the display list until animation-driven alpha values are
13097 // set up and possibly passed on to the view
13098 hasDisplayList = canHaveDisplayList();
13099 break;
13100 }
13101 }
13102 }
Chet Haasea1cff502012-02-21 13:43:44 -080013103 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070013104 if (useDisplayListProperties) {
13105 displayList = getDisplayList();
13106 if (!displayList.isValid()) {
13107 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13108 // to getDisplayList(), the display list will be marked invalid and we should not
13109 // try to use it again.
13110 displayList = null;
13111 hasDisplayList = false;
13112 useDisplayListProperties = false;
13113 }
13114 }
Chet Haase64a48c12012-02-13 16:33:29 -080013115
Chet Haase526057b2012-07-12 17:50:41 -070013116 int sx = 0;
13117 int sy = 0;
13118 if (!hasDisplayList) {
13119 computeScroll();
13120 sx = mScrollX;
13121 sy = mScrollY;
13122 }
13123
Chet Haase64a48c12012-02-13 16:33:29 -080013124 final boolean hasNoCache = cache == null || hasDisplayList;
13125 final boolean offsetForScroll = cache == null && !hasDisplayList &&
13126 layerType != LAYER_TYPE_HARDWARE;
13127
Chet Haasea1cff502012-02-21 13:43:44 -080013128 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070013129 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013130 restoreTo = canvas.save();
13131 }
Chet Haase64a48c12012-02-13 16:33:29 -080013132 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013133 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080013134 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080013135 if (!useDisplayListProperties) {
13136 canvas.translate(mLeft, mTop);
13137 }
Chet Haase64a48c12012-02-13 16:33:29 -080013138 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080013139 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070013140 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080013141 restoreTo = canvas.save();
13142 }
Chet Haase64a48c12012-02-13 16:33:29 -080013143 // mAttachInfo cannot be null, otherwise scalingRequired == false
13144 final float scale = 1.0f / mAttachInfo.mApplicationScale;
13145 canvas.scale(scale, scale);
13146 }
13147 }
13148
Chet Haasea1cff502012-02-21 13:43:44 -080013149 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase21433372012-06-05 07:54:09 -070013150 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix() ||
Dianne Hackborn4702a852012-08-17 15:18:29 -070013151 (mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_ALPHA) == PFLAG3_VIEW_IS_ANIMATING_ALPHA) {
Chet Haase64a48c12012-02-13 16:33:29 -080013152 if (transformToApply != null || !childHasIdentityMatrix) {
13153 int transX = 0;
13154 int transY = 0;
13155
13156 if (offsetForScroll) {
13157 transX = -sx;
13158 transY = -sy;
13159 }
13160
13161 if (transformToApply != null) {
13162 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070013163 if (useDisplayListProperties) {
13164 displayList.setAnimationMatrix(transformToApply.getMatrix());
13165 } else {
13166 // Undo the scroll translation, apply the transformation matrix,
13167 // then redo the scroll translate to get the correct result.
13168 canvas.translate(-transX, -transY);
13169 canvas.concat(transformToApply.getMatrix());
13170 canvas.translate(transX, transY);
13171 }
Chet Haasea1cff502012-02-21 13:43:44 -080013172 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013173 }
13174
13175 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070013176 if (transformAlpha < 1) {
Chet Haase21433372012-06-05 07:54:09 -070013177 alpha *= transformAlpha;
Chet Haasea1cff502012-02-21 13:43:44 -080013178 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013179 }
13180 }
13181
Chet Haasea1cff502012-02-21 13:43:44 -080013182 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013183 canvas.translate(-transX, -transY);
13184 canvas.concat(getMatrix());
13185 canvas.translate(transX, transY);
13186 }
13187 }
13188
Chet Haase21433372012-06-05 07:54:09 -070013189 // Deal with alpha if it is or used to be <1
13190 if (alpha < 1 ||
Dianne Hackborn4702a852012-08-17 15:18:29 -070013191 (mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_ALPHA) == PFLAG3_VIEW_IS_ANIMATING_ALPHA) {
Chet Haase21433372012-06-05 07:54:09 -070013192 if (alpha < 1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013193 mPrivateFlags3 |= PFLAG3_VIEW_IS_ANIMATING_ALPHA;
Chet Haase21433372012-06-05 07:54:09 -070013194 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013195 mPrivateFlags3 &= ~PFLAG3_VIEW_IS_ANIMATING_ALPHA;
Chet Haase21433372012-06-05 07:54:09 -070013196 }
Chet Haasea1cff502012-02-21 13:43:44 -080013197 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013198 if (hasNoCache) {
13199 final int multipliedAlpha = (int) (255 * alpha);
13200 if (!onSetAlpha(multipliedAlpha)) {
13201 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080013202 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080013203 layerType != LAYER_TYPE_NONE) {
13204 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
13205 }
Chet Haase9420abd2012-03-29 16:28:32 -070013206 if (useDisplayListProperties) {
13207 displayList.setAlpha(alpha * getAlpha());
13208 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070013209 final int scrollX = hasDisplayList ? 0 : sx;
13210 final int scrollY = hasDisplayList ? 0 : sy;
13211 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
13212 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080013213 }
13214 } else {
13215 // Alpha is handled by the child directly, clobber the layer's alpha
Dianne Hackborn4702a852012-08-17 15:18:29 -070013216 mPrivateFlags |= PFLAG_ALPHA_SET;
Chet Haase64a48c12012-02-13 16:33:29 -080013217 }
13218 }
13219 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013220 } else if ((mPrivateFlags & PFLAG_ALPHA_SET) == PFLAG_ALPHA_SET) {
Chet Haase64a48c12012-02-13 16:33:29 -080013221 onSetAlpha(255);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013222 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase64a48c12012-02-13 16:33:29 -080013223 }
13224
Chet Haasea1cff502012-02-21 13:43:44 -080013225 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
13226 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013227 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013228 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080013229 } else {
13230 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013231 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080013232 } else {
13233 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
13234 }
13235 }
13236 }
13237
Chet Haase9420abd2012-03-29 16:28:32 -070013238 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080013239 displayList = getDisplayList();
13240 if (!displayList.isValid()) {
13241 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13242 // to getDisplayList(), the display list will be marked invalid and we should not
13243 // try to use it again.
13244 displayList = null;
13245 hasDisplayList = false;
13246 }
13247 }
13248
13249 if (hasNoCache) {
13250 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080013251 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080013252 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080013253 if (layer != null && layer.isValid()) {
13254 mLayerPaint.setAlpha((int) (alpha * 255));
13255 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
13256 layerRendered = true;
13257 } else {
13258 final int scrollX = hasDisplayList ? 0 : sx;
13259 final int scrollY = hasDisplayList ? 0 : sy;
13260 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080013261 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080013262 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
13263 }
13264 }
13265
13266 if (!layerRendered) {
13267 if (!hasDisplayList) {
13268 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070013269 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
13270 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase64a48c12012-02-13 16:33:29 -080013271 dispatchDraw(canvas);
13272 } else {
13273 draw(canvas);
13274 }
13275 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013276 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase1271e2c2012-04-20 09:54:27 -070013277 ((HardwareCanvas) canvas).drawDisplayList(displayList, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080013278 }
13279 }
13280 } else if (cache != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013281 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase64a48c12012-02-13 16:33:29 -080013282 Paint cachePaint;
13283
13284 if (layerType == LAYER_TYPE_NONE) {
13285 cachePaint = parent.mCachePaint;
13286 if (cachePaint == null) {
13287 cachePaint = new Paint();
13288 cachePaint.setDither(false);
13289 parent.mCachePaint = cachePaint;
13290 }
Chet Haase9420abd2012-03-29 16:28:32 -070013291 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080013292 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080013293 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
13294 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013295 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080013296 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080013297 }
13298 } else {
13299 cachePaint = mLayerPaint;
13300 cachePaint.setAlpha((int) (alpha * 255));
13301 }
13302 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
13303 }
13304
Chet Haasea1cff502012-02-21 13:43:44 -080013305 if (restoreTo >= 0) {
13306 canvas.restoreToCount(restoreTo);
13307 }
Chet Haase64a48c12012-02-13 16:33:29 -080013308
13309 if (a != null && !more) {
13310 if (!hardwareAccelerated && !a.getFillAfter()) {
13311 onSetAlpha(255);
13312 }
13313 parent.finishAnimatingView(this, a);
13314 }
13315
13316 if (more && hardwareAccelerated) {
13317 // invalidation is the trigger to recreate display lists, so if we're using
13318 // display lists to render, force an invalidate to allow the animation to
13319 // continue drawing another frame
13320 parent.invalidate(true);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013321 if (a.hasAlpha() && (mPrivateFlags & PFLAG_ALPHA_SET) == PFLAG_ALPHA_SET) {
Chet Haase64a48c12012-02-13 16:33:29 -080013322 // alpha animations should cause the child to recreate its display list
13323 invalidate(true);
13324 }
13325 }
13326
13327 mRecreateDisplayList = false;
13328
13329 return more;
13330 }
13331
13332 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013333 * Manually render this view (and all of its children) to the given Canvas.
13334 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070013335 * called. When implementing a view, implement
13336 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
13337 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013338 *
13339 * @param canvas The Canvas to which the View is rendered.
13340 */
13341 public void draw(Canvas canvas) {
Romain Guy5bcdff42009-05-14 21:27:18 -070013342 final int privateFlags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070013343 final boolean dirtyOpaque = (privateFlags & PFLAG_DIRTY_MASK) == PFLAG_DIRTY_OPAQUE &&
Romain Guy5bcdff42009-05-14 21:27:18 -070013344 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013345 mPrivateFlags = (privateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070013346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013347 /*
13348 * Draw traversal performs several drawing steps which must be executed
13349 * in the appropriate order:
13350 *
13351 * 1. Draw the background
13352 * 2. If necessary, save the canvas' layers to prepare for fading
13353 * 3. Draw view's content
13354 * 4. Draw children
13355 * 5. If necessary, draw the fading edges and restore layers
13356 * 6. Draw decorations (scrollbars for instance)
13357 */
13358
13359 // Step 1, draw the background, if needed
13360 int saveCount;
13361
Romain Guy24443ea2009-05-11 11:56:30 -070013362 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013363 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070013364 if (background != null) {
13365 final int scrollX = mScrollX;
13366 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013367
Romain Guy24443ea2009-05-11 11:56:30 -070013368 if (mBackgroundSizeChanged) {
13369 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
13370 mBackgroundSizeChanged = false;
13371 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013372
Romain Guy24443ea2009-05-11 11:56:30 -070013373 if ((scrollX | scrollY) == 0) {
13374 background.draw(canvas);
13375 } else {
13376 canvas.translate(scrollX, scrollY);
13377 background.draw(canvas);
13378 canvas.translate(-scrollX, -scrollY);
13379 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013380 }
13381 }
13382
13383 // skip step 2 & 5 if possible (common case)
13384 final int viewFlags = mViewFlags;
13385 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
13386 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
13387 if (!verticalEdges && !horizontalEdges) {
13388 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013389 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013390
13391 // Step 4, draw the children
13392 dispatchDraw(canvas);
13393
13394 // Step 6, draw decorations (scrollbars)
13395 onDrawScrollBars(canvas);
13396
13397 // we're done...
13398 return;
13399 }
13400
13401 /*
13402 * Here we do the full fledged routine...
13403 * (this is an uncommon case where speed matters less,
13404 * this is why we repeat some of the tests that have been
13405 * done above)
13406 */
13407
13408 boolean drawTop = false;
13409 boolean drawBottom = false;
13410 boolean drawLeft = false;
13411 boolean drawRight = false;
13412
13413 float topFadeStrength = 0.0f;
13414 float bottomFadeStrength = 0.0f;
13415 float leftFadeStrength = 0.0f;
13416 float rightFadeStrength = 0.0f;
13417
13418 // Step 2, save the canvas' layers
13419 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013420
13421 final boolean offsetRequired = isPaddingOffsetRequired();
13422 if (offsetRequired) {
13423 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013424 }
13425
13426 int left = mScrollX + paddingLeft;
13427 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070013428 int top = mScrollY + getFadeTop(offsetRequired);
13429 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013430
13431 if (offsetRequired) {
13432 right += getRightPaddingOffset();
13433 bottom += getBottomPaddingOffset();
13434 }
13435
13436 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070013437 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013438 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013439
13440 // clip the fade length if top and bottom fades overlap
13441 // overlapping fades produce odd-looking artifacts
13442 if (verticalEdges && (top + length > bottom - length)) {
13443 length = (bottom - top) / 2;
13444 }
13445
13446 // also clip horizontal fades if necessary
13447 if (horizontalEdges && (left + length > right - length)) {
13448 length = (right - left) / 2;
13449 }
13450
13451 if (verticalEdges) {
13452 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013453 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013454 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013455 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013456 }
13457
13458 if (horizontalEdges) {
13459 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013460 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013461 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013462 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013463 }
13464
13465 saveCount = canvas.getSaveCount();
13466
13467 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070013468 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013469 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
13470
13471 if (drawTop) {
13472 canvas.saveLayer(left, top, right, top + length, null, flags);
13473 }
13474
13475 if (drawBottom) {
13476 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
13477 }
13478
13479 if (drawLeft) {
13480 canvas.saveLayer(left, top, left + length, bottom, null, flags);
13481 }
13482
13483 if (drawRight) {
13484 canvas.saveLayer(right - length, top, right, bottom, null, flags);
13485 }
13486 } else {
13487 scrollabilityCache.setFadeColor(solidColor);
13488 }
13489
13490 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013491 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013492
13493 // Step 4, draw the children
13494 dispatchDraw(canvas);
13495
13496 // Step 5, draw the fade effect and restore layers
13497 final Paint p = scrollabilityCache.paint;
13498 final Matrix matrix = scrollabilityCache.matrix;
13499 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013500
13501 if (drawTop) {
13502 matrix.setScale(1, fadeHeight * topFadeStrength);
13503 matrix.postTranslate(left, top);
13504 fade.setLocalMatrix(matrix);
13505 canvas.drawRect(left, top, right, top + length, p);
13506 }
13507
13508 if (drawBottom) {
13509 matrix.setScale(1, fadeHeight * bottomFadeStrength);
13510 matrix.postRotate(180);
13511 matrix.postTranslate(left, bottom);
13512 fade.setLocalMatrix(matrix);
13513 canvas.drawRect(left, bottom - length, right, bottom, p);
13514 }
13515
13516 if (drawLeft) {
13517 matrix.setScale(1, fadeHeight * leftFadeStrength);
13518 matrix.postRotate(-90);
13519 matrix.postTranslate(left, top);
13520 fade.setLocalMatrix(matrix);
13521 canvas.drawRect(left, top, left + length, bottom, p);
13522 }
13523
13524 if (drawRight) {
13525 matrix.setScale(1, fadeHeight * rightFadeStrength);
13526 matrix.postRotate(90);
13527 matrix.postTranslate(right, top);
13528 fade.setLocalMatrix(matrix);
13529 canvas.drawRect(right - length, top, right, bottom, p);
13530 }
13531
13532 canvas.restoreToCount(saveCount);
13533
13534 // Step 6, draw decorations (scrollbars)
13535 onDrawScrollBars(canvas);
13536 }
13537
13538 /**
13539 * Override this if your view is known to always be drawn on top of a solid color background,
13540 * and needs to draw fading edges. Returning a non-zero color enables the view system to
13541 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
13542 * should be set to 0xFF.
13543 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013544 * @see #setVerticalFadingEdgeEnabled(boolean)
13545 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013546 *
13547 * @return The known solid color background for this view, or 0 if the color may vary
13548 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013549 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013550 public int getSolidColor() {
13551 return 0;
13552 }
13553
13554 /**
13555 * Build a human readable string representation of the specified view flags.
13556 *
13557 * @param flags the view flags to convert to a string
13558 * @return a String representing the supplied flags
13559 */
13560 private static String printFlags(int flags) {
13561 String output = "";
13562 int numFlags = 0;
13563 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
13564 output += "TAKES_FOCUS";
13565 numFlags++;
13566 }
13567
13568 switch (flags & VISIBILITY_MASK) {
13569 case INVISIBLE:
13570 if (numFlags > 0) {
13571 output += " ";
13572 }
13573 output += "INVISIBLE";
13574 // USELESS HERE numFlags++;
13575 break;
13576 case GONE:
13577 if (numFlags > 0) {
13578 output += " ";
13579 }
13580 output += "GONE";
13581 // USELESS HERE numFlags++;
13582 break;
13583 default:
13584 break;
13585 }
13586 return output;
13587 }
13588
13589 /**
13590 * Build a human readable string representation of the specified private
13591 * view flags.
13592 *
13593 * @param privateFlags the private view flags to convert to a string
13594 * @return a String representing the supplied flags
13595 */
13596 private static String printPrivateFlags(int privateFlags) {
13597 String output = "";
13598 int numFlags = 0;
13599
Dianne Hackborn4702a852012-08-17 15:18:29 -070013600 if ((privateFlags & PFLAG_WANTS_FOCUS) == PFLAG_WANTS_FOCUS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013601 output += "WANTS_FOCUS";
13602 numFlags++;
13603 }
13604
Dianne Hackborn4702a852012-08-17 15:18:29 -070013605 if ((privateFlags & PFLAG_FOCUSED) == PFLAG_FOCUSED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013606 if (numFlags > 0) {
13607 output += " ";
13608 }
13609 output += "FOCUSED";
13610 numFlags++;
13611 }
13612
Dianne Hackborn4702a852012-08-17 15:18:29 -070013613 if ((privateFlags & PFLAG_SELECTED) == PFLAG_SELECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013614 if (numFlags > 0) {
13615 output += " ";
13616 }
13617 output += "SELECTED";
13618 numFlags++;
13619 }
13620
Dianne Hackborn4702a852012-08-17 15:18:29 -070013621 if ((privateFlags & PFLAG_IS_ROOT_NAMESPACE) == PFLAG_IS_ROOT_NAMESPACE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013622 if (numFlags > 0) {
13623 output += " ";
13624 }
13625 output += "IS_ROOT_NAMESPACE";
13626 numFlags++;
13627 }
13628
Dianne Hackborn4702a852012-08-17 15:18:29 -070013629 if ((privateFlags & PFLAG_HAS_BOUNDS) == PFLAG_HAS_BOUNDS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013630 if (numFlags > 0) {
13631 output += " ";
13632 }
13633 output += "HAS_BOUNDS";
13634 numFlags++;
13635 }
13636
Dianne Hackborn4702a852012-08-17 15:18:29 -070013637 if ((privateFlags & PFLAG_DRAWN) == PFLAG_DRAWN) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013638 if (numFlags > 0) {
13639 output += " ";
13640 }
13641 output += "DRAWN";
13642 // USELESS HERE numFlags++;
13643 }
13644 return output;
13645 }
13646
13647 /**
13648 * <p>Indicates whether or not this view's layout will be requested during
13649 * the next hierarchy layout pass.</p>
13650 *
13651 * @return true if the layout will be forced during next layout pass
13652 */
13653 public boolean isLayoutRequested() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013654 return (mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013655 }
13656
13657 /**
13658 * Assign a size and position to a view and all of its
13659 * descendants
13660 *
13661 * <p>This is the second phase of the layout mechanism.
13662 * (The first is measuring). In this phase, each parent calls
13663 * layout on all of its children to position them.
13664 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080013665 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013666 *
Chet Haase9c087442011-01-12 16:20:16 -080013667 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013668 * Derived classes with children should override
13669 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080013670 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013671 *
13672 * @param l Left position, relative to parent
13673 * @param t Top position, relative to parent
13674 * @param r Right position, relative to parent
13675 * @param b Bottom position, relative to parent
13676 */
Romain Guy5429e1d2010-09-07 12:38:00 -070013677 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080013678 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070013679 int oldL = mLeft;
13680 int oldT = mTop;
13681 int oldB = mBottom;
13682 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013683 boolean changed = setFrame(l, t, r, b);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013684 if (changed || (mPrivateFlags & PFLAG_LAYOUT_REQUIRED) == PFLAG_LAYOUT_REQUIRED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013685 onLayout(changed, l, t, r, b);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013686 mPrivateFlags &= ~PFLAG_LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070013687
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013688 ListenerInfo li = mListenerInfo;
13689 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070013690 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013691 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070013692 int numListeners = listenersCopy.size();
13693 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070013694 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070013695 }
13696 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013697 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013698 mPrivateFlags &= ~PFLAG_FORCE_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013699 }
13700
13701 /**
13702 * Called from layout when this view should
13703 * assign a size and position to each of its children.
13704 *
13705 * Derived classes with children should override
13706 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070013707 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013708 * @param changed This is a new size or position for this view
13709 * @param left Left position, relative to parent
13710 * @param top Top position, relative to parent
13711 * @param right Right position, relative to parent
13712 * @param bottom Bottom position, relative to parent
13713 */
13714 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
13715 }
13716
13717 /**
13718 * Assign a size and position to this view.
13719 *
13720 * This is called from layout.
13721 *
13722 * @param left Left position, relative to parent
13723 * @param top Top position, relative to parent
13724 * @param right Right position, relative to parent
13725 * @param bottom Bottom position, relative to parent
13726 * @return true if the new size and position are different than the
13727 * previous ones
13728 * {@hide}
13729 */
13730 protected boolean setFrame(int left, int top, int right, int bottom) {
13731 boolean changed = false;
13732
13733 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070013734 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013735 + right + "," + bottom + ")");
13736 }
13737
13738 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
13739 changed = true;
13740
13741 // Remember our drawn bit
Dianne Hackborn4702a852012-08-17 15:18:29 -070013742 int drawn = mPrivateFlags & PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013744 int oldWidth = mRight - mLeft;
13745 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070013746 int newWidth = right - left;
13747 int newHeight = bottom - top;
13748 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
13749
13750 // Invalidate our old position
13751 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013752
13753 mLeft = left;
13754 mTop = top;
13755 mRight = right;
13756 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -070013757 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013758 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
13759 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013760
Dianne Hackborn4702a852012-08-17 15:18:29 -070013761 mPrivateFlags |= PFLAG_HAS_BOUNDS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013763
Chet Haase75755e22011-07-18 17:48:25 -070013764 if (sizeChanged) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013765 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013766 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013767 if (mTransformationInfo != null) {
13768 mTransformationInfo.mMatrixDirty = true;
13769 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013770 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013771 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
13772 }
13773
13774 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
13775 // If we are visible, force the DRAWN bit to on so that
13776 // this invalidate will go through (at least to our parent).
13777 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013778 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013779 // the DRAWN bit.
Dianne Hackborn4702a852012-08-17 15:18:29 -070013780 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070013781 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080013782 // parent display list may need to be recreated based on a change in the bounds
13783 // of any child
13784 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013785 }
13786
13787 // Reset drawn bit to original value (invalidate turns it off)
13788 mPrivateFlags |= drawn;
13789
13790 mBackgroundSizeChanged = true;
13791 }
13792 return changed;
13793 }
13794
13795 /**
13796 * Finalize inflating a view from XML. This is called as the last phase
13797 * of inflation, after all child views have been added.
13798 *
13799 * <p>Even if the subclass overrides onFinishInflate, they should always be
13800 * sure to call the super method, so that we get called.
13801 */
13802 protected void onFinishInflate() {
13803 }
13804
13805 /**
13806 * Returns the resources associated with this view.
13807 *
13808 * @return Resources object.
13809 */
13810 public Resources getResources() {
13811 return mResources;
13812 }
13813
13814 /**
13815 * Invalidates the specified Drawable.
13816 *
13817 * @param drawable the drawable to invalidate
13818 */
13819 public void invalidateDrawable(Drawable drawable) {
13820 if (verifyDrawable(drawable)) {
13821 final Rect dirty = drawable.getBounds();
13822 final int scrollX = mScrollX;
13823 final int scrollY = mScrollY;
13824
13825 invalidate(dirty.left + scrollX, dirty.top + scrollY,
13826 dirty.right + scrollX, dirty.bottom + scrollY);
13827 }
13828 }
13829
13830 /**
13831 * Schedules an action on a drawable to occur at a specified time.
13832 *
13833 * @param who the recipient of the action
13834 * @param what the action to run on the drawable
13835 * @param when the time at which the action must occur. Uses the
13836 * {@link SystemClock#uptimeMillis} timebase.
13837 */
13838 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080013839 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013840 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080013841 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013842 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
13843 Choreographer.CALLBACK_ANIMATION, what, who,
13844 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080013845 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013846 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080013847 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013848 }
13849 }
13850
13851 /**
13852 * Cancels a scheduled action on a drawable.
13853 *
13854 * @param who the recipient of the action
13855 * @param what the action to cancel
13856 */
13857 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080013858 if (verifyDrawable(who) && what != null) {
13859 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013860 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13861 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080013862 } else {
13863 ViewRootImpl.getRunQueue().removeCallbacks(what);
13864 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013865 }
13866 }
13867
13868 /**
13869 * Unschedule any events associated with the given Drawable. This can be
13870 * used when selecting a new Drawable into a view, so that the previous
13871 * one is completely unscheduled.
13872 *
13873 * @param who The Drawable to unschedule.
13874 *
13875 * @see #drawableStateChanged
13876 */
13877 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080013878 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013879 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13880 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013881 }
13882 }
13883
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013884 /**
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070013885 * Resolve the Drawables depending on the layout direction. This is implicitly supposing
13886 * that the View directionality can and will be resolved before its Drawables.
13887 *
13888 * Will call {@link View#onResolveDrawables} when resolution is done.
13889 */
13890 public void resolveDrawables() {
13891 if (mBackground != null) {
13892 mBackground.setLayoutDirection(getResolvedLayoutDirection());
13893 }
13894 onResolveDrawables(getResolvedLayoutDirection());
13895 }
13896
13897 /**
13898 * Called when layout direction has been resolved.
13899 *
13900 * The default implementation does nothing.
13901 *
13902 * @param layoutDirection The resolved layout direction.
13903 *
13904 * @see {@link #LAYOUT_DIRECTION_LTR}
13905 * @see {@link #LAYOUT_DIRECTION_RTL}
13906 */
13907 public void onResolveDrawables(int layoutDirection) {
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070013908 }
13909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013910 /**
13911 * If your view subclass is displaying its own Drawable objects, it should
13912 * override this function and return true for any Drawable it is
13913 * displaying. This allows animations for those drawables to be
13914 * scheduled.
13915 *
13916 * <p>Be sure to call through to the super class when overriding this
13917 * function.
13918 *
13919 * @param who The Drawable to verify. Return true if it is one you are
13920 * displaying, else return the result of calling through to the
13921 * super class.
13922 *
13923 * @return boolean If true than the Drawable is being displayed in the
13924 * view; else false and it is not allowed to animate.
13925 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013926 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
13927 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013928 */
13929 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013930 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013931 }
13932
13933 /**
13934 * This function is called whenever the state of the view changes in such
13935 * a way that it impacts the state of drawables being shown.
13936 *
13937 * <p>Be sure to call through to the superclass when overriding this
13938 * function.
13939 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013940 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013941 */
13942 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013943 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013944 if (d != null && d.isStateful()) {
13945 d.setState(getDrawableState());
13946 }
13947 }
13948
13949 /**
13950 * Call this to force a view to update its drawable state. This will cause
13951 * drawableStateChanged to be called on this view. Views that are interested
13952 * in the new state should call getDrawableState.
13953 *
13954 * @see #drawableStateChanged
13955 * @see #getDrawableState
13956 */
13957 public void refreshDrawableState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013958 mPrivateFlags |= PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013959 drawableStateChanged();
13960
13961 ViewParent parent = mParent;
13962 if (parent != null) {
13963 parent.childDrawableStateChanged(this);
13964 }
13965 }
13966
13967 /**
13968 * Return an array of resource IDs of the drawable states representing the
13969 * current state of the view.
13970 *
13971 * @return The current drawable state
13972 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013973 * @see Drawable#setState(int[])
13974 * @see #drawableStateChanged()
13975 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013976 */
13977 public final int[] getDrawableState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013978 if ((mDrawableState != null) && ((mPrivateFlags & PFLAG_DRAWABLE_STATE_DIRTY) == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013979 return mDrawableState;
13980 } else {
13981 mDrawableState = onCreateDrawableState(0);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013982 mPrivateFlags &= ~PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013983 return mDrawableState;
13984 }
13985 }
13986
13987 /**
13988 * Generate the new {@link android.graphics.drawable.Drawable} state for
13989 * this view. This is called by the view
13990 * system when the cached Drawable state is determined to be invalid. To
13991 * retrieve the current state, you should use {@link #getDrawableState}.
13992 *
13993 * @param extraSpace if non-zero, this is the number of extra entries you
13994 * would like in the returned array in which you can place your own
13995 * states.
13996 *
13997 * @return Returns an array holding the current {@link Drawable} state of
13998 * the view.
13999 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014000 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014001 */
14002 protected int[] onCreateDrawableState(int extraSpace) {
14003 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
14004 mParent instanceof View) {
14005 return ((View) mParent).onCreateDrawableState(extraSpace);
14006 }
14007
14008 int[] drawableState;
14009
14010 int privateFlags = mPrivateFlags;
14011
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014012 int viewStateIndex = 0;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014013 if ((privateFlags & PFLAG_PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014014 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
14015 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014016 if ((privateFlags & PFLAG_SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014017 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014018 if ((privateFlags & PFLAG_ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070014019 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
14020 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080014021 // This is set if HW acceleration is requested, even if the current
14022 // process doesn't allow it. This is just to allow app preview
14023 // windows to better match their app.
14024 viewStateIndex |= VIEW_STATE_ACCELERATED;
14025 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070014026 if ((privateFlags & PFLAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014027
Christopher Tate3d4bf172011-03-28 16:16:46 -070014028 final int privateFlags2 = mPrivateFlags2;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014029 if ((privateFlags2 & PFLAG2_DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
14030 if ((privateFlags2 & PFLAG2_DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
Christopher Tate3d4bf172011-03-28 16:16:46 -070014031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014032 drawableState = VIEW_STATE_SETS[viewStateIndex];
14033
14034 //noinspection ConstantIfStatement
14035 if (false) {
14036 Log.i("View", "drawableStateIndex=" + viewStateIndex);
14037 Log.i("View", toString()
Dianne Hackborn4702a852012-08-17 15:18:29 -070014038 + " pressed=" + ((privateFlags & PFLAG_PRESSED) != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014039 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
14040 + " fo=" + hasFocus()
Dianne Hackborn4702a852012-08-17 15:18:29 -070014041 + " sl=" + ((privateFlags & PFLAG_SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014042 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014043 + ": " + Arrays.toString(drawableState));
14044 }
14045
14046 if (extraSpace == 0) {
14047 return drawableState;
14048 }
14049
14050 final int[] fullState;
14051 if (drawableState != null) {
14052 fullState = new int[drawableState.length + extraSpace];
14053 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
14054 } else {
14055 fullState = new int[extraSpace];
14056 }
14057
14058 return fullState;
14059 }
14060
14061 /**
14062 * Merge your own state values in <var>additionalState</var> into the base
14063 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014064 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014065 *
14066 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014067 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014068 * own additional state values.
14069 *
14070 * @param additionalState The additional state values you would like
14071 * added to <var>baseState</var>; this array is not modified.
14072 *
14073 * @return As a convenience, the <var>baseState</var> array you originally
14074 * passed into the function is returned.
14075 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014076 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014077 */
14078 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
14079 final int N = baseState.length;
14080 int i = N - 1;
14081 while (i >= 0 && baseState[i] == 0) {
14082 i--;
14083 }
14084 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
14085 return baseState;
14086 }
14087
14088 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070014089 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
14090 * on all Drawable objects associated with this view.
14091 */
14092 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014093 if (mBackground != null) {
14094 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070014095 }
14096 }
14097
14098 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014099 * Sets the background color for this view.
14100 * @param color the color of the background
14101 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014102 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014103 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070014104 if (mBackground instanceof ColorDrawable) {
14105 ((ColorDrawable) mBackground).setColor(color);
Romain Guy7d3082a2012-07-11 17:52:54 -070014106 computeOpaqueFlags();
Chet Haase70d4ba12010-10-06 09:46:45 -070014107 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070014108 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070014109 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014110 }
14111
14112 /**
14113 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070014114 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014115 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070014116 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014117 * @attr ref android.R.styleable#View_background
14118 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014119 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014120 public void setBackgroundResource(int resid) {
14121 if (resid != 0 && resid == mBackgroundResource) {
14122 return;
14123 }
14124
14125 Drawable d= null;
14126 if (resid != 0) {
14127 d = mResources.getDrawable(resid);
14128 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014129 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014130
14131 mBackgroundResource = resid;
14132 }
14133
14134 /**
14135 * Set the background to a given Drawable, or remove the background. If the
14136 * background has padding, this View's padding is set to the background's
14137 * padding. However, when a background is removed, this View's padding isn't
14138 * touched. If setting the padding is desired, please use
14139 * {@link #setPadding(int, int, int, int)}.
14140 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014141 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014142 * background
14143 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014144 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070014145 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070014146 setBackgroundDrawable(background);
14147 }
14148
14149 /**
14150 * @deprecated use {@link #setBackground(Drawable)} instead
14151 */
14152 @Deprecated
14153 public void setBackgroundDrawable(Drawable background) {
Romain Guy846a5332012-07-11 17:44:57 -070014154 computeOpaqueFlags();
14155
Philip Milne6c8ea062012-04-03 17:38:43 -070014156 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070014157 return;
14158 }
14159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014160 boolean requestLayout = false;
14161
14162 mBackgroundResource = 0;
14163
14164 /*
14165 * Regardless of whether we're setting a new background or not, we want
14166 * to clear the previous drawable.
14167 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014168 if (mBackground != null) {
14169 mBackground.setCallback(null);
14170 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014171 }
14172
Philip Milne6c8ea062012-04-03 17:38:43 -070014173 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014174 Rect padding = sThreadLocal.get();
14175 if (padding == null) {
14176 padding = new Rect();
14177 sThreadLocal.set(padding);
14178 }
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014179 background.setLayoutDirection(getResolvedLayoutDirection());
Philip Milne6c8ea062012-04-03 17:38:43 -070014180 if (background.getPadding(padding)) {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014181 // Reset padding resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070014182 mPrivateFlags2 &= ~PFLAG2_PADDING_RESOLVED;
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014183 switch (background.getLayoutDirection()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014184 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014185 internalSetPadding(padding.right, padding.top, padding.left, padding.bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014186 break;
14187 case LAYOUT_DIRECTION_LTR:
14188 default:
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014189 internalSetPadding(padding.left, padding.top, padding.right, padding.bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014190 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014191 }
14192
14193 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
14194 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070014195 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
14196 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014197 requestLayout = true;
14198 }
14199
Philip Milne6c8ea062012-04-03 17:38:43 -070014200 background.setCallback(this);
14201 if (background.isStateful()) {
14202 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014203 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014204 background.setVisible(getVisibility() == VISIBLE, false);
14205 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014206
Dianne Hackborn4702a852012-08-17 15:18:29 -070014207 if ((mPrivateFlags & PFLAG_SKIP_DRAW) != 0) {
14208 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
14209 mPrivateFlags |= PFLAG_ONLY_DRAWS_BACKGROUND;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014210 requestLayout = true;
14211 }
14212 } else {
14213 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070014214 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014215
Dianne Hackborn4702a852012-08-17 15:18:29 -070014216 if ((mPrivateFlags & PFLAG_ONLY_DRAWS_BACKGROUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014217 /*
14218 * This view ONLY drew the background before and we're removing
14219 * the background, so now it won't draw anything
14220 * (hence we SKIP_DRAW)
14221 */
Dianne Hackborn4702a852012-08-17 15:18:29 -070014222 mPrivateFlags &= ~PFLAG_ONLY_DRAWS_BACKGROUND;
14223 mPrivateFlags |= PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014224 }
14225
14226 /*
14227 * When the background is set, we try to apply its padding to this
14228 * View. When the background is removed, we don't touch this View's
14229 * padding. This is noted in the Javadocs. Hence, we don't need to
14230 * requestLayout(), the invalidate() below is sufficient.
14231 */
14232
14233 // The old background's minimum size could have affected this
14234 // View's layout, so let's requestLayout
14235 requestLayout = true;
14236 }
14237
Romain Guy8f1344f52009-05-15 16:03:59 -070014238 computeOpaqueFlags();
14239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014240 if (requestLayout) {
14241 requestLayout();
14242 }
14243
14244 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014245 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014246 }
14247
14248 /**
14249 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070014250 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014251 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070014252 *
14253 * @see #setBackground(Drawable)
14254 *
14255 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014256 */
14257 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014258 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014259 }
14260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014261 /**
14262 * Sets the padding. The view may add on the space required to display
14263 * the scrollbars, depending on the style and visibility of the scrollbars.
14264 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
14265 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
14266 * from the values set in this call.
14267 *
14268 * @attr ref android.R.styleable#View_padding
14269 * @attr ref android.R.styleable#View_paddingBottom
14270 * @attr ref android.R.styleable#View_paddingLeft
14271 * @attr ref android.R.styleable#View_paddingRight
14272 * @attr ref android.R.styleable#View_paddingTop
14273 * @param left the left padding in pixels
14274 * @param top the top padding in pixels
14275 * @param right the right padding in pixels
14276 * @param bottom the bottom padding in pixels
14277 */
14278 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014279 // Reset padding resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070014280 mPrivateFlags2 &= ~PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014281
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014282 mUserPaddingStart = UNDEFINED_PADDING;
14283 mUserPaddingEnd = UNDEFINED_PADDING;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014284
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014285 internalSetPadding(left, top, right, bottom);
14286 }
14287
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014288 /**
14289 * @hide
14290 */
14291 protected void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080014292 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014293 mUserPaddingRight = right;
14294 mUserPaddingBottom = bottom;
14295
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014296 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014297 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070014298
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014299 // Common case is there are no scroll bars.
14300 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014301 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080014302 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014303 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080014304 switch (mVerticalScrollbarPosition) {
14305 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Meglioc91b6ca2012-06-22 14:51:15 -070014306 if (isLayoutRtl()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014307 left += offset;
14308 } else {
14309 right += offset;
14310 }
14311 break;
Adam Powell20232d02010-12-08 21:08:53 -080014312 case SCROLLBAR_POSITION_RIGHT:
14313 right += offset;
14314 break;
14315 case SCROLLBAR_POSITION_LEFT:
14316 left += offset;
14317 break;
14318 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014319 }
Adam Powell20232d02010-12-08 21:08:53 -080014320 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014321 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
14322 ? 0 : getHorizontalScrollbarHeight();
14323 }
14324 }
Romain Guy8506ab42009-06-11 17:35:47 -070014325
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014326 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014327 changed = true;
14328 mPaddingLeft = left;
14329 }
14330 if (mPaddingTop != top) {
14331 changed = true;
14332 mPaddingTop = top;
14333 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014334 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014335 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014336 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014337 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014338 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014339 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014340 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014341 }
14342
14343 if (changed) {
14344 requestLayout();
14345 }
14346 }
14347
14348 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014349 * Sets the relative padding. The view may add on the space required to display
14350 * the scrollbars, depending on the style and visibility of the scrollbars.
14351 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
14352 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
14353 * from the values set in this call.
14354 *
14355 * @attr ref android.R.styleable#View_padding
14356 * @attr ref android.R.styleable#View_paddingBottom
14357 * @attr ref android.R.styleable#View_paddingStart
14358 * @attr ref android.R.styleable#View_paddingEnd
14359 * @attr ref android.R.styleable#View_paddingTop
14360 * @param start the start padding in pixels
14361 * @param top the top padding in pixels
14362 * @param end the end padding in pixels
14363 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014364 */
14365 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014366 // Reset padding resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070014367 mPrivateFlags2 &= ~PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014368
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014369 mUserPaddingStart = start;
14370 mUserPaddingEnd = end;
14371
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014372 switch(getResolvedLayoutDirection()) {
14373 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014374 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014375 break;
14376 case LAYOUT_DIRECTION_LTR:
14377 default:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014378 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014379 }
14380 }
14381
14382 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014383 * Returns the top padding of this view.
14384 *
14385 * @return the top padding in pixels
14386 */
14387 public int getPaddingTop() {
14388 return mPaddingTop;
14389 }
14390
14391 /**
14392 * Returns the bottom padding of this view. If there are inset and enabled
14393 * scrollbars, this value may include the space required to display the
14394 * scrollbars as well.
14395 *
14396 * @return the bottom padding in pixels
14397 */
14398 public int getPaddingBottom() {
14399 return mPaddingBottom;
14400 }
14401
14402 /**
14403 * Returns the left padding of this view. If there are inset and enabled
14404 * scrollbars, this value may include the space required to display the
14405 * scrollbars as well.
14406 *
14407 * @return the left padding in pixels
14408 */
14409 public int getPaddingLeft() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014410 if (!isPaddingResolved()) {
14411 resolvePadding();
14412 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014413 return mPaddingLeft;
14414 }
14415
14416 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014417 * Returns the start padding of this view depending on its resolved layout direction.
14418 * If there are inset and enabled scrollbars, this value may include the space
14419 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014420 *
14421 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014422 */
14423 public int getPaddingStart() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014424 if (!isPaddingResolved()) {
14425 resolvePadding();
14426 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014427 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14428 mPaddingRight : mPaddingLeft;
14429 }
14430
14431 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014432 * Returns the right padding of this view. If there are inset and enabled
14433 * scrollbars, this value may include the space required to display the
14434 * scrollbars as well.
14435 *
14436 * @return the right padding in pixels
14437 */
14438 public int getPaddingRight() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014439 if (!isPaddingResolved()) {
14440 resolvePadding();
14441 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014442 return mPaddingRight;
14443 }
14444
14445 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014446 * Returns the end padding of this view depending on its resolved layout direction.
14447 * If there are inset and enabled scrollbars, this value may include the space
14448 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014449 *
14450 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014451 */
14452 public int getPaddingEnd() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014453 if (!isPaddingResolved()) {
14454 resolvePadding();
14455 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014456 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14457 mPaddingLeft : mPaddingRight;
14458 }
14459
14460 /**
14461 * Return if the padding as been set thru relative values
14462 * {@link #setPaddingRelative(int, int, int, int)} or thru
14463 * @attr ref android.R.styleable#View_paddingStart or
14464 * @attr ref android.R.styleable#View_paddingEnd
14465 *
14466 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014467 */
14468 public boolean isPaddingRelative() {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014469 return (mUserPaddingStart != UNDEFINED_PADDING || mUserPaddingEnd != UNDEFINED_PADDING);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014470 }
14471
14472 /**
Philip Milne1557fd72012-04-04 23:41:34 -070014473 * @hide
14474 */
Philip Milne7a23b492012-04-24 22:12:36 -070014475 public Insets getOpticalInsets() {
Philip Milne1557fd72012-04-04 23:41:34 -070014476 if (mLayoutInsets == null) {
Philip Milnebbd51f12012-04-18 15:09:05 -070014477 mLayoutInsets = (mBackground == null) ? Insets.NONE : mBackground.getLayoutInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070014478 }
14479 return mLayoutInsets;
14480 }
14481
14482 /**
14483 * @hide
14484 */
14485 public void setLayoutInsets(Insets layoutInsets) {
14486 mLayoutInsets = layoutInsets;
14487 }
14488
14489 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014490 * Changes the selection state of this view. A view can be selected or not.
14491 * Note that selection is not the same as focus. Views are typically
14492 * selected in the context of an AdapterView like ListView or GridView;
14493 * the selected view is the view that is highlighted.
14494 *
14495 * @param selected true if the view must be selected, false otherwise
14496 */
14497 public void setSelected(boolean selected) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014498 if (((mPrivateFlags & PFLAG_SELECTED) != 0) != selected) {
14499 mPrivateFlags = (mPrivateFlags & ~PFLAG_SELECTED) | (selected ? PFLAG_SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070014500 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080014501 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014502 refreshDrawableState();
14503 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070014504 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
14505 notifyAccessibilityStateChanged();
14506 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014507 }
14508 }
14509
14510 /**
14511 * Dispatch setSelected to all of this View's children.
14512 *
14513 * @see #setSelected(boolean)
14514 *
14515 * @param selected The new selected state
14516 */
14517 protected void dispatchSetSelected(boolean selected) {
14518 }
14519
14520 /**
14521 * Indicates the selection state of this view.
14522 *
14523 * @return true if the view is selected, false otherwise
14524 */
14525 @ViewDebug.ExportedProperty
14526 public boolean isSelected() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014527 return (mPrivateFlags & PFLAG_SELECTED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014528 }
14529
14530 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014531 * Changes the activated state of this view. A view can be activated or not.
14532 * Note that activation is not the same as selection. Selection is
14533 * a transient property, representing the view (hierarchy) the user is
14534 * currently interacting with. Activation is a longer-term state that the
14535 * user can move views in and out of. For example, in a list view with
14536 * single or multiple selection enabled, the views in the current selection
14537 * set are activated. (Um, yeah, we are deeply sorry about the terminology
14538 * here.) The activated state is propagated down to children of the view it
14539 * is set on.
14540 *
14541 * @param activated true if the view must be activated, false otherwise
14542 */
14543 public void setActivated(boolean activated) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014544 if (((mPrivateFlags & PFLAG_ACTIVATED) != 0) != activated) {
14545 mPrivateFlags = (mPrivateFlags & ~PFLAG_ACTIVATED) | (activated ? PFLAG_ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014546 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014547 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070014548 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014549 }
14550 }
14551
14552 /**
14553 * Dispatch setActivated to all of this View's children.
14554 *
14555 * @see #setActivated(boolean)
14556 *
14557 * @param activated The new activated state
14558 */
14559 protected void dispatchSetActivated(boolean activated) {
14560 }
14561
14562 /**
14563 * Indicates the activation state of this view.
14564 *
14565 * @return true if the view is activated, false otherwise
14566 */
14567 @ViewDebug.ExportedProperty
14568 public boolean isActivated() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014569 return (mPrivateFlags & PFLAG_ACTIVATED) != 0;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014570 }
14571
14572 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014573 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
14574 * observer can be used to get notifications when global events, like
14575 * layout, happen.
14576 *
14577 * The returned ViewTreeObserver observer is not guaranteed to remain
14578 * valid for the lifetime of this View. If the caller of this method keeps
14579 * a long-lived reference to ViewTreeObserver, it should always check for
14580 * the return value of {@link ViewTreeObserver#isAlive()}.
14581 *
14582 * @return The ViewTreeObserver for this view's hierarchy.
14583 */
14584 public ViewTreeObserver getViewTreeObserver() {
14585 if (mAttachInfo != null) {
14586 return mAttachInfo.mTreeObserver;
14587 }
14588 if (mFloatingTreeObserver == null) {
14589 mFloatingTreeObserver = new ViewTreeObserver();
14590 }
14591 return mFloatingTreeObserver;
14592 }
14593
14594 /**
14595 * <p>Finds the topmost view in the current view hierarchy.</p>
14596 *
14597 * @return the topmost view containing this view
14598 */
14599 public View getRootView() {
14600 if (mAttachInfo != null) {
14601 final View v = mAttachInfo.mRootView;
14602 if (v != null) {
14603 return v;
14604 }
14605 }
Romain Guy8506ab42009-06-11 17:35:47 -070014606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014607 View parent = this;
14608
14609 while (parent.mParent != null && parent.mParent instanceof View) {
14610 parent = (View) parent.mParent;
14611 }
14612
14613 return parent;
14614 }
14615
14616 /**
14617 * <p>Computes the coordinates of this view on the screen. The argument
14618 * must be an array of two integers. After the method returns, the array
14619 * contains the x and y location in that order.</p>
14620 *
14621 * @param location an array of two integers in which to hold the coordinates
14622 */
14623 public void getLocationOnScreen(int[] location) {
14624 getLocationInWindow(location);
14625
14626 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070014627 if (info != null) {
14628 location[0] += info.mWindowLeft;
14629 location[1] += info.mWindowTop;
14630 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014631 }
14632
14633 /**
14634 * <p>Computes the coordinates of this view in its window. The argument
14635 * must be an array of two integers. After the method returns, the array
14636 * contains the x and y location in that order.</p>
14637 *
14638 * @param location an array of two integers in which to hold the coordinates
14639 */
14640 public void getLocationInWindow(int[] location) {
14641 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014642 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014643 }
14644
Gilles Debunne6583ce52011-12-06 18:09:02 -080014645 if (mAttachInfo == null) {
14646 // When the view is not attached to a window, this method does not make sense
14647 location[0] = location[1] = 0;
14648 return;
14649 }
14650
Gilles Debunnecea45132011-11-24 02:19:27 +010014651 float[] position = mAttachInfo.mTmpTransformLocation;
14652 position[0] = position[1] = 0.0f;
14653
14654 if (!hasIdentityMatrix()) {
14655 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014656 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014657
Gilles Debunnecea45132011-11-24 02:19:27 +010014658 position[0] += mLeft;
14659 position[1] += mTop;
14660
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014661 ViewParent viewParent = mParent;
14662 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014663 final View view = (View) viewParent;
14664
14665 position[0] -= view.mScrollX;
14666 position[1] -= view.mScrollY;
14667
14668 if (!view.hasIdentityMatrix()) {
14669 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014670 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014671
14672 position[0] += view.mLeft;
14673 position[1] += view.mTop;
14674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014675 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070014676 }
Romain Guy8506ab42009-06-11 17:35:47 -070014677
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014678 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014679 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010014680 final ViewRootImpl vr = (ViewRootImpl) viewParent;
14681 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014682 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014683
14684 location[0] = (int) (position[0] + 0.5f);
14685 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014686 }
14687
14688 /**
14689 * {@hide}
14690 * @param id the id of the view to be found
14691 * @return the view of the specified id, null if cannot be found
14692 */
14693 protected View findViewTraversal(int id) {
14694 if (id == mID) {
14695 return this;
14696 }
14697 return null;
14698 }
14699
14700 /**
14701 * {@hide}
14702 * @param tag the tag of the view to be found
14703 * @return the view of specified tag, null if cannot be found
14704 */
14705 protected View findViewWithTagTraversal(Object tag) {
14706 if (tag != null && tag.equals(mTag)) {
14707 return this;
14708 }
14709 return null;
14710 }
14711
14712 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014713 * {@hide}
14714 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070014715 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080014716 * @return The first view that matches the predicate or null.
14717 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070014718 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080014719 if (predicate.apply(this)) {
14720 return this;
14721 }
14722 return null;
14723 }
14724
14725 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014726 * Look for a child view with the given id. If this view has the given
14727 * id, return this view.
14728 *
14729 * @param id The id to search for.
14730 * @return The view that has the given id in the hierarchy or null
14731 */
14732 public final View findViewById(int id) {
14733 if (id < 0) {
14734 return null;
14735 }
14736 return findViewTraversal(id);
14737 }
14738
14739 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070014740 * Finds a view by its unuque and stable accessibility id.
14741 *
14742 * @param accessibilityId The searched accessibility id.
14743 * @return The found view.
14744 */
14745 final View findViewByAccessibilityId(int accessibilityId) {
14746 if (accessibilityId < 0) {
14747 return null;
14748 }
14749 return findViewByAccessibilityIdTraversal(accessibilityId);
14750 }
14751
14752 /**
14753 * Performs the traversal to find a view by its unuque and stable accessibility id.
14754 *
14755 * <strong>Note:</strong>This method does not stop at the root namespace
14756 * boundary since the user can touch the screen at an arbitrary location
14757 * potentially crossing the root namespace bounday which will send an
14758 * accessibility event to accessibility services and they should be able
14759 * to obtain the event source. Also accessibility ids are guaranteed to be
14760 * unique in the window.
14761 *
14762 * @param accessibilityId The accessibility id.
14763 * @return The found view.
14764 */
14765 View findViewByAccessibilityIdTraversal(int accessibilityId) {
14766 if (getAccessibilityViewId() == accessibilityId) {
14767 return this;
14768 }
14769 return null;
14770 }
14771
14772 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014773 * Look for a child view with the given tag. If this view has the given
14774 * tag, return this view.
14775 *
14776 * @param tag The tag to search for, using "tag.equals(getTag())".
14777 * @return The View that has the given tag in the hierarchy or null
14778 */
14779 public final View findViewWithTag(Object tag) {
14780 if (tag == null) {
14781 return null;
14782 }
14783 return findViewWithTagTraversal(tag);
14784 }
14785
14786 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014787 * {@hide}
14788 * Look for a child view that matches the specified predicate.
14789 * If this view matches the predicate, return this view.
14790 *
14791 * @param predicate The predicate to evaluate.
14792 * @return The first view that matches the predicate or null.
14793 */
14794 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070014795 return findViewByPredicateTraversal(predicate, null);
14796 }
14797
14798 /**
14799 * {@hide}
14800 * Look for a child view that matches the specified predicate,
14801 * starting with the specified view and its descendents and then
14802 * recusively searching the ancestors and siblings of that view
14803 * until this view is reached.
14804 *
14805 * This method is useful in cases where the predicate does not match
14806 * a single unique view (perhaps multiple views use the same id)
14807 * and we are trying to find the view that is "closest" in scope to the
14808 * starting view.
14809 *
14810 * @param start The view to start from.
14811 * @param predicate The predicate to evaluate.
14812 * @return The first view that matches the predicate or null.
14813 */
14814 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
14815 View childToSkip = null;
14816 for (;;) {
14817 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
14818 if (view != null || start == this) {
14819 return view;
14820 }
14821
14822 ViewParent parent = start.getParent();
14823 if (parent == null || !(parent instanceof View)) {
14824 return null;
14825 }
14826
14827 childToSkip = start;
14828 start = (View) parent;
14829 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080014830 }
14831
14832 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014833 * Sets the identifier for this view. The identifier does not have to be
14834 * unique in this view's hierarchy. The identifier should be a positive
14835 * number.
14836 *
14837 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070014838 * @see #getId()
14839 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014840 *
14841 * @param id a number used to identify the view
14842 *
14843 * @attr ref android.R.styleable#View_id
14844 */
14845 public void setId(int id) {
14846 mID = id;
14847 }
14848
14849 /**
14850 * {@hide}
14851 *
14852 * @param isRoot true if the view belongs to the root namespace, false
14853 * otherwise
14854 */
14855 public void setIsRootNamespace(boolean isRoot) {
14856 if (isRoot) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014857 mPrivateFlags |= PFLAG_IS_ROOT_NAMESPACE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014858 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014859 mPrivateFlags &= ~PFLAG_IS_ROOT_NAMESPACE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014860 }
14861 }
14862
14863 /**
14864 * {@hide}
14865 *
14866 * @return true if the view belongs to the root namespace, false otherwise
14867 */
14868 public boolean isRootNamespace() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014869 return (mPrivateFlags&PFLAG_IS_ROOT_NAMESPACE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014870 }
14871
14872 /**
14873 * Returns this view's identifier.
14874 *
14875 * @return a positive integer used to identify the view or {@link #NO_ID}
14876 * if the view has no ID
14877 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014878 * @see #setId(int)
14879 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014880 * @attr ref android.R.styleable#View_id
14881 */
14882 @ViewDebug.CapturedViewProperty
14883 public int getId() {
14884 return mID;
14885 }
14886
14887 /**
14888 * Returns this view's tag.
14889 *
14890 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070014891 *
14892 * @see #setTag(Object)
14893 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014894 */
14895 @ViewDebug.ExportedProperty
14896 public Object getTag() {
14897 return mTag;
14898 }
14899
14900 /**
14901 * Sets the tag associated with this view. A tag can be used to mark
14902 * a view in its hierarchy and does not have to be unique within the
14903 * hierarchy. Tags can also be used to store data within a view without
14904 * resorting to another data structure.
14905 *
14906 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070014907 *
14908 * @see #getTag()
14909 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014910 */
14911 public void setTag(final Object tag) {
14912 mTag = tag;
14913 }
14914
14915 /**
Romain Guyd90a3312009-05-06 14:54:28 -070014916 * Returns the tag associated with this view and the specified key.
14917 *
14918 * @param key The key identifying the tag
14919 *
14920 * @return the Object stored in this view as a tag
14921 *
14922 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070014923 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070014924 */
14925 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014926 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070014927 return null;
14928 }
14929
14930 /**
14931 * Sets a tag associated with this view and a key. A tag can be used
14932 * to mark a view in its hierarchy and does not have to be unique within
14933 * the hierarchy. Tags can also be used to store data within a view
14934 * without resorting to another data structure.
14935 *
14936 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070014937 * application to ensure it is unique (see the <a
14938 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
14939 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070014940 * the Android framework or not associated with any package will cause
14941 * an {@link IllegalArgumentException} to be thrown.
14942 *
14943 * @param key The key identifying the tag
14944 * @param tag An Object to tag the view with
14945 *
14946 * @throws IllegalArgumentException If they specified key is not valid
14947 *
14948 * @see #setTag(Object)
14949 * @see #getTag(int)
14950 */
14951 public void setTag(int key, final Object tag) {
14952 // If the package id is 0x00 or 0x01, it's either an undefined package
14953 // or a framework id
14954 if ((key >>> 24) < 2) {
14955 throw new IllegalArgumentException("The key must be an application-specific "
14956 + "resource id.");
14957 }
14958
Adam Powell2b2f6d62011-09-23 11:15:39 -070014959 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014960 }
14961
14962 /**
14963 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
14964 * framework id.
14965 *
14966 * @hide
14967 */
14968 public void setTagInternal(int key, Object tag) {
14969 if ((key >>> 24) != 0x1) {
14970 throw new IllegalArgumentException("The key must be a framework-specific "
14971 + "resource id.");
14972 }
14973
Adam Powell2b2f6d62011-09-23 11:15:39 -070014974 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014975 }
14976
Adam Powell2b2f6d62011-09-23 11:15:39 -070014977 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014978 if (mKeyedTags == null) {
14979 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070014980 }
14981
Adam Powell7db82ac2011-09-22 19:44:04 -070014982 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014983 }
14984
14985 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014986 * Prints information about this view in the log output, with the tag
14987 * {@link #VIEW_LOG_TAG}.
14988 *
14989 * @hide
14990 */
14991 public void debug() {
14992 debug(0);
14993 }
14994
14995 /**
14996 * Prints information about this view in the log output, with the tag
14997 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
14998 * indentation defined by the <code>depth</code>.
14999 *
15000 * @param depth the indentation level
15001 *
15002 * @hide
15003 */
15004 protected void debug(int depth) {
15005 String output = debugIndent(depth - 1);
15006
15007 output += "+ " + this;
15008 int id = getId();
15009 if (id != -1) {
15010 output += " (id=" + id + ")";
15011 }
15012 Object tag = getTag();
15013 if (tag != null) {
15014 output += " (tag=" + tag + ")";
15015 }
15016 Log.d(VIEW_LOG_TAG, output);
15017
Dianne Hackborn4702a852012-08-17 15:18:29 -070015018 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015019 output = debugIndent(depth) + " FOCUSED";
15020 Log.d(VIEW_LOG_TAG, output);
15021 }
15022
15023 output = debugIndent(depth);
15024 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
15025 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
15026 + "} ";
15027 Log.d(VIEW_LOG_TAG, output);
15028
15029 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
15030 || mPaddingBottom != 0) {
15031 output = debugIndent(depth);
15032 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
15033 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
15034 Log.d(VIEW_LOG_TAG, output);
15035 }
15036
15037 output = debugIndent(depth);
15038 output += "mMeasureWidth=" + mMeasuredWidth +
15039 " mMeasureHeight=" + mMeasuredHeight;
15040 Log.d(VIEW_LOG_TAG, output);
15041
15042 output = debugIndent(depth);
15043 if (mLayoutParams == null) {
15044 output += "BAD! no layout params";
15045 } else {
15046 output = mLayoutParams.debug(output);
15047 }
15048 Log.d(VIEW_LOG_TAG, output);
15049
15050 output = debugIndent(depth);
15051 output += "flags={";
15052 output += View.printFlags(mViewFlags);
15053 output += "}";
15054 Log.d(VIEW_LOG_TAG, output);
15055
15056 output = debugIndent(depth);
15057 output += "privateFlags={";
15058 output += View.printPrivateFlags(mPrivateFlags);
15059 output += "}";
15060 Log.d(VIEW_LOG_TAG, output);
15061 }
15062
15063 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090015064 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015065 *
15066 * @param depth the indentation level
15067 * @return a String containing (depth * 2 + 3) * 2 white spaces
15068 *
15069 * @hide
15070 */
15071 protected static String debugIndent(int depth) {
15072 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
15073 for (int i = 0; i < (depth * 2) + 3; i++) {
15074 spaces.append(' ').append(' ');
15075 }
15076 return spaces.toString();
15077 }
15078
15079 /**
15080 * <p>Return the offset of the widget's text baseline from the widget's top
15081 * boundary. If this widget does not support baseline alignment, this
15082 * method returns -1. </p>
15083 *
15084 * @return the offset of the baseline within the widget's bounds or -1
15085 * if baseline alignment is not supported
15086 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070015087 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015088 public int getBaseline() {
15089 return -1;
15090 }
15091
15092 /**
15093 * Call this when something has changed which has invalidated the
15094 * layout of this view. This will schedule a layout pass of the view
15095 * tree.
15096 */
15097 public void requestLayout() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015098 mPrivateFlags |= PFLAG_FORCE_LAYOUT;
15099 mPrivateFlags |= PFLAG_INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015100
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070015101 if (mParent != null && !mParent.isLayoutRequested()) {
15102 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015103 }
15104 }
15105
15106 /**
15107 * Forces this view to be laid out during the next layout pass.
15108 * This method does not call requestLayout() or forceLayout()
15109 * on the parent.
15110 */
15111 public void forceLayout() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015112 mPrivateFlags |= PFLAG_FORCE_LAYOUT;
15113 mPrivateFlags |= PFLAG_INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015114 }
15115
15116 /**
15117 * <p>
15118 * This is called to find out how big a view should be. The parent
15119 * supplies constraint information in the width and height parameters.
15120 * </p>
15121 *
15122 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080015123 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015124 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080015125 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015126 * </p>
15127 *
15128 *
15129 * @param widthMeasureSpec Horizontal space requirements as imposed by the
15130 * parent
15131 * @param heightMeasureSpec Vertical space requirements as imposed by the
15132 * parent
15133 *
15134 * @see #onMeasure(int, int)
15135 */
15136 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015137 if ((mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015138 widthMeasureSpec != mOldWidthMeasureSpec ||
15139 heightMeasureSpec != mOldHeightMeasureSpec) {
15140
15141 // first clears the measured dimension flag
Dianne Hackborn4702a852012-08-17 15:18:29 -070015142 mPrivateFlags &= ~PFLAG_MEASURED_DIMENSION_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015143
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070015144 if (!isPaddingResolved()) {
Adam Powell0090f202012-08-07 17:15:30 -070015145 resolvePadding();
15146 }
15147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015148 // measure ourselves, this should set the measured dimension flag back
15149 onMeasure(widthMeasureSpec, heightMeasureSpec);
15150
15151 // flag not set, setMeasuredDimension() was not invoked, we raise
15152 // an exception to warn the developer
Dianne Hackborn4702a852012-08-17 15:18:29 -070015153 if ((mPrivateFlags & PFLAG_MEASURED_DIMENSION_SET) != PFLAG_MEASURED_DIMENSION_SET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015154 throw new IllegalStateException("onMeasure() did not set the"
15155 + " measured dimension by calling"
15156 + " setMeasuredDimension()");
15157 }
15158
Dianne Hackborn4702a852012-08-17 15:18:29 -070015159 mPrivateFlags |= PFLAG_LAYOUT_REQUIRED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015160 }
15161
15162 mOldWidthMeasureSpec = widthMeasureSpec;
15163 mOldHeightMeasureSpec = heightMeasureSpec;
15164 }
15165
15166 /**
15167 * <p>
15168 * Measure the view and its content to determine the measured width and the
15169 * measured height. This method is invoked by {@link #measure(int, int)} and
15170 * should be overriden by subclasses to provide accurate and efficient
15171 * measurement of their contents.
15172 * </p>
15173 *
15174 * <p>
15175 * <strong>CONTRACT:</strong> When overriding this method, you
15176 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
15177 * measured width and height of this view. Failure to do so will trigger an
15178 * <code>IllegalStateException</code>, thrown by
15179 * {@link #measure(int, int)}. Calling the superclass'
15180 * {@link #onMeasure(int, int)} is a valid use.
15181 * </p>
15182 *
15183 * <p>
15184 * The base class implementation of measure defaults to the background size,
15185 * unless a larger size is allowed by the MeasureSpec. Subclasses should
15186 * override {@link #onMeasure(int, int)} to provide better measurements of
15187 * their content.
15188 * </p>
15189 *
15190 * <p>
15191 * If this method is overridden, it is the subclass's responsibility to make
15192 * sure the measured height and width are at least the view's minimum height
15193 * and width ({@link #getSuggestedMinimumHeight()} and
15194 * {@link #getSuggestedMinimumWidth()}).
15195 * </p>
15196 *
15197 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
15198 * The requirements are encoded with
15199 * {@link android.view.View.MeasureSpec}.
15200 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
15201 * The requirements are encoded with
15202 * {@link android.view.View.MeasureSpec}.
15203 *
15204 * @see #getMeasuredWidth()
15205 * @see #getMeasuredHeight()
15206 * @see #setMeasuredDimension(int, int)
15207 * @see #getSuggestedMinimumHeight()
15208 * @see #getSuggestedMinimumWidth()
15209 * @see android.view.View.MeasureSpec#getMode(int)
15210 * @see android.view.View.MeasureSpec#getSize(int)
15211 */
15212 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
15213 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
15214 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
15215 }
15216
15217 /**
15218 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
15219 * measured width and measured height. Failing to do so will trigger an
15220 * exception at measurement time.</p>
15221 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080015222 * @param measuredWidth The measured width of this view. May be a complex
15223 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15224 * {@link #MEASURED_STATE_TOO_SMALL}.
15225 * @param measuredHeight The measured height of this view. May be a complex
15226 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15227 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015228 */
15229 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
15230 mMeasuredWidth = measuredWidth;
15231 mMeasuredHeight = measuredHeight;
15232
Dianne Hackborn4702a852012-08-17 15:18:29 -070015233 mPrivateFlags |= PFLAG_MEASURED_DIMENSION_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015234 }
15235
15236 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080015237 * Merge two states as returned by {@link #getMeasuredState()}.
15238 * @param curState The current state as returned from a view or the result
15239 * of combining multiple views.
15240 * @param newState The new view state to combine.
15241 * @return Returns a new integer reflecting the combination of the two
15242 * states.
15243 */
15244 public static int combineMeasuredStates(int curState, int newState) {
15245 return curState | newState;
15246 }
15247
15248 /**
15249 * Version of {@link #resolveSizeAndState(int, int, int)}
15250 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
15251 */
15252 public static int resolveSize(int size, int measureSpec) {
15253 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
15254 }
15255
15256 /**
15257 * Utility to reconcile a desired size and state, with constraints imposed
15258 * by a MeasureSpec. Will take the desired size, unless a different size
15259 * is imposed by the constraints. The returned value is a compound integer,
15260 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
15261 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
15262 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015263 *
15264 * @param size How big the view wants to be
15265 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080015266 * @return Size information bit mask as defined by
15267 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015268 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080015269 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015270 int result = size;
15271 int specMode = MeasureSpec.getMode(measureSpec);
15272 int specSize = MeasureSpec.getSize(measureSpec);
15273 switch (specMode) {
15274 case MeasureSpec.UNSPECIFIED:
15275 result = size;
15276 break;
15277 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080015278 if (specSize < size) {
15279 result = specSize | MEASURED_STATE_TOO_SMALL;
15280 } else {
15281 result = size;
15282 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015283 break;
15284 case MeasureSpec.EXACTLY:
15285 result = specSize;
15286 break;
15287 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080015288 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015289 }
15290
15291 /**
15292 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070015293 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015294 * by the MeasureSpec.
15295 *
15296 * @param size Default size for this view
15297 * @param measureSpec Constraints imposed by the parent
15298 * @return The size this view should be.
15299 */
15300 public static int getDefaultSize(int size, int measureSpec) {
15301 int result = size;
15302 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070015303 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015304
15305 switch (specMode) {
15306 case MeasureSpec.UNSPECIFIED:
15307 result = size;
15308 break;
15309 case MeasureSpec.AT_MOST:
15310 case MeasureSpec.EXACTLY:
15311 result = specSize;
15312 break;
15313 }
15314 return result;
15315 }
15316
15317 /**
15318 * Returns the suggested minimum height that the view should use. This
15319 * returns the maximum of the view's minimum height
15320 * and the background's minimum height
15321 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
15322 * <p>
15323 * When being used in {@link #onMeasure(int, int)}, the caller should still
15324 * ensure the returned height is within the requirements of the parent.
15325 *
15326 * @return The suggested minimum height of the view.
15327 */
15328 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015329 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015331 }
15332
15333 /**
15334 * Returns the suggested minimum width that the view should use. This
15335 * returns the maximum of the view's minimum width)
15336 * and the background's minimum width
15337 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
15338 * <p>
15339 * When being used in {@link #onMeasure(int, int)}, the caller should still
15340 * ensure the returned width is within the requirements of the parent.
15341 *
15342 * @return The suggested minimum width of the view.
15343 */
15344 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015345 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
15346 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015347
Philip Milne6c8ea062012-04-03 17:38:43 -070015348 /**
15349 * Returns the minimum height of the view.
15350 *
15351 * @return the minimum height the view will try to be.
15352 *
15353 * @see #setMinimumHeight(int)
15354 *
15355 * @attr ref android.R.styleable#View_minHeight
15356 */
15357 public int getMinimumHeight() {
15358 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015359 }
15360
15361 /**
15362 * Sets the minimum height of the view. It is not guaranteed the view will
15363 * be able to achieve this minimum height (for example, if its parent layout
15364 * constrains it with less available height).
15365 *
15366 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015367 *
15368 * @see #getMinimumHeight()
15369 *
15370 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015371 */
15372 public void setMinimumHeight(int minHeight) {
15373 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070015374 requestLayout();
15375 }
15376
15377 /**
15378 * Returns the minimum width of the view.
15379 *
15380 * @return the minimum width the view will try to be.
15381 *
15382 * @see #setMinimumWidth(int)
15383 *
15384 * @attr ref android.R.styleable#View_minWidth
15385 */
15386 public int getMinimumWidth() {
15387 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015388 }
15389
15390 /**
15391 * Sets the minimum width of the view. It is not guaranteed the view will
15392 * be able to achieve this minimum width (for example, if its parent layout
15393 * constrains it with less available width).
15394 *
15395 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015396 *
15397 * @see #getMinimumWidth()
15398 *
15399 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015400 */
15401 public void setMinimumWidth(int minWidth) {
15402 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070015403 requestLayout();
15404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015405 }
15406
15407 /**
15408 * Get the animation currently associated with this view.
15409 *
15410 * @return The animation that is currently playing or
15411 * scheduled to play for this view.
15412 */
15413 public Animation getAnimation() {
15414 return mCurrentAnimation;
15415 }
15416
15417 /**
15418 * Start the specified animation now.
15419 *
15420 * @param animation the animation to start now
15421 */
15422 public void startAnimation(Animation animation) {
15423 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
15424 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080015425 invalidateParentCaches();
15426 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015427 }
15428
15429 /**
15430 * Cancels any animations for this view.
15431 */
15432 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080015433 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080015434 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080015435 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015436 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080015437 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015438 }
15439
15440 /**
15441 * Sets the next animation to play for this view.
15442 * If you want the animation to play immediately, use
Chet Haase42428932012-05-11 15:39:07 -070015443 * {@link #startAnimation(android.view.animation.Animation)} instead.
15444 * This method provides allows fine-grained
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015445 * control over the start time and invalidation, but you
15446 * must make sure that 1) the animation has a start time set, and
Chet Haase42428932012-05-11 15:39:07 -070015447 * 2) the view's parent (which controls animations on its children)
15448 * will be invalidated when the animation is supposed to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015449 * start.
15450 *
15451 * @param animation The next animation, or null.
15452 */
15453 public void setAnimation(Animation animation) {
15454 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070015455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015456 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070015457 // If the screen is off assume the animation start time is now instead of
15458 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
15459 // would cause the animation to start when the screen turns back on
15460 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
15461 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
15462 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
15463 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015464 animation.reset();
15465 }
15466 }
15467
15468 /**
15469 * Invoked by a parent ViewGroup to notify the start of the animation
15470 * currently associated with this view. If you override this method,
15471 * always call super.onAnimationStart();
15472 *
15473 * @see #setAnimation(android.view.animation.Animation)
15474 * @see #getAnimation()
15475 */
15476 protected void onAnimationStart() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015477 mPrivateFlags |= PFLAG_ANIMATION_STARTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015478 }
15479
15480 /**
15481 * Invoked by a parent ViewGroup to notify the end of the animation
15482 * currently associated with this view. If you override this method,
15483 * always call super.onAnimationEnd();
15484 *
15485 * @see #setAnimation(android.view.animation.Animation)
15486 * @see #getAnimation()
15487 */
15488 protected void onAnimationEnd() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015489 mPrivateFlags &= ~PFLAG_ANIMATION_STARTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015490 }
15491
15492 /**
15493 * Invoked if there is a Transform that involves alpha. Subclass that can
15494 * draw themselves with the specified alpha should return true, and then
15495 * respect that alpha when their onDraw() is called. If this returns false
15496 * then the view may be redirected to draw into an offscreen buffer to
15497 * fulfill the request, which will look fine, but may be slower than if the
15498 * subclass handles it internally. The default implementation returns false.
15499 *
15500 * @param alpha The alpha (0..255) to apply to the view's drawing
15501 * @return true if the view can draw with the specified alpha.
15502 */
15503 protected boolean onSetAlpha(int alpha) {
15504 return false;
15505 }
15506
15507 /**
15508 * This is used by the RootView to perform an optimization when
15509 * the view hierarchy contains one or several SurfaceView.
15510 * SurfaceView is always considered transparent, but its children are not,
15511 * therefore all View objects remove themselves from the global transparent
15512 * region (passed as a parameter to this function).
15513 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015514 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015515 *
15516 * @return Returns true if the effective visibility of the view at this
15517 * point is opaque, regardless of the transparent region; returns false
15518 * if it is possible for underlying windows to be seen behind the view.
15519 *
15520 * {@hide}
15521 */
15522 public boolean gatherTransparentRegion(Region region) {
15523 final AttachInfo attachInfo = mAttachInfo;
15524 if (region != null && attachInfo != null) {
15525 final int pflags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070015526 if ((pflags & PFLAG_SKIP_DRAW) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015527 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
15528 // remove it from the transparent region.
15529 final int[] location = attachInfo.mTransparentLocation;
15530 getLocationInWindow(location);
15531 region.op(location[0], location[1], location[0] + mRight - mLeft,
15532 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Dianne Hackborn4702a852012-08-17 15:18:29 -070015533 } else if ((pflags & PFLAG_ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015534 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
15535 // exists, so we remove the background drawable's non-transparent
15536 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070015537 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015538 }
15539 }
15540 return true;
15541 }
15542
15543 /**
15544 * Play a sound effect for this view.
15545 *
15546 * <p>The framework will play sound effects for some built in actions, such as
15547 * clicking, but you may wish to play these effects in your widget,
15548 * for instance, for internal navigation.
15549 *
15550 * <p>The sound effect will only be played if sound effects are enabled by the user, and
15551 * {@link #isSoundEffectsEnabled()} is true.
15552 *
15553 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
15554 */
15555 public void playSoundEffect(int soundConstant) {
15556 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
15557 return;
15558 }
15559 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
15560 }
15561
15562 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015563 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015564 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015565 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015566 *
15567 * <p>The framework will provide haptic feedback for some built in actions,
15568 * such as long presses, but you may wish to provide feedback for your
15569 * own widget.
15570 *
15571 * <p>The feedback will only be performed if
15572 * {@link #isHapticFeedbackEnabled()} is true.
15573 *
15574 * @param feedbackConstant One of the constants defined in
15575 * {@link HapticFeedbackConstants}
15576 */
15577 public boolean performHapticFeedback(int feedbackConstant) {
15578 return performHapticFeedback(feedbackConstant, 0);
15579 }
15580
15581 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015582 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015583 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015584 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015585 *
15586 * @param feedbackConstant One of the constants defined in
15587 * {@link HapticFeedbackConstants}
15588 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
15589 */
15590 public boolean performHapticFeedback(int feedbackConstant, int flags) {
15591 if (mAttachInfo == null) {
15592 return false;
15593 }
Romain Guyf607bdc2010-09-10 19:20:06 -070015594 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070015595 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015596 && !isHapticFeedbackEnabled()) {
15597 return false;
15598 }
Romain Guy812ccbe2010-06-01 14:07:24 -070015599 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
15600 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015601 }
15602
15603 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015604 * Request that the visibility of the status bar or other screen/window
15605 * decorations be changed.
15606 *
15607 * <p>This method is used to put the over device UI into temporary modes
15608 * where the user's attention is focused more on the application content,
15609 * by dimming or hiding surrounding system affordances. This is typically
15610 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
15611 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
15612 * to be placed behind the action bar (and with these flags other system
15613 * affordances) so that smooth transitions between hiding and showing them
15614 * can be done.
15615 *
15616 * <p>Two representative examples of the use of system UI visibility is
15617 * implementing a content browsing application (like a magazine reader)
15618 * and a video playing application.
15619 *
15620 * <p>The first code shows a typical implementation of a View in a content
15621 * browsing application. In this implementation, the application goes
15622 * into a content-oriented mode by hiding the status bar and action bar,
15623 * and putting the navigation elements into lights out mode. The user can
15624 * then interact with content while in this mode. Such an application should
15625 * provide an easy way for the user to toggle out of the mode (such as to
15626 * check information in the status bar or access notifications). In the
15627 * implementation here, this is done simply by tapping on the content.
15628 *
15629 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
15630 * content}
15631 *
15632 * <p>This second code sample shows a typical implementation of a View
15633 * in a video playing application. In this situation, while the video is
15634 * playing the application would like to go into a complete full-screen mode,
15635 * to use as much of the display as possible for the video. When in this state
15636 * the user can not interact with the application; the system intercepts
Dianne Hackborncf675782012-05-10 15:07:24 -070015637 * touching on the screen to pop the UI out of full screen mode. See
15638 * {@link #fitSystemWindows(Rect)} for a sample layout that goes with this code.
Dianne Hackborn98014352012-04-05 18:31:41 -070015639 *
15640 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
15641 * content}
15642 *
15643 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15644 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15645 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15646 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015647 */
15648 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040015649 if (visibility != mSystemUiVisibility) {
15650 mSystemUiVisibility = visibility;
15651 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15652 mParent.recomputeViewAttributes(this);
15653 }
Joe Onorato664644d2011-01-23 17:53:23 -080015654 }
15655 }
15656
15657 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015658 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
15659 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15660 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15661 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15662 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015663 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080015664 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080015665 return mSystemUiVisibility;
15666 }
15667
Scott Mainec6331b2011-05-24 16:55:56 -070015668 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070015669 * Returns the current system UI visibility that is currently set for
15670 * the entire window. This is the combination of the
15671 * {@link #setSystemUiVisibility(int)} values supplied by all of the
15672 * views in the window.
15673 */
15674 public int getWindowSystemUiVisibility() {
15675 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
15676 }
15677
15678 /**
15679 * Override to find out when the window's requested system UI visibility
15680 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
15681 * This is different from the callbacks recieved through
15682 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
15683 * in that this is only telling you about the local request of the window,
15684 * not the actual values applied by the system.
15685 */
15686 public void onWindowSystemUiVisibilityChanged(int visible) {
15687 }
15688
15689 /**
15690 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
15691 * the view hierarchy.
15692 */
15693 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
15694 onWindowSystemUiVisibilityChanged(visible);
15695 }
15696
15697 /**
Scott Mainec6331b2011-05-24 16:55:56 -070015698 * Set a listener to receive callbacks when the visibility of the system bar changes.
15699 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
15700 */
Joe Onorato664644d2011-01-23 17:53:23 -080015701 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015702 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080015703 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15704 mParent.recomputeViewAttributes(this);
15705 }
15706 }
15707
15708 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015709 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
15710 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080015711 */
15712 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015713 ListenerInfo li = mListenerInfo;
15714 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
15715 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080015716 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080015717 }
15718 }
15719
Dianne Hackborncf675782012-05-10 15:07:24 -070015720 boolean updateLocalSystemUiVisibility(int localValue, int localChanges) {
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015721 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
15722 if (val != mSystemUiVisibility) {
15723 setSystemUiVisibility(val);
Dianne Hackborncf675782012-05-10 15:07:24 -070015724 return true;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015725 }
Dianne Hackborncf675782012-05-10 15:07:24 -070015726 return false;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015727 }
15728
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070015729 /** @hide */
15730 public void setDisabledSystemUiVisibility(int flags) {
15731 if (mAttachInfo != null) {
15732 if (mAttachInfo.mDisabledSystemUiVisibility != flags) {
15733 mAttachInfo.mDisabledSystemUiVisibility = flags;
15734 if (mParent != null) {
15735 mParent.recomputeViewAttributes(this);
15736 }
15737 }
15738 }
15739 }
15740
Joe Onorato664644d2011-01-23 17:53:23 -080015741 /**
Joe Malin32736f02011-01-19 16:14:20 -080015742 * Creates an image that the system displays during the drag and drop
15743 * operation. This is called a &quot;drag shadow&quot;. The default implementation
15744 * for a DragShadowBuilder based on a View returns an image that has exactly the same
15745 * appearance as the given View. The default also positions the center of the drag shadow
15746 * directly under the touch point. If no View is provided (the constructor with no parameters
15747 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
15748 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
15749 * default is an invisible drag shadow.
15750 * <p>
15751 * You are not required to use the View you provide to the constructor as the basis of the
15752 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
15753 * anything you want as the drag shadow.
15754 * </p>
15755 * <p>
15756 * You pass a DragShadowBuilder object to the system when you start the drag. The system
15757 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
15758 * size and position of the drag shadow. It uses this data to construct a
15759 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
15760 * so that your application can draw the shadow image in the Canvas.
15761 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070015762 *
15763 * <div class="special reference">
15764 * <h3>Developer Guides</h3>
15765 * <p>For a guide to implementing drag and drop features, read the
15766 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
15767 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070015768 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015769 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070015770 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070015771
15772 /**
Joe Malin32736f02011-01-19 16:14:20 -080015773 * Constructs a shadow image builder based on a View. By default, the resulting drag
15774 * shadow will have the same appearance and dimensions as the View, with the touch point
15775 * over the center of the View.
15776 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070015777 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015778 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070015779 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070015780 }
15781
Christopher Tate17ed60c2011-01-18 12:50:26 -080015782 /**
15783 * Construct a shadow builder object with no associated View. This
15784 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
15785 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
15786 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080015787 * reference to any View object. If they are not overridden, then the result is an
15788 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080015789 */
15790 public DragShadowBuilder() {
15791 mView = new WeakReference<View>(null);
15792 }
15793
15794 /**
15795 * Returns the View object that had been passed to the
15796 * {@link #View.DragShadowBuilder(View)}
15797 * constructor. If that View parameter was {@code null} or if the
15798 * {@link #View.DragShadowBuilder()}
15799 * constructor was used to instantiate the builder object, this method will return
15800 * null.
15801 *
15802 * @return The View object associate with this builder object.
15803 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070015804 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070015805 final public View getView() {
15806 return mView.get();
15807 }
15808
Christopher Tate2c095f32010-10-04 14:13:40 -070015809 /**
Joe Malin32736f02011-01-19 16:14:20 -080015810 * Provides the metrics for the shadow image. These include the dimensions of
15811 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070015812 * be centered under the touch location while dragging.
15813 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015814 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080015815 * same as the dimensions of the View itself and centers the shadow under
15816 * the touch point.
15817 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070015818 *
Joe Malin32736f02011-01-19 16:14:20 -080015819 * @param shadowSize A {@link android.graphics.Point} containing the width and height
15820 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
15821 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
15822 * image.
15823 *
15824 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
15825 * shadow image that should be underneath the touch point during the drag and drop
15826 * operation. Your application must set {@link android.graphics.Point#x} to the
15827 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070015828 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015829 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070015830 final View view = mView.get();
15831 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015832 shadowSize.set(view.getWidth(), view.getHeight());
15833 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070015834 } else {
15835 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
15836 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015837 }
15838
15839 /**
Joe Malin32736f02011-01-19 16:14:20 -080015840 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
15841 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015842 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070015843 *
Joe Malin32736f02011-01-19 16:14:20 -080015844 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070015845 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015846 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070015847 final View view = mView.get();
15848 if (view != null) {
15849 view.draw(canvas);
15850 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015851 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070015852 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015853 }
15854 }
15855
15856 /**
Joe Malin32736f02011-01-19 16:14:20 -080015857 * Starts a drag and drop operation. When your application calls this method, it passes a
15858 * {@link android.view.View.DragShadowBuilder} object to the system. The
15859 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
15860 * to get metrics for the drag shadow, and then calls the object's
15861 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
15862 * <p>
15863 * Once the system has the drag shadow, it begins the drag and drop operation by sending
15864 * drag events to all the View objects in your application that are currently visible. It does
15865 * this either by calling the View object's drag listener (an implementation of
15866 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
15867 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
15868 * Both are passed a {@link android.view.DragEvent} object that has a
15869 * {@link android.view.DragEvent#getAction()} value of
15870 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
15871 * </p>
15872 * <p>
15873 * Your application can invoke startDrag() on any attached View object. The View object does not
15874 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
15875 * be related to the View the user selected for dragging.
15876 * </p>
15877 * @param data A {@link android.content.ClipData} object pointing to the data to be
15878 * transferred by the drag and drop operation.
15879 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
15880 * drag shadow.
15881 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
15882 * drop operation. This Object is put into every DragEvent object sent by the system during the
15883 * current drag.
15884 * <p>
15885 * myLocalState is a lightweight mechanism for the sending information from the dragged View
15886 * to the target Views. For example, it can contain flags that differentiate between a
15887 * a copy operation and a move operation.
15888 * </p>
15889 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
15890 * so the parameter should be set to 0.
15891 * @return {@code true} if the method completes successfully, or
15892 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
15893 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070015894 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015895 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015896 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070015897 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015898 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070015899 }
15900 boolean okay = false;
15901
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015902 Point shadowSize = new Point();
15903 Point shadowTouchPoint = new Point();
15904 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070015905
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015906 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
15907 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
15908 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070015909 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015910
Chris Tatea32dcf72010-10-14 12:13:50 -070015911 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015912 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
15913 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070015914 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015915 Surface surface = new Surface();
15916 try {
15917 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015918 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070015919 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070015920 + " surface=" + surface);
15921 if (token != null) {
15922 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070015923 try {
Chris Tate6b391282010-10-14 15:48:59 -070015924 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015925 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070015926 } finally {
15927 surface.unlockCanvasAndPost(canvas);
15928 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015929
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015930 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080015931
15932 // Cache the local state object for delivery with DragEvents
15933 root.setLocalDragState(myLocalState);
15934
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015935 // repurpose 'shadowSize' for the last touch point
15936 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070015937
Christopher Tatea53146c2010-09-07 11:57:52 -070015938 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015939 shadowSize.x, shadowSize.y,
15940 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070015941 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070015942
15943 // Off and running! Release our local surface instance; the drag
15944 // shadow surface is now managed by the system process.
15945 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070015946 }
15947 } catch (Exception e) {
15948 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
15949 surface.destroy();
15950 }
15951
15952 return okay;
15953 }
15954
Christopher Tatea53146c2010-09-07 11:57:52 -070015955 /**
Joe Malin32736f02011-01-19 16:14:20 -080015956 * Handles drag events sent by the system following a call to
15957 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
15958 *<p>
15959 * When the system calls this method, it passes a
15960 * {@link android.view.DragEvent} object. A call to
15961 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
15962 * in DragEvent. The method uses these to determine what is happening in the drag and drop
15963 * operation.
15964 * @param event The {@link android.view.DragEvent} sent by the system.
15965 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
15966 * in DragEvent, indicating the type of drag event represented by this object.
15967 * @return {@code true} if the method was successful, otherwise {@code false}.
15968 * <p>
15969 * The method should return {@code true} in response to an action type of
15970 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
15971 * operation.
15972 * </p>
15973 * <p>
15974 * The method should also return {@code true} in response to an action type of
15975 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
15976 * {@code false} if it didn't.
15977 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015978 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070015979 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070015980 return false;
15981 }
15982
15983 /**
Joe Malin32736f02011-01-19 16:14:20 -080015984 * Detects if this View is enabled and has a drag event listener.
15985 * If both are true, then it calls the drag event listener with the
15986 * {@link android.view.DragEvent} it received. If the drag event listener returns
15987 * {@code true}, then dispatchDragEvent() returns {@code true}.
15988 * <p>
15989 * For all other cases, the method calls the
15990 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
15991 * method and returns its result.
15992 * </p>
15993 * <p>
15994 * This ensures that a drag event is always consumed, even if the View does not have a drag
15995 * event listener. However, if the View has a listener and the listener returns true, then
15996 * onDragEvent() is not called.
15997 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015998 */
15999 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080016000 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070016001 ListenerInfo li = mListenerInfo;
16002 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
16003 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070016004 return true;
16005 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016006 return onDragEvent(event);
16007 }
16008
Christopher Tate3d4bf172011-03-28 16:16:46 -070016009 boolean canAcceptDrag() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016010 return (mPrivateFlags2 & PFLAG2_DRAG_CAN_ACCEPT) != 0;
Christopher Tate3d4bf172011-03-28 16:16:46 -070016011 }
16012
Christopher Tatea53146c2010-09-07 11:57:52 -070016013 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070016014 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
16015 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070016016 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070016017 */
16018 public void onCloseSystemDialogs(String reason) {
16019 }
Joe Malin32736f02011-01-19 16:14:20 -080016020
Dianne Hackbornffa42482009-09-23 22:20:11 -070016021 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016022 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070016023 * update a Region being computed for
16024 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016025 * that any non-transparent parts of the Drawable are removed from the
16026 * given transparent region.
16027 *
16028 * @param dr The Drawable whose transparency is to be applied to the region.
16029 * @param region A Region holding the current transparency information,
16030 * where any parts of the region that are set are considered to be
16031 * transparent. On return, this region will be modified to have the
16032 * transparency information reduced by the corresponding parts of the
16033 * Drawable that are not transparent.
16034 * {@hide}
16035 */
16036 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
16037 if (DBG) {
16038 Log.i("View", "Getting transparent region for: " + this);
16039 }
16040 final Region r = dr.getTransparentRegion();
16041 final Rect db = dr.getBounds();
16042 final AttachInfo attachInfo = mAttachInfo;
16043 if (r != null && attachInfo != null) {
16044 final int w = getRight()-getLeft();
16045 final int h = getBottom()-getTop();
16046 if (db.left > 0) {
16047 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
16048 r.op(0, 0, db.left, h, Region.Op.UNION);
16049 }
16050 if (db.right < w) {
16051 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
16052 r.op(db.right, 0, w, h, Region.Op.UNION);
16053 }
16054 if (db.top > 0) {
16055 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
16056 r.op(0, 0, w, db.top, Region.Op.UNION);
16057 }
16058 if (db.bottom < h) {
16059 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
16060 r.op(0, db.bottom, w, h, Region.Op.UNION);
16061 }
16062 final int[] location = attachInfo.mTransparentLocation;
16063 getLocationInWindow(location);
16064 r.translate(location[0], location[1]);
16065 region.op(r, Region.Op.INTERSECT);
16066 } else {
16067 region.op(db, Region.Op.DIFFERENCE);
16068 }
16069 }
16070
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016071 private void checkForLongClick(int delayOffset) {
16072 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
16073 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016074
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016075 if (mPendingCheckForLongPress == null) {
16076 mPendingCheckForLongPress = new CheckForLongPress();
16077 }
16078 mPendingCheckForLongPress.rememberWindowAttachCount();
16079 postDelayed(mPendingCheckForLongPress,
16080 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016081 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016082 }
16083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016084 /**
16085 * Inflate a view from an XML resource. This convenience method wraps the {@link
16086 * LayoutInflater} class, which provides a full range of options for view inflation.
16087 *
16088 * @param context The Context object for your activity or application.
16089 * @param resource The resource ID to inflate
16090 * @param root A view group that will be the parent. Used to properly inflate the
16091 * layout_* parameters.
16092 * @see LayoutInflater
16093 */
16094 public static View inflate(Context context, int resource, ViewGroup root) {
16095 LayoutInflater factory = LayoutInflater.from(context);
16096 return factory.inflate(resource, root);
16097 }
Romain Guy33e72ae2010-07-17 12:40:29 -070016098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016099 /**
Adam Powell637d3372010-08-25 14:37:03 -070016100 * Scroll the view with standard behavior for scrolling beyond the normal
16101 * content boundaries. Views that call this method should override
16102 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
16103 * results of an over-scroll operation.
16104 *
16105 * Views can use this method to handle any touch or fling-based scrolling.
16106 *
16107 * @param deltaX Change in X in pixels
16108 * @param deltaY Change in Y in pixels
16109 * @param scrollX Current X scroll value in pixels before applying deltaX
16110 * @param scrollY Current Y scroll value in pixels before applying deltaY
16111 * @param scrollRangeX Maximum content scroll range along the X axis
16112 * @param scrollRangeY Maximum content scroll range along the Y axis
16113 * @param maxOverScrollX Number of pixels to overscroll by in either direction
16114 * along the X axis.
16115 * @param maxOverScrollY Number of pixels to overscroll by in either direction
16116 * along the Y axis.
16117 * @param isTouchEvent true if this scroll operation is the result of a touch event.
16118 * @return true if scrolling was clamped to an over-scroll boundary along either
16119 * axis, false otherwise.
16120 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070016121 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070016122 protected boolean overScrollBy(int deltaX, int deltaY,
16123 int scrollX, int scrollY,
16124 int scrollRangeX, int scrollRangeY,
16125 int maxOverScrollX, int maxOverScrollY,
16126 boolean isTouchEvent) {
16127 final int overScrollMode = mOverScrollMode;
16128 final boolean canScrollHorizontal =
16129 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
16130 final boolean canScrollVertical =
16131 computeVerticalScrollRange() > computeVerticalScrollExtent();
16132 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
16133 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
16134 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
16135 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
16136
16137 int newScrollX = scrollX + deltaX;
16138 if (!overScrollHorizontal) {
16139 maxOverScrollX = 0;
16140 }
16141
16142 int newScrollY = scrollY + deltaY;
16143 if (!overScrollVertical) {
16144 maxOverScrollY = 0;
16145 }
16146
16147 // Clamp values if at the limits and record
16148 final int left = -maxOverScrollX;
16149 final int right = maxOverScrollX + scrollRangeX;
16150 final int top = -maxOverScrollY;
16151 final int bottom = maxOverScrollY + scrollRangeY;
16152
16153 boolean clampedX = false;
16154 if (newScrollX > right) {
16155 newScrollX = right;
16156 clampedX = true;
16157 } else if (newScrollX < left) {
16158 newScrollX = left;
16159 clampedX = true;
16160 }
16161
16162 boolean clampedY = false;
16163 if (newScrollY > bottom) {
16164 newScrollY = bottom;
16165 clampedY = true;
16166 } else if (newScrollY < top) {
16167 newScrollY = top;
16168 clampedY = true;
16169 }
16170
16171 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
16172
16173 return clampedX || clampedY;
16174 }
16175
16176 /**
16177 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
16178 * respond to the results of an over-scroll operation.
16179 *
16180 * @param scrollX New X scroll value in pixels
16181 * @param scrollY New Y scroll value in pixels
16182 * @param clampedX True if scrollX was clamped to an over-scroll boundary
16183 * @param clampedY True if scrollY was clamped to an over-scroll boundary
16184 */
16185 protected void onOverScrolled(int scrollX, int scrollY,
16186 boolean clampedX, boolean clampedY) {
16187 // Intentionally empty.
16188 }
16189
16190 /**
16191 * Returns the over-scroll mode for this view. The result will be
16192 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16193 * (allow over-scrolling only if the view content is larger than the container),
16194 * or {@link #OVER_SCROLL_NEVER}.
16195 *
16196 * @return This view's over-scroll mode.
16197 */
16198 public int getOverScrollMode() {
16199 return mOverScrollMode;
16200 }
16201
16202 /**
16203 * Set the over-scroll mode for this view. Valid over-scroll modes are
16204 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16205 * (allow over-scrolling only if the view content is larger than the container),
16206 * or {@link #OVER_SCROLL_NEVER}.
16207 *
16208 * Setting the over-scroll mode of a view will have an effect only if the
16209 * view is capable of scrolling.
16210 *
16211 * @param overScrollMode The new over-scroll mode for this view.
16212 */
16213 public void setOverScrollMode(int overScrollMode) {
16214 if (overScrollMode != OVER_SCROLL_ALWAYS &&
16215 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
16216 overScrollMode != OVER_SCROLL_NEVER) {
16217 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
16218 }
16219 mOverScrollMode = overScrollMode;
16220 }
16221
16222 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016223 * Gets a scale factor that determines the distance the view should scroll
16224 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
16225 * @return The vertical scroll scale factor.
16226 * @hide
16227 */
16228 protected float getVerticalScrollFactor() {
16229 if (mVerticalScrollFactor == 0) {
16230 TypedValue outValue = new TypedValue();
16231 if (!mContext.getTheme().resolveAttribute(
16232 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
16233 throw new IllegalStateException(
16234 "Expected theme to define listPreferredItemHeight.");
16235 }
16236 mVerticalScrollFactor = outValue.getDimension(
16237 mContext.getResources().getDisplayMetrics());
16238 }
16239 return mVerticalScrollFactor;
16240 }
16241
16242 /**
16243 * Gets a scale factor that determines the distance the view should scroll
16244 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
16245 * @return The horizontal scroll scale factor.
16246 * @hide
16247 */
16248 protected float getHorizontalScrollFactor() {
16249 // TODO: Should use something else.
16250 return getVerticalScrollFactor();
16251 }
16252
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016253 /**
16254 * Return the value specifying the text direction or policy that was set with
16255 * {@link #setTextDirection(int)}.
16256 *
16257 * @return the defined text direction. It can be one of:
16258 *
16259 * {@link #TEXT_DIRECTION_INHERIT},
16260 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16261 * {@link #TEXT_DIRECTION_ANY_RTL},
16262 * {@link #TEXT_DIRECTION_LTR},
16263 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016264 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016265 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016266 @ViewDebug.ExportedProperty(category = "text", mapping = {
16267 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
16268 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
16269 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
16270 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
16271 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
16272 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
16273 })
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016274 public int getTextDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016275 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_MASK) >> PFLAG2_TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016276 }
16277
16278 /**
16279 * Set the text direction.
16280 *
16281 * @param textDirection the direction to set. Should be one of:
16282 *
16283 * {@link #TEXT_DIRECTION_INHERIT},
16284 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16285 * {@link #TEXT_DIRECTION_ANY_RTL},
16286 * {@link #TEXT_DIRECTION_LTR},
16287 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016288 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016289 */
16290 public void setTextDirection(int textDirection) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016291 if (getTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016292 // Reset the current text direction and the resolved one
Dianne Hackborn4702a852012-08-17 15:18:29 -070016293 mPrivateFlags2 &= ~PFLAG2_TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016294 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016295 // Set the new text direction
Dianne Hackborn4702a852012-08-17 15:18:29 -070016296 mPrivateFlags2 |= ((textDirection << PFLAG2_TEXT_DIRECTION_MASK_SHIFT) & PFLAG2_TEXT_DIRECTION_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016297 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016298 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016299 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016300 }
16301 }
16302
16303 /**
16304 * Return the resolved text direction.
16305 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016306 * This needs resolution if the value is TEXT_DIRECTION_INHERIT. The resolution matches
16307 * {@link #getTextDirection()}if it is not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds
16308 * up the parent chain of the view. if there is no parent, then it will return the default
16309 * {@link #TEXT_DIRECTION_FIRST_STRONG}.
16310 *
16311 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016312 *
Doug Feltcb3791202011-07-07 11:57:48 -070016313 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16314 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016315 * {@link #TEXT_DIRECTION_LTR},
16316 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016317 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016318 */
16319 public int getResolvedTextDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070016320 // The text direction will be resolved only if needed
Dianne Hackborn4702a852012-08-17 15:18:29 -070016321 if ((mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_RESOLVED) != PFLAG2_TEXT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016322 resolveTextDirection();
16323 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070016324 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_RESOLVED_MASK) >> PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016325 }
16326
16327 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016328 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
16329 * resolution is done.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016330 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016331 public void resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016332 // Reset any previous text direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016333 mPrivateFlags2 &= ~(PFLAG2_TEXT_DIRECTION_RESOLVED | PFLAG2_TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016334
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016335 if (hasRtlSupport()) {
16336 // Set resolved text direction flag depending on text direction flag
16337 final int textDirection = getTextDirection();
16338 switch(textDirection) {
16339 case TEXT_DIRECTION_INHERIT:
16340 if (canResolveTextDirection()) {
16341 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016342
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016343 // Set current resolved direction to the same value as the parent's one
16344 final int parentResolvedDirection = viewGroup.getResolvedTextDirection();
16345 switch (parentResolvedDirection) {
16346 case TEXT_DIRECTION_FIRST_STRONG:
16347 case TEXT_DIRECTION_ANY_RTL:
16348 case TEXT_DIRECTION_LTR:
16349 case TEXT_DIRECTION_RTL:
16350 case TEXT_DIRECTION_LOCALE:
16351 mPrivateFlags2 |=
Dianne Hackborn4702a852012-08-17 15:18:29 -070016352 (parentResolvedDirection << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016353 break;
16354 default:
16355 // Default resolved direction is "first strong" heuristic
Dianne Hackborn4702a852012-08-17 15:18:29 -070016356 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016357 }
16358 } else {
16359 // We cannot do the resolution if there is no parent, so use the default one
Dianne Hackborn4702a852012-08-17 15:18:29 -070016360 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016361 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016362 break;
16363 case TEXT_DIRECTION_FIRST_STRONG:
16364 case TEXT_DIRECTION_ANY_RTL:
16365 case TEXT_DIRECTION_LTR:
16366 case TEXT_DIRECTION_RTL:
16367 case TEXT_DIRECTION_LOCALE:
16368 // Resolved direction is the same as text direction
Dianne Hackborn4702a852012-08-17 15:18:29 -070016369 mPrivateFlags2 |= (textDirection << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016370 break;
16371 default:
16372 // Default resolved direction is "first strong" heuristic
Dianne Hackborn4702a852012-08-17 15:18:29 -070016373 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016374 }
16375 } else {
16376 // Default resolved direction is "first strong" heuristic
Dianne Hackborn4702a852012-08-17 15:18:29 -070016377 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016378 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016379
16380 // Set to resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070016381 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016382 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016383 }
16384
16385 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016386 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016387 * resolution should override this method.
16388 *
16389 * The default implementation does nothing.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016390 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016391 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016392 }
16393
16394 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016395 * Check if text direction resolution can be done.
16396 *
16397 * @return true if text direction resolution can be done otherwise return false.
16398 */
16399 public boolean canResolveTextDirection() {
16400 switch (getTextDirection()) {
16401 case TEXT_DIRECTION_INHERIT:
16402 return (mParent != null) && (mParent instanceof ViewGroup);
16403 default:
16404 return true;
16405 }
16406 }
16407
16408 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016409 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016410 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016411 * reset is done.
16412 */
16413 public void resetResolvedTextDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016414 mPrivateFlags2 &= ~(PFLAG2_TEXT_DIRECTION_RESOLVED | PFLAG2_TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016415 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016416 }
16417
16418 /**
16419 * Called when text direction is reset. Subclasses that care about text direction reset should
16420 * override this method and do a reset of the text direction of their children. The default
16421 * implementation does nothing.
16422 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016423 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016424 }
16425
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016426 /**
16427 * Return the value specifying the text alignment or policy that was set with
16428 * {@link #setTextAlignment(int)}.
16429 *
16430 * @return the defined text alignment. It can be one of:
16431 *
16432 * {@link #TEXT_ALIGNMENT_INHERIT},
16433 * {@link #TEXT_ALIGNMENT_GRAVITY},
16434 * {@link #TEXT_ALIGNMENT_CENTER},
16435 * {@link #TEXT_ALIGNMENT_TEXT_START},
16436 * {@link #TEXT_ALIGNMENT_TEXT_END},
16437 * {@link #TEXT_ALIGNMENT_VIEW_START},
16438 * {@link #TEXT_ALIGNMENT_VIEW_END}
16439 */
16440 @ViewDebug.ExportedProperty(category = "text", mapping = {
16441 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16442 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16443 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16444 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16445 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16446 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16447 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16448 })
16449 public int getTextAlignment() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016450 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_MASK) >> PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016451 }
16452
16453 /**
16454 * Set the text alignment.
16455 *
16456 * @param textAlignment The text alignment to set. Should be one of
16457 *
16458 * {@link #TEXT_ALIGNMENT_INHERIT},
16459 * {@link #TEXT_ALIGNMENT_GRAVITY},
16460 * {@link #TEXT_ALIGNMENT_CENTER},
16461 * {@link #TEXT_ALIGNMENT_TEXT_START},
16462 * {@link #TEXT_ALIGNMENT_TEXT_END},
16463 * {@link #TEXT_ALIGNMENT_VIEW_START},
16464 * {@link #TEXT_ALIGNMENT_VIEW_END}
16465 *
16466 * @attr ref android.R.styleable#View_textAlignment
16467 */
16468 public void setTextAlignment(int textAlignment) {
16469 if (textAlignment != getTextAlignment()) {
16470 // Reset the current and resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016471 mPrivateFlags2 &= ~PFLAG2_TEXT_ALIGNMENT_MASK;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016472 resetResolvedTextAlignment();
16473 // Set the new text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016474 mPrivateFlags2 |= ((textAlignment << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT) & PFLAG2_TEXT_ALIGNMENT_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016475 // Refresh
16476 requestLayout();
16477 invalidate(true);
16478 }
16479 }
16480
16481 /**
16482 * Return the resolved text alignment.
16483 *
16484 * The resolved text alignment. This needs resolution if the value is
16485 * TEXT_ALIGNMENT_INHERIT. The resolution matches {@link #setTextAlignment(int)} if it is
16486 * not TEXT_ALIGNMENT_INHERIT, otherwise resolution proceeds up the parent chain of the view.
16487 *
16488 * @return the resolved text alignment. Returns one of:
16489 *
16490 * {@link #TEXT_ALIGNMENT_GRAVITY},
16491 * {@link #TEXT_ALIGNMENT_CENTER},
16492 * {@link #TEXT_ALIGNMENT_TEXT_START},
16493 * {@link #TEXT_ALIGNMENT_TEXT_END},
16494 * {@link #TEXT_ALIGNMENT_VIEW_START},
16495 * {@link #TEXT_ALIGNMENT_VIEW_END}
16496 */
16497 @ViewDebug.ExportedProperty(category = "text", mapping = {
16498 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16499 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16500 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16501 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16502 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16503 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16504 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16505 })
16506 public int getResolvedTextAlignment() {
16507 // If text alignment is not resolved, then resolve it
Dianne Hackborn4702a852012-08-17 15:18:29 -070016508 if ((mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_RESOLVED) != PFLAG2_TEXT_ALIGNMENT_RESOLVED) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016509 resolveTextAlignment();
16510 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070016511 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK) >> PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016512 }
16513
16514 /**
16515 * Resolve the text alignment. Will call {@link View#onResolvedTextAlignmentChanged} when
16516 * resolution is done.
16517 */
16518 public void resolveTextAlignment() {
16519 // Reset any previous text alignment resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016520 mPrivateFlags2 &= ~(PFLAG2_TEXT_ALIGNMENT_RESOLVED | PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016521
16522 if (hasRtlSupport()) {
16523 // Set resolved text alignment flag depending on text alignment flag
16524 final int textAlignment = getTextAlignment();
16525 switch (textAlignment) {
16526 case TEXT_ALIGNMENT_INHERIT:
16527 // Check if we can resolve the text alignment
16528 if (canResolveLayoutDirection() && mParent instanceof View) {
16529 View view = (View) mParent;
16530
16531 final int parentResolvedTextAlignment = view.getResolvedTextAlignment();
16532 switch (parentResolvedTextAlignment) {
16533 case TEXT_ALIGNMENT_GRAVITY:
16534 case TEXT_ALIGNMENT_TEXT_START:
16535 case TEXT_ALIGNMENT_TEXT_END:
16536 case TEXT_ALIGNMENT_CENTER:
16537 case TEXT_ALIGNMENT_VIEW_START:
16538 case TEXT_ALIGNMENT_VIEW_END:
16539 // Resolved text alignment is the same as the parent resolved
16540 // text alignment
16541 mPrivateFlags2 |=
Dianne Hackborn4702a852012-08-17 15:18:29 -070016542 (parentResolvedTextAlignment << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016543 break;
16544 default:
16545 // Use default resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016546 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016547 }
16548 }
16549 else {
16550 // We cannot do the resolution if there is no parent so use the default
Dianne Hackborn4702a852012-08-17 15:18:29 -070016551 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016552 }
16553 break;
16554 case TEXT_ALIGNMENT_GRAVITY:
16555 case TEXT_ALIGNMENT_TEXT_START:
16556 case TEXT_ALIGNMENT_TEXT_END:
16557 case TEXT_ALIGNMENT_CENTER:
16558 case TEXT_ALIGNMENT_VIEW_START:
16559 case TEXT_ALIGNMENT_VIEW_END:
16560 // Resolved text alignment is the same as text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016561 mPrivateFlags2 |= (textAlignment << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016562 break;
16563 default:
16564 // Use default resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016565 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016566 }
16567 } else {
16568 // Use default resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016569 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016570 }
16571
16572 // Set the resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070016573 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016574 onResolvedTextAlignmentChanged();
16575 }
16576
16577 /**
16578 * Check if text alignment resolution can be done.
16579 *
16580 * @return true if text alignment resolution can be done otherwise return false.
16581 */
16582 public boolean canResolveTextAlignment() {
16583 switch (getTextAlignment()) {
16584 case TEXT_DIRECTION_INHERIT:
16585 return (mParent != null);
16586 default:
16587 return true;
16588 }
16589 }
16590
16591 /**
16592 * Called when text alignment has been resolved. Subclasses that care about text alignment
16593 * resolution should override this method.
16594 *
16595 * The default implementation does nothing.
16596 */
16597 public void onResolvedTextAlignmentChanged() {
16598 }
16599
16600 /**
16601 * Reset resolved text alignment. Text alignment can be resolved with a call to
16602 * getResolvedTextAlignment(). Will call {@link View#onResolvedTextAlignmentReset} when
16603 * reset is done.
16604 */
16605 public void resetResolvedTextAlignment() {
16606 // Reset any previous text alignment resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016607 mPrivateFlags2 &= ~(PFLAG2_TEXT_ALIGNMENT_RESOLVED | PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016608 onResolvedTextAlignmentReset();
16609 }
16610
16611 /**
16612 * Called when text alignment is reset. Subclasses that care about text alignment reset should
16613 * override this method and do a reset of the text alignment of their children. The default
16614 * implementation does nothing.
16615 */
16616 public void onResolvedTextAlignmentReset() {
16617 }
16618
Adam Powella9108a22012-07-18 11:18:09 -070016619 /**
16620 * Generate a value suitable for use in {@link #setId(int)}.
16621 * This value will not collide with ID values generated at build time by aapt for R.id.
16622 *
16623 * @return a generated ID value
16624 */
16625 public static int generateViewId() {
16626 for (;;) {
16627 final int result = sNextGeneratedId.get();
16628 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
16629 int newValue = result + 1;
16630 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
16631 if (sNextGeneratedId.compareAndSet(result, newValue)) {
16632 return result;
16633 }
16634 }
16635 }
16636
Chet Haaseb39f0512011-05-24 14:36:40 -070016637 //
16638 // Properties
16639 //
16640 /**
16641 * A Property wrapper around the <code>alpha</code> functionality handled by the
16642 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
16643 */
Chet Haased47f1532011-12-16 11:18:52 -080016644 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016645 @Override
16646 public void setValue(View object, float value) {
16647 object.setAlpha(value);
16648 }
16649
16650 @Override
16651 public Float get(View object) {
16652 return object.getAlpha();
16653 }
16654 };
16655
16656 /**
16657 * A Property wrapper around the <code>translationX</code> functionality handled by the
16658 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
16659 */
Chet Haased47f1532011-12-16 11:18:52 -080016660 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016661 @Override
16662 public void setValue(View object, float value) {
16663 object.setTranslationX(value);
16664 }
16665
16666 @Override
16667 public Float get(View object) {
16668 return object.getTranslationX();
16669 }
16670 };
16671
16672 /**
16673 * A Property wrapper around the <code>translationY</code> functionality handled by the
16674 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
16675 */
Chet Haased47f1532011-12-16 11:18:52 -080016676 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016677 @Override
16678 public void setValue(View object, float value) {
16679 object.setTranslationY(value);
16680 }
16681
16682 @Override
16683 public Float get(View object) {
16684 return object.getTranslationY();
16685 }
16686 };
16687
16688 /**
16689 * A Property wrapper around the <code>x</code> functionality handled by the
16690 * {@link View#setX(float)} and {@link View#getX()} methods.
16691 */
Chet Haased47f1532011-12-16 11:18:52 -080016692 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016693 @Override
16694 public void setValue(View object, float value) {
16695 object.setX(value);
16696 }
16697
16698 @Override
16699 public Float get(View object) {
16700 return object.getX();
16701 }
16702 };
16703
16704 /**
16705 * A Property wrapper around the <code>y</code> functionality handled by the
16706 * {@link View#setY(float)} and {@link View#getY()} methods.
16707 */
Chet Haased47f1532011-12-16 11:18:52 -080016708 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016709 @Override
16710 public void setValue(View object, float value) {
16711 object.setY(value);
16712 }
16713
16714 @Override
16715 public Float get(View object) {
16716 return object.getY();
16717 }
16718 };
16719
16720 /**
16721 * A Property wrapper around the <code>rotation</code> functionality handled by the
16722 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
16723 */
Chet Haased47f1532011-12-16 11:18:52 -080016724 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016725 @Override
16726 public void setValue(View object, float value) {
16727 object.setRotation(value);
16728 }
16729
16730 @Override
16731 public Float get(View object) {
16732 return object.getRotation();
16733 }
16734 };
16735
16736 /**
16737 * A Property wrapper around the <code>rotationX</code> functionality handled by the
16738 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
16739 */
Chet Haased47f1532011-12-16 11:18:52 -080016740 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016741 @Override
16742 public void setValue(View object, float value) {
16743 object.setRotationX(value);
16744 }
16745
16746 @Override
16747 public Float get(View object) {
16748 return object.getRotationX();
16749 }
16750 };
16751
16752 /**
16753 * A Property wrapper around the <code>rotationY</code> functionality handled by the
16754 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
16755 */
Chet Haased47f1532011-12-16 11:18:52 -080016756 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016757 @Override
16758 public void setValue(View object, float value) {
16759 object.setRotationY(value);
16760 }
16761
16762 @Override
16763 public Float get(View object) {
16764 return object.getRotationY();
16765 }
16766 };
16767
16768 /**
16769 * A Property wrapper around the <code>scaleX</code> functionality handled by the
16770 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
16771 */
Chet Haased47f1532011-12-16 11:18:52 -080016772 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016773 @Override
16774 public void setValue(View object, float value) {
16775 object.setScaleX(value);
16776 }
16777
16778 @Override
16779 public Float get(View object) {
16780 return object.getScaleX();
16781 }
16782 };
16783
16784 /**
16785 * A Property wrapper around the <code>scaleY</code> functionality handled by the
16786 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
16787 */
Chet Haased47f1532011-12-16 11:18:52 -080016788 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016789 @Override
16790 public void setValue(View object, float value) {
16791 object.setScaleY(value);
16792 }
16793
16794 @Override
16795 public Float get(View object) {
16796 return object.getScaleY();
16797 }
16798 };
16799
Jeff Brown33bbfd22011-02-24 20:55:35 -080016800 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016801 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
16802 * Each MeasureSpec represents a requirement for either the width or the height.
16803 * A MeasureSpec is comprised of a size and a mode. There are three possible
16804 * modes:
16805 * <dl>
16806 * <dt>UNSPECIFIED</dt>
16807 * <dd>
16808 * The parent has not imposed any constraint on the child. It can be whatever size
16809 * it wants.
16810 * </dd>
16811 *
16812 * <dt>EXACTLY</dt>
16813 * <dd>
16814 * The parent has determined an exact size for the child. The child is going to be
16815 * given those bounds regardless of how big it wants to be.
16816 * </dd>
16817 *
16818 * <dt>AT_MOST</dt>
16819 * <dd>
16820 * The child can be as large as it wants up to the specified size.
16821 * </dd>
16822 * </dl>
16823 *
16824 * MeasureSpecs are implemented as ints to reduce object allocation. This class
16825 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
16826 */
16827 public static class MeasureSpec {
16828 private static final int MODE_SHIFT = 30;
16829 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
16830
16831 /**
16832 * Measure specification mode: The parent has not imposed any constraint
16833 * on the child. It can be whatever size it wants.
16834 */
16835 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
16836
16837 /**
16838 * Measure specification mode: The parent has determined an exact size
16839 * for the child. The child is going to be given those bounds regardless
16840 * of how big it wants to be.
16841 */
16842 public static final int EXACTLY = 1 << MODE_SHIFT;
16843
16844 /**
16845 * Measure specification mode: The child can be as large as it wants up
16846 * to the specified size.
16847 */
16848 public static final int AT_MOST = 2 << MODE_SHIFT;
16849
16850 /**
16851 * Creates a measure specification based on the supplied size and mode.
16852 *
16853 * The mode must always be one of the following:
16854 * <ul>
16855 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
16856 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
16857 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
16858 * </ul>
16859 *
16860 * @param size the size of the measure specification
16861 * @param mode the mode of the measure specification
16862 * @return the measure specification based on size and mode
16863 */
16864 public static int makeMeasureSpec(int size, int mode) {
16865 return size + mode;
16866 }
16867
16868 /**
16869 * Extracts the mode from the supplied measure specification.
16870 *
16871 * @param measureSpec the measure specification to extract the mode from
16872 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
16873 * {@link android.view.View.MeasureSpec#AT_MOST} or
16874 * {@link android.view.View.MeasureSpec#EXACTLY}
16875 */
16876 public static int getMode(int measureSpec) {
16877 return (measureSpec & MODE_MASK);
16878 }
16879
16880 /**
16881 * Extracts the size from the supplied measure specification.
16882 *
16883 * @param measureSpec the measure specification to extract the size from
16884 * @return the size in pixels defined in the supplied measure specification
16885 */
16886 public static int getSize(int measureSpec) {
16887 return (measureSpec & ~MODE_MASK);
16888 }
16889
16890 /**
16891 * Returns a String representation of the specified measure
16892 * specification.
16893 *
16894 * @param measureSpec the measure specification to convert to a String
16895 * @return a String with the following format: "MeasureSpec: MODE SIZE"
16896 */
16897 public static String toString(int measureSpec) {
16898 int mode = getMode(measureSpec);
16899 int size = getSize(measureSpec);
16900
16901 StringBuilder sb = new StringBuilder("MeasureSpec: ");
16902
16903 if (mode == UNSPECIFIED)
16904 sb.append("UNSPECIFIED ");
16905 else if (mode == EXACTLY)
16906 sb.append("EXACTLY ");
16907 else if (mode == AT_MOST)
16908 sb.append("AT_MOST ");
16909 else
16910 sb.append(mode).append(" ");
16911
16912 sb.append(size);
16913 return sb.toString();
16914 }
16915 }
16916
16917 class CheckForLongPress implements Runnable {
16918
16919 private int mOriginalWindowAttachCount;
16920
16921 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070016922 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016923 && mOriginalWindowAttachCount == mWindowAttachCount) {
16924 if (performLongClick()) {
16925 mHasPerformedLongPress = true;
16926 }
16927 }
16928 }
16929
16930 public void rememberWindowAttachCount() {
16931 mOriginalWindowAttachCount = mWindowAttachCount;
16932 }
16933 }
Joe Malin32736f02011-01-19 16:14:20 -080016934
Adam Powelle14579b2009-12-16 18:39:52 -080016935 private final class CheckForTap implements Runnable {
16936 public void run() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016937 mPrivateFlags &= ~PFLAG_PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080016938 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016939 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080016940 }
16941 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016942
Adam Powella35d7682010-03-12 14:48:13 -080016943 private final class PerformClick implements Runnable {
16944 public void run() {
16945 performClick();
16946 }
16947 }
16948
Dianne Hackborn63042d62011-01-26 18:56:29 -080016949 /** @hide */
16950 public void hackTurnOffWindowResizeAnim(boolean off) {
16951 mAttachInfo.mTurnOffWindowResizeAnim = off;
16952 }
Joe Malin32736f02011-01-19 16:14:20 -080016953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016954 /**
Chet Haasea00f3862011-02-22 06:34:40 -080016955 * This method returns a ViewPropertyAnimator object, which can be used to animate
16956 * specific properties on this View.
16957 *
16958 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
16959 */
16960 public ViewPropertyAnimator animate() {
16961 if (mAnimator == null) {
16962 mAnimator = new ViewPropertyAnimator(this);
16963 }
16964 return mAnimator;
16965 }
16966
16967 /**
Jean Chalard405bc512012-05-29 19:12:34 +090016968 * Interface definition for a callback to be invoked when a hardware key event is
16969 * dispatched to this view. The callback will be invoked before the key event is
16970 * given to the view. This is only useful for hardware keyboards; a software input
16971 * method has no obligation to trigger this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016972 */
16973 public interface OnKeyListener {
16974 /**
Jean Chalard405bc512012-05-29 19:12:34 +090016975 * Called when a hardware key is dispatched to a view. This allows listeners to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016976 * get a chance to respond before the target view.
Jean Chalard405bc512012-05-29 19:12:34 +090016977 * <p>Key presses in software keyboards will generally NOT trigger this method,
16978 * although some may elect to do so in some situations. Do not assume a
16979 * software input method has to be key-based; even if it is, it may use key presses
16980 * in a different way than you expect, so there is no way to reliably catch soft
16981 * input key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016982 *
16983 * @param v The view the key has been dispatched to.
16984 * @param keyCode The code for the physical key that was pressed
16985 * @param event The KeyEvent object containing full information about
16986 * the event.
16987 * @return True if the listener has consumed the event, false otherwise.
16988 */
16989 boolean onKey(View v, int keyCode, KeyEvent event);
16990 }
16991
16992 /**
16993 * Interface definition for a callback to be invoked when a touch event is
16994 * dispatched to this view. The callback will be invoked before the touch
16995 * event is given to the view.
16996 */
16997 public interface OnTouchListener {
16998 /**
16999 * Called when a touch event is dispatched to a view. This allows listeners to
17000 * get a chance to respond before the target view.
17001 *
17002 * @param v The view the touch event has been dispatched to.
17003 * @param event The MotionEvent object containing full information about
17004 * the event.
17005 * @return True if the listener has consumed the event, false otherwise.
17006 */
17007 boolean onTouch(View v, MotionEvent event);
17008 }
17009
17010 /**
Jeff Brown10b62902011-06-20 16:40:37 -070017011 * Interface definition for a callback to be invoked when a hover event is
17012 * dispatched to this view. The callback will be invoked before the hover
17013 * event is given to the view.
17014 */
17015 public interface OnHoverListener {
17016 /**
17017 * Called when a hover event is dispatched to a view. This allows listeners to
17018 * get a chance to respond before the target view.
17019 *
17020 * @param v The view the hover event has been dispatched to.
17021 * @param event The MotionEvent object containing full information about
17022 * the event.
17023 * @return True if the listener has consumed the event, false otherwise.
17024 */
17025 boolean onHover(View v, MotionEvent event);
17026 }
17027
17028 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080017029 * Interface definition for a callback to be invoked when a generic motion event is
17030 * dispatched to this view. The callback will be invoked before the generic motion
17031 * event is given to the view.
17032 */
17033 public interface OnGenericMotionListener {
17034 /**
17035 * Called when a generic motion event is dispatched to a view. This allows listeners to
17036 * get a chance to respond before the target view.
17037 *
17038 * @param v The view the generic motion event has been dispatched to.
17039 * @param event The MotionEvent object containing full information about
17040 * the event.
17041 * @return True if the listener has consumed the event, false otherwise.
17042 */
17043 boolean onGenericMotion(View v, MotionEvent event);
17044 }
17045
17046 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017047 * Interface definition for a callback to be invoked when a view has been clicked and held.
17048 */
17049 public interface OnLongClickListener {
17050 /**
17051 * Called when a view has been clicked and held.
17052 *
17053 * @param v The view that was clicked and held.
17054 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080017055 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017056 */
17057 boolean onLongClick(View v);
17058 }
17059
17060 /**
Chris Tate32affef2010-10-18 15:29:21 -070017061 * Interface definition for a callback to be invoked when a drag is being dispatched
17062 * to this view. The callback will be invoked before the hosting view's own
17063 * onDrag(event) method. If the listener wants to fall back to the hosting view's
17064 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070017065 *
17066 * <div class="special reference">
17067 * <h3>Developer Guides</h3>
17068 * <p>For a guide to implementing drag and drop features, read the
17069 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
17070 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070017071 */
17072 public interface OnDragListener {
17073 /**
17074 * Called when a drag event is dispatched to a view. This allows listeners
17075 * to get a chance to override base View behavior.
17076 *
Joe Malin32736f02011-01-19 16:14:20 -080017077 * @param v The View that received the drag event.
17078 * @param event The {@link android.view.DragEvent} object for the drag event.
17079 * @return {@code true} if the drag event was handled successfully, or {@code false}
17080 * if the drag event was not handled. Note that {@code false} will trigger the View
17081 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070017082 */
17083 boolean onDrag(View v, DragEvent event);
17084 }
17085
17086 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017087 * Interface definition for a callback to be invoked when the focus state of
17088 * a view changed.
17089 */
17090 public interface OnFocusChangeListener {
17091 /**
17092 * Called when the focus state of a view has changed.
17093 *
17094 * @param v The view whose state has changed.
17095 * @param hasFocus The new focus state of v.
17096 */
17097 void onFocusChange(View v, boolean hasFocus);
17098 }
17099
17100 /**
17101 * Interface definition for a callback to be invoked when a view is clicked.
17102 */
17103 public interface OnClickListener {
17104 /**
17105 * Called when a view has been clicked.
17106 *
17107 * @param v The view that was clicked.
17108 */
17109 void onClick(View v);
17110 }
17111
17112 /**
17113 * Interface definition for a callback to be invoked when the context menu
17114 * for this view is being built.
17115 */
17116 public interface OnCreateContextMenuListener {
17117 /**
17118 * Called when the context menu for this view is being built. It is not
17119 * safe to hold onto the menu after this method returns.
17120 *
17121 * @param menu The context menu that is being built
17122 * @param v The view for which the context menu is being built
17123 * @param menuInfo Extra information about the item for which the
17124 * context menu should be shown. This information will vary
17125 * depending on the class of v.
17126 */
17127 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
17128 }
17129
Joe Onorato664644d2011-01-23 17:53:23 -080017130 /**
17131 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017132 * visibility. This reports <strong>global</strong> changes to the system UI
Dianne Hackborncf675782012-05-10 15:07:24 -070017133 * state, not what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080017134 *
Philip Milne6c8ea062012-04-03 17:38:43 -070017135 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080017136 */
17137 public interface OnSystemUiVisibilityChangeListener {
17138 /**
17139 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070017140 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080017141 *
Dianne Hackborncf675782012-05-10 15:07:24 -070017142 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
17143 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, and {@link #SYSTEM_UI_FLAG_FULLSCREEN}.
17144 * This tells you the <strong>global</strong> state of these UI visibility
17145 * flags, not what your app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080017146 */
17147 public void onSystemUiVisibilityChange(int visibility);
17148 }
17149
Adam Powell4afd62b2011-02-18 15:02:18 -080017150 /**
17151 * Interface definition for a callback to be invoked when this view is attached
17152 * or detached from its window.
17153 */
17154 public interface OnAttachStateChangeListener {
17155 /**
17156 * Called when the view is attached to a window.
17157 * @param v The view that was attached
17158 */
17159 public void onViewAttachedToWindow(View v);
17160 /**
17161 * Called when the view is detached from a window.
17162 * @param v The view that was detached
17163 */
17164 public void onViewDetachedFromWindow(View v);
17165 }
17166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017167 private final class UnsetPressedState implements Runnable {
17168 public void run() {
17169 setPressed(false);
17170 }
17171 }
17172
17173 /**
17174 * Base class for derived classes that want to save and restore their own
17175 * state in {@link android.view.View#onSaveInstanceState()}.
17176 */
17177 public static class BaseSavedState extends AbsSavedState {
17178 /**
17179 * Constructor used when reading from a parcel. Reads the state of the superclass.
17180 *
17181 * @param source
17182 */
17183 public BaseSavedState(Parcel source) {
17184 super(source);
17185 }
17186
17187 /**
17188 * Constructor called by derived classes when creating their SavedState objects
17189 *
17190 * @param superState The state of the superclass of this view
17191 */
17192 public BaseSavedState(Parcelable superState) {
17193 super(superState);
17194 }
17195
17196 public static final Parcelable.Creator<BaseSavedState> CREATOR =
17197 new Parcelable.Creator<BaseSavedState>() {
17198 public BaseSavedState createFromParcel(Parcel in) {
17199 return new BaseSavedState(in);
17200 }
17201
17202 public BaseSavedState[] newArray(int size) {
17203 return new BaseSavedState[size];
17204 }
17205 };
17206 }
17207
17208 /**
17209 * A set of information given to a view when it is attached to its parent
17210 * window.
17211 */
17212 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017213 interface Callbacks {
17214 void playSoundEffect(int effectId);
17215 boolean performHapticFeedback(int effectId, boolean always);
17216 }
17217
17218 /**
17219 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
17220 * to a Handler. This class contains the target (View) to invalidate and
17221 * the coordinates of the dirty rectangle.
17222 *
17223 * For performance purposes, this class also implements a pool of up to
17224 * POOL_LIMIT objects that get reused. This reduces memory allocations
17225 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017226 */
Romain Guyd928d682009-03-31 17:52:16 -070017227 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017228 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070017229 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
17230 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070017231 public InvalidateInfo newInstance() {
17232 return new InvalidateInfo();
17233 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017234
Romain Guyd928d682009-03-31 17:52:16 -070017235 public void onAcquired(InvalidateInfo element) {
17236 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017237
Romain Guyd928d682009-03-31 17:52:16 -070017238 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070017239 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070017240 }
17241 }, POOL_LIMIT)
17242 );
17243
17244 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017245 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017246
17247 View target;
17248
17249 int left;
17250 int top;
17251 int right;
17252 int bottom;
17253
Romain Guyd928d682009-03-31 17:52:16 -070017254 public void setNextPoolable(InvalidateInfo element) {
17255 mNext = element;
17256 }
17257
17258 public InvalidateInfo getNextPoolable() {
17259 return mNext;
17260 }
17261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017262 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070017263 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017264 }
17265
17266 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070017267 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017268 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017269
17270 public boolean isPooled() {
17271 return mIsPooled;
17272 }
17273
17274 public void setPooled(boolean isPooled) {
17275 mIsPooled = isPooled;
17276 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017277 }
17278
17279 final IWindowSession mSession;
17280
17281 final IWindow mWindow;
17282
17283 final IBinder mWindowToken;
17284
Jeff Brown98365d72012-08-19 20:30:52 -070017285 final Display mDisplay;
17286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017287 final Callbacks mRootCallbacks;
17288
Romain Guy59a12ca2011-06-09 17:48:21 -070017289 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080017290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017291 /**
17292 * The top view of the hierarchy.
17293 */
17294 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070017295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017296 IBinder mPanelParentWindowToken;
17297 Surface mSurface;
17298
Romain Guyb051e892010-09-28 19:09:36 -070017299 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080017300 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070017301 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080017302
Romain Guy7e4e5612012-03-05 14:37:29 -080017303 boolean mScreenOn;
17304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017305 /**
Romain Guy8506ab42009-06-11 17:35:47 -070017306 * Scale factor used by the compatibility mode
17307 */
17308 float mApplicationScale;
17309
17310 /**
17311 * Indicates whether the application is in compatibility mode
17312 */
17313 boolean mScalingRequired;
17314
17315 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017316 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080017317 */
17318 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080017319
Dianne Hackborn63042d62011-01-26 18:56:29 -080017320 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017321 * Left position of this view's window
17322 */
17323 int mWindowLeft;
17324
17325 /**
17326 * Top position of this view's window
17327 */
17328 int mWindowTop;
17329
17330 /**
Adam Powell26153a32010-11-08 15:22:27 -080017331 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070017332 */
Adam Powell26153a32010-11-08 15:22:27 -080017333 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070017334
17335 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017336 * For windows that are full-screen but using insets to layout inside
17337 * of the screen decorations, these are the current insets for the
17338 * content of the window.
17339 */
17340 final Rect mContentInsets = new Rect();
17341
17342 /**
17343 * For windows that are full-screen but using insets to layout inside
17344 * of the screen decorations, these are the current insets for the
17345 * actual visible parts of the window.
17346 */
17347 final Rect mVisibleInsets = new Rect();
17348
17349 /**
17350 * The internal insets given by this window. This value is
17351 * supplied by the client (through
17352 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
17353 * be given to the window manager when changed to be used in laying
17354 * out windows behind it.
17355 */
17356 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
17357 = new ViewTreeObserver.InternalInsetsInfo();
17358
17359 /**
17360 * All views in the window's hierarchy that serve as scroll containers,
17361 * used to determine if the window can be resized or must be panned
17362 * to adjust for a soft input area.
17363 */
17364 final ArrayList<View> mScrollContainers = new ArrayList<View>();
17365
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070017366 final KeyEvent.DispatcherState mKeyDispatchState
17367 = new KeyEvent.DispatcherState();
17368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017369 /**
17370 * Indicates whether the view's window currently has the focus.
17371 */
17372 boolean mHasWindowFocus;
17373
17374 /**
17375 * The current visibility of the window.
17376 */
17377 int mWindowVisibility;
17378
17379 /**
17380 * Indicates the time at which drawing started to occur.
17381 */
17382 long mDrawingTime;
17383
17384 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070017385 * Indicates whether or not ignoring the DIRTY_MASK flags.
17386 */
17387 boolean mIgnoreDirtyState;
17388
17389 /**
Romain Guy02ccac62011-06-24 13:20:23 -070017390 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
17391 * to avoid clearing that flag prematurely.
17392 */
17393 boolean mSetIgnoreDirtyState = false;
17394
17395 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017396 * Indicates whether the view's window is currently in touch mode.
17397 */
17398 boolean mInTouchMode;
17399
17400 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017401 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017402 * the next time it performs a traversal
17403 */
17404 boolean mRecomputeGlobalAttributes;
17405
17406 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017407 * Always report new attributes at next traversal.
17408 */
17409 boolean mForceReportNewAttributes;
17410
17411 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017412 * Set during a traveral if any views want to keep the screen on.
17413 */
17414 boolean mKeepScreenOn;
17415
17416 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017417 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
17418 */
17419 int mSystemUiVisibility;
17420
17421 /**
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070017422 * Hack to force certain system UI visibility flags to be cleared.
17423 */
17424 int mDisabledSystemUiVisibility;
17425
17426 /**
Dianne Hackborncf675782012-05-10 15:07:24 -070017427 * Last global system UI visibility reported by the window manager.
17428 */
17429 int mGlobalSystemUiVisibility;
17430
17431 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017432 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
17433 * attached.
17434 */
17435 boolean mHasSystemUiListeners;
17436
17437 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017438 * Set if the visibility of any views has changed.
17439 */
17440 boolean mViewVisibilityChanged;
17441
17442 /**
17443 * Set to true if a view has been scrolled.
17444 */
17445 boolean mViewScrollChanged;
17446
17447 /**
17448 * Global to the view hierarchy used as a temporary for dealing with
17449 * x/y points in the transparent region computations.
17450 */
17451 final int[] mTransparentLocation = new int[2];
17452
17453 /**
17454 * Global to the view hierarchy used as a temporary for dealing with
17455 * x/y points in the ViewGroup.invalidateChild implementation.
17456 */
17457 final int[] mInvalidateChildLocation = new int[2];
17458
Chet Haasec3aa3612010-06-17 08:50:37 -070017459
17460 /**
17461 * Global to the view hierarchy used as a temporary for dealing with
17462 * x/y location when view is transformed.
17463 */
17464 final float[] mTmpTransformLocation = new float[2];
17465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017466 /**
17467 * The view tree observer used to dispatch global events like
17468 * layout, pre-draw, touch mode change, etc.
17469 */
17470 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
17471
17472 /**
17473 * A Canvas used by the view hierarchy to perform bitmap caching.
17474 */
17475 Canvas mCanvas;
17476
17477 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080017478 * The view root impl.
17479 */
17480 final ViewRootImpl mViewRootImpl;
17481
17482 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070017483 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017484 * handler can be used to pump events in the UI events queue.
17485 */
17486 final Handler mHandler;
17487
17488 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017489 * Temporary for use in computing invalidate rectangles while
17490 * calling up the hierarchy.
17491 */
17492 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070017493
17494 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070017495 * Temporary for use in computing hit areas with transformed views
17496 */
17497 final RectF mTmpTransformRect = new RectF();
17498
17499 /**
Chet Haase599913d2012-07-23 16:22:05 -070017500 * Temporary for use in transforming invalidation rect
17501 */
17502 final Matrix mTmpMatrix = new Matrix();
17503
17504 /**
17505 * Temporary for use in transforming invalidation rect
17506 */
17507 final Transformation mTmpTransformation = new Transformation();
17508
17509 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070017510 * Temporary list for use in collecting focusable descendents of a view.
17511 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070017512 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070017513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017514 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017515 * The id of the window for accessibility purposes.
17516 */
17517 int mAccessibilityWindowId = View.NO_ID;
17518
17519 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -070017520 * Whether to ingore not exposed for accessibility Views when
17521 * reporting the view tree to accessibility services.
17522 */
17523 boolean mIncludeNotImportantViews;
17524
17525 /**
17526 * The drawable for highlighting accessibility focus.
17527 */
17528 Drawable mAccessibilityFocusDrawable;
17529
17530 /**
Philip Milne10ca24a2012-04-23 15:38:27 -070017531 * Show where the margins, bounds and layout bounds are for each view.
17532 */
Dianne Hackborna53de062012-05-08 18:53:51 -070017533 boolean mDebugLayout = SystemProperties.getBoolean(DEBUG_LAYOUT_PROPERTY, false);
Philip Milne10ca24a2012-04-23 15:38:27 -070017534
17535 /**
Romain Guyab4c4f4f2012-05-06 13:11:24 -070017536 * Point used to compute visible regions.
17537 */
17538 final Point mPoint = new Point();
17539
17540 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017541 * Creates a new set of attachment information with the specified
17542 * events handler and thread.
17543 *
17544 * @param handler the events handler the view must use
17545 */
Jeff Brown98365d72012-08-19 20:30:52 -070017546 AttachInfo(IWindowSession session, IWindow window, Display display,
Jeff Browna175a5b2012-02-15 19:18:31 -080017547 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017548 mSession = session;
17549 mWindow = window;
17550 mWindowToken = window.asBinder();
Jeff Brown98365d72012-08-19 20:30:52 -070017551 mDisplay = display;
Jeff Browna175a5b2012-02-15 19:18:31 -080017552 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017553 mHandler = handler;
17554 mRootCallbacks = effectPlayer;
17555 }
17556 }
17557
17558 /**
17559 * <p>ScrollabilityCache holds various fields used by a View when scrolling
17560 * is supported. This avoids keeping too many unused fields in most
17561 * instances of View.</p>
17562 */
Mike Cleronf116bf82009-09-27 19:14:12 -070017563 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080017564
Mike Cleronf116bf82009-09-27 19:14:12 -070017565 /**
17566 * Scrollbars are not visible
17567 */
17568 public static final int OFF = 0;
17569
17570 /**
17571 * Scrollbars are visible
17572 */
17573 public static final int ON = 1;
17574
17575 /**
17576 * Scrollbars are fading away
17577 */
17578 public static final int FADING = 2;
17579
17580 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080017581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017582 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070017583 public int scrollBarDefaultDelayBeforeFade;
17584 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017585
17586 public int scrollBarSize;
17587 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070017588 public float[] interpolatorValues;
17589 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017590
17591 public final Paint paint;
17592 public final Matrix matrix;
17593 public Shader shader;
17594
Mike Cleronf116bf82009-09-27 19:14:12 -070017595 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
17596
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017597 private static final float[] OPAQUE = { 255 };
17598 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080017599
Mike Cleronf116bf82009-09-27 19:14:12 -070017600 /**
17601 * When fading should start. This time moves into the future every time
17602 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
17603 */
17604 public long fadeStartTime;
17605
17606
17607 /**
17608 * The current state of the scrollbars: ON, OFF, or FADING
17609 */
17610 public int state = OFF;
17611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017612 private int mLastColor;
17613
Mike Cleronf116bf82009-09-27 19:14:12 -070017614 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017615 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
17616 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070017617 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
17618 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017619
17620 paint = new Paint();
17621 matrix = new Matrix();
17622 // use use a height of 1, and then wack the matrix each time we
17623 // actually use it.
17624 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017626 paint.setShader(shader);
17627 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070017628 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017629 }
Romain Guy8506ab42009-06-11 17:35:47 -070017630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017631 public void setFadeColor(int color) {
17632 if (color != 0 && color != mLastColor) {
17633 mLastColor = color;
17634 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070017635
Romain Guye55e1a72009-08-27 10:42:26 -070017636 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
17637 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017639 paint.setShader(shader);
17640 // Restore the default transfer mode (src_over)
17641 paint.setXfermode(null);
17642 }
17643 }
Joe Malin32736f02011-01-19 16:14:20 -080017644
Mike Cleronf116bf82009-09-27 19:14:12 -070017645 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070017646 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070017647 if (now >= fadeStartTime) {
17648
17649 // the animation fades the scrollbars out by changing
17650 // the opacity (alpha) from fully opaque to fully
17651 // transparent
17652 int nextFrame = (int) now;
17653 int framesCount = 0;
17654
17655 Interpolator interpolator = scrollBarInterpolator;
17656
17657 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017658 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070017659
17660 // End transparent
17661 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017662 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070017663
17664 state = FADING;
17665
17666 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080017667 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070017668 }
17669 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070017670 }
Mike Cleronf116bf82009-09-27 19:14:12 -070017671
Svetoslav Ganova0156172011-06-26 17:55:44 -070017672 /**
17673 * Resuable callback for sending
17674 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
17675 */
17676 private class SendViewScrolledAccessibilityEvent implements Runnable {
17677 public volatile boolean mIsPending;
17678
17679 public void run() {
17680 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
17681 mIsPending = false;
17682 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017683 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017684
17685 /**
17686 * <p>
17687 * This class represents a delegate that can be registered in a {@link View}
17688 * to enhance accessibility support via composition rather via inheritance.
17689 * It is specifically targeted to widget developers that extend basic View
17690 * classes i.e. classes in package android.view, that would like their
17691 * applications to be backwards compatible.
17692 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080017693 * <div class="special reference">
17694 * <h3>Developer Guides</h3>
17695 * <p>For more information about making applications accessible, read the
17696 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
17697 * developer guide.</p>
17698 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017699 * <p>
17700 * A scenario in which a developer would like to use an accessibility delegate
17701 * is overriding a method introduced in a later API version then the minimal API
17702 * version supported by the application. For example, the method
17703 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
17704 * in API version 4 when the accessibility APIs were first introduced. If a
17705 * developer would like his application to run on API version 4 devices (assuming
17706 * all other APIs used by the application are version 4 or lower) and take advantage
17707 * of this method, instead of overriding the method which would break the application's
17708 * backwards compatibility, he can override the corresponding method in this
17709 * delegate and register the delegate in the target View if the API version of
17710 * the system is high enough i.e. the API version is same or higher to the API
17711 * version that introduced
17712 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
17713 * </p>
17714 * <p>
17715 * Here is an example implementation:
17716 * </p>
17717 * <code><pre><p>
17718 * if (Build.VERSION.SDK_INT >= 14) {
17719 * // If the API version is equal of higher than the version in
17720 * // which onInitializeAccessibilityNodeInfo was introduced we
17721 * // register a delegate with a customized implementation.
17722 * View view = findViewById(R.id.view_id);
17723 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
17724 * public void onInitializeAccessibilityNodeInfo(View host,
17725 * AccessibilityNodeInfo info) {
17726 * // Let the default implementation populate the info.
17727 * super.onInitializeAccessibilityNodeInfo(host, info);
17728 * // Set some other information.
17729 * info.setEnabled(host.isEnabled());
17730 * }
17731 * });
17732 * }
17733 * </code></pre></p>
17734 * <p>
17735 * This delegate contains methods that correspond to the accessibility methods
17736 * in View. If a delegate has been specified the implementation in View hands
17737 * off handling to the corresponding method in this delegate. The default
17738 * implementation the delegate methods behaves exactly as the corresponding
17739 * method in View for the case of no accessibility delegate been set. Hence,
17740 * to customize the behavior of a View method, clients can override only the
17741 * corresponding delegate method without altering the behavior of the rest
17742 * accessibility related methods of the host view.
17743 * </p>
17744 */
17745 public static class AccessibilityDelegate {
17746
17747 /**
17748 * Sends an accessibility event of the given type. If accessibility is not
17749 * enabled this method has no effect.
17750 * <p>
17751 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
17752 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
17753 * been set.
17754 * </p>
17755 *
17756 * @param host The View hosting the delegate.
17757 * @param eventType The type of the event to send.
17758 *
17759 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
17760 */
17761 public void sendAccessibilityEvent(View host, int eventType) {
17762 host.sendAccessibilityEventInternal(eventType);
17763 }
17764
17765 /**
alanv8eeefef2012-05-07 16:57:53 -070017766 * Performs the specified accessibility action on the view. For
17767 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
17768 * <p>
17769 * The default implementation behaves as
17770 * {@link View#performAccessibilityAction(int, Bundle)
17771 * View#performAccessibilityAction(int, Bundle)} for the case of
17772 * no accessibility delegate been set.
17773 * </p>
17774 *
17775 * @param action The action to perform.
17776 * @return Whether the action was performed.
17777 *
17778 * @see View#performAccessibilityAction(int, Bundle)
17779 * View#performAccessibilityAction(int, Bundle)
17780 */
17781 public boolean performAccessibilityAction(View host, int action, Bundle args) {
17782 return host.performAccessibilityActionInternal(action, args);
17783 }
17784
17785 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017786 * Sends an accessibility event. This method behaves exactly as
17787 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
17788 * empty {@link AccessibilityEvent} and does not perform a check whether
17789 * accessibility is enabled.
17790 * <p>
17791 * The default implementation behaves as
17792 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17793 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
17794 * the case of no accessibility delegate been set.
17795 * </p>
17796 *
17797 * @param host The View hosting the delegate.
17798 * @param event The event to send.
17799 *
17800 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17801 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17802 */
17803 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
17804 host.sendAccessibilityEventUncheckedInternal(event);
17805 }
17806
17807 /**
17808 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
17809 * to its children for adding their text content to the event.
17810 * <p>
17811 * The default implementation behaves as
17812 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17813 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
17814 * the case of no accessibility delegate been set.
17815 * </p>
17816 *
17817 * @param host The View hosting the delegate.
17818 * @param event The event.
17819 * @return True if the event population was completed.
17820 *
17821 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17822 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17823 */
17824 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17825 return host.dispatchPopulateAccessibilityEventInternal(event);
17826 }
17827
17828 /**
17829 * Gives a chance to the host View to populate the accessibility event with its
17830 * text content.
17831 * <p>
17832 * The default implementation behaves as
17833 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
17834 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
17835 * the case of no accessibility delegate been set.
17836 * </p>
17837 *
17838 * @param host The View hosting the delegate.
17839 * @param event The accessibility event which to populate.
17840 *
17841 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
17842 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
17843 */
17844 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17845 host.onPopulateAccessibilityEventInternal(event);
17846 }
17847
17848 /**
17849 * Initializes an {@link AccessibilityEvent} with information about the
17850 * the host View which is the event source.
17851 * <p>
17852 * The default implementation behaves as
17853 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
17854 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
17855 * the case of no accessibility delegate been set.
17856 * </p>
17857 *
17858 * @param host The View hosting the delegate.
17859 * @param event The event to initialize.
17860 *
17861 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
17862 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
17863 */
17864 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
17865 host.onInitializeAccessibilityEventInternal(event);
17866 }
17867
17868 /**
17869 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
17870 * <p>
17871 * The default implementation behaves as
17872 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17873 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
17874 * the case of no accessibility delegate been set.
17875 * </p>
17876 *
17877 * @param host The View hosting the delegate.
17878 * @param info The instance to initialize.
17879 *
17880 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17881 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17882 */
17883 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
17884 host.onInitializeAccessibilityNodeInfoInternal(info);
17885 }
17886
17887 /**
17888 * Called when a child of the host View has requested sending an
17889 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
17890 * to augment the event.
17891 * <p>
17892 * The default implementation behaves as
17893 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17894 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
17895 * the case of no accessibility delegate been set.
17896 * </p>
17897 *
17898 * @param host The View hosting the delegate.
17899 * @param child The child which requests sending the event.
17900 * @param event The event to be sent.
17901 * @return True if the event should be sent
17902 *
17903 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17904 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17905 */
17906 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
17907 AccessibilityEvent event) {
17908 return host.onRequestSendAccessibilityEventInternal(child, event);
17909 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070017910
17911 /**
17912 * Gets the provider for managing a virtual view hierarchy rooted at this View
17913 * and reported to {@link android.accessibilityservice.AccessibilityService}s
17914 * that explore the window content.
17915 * <p>
17916 * The default implementation behaves as
17917 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
17918 * the case of no accessibility delegate been set.
17919 * </p>
17920 *
17921 * @return The provider.
17922 *
17923 * @see AccessibilityNodeProvider
17924 */
17925 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
17926 return null;
17927 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017928 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017929}