blob: a89c970a985dcea56cd14b8c2808b1aa9412297f [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view;
18
Christopher Tatea53146c2010-09-07 11:57:52 -070019import android.content.ClipData;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.Context;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080021import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.res.Resources;
23import android.content.res.TypedArray;
24import android.graphics.Bitmap;
Adam Powell2b342f02010-08-18 18:14:13 -070025import android.graphics.Camera;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.graphics.Canvas;
Philip Milne1557fd72012-04-04 23:41:34 -070027import android.graphics.Insets;
Mike Cleronf116bf82009-09-27 19:14:12 -070028import android.graphics.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.graphics.LinearGradient;
30import android.graphics.Matrix;
31import android.graphics.Paint;
32import android.graphics.PixelFormat;
svetoslavganov75986cf2009-05-14 22:28:01 -070033import android.graphics.Point;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.graphics.PorterDuff;
35import android.graphics.PorterDuffXfermode;
36import android.graphics.Rect;
Adam Powell6e346362010-07-23 10:18:23 -070037import android.graphics.RectF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.graphics.Region;
39import android.graphics.Shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.graphics.drawable.ColorDrawable;
41import android.graphics.drawable.Drawable;
Svetoslav Ganovaa780c12012-04-19 23:01:39 -070042import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.os.Handler;
44import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.os.Parcel;
46import android.os.Parcelable;
47import android.os.RemoteException;
48import android.os.SystemClock;
Philip Milne10ca24a2012-04-23 15:38:27 -070049import android.os.SystemProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.util.AttributeSet;
Doug Feltcb3791202011-07-07 11:57:48 -070051import android.util.FloatProperty;
52import android.util.LocaleUtil;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070054import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070055import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070056import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.util.Pools;
Doug Feltcb3791202011-07-07 11:57:48 -070058import android.util.Property;
svetoslavganov75986cf2009-05-14 22:28:01 -070059import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080060import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.view.ContextMenu.ContextMenuInfo;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -070062import android.view.AccessibilityIterators.TextSegmentIterator;
63import android.view.AccessibilityIterators.CharacterTextSegmentIterator;
64import android.view.AccessibilityIterators.WordTextSegmentIterator;
65import android.view.AccessibilityIterators.ParagraphTextSegmentIterator;
svetoslavganov75986cf2009-05-14 22:28:01 -070066import android.view.accessibility.AccessibilityEvent;
67import android.view.accessibility.AccessibilityEventSource;
68import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070069import android.view.accessibility.AccessibilityNodeInfo;
Svetoslav Ganov02107852011-10-03 17:06:56 -070070import android.view.accessibility.AccessibilityNodeProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070072import android.view.animation.AnimationUtils;
Chet Haase64a48c12012-02-13 16:33:29 -080073import android.view.animation.Transformation;
svetoslavganov75986cf2009-05-14 22:28:01 -070074import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.view.inputmethod.InputConnection;
76import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.widget.ScrollBarDrawable;
78
Romain Guy1ef3fdb2011-09-09 15:30:30 -070079import static android.os.Build.VERSION_CODES.*;
Philip Milne6c8ea062012-04-03 17:38:43 -070080import static java.lang.Math.max;
Romain Guy1ef3fdb2011-09-09 15:30:30 -070081
Doug Feltcb3791202011-07-07 11:57:48 -070082import com.android.internal.R;
83import com.android.internal.util.Predicate;
84import com.android.internal.view.menu.MenuBuilder;
85
Christopher Tatea0374192010-10-05 13:06:41 -070086import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070087import java.lang.reflect.InvocationTargetException;
88import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089import java.util.ArrayList;
90import java.util.Arrays;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070091import java.util.Locale;
Adam Powell4afd62b2011-02-18 15:02:18 -080092import java.util.concurrent.CopyOnWriteArrayList;
Adam Powella9108a22012-07-18 11:18:09 -070093import java.util.concurrent.atomic.AtomicInteger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094
95/**
96 * <p>
97 * This class represents the basic building block for user interface components. A View
98 * occupies a rectangular area on the screen and is responsible for drawing and
99 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -0700100 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
102 * are invisible containers that hold other Views (or other ViewGroups) and define
103 * their layout properties.
104 * </p>
105 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700106 * <div class="special reference">
107 * <h3>Developer Guides</h3>
108 * <p>For information about using this class to develop your application's user interface,
109 * 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 -0800110 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700111 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 * <a name="Using"></a>
113 * <h3>Using Views</h3>
114 * <p>
115 * All of the views in a window are arranged in a single tree. You can add views
116 * either from code or by specifying a tree of views in one or more XML layout
117 * files. There are many specialized subclasses of views that act as controls or
118 * are capable of displaying text, images, or other content.
119 * </p>
120 * <p>
121 * Once you have created a tree of views, there are typically a few types of
122 * common operations you may wish to perform:
123 * <ul>
124 * <li><strong>Set properties:</strong> for example setting the text of a
125 * {@link android.widget.TextView}. The available properties and the methods
126 * that set them will vary among the different subclasses of views. Note that
127 * properties that are known at build time can be set in the XML layout
128 * files.</li>
129 * <li><strong>Set focus:</strong> The framework will handled moving focus in
130 * response to user input. To force focus to a specific view, call
131 * {@link #requestFocus}.</li>
132 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
133 * that will be notified when something interesting happens to the view. For
134 * example, all views will let you set a listener to be notified when the view
135 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700136 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
Philip Milne6c8ea062012-04-03 17:38:43 -0700137 * Other view subclasses offer more specialized listeners. For example, a Button
Romain Guy5c22a8c2011-05-13 11:48:45 -0700138 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700140 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 * </ul>
142 * </p>
143 * <p><em>
144 * Note: The Android framework is responsible for measuring, laying out and
145 * drawing views. You should not call methods that perform these actions on
146 * views yourself unless you are actually implementing a
147 * {@link android.view.ViewGroup}.
148 * </em></p>
149 *
150 * <a name="Lifecycle"></a>
151 * <h3>Implementing a Custom View</h3>
152 *
153 * <p>
154 * To implement a custom view, you will usually begin by providing overrides for
155 * some of the standard methods that the framework calls on all views. You do
156 * not need to override all of these methods. In fact, you can start by just
157 * overriding {@link #onDraw(android.graphics.Canvas)}.
158 * <table border="2" width="85%" align="center" cellpadding="5">
159 * <thead>
160 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
161 * </thead>
162 *
163 * <tbody>
164 * <tr>
165 * <td rowspan="2">Creation</td>
166 * <td>Constructors</td>
167 * <td>There is a form of the constructor that are called when the view
168 * is created from code and a form that is called when the view is
169 * inflated from a layout file. The second form should parse and apply
170 * any attributes defined in the layout file.
171 * </td>
172 * </tr>
173 * <tr>
174 * <td><code>{@link #onFinishInflate()}</code></td>
175 * <td>Called after a view and all of its children has been inflated
176 * from XML.</td>
177 * </tr>
178 *
179 * <tr>
180 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700181 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 * <td>Called to determine the size requirements for this view and all
183 * of its children.
184 * </td>
185 * </tr>
186 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700187 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 * <td>Called when this view should assign a size and position to all
189 * of its children.
190 * </td>
191 * </tr>
192 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700193 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 * <td>Called when the size of this view has changed.
195 * </td>
196 * </tr>
197 *
198 * <tr>
199 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700200 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 * <td>Called when the view should render its content.
202 * </td>
203 * </tr>
204 *
205 * <tr>
206 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700207 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
Jean Chalard405bc512012-05-29 19:12:34 +0900208 * <td>Called when a new hardware key event occurs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 * </td>
210 * </tr>
211 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700212 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
Jean Chalard405bc512012-05-29 19:12:34 +0900213 * <td>Called when a hardware key up event occurs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 * </td>
215 * </tr>
216 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700217 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 * <td>Called when a trackball motion event occurs.
219 * </td>
220 * </tr>
221 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700222 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 * <td>Called when a touch screen motion event occurs.
224 * </td>
225 * </tr>
226 *
227 * <tr>
228 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700229 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 * <td>Called when the view gains or loses focus.
231 * </td>
232 * </tr>
233 *
234 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700235 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 * <td>Called when the window containing the view gains or loses focus.
237 * </td>
238 * </tr>
239 *
240 * <tr>
241 * <td rowspan="3">Attaching</td>
242 * <td><code>{@link #onAttachedToWindow()}</code></td>
243 * <td>Called when the view is attached to a window.
244 * </td>
245 * </tr>
246 *
247 * <tr>
248 * <td><code>{@link #onDetachedFromWindow}</code></td>
249 * <td>Called when the view is detached from its window.
250 * </td>
251 * </tr>
252 *
253 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700254 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 * <td>Called when the visibility of the window containing the view
256 * has changed.
257 * </td>
258 * </tr>
259 * </tbody>
260 *
261 * </table>
262 * </p>
263 *
264 * <a name="IDs"></a>
265 * <h3>IDs</h3>
266 * Views may have an integer id associated with them. These ids are typically
267 * assigned in the layout XML files, and are used to find specific views within
268 * the view tree. A common pattern is to:
269 * <ul>
270 * <li>Define a Button in the layout file and assign it a unique ID.
271 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700272 * &lt;Button
273 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 * android:layout_width="wrap_content"
275 * android:layout_height="wrap_content"
276 * android:text="@string/my_button_text"/&gt;
277 * </pre></li>
278 * <li>From the onCreate method of an Activity, find the Button
279 * <pre class="prettyprint">
280 * Button myButton = (Button) findViewById(R.id.my_button);
281 * </pre></li>
282 * </ul>
283 * <p>
284 * View IDs need not be unique throughout the tree, but it is good practice to
285 * ensure that they are at least unique within the part of the tree you are
286 * searching.
287 * </p>
288 *
289 * <a name="Position"></a>
290 * <h3>Position</h3>
291 * <p>
292 * The geometry of a view is that of a rectangle. A view has a location,
293 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
294 * two dimensions, expressed as a width and a height. The unit for location
295 * and dimensions is the pixel.
296 * </p>
297 *
298 * <p>
299 * It is possible to retrieve the location of a view by invoking the methods
300 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
301 * coordinate of the rectangle representing the view. The latter returns the
302 * top, or Y, coordinate of the rectangle representing the view. These methods
303 * both return the location of the view relative to its parent. For instance,
304 * when getLeft() returns 20, that means the view is located 20 pixels to the
305 * right of the left edge of its direct parent.
306 * </p>
307 *
308 * <p>
309 * In addition, several convenience methods are offered to avoid unnecessary
310 * computations, namely {@link #getRight()} and {@link #getBottom()}.
311 * These methods return the coordinates of the right and bottom edges of the
312 * rectangle representing the view. For instance, calling {@link #getRight()}
313 * is similar to the following computation: <code>getLeft() + getWidth()</code>
314 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
315 * </p>
316 *
317 * <a name="SizePaddingMargins"></a>
318 * <h3>Size, padding and margins</h3>
319 * <p>
320 * The size of a view is expressed with a width and a height. A view actually
321 * possess two pairs of width and height values.
322 * </p>
323 *
324 * <p>
325 * The first pair is known as <em>measured width</em> and
326 * <em>measured height</em>. These dimensions define how big a view wants to be
327 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
328 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
329 * and {@link #getMeasuredHeight()}.
330 * </p>
331 *
332 * <p>
333 * The second pair is simply known as <em>width</em> and <em>height</em>, or
334 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
335 * dimensions define the actual size of the view on screen, at drawing time and
336 * after layout. These values may, but do not have to, be different from the
337 * measured width and height. The width and height can be obtained by calling
338 * {@link #getWidth()} and {@link #getHeight()}.
339 * </p>
340 *
341 * <p>
342 * To measure its dimensions, a view takes into account its padding. The padding
343 * is expressed in pixels for the left, top, right and bottom parts of the view.
344 * Padding can be used to offset the content of the view by a specific amount of
345 * pixels. For instance, a left padding of 2 will push the view's content by
346 * 2 pixels to the right of the left edge. Padding can be set using the
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -0700347 * {@link #setPadding(int, int, int, int)} or {@link #setPaddingRelative(int, int, int, int)}
348 * method and queried by calling {@link #getPaddingLeft()}, {@link #getPaddingTop()},
349 * {@link #getPaddingRight()}, {@link #getPaddingBottom()}, {@link #getPaddingStart()},
350 * {@link #getPaddingEnd()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 * </p>
352 *
353 * <p>
354 * Even though a view can define a padding, it does not provide any support for
355 * margins. However, view groups provide such a support. Refer to
356 * {@link android.view.ViewGroup} and
357 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
358 * </p>
359 *
360 * <a name="Layout"></a>
361 * <h3>Layout</h3>
362 * <p>
363 * Layout is a two pass process: a measure pass and a layout pass. The measuring
364 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
365 * of the view tree. Each view pushes dimension specifications down the tree
366 * during the recursion. At the end of the measure pass, every view has stored
367 * its measurements. The second pass happens in
368 * {@link #layout(int,int,int,int)} and is also top-down. During
369 * this pass each parent is responsible for positioning all of its children
370 * using the sizes computed in the measure pass.
371 * </p>
372 *
373 * <p>
374 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
375 * {@link #getMeasuredHeight()} values must be set, along with those for all of
376 * that view's descendants. A view's measured width and measured height values
377 * must respect the constraints imposed by the view's parents. This guarantees
378 * that at the end of the measure pass, all parents accept all of their
379 * children's measurements. A parent view may call measure() more than once on
380 * its children. For example, the parent may measure each child once with
381 * unspecified dimensions to find out how big they want to be, then call
382 * measure() on them again with actual numbers if the sum of all the children's
383 * unconstrained sizes is too big or too small.
384 * </p>
385 *
386 * <p>
387 * The measure pass uses two classes to communicate dimensions. The
388 * {@link MeasureSpec} class is used by views to tell their parents how they
389 * want to be measured and positioned. The base LayoutParams class just
390 * describes how big the view wants to be for both width and height. For each
391 * dimension, it can specify one of:
392 * <ul>
393 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800394 * <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 -0800395 * (minus padding)
396 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
397 * enclose its content (plus padding).
398 * </ul>
399 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
400 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
401 * an X and Y value.
402 * </p>
403 *
404 * <p>
405 * MeasureSpecs are used to push requirements down the tree from parent to
406 * child. A MeasureSpec can be in one of three modes:
407 * <ul>
408 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
409 * of a child view. For example, a LinearLayout may call measure() on its child
410 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
411 * tall the child view wants to be given a width of 240 pixels.
412 * <li>EXACTLY: This is used by the parent to impose an exact size on the
413 * child. The child must use this size, and guarantee that all of its
414 * descendants will fit within this size.
415 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
416 * child. The child must gurantee that it and all of its descendants will fit
417 * within this size.
418 * </ul>
419 * </p>
420 *
421 * <p>
422 * To intiate a layout, call {@link #requestLayout}. This method is typically
423 * called by a view on itself when it believes that is can no longer fit within
424 * its current bounds.
425 * </p>
426 *
427 * <a name="Drawing"></a>
428 * <h3>Drawing</h3>
429 * <p>
430 * Drawing is handled by walking the tree and rendering each view that
Joe Fernandez558459f2011-10-13 16:47:36 -0700431 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 * this means that parents will draw before (i.e., behind) their children, with
433 * siblings drawn in the order they appear in the tree.
434 * If you set a background drawable for a View, then the View will draw it for you
435 * before calling back to its <code>onDraw()</code> method.
436 * </p>
437 *
438 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700439 * 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 -0800440 * </p>
441 *
442 * <p>
443 * To force a view to draw, call {@link #invalidate()}.
444 * </p>
445 *
446 * <a name="EventHandlingThreading"></a>
447 * <h3>Event Handling and Threading</h3>
448 * <p>
449 * The basic cycle of a view is as follows:
450 * <ol>
451 * <li>An event comes in and is dispatched to the appropriate view. The view
452 * handles the event and notifies any listeners.</li>
453 * <li>If in the course of processing the event, the view's bounds may need
454 * to be changed, the view will call {@link #requestLayout()}.</li>
455 * <li>Similarly, if in the course of processing the event the view's appearance
456 * may need to be changed, the view will call {@link #invalidate()}.</li>
457 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
458 * the framework will take care of measuring, laying out, and drawing the tree
459 * as appropriate.</li>
460 * </ol>
461 * </p>
462 *
463 * <p><em>Note: The entire view tree is single threaded. You must always be on
464 * the UI thread when calling any method on any view.</em>
465 * If you are doing work on other threads and want to update the state of a view
466 * from that thread, you should use a {@link Handler}.
467 * </p>
468 *
469 * <a name="FocusHandling"></a>
470 * <h3>Focus Handling</h3>
471 * <p>
472 * The framework will handle routine focus movement in response to user input.
473 * This includes changing the focus as views are removed or hidden, or as new
474 * views become available. Views indicate their willingness to take focus
475 * through the {@link #isFocusable} method. To change whether a view can take
476 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
477 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
478 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
479 * </p>
480 * <p>
481 * Focus movement is based on an algorithm which finds the nearest neighbor in a
482 * given direction. In rare cases, the default algorithm may not match the
483 * intended behavior of the developer. In these situations, you can provide
484 * explicit overrides by using these XML attributes in the layout file:
485 * <pre>
486 * nextFocusDown
487 * nextFocusLeft
488 * nextFocusRight
489 * nextFocusUp
490 * </pre>
491 * </p>
492 *
493 *
494 * <p>
495 * To get a particular view to take focus, call {@link #requestFocus()}.
496 * </p>
497 *
498 * <a name="TouchMode"></a>
499 * <h3>Touch Mode</h3>
500 * <p>
501 * When a user is navigating a user interface via directional keys such as a D-pad, it is
502 * necessary to give focus to actionable items such as buttons so the user can see
503 * what will take input. If the device has touch capabilities, however, and the user
504 * begins interacting with the interface by touching it, it is no longer necessary to
505 * always highlight, or give focus to, a particular view. This motivates a mode
506 * for interaction named 'touch mode'.
507 * </p>
508 * <p>
509 * For a touch capable device, once the user touches the screen, the device
510 * will enter touch mode. From this point onward, only views for which
511 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
512 * Other views that are touchable, like buttons, will not take focus when touched; they will
513 * only fire the on click listeners.
514 * </p>
515 * <p>
516 * Any time a user hits a directional key, such as a D-pad direction, the view device will
517 * exit touch mode, and find a view to take focus, so that the user may resume interacting
518 * with the user interface without touching the screen again.
519 * </p>
520 * <p>
521 * The touch mode state is maintained across {@link android.app.Activity}s. Call
522 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
523 * </p>
524 *
525 * <a name="Scrolling"></a>
526 * <h3>Scrolling</h3>
527 * <p>
528 * The framework provides basic support for views that wish to internally
529 * scroll their content. This includes keeping track of the X and Y scroll
530 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800531 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700532 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 * </p>
534 *
535 * <a name="Tags"></a>
536 * <h3>Tags</h3>
537 * <p>
538 * Unlike IDs, tags are not used to identify views. Tags are essentially an
539 * extra piece of information that can be associated with a view. They are most
540 * often used as a convenience to store data related to views in the views
541 * themselves rather than by putting them in a separate structure.
542 * </p>
543 *
Chet Haasecb150fe2012-05-03 15:15:05 -0700544 * <a name="Properties"></a>
545 * <h3>Properties</h3>
546 * <p>
547 * The View class exposes an {@link #ALPHA} property, as well as several transform-related
548 * properties, such as {@link #TRANSLATION_X} and {@link #TRANSLATION_Y}. These properties are
549 * available both in the {@link Property} form as well as in similarly-named setter/getter
550 * methods (such as {@link #setAlpha(float)} for {@link #ALPHA}). These properties can
551 * be used to set persistent state associated with these rendering-related properties on the view.
552 * The properties and methods can also be used in conjunction with
553 * {@link android.animation.Animator Animator}-based animations, described more in the
554 * <a href="#Animation">Animation</a> section.
555 * </p>
556 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 * <a name="Animation"></a>
558 * <h3>Animation</h3>
559 * <p>
Chet Haasecb150fe2012-05-03 15:15:05 -0700560 * Starting with Android 3.0, the preferred way of animating views is to use the
561 * {@link android.animation} package APIs. These {@link android.animation.Animator Animator}-based
562 * classes change actual properties of the View object, such as {@link #setAlpha(float) alpha} and
563 * {@link #setTranslationX(float) translationX}. This behavior is contrasted to that of the pre-3.0
564 * {@link android.view.animation.Animation Animation}-based classes, which instead animate only
565 * how the view is drawn on the display. In particular, the {@link ViewPropertyAnimator} class
566 * makes animating these View properties particularly easy and efficient.
567 * </p>
568 * <p>
569 * 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 -0800570 * You can attach an {@link Animation} object to a view using
571 * {@link #setAnimation(Animation)} or
572 * {@link #startAnimation(Animation)}. The animation can alter the scale,
573 * rotation, translation and alpha of a view over time. If the animation is
574 * attached to a view that has children, the animation will affect the entire
575 * subtree rooted by that node. When an animation is started, the framework will
576 * take care of redrawing the appropriate views until the animation completes.
577 * </p>
578 *
Jeff Brown85a31762010-09-01 17:01:00 -0700579 * <a name="Security"></a>
580 * <h3>Security</h3>
581 * <p>
582 * Sometimes it is essential that an application be able to verify that an action
583 * is being performed with the full knowledge and consent of the user, such as
584 * granting a permission request, making a purchase or clicking on an advertisement.
585 * Unfortunately, a malicious application could try to spoof the user into
586 * performing these actions, unaware, by concealing the intended purpose of the view.
587 * As a remedy, the framework offers a touch filtering mechanism that can be used to
588 * improve the security of views that provide access to sensitive functionality.
589 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700590 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800591 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700592 * will discard touches that are received whenever the view's window is obscured by
593 * another visible window. As a result, the view will not receive touches whenever a
594 * toast, dialog or other window appears above the view's window.
595 * </p><p>
596 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700597 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
598 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700599 * </p>
600 *
Romain Guy171c5922011-01-06 10:04:23 -0800601 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700602 * @attr ref android.R.styleable#View_background
603 * @attr ref android.R.styleable#View_clickable
604 * @attr ref android.R.styleable#View_contentDescription
605 * @attr ref android.R.styleable#View_drawingCacheQuality
606 * @attr ref android.R.styleable#View_duplicateParentState
607 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700608 * @attr ref android.R.styleable#View_requiresFadingEdge
Philip Milne6c8ea062012-04-03 17:38:43 -0700609 * @attr ref android.R.styleable#View_fadeScrollbars
Romain Guyd6a463a2009-05-21 23:10:10 -0700610 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700611 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700613 * @attr ref android.R.styleable#View_isScrollContainer
614 * @attr ref android.R.styleable#View_focusable
615 * @attr ref android.R.styleable#View_focusableInTouchMode
616 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
617 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800618 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700619 * @attr ref android.R.styleable#View_longClickable
620 * @attr ref android.R.styleable#View_minHeight
621 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 * @attr ref android.R.styleable#View_nextFocusDown
623 * @attr ref android.R.styleable#View_nextFocusLeft
624 * @attr ref android.R.styleable#View_nextFocusRight
625 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700626 * @attr ref android.R.styleable#View_onClick
627 * @attr ref android.R.styleable#View_padding
628 * @attr ref android.R.styleable#View_paddingBottom
629 * @attr ref android.R.styleable#View_paddingLeft
630 * @attr ref android.R.styleable#View_paddingRight
631 * @attr ref android.R.styleable#View_paddingTop
Fabrice Di Meglio101d5aa2012-02-16 18:36:06 -0800632 * @attr ref android.R.styleable#View_paddingStart
633 * @attr ref android.R.styleable#View_paddingEnd
Romain Guyd6a463a2009-05-21 23:10:10 -0700634 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800635 * @attr ref android.R.styleable#View_rotation
636 * @attr ref android.R.styleable#View_rotationX
637 * @attr ref android.R.styleable#View_rotationY
638 * @attr ref android.R.styleable#View_scaleX
639 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 * @attr ref android.R.styleable#View_scrollX
641 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700642 * @attr ref android.R.styleable#View_scrollbarSize
643 * @attr ref android.R.styleable#View_scrollbarStyle
644 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700645 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
646 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
648 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 * @attr ref android.R.styleable#View_scrollbarThumbVertical
650 * @attr ref android.R.styleable#View_scrollbarTrackVertical
651 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
652 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700653 * @attr ref android.R.styleable#View_soundEffectsEnabled
654 * @attr ref android.R.styleable#View_tag
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -0700655 * @attr ref android.R.styleable#View_textAlignment
Chet Haase73066682010-11-29 15:55:32 -0800656 * @attr ref android.R.styleable#View_transformPivotX
657 * @attr ref android.R.styleable#View_transformPivotY
658 * @attr ref android.R.styleable#View_translationX
659 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700660 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 *
662 * @see android.view.ViewGroup
663 */
Fabrice Di Megliob03b4342012-06-04 12:55:30 -0700664public class View implements Drawable.Callback, KeyEvent.Callback,
Adam Powell8fc54f92011-09-07 16:40:45 -0700665 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 private static final boolean DBG = false;
667
668 /**
669 * The logging tag used by this class with android.util.Log.
670 */
671 protected static final String VIEW_LOG_TAG = "View";
672
673 /**
Guang Zhu0d607fb2012-05-11 19:34:56 -0700674 * When set to true, apps will draw debugging information about their layouts.
Romain Guy4b8c4f82012-04-27 15:48:35 -0700675 *
676 * @hide
677 */
678 public static final String DEBUG_LAYOUT_PROPERTY = "debug.layout";
679
680 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 * Used to mark a View that has no ID.
682 */
683 public static final int NO_ID = -1;
684
685 /**
686 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
687 * calling setFlags.
688 */
689 private static final int NOT_FOCUSABLE = 0x00000000;
690
691 /**
692 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
693 * setFlags.
694 */
695 private static final int FOCUSABLE = 0x00000001;
696
697 /**
698 * Mask for use with setFlags indicating bits used for focus.
699 */
700 private static final int FOCUSABLE_MASK = 0x00000001;
701
702 /**
703 * This view will adjust its padding to fit sytem windows (e.g. status bar)
704 */
705 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
706
707 /**
Scott Main812634c22011-07-27 13:22:35 -0700708 * This view is visible.
709 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
710 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 */
712 public static final int VISIBLE = 0x00000000;
713
714 /**
715 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700716 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
717 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718 */
719 public static final int INVISIBLE = 0x00000004;
720
721 /**
722 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700723 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
724 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 */
726 public static final int GONE = 0x00000008;
727
728 /**
729 * Mask for use with setFlags indicating bits used for visibility.
730 * {@hide}
731 */
732 static final int VISIBILITY_MASK = 0x0000000C;
733
734 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
735
736 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700737 * This view is enabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 * Use with ENABLED_MASK when calling setFlags.
739 * {@hide}
740 */
741 static final int ENABLED = 0x00000000;
742
743 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700744 * This view is disabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 * Use with ENABLED_MASK when calling setFlags.
746 * {@hide}
747 */
748 static final int DISABLED = 0x00000020;
749
750 /**
751 * Mask for use with setFlags indicating bits used for indicating whether
752 * this view is enabled
753 * {@hide}
754 */
755 static final int ENABLED_MASK = 0x00000020;
756
757 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700758 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
759 * called and further optimizations will be performed. It is okay to have
760 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 * {@hide}
762 */
763 static final int WILL_NOT_DRAW = 0x00000080;
764
765 /**
766 * Mask for use with setFlags indicating bits used for indicating whether
767 * this view is will draw
768 * {@hide}
769 */
770 static final int DRAW_MASK = 0x00000080;
771
772 /**
773 * <p>This view doesn't show scrollbars.</p>
774 * {@hide}
775 */
776 static final int SCROLLBARS_NONE = 0x00000000;
777
778 /**
779 * <p>This view shows horizontal scrollbars.</p>
780 * {@hide}
781 */
782 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
783
784 /**
785 * <p>This view shows vertical scrollbars.</p>
786 * {@hide}
787 */
788 static final int SCROLLBARS_VERTICAL = 0x00000200;
789
790 /**
791 * <p>Mask for use with setFlags indicating bits used for indicating which
792 * scrollbars are enabled.</p>
793 * {@hide}
794 */
795 static final int SCROLLBARS_MASK = 0x00000300;
796
Jeff Brown85a31762010-09-01 17:01:00 -0700797 /**
798 * Indicates that the view should filter touches when its window is obscured.
799 * Refer to the class comments for more information about this security feature.
800 * {@hide}
801 */
802 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
803
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700804 /**
805 * Set for framework elements that use FITS_SYSTEM_WINDOWS, to indicate
806 * that they are optional and should be skipped if the window has
807 * requested system UI flags that ignore those insets for layout.
808 */
809 static final int OPTIONAL_FITS_SYSTEM_WINDOWS = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810
811 /**
812 * <p>This view doesn't show fading edges.</p>
813 * {@hide}
814 */
815 static final int FADING_EDGE_NONE = 0x00000000;
816
817 /**
818 * <p>This view shows horizontal fading edges.</p>
819 * {@hide}
820 */
821 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
822
823 /**
824 * <p>This view shows vertical fading edges.</p>
825 * {@hide}
826 */
827 static final int FADING_EDGE_VERTICAL = 0x00002000;
828
829 /**
830 * <p>Mask for use with setFlags indicating bits used for indicating which
831 * fading edges are enabled.</p>
832 * {@hide}
833 */
834 static final int FADING_EDGE_MASK = 0x00003000;
835
836 /**
837 * <p>Indicates this view can be clicked. When clickable, a View reacts
838 * to clicks by notifying the OnClickListener.<p>
839 * {@hide}
840 */
841 static final int CLICKABLE = 0x00004000;
842
843 /**
844 * <p>Indicates this view is caching its drawing into a bitmap.</p>
845 * {@hide}
846 */
847 static final int DRAWING_CACHE_ENABLED = 0x00008000;
848
849 /**
850 * <p>Indicates that no icicle should be saved for this view.<p>
851 * {@hide}
852 */
853 static final int SAVE_DISABLED = 0x000010000;
854
855 /**
856 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
857 * property.</p>
858 * {@hide}
859 */
860 static final int SAVE_DISABLED_MASK = 0x000010000;
861
862 /**
863 * <p>Indicates that no drawing cache should ever be created for this view.<p>
864 * {@hide}
865 */
866 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
867
868 /**
869 * <p>Indicates this view can take / keep focus when int touch mode.</p>
870 * {@hide}
871 */
872 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
873
874 /**
875 * <p>Enables low quality mode for the drawing cache.</p>
876 */
877 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
878
879 /**
880 * <p>Enables high quality mode for the drawing cache.</p>
881 */
882 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
883
884 /**
885 * <p>Enables automatic quality mode for the drawing cache.</p>
886 */
887 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
888
889 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
890 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
891 };
892
893 /**
894 * <p>Mask for use with setFlags indicating bits used for the cache
895 * quality property.</p>
896 * {@hide}
897 */
898 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
899
900 /**
901 * <p>
902 * Indicates this view can be long clicked. When long clickable, a View
903 * reacts to long clicks by notifying the OnLongClickListener or showing a
904 * context menu.
905 * </p>
906 * {@hide}
907 */
908 static final int LONG_CLICKABLE = 0x00200000;
909
910 /**
911 * <p>Indicates that this view gets its drawable states from its direct parent
912 * and ignores its original internal states.</p>
913 *
914 * @hide
915 */
916 static final int DUPLICATE_PARENT_STATE = 0x00400000;
917
918 /**
919 * The scrollbar style to display the scrollbars inside the content area,
920 * without increasing the padding. The scrollbars will be overlaid with
921 * translucency on the view's content.
922 */
923 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
924
925 /**
926 * The scrollbar style to display the scrollbars inside the padded area,
927 * increasing the padding of the view. The scrollbars will not overlap the
928 * content area of the view.
929 */
930 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
931
932 /**
933 * The scrollbar style to display the scrollbars at the edge of the view,
934 * without increasing the padding. The scrollbars will be overlaid with
935 * translucency.
936 */
937 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
938
939 /**
940 * The scrollbar style to display the scrollbars at the edge of the view,
941 * increasing the padding of the view. The scrollbars will only overlap the
942 * background, if any.
943 */
944 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
945
946 /**
947 * Mask to check if the scrollbar style is overlay or inset.
948 * {@hide}
949 */
950 static final int SCROLLBARS_INSET_MASK = 0x01000000;
951
952 /**
953 * Mask to check if the scrollbar style is inside or outside.
954 * {@hide}
955 */
956 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
957
958 /**
959 * Mask for scrollbar style.
960 * {@hide}
961 */
962 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
963
964 /**
965 * View flag indicating that the screen should remain on while the
966 * window containing this view is visible to the user. This effectively
967 * takes care of automatically setting the WindowManager's
968 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
969 */
970 public static final int KEEP_SCREEN_ON = 0x04000000;
971
972 /**
973 * View flag indicating whether this view should have sound effects enabled
974 * for events such as clicking and touching.
975 */
976 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
977
978 /**
979 * View flag indicating whether this view should have haptic feedback
980 * enabled for events such as long presses.
981 */
982 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
983
984 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700985 * <p>Indicates that the view hierarchy should stop saving state when
986 * it reaches this view. If state saving is initiated immediately at
987 * the view, it will be allowed.
988 * {@hide}
989 */
990 static final int PARENT_SAVE_DISABLED = 0x20000000;
991
992 /**
993 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
994 * {@hide}
995 */
996 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
997
998 /**
svetoslavganov75986cf2009-05-14 22:28:01 -0700999 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1000 * should add all focusable Views regardless if they are focusable in touch mode.
1001 */
1002 public static final int FOCUSABLES_ALL = 0x00000000;
1003
1004 /**
1005 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1006 * should add only Views focusable in touch mode.
1007 */
1008 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1009
1010 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001011 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 * item.
1013 */
1014 public static final int FOCUS_BACKWARD = 0x00000001;
1015
1016 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001017 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 * item.
1019 */
1020 public static final int FOCUS_FORWARD = 0x00000002;
1021
1022 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001023 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 */
1025 public static final int FOCUS_LEFT = 0x00000011;
1026
1027 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001028 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 */
1030 public static final int FOCUS_UP = 0x00000021;
1031
1032 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001033 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 */
1035 public static final int FOCUS_RIGHT = 0x00000042;
1036
1037 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001038 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 */
1040 public static final int FOCUS_DOWN = 0x00000082;
1041
Svetoslav Ganov42138042012-03-20 11:51:39 -07001042 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001043 * Bits of {@link #getMeasuredWidthAndState()} and
1044 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1045 */
1046 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1047
1048 /**
1049 * Bits of {@link #getMeasuredWidthAndState()} and
1050 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1051 */
1052 public static final int MEASURED_STATE_MASK = 0xff000000;
1053
1054 /**
1055 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1056 * for functions that combine both width and height into a single int,
1057 * such as {@link #getMeasuredState()} and the childState argument of
1058 * {@link #resolveSizeAndState(int, int, int)}.
1059 */
1060 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1061
1062 /**
1063 * Bit of {@link #getMeasuredWidthAndState()} and
1064 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1065 * is smaller that the space the view would like to have.
1066 */
1067 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1068
1069 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 * Base View state sets
1071 */
1072 // Singles
1073 /**
1074 * Indicates the view has no states set. States are used with
1075 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1076 * view depending on its state.
1077 *
1078 * @see android.graphics.drawable.Drawable
1079 * @see #getDrawableState()
1080 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001081 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082 /**
1083 * Indicates the view is enabled. States are used with
1084 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1085 * view depending on its state.
1086 *
1087 * @see android.graphics.drawable.Drawable
1088 * @see #getDrawableState()
1089 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001090 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 /**
1092 * Indicates the view is focused. States are used with
1093 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1094 * view depending on its state.
1095 *
1096 * @see android.graphics.drawable.Drawable
1097 * @see #getDrawableState()
1098 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001099 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 /**
1101 * Indicates the view is selected. States are used with
1102 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1103 * view depending on its state.
1104 *
1105 * @see android.graphics.drawable.Drawable
1106 * @see #getDrawableState()
1107 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001108 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109 /**
1110 * Indicates the view is pressed. States are used with
1111 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1112 * view depending on its state.
1113 *
1114 * @see android.graphics.drawable.Drawable
1115 * @see #getDrawableState()
1116 * @hide
1117 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001118 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 /**
1120 * Indicates the view's window has focus. States are used with
1121 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1122 * view depending on its state.
1123 *
1124 * @see android.graphics.drawable.Drawable
1125 * @see #getDrawableState()
1126 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001127 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 // Doubles
1129 /**
1130 * Indicates the view is enabled and has the focus.
1131 *
1132 * @see #ENABLED_STATE_SET
1133 * @see #FOCUSED_STATE_SET
1134 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001135 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 /**
1137 * Indicates the view is enabled and selected.
1138 *
1139 * @see #ENABLED_STATE_SET
1140 * @see #SELECTED_STATE_SET
1141 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001142 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 /**
1144 * Indicates the view is enabled and that its window has focus.
1145 *
1146 * @see #ENABLED_STATE_SET
1147 * @see #WINDOW_FOCUSED_STATE_SET
1148 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001149 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 /**
1151 * Indicates the view is focused and selected.
1152 *
1153 * @see #FOCUSED_STATE_SET
1154 * @see #SELECTED_STATE_SET
1155 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001156 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 /**
1158 * Indicates the view has the focus and that its window has the focus.
1159 *
1160 * @see #FOCUSED_STATE_SET
1161 * @see #WINDOW_FOCUSED_STATE_SET
1162 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001163 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 /**
1165 * Indicates the view is selected and that its window has the focus.
1166 *
1167 * @see #SELECTED_STATE_SET
1168 * @see #WINDOW_FOCUSED_STATE_SET
1169 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001170 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 // Triples
1172 /**
1173 * Indicates the view is enabled, focused and selected.
1174 *
1175 * @see #ENABLED_STATE_SET
1176 * @see #FOCUSED_STATE_SET
1177 * @see #SELECTED_STATE_SET
1178 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001179 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 /**
1181 * Indicates the view is enabled, focused and its window has the focus.
1182 *
1183 * @see #ENABLED_STATE_SET
1184 * @see #FOCUSED_STATE_SET
1185 * @see #WINDOW_FOCUSED_STATE_SET
1186 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001187 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 /**
1189 * Indicates the view is enabled, selected and its window has the focus.
1190 *
1191 * @see #ENABLED_STATE_SET
1192 * @see #SELECTED_STATE_SET
1193 * @see #WINDOW_FOCUSED_STATE_SET
1194 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001195 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 /**
1197 * Indicates the view is focused, selected and its window has the focus.
1198 *
1199 * @see #FOCUSED_STATE_SET
1200 * @see #SELECTED_STATE_SET
1201 * @see #WINDOW_FOCUSED_STATE_SET
1202 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001203 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 /**
1205 * Indicates the view is enabled, focused, selected and its window
1206 * has the focus.
1207 *
1208 * @see #ENABLED_STATE_SET
1209 * @see #FOCUSED_STATE_SET
1210 * @see #SELECTED_STATE_SET
1211 * @see #WINDOW_FOCUSED_STATE_SET
1212 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001213 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 /**
1215 * Indicates the view is pressed and its window has the focus.
1216 *
1217 * @see #PRESSED_STATE_SET
1218 * @see #WINDOW_FOCUSED_STATE_SET
1219 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001220 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 /**
1222 * Indicates the view is pressed and selected.
1223 *
1224 * @see #PRESSED_STATE_SET
1225 * @see #SELECTED_STATE_SET
1226 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001227 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 /**
1229 * Indicates the view is pressed, selected and its window has the focus.
1230 *
1231 * @see #PRESSED_STATE_SET
1232 * @see #SELECTED_STATE_SET
1233 * @see #WINDOW_FOCUSED_STATE_SET
1234 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001235 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 /**
1237 * Indicates the view is pressed and focused.
1238 *
1239 * @see #PRESSED_STATE_SET
1240 * @see #FOCUSED_STATE_SET
1241 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001242 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 /**
1244 * Indicates the view is pressed, focused and its window has the focus.
1245 *
1246 * @see #PRESSED_STATE_SET
1247 * @see #FOCUSED_STATE_SET
1248 * @see #WINDOW_FOCUSED_STATE_SET
1249 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001250 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 /**
1252 * Indicates the view is pressed, focused and selected.
1253 *
1254 * @see #PRESSED_STATE_SET
1255 * @see #SELECTED_STATE_SET
1256 * @see #FOCUSED_STATE_SET
1257 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001258 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 /**
1260 * Indicates the view is pressed, focused, selected and its window has the focus.
1261 *
1262 * @see #PRESSED_STATE_SET
1263 * @see #FOCUSED_STATE_SET
1264 * @see #SELECTED_STATE_SET
1265 * @see #WINDOW_FOCUSED_STATE_SET
1266 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001267 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 /**
1269 * Indicates the view is pressed and enabled.
1270 *
1271 * @see #PRESSED_STATE_SET
1272 * @see #ENABLED_STATE_SET
1273 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001274 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275 /**
1276 * Indicates the view is pressed, enabled and its window has the focus.
1277 *
1278 * @see #PRESSED_STATE_SET
1279 * @see #ENABLED_STATE_SET
1280 * @see #WINDOW_FOCUSED_STATE_SET
1281 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001282 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 /**
1284 * Indicates the view is pressed, enabled and selected.
1285 *
1286 * @see #PRESSED_STATE_SET
1287 * @see #ENABLED_STATE_SET
1288 * @see #SELECTED_STATE_SET
1289 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001290 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 /**
1292 * Indicates the view is pressed, enabled, selected and its window has the
1293 * focus.
1294 *
1295 * @see #PRESSED_STATE_SET
1296 * @see #ENABLED_STATE_SET
1297 * @see #SELECTED_STATE_SET
1298 * @see #WINDOW_FOCUSED_STATE_SET
1299 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001300 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 /**
1302 * Indicates the view is pressed, enabled and focused.
1303 *
1304 * @see #PRESSED_STATE_SET
1305 * @see #ENABLED_STATE_SET
1306 * @see #FOCUSED_STATE_SET
1307 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001308 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 /**
1310 * Indicates the view is pressed, enabled, focused and its window has the
1311 * focus.
1312 *
1313 * @see #PRESSED_STATE_SET
1314 * @see #ENABLED_STATE_SET
1315 * @see #FOCUSED_STATE_SET
1316 * @see #WINDOW_FOCUSED_STATE_SET
1317 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001318 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001319 /**
1320 * Indicates the view is pressed, enabled, focused and selected.
1321 *
1322 * @see #PRESSED_STATE_SET
1323 * @see #ENABLED_STATE_SET
1324 * @see #SELECTED_STATE_SET
1325 * @see #FOCUSED_STATE_SET
1326 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001327 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001328 /**
1329 * Indicates the view is pressed, enabled, focused, selected and its window
1330 * has the focus.
1331 *
1332 * @see #PRESSED_STATE_SET
1333 * @see #ENABLED_STATE_SET
1334 * @see #SELECTED_STATE_SET
1335 * @see #FOCUSED_STATE_SET
1336 * @see #WINDOW_FOCUSED_STATE_SET
1337 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001338 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339
1340 /**
1341 * The order here is very important to {@link #getDrawableState()}
1342 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001343 private static final int[][] VIEW_STATE_SETS;
1344
Romain Guyb051e892010-09-28 19:09:36 -07001345 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1346 static final int VIEW_STATE_SELECTED = 1 << 1;
1347 static final int VIEW_STATE_FOCUSED = 1 << 2;
1348 static final int VIEW_STATE_ENABLED = 1 << 3;
1349 static final int VIEW_STATE_PRESSED = 1 << 4;
1350 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001351 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001352 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001353 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1354 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001355
1356 static final int[] VIEW_STATE_IDS = new int[] {
1357 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1358 R.attr.state_selected, VIEW_STATE_SELECTED,
1359 R.attr.state_focused, VIEW_STATE_FOCUSED,
1360 R.attr.state_enabled, VIEW_STATE_ENABLED,
1361 R.attr.state_pressed, VIEW_STATE_PRESSED,
1362 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001363 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001364 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001365 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
Svetoslav Ganov42138042012-03-20 11:51:39 -07001366 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001367 };
1368
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001369 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001370 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1371 throw new IllegalStateException(
1372 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1373 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001374 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001375 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001376 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001377 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001378 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001379 orderedIds[i * 2] = viewState;
1380 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001381 }
1382 }
1383 }
Romain Guyb051e892010-09-28 19:09:36 -07001384 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1385 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1386 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001387 int numBits = Integer.bitCount(i);
1388 int[] set = new int[numBits];
1389 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001390 for (int j = 0; j < orderedIds.length; j += 2) {
1391 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001392 set[pos++] = orderedIds[j];
1393 }
1394 }
1395 VIEW_STATE_SETS[i] = set;
1396 }
1397
1398 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1399 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1400 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1401 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1402 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1403 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1404 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1405 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1406 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1407 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1408 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1409 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1410 | VIEW_STATE_FOCUSED];
1411 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1412 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1413 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1414 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1415 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1416 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1417 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1418 | VIEW_STATE_ENABLED];
1419 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1420 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1421 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1422 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1423 | VIEW_STATE_ENABLED];
1424 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1425 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1426 | VIEW_STATE_ENABLED];
1427 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1428 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1429 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1430
1431 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1432 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1433 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1434 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1435 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1436 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1437 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1438 | VIEW_STATE_PRESSED];
1439 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1440 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1441 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1442 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1443 | VIEW_STATE_PRESSED];
1444 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1445 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1446 | VIEW_STATE_PRESSED];
1447 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1448 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1449 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1450 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1451 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1452 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1453 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1454 | VIEW_STATE_PRESSED];
1455 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1456 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1457 | VIEW_STATE_PRESSED];
1458 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1459 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1460 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1461 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1462 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1463 | VIEW_STATE_PRESSED];
1464 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1465 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1466 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1467 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1468 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1469 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1470 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1471 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1472 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1473 | VIEW_STATE_PRESSED];
1474 }
1475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001476 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001477 * Accessibility event types that are dispatched for text population.
1478 */
1479 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1480 AccessibilityEvent.TYPE_VIEW_CLICKED
1481 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1482 | AccessibilityEvent.TYPE_VIEW_SELECTED
1483 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1484 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1485 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001486 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
Svetoslav Ganov84dd52e2011-11-18 10:24:00 -08001487 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
Svetoslav Ganov42138042012-03-20 11:51:39 -07001488 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001489 | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED
1490 | AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001491
1492 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001493 * Temporary Rect currently for use in setBackground(). This will probably
1494 * be extended in the future to hold our own class with more than just
1495 * a Rect. :)
1496 */
1497 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001498
1499 /**
1500 * Map used to store views' tags.
1501 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001502 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001505 * The next available accessiiblity id.
1506 */
1507 private static int sNextAccessibilityViewId;
1508
1509 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001510 * The animation currently associated with this view.
1511 * @hide
1512 */
1513 protected Animation mCurrentAnimation = null;
1514
1515 /**
1516 * Width as measured during measure pass.
1517 * {@hide}
1518 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001519 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001520 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521
1522 /**
1523 * Height as measured during measure pass.
1524 * {@hide}
1525 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001526 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001527 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528
1529 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001530 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1531 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1532 * its display list. This flag, used only when hw accelerated, allows us to clear the
1533 * flag while retaining this information until it's needed (at getDisplayList() time and
1534 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1535 *
1536 * {@hide}
1537 */
1538 boolean mRecreateDisplayList = false;
1539
1540 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 * The view's identifier.
1542 * {@hide}
1543 *
1544 * @see #setId(int)
1545 * @see #getId()
1546 */
1547 @ViewDebug.ExportedProperty(resolveId = true)
1548 int mID = NO_ID;
1549
1550 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001551 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001552 */
1553 int mAccessibilityViewId = NO_ID;
1554
1555 /**
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001556 * @hide
1557 */
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07001558 private int mAccessibilityCursorPosition = ACCESSIBILITY_CURSOR_POSITION_UNDEFINED;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001559
1560 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 * The view's tag.
1562 * {@hide}
1563 *
1564 * @see #setTag(Object)
1565 * @see #getTag()
1566 */
1567 protected Object mTag;
1568
1569 // for mPrivateFlags:
1570 /** {@hide} */
1571 static final int WANTS_FOCUS = 0x00000001;
1572 /** {@hide} */
1573 static final int FOCUSED = 0x00000002;
1574 /** {@hide} */
1575 static final int SELECTED = 0x00000004;
1576 /** {@hide} */
1577 static final int IS_ROOT_NAMESPACE = 0x00000008;
1578 /** {@hide} */
1579 static final int HAS_BOUNDS = 0x00000010;
1580 /** {@hide} */
1581 static final int DRAWN = 0x00000020;
1582 /**
1583 * When this flag is set, this view is running an animation on behalf of its
1584 * children and should therefore not cancel invalidate requests, even if they
1585 * lie outside of this view's bounds.
1586 *
1587 * {@hide}
1588 */
1589 static final int DRAW_ANIMATION = 0x00000040;
1590 /** {@hide} */
1591 static final int SKIP_DRAW = 0x00000080;
1592 /** {@hide} */
1593 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1594 /** {@hide} */
1595 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1596 /** {@hide} */
1597 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1598 /** {@hide} */
1599 static final int MEASURED_DIMENSION_SET = 0x00000800;
1600 /** {@hide} */
1601 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001602 /** {@hide} */
1603 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001604
1605 private static final int PRESSED = 0x00004000;
1606
1607 /** {@hide} */
1608 static final int DRAWING_CACHE_VALID = 0x00008000;
1609 /**
1610 * Flag used to indicate that this view should be drawn once more (and only once
1611 * more) after its animation has completed.
1612 * {@hide}
1613 */
1614 static final int ANIMATION_STARTED = 0x00010000;
1615
1616 private static final int SAVE_STATE_CALLED = 0x00020000;
1617
1618 /**
1619 * Indicates that the View returned true when onSetAlpha() was called and that
1620 * the alpha must be restored.
1621 * {@hide}
1622 */
1623 static final int ALPHA_SET = 0x00040000;
1624
1625 /**
1626 * Set by {@link #setScrollContainer(boolean)}.
1627 */
1628 static final int SCROLL_CONTAINER = 0x00080000;
1629
1630 /**
1631 * Set by {@link #setScrollContainer(boolean)}.
1632 */
1633 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1634
1635 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001636 * View flag indicating whether this view was invalidated (fully or partially.)
1637 *
1638 * @hide
1639 */
1640 static final int DIRTY = 0x00200000;
1641
1642 /**
1643 * View flag indicating whether this view was invalidated by an opaque
1644 * invalidate request.
1645 *
1646 * @hide
1647 */
1648 static final int DIRTY_OPAQUE = 0x00400000;
1649
1650 /**
1651 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1652 *
1653 * @hide
1654 */
1655 static final int DIRTY_MASK = 0x00600000;
1656
1657 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001658 * Indicates whether the background is opaque.
1659 *
1660 * @hide
1661 */
1662 static final int OPAQUE_BACKGROUND = 0x00800000;
1663
1664 /**
1665 * Indicates whether the scrollbars are opaque.
1666 *
1667 * @hide
1668 */
1669 static final int OPAQUE_SCROLLBARS = 0x01000000;
1670
1671 /**
1672 * Indicates whether the view is opaque.
1673 *
1674 * @hide
1675 */
1676 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001677
Adam Powelle14579b2009-12-16 18:39:52 -08001678 /**
1679 * Indicates a prepressed state;
1680 * the short time between ACTION_DOWN and recognizing
1681 * a 'real' press. Prepressed is used to recognize quick taps
1682 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001683 *
Adam Powelle14579b2009-12-16 18:39:52 -08001684 * @hide
1685 */
1686 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001687
Adam Powellc9fbaab2010-02-16 17:16:19 -08001688 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001689 * Indicates whether the view is temporarily detached.
1690 *
1691 * @hide
1692 */
1693 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001694
Adam Powell8568c3a2010-04-19 14:26:11 -07001695 /**
1696 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001697 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001698 * @hide
1699 */
1700 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001701
1702 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001703 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1704 * @hide
1705 */
1706 private static final int HOVERED = 0x10000000;
1707
1708 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001709 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1710 * for transform operations
1711 *
1712 * @hide
1713 */
Adam Powellf37df072010-09-17 16:22:49 -07001714 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001715
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001716 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001717 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001718
Chet Haasefd2b0022010-08-06 13:08:56 -07001719 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001720 * Indicates that this view was specifically invalidated, not just dirtied because some
1721 * child view was invalidated. The flag is used to determine when we need to recreate
1722 * a view's display list (as opposed to just returning a reference to its existing
1723 * display list).
1724 *
1725 * @hide
1726 */
1727 static final int INVALIDATED = 0x80000000;
1728
Christopher Tate3d4bf172011-03-28 16:16:46 -07001729 /* Masks for mPrivateFlags2 */
1730
1731 /**
1732 * Indicates that this view has reported that it can accept the current drag's content.
1733 * Cleared when the drag operation concludes.
1734 * @hide
1735 */
1736 static final int DRAG_CAN_ACCEPT = 0x00000001;
1737
1738 /**
1739 * Indicates that this view is currently directly under the drag location in a
1740 * drag-and-drop operation involving content that it can accept. Cleared when
1741 * the drag exits the view, or when the drag operation concludes.
1742 * @hide
1743 */
1744 static final int DRAG_HOVERED = 0x00000002;
1745
Cibu Johny86666632010-02-22 13:01:02 -08001746 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001747 * Horizontal layout direction of this view is from Left to Right.
1748 * Use with {@link #setLayoutDirection}.
Cibu Johny86666632010-02-22 13:01:02 -08001749 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001750 public static final int LAYOUT_DIRECTION_LTR = 0;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001751
1752 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001753 * Horizontal layout direction of this view is from Right to Left.
1754 * Use with {@link #setLayoutDirection}.
1755 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001756 public static final int LAYOUT_DIRECTION_RTL = 1;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001757
1758 /**
1759 * Horizontal layout direction of this view is inherited from its parent.
1760 * Use with {@link #setLayoutDirection}.
1761 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001762 public static final int LAYOUT_DIRECTION_INHERIT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001763
1764 /**
1765 * Horizontal layout direction of this view is from deduced from the default language
1766 * script for the locale. Use with {@link #setLayoutDirection}.
1767 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001768 public static final int LAYOUT_DIRECTION_LOCALE = 3;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001769
1770 /**
1771 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001772 * @hide
1773 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001774 static final int LAYOUT_DIRECTION_MASK_SHIFT = 2;
1775
1776 /**
1777 * Mask for use with private flags indicating bits used for horizontal layout direction.
1778 * @hide
1779 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001780 static final int LAYOUT_DIRECTION_MASK = 0x00000003 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001781
1782 /**
1783 * Indicates whether the view horizontal layout direction has been resolved and drawn to the
1784 * right-to-left direction.
1785 * @hide
1786 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001787 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 4 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001788
1789 /**
1790 * Indicates whether the view horizontal layout direction has been resolved.
1791 * @hide
1792 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001793 static final int LAYOUT_DIRECTION_RESOLVED = 8 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001794
1795 /**
1796 * Mask for use with private flags indicating bits used for resolved horizontal layout direction.
1797 * @hide
1798 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001799 static final int LAYOUT_DIRECTION_RESOLVED_MASK = 0x0000000C << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001800
1801 /*
1802 * Array of horizontal layout direction flags for mapping attribute "layoutDirection" to correct
1803 * flag value.
1804 * @hide
1805 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001806 private static final int[] LAYOUT_DIRECTION_FLAGS = {
1807 LAYOUT_DIRECTION_LTR,
1808 LAYOUT_DIRECTION_RTL,
1809 LAYOUT_DIRECTION_INHERIT,
1810 LAYOUT_DIRECTION_LOCALE
1811 };
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001812
1813 /**
1814 * Default horizontal layout direction.
1815 * @hide
1816 */
1817 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001818
Adam Powell539ee872012-02-03 19:00:49 -08001819 /**
1820 * Indicates that the view is tracking some sort of transient state
1821 * that the app should not need to be aware of, but that the framework
1822 * should take special care to preserve.
1823 *
1824 * @hide
1825 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001826 static final int HAS_TRANSIENT_STATE = 0x00000100;
Adam Powell539ee872012-02-03 19:00:49 -08001827
1828
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001829 /**
1830 * Text direction is inherited thru {@link ViewGroup}
1831 */
1832 public static final int TEXT_DIRECTION_INHERIT = 0;
1833
1834 /**
1835 * Text direction is using "first strong algorithm". The first strong directional character
1836 * determines the paragraph direction. If there is no strong directional character, the
1837 * paragraph direction is the view's resolved layout direction.
1838 */
1839 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
1840
1841 /**
1842 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
1843 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
1844 * If there are neither, the paragraph direction is the view's resolved layout direction.
1845 */
1846 public static final int TEXT_DIRECTION_ANY_RTL = 2;
1847
1848 /**
1849 * Text direction is forced to LTR.
1850 */
1851 public static final int TEXT_DIRECTION_LTR = 3;
1852
1853 /**
1854 * Text direction is forced to RTL.
1855 */
1856 public static final int TEXT_DIRECTION_RTL = 4;
1857
1858 /**
1859 * Text direction is coming from the system Locale.
1860 */
1861 public static final int TEXT_DIRECTION_LOCALE = 5;
1862
1863 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001864 * Default text direction is inherited
1865 */
1866 protected static int TEXT_DIRECTION_DEFAULT = TEXT_DIRECTION_INHERIT;
1867
1868 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001869 * Bit shift to get the horizontal layout direction. (bits after LAYOUT_DIRECTION_RESOLVED)
1870 * @hide
1871 */
1872 static final int TEXT_DIRECTION_MASK_SHIFT = 6;
1873
1874 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001875 * Mask for use with private flags indicating bits used for text direction.
1876 * @hide
1877 */
1878 static final int TEXT_DIRECTION_MASK = 0x00000007 << TEXT_DIRECTION_MASK_SHIFT;
1879
1880 /**
1881 * Array of text direction flags for mapping attribute "textDirection" to correct
1882 * flag value.
1883 * @hide
1884 */
1885 private static final int[] TEXT_DIRECTION_FLAGS = {
1886 TEXT_DIRECTION_INHERIT << TEXT_DIRECTION_MASK_SHIFT,
1887 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_MASK_SHIFT,
1888 TEXT_DIRECTION_ANY_RTL << TEXT_DIRECTION_MASK_SHIFT,
1889 TEXT_DIRECTION_LTR << TEXT_DIRECTION_MASK_SHIFT,
1890 TEXT_DIRECTION_RTL << TEXT_DIRECTION_MASK_SHIFT,
1891 TEXT_DIRECTION_LOCALE << TEXT_DIRECTION_MASK_SHIFT
1892 };
1893
1894 /**
1895 * Indicates whether the view text direction has been resolved.
1896 * @hide
1897 */
1898 static final int TEXT_DIRECTION_RESOLVED = 0x00000008 << TEXT_DIRECTION_MASK_SHIFT;
1899
1900 /**
1901 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1902 * @hide
1903 */
1904 static final int TEXT_DIRECTION_RESOLVED_MASK_SHIFT = 10;
1905
1906 /**
1907 * Mask for use with private flags indicating bits used for resolved text direction.
1908 * @hide
1909 */
1910 static final int TEXT_DIRECTION_RESOLVED_MASK = 0x00000007 << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1911
1912 /**
1913 * Indicates whether the view text direction has been resolved to the "first strong" heuristic.
1914 * @hide
1915 */
1916 static final int TEXT_DIRECTION_RESOLVED_DEFAULT =
1917 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1918
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001919 /*
1920 * Default text alignment. The text alignment of this View is inherited from its parent.
1921 * Use with {@link #setTextAlignment(int)}
1922 */
1923 public static final int TEXT_ALIGNMENT_INHERIT = 0;
1924
1925 /**
1926 * Default for the root view. The gravity determines the text alignment, ALIGN_NORMAL,
1927 * ALIGN_CENTER, or ALIGN_OPPOSITE, which are relative to each paragraph’s text direction.
1928 *
1929 * Use with {@link #setTextAlignment(int)}
1930 */
1931 public static final int TEXT_ALIGNMENT_GRAVITY = 1;
1932
1933 /**
1934 * Align to the start of the paragraph, e.g. ALIGN_NORMAL.
1935 *
1936 * Use with {@link #setTextAlignment(int)}
1937 */
1938 public static final int TEXT_ALIGNMENT_TEXT_START = 2;
1939
1940 /**
1941 * Align to the end of the paragraph, e.g. ALIGN_OPPOSITE.
1942 *
1943 * Use with {@link #setTextAlignment(int)}
1944 */
1945 public static final int TEXT_ALIGNMENT_TEXT_END = 3;
1946
1947 /**
1948 * Center the paragraph, e.g. ALIGN_CENTER.
1949 *
1950 * Use with {@link #setTextAlignment(int)}
1951 */
1952 public static final int TEXT_ALIGNMENT_CENTER = 4;
1953
1954 /**
1955 * Align to the start of the view, which is ALIGN_LEFT if the view’s resolved
1956 * layoutDirection is LTR, and ALIGN_RIGHT otherwise.
1957 *
1958 * Use with {@link #setTextAlignment(int)}
1959 */
1960 public static final int TEXT_ALIGNMENT_VIEW_START = 5;
1961
1962 /**
1963 * Align to the end of the view, which is ALIGN_RIGHT if the view’s resolved
1964 * layoutDirection is LTR, and ALIGN_LEFT otherwise.
1965 *
1966 * Use with {@link #setTextAlignment(int)}
1967 */
1968 public static final int TEXT_ALIGNMENT_VIEW_END = 6;
1969
1970 /**
1971 * Default text alignment is inherited
1972 */
1973 protected static int TEXT_ALIGNMENT_DEFAULT = TEXT_ALIGNMENT_GRAVITY;
1974
1975 /**
1976 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1977 * @hide
1978 */
1979 static final int TEXT_ALIGNMENT_MASK_SHIFT = 13;
1980
1981 /**
1982 * Mask for use with private flags indicating bits used for text alignment.
1983 * @hide
1984 */
1985 static final int TEXT_ALIGNMENT_MASK = 0x00000007 << TEXT_ALIGNMENT_MASK_SHIFT;
1986
1987 /**
1988 * Array of text direction flags for mapping attribute "textAlignment" to correct
1989 * flag value.
1990 * @hide
1991 */
1992 private static final int[] TEXT_ALIGNMENT_FLAGS = {
1993 TEXT_ALIGNMENT_INHERIT << TEXT_ALIGNMENT_MASK_SHIFT,
1994 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_MASK_SHIFT,
1995 TEXT_ALIGNMENT_TEXT_START << TEXT_ALIGNMENT_MASK_SHIFT,
1996 TEXT_ALIGNMENT_TEXT_END << TEXT_ALIGNMENT_MASK_SHIFT,
1997 TEXT_ALIGNMENT_CENTER << TEXT_ALIGNMENT_MASK_SHIFT,
1998 TEXT_ALIGNMENT_VIEW_START << TEXT_ALIGNMENT_MASK_SHIFT,
1999 TEXT_ALIGNMENT_VIEW_END << TEXT_ALIGNMENT_MASK_SHIFT
2000 };
2001
2002 /**
2003 * Indicates whether the view text alignment has been resolved.
2004 * @hide
2005 */
2006 static final int TEXT_ALIGNMENT_RESOLVED = 0x00000008 << TEXT_ALIGNMENT_MASK_SHIFT;
2007
2008 /**
2009 * Bit shift to get the resolved text alignment.
2010 * @hide
2011 */
2012 static final int TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT = 17;
2013
2014 /**
2015 * Mask for use with private flags indicating bits used for text alignment.
2016 * @hide
2017 */
2018 static final int TEXT_ALIGNMENT_RESOLVED_MASK = 0x00000007 << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
2019
2020 /**
2021 * Indicates whether if the view text alignment has been resolved to gravity
2022 */
2023 public static final int TEXT_ALIGNMENT_RESOLVED_DEFAULT =
2024 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
2025
Svetoslav Ganov42138042012-03-20 11:51:39 -07002026 // Accessiblity constants for mPrivateFlags2
2027
2028 /**
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07002029 * Shift for the bits in {@link #mPrivateFlags2} related to the
2030 * "importantForAccessibility" attribute.
Svetoslav Ganov42138042012-03-20 11:51:39 -07002031 */
2032 static final int IMPORTANT_FOR_ACCESSIBILITY_SHIFT = 20;
2033
2034 /**
2035 * Automatically determine whether a view is important for accessibility.
2036 */
2037 public static final int IMPORTANT_FOR_ACCESSIBILITY_AUTO = 0x00000000;
2038
2039 /**
2040 * The view is important for accessibility.
2041 */
2042 public static final int IMPORTANT_FOR_ACCESSIBILITY_YES = 0x00000001;
2043
2044 /**
2045 * The view is not important for accessibility.
2046 */
2047 public static final int IMPORTANT_FOR_ACCESSIBILITY_NO = 0x00000002;
2048
2049 /**
2050 * The default whether the view is important for accessiblity.
2051 */
2052 static final int IMPORTANT_FOR_ACCESSIBILITY_DEFAULT = IMPORTANT_FOR_ACCESSIBILITY_AUTO;
2053
2054 /**
2055 * Mask for obtainig the bits which specify how to determine
2056 * whether a view is important for accessibility.
2057 */
2058 static final int IMPORTANT_FOR_ACCESSIBILITY_MASK = (IMPORTANT_FOR_ACCESSIBILITY_AUTO
2059 | IMPORTANT_FOR_ACCESSIBILITY_YES | IMPORTANT_FOR_ACCESSIBILITY_NO)
2060 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
2061
2062 /**
2063 * Flag indicating whether a view has accessibility focus.
2064 */
2065 static final int ACCESSIBILITY_FOCUSED = 0x00000040 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
2066
2067 /**
2068 * Flag indicating whether a view state for accessibility has changed.
2069 */
2070 static final int ACCESSIBILITY_STATE_CHANGED = 0x00000080 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07002071
Chet Haaseafd5c3e2012-05-10 13:21:10 -07002072 /**
Chet Haase1a3ab172012-05-11 08:41:20 -07002073 * Flag indicating whether a view failed the quickReject() check in draw(). This condition
2074 * is used to check whether later changes to the view's transform should invalidate the
2075 * view to force the quickReject test to run again.
2076 */
Chet Haase21433372012-06-05 07:54:09 -07002077 static final int VIEW_QUICK_REJECTED = 0x10000000;
Chet Haase1a3ab172012-05-11 08:41:20 -07002078
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07002079 // There are a couple of flags left in mPrivateFlags2
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07002080
Christopher Tate3d4bf172011-03-28 16:16:46 -07002081 /* End of masks for mPrivateFlags2 */
2082
Chet Haase21433372012-06-05 07:54:09 -07002083 /* Masks for mPrivateFlags3 */
2084
2085 /**
2086 * Flag indicating that view has a transform animation set on it. This is used to track whether
2087 * an animation is cleared between successive frames, in order to tell the associated
2088 * DisplayList to clear its animation matrix.
2089 */
2090 static final int VIEW_IS_ANIMATING_TRANSFORM = 0x1;
2091
2092 /**
2093 * Flag indicating that view has an alpha animation set on it. This is used to track whether an
2094 * animation is cleared between successive frames, in order to tell the associated
2095 * DisplayList to restore its alpha value.
2096 */
2097 static final int VIEW_IS_ANIMATING_ALPHA = 0x2;
2098
2099
2100 /* End of masks for mPrivateFlags3 */
2101
Christopher Tate3d4bf172011-03-28 16:16:46 -07002102 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
2103
Chet Haasedaf98e92011-01-10 14:10:36 -08002104 /**
Adam Powell637d3372010-08-25 14:37:03 -07002105 * Always allow a user to over-scroll this view, provided it is a
2106 * view that can scroll.
2107 *
2108 * @see #getOverScrollMode()
2109 * @see #setOverScrollMode(int)
2110 */
2111 public static final int OVER_SCROLL_ALWAYS = 0;
2112
2113 /**
2114 * Allow a user to over-scroll this view only if the content is large
2115 * enough to meaningfully scroll, provided it is a view that can scroll.
2116 *
2117 * @see #getOverScrollMode()
2118 * @see #setOverScrollMode(int)
2119 */
2120 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
2121
2122 /**
2123 * Never allow a user to over-scroll this view.
2124 *
2125 * @see #getOverScrollMode()
2126 * @see #setOverScrollMode(int)
2127 */
2128 public static final int OVER_SCROLL_NEVER = 2;
2129
2130 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002131 * Special constant for {@link #setSystemUiVisibility(int)}: View has
2132 * requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08002133 *
Joe Malin32736f02011-01-19 16:14:20 -08002134 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002135 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002136 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08002137
2138 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002139 * Flag for {@link #setSystemUiVisibility(int)}: View has requested the
2140 * system UI to enter an unobtrusive "low profile" mode.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002141 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002142 * <p>This is for use in games, book readers, video players, or any other
Philip Milne6c8ea062012-04-03 17:38:43 -07002143 * "immersive" application where the usual system chrome is deemed too distracting.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002144 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002145 * <p>In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08002146 *
Joe Malin32736f02011-01-19 16:14:20 -08002147 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002148 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002149 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
2150
2151 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002152 * Flag for {@link #setSystemUiVisibility(int)}: View has requested that the
2153 * system navigation be temporarily hidden.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002154 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002155 * <p>This is an even less obtrusive state than that called for by
Daniel Sandler60ee2562011-07-22 12:34:33 -04002156 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
2157 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
2158 * those to disappear. This is useful (in conjunction with the
Philip Milne6c8ea062012-04-03 17:38:43 -07002159 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
Daniel Sandler60ee2562011-07-22 12:34:33 -04002160 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
2161 * window flags) for displaying content using every last pixel on the display.
2162 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002163 * <p>There is a limitation: because navigation controls are so important, the least user
2164 * interaction will cause them to reappear immediately. When this happens, both
2165 * this flag and {@link #SYSTEM_UI_FLAG_FULLSCREEN} will be cleared automatically,
2166 * so that both elements reappear at the same time.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002167 *
2168 * @see #setSystemUiVisibility(int)
2169 */
2170 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
2171
2172 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002173 * Flag for {@link #setSystemUiVisibility(int)}: View has requested to go
2174 * into the normal fullscreen mode so that its content can take over the screen
2175 * while still allowing the user to interact with the application.
2176 *
2177 * <p>This has the same visual effect as
2178 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN
2179 * WindowManager.LayoutParams.FLAG_FULLSCREEN},
2180 * meaning that non-critical screen decorations (such as the status bar) will be
2181 * hidden while the user is in the View's window, focusing the experience on
2182 * that content. Unlike the window flag, if you are using ActionBar in
2183 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2184 * Window.FEATURE_ACTION_BAR_OVERLAY}, then enabling this flag will also
2185 * hide the action bar.
2186 *
2187 * <p>This approach to going fullscreen is best used over the window flag when
2188 * it is a transient state -- that is, the application does this at certain
2189 * points in its user interaction where it wants to allow the user to focus
2190 * on content, but not as a continuous state. For situations where the application
2191 * would like to simply stay full screen the entire time (such as a game that
2192 * wants to take over the screen), the
2193 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN window flag}
2194 * is usually a better approach. The state set here will be removed by the system
2195 * in various situations (such as the user moving to another application) like
2196 * the other system UI states.
2197 *
2198 * <p>When using this flag, the application should provide some easy facility
2199 * for the user to go out of it. A common example would be in an e-book
2200 * reader, where tapping on the screen brings back whatever screen and UI
2201 * decorations that had been hidden while the user was immersed in reading
2202 * the book.
2203 *
2204 * @see #setSystemUiVisibility(int)
2205 */
2206 public static final int SYSTEM_UI_FLAG_FULLSCREEN = 0x00000004;
2207
2208 /**
2209 * Flag for {@link #setSystemUiVisibility(int)}: When using other layout
2210 * flags, we would like a stable view of the content insets given to
2211 * {@link #fitSystemWindows(Rect)}. This means that the insets seen there
2212 * will always represent the worst case that the application can expect
Dianne Hackborn5b5cc4d2012-05-16 13:15:00 -07002213 * as a continuous state. In the stock Android UI this is the space for
2214 * the system bar, nav bar, and status bar, but not more transient elements
2215 * such as an input method.
2216 *
2217 * The stable layout your UI sees is based on the system UI modes you can
2218 * switch to. That is, if you specify {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
2219 * then you will get a stable layout for changes of the
2220 * {@link #SYSTEM_UI_FLAG_FULLSCREEN} mode; if you specify
2221 * {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN} and
2222 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION}, then you can transition
2223 * to {@link #SYSTEM_UI_FLAG_FULLSCREEN} and {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}
2224 * with a stable layout. (Note that you should avoid using
2225 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} by itself.)
2226 *
2227 * If you have set the window flag {@ WindowManager.LayoutParams#FLAG_FULLSCREEN}
2228 * to hide the status bar (instead of using {@link #SYSTEM_UI_FLAG_FULLSCREEN}),
2229 * then a hidden status bar will be considered a "stable" state for purposes
2230 * here. This allows your UI to continually hide the status bar, while still
2231 * using the system UI flags to hide the action bar while still retaining
2232 * a stable layout. Note that changing the window fullscreen flag will never
2233 * provide a stable layout for a clean transition.
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002234 *
2235 * <p>If you are using ActionBar in
2236 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2237 * Window.FEATURE_ACTION_BAR_OVERLAY}, this flag will also impact the
2238 * insets it adds to those given to the application.
2239 */
2240 public static final int SYSTEM_UI_FLAG_LAYOUT_STABLE = 0x00000100;
2241
2242 /**
2243 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2244 * to be layed out as if it has requested
2245 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, even if it currently hasn't. This
2246 * allows it to avoid artifacts when switching in and out of that mode, at
2247 * the expense that some of its user interface may be covered by screen
2248 * decorations when they are shown. You can perform layout of your inner
2249 * UI elements to account for the navagation system UI through the
2250 * {@link #fitSystemWindows(Rect)} method.
2251 */
2252 public static final int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 0x00000200;
2253
2254 /**
2255 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2256 * to be layed out as if it has requested
2257 * {@link #SYSTEM_UI_FLAG_FULLSCREEN}, even if it currently hasn't. This
2258 * allows it to avoid artifacts when switching in and out of that mode, at
2259 * the expense that some of its user interface may be covered by screen
2260 * decorations when they are shown. You can perform layout of your inner
2261 * UI elements to account for non-fullscreen system UI through the
2262 * {@link #fitSystemWindows(Rect)} method.
2263 */
2264 public static final int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 0x00000400;
2265
2266 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04002267 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
2268 */
2269 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
2270
2271 /**
2272 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
2273 */
2274 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08002275
2276 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002277 * @hide
2278 *
2279 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2280 * out of the public fields to keep the undefined bits out of the developer's way.
2281 *
2282 * Flag to make the status bar not expandable. Unless you also
2283 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
2284 */
2285 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
2286
2287 /**
2288 * @hide
2289 *
2290 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2291 * out of the public fields to keep the undefined bits out of the developer's way.
2292 *
2293 * Flag to hide notification icons and scrolling ticker text.
2294 */
2295 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
2296
2297 /**
2298 * @hide
2299 *
2300 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2301 * out of the public fields to keep the undefined bits out of the developer's way.
2302 *
2303 * Flag to disable incoming notification alerts. This will not block
2304 * icons, but it will block sound, vibrating and other visual or aural notifications.
2305 */
2306 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
2307
2308 /**
2309 * @hide
2310 *
2311 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2312 * out of the public fields to keep the undefined bits out of the developer's way.
2313 *
2314 * Flag to hide only the scrolling ticker. Note that
2315 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
2316 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
2317 */
2318 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
2319
2320 /**
2321 * @hide
2322 *
2323 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2324 * out of the public fields to keep the undefined bits out of the developer's way.
2325 *
2326 * Flag to hide the center system info area.
2327 */
2328 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
2329
2330 /**
2331 * @hide
2332 *
2333 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2334 * out of the public fields to keep the undefined bits out of the developer's way.
2335 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002336 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002337 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2338 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04002339 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002340
2341 /**
2342 * @hide
2343 *
2344 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2345 * out of the public fields to keep the undefined bits out of the developer's way.
2346 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002347 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08002348 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2349 */
2350 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
2351
2352 /**
2353 * @hide
2354 *
2355 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2356 * out of the public fields to keep the undefined bits out of the developer's way.
2357 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002358 * Flag to hide only the clock. You might use this if your activity has
2359 * its own clock making the status bar's clock redundant.
2360 */
Joe Onorato6478adc2011-01-27 21:15:01 -08002361 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
2362
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002363 /**
2364 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04002365 *
2366 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2367 * out of the public fields to keep the undefined bits out of the developer's way.
2368 *
2369 * Flag to hide only the recent apps button. Don't use this
2370 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2371 */
2372 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
2373
2374 /**
2375 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002376 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002377 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002378
2379 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002380 * These are the system UI flags that can be cleared by events outside
2381 * of an application. Currently this is just the ability to tap on the
2382 * screen while hiding the navigation bar to have it return.
2383 * @hide
2384 */
2385 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002386 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION
2387 | SYSTEM_UI_FLAG_FULLSCREEN;
2388
2389 /**
2390 * Flags that can impact the layout in relation to system UI.
2391 */
2392 public static final int SYSTEM_UI_LAYOUT_FLAGS =
2393 SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
2394 | SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002395
2396 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07002397 * Find views that render the specified text.
2398 *
2399 * @see #findViewsWithText(ArrayList, CharSequence, int)
2400 */
2401 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
2402
2403 /**
2404 * Find find views that contain the specified content description.
2405 *
2406 * @see #findViewsWithText(ArrayList, CharSequence, int)
2407 */
2408 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
2409
2410 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07002411 * Find views that contain {@link AccessibilityNodeProvider}. Such
2412 * a View is a root of virtual view hierarchy and may contain the searched
2413 * text. If this flag is set Views with providers are automatically
2414 * added and it is a responsibility of the client to call the APIs of
2415 * the provider to determine whether the virtual tree rooted at this View
2416 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
2417 * represeting the virtual views with this text.
2418 *
2419 * @see #findViewsWithText(ArrayList, CharSequence, int)
2420 *
2421 * @hide
2422 */
2423 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
2424
2425 /**
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07002426 * The undefined cursor position.
2427 */
2428 private static final int ACCESSIBILITY_CURSOR_POSITION_UNDEFINED = -1;
2429
2430 /**
Romain Guybb9908b2012-03-08 11:14:07 -08002431 * Indicates that the screen has changed state and is now off.
2432 *
2433 * @see #onScreenStateChanged(int)
2434 */
2435 public static final int SCREEN_STATE_OFF = 0x0;
2436
2437 /**
2438 * Indicates that the screen has changed state and is now on.
2439 *
Romain Guy1e3d3132012-03-08 15:55:56 -08002440 * @see #onScreenStateChanged(int)
Romain Guybb9908b2012-03-08 11:14:07 -08002441 */
2442 public static final int SCREEN_STATE_ON = 0x1;
2443
2444 /**
Adam Powell637d3372010-08-25 14:37:03 -07002445 * Controls the over-scroll mode for this view.
2446 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2447 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2448 * and {@link #OVER_SCROLL_NEVER}.
2449 */
2450 private int mOverScrollMode;
2451
2452 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002453 * The parent this view is attached to.
2454 * {@hide}
2455 *
2456 * @see #getParent()
2457 */
2458 protected ViewParent mParent;
2459
2460 /**
2461 * {@hide}
2462 */
2463 AttachInfo mAttachInfo;
2464
2465 /**
2466 * {@hide}
2467 */
Romain Guy809a7f62009-05-14 15:44:42 -07002468 @ViewDebug.ExportedProperty(flagMapping = {
2469 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
2470 name = "FORCE_LAYOUT"),
2471 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
2472 name = "LAYOUT_REQUIRED"),
2473 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002474 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07002475 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
2476 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
2477 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2478 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
2479 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002480 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002481 int mPrivateFlags2;
Chet Haase21433372012-06-05 07:54:09 -07002482 int mPrivateFlags3;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002483
2484 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002485 * This view's request for the visibility of the status bar.
2486 * @hide
2487 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002488 @ViewDebug.ExportedProperty(flagMapping = {
2489 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2490 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2491 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2492 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2493 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2494 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2495 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2496 equals = SYSTEM_UI_FLAG_VISIBLE,
2497 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2498 })
Joe Onorato664644d2011-01-23 17:53:23 -08002499 int mSystemUiVisibility;
2500
2501 /**
Chet Haase563d4f22012-04-18 16:20:08 -07002502 * Reference count for transient state.
2503 * @see #setHasTransientState(boolean)
2504 */
2505 int mTransientStateCount = 0;
2506
2507 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002508 * Count of how many windows this view has been attached to.
2509 */
2510 int mWindowAttachCount;
2511
2512 /**
2513 * The layout parameters associated with this view and used by the parent
2514 * {@link android.view.ViewGroup} to determine how this view should be
2515 * laid out.
2516 * {@hide}
2517 */
2518 protected ViewGroup.LayoutParams mLayoutParams;
2519
2520 /**
2521 * The view flags hold various views states.
2522 * {@hide}
2523 */
2524 @ViewDebug.ExportedProperty
2525 int mViewFlags;
2526
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002527 static class TransformationInfo {
2528 /**
2529 * The transform matrix for the View. This transform is calculated internally
2530 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2531 * is used by default. Do *not* use this variable directly; instead call
2532 * getMatrix(), which will automatically recalculate the matrix if necessary
2533 * to get the correct matrix based on the latest rotation and scale properties.
2534 */
2535 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002536
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002537 /**
2538 * The transform matrix for the View. This transform is calculated internally
2539 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2540 * is used by default. Do *not* use this variable directly; instead call
2541 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2542 * to get the correct matrix based on the latest rotation and scale properties.
2543 */
2544 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002545
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002546 /**
2547 * An internal variable that tracks whether we need to recalculate the
2548 * transform matrix, based on whether the rotation or scaleX/Y properties
2549 * have changed since the matrix was last calculated.
2550 */
2551 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002552
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002553 /**
2554 * An internal variable that tracks whether we need to recalculate the
2555 * transform matrix, based on whether the rotation or scaleX/Y properties
2556 * have changed since the matrix was last calculated.
2557 */
2558 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002559
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002560 /**
2561 * A variable that tracks whether we need to recalculate the
2562 * transform matrix, based on whether the rotation or scaleX/Y properties
2563 * have changed since the matrix was last calculated. This variable
2564 * is only valid after a call to updateMatrix() or to a function that
2565 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2566 */
2567 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002568
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002569 /**
2570 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2571 */
2572 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002573
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002574 /**
2575 * This matrix is used when computing the matrix for 3D rotations.
2576 */
2577 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002578
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002579 /**
2580 * These prev values are used to recalculate a centered pivot point when necessary. The
2581 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2582 * set), so thes values are only used then as well.
2583 */
2584 private int mPrevWidth = -1;
2585 private int mPrevHeight = -1;
Philip Milne6c8ea062012-04-03 17:38:43 -07002586
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002587 /**
2588 * The degrees rotation around the vertical axis through the pivot point.
2589 */
2590 @ViewDebug.ExportedProperty
2591 float mRotationY = 0f;
2592
2593 /**
2594 * The degrees rotation around the horizontal axis through the pivot point.
2595 */
2596 @ViewDebug.ExportedProperty
2597 float mRotationX = 0f;
2598
2599 /**
2600 * The degrees rotation around the pivot point.
2601 */
2602 @ViewDebug.ExportedProperty
2603 float mRotation = 0f;
2604
2605 /**
2606 * The amount of translation of the object away from its left property (post-layout).
2607 */
2608 @ViewDebug.ExportedProperty
2609 float mTranslationX = 0f;
2610
2611 /**
2612 * The amount of translation of the object away from its top property (post-layout).
2613 */
2614 @ViewDebug.ExportedProperty
2615 float mTranslationY = 0f;
2616
2617 /**
2618 * The amount of scale in the x direction around the pivot point. A
2619 * value of 1 means no scaling is applied.
2620 */
2621 @ViewDebug.ExportedProperty
2622 float mScaleX = 1f;
2623
2624 /**
2625 * The amount of scale in the y direction around the pivot point. A
2626 * value of 1 means no scaling is applied.
2627 */
2628 @ViewDebug.ExportedProperty
2629 float mScaleY = 1f;
2630
2631 /**
Chet Haasea33de552012-02-03 16:28:24 -08002632 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002633 */
2634 @ViewDebug.ExportedProperty
2635 float mPivotX = 0f;
2636
2637 /**
Chet Haasea33de552012-02-03 16:28:24 -08002638 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002639 */
2640 @ViewDebug.ExportedProperty
2641 float mPivotY = 0f;
2642
2643 /**
2644 * The opacity of the View. This is a value from 0 to 1, where 0 means
2645 * completely transparent and 1 means completely opaque.
2646 */
2647 @ViewDebug.ExportedProperty
2648 float mAlpha = 1f;
2649 }
2650
2651 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002652
Joe Malin32736f02011-01-19 16:14:20 -08002653 private boolean mLastIsOpaque;
2654
Chet Haasefd2b0022010-08-06 13:08:56 -07002655 /**
2656 * Convenience value to check for float values that are close enough to zero to be considered
2657 * zero.
2658 */
Romain Guy2542d192010-08-18 11:47:12 -07002659 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002660
2661 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002662 * The distance in pixels from the left edge of this view's parent
2663 * to the left edge of this view.
2664 * {@hide}
2665 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002666 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002667 protected int mLeft;
2668 /**
2669 * The distance in pixels from the left edge of this view's parent
2670 * to the right edge of this view.
2671 * {@hide}
2672 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002673 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002674 protected int mRight;
2675 /**
2676 * The distance in pixels from the top edge of this view's parent
2677 * to the top edge of this view.
2678 * {@hide}
2679 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002680 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002681 protected int mTop;
2682 /**
2683 * The distance in pixels from the top edge of this view's parent
2684 * to the bottom edge of this view.
2685 * {@hide}
2686 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002687 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002688 protected int mBottom;
2689
2690 /**
2691 * The offset, in pixels, by which the content of this view is scrolled
2692 * horizontally.
2693 * {@hide}
2694 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002695 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002696 protected int mScrollX;
2697 /**
2698 * The offset, in pixels, by which the content of this view is scrolled
2699 * vertically.
2700 * {@hide}
2701 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002702 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002703 protected int mScrollY;
2704
2705 /**
2706 * The left padding in pixels, that is the distance in pixels between the
2707 * left edge of this view and the left edge of its content.
2708 * {@hide}
2709 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002710 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002711 protected int mPaddingLeft;
2712 /**
2713 * The right padding in pixels, that is the distance in pixels between the
2714 * right edge of this view and the right edge of its content.
2715 * {@hide}
2716 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002717 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002718 protected int mPaddingRight;
2719 /**
2720 * The top padding in pixels, that is the distance in pixels between the
2721 * top edge of this view and the top edge of its content.
2722 * {@hide}
2723 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002724 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002725 protected int mPaddingTop;
2726 /**
2727 * The bottom padding in pixels, that is the distance in pixels between the
2728 * bottom edge of this view and the bottom edge of its content.
2729 * {@hide}
2730 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002731 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002732 protected int mPaddingBottom;
2733
2734 /**
Philip Milne1557fd72012-04-04 23:41:34 -07002735 * The layout insets in pixels, that is the distance in pixels between the
2736 * visible edges of this view its bounds.
2737 */
2738 private Insets mLayoutInsets;
2739
2740 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002741 * Briefly describes the view and is primarily used for accessibility support.
2742 */
2743 private CharSequence mContentDescription;
2744
2745 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002746 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002747 *
2748 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002749 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002750 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002751 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002752
2753 /**
2754 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002755 *
2756 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002757 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002758 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002759 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002760
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002761 /**
Adam Powell20232d02010-12-08 21:08:53 -08002762 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002763 *
2764 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002765 */
2766 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002767 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002768
2769 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002770 * Cache the paddingStart set by the user to append to the scrollbar's size.
2771 *
2772 */
2773 @ViewDebug.ExportedProperty(category = "padding")
2774 int mUserPaddingStart;
2775
2776 /**
2777 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2778 *
2779 */
2780 @ViewDebug.ExportedProperty(category = "padding")
2781 int mUserPaddingEnd;
2782
2783 /**
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07002784 * Default undefined padding
2785 */
2786 private static final int UNDEFINED_PADDING = Integer.MIN_VALUE;
2787
2788 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002789 * @hide
2790 */
2791 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2792 /**
2793 * @hide
2794 */
2795 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002796
Philip Milne6c8ea062012-04-03 17:38:43 -07002797 private Drawable mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002798
2799 private int mBackgroundResource;
2800 private boolean mBackgroundSizeChanged;
2801
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002802 static class ListenerInfo {
2803 /**
2804 * Listener used to dispatch focus change events.
2805 * This field should be made private, so it is hidden from the SDK.
2806 * {@hide}
2807 */
2808 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002809
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002810 /**
2811 * Listeners for layout change events.
2812 */
2813 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002814
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002815 /**
2816 * Listeners for attach events.
2817 */
2818 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002819
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002820 /**
2821 * Listener used to dispatch click events.
2822 * This field should be made private, so it is hidden from the SDK.
2823 * {@hide}
2824 */
2825 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002826
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002827 /**
2828 * Listener used to dispatch long click events.
2829 * This field should be made private, so it is hidden from the SDK.
2830 * {@hide}
2831 */
2832 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002833
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002834 /**
2835 * Listener used to build the context menu.
2836 * This field should be made private, so it is hidden from the SDK.
2837 * {@hide}
2838 */
2839 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002840
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002841 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002842
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002843 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002844
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002845 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002846
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002847 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002848
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002849 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002850
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002851 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2852 }
2853
2854 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002855
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002856 /**
2857 * The application environment this view lives in.
2858 * This field should be made private, so it is hidden from the SDK.
2859 * {@hide}
2860 */
2861 protected Context mContext;
2862
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002863 private final Resources mResources;
2864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002865 private ScrollabilityCache mScrollCache;
2866
2867 private int[] mDrawableState = null;
2868
Romain Guy0211a0a2011-02-14 16:34:59 -08002869 /**
2870 * Set to true when drawing cache is enabled and cannot be created.
Philip Milne6c8ea062012-04-03 17:38:43 -07002871 *
Romain Guy0211a0a2011-02-14 16:34:59 -08002872 * @hide
2873 */
2874 public boolean mCachingFailed;
2875
Romain Guy02890fd2010-08-06 17:58:44 -07002876 private Bitmap mDrawingCache;
2877 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002878 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002879 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002880
2881 /**
2882 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2883 * the user may specify which view to go to next.
2884 */
2885 private int mNextFocusLeftId = View.NO_ID;
2886
2887 /**
2888 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2889 * the user may specify which view to go to next.
2890 */
2891 private int mNextFocusRightId = View.NO_ID;
2892
2893 /**
2894 * When this view has focus and the next focus is {@link #FOCUS_UP},
2895 * the user may specify which view to go to next.
2896 */
2897 private int mNextFocusUpId = View.NO_ID;
2898
2899 /**
2900 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2901 * the user may specify which view to go to next.
2902 */
2903 private int mNextFocusDownId = View.NO_ID;
2904
Jeff Brown4e6319b2010-12-13 10:36:51 -08002905 /**
2906 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2907 * the user may specify which view to go to next.
2908 */
2909 int mNextFocusForwardId = View.NO_ID;
2910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002911 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002912 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002913 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002914 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002916 private UnsetPressedState mUnsetPressedState;
2917
2918 /**
2919 * Whether the long press's action has been invoked. The tap's action is invoked on the
2920 * up event while a long press is invoked as soon as the long press duration is reached, so
2921 * a long press could be performed before the tap is checked, in which case the tap's action
2922 * should not be invoked.
2923 */
2924 private boolean mHasPerformedLongPress;
2925
2926 /**
2927 * The minimum height of the view. We'll try our best to have the height
2928 * of this view to at least this amount.
2929 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002930 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002931 private int mMinHeight;
2932
2933 /**
2934 * The minimum width of the view. We'll try our best to have the width
2935 * of this view to at least this amount.
2936 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002937 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002938 private int mMinWidth;
2939
2940 /**
2941 * The delegate to handle touch events that are physically in this view
2942 * but should be handled by another view.
2943 */
2944 private TouchDelegate mTouchDelegate = null;
2945
2946 /**
2947 * Solid color to use as a background when creating the drawing cache. Enables
2948 * the cache to use 16 bit bitmaps instead of 32 bit.
2949 */
2950 private int mDrawingCacheBackgroundColor = 0;
2951
2952 /**
2953 * Special tree observer used when mAttachInfo is null.
2954 */
2955 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002956
Adam Powelle14579b2009-12-16 18:39:52 -08002957 /**
2958 * Cache the touch slop from the context that created the view.
2959 */
2960 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002962 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002963 * Object that handles automatic animation of view properties.
2964 */
2965 private ViewPropertyAnimator mAnimator = null;
2966
2967 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002968 * Flag indicating that a drag can cross window boundaries. When
2969 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2970 * with this flag set, all visible applications will be able to participate
2971 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002972 *
2973 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002974 */
2975 public static final int DRAG_FLAG_GLOBAL = 1;
2976
2977 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002978 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2979 */
2980 private float mVerticalScrollFactor;
2981
2982 /**
Adam Powell20232d02010-12-08 21:08:53 -08002983 * Position of the vertical scroll bar.
2984 */
2985 private int mVerticalScrollbarPosition;
2986
2987 /**
2988 * Position the scroll bar at the default position as determined by the system.
2989 */
2990 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2991
2992 /**
2993 * Position the scroll bar along the left edge.
2994 */
2995 public static final int SCROLLBAR_POSITION_LEFT = 1;
2996
2997 /**
2998 * Position the scroll bar along the right edge.
2999 */
3000 public static final int SCROLLBAR_POSITION_RIGHT = 2;
3001
3002 /**
Romain Guy171c5922011-01-06 10:04:23 -08003003 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08003004 *
3005 * @see #getLayerType()
3006 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003007 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08003008 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003009 */
3010 public static final int LAYER_TYPE_NONE = 0;
3011
3012 /**
3013 * <p>Indicates that the view has a software layer. A software layer is backed
3014 * by a bitmap and causes the view to be rendered using Android's software
3015 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003016 *
Romain Guy171c5922011-01-06 10:04:23 -08003017 * <p>Software layers have various usages:</p>
3018 * <p>When the application is not using hardware acceleration, a software layer
3019 * is useful to apply a specific color filter and/or blending mode and/or
3020 * translucency to a view and all its children.</p>
3021 * <p>When the application is using hardware acceleration, a software layer
3022 * is useful to render drawing primitives not supported by the hardware
3023 * accelerated pipeline. It can also be used to cache a complex view tree
3024 * into a texture and reduce the complexity of drawing operations. For instance,
3025 * when animating a complex view tree with a translation, a software layer can
3026 * be used to render the view tree only once.</p>
3027 * <p>Software layers should be avoided when the affected view tree updates
3028 * often. Every update will require to re-render the software layer, which can
3029 * potentially be slow (particularly when hardware acceleration is turned on
3030 * since the layer will have to be uploaded into a hardware texture after every
3031 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08003032 *
3033 * @see #getLayerType()
3034 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003035 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08003036 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003037 */
3038 public static final int LAYER_TYPE_SOFTWARE = 1;
3039
3040 /**
3041 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
3042 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
3043 * OpenGL hardware) and causes the view to be rendered using Android's hardware
3044 * rendering pipeline, but only if hardware acceleration is turned on for the
3045 * view hierarchy. When hardware acceleration is turned off, hardware layers
3046 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003047 *
Romain Guy171c5922011-01-06 10:04:23 -08003048 * <p>A hardware layer is useful to apply a specific color filter and/or
3049 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08003050 * <p>A hardware layer can be used to cache a complex view tree into a
3051 * texture and reduce the complexity of drawing operations. For instance,
3052 * when animating a complex view tree with a translation, a hardware layer can
3053 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08003054 * <p>A hardware layer can also be used to increase the rendering quality when
3055 * rotation transformations are applied on a view. It can also be used to
3056 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003057 *
3058 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08003059 * @see #setLayerType(int, android.graphics.Paint)
3060 * @see #LAYER_TYPE_NONE
3061 * @see #LAYER_TYPE_SOFTWARE
3062 */
3063 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08003064
Romain Guy3aaff3a2011-01-12 14:18:47 -08003065 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
3066 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
3067 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
3068 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
3069 })
Romain Guy171c5922011-01-06 10:04:23 -08003070 int mLayerType = LAYER_TYPE_NONE;
3071 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08003072 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08003073
3074 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07003075 * Set to true when the view is sending hover accessibility events because it
3076 * is the innermost hovered view.
3077 */
3078 private boolean mSendingHoverAccessibilityEvents;
3079
Adam Powella9108a22012-07-18 11:18:09 -07003080 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
3081
Jeff Brown87b7f802011-06-21 18:35:45 -07003082 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003083 * Simple constructor to use when creating a view from code.
3084 *
3085 * @param context The Context the view is running in, through which it can
3086 * access the current theme, resources, etc.
3087 */
3088 public View(Context context) {
3089 mContext = context;
3090 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003091 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003092 // Set layout and text direction defaults
3093 mPrivateFlags2 = (LAYOUT_DIRECTION_DEFAULT << LAYOUT_DIRECTION_MASK_SHIFT) |
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003094 (TEXT_DIRECTION_DEFAULT << TEXT_DIRECTION_MASK_SHIFT) |
Svetoslav Ganov42138042012-03-20 11:51:39 -07003095 (TEXT_ALIGNMENT_DEFAULT << TEXT_ALIGNMENT_MASK_SHIFT) |
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07003096 (IMPORTANT_FOR_ACCESSIBILITY_DEFAULT << IMPORTANT_FOR_ACCESSIBILITY_SHIFT);
Adam Powelle14579b2009-12-16 18:39:52 -08003097 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07003098 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003099 mUserPaddingStart = UNDEFINED_PADDING;
3100 mUserPaddingEnd = UNDEFINED_PADDING;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003101 }
3102
3103 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07003104 * Delegate for injecting accessiblity functionality.
3105 */
3106 AccessibilityDelegate mAccessibilityDelegate;
3107
3108 /**
3109 * Consistency verifier for debugging purposes.
3110 * @hide
3111 */
3112 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
3113 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
3114 new InputEventConsistencyVerifier(this, 0) : null;
3115
3116 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003117 * Constructor that is called when inflating a view from XML. This is called
3118 * when a view is being constructed from an XML file, supplying attributes
3119 * that were specified in the XML file. This version uses a default style of
3120 * 0, so the only attribute values applied are those in the Context's Theme
3121 * and the given AttributeSet.
3122 *
3123 * <p>
3124 * The method onFinishInflate() will be called after all children have been
3125 * added.
3126 *
3127 * @param context The Context the view is running in, through which it can
3128 * access the current theme, resources, etc.
3129 * @param attrs The attributes of the XML tag that is inflating the view.
3130 * @see #View(Context, AttributeSet, int)
3131 */
3132 public View(Context context, AttributeSet attrs) {
3133 this(context, attrs, 0);
3134 }
3135
3136 /**
3137 * Perform inflation from XML and apply a class-specific base style. This
3138 * constructor of View allows subclasses to use their own base style when
3139 * they are inflating. For example, a Button class's constructor would call
3140 * this version of the super class constructor and supply
3141 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
3142 * the theme's button style to modify all of the base view attributes (in
3143 * particular its background) as well as the Button class's attributes.
3144 *
3145 * @param context The Context the view is running in, through which it can
3146 * access the current theme, resources, etc.
3147 * @param attrs The attributes of the XML tag that is inflating the view.
3148 * @param defStyle The default style to apply to this view. If 0, no style
3149 * will be applied (beyond what is included in the theme). This may
3150 * either be an attribute resource, whose value will be retrieved
3151 * from the current theme, or an explicit style resource.
3152 * @see #View(Context, AttributeSet)
3153 */
3154 public View(Context context, AttributeSet attrs, int defStyle) {
3155 this(context);
3156
3157 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
3158 defStyle, 0);
3159
3160 Drawable background = null;
3161
3162 int leftPadding = -1;
3163 int topPadding = -1;
3164 int rightPadding = -1;
3165 int bottomPadding = -1;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003166 int startPadding = UNDEFINED_PADDING;
3167 int endPadding = UNDEFINED_PADDING;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003168
3169 int padding = -1;
3170
3171 int viewFlagValues = 0;
3172 int viewFlagMasks = 0;
3173
3174 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07003175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003176 int x = 0;
3177 int y = 0;
3178
Chet Haase73066682010-11-29 15:55:32 -08003179 float tx = 0;
3180 float ty = 0;
3181 float rotation = 0;
3182 float rotationX = 0;
3183 float rotationY = 0;
3184 float sx = 1f;
3185 float sy = 1f;
3186 boolean transformSet = false;
3187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003188 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
3189
Adam Powell637d3372010-08-25 14:37:03 -07003190 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003191 final int N = a.getIndexCount();
3192 for (int i = 0; i < N; i++) {
3193 int attr = a.getIndex(i);
3194 switch (attr) {
3195 case com.android.internal.R.styleable.View_background:
3196 background = a.getDrawable(attr);
3197 break;
3198 case com.android.internal.R.styleable.View_padding:
3199 padding = a.getDimensionPixelSize(attr, -1);
3200 break;
3201 case com.android.internal.R.styleable.View_paddingLeft:
3202 leftPadding = a.getDimensionPixelSize(attr, -1);
3203 break;
3204 case com.android.internal.R.styleable.View_paddingTop:
3205 topPadding = a.getDimensionPixelSize(attr, -1);
3206 break;
3207 case com.android.internal.R.styleable.View_paddingRight:
3208 rightPadding = a.getDimensionPixelSize(attr, -1);
3209 break;
3210 case com.android.internal.R.styleable.View_paddingBottom:
3211 bottomPadding = a.getDimensionPixelSize(attr, -1);
3212 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003213 case com.android.internal.R.styleable.View_paddingStart:
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003214 startPadding = a.getDimensionPixelSize(attr, UNDEFINED_PADDING);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003215 break;
3216 case com.android.internal.R.styleable.View_paddingEnd:
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003217 endPadding = a.getDimensionPixelSize(attr, UNDEFINED_PADDING);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003218 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003219 case com.android.internal.R.styleable.View_scrollX:
3220 x = a.getDimensionPixelOffset(attr, 0);
3221 break;
3222 case com.android.internal.R.styleable.View_scrollY:
3223 y = a.getDimensionPixelOffset(attr, 0);
3224 break;
Chet Haase73066682010-11-29 15:55:32 -08003225 case com.android.internal.R.styleable.View_alpha:
3226 setAlpha(a.getFloat(attr, 1f));
3227 break;
3228 case com.android.internal.R.styleable.View_transformPivotX:
3229 setPivotX(a.getDimensionPixelOffset(attr, 0));
3230 break;
3231 case com.android.internal.R.styleable.View_transformPivotY:
3232 setPivotY(a.getDimensionPixelOffset(attr, 0));
3233 break;
3234 case com.android.internal.R.styleable.View_translationX:
3235 tx = a.getDimensionPixelOffset(attr, 0);
3236 transformSet = true;
3237 break;
3238 case com.android.internal.R.styleable.View_translationY:
3239 ty = a.getDimensionPixelOffset(attr, 0);
3240 transformSet = true;
3241 break;
3242 case com.android.internal.R.styleable.View_rotation:
3243 rotation = a.getFloat(attr, 0);
3244 transformSet = true;
3245 break;
3246 case com.android.internal.R.styleable.View_rotationX:
3247 rotationX = a.getFloat(attr, 0);
3248 transformSet = true;
3249 break;
3250 case com.android.internal.R.styleable.View_rotationY:
3251 rotationY = a.getFloat(attr, 0);
3252 transformSet = true;
3253 break;
3254 case com.android.internal.R.styleable.View_scaleX:
3255 sx = a.getFloat(attr, 1f);
3256 transformSet = true;
3257 break;
3258 case com.android.internal.R.styleable.View_scaleY:
3259 sy = a.getFloat(attr, 1f);
3260 transformSet = true;
3261 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003262 case com.android.internal.R.styleable.View_id:
3263 mID = a.getResourceId(attr, NO_ID);
3264 break;
3265 case com.android.internal.R.styleable.View_tag:
3266 mTag = a.getText(attr);
3267 break;
3268 case com.android.internal.R.styleable.View_fitsSystemWindows:
3269 if (a.getBoolean(attr, false)) {
3270 viewFlagValues |= FITS_SYSTEM_WINDOWS;
3271 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
3272 }
3273 break;
3274 case com.android.internal.R.styleable.View_focusable:
3275 if (a.getBoolean(attr, false)) {
3276 viewFlagValues |= FOCUSABLE;
3277 viewFlagMasks |= FOCUSABLE_MASK;
3278 }
3279 break;
3280 case com.android.internal.R.styleable.View_focusableInTouchMode:
3281 if (a.getBoolean(attr, false)) {
3282 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
3283 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
3284 }
3285 break;
3286 case com.android.internal.R.styleable.View_clickable:
3287 if (a.getBoolean(attr, false)) {
3288 viewFlagValues |= CLICKABLE;
3289 viewFlagMasks |= CLICKABLE;
3290 }
3291 break;
3292 case com.android.internal.R.styleable.View_longClickable:
3293 if (a.getBoolean(attr, false)) {
3294 viewFlagValues |= LONG_CLICKABLE;
3295 viewFlagMasks |= LONG_CLICKABLE;
3296 }
3297 break;
3298 case com.android.internal.R.styleable.View_saveEnabled:
3299 if (!a.getBoolean(attr, true)) {
3300 viewFlagValues |= SAVE_DISABLED;
3301 viewFlagMasks |= SAVE_DISABLED_MASK;
3302 }
3303 break;
3304 case com.android.internal.R.styleable.View_duplicateParentState:
3305 if (a.getBoolean(attr, false)) {
3306 viewFlagValues |= DUPLICATE_PARENT_STATE;
3307 viewFlagMasks |= DUPLICATE_PARENT_STATE;
3308 }
3309 break;
3310 case com.android.internal.R.styleable.View_visibility:
3311 final int visibility = a.getInt(attr, 0);
3312 if (visibility != 0) {
3313 viewFlagValues |= VISIBILITY_FLAGS[visibility];
3314 viewFlagMasks |= VISIBILITY_MASK;
3315 }
3316 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003317 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003318 // Clear any layout direction flags (included resolved bits) already set
3319 mPrivateFlags2 &= ~(LAYOUT_DIRECTION_MASK | LAYOUT_DIRECTION_RESOLVED_MASK);
3320 // Set the layout direction flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003321 final int layoutDirection = a.getInt(attr, -1);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003322 final int value = (layoutDirection != -1) ?
3323 LAYOUT_DIRECTION_FLAGS[layoutDirection] : LAYOUT_DIRECTION_DEFAULT;
3324 mPrivateFlags2 |= (value << LAYOUT_DIRECTION_MASK_SHIFT);
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07003325 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003326 case com.android.internal.R.styleable.View_drawingCacheQuality:
3327 final int cacheQuality = a.getInt(attr, 0);
3328 if (cacheQuality != 0) {
3329 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
3330 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
3331 }
3332 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07003333 case com.android.internal.R.styleable.View_contentDescription:
Svetoslav Ganove47957a2012-06-05 14:46:50 -07003334 setContentDescription(a.getString(attr));
svetoslavganov75986cf2009-05-14 22:28:01 -07003335 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003336 case com.android.internal.R.styleable.View_soundEffectsEnabled:
3337 if (!a.getBoolean(attr, true)) {
3338 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
3339 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
3340 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003341 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003342 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
3343 if (!a.getBoolean(attr, true)) {
3344 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
3345 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
3346 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003347 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003348 case R.styleable.View_scrollbars:
3349 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
3350 if (scrollbars != SCROLLBARS_NONE) {
3351 viewFlagValues |= scrollbars;
3352 viewFlagMasks |= SCROLLBARS_MASK;
3353 initializeScrollbars(a);
3354 }
3355 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07003356 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003357 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07003358 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
3359 // Ignore the attribute starting with ICS
3360 break;
3361 }
3362 // With builds < ICS, fall through and apply fading edges
3363 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003364 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
3365 if (fadingEdge != FADING_EDGE_NONE) {
3366 viewFlagValues |= fadingEdge;
3367 viewFlagMasks |= FADING_EDGE_MASK;
3368 initializeFadingEdge(a);
3369 }
3370 break;
3371 case R.styleable.View_scrollbarStyle:
3372 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3373 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3374 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3375 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3376 }
3377 break;
3378 case R.styleable.View_isScrollContainer:
3379 setScrollContainer = true;
3380 if (a.getBoolean(attr, false)) {
3381 setScrollContainer(true);
3382 }
3383 break;
3384 case com.android.internal.R.styleable.View_keepScreenOn:
3385 if (a.getBoolean(attr, false)) {
3386 viewFlagValues |= KEEP_SCREEN_ON;
3387 viewFlagMasks |= KEEP_SCREEN_ON;
3388 }
3389 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003390 case R.styleable.View_filterTouchesWhenObscured:
3391 if (a.getBoolean(attr, false)) {
3392 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3393 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3394 }
3395 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003396 case R.styleable.View_nextFocusLeft:
3397 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3398 break;
3399 case R.styleable.View_nextFocusRight:
3400 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3401 break;
3402 case R.styleable.View_nextFocusUp:
3403 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3404 break;
3405 case R.styleable.View_nextFocusDown:
3406 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3407 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003408 case R.styleable.View_nextFocusForward:
3409 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3410 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003411 case R.styleable.View_minWidth:
3412 mMinWidth = a.getDimensionPixelSize(attr, 0);
3413 break;
3414 case R.styleable.View_minHeight:
3415 mMinHeight = a.getDimensionPixelSize(attr, 0);
3416 break;
Romain Guy9a817362009-05-01 10:57:14 -07003417 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003418 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003419 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003420 + "be used within a restricted context");
3421 }
3422
Romain Guy9a817362009-05-01 10:57:14 -07003423 final String handlerName = a.getString(attr);
3424 if (handlerName != null) {
3425 setOnClickListener(new OnClickListener() {
3426 private Method mHandler;
3427
3428 public void onClick(View v) {
3429 if (mHandler == null) {
3430 try {
3431 mHandler = getContext().getClass().getMethod(handlerName,
3432 View.class);
3433 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003434 int id = getId();
3435 String idText = id == NO_ID ? "" : " with id '"
3436 + getContext().getResources().getResourceEntryName(
3437 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003438 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003439 handlerName + "(View) in the activity "
3440 + getContext().getClass() + " for onClick handler"
3441 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003442 }
3443 }
3444
3445 try {
3446 mHandler.invoke(getContext(), View.this);
3447 } catch (IllegalAccessException e) {
3448 throw new IllegalStateException("Could not execute non "
3449 + "public method of the activity", e);
3450 } catch (InvocationTargetException e) {
3451 throw new IllegalStateException("Could not execute "
3452 + "method of the activity", e);
3453 }
3454 }
3455 });
3456 }
3457 break;
Adam Powell637d3372010-08-25 14:37:03 -07003458 case R.styleable.View_overScrollMode:
3459 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3460 break;
Adam Powell20232d02010-12-08 21:08:53 -08003461 case R.styleable.View_verticalScrollbarPosition:
3462 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3463 break;
Romain Guy171c5922011-01-06 10:04:23 -08003464 case R.styleable.View_layerType:
3465 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3466 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003467 case R.styleable.View_textDirection:
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003468 // Clear any text direction flag already set
3469 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
3470 // Set the text direction flags depending on the value of the attribute
3471 final int textDirection = a.getInt(attr, -1);
3472 if (textDirection != -1) {
3473 mPrivateFlags2 |= TEXT_DIRECTION_FLAGS[textDirection];
3474 }
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003475 break;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003476 case R.styleable.View_textAlignment:
3477 // Clear any text alignment flag already set
3478 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
3479 // Set the text alignment flag depending on the value of the attribute
3480 final int textAlignment = a.getInt(attr, TEXT_ALIGNMENT_DEFAULT);
3481 mPrivateFlags2 |= TEXT_ALIGNMENT_FLAGS[textAlignment];
3482 break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07003483 case R.styleable.View_importantForAccessibility:
3484 setImportantForAccessibility(a.getInt(attr,
3485 IMPORTANT_FOR_ACCESSIBILITY_DEFAULT));
Svetoslav Ganov86783472012-06-06 21:12:20 -07003486 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003487 }
3488 }
3489
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003490 a.recycle();
3491
Adam Powell637d3372010-08-25 14:37:03 -07003492 setOverScrollMode(overScrollMode);
3493
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003494 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3495 // layout direction). Those cached values will be used later during padding resolution.
3496 mUserPaddingStart = startPadding;
3497 mUserPaddingEnd = endPadding;
3498
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003499 if (background != null) {
3500 setBackground(background);
3501 }
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003503 if (padding >= 0) {
3504 leftPadding = padding;
3505 topPadding = padding;
3506 rightPadding = padding;
3507 bottomPadding = padding;
3508 }
3509
3510 // If the user specified the padding (either with android:padding or
3511 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3512 // use the default padding or the padding from the background drawable
3513 // (stored at this point in mPadding*)
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003514 internalSetPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003515 topPadding >= 0 ? topPadding : mPaddingTop,
3516 rightPadding >= 0 ? rightPadding : mPaddingRight,
3517 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3518
3519 if (viewFlagMasks != 0) {
3520 setFlags(viewFlagValues, viewFlagMasks);
3521 }
3522
3523 // Needs to be called after mViewFlags is set
3524 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3525 recomputePadding();
3526 }
3527
3528 if (x != 0 || y != 0) {
3529 scrollTo(x, y);
3530 }
3531
Chet Haase73066682010-11-29 15:55:32 -08003532 if (transformSet) {
3533 setTranslationX(tx);
3534 setTranslationY(ty);
3535 setRotation(rotation);
3536 setRotationX(rotationX);
3537 setRotationY(rotationY);
3538 setScaleX(sx);
3539 setScaleY(sy);
3540 }
3541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003542 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3543 setScrollContainer(true);
3544 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003545
3546 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003547 }
3548
3549 /**
3550 * Non-public constructor for use in testing
3551 */
3552 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003553 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003554 }
3555
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003556 /**
3557 * <p>
3558 * Initializes the fading edges from a given set of styled attributes. This
3559 * method should be called by subclasses that need fading edges and when an
3560 * instance of these subclasses is created programmatically rather than
3561 * being inflated from XML. This method is automatically called when the XML
3562 * is inflated.
3563 * </p>
3564 *
3565 * @param a the styled attributes set to initialize the fading edges from
3566 */
3567 protected void initializeFadingEdge(TypedArray a) {
3568 initScrollCache();
3569
3570 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3571 R.styleable.View_fadingEdgeLength,
3572 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3573 }
3574
3575 /**
3576 * Returns the size of the vertical faded edges used to indicate that more
3577 * content in this view is visible.
3578 *
3579 * @return The size in pixels of the vertical faded edge or 0 if vertical
3580 * faded edges are not enabled for this view.
3581 * @attr ref android.R.styleable#View_fadingEdgeLength
3582 */
3583 public int getVerticalFadingEdgeLength() {
3584 if (isVerticalFadingEdgeEnabled()) {
3585 ScrollabilityCache cache = mScrollCache;
3586 if (cache != null) {
3587 return cache.fadingEdgeLength;
3588 }
3589 }
3590 return 0;
3591 }
3592
3593 /**
3594 * Set the size of the faded edge used to indicate that more content in this
3595 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003596 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3597 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3598 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003599 *
3600 * @param length The size in pixels of the faded edge used to indicate that more
3601 * content in this view is visible.
3602 */
3603 public void setFadingEdgeLength(int length) {
3604 initScrollCache();
3605 mScrollCache.fadingEdgeLength = length;
3606 }
3607
3608 /**
3609 * Returns the size of the horizontal faded edges used to indicate that more
3610 * content in this view is visible.
3611 *
3612 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3613 * faded edges are not enabled for this view.
3614 * @attr ref android.R.styleable#View_fadingEdgeLength
3615 */
3616 public int getHorizontalFadingEdgeLength() {
3617 if (isHorizontalFadingEdgeEnabled()) {
3618 ScrollabilityCache cache = mScrollCache;
3619 if (cache != null) {
3620 return cache.fadingEdgeLength;
3621 }
3622 }
3623 return 0;
3624 }
3625
3626 /**
3627 * Returns the width of the vertical scrollbar.
3628 *
3629 * @return The width in pixels of the vertical scrollbar or 0 if there
3630 * is no vertical scrollbar.
3631 */
3632 public int getVerticalScrollbarWidth() {
3633 ScrollabilityCache cache = mScrollCache;
3634 if (cache != null) {
3635 ScrollBarDrawable scrollBar = cache.scrollBar;
3636 if (scrollBar != null) {
3637 int size = scrollBar.getSize(true);
3638 if (size <= 0) {
3639 size = cache.scrollBarSize;
3640 }
3641 return size;
3642 }
3643 return 0;
3644 }
3645 return 0;
3646 }
3647
3648 /**
3649 * Returns the height of the horizontal scrollbar.
3650 *
3651 * @return The height in pixels of the horizontal scrollbar or 0 if
3652 * there is no horizontal scrollbar.
3653 */
3654 protected int getHorizontalScrollbarHeight() {
3655 ScrollabilityCache cache = mScrollCache;
3656 if (cache != null) {
3657 ScrollBarDrawable scrollBar = cache.scrollBar;
3658 if (scrollBar != null) {
3659 int size = scrollBar.getSize(false);
3660 if (size <= 0) {
3661 size = cache.scrollBarSize;
3662 }
3663 return size;
3664 }
3665 return 0;
3666 }
3667 return 0;
3668 }
3669
3670 /**
3671 * <p>
3672 * Initializes the scrollbars from a given set of styled attributes. This
3673 * method should be called by subclasses that need scrollbars and when an
3674 * instance of these subclasses is created programmatically rather than
3675 * being inflated from XML. This method is automatically called when the XML
3676 * is inflated.
3677 * </p>
3678 *
3679 * @param a the styled attributes set to initialize the scrollbars from
3680 */
3681 protected void initializeScrollbars(TypedArray a) {
3682 initScrollCache();
3683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003684 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003685
Mike Cleronf116bf82009-09-27 19:14:12 -07003686 if (scrollabilityCache.scrollBar == null) {
3687 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3688 }
Joe Malin32736f02011-01-19 16:14:20 -08003689
Romain Guy8bda2482010-03-02 11:42:11 -08003690 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003691
Mike Cleronf116bf82009-09-27 19:14:12 -07003692 if (!fadeScrollbars) {
3693 scrollabilityCache.state = ScrollabilityCache.ON;
3694 }
3695 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003696
3697
Mike Cleronf116bf82009-09-27 19:14:12 -07003698 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3699 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3700 .getScrollBarFadeDuration());
3701 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3702 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3703 ViewConfiguration.getScrollDefaultDelay());
3704
Joe Malin32736f02011-01-19 16:14:20 -08003705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003706 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3707 com.android.internal.R.styleable.View_scrollbarSize,
3708 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3709
3710 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3711 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3712
3713 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3714 if (thumb != null) {
3715 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3716 }
3717
3718 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3719 false);
3720 if (alwaysDraw) {
3721 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3722 }
3723
3724 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3725 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3726
3727 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3728 if (thumb != null) {
3729 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3730 }
3731
3732 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3733 false);
3734 if (alwaysDraw) {
3735 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3736 }
3737
Fabrice Di Megliob03b4342012-06-04 12:55:30 -07003738 // Apply layout direction to the new Drawables if needed
3739 final int layoutDirection = getResolvedLayoutDirection();
3740 if (track != null) {
3741 track.setLayoutDirection(layoutDirection);
3742 }
3743 if (thumb != null) {
3744 thumb.setLayoutDirection(layoutDirection);
3745 }
3746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003747 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003748 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003749 }
3750
3751 /**
3752 * <p>
3753 * Initalizes the scrollability cache if necessary.
3754 * </p>
3755 */
3756 private void initScrollCache() {
3757 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003758 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003759 }
3760 }
3761
Philip Milne6c8ea062012-04-03 17:38:43 -07003762 private ScrollabilityCache getScrollCache() {
3763 initScrollCache();
3764 return mScrollCache;
3765 }
3766
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003767 /**
Adam Powell20232d02010-12-08 21:08:53 -08003768 * Set the position of the vertical scroll bar. Should be one of
3769 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3770 * {@link #SCROLLBAR_POSITION_RIGHT}.
3771 *
3772 * @param position Where the vertical scroll bar should be positioned.
3773 */
3774 public void setVerticalScrollbarPosition(int position) {
3775 if (mVerticalScrollbarPosition != position) {
3776 mVerticalScrollbarPosition = position;
3777 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003778 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003779 }
3780 }
3781
3782 /**
3783 * @return The position where the vertical scroll bar will show, if applicable.
3784 * @see #setVerticalScrollbarPosition(int)
3785 */
3786 public int getVerticalScrollbarPosition() {
3787 return mVerticalScrollbarPosition;
3788 }
3789
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003790 ListenerInfo getListenerInfo() {
3791 if (mListenerInfo != null) {
3792 return mListenerInfo;
3793 }
3794 mListenerInfo = new ListenerInfo();
3795 return mListenerInfo;
3796 }
3797
Adam Powell20232d02010-12-08 21:08:53 -08003798 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003799 * Register a callback to be invoked when focus of this view changed.
3800 *
3801 * @param l The callback that will run.
3802 */
3803 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003804 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003805 }
3806
3807 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003808 * Add a listener that will be called when the bounds of the view change due to
3809 * layout processing.
3810 *
3811 * @param listener The listener that will be called when layout bounds change.
3812 */
3813 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003814 ListenerInfo li = getListenerInfo();
3815 if (li.mOnLayoutChangeListeners == null) {
3816 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003817 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003818 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3819 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003820 }
Chet Haase21cd1382010-09-01 17:42:29 -07003821 }
3822
3823 /**
3824 * Remove a listener for layout changes.
3825 *
3826 * @param listener The listener for layout bounds change.
3827 */
3828 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003829 ListenerInfo li = mListenerInfo;
3830 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003831 return;
3832 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003833 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003834 }
3835
3836 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003837 * Add a listener for attach state changes.
3838 *
3839 * This listener will be called whenever this view is attached or detached
3840 * from a window. Remove the listener using
3841 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3842 *
3843 * @param listener Listener to attach
3844 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3845 */
3846 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003847 ListenerInfo li = getListenerInfo();
3848 if (li.mOnAttachStateChangeListeners == null) {
3849 li.mOnAttachStateChangeListeners
3850 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003851 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003852 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003853 }
3854
3855 /**
3856 * Remove a listener for attach state changes. The listener will receive no further
3857 * notification of window attach/detach events.
3858 *
3859 * @param listener Listener to remove
3860 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3861 */
3862 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003863 ListenerInfo li = mListenerInfo;
3864 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003865 return;
3866 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003867 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003868 }
3869
3870 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003871 * Returns the focus-change callback registered for this view.
3872 *
3873 * @return The callback, or null if one is not registered.
3874 */
3875 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003876 ListenerInfo li = mListenerInfo;
3877 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003878 }
3879
3880 /**
3881 * Register a callback to be invoked when this view is clicked. If this view is not
3882 * clickable, it becomes clickable.
3883 *
3884 * @param l The callback that will run
3885 *
3886 * @see #setClickable(boolean)
3887 */
3888 public void setOnClickListener(OnClickListener l) {
3889 if (!isClickable()) {
3890 setClickable(true);
3891 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003892 getListenerInfo().mOnClickListener = l;
3893 }
3894
3895 /**
3896 * Return whether this view has an attached OnClickListener. Returns
3897 * true if there is a listener, false if there is none.
3898 */
3899 public boolean hasOnClickListeners() {
3900 ListenerInfo li = mListenerInfo;
3901 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003902 }
3903
3904 /**
3905 * Register a callback to be invoked when this view is clicked and held. If this view is not
3906 * long clickable, it becomes long clickable.
3907 *
3908 * @param l The callback that will run
3909 *
3910 * @see #setLongClickable(boolean)
3911 */
3912 public void setOnLongClickListener(OnLongClickListener l) {
3913 if (!isLongClickable()) {
3914 setLongClickable(true);
3915 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003916 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003917 }
3918
3919 /**
3920 * Register a callback to be invoked when the context menu for this view is
3921 * being built. If this view is not long clickable, it becomes long clickable.
3922 *
3923 * @param l The callback that will run
3924 *
3925 */
3926 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3927 if (!isLongClickable()) {
3928 setLongClickable(true);
3929 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003930 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003931 }
3932
3933 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003934 * Call this view's OnClickListener, if it is defined. Performs all normal
3935 * actions associated with clicking: reporting accessibility event, playing
3936 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003937 *
3938 * @return True there was an assigned OnClickListener that was called, false
3939 * otherwise is returned.
3940 */
3941 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003942 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3943
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003944 ListenerInfo li = mListenerInfo;
3945 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003946 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003947 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003948 return true;
3949 }
3950
3951 return false;
3952 }
3953
3954 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003955 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
3956 * this only calls the listener, and does not do any associated clicking
3957 * actions like reporting an accessibility event.
3958 *
3959 * @return True there was an assigned OnClickListener that was called, false
3960 * otherwise is returned.
3961 */
3962 public boolean callOnClick() {
3963 ListenerInfo li = mListenerInfo;
3964 if (li != null && li.mOnClickListener != null) {
3965 li.mOnClickListener.onClick(this);
3966 return true;
3967 }
3968 return false;
3969 }
3970
3971 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003972 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3973 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003974 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003975 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003976 */
3977 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003978 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003980 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003981 ListenerInfo li = mListenerInfo;
3982 if (li != null && li.mOnLongClickListener != null) {
3983 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003984 }
3985 if (!handled) {
3986 handled = showContextMenu();
3987 }
3988 if (handled) {
3989 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3990 }
3991 return handled;
3992 }
3993
3994 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003995 * Performs button-related actions during a touch down event.
3996 *
3997 * @param event The event.
3998 * @return True if the down was consumed.
3999 *
4000 * @hide
4001 */
4002 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
4003 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
4004 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
4005 return true;
4006 }
4007 }
4008 return false;
4009 }
4010
4011 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004012 * Bring up the context menu for this view.
4013 *
4014 * @return Whether a context menu was displayed.
4015 */
4016 public boolean showContextMenu() {
4017 return getParent().showContextMenuForChild(this);
4018 }
4019
4020 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004021 * Bring up the context menu for this view, referring to the item under the specified point.
4022 *
4023 * @param x The referenced x coordinate.
4024 * @param y The referenced y coordinate.
4025 * @param metaState The keyboard modifiers that were pressed.
4026 * @return Whether a context menu was displayed.
4027 *
4028 * @hide
4029 */
4030 public boolean showContextMenu(float x, float y, int metaState) {
4031 return showContextMenu();
4032 }
4033
4034 /**
Adam Powell6e346362010-07-23 10:18:23 -07004035 * Start an action mode.
4036 *
4037 * @param callback Callback that will control the lifecycle of the action mode
4038 * @return The new action mode if it is started, null otherwise
4039 *
4040 * @see ActionMode
4041 */
4042 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08004043 ViewParent parent = getParent();
4044 if (parent == null) return null;
4045 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07004046 }
4047
4048 /**
Jean Chalard405bc512012-05-29 19:12:34 +09004049 * Register a callback to be invoked when a hardware key is pressed in this view.
4050 * Key presses in software input methods will generally not trigger the methods of
4051 * this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004052 * @param l the key listener to attach to this view
4053 */
4054 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004055 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004056 }
4057
4058 /**
4059 * Register a callback to be invoked when a touch event is sent to this view.
4060 * @param l the touch listener to attach to this view
4061 */
4062 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004063 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004064 }
4065
4066 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004067 * Register a callback to be invoked when a generic motion event is sent to this view.
4068 * @param l the generic motion listener to attach to this view
4069 */
4070 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004071 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08004072 }
4073
4074 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07004075 * Register a callback to be invoked when a hover event is sent to this view.
4076 * @param l the hover listener to attach to this view
4077 */
4078 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004079 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07004080 }
4081
4082 /**
Joe Malin32736f02011-01-19 16:14:20 -08004083 * Register a drag event listener callback object for this View. The parameter is
4084 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
4085 * View, the system calls the
4086 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
4087 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07004088 */
4089 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004090 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07004091 }
4092
4093 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07004094 * Give this view focus. This will cause
4095 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004096 *
4097 * Note: this does not check whether this {@link View} should get focus, it just
4098 * gives it focus no matter what. It should only be called internally by framework
4099 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
4100 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004101 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
4102 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004103 * focus moved when requestFocus() is called. It may not always
4104 * apply, in which case use the default View.FOCUS_DOWN.
4105 * @param previouslyFocusedRect The rectangle of the view that had focus
4106 * prior in this View's coordinate system.
4107 */
4108 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
4109 if (DBG) {
4110 System.out.println(this + " requestFocus()");
4111 }
4112
4113 if ((mPrivateFlags & FOCUSED) == 0) {
4114 mPrivateFlags |= FOCUSED;
4115
4116 if (mParent != null) {
4117 mParent.requestChildFocus(this, this);
4118 }
4119
4120 onFocusChanged(true, direction, previouslyFocusedRect);
4121 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004122
4123 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4124 notifyAccessibilityStateChanged();
4125 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004126 }
4127 }
4128
4129 /**
4130 * Request that a rectangle of this view be visible on the screen,
4131 * scrolling if necessary just enough.
4132 *
4133 * <p>A View should call this if it maintains some notion of which part
4134 * of its content is interesting. For example, a text editing view
4135 * should call this when its cursor moves.
4136 *
4137 * @param rectangle The rectangle.
4138 * @return Whether any parent scrolled.
4139 */
4140 public boolean requestRectangleOnScreen(Rect rectangle) {
4141 return requestRectangleOnScreen(rectangle, false);
4142 }
4143
4144 /**
4145 * Request that a rectangle of this view be visible on the screen,
4146 * scrolling if necessary just enough.
4147 *
4148 * <p>A View should call this if it maintains some notion of which part
4149 * of its content is interesting. For example, a text editing view
4150 * should call this when its cursor moves.
4151 *
4152 * <p>When <code>immediate</code> is set to true, scrolling will not be
4153 * animated.
4154 *
4155 * @param rectangle The rectangle.
4156 * @param immediate True to forbid animated scrolling, false otherwise
4157 * @return Whether any parent scrolled.
4158 */
4159 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
4160 View child = this;
4161 ViewParent parent = mParent;
4162 boolean scrolled = false;
4163 while (parent != null) {
4164 scrolled |= parent.requestChildRectangleOnScreen(child,
4165 rectangle, immediate);
4166
4167 // offset rect so next call has the rectangle in the
4168 // coordinate system of its direct child.
4169 rectangle.offset(child.getLeft(), child.getTop());
4170 rectangle.offset(-child.getScrollX(), -child.getScrollY());
4171
4172 if (!(parent instanceof View)) {
4173 break;
4174 }
Romain Guy8506ab42009-06-11 17:35:47 -07004175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004176 child = (View) parent;
4177 parent = child.getParent();
4178 }
4179 return scrolled;
4180 }
4181
4182 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004183 * Called when this view wants to give up focus. If focus is cleared
4184 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
4185 * <p>
4186 * <strong>Note:</strong> When a View clears focus the framework is trying
4187 * to give focus to the first focusable View from the top. Hence, if this
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004188 * View is the first from the top that can take focus, then all callbacks
4189 * related to clearing focus will be invoked after wich the framework will
4190 * give focus to this view.
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004191 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004192 */
4193 public void clearFocus() {
4194 if (DBG) {
4195 System.out.println(this + " clearFocus()");
4196 }
4197
4198 if ((mPrivateFlags & FOCUSED) != 0) {
4199 mPrivateFlags &= ~FOCUSED;
4200
4201 if (mParent != null) {
4202 mParent.clearChildFocus(this);
4203 }
4204
4205 onFocusChanged(false, 0, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004207 refreshDrawableState();
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004208
4209 ensureInputFocusOnFirstFocusable();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004210
4211 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4212 notifyAccessibilityStateChanged();
4213 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004214 }
4215 }
4216
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004217 void ensureInputFocusOnFirstFocusable() {
4218 View root = getRootView();
4219 if (root != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004220 root.requestFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004221 }
4222 }
4223
4224 /**
4225 * Called internally by the view system when a new view is getting focus.
4226 * This is what clears the old focus.
4227 */
4228 void unFocus() {
4229 if (DBG) {
4230 System.out.println(this + " unFocus()");
4231 }
4232
4233 if ((mPrivateFlags & FOCUSED) != 0) {
4234 mPrivateFlags &= ~FOCUSED;
4235
4236 onFocusChanged(false, 0, null);
4237 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004238
4239 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4240 notifyAccessibilityStateChanged();
4241 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004242 }
4243 }
4244
4245 /**
4246 * Returns true if this view has focus iteself, or is the ancestor of the
4247 * view that has focus.
4248 *
4249 * @return True if this view has or contains focus, false otherwise.
4250 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004251 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004252 public boolean hasFocus() {
4253 return (mPrivateFlags & FOCUSED) != 0;
4254 }
4255
4256 /**
4257 * Returns true if this view is focusable or if it contains a reachable View
4258 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
4259 * is a View whose parents do not block descendants focus.
4260 *
4261 * Only {@link #VISIBLE} views are considered focusable.
4262 *
4263 * @return True if the view is focusable or if the view contains a focusable
4264 * View, false otherwise.
4265 *
4266 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
4267 */
4268 public boolean hasFocusable() {
4269 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4270 }
4271
4272 /**
4273 * Called by the view system when the focus state of this view changes.
4274 * When the focus change event is caused by directional navigation, direction
4275 * and previouslyFocusedRect provide insight into where the focus is coming from.
4276 * When overriding, be sure to call up through to the super class so that
4277 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004278 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004279 * @param gainFocus True if the View has focus; false otherwise.
4280 * @param direction The direction focus has moved when requestFocus()
4281 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004282 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4283 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4284 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004285 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4286 * system, of the previously focused view. If applicable, this will be
4287 * passed in as finer grained information about where the focus is coming
4288 * from (in addition to direction). Will be <code>null</code> otherwise.
4289 */
4290 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004291 if (gainFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004292 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4293 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004294 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004295 }
4296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004297 InputMethodManager imm = InputMethodManager.peekInstance();
4298 if (!gainFocus) {
4299 if (isPressed()) {
4300 setPressed(false);
4301 }
4302 if (imm != null && mAttachInfo != null
4303 && mAttachInfo.mHasWindowFocus) {
4304 imm.focusOut(this);
4305 }
Romain Guya2431d02009-04-30 16:30:00 -07004306 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004307 } else if (imm != null && mAttachInfo != null
4308 && mAttachInfo.mHasWindowFocus) {
4309 imm.focusIn(this);
4310 }
Romain Guy8506ab42009-06-11 17:35:47 -07004311
Romain Guy0fd89bf2011-01-26 15:41:30 -08004312 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004313 ListenerInfo li = mListenerInfo;
4314 if (li != null && li.mOnFocusChangeListener != null) {
4315 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004316 }
Joe Malin32736f02011-01-19 16:14:20 -08004317
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004318 if (mAttachInfo != null) {
4319 mAttachInfo.mKeyDispatchState.reset(this);
4320 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004321 }
4322
4323 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004324 * Sends an accessibility event of the given type. If accessiiblity is
4325 * not enabled this method has no effect. The default implementation calls
4326 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4327 * to populate information about the event source (this View), then calls
4328 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4329 * populate the text content of the event source including its descendants,
4330 * and last calls
4331 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4332 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004333 * <p>
4334 * If an {@link AccessibilityDelegate} has been specified via calling
4335 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4336 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4337 * responsible for handling this call.
4338 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004339 *
Scott Mainb303d832011-10-12 16:45:18 -07004340 * @param eventType The type of the event to send, as defined by several types from
4341 * {@link android.view.accessibility.AccessibilityEvent}, such as
4342 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4343 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004344 *
4345 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4346 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4347 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004348 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004349 */
4350 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004351 if (mAccessibilityDelegate != null) {
4352 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4353 } else {
4354 sendAccessibilityEventInternal(eventType);
4355 }
4356 }
4357
4358 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004359 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4360 * {@link AccessibilityEvent} to make an announcement which is related to some
4361 * sort of a context change for which none of the events representing UI transitions
4362 * is a good fit. For example, announcing a new page in a book. If accessibility
4363 * is not enabled this method does nothing.
4364 *
4365 * @param text The announcement text.
4366 */
4367 public void announceForAccessibility(CharSequence text) {
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004368 if (AccessibilityManager.getInstance(mContext).isEnabled() && mParent != null) {
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004369 AccessibilityEvent event = AccessibilityEvent.obtain(
4370 AccessibilityEvent.TYPE_ANNOUNCEMENT);
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004371 onInitializeAccessibilityEvent(event);
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004372 event.getText().add(text);
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004373 event.setContentDescription(null);
4374 mParent.requestSendAccessibilityEvent(this, event);
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004375 }
4376 }
4377
4378 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004379 * @see #sendAccessibilityEvent(int)
4380 *
4381 * Note: Called from the default {@link AccessibilityDelegate}.
4382 */
4383 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004384 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4385 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4386 }
4387 }
4388
4389 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004390 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4391 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004392 * perform a check whether accessibility is enabled.
4393 * <p>
4394 * If an {@link AccessibilityDelegate} has been specified via calling
4395 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4396 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4397 * is responsible for handling this call.
4398 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004399 *
4400 * @param event The event to send.
4401 *
4402 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004403 */
4404 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004405 if (mAccessibilityDelegate != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004406 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004407 } else {
4408 sendAccessibilityEventUncheckedInternal(event);
4409 }
4410 }
4411
4412 /**
4413 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4414 *
4415 * Note: Called from the default {@link AccessibilityDelegate}.
4416 */
4417 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004418 if (!isShown()) {
4419 return;
4420 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004421 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004422 // Only a subset of accessibility events populates text content.
4423 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4424 dispatchPopulateAccessibilityEvent(event);
4425 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004426 // In the beginning we called #isShown(), so we know that getParent() is not null.
4427 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004428 }
4429
4430 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004431 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4432 * to its children for adding their text content to the event. Note that the
4433 * event text is populated in a separate dispatch path since we add to the
4434 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004435 * A typical implementation will call
4436 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4437 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4438 * on each child. Override this method if custom population of the event text
4439 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004440 * <p>
4441 * If an {@link AccessibilityDelegate} has been specified via calling
4442 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4443 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4444 * is responsible for handling this call.
4445 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004446 * <p>
4447 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4448 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4449 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004450 *
4451 * @param event The event.
4452 *
4453 * @return True if the event population was completed.
4454 */
4455 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004456 if (mAccessibilityDelegate != null) {
4457 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4458 } else {
4459 return dispatchPopulateAccessibilityEventInternal(event);
4460 }
4461 }
4462
4463 /**
4464 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4465 *
4466 * Note: Called from the default {@link AccessibilityDelegate}.
4467 */
4468 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004469 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004470 return false;
4471 }
4472
4473 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004474 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004475 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004476 * text content. While this method is free to modify event
4477 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004478 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4479 * <p>
4480 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004481 * to the text added by the super implementation:
4482 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004483 * super.onPopulateAccessibilityEvent(event);
4484 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4485 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4486 * mCurrentDate.getTimeInMillis(), flags);
4487 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004488 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004489 * <p>
4490 * If an {@link AccessibilityDelegate} has been specified via calling
4491 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4492 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4493 * is responsible for handling this call.
4494 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004495 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4496 * information to the event, in case the default implementation has basic information to add.
4497 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004498 *
4499 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004500 *
4501 * @see #sendAccessibilityEvent(int)
4502 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004503 */
4504 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004505 if (mAccessibilityDelegate != null) {
4506 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4507 } else {
4508 onPopulateAccessibilityEventInternal(event);
4509 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004510 }
4511
4512 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004513 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4514 *
4515 * Note: Called from the default {@link AccessibilityDelegate}.
4516 */
4517 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4518
4519 }
4520
4521 /**
4522 * Initializes an {@link AccessibilityEvent} with information about
4523 * this View which is the event source. In other words, the source of
4524 * an accessibility event is the view whose state change triggered firing
4525 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004526 * <p>
4527 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004528 * to properties set by the super implementation:
4529 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4530 * super.onInitializeAccessibilityEvent(event);
4531 * event.setPassword(true);
4532 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004533 * <p>
4534 * If an {@link AccessibilityDelegate} has been specified via calling
4535 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4536 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4537 * is responsible for handling this call.
4538 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004539 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4540 * information to the event, in case the default implementation has basic information to add.
4541 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004542 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004543 *
4544 * @see #sendAccessibilityEvent(int)
4545 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4546 */
4547 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004548 if (mAccessibilityDelegate != null) {
4549 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4550 } else {
4551 onInitializeAccessibilityEventInternal(event);
4552 }
4553 }
4554
4555 /**
4556 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4557 *
4558 * Note: Called from the default {@link AccessibilityDelegate}.
4559 */
4560 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004561 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004562 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004563 event.setPackageName(getContext().getPackageName());
4564 event.setEnabled(isEnabled());
4565 event.setContentDescription(mContentDescription);
4566
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004567 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004568 ArrayList<View> focusablesTempList = mAttachInfo.mTempArrayList;
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004569 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4570 FOCUSABLES_ALL);
4571 event.setItemCount(focusablesTempList.size());
4572 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4573 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004574 }
4575 }
4576
4577 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004578 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4579 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4580 * This method is responsible for obtaining an accessibility node info from a
4581 * pool of reusable instances and calling
4582 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4583 * initialize the former.
4584 * <p>
4585 * Note: The client is responsible for recycling the obtained instance by calling
4586 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4587 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004588 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004589 * @return A populated {@link AccessibilityNodeInfo}.
4590 *
4591 * @see AccessibilityNodeInfo
4592 */
4593 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004594 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4595 if (provider != null) {
4596 return provider.createAccessibilityNodeInfo(View.NO_ID);
4597 } else {
4598 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4599 onInitializeAccessibilityNodeInfo(info);
4600 return info;
4601 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004602 }
4603
4604 /**
4605 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4606 * The base implementation sets:
4607 * <ul>
4608 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004609 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4610 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004611 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4612 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4613 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4614 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4615 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4616 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4617 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4618 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4619 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4620 * </ul>
4621 * <p>
4622 * Subclasses should override this method, call the super implementation,
4623 * and set additional attributes.
4624 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004625 * <p>
4626 * If an {@link AccessibilityDelegate} has been specified via calling
4627 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4628 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4629 * is responsible for handling this call.
4630 * </p>
4631 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004632 * @param info The instance to initialize.
4633 */
4634 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004635 if (mAccessibilityDelegate != null) {
4636 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4637 } else {
4638 onInitializeAccessibilityNodeInfoInternal(info);
4639 }
4640 }
4641
4642 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004643 * Gets the location of this view in screen coordintates.
4644 *
4645 * @param outRect The output location
4646 */
4647 private void getBoundsOnScreen(Rect outRect) {
4648 if (mAttachInfo == null) {
4649 return;
4650 }
4651
4652 RectF position = mAttachInfo.mTmpTransformRect;
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004653 position.set(0, 0, mRight - mLeft, mBottom - mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004654
4655 if (!hasIdentityMatrix()) {
4656 getMatrix().mapRect(position);
4657 }
4658
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004659 position.offset(mLeft, mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004660
4661 ViewParent parent = mParent;
4662 while (parent instanceof View) {
4663 View parentView = (View) parent;
4664
4665 position.offset(-parentView.mScrollX, -parentView.mScrollY);
4666
4667 if (!parentView.hasIdentityMatrix()) {
4668 parentView.getMatrix().mapRect(position);
4669 }
4670
4671 position.offset(parentView.mLeft, parentView.mTop);
4672
4673 parent = parentView.mParent;
4674 }
4675
4676 if (parent instanceof ViewRootImpl) {
4677 ViewRootImpl viewRootImpl = (ViewRootImpl) parent;
4678 position.offset(0, -viewRootImpl.mCurScrollY);
4679 }
4680
4681 position.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
4682
4683 outRect.set((int) (position.left + 0.5f), (int) (position.top + 0.5f),
4684 (int) (position.right + 0.5f), (int) (position.bottom + 0.5f));
4685 }
4686
4687 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004688 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4689 *
4690 * Note: Called from the default {@link AccessibilityDelegate}.
4691 */
4692 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004693 Rect bounds = mAttachInfo.mTmpInvalRect;
Svetoslav Ganov983119a2012-07-03 21:04:10 -07004694
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004695 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004696 info.setBoundsInParent(bounds);
4697
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004698 getBoundsOnScreen(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004699 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004700
Svetoslav Ganovc406be92012-05-11 16:12:32 -07004701 ViewParent parent = getParentForAccessibility();
4702 if (parent instanceof View) {
4703 info.setParent((View) parent);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004704 }
4705
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004706 info.setVisibleToUser(isVisibleToUser());
4707
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004708 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004709 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004710 info.setContentDescription(getContentDescription());
4711
4712 info.setEnabled(isEnabled());
4713 info.setClickable(isClickable());
4714 info.setFocusable(isFocusable());
4715 info.setFocused(isFocused());
Svetoslav Ganov42138042012-03-20 11:51:39 -07004716 info.setAccessibilityFocused(isAccessibilityFocused());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004717 info.setSelected(isSelected());
4718 info.setLongClickable(isLongClickable());
4719
4720 // TODO: These make sense only if we are in an AdapterView but all
4721 // views can be selected. Maybe from accessiiblity perspective
4722 // we should report as selectable view in an AdapterView.
4723 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4724 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4725
4726 if (isFocusable()) {
4727 if (isFocused()) {
4728 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4729 } else {
4730 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4731 }
4732 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004733
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07004734 if (!isAccessibilityFocused()) {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07004735 info.addAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07004736 } else {
4737 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
4738 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004739
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07004740 if (isClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004741 info.addAction(AccessibilityNodeInfo.ACTION_CLICK);
4742 }
4743
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07004744 if (isLongClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004745 info.addAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
4746 }
4747
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004748 if (mContentDescription != null && mContentDescription.length() > 0) {
Svetoslav Ganov2b435aa2012-05-04 17:16:37 -07004749 info.addAction(AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY);
4750 info.addAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY);
4751 info.setMovementGranularities(AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004752 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD
4753 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH);
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004754 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004755 }
4756
4757 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004758 * Computes whether this view is visible to the user. Such a view is
4759 * attached, visible, all its predecessors are visible, it is not clipped
4760 * entirely by its predecessors, and has an alpha greater than zero.
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004761 *
4762 * @return Whether the view is visible on the screen.
Guang Zhu0d607fb2012-05-11 19:34:56 -07004763 *
4764 * @hide
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004765 */
Guang Zhu0d607fb2012-05-11 19:34:56 -07004766 protected boolean isVisibleToUser() {
4767 return isVisibleToUser(null);
4768 }
4769
4770 /**
Romain Guyf0af1d52012-07-11 18:31:21 -07004771 * Computes whether the given portion of this view is visible to the user.
4772 * Such a view is attached, visible, all its predecessors are visible,
4773 * has an alpha greater than zero, and the specified portion is not
4774 * clipped entirely by its predecessors.
Guang Zhu0d607fb2012-05-11 19:34:56 -07004775 *
4776 * @param boundInView the portion of the view to test; coordinates should be relative; may be
4777 * <code>null</code>, and the entire view will be tested in this case.
4778 * When <code>true</code> is returned by the function, the actual visible
4779 * region will be stored in this parameter; that is, if boundInView is fully
4780 * contained within the view, no modification will be made, otherwise regions
4781 * outside of the visible area of the view will be clipped.
4782 *
4783 * @return Whether the specified portion of the view is visible on the screen.
4784 *
4785 * @hide
4786 */
4787 protected boolean isVisibleToUser(Rect boundInView) {
Romain Guyf0af1d52012-07-11 18:31:21 -07004788 if (mAttachInfo != null) {
4789 Rect visibleRect = mAttachInfo.mTmpInvalRect;
4790 Point offset = mAttachInfo.mPoint;
4791 // The first two checks are made also made by isShown() which
4792 // however traverses the tree up to the parent to catch that.
4793 // Therefore, we do some fail fast check to minimize the up
4794 // tree traversal.
4795 boolean isVisible = mAttachInfo.mWindowVisibility == View.VISIBLE
4796 && getAlpha() > 0
4797 && isShown()
4798 && getGlobalVisibleRect(visibleRect, offset);
Guang Zhu0d607fb2012-05-11 19:34:56 -07004799 if (isVisible && boundInView != null) {
4800 visibleRect.offset(-offset.x, -offset.y);
Romain Guyf0af1d52012-07-11 18:31:21 -07004801 // isVisible is always true here, use a simple assignment
4802 isVisible = boundInView.intersect(visibleRect);
Guang Zhu0d607fb2012-05-11 19:34:56 -07004803 }
4804 return isVisible;
Romain Guyf0af1d52012-07-11 18:31:21 -07004805 }
4806
4807 return false;
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004808 }
4809
4810 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004811 * Sets a delegate for implementing accessibility support via compositon as
4812 * opposed to inheritance. The delegate's primary use is for implementing
4813 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4814 *
4815 * @param delegate The delegate instance.
4816 *
4817 * @see AccessibilityDelegate
4818 */
4819 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4820 mAccessibilityDelegate = delegate;
4821 }
4822
4823 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004824 * Gets the provider for managing a virtual view hierarchy rooted at this View
4825 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4826 * that explore the window content.
4827 * <p>
4828 * If this method returns an instance, this instance is responsible for managing
4829 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4830 * View including the one representing the View itself. Similarly the returned
4831 * instance is responsible for performing accessibility actions on any virtual
4832 * view or the root view itself.
4833 * </p>
4834 * <p>
4835 * If an {@link AccessibilityDelegate} has been specified via calling
4836 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4837 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4838 * is responsible for handling this call.
4839 * </p>
4840 *
4841 * @return The provider.
4842 *
4843 * @see AccessibilityNodeProvider
4844 */
4845 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4846 if (mAccessibilityDelegate != null) {
4847 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4848 } else {
4849 return null;
4850 }
4851 }
4852
4853 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004854 * Gets the unique identifier of this view on the screen for accessibility purposes.
4855 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4856 *
4857 * @return The view accessibility id.
4858 *
4859 * @hide
4860 */
4861 public int getAccessibilityViewId() {
4862 if (mAccessibilityViewId == NO_ID) {
4863 mAccessibilityViewId = sNextAccessibilityViewId++;
4864 }
4865 return mAccessibilityViewId;
4866 }
4867
4868 /**
4869 * Gets the unique identifier of the window in which this View reseides.
4870 *
4871 * @return The window accessibility id.
4872 *
4873 * @hide
4874 */
4875 public int getAccessibilityWindowId() {
4876 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4877 }
4878
4879 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004880 * Gets the {@link View} description. It briefly describes the view and is
4881 * primarily used for accessibility support. Set this property to enable
4882 * better accessibility support for your application. This is especially
4883 * true for views that do not have textual representation (For example,
4884 * ImageButton).
4885 *
Svetoslav Ganov42138042012-03-20 11:51:39 -07004886 * @return The content description.
svetoslavganov75986cf2009-05-14 22:28:01 -07004887 *
4888 * @attr ref android.R.styleable#View_contentDescription
4889 */
Svetoslav Ganov42138042012-03-20 11:51:39 -07004890 @ViewDebug.ExportedProperty(category = "accessibility")
svetoslavganov75986cf2009-05-14 22:28:01 -07004891 public CharSequence getContentDescription() {
4892 return mContentDescription;
4893 }
4894
4895 /**
4896 * Sets the {@link View} description. It briefly describes the view and is
4897 * primarily used for accessibility support. Set this property to enable
4898 * better accessibility support for your application. This is especially
4899 * true for views that do not have textual representation (For example,
4900 * ImageButton).
4901 *
4902 * @param contentDescription The content description.
4903 *
4904 * @attr ref android.R.styleable#View_contentDescription
4905 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07004906 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07004907 public void setContentDescription(CharSequence contentDescription) {
4908 mContentDescription = contentDescription;
Svetoslav Ganove47957a2012-06-05 14:46:50 -07004909 final boolean nonEmptyDesc = contentDescription != null && contentDescription.length() > 0;
4910 if (nonEmptyDesc && getImportantForAccessibility() == IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
4911 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
4912 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004913 }
4914
4915 /**
Romain Guya2431d02009-04-30 16:30:00 -07004916 * Invoked whenever this view loses focus, either by losing window focus or by losing
4917 * focus within its window. This method can be used to clear any state tied to the
4918 * focus. For instance, if a button is held pressed with the trackball and the window
4919 * loses focus, this method can be used to cancel the press.
4920 *
4921 * Subclasses of View overriding this method should always call super.onFocusLost().
4922 *
4923 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004924 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004925 *
4926 * @hide pending API council approval
4927 */
4928 protected void onFocusLost() {
4929 resetPressedState();
4930 }
4931
4932 private void resetPressedState() {
4933 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4934 return;
4935 }
4936
4937 if (isPressed()) {
4938 setPressed(false);
4939
4940 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004941 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004942 }
4943 }
4944 }
4945
4946 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004947 * Returns true if this view has focus
4948 *
4949 * @return True if this view has focus, false otherwise.
4950 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004951 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004952 public boolean isFocused() {
4953 return (mPrivateFlags & FOCUSED) != 0;
4954 }
4955
4956 /**
4957 * Find the view in the hierarchy rooted at this view that currently has
4958 * focus.
4959 *
4960 * @return The view that currently has focus, or null if no focused view can
4961 * be found.
4962 */
4963 public View findFocus() {
4964 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4965 }
4966
4967 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07004968 * Indicates whether this view is one of the set of scrollable containers in
4969 * its window.
4970 *
4971 * @return whether this view is one of the set of scrollable containers in
4972 * its window
4973 *
4974 * @attr ref android.R.styleable#View_isScrollContainer
4975 */
4976 public boolean isScrollContainer() {
4977 return (mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0;
4978 }
4979
4980 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004981 * Change whether this view is one of the set of scrollable containers in
4982 * its window. This will be used to determine whether the window can
4983 * resize or must pan when a soft input area is open -- scrollable
4984 * containers allow the window to use resize mode since the container
4985 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07004986 *
4987 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004988 */
4989 public void setScrollContainer(boolean isScrollContainer) {
4990 if (isScrollContainer) {
4991 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4992 mAttachInfo.mScrollContainers.add(this);
4993 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4994 }
4995 mPrivateFlags |= SCROLL_CONTAINER;
4996 } else {
4997 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4998 mAttachInfo.mScrollContainers.remove(this);
4999 }
5000 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
5001 }
5002 }
5003
5004 /**
5005 * Returns the quality of the drawing cache.
5006 *
5007 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5008 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5009 *
5010 * @see #setDrawingCacheQuality(int)
5011 * @see #setDrawingCacheEnabled(boolean)
5012 * @see #isDrawingCacheEnabled()
5013 *
5014 * @attr ref android.R.styleable#View_drawingCacheQuality
5015 */
5016 public int getDrawingCacheQuality() {
5017 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
5018 }
5019
5020 /**
5021 * Set the drawing cache quality of this view. This value is used only when the
5022 * drawing cache is enabled
5023 *
5024 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5025 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5026 *
5027 * @see #getDrawingCacheQuality()
5028 * @see #setDrawingCacheEnabled(boolean)
5029 * @see #isDrawingCacheEnabled()
5030 *
5031 * @attr ref android.R.styleable#View_drawingCacheQuality
5032 */
5033 public void setDrawingCacheQuality(int quality) {
5034 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
5035 }
5036
5037 /**
5038 * Returns whether the screen should remain on, corresponding to the current
5039 * value of {@link #KEEP_SCREEN_ON}.
5040 *
5041 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
5042 *
5043 * @see #setKeepScreenOn(boolean)
5044 *
5045 * @attr ref android.R.styleable#View_keepScreenOn
5046 */
5047 public boolean getKeepScreenOn() {
5048 return (mViewFlags & KEEP_SCREEN_ON) != 0;
5049 }
5050
5051 /**
5052 * Controls whether the screen should remain on, modifying the
5053 * value of {@link #KEEP_SCREEN_ON}.
5054 *
5055 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
5056 *
5057 * @see #getKeepScreenOn()
5058 *
5059 * @attr ref android.R.styleable#View_keepScreenOn
5060 */
5061 public void setKeepScreenOn(boolean keepScreenOn) {
5062 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
5063 }
5064
5065 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005066 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5067 * @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 -08005068 *
5069 * @attr ref android.R.styleable#View_nextFocusLeft
5070 */
5071 public int getNextFocusLeftId() {
5072 return mNextFocusLeftId;
5073 }
5074
5075 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005076 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5077 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
5078 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005079 *
5080 * @attr ref android.R.styleable#View_nextFocusLeft
5081 */
5082 public void setNextFocusLeftId(int nextFocusLeftId) {
5083 mNextFocusLeftId = nextFocusLeftId;
5084 }
5085
5086 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005087 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5088 * @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 -08005089 *
5090 * @attr ref android.R.styleable#View_nextFocusRight
5091 */
5092 public int getNextFocusRightId() {
5093 return mNextFocusRightId;
5094 }
5095
5096 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005097 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5098 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
5099 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005100 *
5101 * @attr ref android.R.styleable#View_nextFocusRight
5102 */
5103 public void setNextFocusRightId(int nextFocusRightId) {
5104 mNextFocusRightId = nextFocusRightId;
5105 }
5106
5107 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005108 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5109 * @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 -08005110 *
5111 * @attr ref android.R.styleable#View_nextFocusUp
5112 */
5113 public int getNextFocusUpId() {
5114 return mNextFocusUpId;
5115 }
5116
5117 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005118 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5119 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
5120 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005121 *
5122 * @attr ref android.R.styleable#View_nextFocusUp
5123 */
5124 public void setNextFocusUpId(int nextFocusUpId) {
5125 mNextFocusUpId = nextFocusUpId;
5126 }
5127
5128 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005129 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5130 * @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 -08005131 *
5132 * @attr ref android.R.styleable#View_nextFocusDown
5133 */
5134 public int getNextFocusDownId() {
5135 return mNextFocusDownId;
5136 }
5137
5138 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005139 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5140 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
5141 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005142 *
5143 * @attr ref android.R.styleable#View_nextFocusDown
5144 */
5145 public void setNextFocusDownId(int nextFocusDownId) {
5146 mNextFocusDownId = nextFocusDownId;
5147 }
5148
5149 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005150 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5151 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
5152 *
5153 * @attr ref android.R.styleable#View_nextFocusForward
5154 */
5155 public int getNextFocusForwardId() {
5156 return mNextFocusForwardId;
5157 }
5158
5159 /**
5160 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5161 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
5162 * decide automatically.
5163 *
5164 * @attr ref android.R.styleable#View_nextFocusForward
5165 */
5166 public void setNextFocusForwardId(int nextFocusForwardId) {
5167 mNextFocusForwardId = nextFocusForwardId;
5168 }
5169
5170 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005171 * Returns the visibility of this view and all of its ancestors
5172 *
5173 * @return True if this view and all of its ancestors are {@link #VISIBLE}
5174 */
5175 public boolean isShown() {
5176 View current = this;
5177 //noinspection ConstantConditions
5178 do {
5179 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5180 return false;
5181 }
5182 ViewParent parent = current.mParent;
5183 if (parent == null) {
5184 return false; // We are not attached to the view root
5185 }
5186 if (!(parent instanceof View)) {
5187 return true;
5188 }
5189 current = (View) parent;
5190 } while (current != null);
5191
5192 return false;
5193 }
5194
5195 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005196 * Called by the view hierarchy when the content insets for a window have
5197 * changed, to allow it to adjust its content to fit within those windows.
5198 * The content insets tell you the space that the status bar, input method,
5199 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005200 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005201 * <p>You do not normally need to deal with this function, since the default
5202 * window decoration given to applications takes care of applying it to the
5203 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
5204 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
5205 * and your content can be placed under those system elements. You can then
5206 * use this method within your view hierarchy if you have parts of your UI
5207 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005208 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005209 * <p>The default implementation of this method simply applies the content
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005210 * inset's to the view's padding, consuming that content (modifying the
5211 * insets to be 0), and returning true. This behavior is off by default, but can
5212 * be enabled through {@link #setFitsSystemWindows(boolean)}.
5213 *
5214 * <p>This function's traversal down the hierarchy is depth-first. The same content
5215 * insets object is propagated down the hierarchy, so any changes made to it will
5216 * be seen by all following views (including potentially ones above in
5217 * the hierarchy since this is a depth-first traversal). The first view
5218 * that returns true will abort the entire traversal.
5219 *
5220 * <p>The default implementation works well for a situation where it is
5221 * used with a container that covers the entire window, allowing it to
5222 * apply the appropriate insets to its content on all edges. If you need
5223 * a more complicated layout (such as two different views fitting system
5224 * windows, one on the top of the window, and one on the bottom),
5225 * you can override the method and handle the insets however you would like.
5226 * Note that the insets provided by the framework are always relative to the
5227 * far edges of the window, not accounting for the location of the called view
5228 * within that window. (In fact when this method is called you do not yet know
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005229 * where the layout will place the view, as it is done before layout happens.)
5230 *
5231 * <p>Note: unlike many View methods, there is no dispatch phase to this
5232 * call. If you are overriding it in a ViewGroup and want to allow the
5233 * call to continue to your children, you must be sure to call the super
5234 * implementation.
5235 *
Dianne Hackborncf675782012-05-10 15:07:24 -07005236 * <p>Here is a sample layout that makes use of fitting system windows
5237 * to have controls for a video view placed inside of the window decorations
5238 * that it hides and shows. This can be used with code like the second
5239 * sample (video player) shown in {@link #setSystemUiVisibility(int)}.
5240 *
5241 * {@sample development/samples/ApiDemos/res/layout/video_player.xml complete}
5242 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005243 * @param insets Current content insets of the window. Prior to
5244 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
5245 * the insets or else you and Android will be unhappy.
5246 *
5247 * @return Return true if this view applied the insets and it should not
5248 * continue propagating further down the hierarchy, false otherwise.
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005249 * @see #getFitsSystemWindows()
Romain Guyf0af1d52012-07-11 18:31:21 -07005250 * @see #setFitsSystemWindows(boolean)
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005251 * @see #setSystemUiVisibility(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005252 */
5253 protected boolean fitSystemWindows(Rect insets) {
5254 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07005255 mUserPaddingStart = UNDEFINED_PADDING;
5256 mUserPaddingEnd = UNDEFINED_PADDING;
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005257 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
5258 || mAttachInfo == null
5259 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
5260 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
5261 return true;
5262 } else {
5263 internalSetPadding(0, 0, 0, 0);
5264 return false;
5265 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005266 }
5267 return false;
5268 }
5269
5270 /**
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005271 * Sets whether or not this view should account for system screen decorations
5272 * such as the status bar and inset its content; that is, controlling whether
5273 * the default implementation of {@link #fitSystemWindows(Rect)} will be
5274 * executed. See that method for more details.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005275 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005276 * <p>Note that if you are providing your own implementation of
5277 * {@link #fitSystemWindows(Rect)}, then there is no need to set this
5278 * flag to true -- your implementation will be overriding the default
5279 * implementation that checks this flag.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005280 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005281 * @param fitSystemWindows If true, then the default implementation of
5282 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005283 *
5284 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005285 * @see #getFitsSystemWindows()
5286 * @see #fitSystemWindows(Rect)
5287 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005288 */
5289 public void setFitsSystemWindows(boolean fitSystemWindows) {
5290 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
5291 }
5292
5293 /**
Dianne Hackborncf675782012-05-10 15:07:24 -07005294 * Check for state of {@link #setFitsSystemWindows(boolean). If this method
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005295 * returns true, the default implementation of {@link #fitSystemWindows(Rect)}
5296 * will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005297 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005298 * @return Returns true if the default implementation of
5299 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005300 *
5301 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005302 * @see #setFitsSystemWindows()
5303 * @see #fitSystemWindows(Rect)
5304 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005305 */
Dianne Hackborncf675782012-05-10 15:07:24 -07005306 public boolean getFitsSystemWindows() {
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005307 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
5308 }
5309
Dianne Hackbornb1b55e62012-05-10 16:25:54 -07005310 /** @hide */
5311 public boolean fitsSystemWindows() {
5312 return getFitsSystemWindows();
5313 }
5314
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005315 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005316 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
5317 */
5318 public void requestFitSystemWindows() {
5319 if (mParent != null) {
5320 mParent.requestFitSystemWindows();
5321 }
5322 }
5323
5324 /**
5325 * For use by PhoneWindow to make its own system window fitting optional.
5326 * @hide
5327 */
5328 public void makeOptionalFitsSystemWindows() {
5329 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5330 }
5331
5332 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005333 * Returns the visibility status for this view.
5334 *
5335 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5336 * @attr ref android.R.styleable#View_visibility
5337 */
5338 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005339 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5340 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5341 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005342 })
5343 public int getVisibility() {
5344 return mViewFlags & VISIBILITY_MASK;
5345 }
5346
5347 /**
5348 * Set the enabled state of this view.
5349 *
5350 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5351 * @attr ref android.R.styleable#View_visibility
5352 */
5353 @RemotableViewMethod
5354 public void setVisibility(int visibility) {
5355 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005356 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005357 }
5358
5359 /**
5360 * Returns the enabled status for this view. The interpretation of the
5361 * enabled state varies by subclass.
5362 *
5363 * @return True if this view is enabled, false otherwise.
5364 */
5365 @ViewDebug.ExportedProperty
5366 public boolean isEnabled() {
5367 return (mViewFlags & ENABLED_MASK) == ENABLED;
5368 }
5369
5370 /**
5371 * Set the enabled state of this view. The interpretation of the enabled
5372 * state varies by subclass.
5373 *
5374 * @param enabled True if this view is enabled, false otherwise.
5375 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005376 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005377 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005378 if (enabled == isEnabled()) return;
5379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005380 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5381
5382 /*
5383 * The View most likely has to change its appearance, so refresh
5384 * the drawable state.
5385 */
5386 refreshDrawableState();
5387
5388 // Invalidate too, since the default behavior for views is to be
5389 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005390 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005391 }
5392
5393 /**
5394 * Set whether this view can receive the focus.
5395 *
5396 * Setting this to false will also ensure that this view is not focusable
5397 * in touch mode.
5398 *
5399 * @param focusable If true, this view can receive the focus.
5400 *
5401 * @see #setFocusableInTouchMode(boolean)
5402 * @attr ref android.R.styleable#View_focusable
5403 */
5404 public void setFocusable(boolean focusable) {
5405 if (!focusable) {
5406 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5407 }
5408 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5409 }
5410
5411 /**
5412 * Set whether this view can receive focus while in touch mode.
5413 *
5414 * Setting this to true will also ensure that this view is focusable.
5415 *
5416 * @param focusableInTouchMode If true, this view can receive the focus while
5417 * in touch mode.
5418 *
5419 * @see #setFocusable(boolean)
5420 * @attr ref android.R.styleable#View_focusableInTouchMode
5421 */
5422 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5423 // Focusable in touch mode should always be set before the focusable flag
5424 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5425 // which, in touch mode, will not successfully request focus on this view
5426 // because the focusable in touch mode flag is not set
5427 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5428 if (focusableInTouchMode) {
5429 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5430 }
5431 }
5432
5433 /**
5434 * Set whether this view should have sound effects enabled for events such as
5435 * clicking and touching.
5436 *
5437 * <p>You may wish to disable sound effects for a view if you already play sounds,
5438 * for instance, a dial key that plays dtmf tones.
5439 *
5440 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5441 * @see #isSoundEffectsEnabled()
5442 * @see #playSoundEffect(int)
5443 * @attr ref android.R.styleable#View_soundEffectsEnabled
5444 */
5445 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5446 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5447 }
5448
5449 /**
5450 * @return whether this view should have sound effects enabled for events such as
5451 * clicking and touching.
5452 *
5453 * @see #setSoundEffectsEnabled(boolean)
5454 * @see #playSoundEffect(int)
5455 * @attr ref android.R.styleable#View_soundEffectsEnabled
5456 */
5457 @ViewDebug.ExportedProperty
5458 public boolean isSoundEffectsEnabled() {
5459 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5460 }
5461
5462 /**
5463 * Set whether this view should have haptic feedback for events such as
5464 * long presses.
5465 *
5466 * <p>You may wish to disable haptic feedback if your view already controls
5467 * its own haptic feedback.
5468 *
5469 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5470 * @see #isHapticFeedbackEnabled()
5471 * @see #performHapticFeedback(int)
5472 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5473 */
5474 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5475 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5476 }
5477
5478 /**
5479 * @return whether this view should have haptic feedback enabled for events
5480 * long presses.
5481 *
5482 * @see #setHapticFeedbackEnabled(boolean)
5483 * @see #performHapticFeedback(int)
5484 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5485 */
5486 @ViewDebug.ExportedProperty
5487 public boolean isHapticFeedbackEnabled() {
5488 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5489 }
5490
5491 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005492 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005493 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005494 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5495 * {@link #LAYOUT_DIRECTION_RTL},
5496 * {@link #LAYOUT_DIRECTION_INHERIT} or
5497 * {@link #LAYOUT_DIRECTION_LOCALE}.
5498 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005499 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005500 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005501 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5502 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5503 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5504 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005505 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005506 public int getLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005507 return (mPrivateFlags2 & LAYOUT_DIRECTION_MASK) >> LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005508 }
5509
5510 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005511 * Set the layout direction for this view. This will propagate a reset of layout direction
5512 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005513 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005514 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
5515 * {@link #LAYOUT_DIRECTION_RTL},
5516 * {@link #LAYOUT_DIRECTION_INHERIT} or
5517 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005518 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005519 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005520 */
5521 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005522 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005523 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005524 // Reset the current layout direction and the resolved one
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005525 mPrivateFlags2 &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07005526 resetResolvedLayoutDirection();
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005527 // Set the new layout direction (filtered) and ask for a layout pass
5528 mPrivateFlags2 |=
5529 ((layoutDirection << LAYOUT_DIRECTION_MASK_SHIFT) & LAYOUT_DIRECTION_MASK);
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07005530 resolvePadding();
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005531 requestLayout();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005532 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005533 }
5534
5535 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005536 * Returns the resolved layout direction for this view.
5537 *
5538 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005539 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005540 */
5541 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005542 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5543 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005544 })
5545 public int getResolvedLayoutDirection() {
Fabrice Di Megliob93911f2012-06-26 19:43:15 -07005546 // The layout direction will be resolved only if needed
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -07005547 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) != LAYOUT_DIRECTION_RESOLVED) {
5548 resolveLayoutDirection();
5549 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07005550 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005551 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
5552 }
5553
5554 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005555 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5556 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005557 *
5558 * @return true if the layout is right-to-left.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005559 */
5560 @ViewDebug.ExportedProperty(category = "layout")
5561 public boolean isLayoutRtl() {
5562 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
5563 }
5564
5565 /**
Adam Powell539ee872012-02-03 19:00:49 -08005566 * Indicates whether the view is currently tracking transient state that the
5567 * app should not need to concern itself with saving and restoring, but that
5568 * the framework should take special note to preserve when possible.
5569 *
Adam Powell785c4472012-05-02 21:25:39 -07005570 * <p>A view with transient state cannot be trivially rebound from an external
5571 * data source, such as an adapter binding item views in a list. This may be
5572 * because the view is performing an animation, tracking user selection
5573 * of content, or similar.</p>
5574 *
Adam Powell539ee872012-02-03 19:00:49 -08005575 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005576 */
5577 @ViewDebug.ExportedProperty(category = "layout")
5578 public boolean hasTransientState() {
5579 return (mPrivateFlags2 & HAS_TRANSIENT_STATE) == HAS_TRANSIENT_STATE;
5580 }
5581
5582 /**
5583 * Set whether this view is currently tracking transient state that the
Chet Haase563d4f22012-04-18 16:20:08 -07005584 * framework should attempt to preserve when possible. This flag is reference counted,
5585 * so every call to setHasTransientState(true) should be paired with a later call
5586 * to setHasTransientState(false).
Adam Powell539ee872012-02-03 19:00:49 -08005587 *
Adam Powell785c4472012-05-02 21:25:39 -07005588 * <p>A view with transient state cannot be trivially rebound from an external
5589 * data source, such as an adapter binding item views in a list. This may be
5590 * because the view is performing an animation, tracking user selection
5591 * of content, or similar.</p>
5592 *
Adam Powell539ee872012-02-03 19:00:49 -08005593 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005594 */
5595 public void setHasTransientState(boolean hasTransientState) {
Chet Haase563d4f22012-04-18 16:20:08 -07005596 mTransientStateCount = hasTransientState ? mTransientStateCount + 1 :
5597 mTransientStateCount - 1;
5598 if (mTransientStateCount < 0) {
5599 mTransientStateCount = 0;
5600 Log.e(VIEW_LOG_TAG, "hasTransientState decremented below 0: " +
5601 "unmatched pair of setHasTransientState calls");
5602 }
5603 if ((hasTransientState && mTransientStateCount == 1) ||
Adam Powell057a5852012-05-11 10:28:38 -07005604 (!hasTransientState && mTransientStateCount == 0)) {
Chet Haase563d4f22012-04-18 16:20:08 -07005605 // update flag if we've just incremented up from 0 or decremented down to 0
5606 mPrivateFlags2 = (mPrivateFlags2 & ~HAS_TRANSIENT_STATE) |
5607 (hasTransientState ? HAS_TRANSIENT_STATE : 0);
5608 if (mParent != null) {
5609 try {
5610 mParent.childHasTransientStateChanged(this, hasTransientState);
5611 } catch (AbstractMethodError e) {
5612 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5613 " does not fully implement ViewParent", e);
5614 }
Adam Powell539ee872012-02-03 19:00:49 -08005615 }
5616 }
5617 }
5618
5619 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005620 * If this view doesn't do any drawing on its own, set this flag to
5621 * allow further optimizations. By default, this flag is not set on
5622 * View, but could be set on some View subclasses such as ViewGroup.
5623 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005624 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5625 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005626 *
5627 * @param willNotDraw whether or not this View draw on its own
5628 */
5629 public void setWillNotDraw(boolean willNotDraw) {
5630 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5631 }
5632
5633 /**
5634 * Returns whether or not this View draws on its own.
5635 *
5636 * @return true if this view has nothing to draw, false otherwise
5637 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005638 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005639 public boolean willNotDraw() {
5640 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
5641 }
5642
5643 /**
5644 * When a View's drawing cache is enabled, drawing is redirected to an
5645 * offscreen bitmap. Some views, like an ImageView, must be able to
5646 * bypass this mechanism if they already draw a single bitmap, to avoid
5647 * unnecessary usage of the memory.
5648 *
5649 * @param willNotCacheDrawing true if this view does not cache its
5650 * drawing, false otherwise
5651 */
5652 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
5653 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
5654 }
5655
5656 /**
5657 * Returns whether or not this View can cache its drawing or not.
5658 *
5659 * @return true if this view does not cache its drawing, false otherwise
5660 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005661 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005662 public boolean willNotCacheDrawing() {
5663 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5664 }
5665
5666 /**
5667 * Indicates whether this view reacts to click events or not.
5668 *
5669 * @return true if the view is clickable, false otherwise
5670 *
5671 * @see #setClickable(boolean)
5672 * @attr ref android.R.styleable#View_clickable
5673 */
5674 @ViewDebug.ExportedProperty
5675 public boolean isClickable() {
5676 return (mViewFlags & CLICKABLE) == CLICKABLE;
5677 }
5678
5679 /**
5680 * Enables or disables click events for this view. When a view
5681 * is clickable it will change its state to "pressed" on every click.
5682 * Subclasses should set the view clickable to visually react to
5683 * user's clicks.
5684 *
5685 * @param clickable true to make the view clickable, false otherwise
5686 *
5687 * @see #isClickable()
5688 * @attr ref android.R.styleable#View_clickable
5689 */
5690 public void setClickable(boolean clickable) {
5691 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5692 }
5693
5694 /**
5695 * Indicates whether this view reacts to long click events or not.
5696 *
5697 * @return true if the view is long clickable, false otherwise
5698 *
5699 * @see #setLongClickable(boolean)
5700 * @attr ref android.R.styleable#View_longClickable
5701 */
5702 public boolean isLongClickable() {
5703 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5704 }
5705
5706 /**
5707 * Enables or disables long click events for this view. When a view is long
5708 * clickable it reacts to the user holding down the button for a longer
5709 * duration than a tap. This event can either launch the listener or a
5710 * context menu.
5711 *
5712 * @param longClickable true to make the view long clickable, false otherwise
5713 * @see #isLongClickable()
5714 * @attr ref android.R.styleable#View_longClickable
5715 */
5716 public void setLongClickable(boolean longClickable) {
5717 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5718 }
5719
5720 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005721 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005722 *
5723 * @see #isClickable()
5724 * @see #setClickable(boolean)
5725 *
5726 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5727 * the View's internal state from a previously set "pressed" state.
5728 */
5729 public void setPressed(boolean pressed) {
Adam Powell035a1fc2012-02-27 15:23:50 -08005730 final boolean needsRefresh = pressed != ((mPrivateFlags & PRESSED) == PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005732 if (pressed) {
5733 mPrivateFlags |= PRESSED;
5734 } else {
5735 mPrivateFlags &= ~PRESSED;
5736 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005737
5738 if (needsRefresh) {
5739 refreshDrawableState();
5740 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005741 dispatchSetPressed(pressed);
5742 }
5743
5744 /**
5745 * Dispatch setPressed to all of this View's children.
5746 *
5747 * @see #setPressed(boolean)
5748 *
5749 * @param pressed The new pressed state
5750 */
5751 protected void dispatchSetPressed(boolean pressed) {
5752 }
5753
5754 /**
5755 * Indicates whether the view is currently in pressed state. Unless
5756 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5757 * the pressed state.
5758 *
Philip Milne6c8ea062012-04-03 17:38:43 -07005759 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005760 * @see #isClickable()
5761 * @see #setClickable(boolean)
5762 *
5763 * @return true if the view is currently pressed, false otherwise
5764 */
5765 public boolean isPressed() {
5766 return (mPrivateFlags & PRESSED) == PRESSED;
5767 }
5768
5769 /**
5770 * Indicates whether this view will save its state (that is,
5771 * whether its {@link #onSaveInstanceState} method will be called).
5772 *
5773 * @return Returns true if the view state saving is enabled, else false.
5774 *
5775 * @see #setSaveEnabled(boolean)
5776 * @attr ref android.R.styleable#View_saveEnabled
5777 */
5778 public boolean isSaveEnabled() {
5779 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5780 }
5781
5782 /**
5783 * Controls whether the saving of this view's state is
5784 * enabled (that is, whether its {@link #onSaveInstanceState} method
5785 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005786 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005787 * for its state to be saved. This flag can only disable the
5788 * saving of this view; any child views may still have their state saved.
5789 *
5790 * @param enabled Set to false to <em>disable</em> state saving, or true
5791 * (the default) to allow it.
5792 *
5793 * @see #isSaveEnabled()
5794 * @see #setId(int)
5795 * @see #onSaveInstanceState()
5796 * @attr ref android.R.styleable#View_saveEnabled
5797 */
5798 public void setSaveEnabled(boolean enabled) {
5799 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5800 }
5801
Jeff Brown85a31762010-09-01 17:01:00 -07005802 /**
5803 * Gets whether the framework should discard touches when the view's
5804 * window is obscured by another visible window.
5805 * Refer to the {@link View} security documentation for more details.
5806 *
5807 * @return True if touch filtering is enabled.
5808 *
5809 * @see #setFilterTouchesWhenObscured(boolean)
5810 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5811 */
5812 @ViewDebug.ExportedProperty
5813 public boolean getFilterTouchesWhenObscured() {
5814 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5815 }
5816
5817 /**
5818 * Sets whether the framework should discard touches when the view's
5819 * window is obscured by another visible window.
5820 * Refer to the {@link View} security documentation for more details.
5821 *
5822 * @param enabled True if touch filtering should be enabled.
5823 *
5824 * @see #getFilterTouchesWhenObscured
5825 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5826 */
5827 public void setFilterTouchesWhenObscured(boolean enabled) {
5828 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5829 FILTER_TOUCHES_WHEN_OBSCURED);
5830 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005831
5832 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005833 * Indicates whether the entire hierarchy under this view will save its
5834 * state when a state saving traversal occurs from its parent. The default
5835 * is true; if false, these views will not be saved unless
5836 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5837 *
5838 * @return Returns true if the view state saving from parent is enabled, else false.
5839 *
5840 * @see #setSaveFromParentEnabled(boolean)
5841 */
5842 public boolean isSaveFromParentEnabled() {
5843 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5844 }
5845
5846 /**
5847 * Controls whether the entire hierarchy under this view will save its
5848 * state when a state saving traversal occurs from its parent. The default
5849 * is true; if false, these views will not be saved unless
5850 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5851 *
5852 * @param enabled Set to false to <em>disable</em> state saving, or true
5853 * (the default) to allow it.
5854 *
5855 * @see #isSaveFromParentEnabled()
5856 * @see #setId(int)
5857 * @see #onSaveInstanceState()
5858 */
5859 public void setSaveFromParentEnabled(boolean enabled) {
5860 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5861 }
5862
5863
5864 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005865 * Returns whether this View is able to take focus.
5866 *
5867 * @return True if this view can take focus, or false otherwise.
5868 * @attr ref android.R.styleable#View_focusable
5869 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005870 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005871 public final boolean isFocusable() {
5872 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
5873 }
5874
5875 /**
5876 * When a view is focusable, it may not want to take focus when in touch mode.
5877 * For example, a button would like focus when the user is navigating via a D-pad
5878 * so that the user can click on it, but once the user starts touching the screen,
5879 * the button shouldn't take focus
5880 * @return Whether the view is focusable in touch mode.
5881 * @attr ref android.R.styleable#View_focusableInTouchMode
5882 */
5883 @ViewDebug.ExportedProperty
5884 public final boolean isFocusableInTouchMode() {
5885 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5886 }
5887
5888 /**
5889 * Find the nearest view in the specified direction that can take focus.
5890 * This does not actually give focus to that view.
5891 *
5892 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5893 *
5894 * @return The nearest focusable in the specified direction, or null if none
5895 * can be found.
5896 */
5897 public View focusSearch(int direction) {
5898 if (mParent != null) {
5899 return mParent.focusSearch(this, direction);
5900 } else {
5901 return null;
5902 }
5903 }
5904
5905 /**
5906 * This method is the last chance for the focused view and its ancestors to
5907 * respond to an arrow key. This is called when the focused view did not
5908 * consume the key internally, nor could the view system find a new view in
5909 * the requested direction to give focus to.
5910 *
5911 * @param focused The currently focused view.
5912 * @param direction The direction focus wants to move. One of FOCUS_UP,
5913 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5914 * @return True if the this view consumed this unhandled move.
5915 */
5916 public boolean dispatchUnhandledMove(View focused, int direction) {
5917 return false;
5918 }
5919
5920 /**
5921 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005922 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005923 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005924 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5925 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005926 * @return The user specified next view, or null if there is none.
5927 */
5928 View findUserSetNextFocus(View root, int direction) {
5929 switch (direction) {
5930 case FOCUS_LEFT:
5931 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005932 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005933 case FOCUS_RIGHT:
5934 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005935 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005936 case FOCUS_UP:
5937 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005938 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005939 case FOCUS_DOWN:
5940 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005941 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005942 case FOCUS_FORWARD:
5943 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005944 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005945 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08005946 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08005947 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005948 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005949 @Override
5950 public boolean apply(View t) {
5951 return t.mNextFocusForwardId == id;
5952 }
5953 });
5954 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005955 }
5956 return null;
5957 }
5958
Jeff Brown4dfbec22011-08-15 14:55:37 -07005959 private View findViewInsideOutShouldExist(View root, final int childViewId) {
5960 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
5961 @Override
5962 public boolean apply(View t) {
5963 return t.mID == childViewId;
5964 }
5965 });
5966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005967 if (result == null) {
5968 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
5969 + "by user for id " + childViewId);
5970 }
5971 return result;
5972 }
5973
5974 /**
5975 * Find and return all focusable views that are descendants of this view,
5976 * possibly including this view if it is focusable itself.
5977 *
5978 * @param direction The direction of the focus
5979 * @return A list of focusable views
5980 */
5981 public ArrayList<View> getFocusables(int direction) {
5982 ArrayList<View> result = new ArrayList<View>(24);
5983 addFocusables(result, direction);
5984 return result;
5985 }
5986
5987 /**
5988 * Add any focusable views that are descendants of this view (possibly
5989 * including this view if it is focusable itself) to views. If we are in touch mode,
5990 * only add views that are also focusable in touch mode.
5991 *
5992 * @param views Focusable views found so far
5993 * @param direction The direction of the focus
5994 */
5995 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005996 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
5997 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005998
svetoslavganov75986cf2009-05-14 22:28:01 -07005999 /**
6000 * Adds any focusable views that are descendants of this view (possibly
6001 * including this view if it is focusable itself) to views. This method
6002 * adds all focusable views regardless if we are in touch mode or
Svetoslav Ganov42138042012-03-20 11:51:39 -07006003 * only views focusable in touch mode if we are in touch mode or
6004 * only views that can take accessibility focus if accessibility is enabeld
6005 * depending on the focusable mode paramater.
svetoslavganov75986cf2009-05-14 22:28:01 -07006006 *
6007 * @param views Focusable views found so far or null if all we are interested is
6008 * the number of focusables.
6009 * @param direction The direction of the focus.
6010 * @param focusableMode The type of focusables to be added.
6011 *
6012 * @see #FOCUSABLES_ALL
6013 * @see #FOCUSABLES_TOUCH_MODE
6014 */
6015 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006016 if (views == null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006017 return;
6018 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006019 if (!isFocusable()) {
6020 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07006021 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006022 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE
6023 && isInTouchMode() && !isFocusableInTouchMode()) {
6024 return;
6025 }
6026 views.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006027 }
6028
6029 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006030 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006031 * The search is performed by either the text that the View renders or the content
6032 * description that describes the view for accessibility purposes and the view does
6033 * not render or both. Clients can specify how the search is to be performed via
6034 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
6035 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006036 *
6037 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006038 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07006039 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006040 * @see #FIND_VIEWS_WITH_TEXT
6041 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
6042 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006043 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006044 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07006045 if (getAccessibilityNodeProvider() != null) {
6046 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
6047 outViews.add(this);
6048 }
6049 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006050 && (searched != null && searched.length() > 0)
6051 && (mContentDescription != null && mContentDescription.length() > 0)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006052 String searchedLowerCase = searched.toString().toLowerCase();
6053 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
6054 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
6055 outViews.add(this);
6056 }
6057 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006058 }
6059
6060 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006061 * Find and return all touchable views that are descendants of this view,
6062 * possibly including this view if it is touchable itself.
6063 *
6064 * @return A list of touchable views
6065 */
6066 public ArrayList<View> getTouchables() {
6067 ArrayList<View> result = new ArrayList<View>();
6068 addTouchables(result);
6069 return result;
6070 }
6071
6072 /**
6073 * Add any touchable views that are descendants of this view (possibly
6074 * including this view if it is touchable itself) to views.
6075 *
6076 * @param views Touchable views found so far
6077 */
6078 public void addTouchables(ArrayList<View> views) {
6079 final int viewFlags = mViewFlags;
6080
6081 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
6082 && (viewFlags & ENABLED_MASK) == ENABLED) {
6083 views.add(this);
6084 }
6085 }
6086
6087 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006088 * Returns whether this View is accessibility focused.
6089 *
6090 * @return True if this View is accessibility focused.
6091 */
6092 boolean isAccessibilityFocused() {
6093 return (mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0;
6094 }
6095
6096 /**
6097 * Call this to try to give accessibility focus to this view.
6098 *
6099 * A view will not actually take focus if {@link AccessibilityManager#isEnabled()}
6100 * returns false or the view is no visible or the view already has accessibility
6101 * focus.
6102 *
6103 * See also {@link #focusSearch(int)}, which is what you call to say that you
6104 * have focus, and you want your parent to look for the next one.
6105 *
6106 * @return Whether this view actually took accessibility focus.
6107 *
6108 * @hide
6109 */
6110 public boolean requestAccessibilityFocus() {
Svetoslav Ganov07b726c2012-04-30 12:24:57 -07006111 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
6112 if (!manager.isEnabled() || !manager.isTouchExplorationEnabled()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006113 return false;
6114 }
6115 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6116 return false;
6117 }
6118 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) == 0) {
6119 mPrivateFlags2 |= ACCESSIBILITY_FOCUSED;
6120 ViewRootImpl viewRootImpl = getViewRootImpl();
6121 if (viewRootImpl != null) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006122 viewRootImpl.setAccessibilityFocus(this, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07006123 }
6124 invalidate();
6125 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
6126 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006127 return true;
6128 }
6129 return false;
6130 }
6131
6132 /**
6133 * Call this to try to clear accessibility focus of this view.
6134 *
6135 * See also {@link #focusSearch(int)}, which is what you call to say that you
6136 * have focus, and you want your parent to look for the next one.
6137 *
6138 * @hide
6139 */
6140 public void clearAccessibilityFocus() {
Svetoslav Ganov791fd312012-05-14 15:12:30 -07006141 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6142 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006143 invalidate();
6144 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
6145 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006146 }
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006147 // Clear the global reference of accessibility focus if this
6148 // view or any of its descendants had accessibility focus.
6149 ViewRootImpl viewRootImpl = getViewRootImpl();
6150 if (viewRootImpl != null) {
6151 View focusHost = viewRootImpl.getAccessibilityFocusedHost();
6152 if (focusHost != null && ViewRootImpl.isViewDescendantOf(focusHost, this)) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006153 viewRootImpl.setAccessibilityFocus(null, null);
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006154 }
6155 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006156 }
6157
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07006158 private void sendAccessibilityHoverEvent(int eventType) {
6159 // Since we are not delivering to a client accessibility events from not
6160 // important views (unless the clinet request that) we need to fire the
6161 // event from the deepest view exposed to the client. As a consequence if
6162 // the user crosses a not exposed view the client will see enter and exit
6163 // of the exposed predecessor followed by and enter and exit of that same
6164 // predecessor when entering and exiting the not exposed descendant. This
6165 // is fine since the client has a clear idea which view is hovered at the
6166 // price of a couple more events being sent. This is a simple and
6167 // working solution.
6168 View source = this;
6169 while (true) {
6170 if (source.includeForAccessibility()) {
6171 source.sendAccessibilityEvent(eventType);
6172 return;
6173 }
6174 ViewParent parent = source.getParent();
6175 if (parent instanceof View) {
6176 source = (View) parent;
6177 } else {
6178 return;
6179 }
6180 }
6181 }
6182
Svetoslav Ganov42138042012-03-20 11:51:39 -07006183 /**
6184 * Clears accessibility focus without calling any callback methods
6185 * normally invoked in {@link #clearAccessibilityFocus()}. This method
6186 * is used for clearing accessibility focus when giving this focus to
6187 * another view.
6188 */
6189 void clearAccessibilityFocusNoCallbacks() {
6190 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6191 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
6192 invalidate();
6193 }
6194 }
6195
6196 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006197 * Call this to try to give focus to a specific view or to one of its
6198 * descendants.
6199 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006200 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6201 * false), or if it is focusable and it is not focusable in touch mode
6202 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006203 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006204 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006205 * have focus, and you want your parent to look for the next one.
6206 *
6207 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
6208 * {@link #FOCUS_DOWN} and <code>null</code>.
6209 *
6210 * @return Whether this view or one of its descendants actually took focus.
6211 */
6212 public final boolean requestFocus() {
6213 return requestFocus(View.FOCUS_DOWN);
6214 }
6215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006216 /**
6217 * Call this to try to give focus to a specific view or to one of its
6218 * descendants and give it a hint about what direction focus is heading.
6219 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006220 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6221 * false), or if it is focusable and it is not focusable in touch mode
6222 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006223 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006224 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006225 * have focus, and you want your parent to look for the next one.
6226 *
6227 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
6228 * <code>null</code> set for the previously focused rectangle.
6229 *
6230 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6231 * @return Whether this view or one of its descendants actually took focus.
6232 */
6233 public final boolean requestFocus(int direction) {
6234 return requestFocus(direction, null);
6235 }
6236
6237 /**
6238 * Call this to try to give focus to a specific view or to one of its descendants
6239 * and give it hints about the direction and a specific rectangle that the focus
6240 * is coming from. The rectangle can help give larger views a finer grained hint
6241 * about where focus is coming from, and therefore, where to show selection, or
6242 * forward focus change internally.
6243 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006244 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6245 * false), or if it is focusable and it is not focusable in touch mode
6246 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006247 *
6248 * A View will not take focus if it is not visible.
6249 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006250 * A View will not take focus if one of its parents has
6251 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
6252 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006253 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006254 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006255 * have focus, and you want your parent to look for the next one.
6256 *
6257 * You may wish to override this method if your custom {@link View} has an internal
6258 * {@link View} that it wishes to forward the request to.
6259 *
6260 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6261 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
6262 * to give a finer grained hint about where focus is coming from. May be null
6263 * if there is no hint.
6264 * @return Whether this view or one of its descendants actually took focus.
6265 */
6266 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006267 return requestFocusNoSearch(direction, previouslyFocusedRect);
6268 }
6269
6270 private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006271 // need to be focusable
6272 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
6273 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6274 return false;
6275 }
6276
6277 // need to be focusable in touch mode if in touch mode
6278 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006279 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
6280 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006281 }
6282
6283 // need to not have any parents blocking us
6284 if (hasAncestorThatBlocksDescendantFocus()) {
6285 return false;
6286 }
6287
6288 handleFocusGainInternal(direction, previouslyFocusedRect);
6289 return true;
6290 }
6291
6292 /**
6293 * Call this to try to give focus to a specific view or to one of its descendants. This is a
6294 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
6295 * touch mode to request focus when they are touched.
6296 *
6297 * @return Whether this view or one of its descendants actually took focus.
6298 *
6299 * @see #isInTouchMode()
6300 *
6301 */
6302 public final boolean requestFocusFromTouch() {
6303 // Leave touch mode if we need to
6304 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006305 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07006306 if (viewRoot != null) {
6307 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006308 }
6309 }
6310 return requestFocus(View.FOCUS_DOWN);
6311 }
6312
6313 /**
6314 * @return Whether any ancestor of this view blocks descendant focus.
6315 */
6316 private boolean hasAncestorThatBlocksDescendantFocus() {
6317 ViewParent ancestor = mParent;
6318 while (ancestor instanceof ViewGroup) {
6319 final ViewGroup vgAncestor = (ViewGroup) ancestor;
6320 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
6321 return true;
6322 } else {
6323 ancestor = vgAncestor.getParent();
6324 }
6325 }
6326 return false;
6327 }
6328
6329 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006330 * Gets the mode for determining whether this View is important for accessibility
6331 * which is if it fires accessibility events and if it is reported to
6332 * accessibility services that query the screen.
6333 *
6334 * @return The mode for determining whether a View is important for accessibility.
6335 *
6336 * @attr ref android.R.styleable#View_importantForAccessibility
6337 *
6338 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6339 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6340 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6341 */
6342 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07006343 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_AUTO, to = "auto"),
6344 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_YES, to = "yes"),
6345 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_NO, to = "no")
Svetoslav Ganov42138042012-03-20 11:51:39 -07006346 })
6347 public int getImportantForAccessibility() {
6348 return (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6349 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6350 }
6351
6352 /**
6353 * Sets how to determine whether this view is important for accessibility
6354 * which is if it fires accessibility events and if it is reported to
6355 * accessibility services that query the screen.
6356 *
6357 * @param mode How to determine whether this view is important for accessibility.
6358 *
6359 * @attr ref android.R.styleable#View_importantForAccessibility
6360 *
6361 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6362 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6363 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6364 */
6365 public void setImportantForAccessibility(int mode) {
6366 if (mode != getImportantForAccessibility()) {
6367 mPrivateFlags2 &= ~IMPORTANT_FOR_ACCESSIBILITY_MASK;
6368 mPrivateFlags2 |= (mode << IMPORTANT_FOR_ACCESSIBILITY_SHIFT)
6369 & IMPORTANT_FOR_ACCESSIBILITY_MASK;
6370 notifyAccessibilityStateChanged();
6371 }
6372 }
6373
6374 /**
6375 * Gets whether this view should be exposed for accessibility.
6376 *
6377 * @return Whether the view is exposed for accessibility.
6378 *
6379 * @hide
6380 */
6381 public boolean isImportantForAccessibility() {
6382 final int mode = (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6383 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6384 switch (mode) {
6385 case IMPORTANT_FOR_ACCESSIBILITY_YES:
6386 return true;
6387 case IMPORTANT_FOR_ACCESSIBILITY_NO:
6388 return false;
6389 case IMPORTANT_FOR_ACCESSIBILITY_AUTO:
Svetoslav Ganov34caec92012-07-19 18:07:58 -07006390 return isActionableForAccessibility() || hasListenersForAccessibility()
6391 || getAccessibilityNodeProvider() != null;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006392 default:
6393 throw new IllegalArgumentException("Unknow important for accessibility mode: "
6394 + mode);
6395 }
6396 }
6397
6398 /**
6399 * Gets the parent for accessibility purposes. Note that the parent for
6400 * accessibility is not necessary the immediate parent. It is the first
6401 * predecessor that is important for accessibility.
6402 *
6403 * @return The parent for accessibility purposes.
6404 */
6405 public ViewParent getParentForAccessibility() {
6406 if (mParent instanceof View) {
6407 View parentView = (View) mParent;
6408 if (parentView.includeForAccessibility()) {
6409 return mParent;
6410 } else {
6411 return mParent.getParentForAccessibility();
6412 }
6413 }
6414 return null;
6415 }
6416
6417 /**
6418 * Adds the children of a given View for accessibility. Since some Views are
6419 * not important for accessibility the children for accessibility are not
6420 * necessarily direct children of the riew, rather they are the first level of
6421 * descendants important for accessibility.
6422 *
6423 * @param children The list of children for accessibility.
6424 */
6425 public void addChildrenForAccessibility(ArrayList<View> children) {
6426 if (includeForAccessibility()) {
6427 children.add(this);
6428 }
6429 }
6430
6431 /**
6432 * Whether to regard this view for accessibility. A view is regarded for
6433 * accessibility if it is important for accessibility or the querying
6434 * accessibility service has explicitly requested that view not
6435 * important for accessibility are regarded.
6436 *
6437 * @return Whether to regard the view for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006438 *
6439 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006440 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006441 public boolean includeForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006442 if (mAttachInfo != null) {
Romain Guyf0af1d52012-07-11 18:31:21 -07006443 return mAttachInfo.mIncludeNotImportantViews || isImportantForAccessibility();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006444 }
6445 return false;
6446 }
6447
6448 /**
6449 * Returns whether the View is considered actionable from
6450 * accessibility perspective. Such view are important for
6451 * accessiiblity.
6452 *
6453 * @return True if the view is actionable for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006454 *
6455 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006456 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006457 public boolean isActionableForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006458 return (isClickable() || isLongClickable() || isFocusable());
6459 }
6460
6461 /**
6462 * Returns whether the View has registered callbacks wich makes it
6463 * important for accessiiblity.
6464 *
6465 * @return True if the view is actionable for accessibility.
6466 */
6467 private boolean hasListenersForAccessibility() {
6468 ListenerInfo info = getListenerInfo();
6469 return mTouchDelegate != null || info.mOnKeyListener != null
6470 || info.mOnTouchListener != null || info.mOnGenericMotionListener != null
6471 || info.mOnHoverListener != null || info.mOnDragListener != null;
6472 }
6473
6474 /**
6475 * Notifies accessibility services that some view's important for
6476 * accessibility state has changed. Note that such notifications
6477 * are made at most once every
6478 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}
6479 * to avoid unnecessary load to the system. Also once a view has
6480 * made a notifucation this method is a NOP until the notification has
6481 * been sent to clients.
6482 *
6483 * @hide
6484 *
6485 * TODO: Makse sure this method is called for any view state change
6486 * that is interesting for accessilility purposes.
6487 */
6488 public void notifyAccessibilityStateChanged() {
Svetoslav Ganovc406be92012-05-11 16:12:32 -07006489 if (!AccessibilityManager.getInstance(mContext).isEnabled()) {
6490 return;
6491 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006492 if ((mPrivateFlags2 & ACCESSIBILITY_STATE_CHANGED) == 0) {
6493 mPrivateFlags2 |= ACCESSIBILITY_STATE_CHANGED;
6494 if (mParent != null) {
6495 mParent.childAccessibilityStateChanged(this);
6496 }
6497 }
6498 }
6499
6500 /**
6501 * Reset the state indicating the this view has requested clients
6502 * interested in its accessiblity state to be notified.
6503 *
6504 * @hide
6505 */
6506 public void resetAccessibilityStateChanged() {
6507 mPrivateFlags2 &= ~ACCESSIBILITY_STATE_CHANGED;
6508 }
6509
6510 /**
6511 * Performs the specified accessibility action on the view. For
6512 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
alanv8eeefef2012-05-07 16:57:53 -07006513 * <p>
6514 * If an {@link AccessibilityDelegate} has been specified via calling
6515 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
6516 * {@link AccessibilityDelegate#performAccessibilityAction(View, int, Bundle)}
6517 * is responsible for handling this call.
6518 * </p>
Svetoslav Ganov42138042012-03-20 11:51:39 -07006519 *
6520 * @param action The action to perform.
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006521 * @param arguments Optional action arguments.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006522 * @return Whether the action was performed.
6523 */
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006524 public boolean performAccessibilityAction(int action, Bundle arguments) {
alanv8eeefef2012-05-07 16:57:53 -07006525 if (mAccessibilityDelegate != null) {
6526 return mAccessibilityDelegate.performAccessibilityAction(this, action, arguments);
6527 } else {
6528 return performAccessibilityActionInternal(action, arguments);
6529 }
6530 }
6531
6532 /**
6533 * @see #performAccessibilityAction(int, Bundle)
6534 *
6535 * Note: Called from the default {@link AccessibilityDelegate}.
6536 */
6537 boolean performAccessibilityActionInternal(int action, Bundle arguments) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006538 switch (action) {
6539 case AccessibilityNodeInfo.ACTION_CLICK: {
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006540 if (isClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006541 return performClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006542 }
6543 } break;
6544 case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
6545 if (isLongClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006546 return performLongClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006547 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006548 } break;
6549 case AccessibilityNodeInfo.ACTION_FOCUS: {
6550 if (!hasFocus()) {
6551 // Get out of touch mode since accessibility
6552 // wants to move focus around.
6553 getViewRootImpl().ensureTouchMode(false);
6554 return requestFocus();
6555 }
6556 } break;
6557 case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS: {
6558 if (hasFocus()) {
6559 clearFocus();
6560 return !isFocused();
6561 }
6562 } break;
6563 case AccessibilityNodeInfo.ACTION_SELECT: {
6564 if (!isSelected()) {
6565 setSelected(true);
6566 return isSelected();
6567 }
6568 } break;
6569 case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
6570 if (isSelected()) {
6571 setSelected(false);
6572 return !isSelected();
6573 }
6574 } break;
6575 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07006576 if (!isAccessibilityFocused()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006577 return requestAccessibilityFocus();
6578 }
6579 } break;
6580 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
6581 if (isAccessibilityFocused()) {
6582 clearAccessibilityFocus();
6583 return true;
6584 }
6585 } break;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006586 case AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY: {
6587 if (arguments != null) {
6588 final int granularity = arguments.getInt(
6589 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6590 return nextAtGranularity(granularity);
6591 }
6592 } break;
6593 case AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY: {
6594 if (arguments != null) {
6595 final int granularity = arguments.getInt(
6596 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6597 return previousAtGranularity(granularity);
6598 }
6599 } break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006600 }
6601 return false;
6602 }
6603
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006604 private boolean nextAtGranularity(int granularity) {
6605 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006606 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006607 return false;
6608 }
6609 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6610 if (iterator == null) {
6611 return false;
6612 }
6613 final int current = getAccessibilityCursorPosition();
6614 final int[] range = iterator.following(current);
6615 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006616 return false;
6617 }
6618 final int start = range[0];
6619 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006620 setAccessibilityCursorPosition(end);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006621 sendViewTextTraversedAtGranularityEvent(
6622 AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY,
6623 granularity, start, end);
6624 return true;
6625 }
6626
6627 private boolean previousAtGranularity(int granularity) {
6628 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006629 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006630 return false;
6631 }
6632 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6633 if (iterator == null) {
6634 return false;
6635 }
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006636 int current = getAccessibilityCursorPosition();
6637 if (current == ACCESSIBILITY_CURSOR_POSITION_UNDEFINED) {
6638 current = text.length();
6639 } else if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
6640 // When traversing by character we always put the cursor after the character
6641 // to ease edit and have to compensate before asking the for previous segment.
6642 current--;
6643 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006644 final int[] range = iterator.preceding(current);
6645 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006646 return false;
6647 }
6648 final int start = range[0];
6649 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006650 // Always put the cursor after the character to ease edit.
6651 if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
6652 setAccessibilityCursorPosition(end);
6653 } else {
6654 setAccessibilityCursorPosition(start);
6655 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006656 sendViewTextTraversedAtGranularityEvent(
6657 AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
6658 granularity, start, end);
6659 return true;
6660 }
6661
6662 /**
6663 * Gets the text reported for accessibility purposes.
6664 *
6665 * @return The accessibility text.
6666 *
6667 * @hide
6668 */
6669 public CharSequence getIterableTextForAccessibility() {
6670 return mContentDescription;
6671 }
6672
6673 /**
6674 * @hide
6675 */
6676 public int getAccessibilityCursorPosition() {
6677 return mAccessibilityCursorPosition;
6678 }
6679
6680 /**
6681 * @hide
6682 */
6683 public void setAccessibilityCursorPosition(int position) {
6684 mAccessibilityCursorPosition = position;
6685 }
6686
6687 private void sendViewTextTraversedAtGranularityEvent(int action, int granularity,
6688 int fromIndex, int toIndex) {
6689 if (mParent == null) {
6690 return;
6691 }
6692 AccessibilityEvent event = AccessibilityEvent.obtain(
6693 AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY);
6694 onInitializeAccessibilityEvent(event);
6695 onPopulateAccessibilityEvent(event);
6696 event.setFromIndex(fromIndex);
6697 event.setToIndex(toIndex);
6698 event.setAction(action);
6699 event.setMovementGranularity(granularity);
6700 mParent.requestSendAccessibilityEvent(this, event);
6701 }
6702
6703 /**
6704 * @hide
6705 */
6706 public TextSegmentIterator getIteratorForGranularity(int granularity) {
6707 switch (granularity) {
6708 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER: {
6709 CharSequence text = getIterableTextForAccessibility();
6710 if (text != null && text.length() > 0) {
6711 CharacterTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07006712 CharacterTextSegmentIterator.getInstance(
6713 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006714 iterator.initialize(text.toString());
6715 return iterator;
6716 }
6717 } break;
6718 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD: {
6719 CharSequence text = getIterableTextForAccessibility();
6720 if (text != null && text.length() > 0) {
6721 WordTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07006722 WordTextSegmentIterator.getInstance(
6723 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006724 iterator.initialize(text.toString());
6725 return iterator;
6726 }
6727 } break;
6728 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH: {
6729 CharSequence text = getIterableTextForAccessibility();
6730 if (text != null && text.length() > 0) {
6731 ParagraphTextSegmentIterator iterator =
6732 ParagraphTextSegmentIterator.getInstance();
6733 iterator.initialize(text.toString());
6734 return iterator;
6735 }
6736 } break;
6737 }
6738 return null;
6739 }
6740
Svetoslav Ganov42138042012-03-20 11:51:39 -07006741 /**
Romain Guya440b002010-02-24 15:57:54 -08006742 * @hide
6743 */
6744 public void dispatchStartTemporaryDetach() {
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07006745 clearAccessibilityFocus();
Romain Guy38c2ece2012-05-24 14:20:56 -07006746 clearDisplayList();
6747
Romain Guya440b002010-02-24 15:57:54 -08006748 onStartTemporaryDetach();
6749 }
6750
6751 /**
6752 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006753 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
6754 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08006755 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006756 */
6757 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08006758 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08006759 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08006760 }
6761
6762 /**
6763 * @hide
6764 */
6765 public void dispatchFinishTemporaryDetach() {
6766 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006767 }
Romain Guy8506ab42009-06-11 17:35:47 -07006768
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006769 /**
6770 * Called after {@link #onStartTemporaryDetach} when the container is done
6771 * changing the view.
6772 */
6773 public void onFinishTemporaryDetach() {
6774 }
Romain Guy8506ab42009-06-11 17:35:47 -07006775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006776 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006777 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
6778 * for this view's window. Returns null if the view is not currently attached
6779 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07006780 * just use the standard high-level event callbacks like
6781 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006782 */
6783 public KeyEvent.DispatcherState getKeyDispatcherState() {
6784 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
6785 }
Joe Malin32736f02011-01-19 16:14:20 -08006786
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006787 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006788 * Dispatch a key event before it is processed by any input method
6789 * associated with the view hierarchy. This can be used to intercept
6790 * key events in special situations before the IME consumes them; a
6791 * typical example would be handling the BACK key to update the application's
6792 * UI instead of allowing the IME to see it and close itself.
6793 *
6794 * @param event The key event to be dispatched.
6795 * @return True if the event was handled, false otherwise.
6796 */
6797 public boolean dispatchKeyEventPreIme(KeyEvent event) {
6798 return onKeyPreIme(event.getKeyCode(), event);
6799 }
6800
6801 /**
6802 * Dispatch a key event to the next view on the focus path. This path runs
6803 * from the top of the view tree down to the currently focused view. If this
6804 * view has focus, it will dispatch to itself. Otherwise it will dispatch
6805 * the next node down the focus path. This method also fires any key
6806 * listeners.
6807 *
6808 * @param event The key event to be dispatched.
6809 * @return True if the event was handled, false otherwise.
6810 */
6811 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006812 if (mInputEventConsistencyVerifier != null) {
6813 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
6814 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006815
Jeff Brown21bc5c92011-02-28 18:27:14 -08006816 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07006817 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006818 ListenerInfo li = mListenerInfo;
6819 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6820 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006821 return true;
6822 }
6823
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006824 if (event.dispatch(this, mAttachInfo != null
6825 ? mAttachInfo.mKeyDispatchState : null, this)) {
6826 return true;
6827 }
6828
6829 if (mInputEventConsistencyVerifier != null) {
6830 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6831 }
6832 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006833 }
6834
6835 /**
6836 * Dispatches a key shortcut event.
6837 *
6838 * @param event The key event to be dispatched.
6839 * @return True if the event was handled by the view, false otherwise.
6840 */
6841 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
6842 return onKeyShortcut(event.getKeyCode(), event);
6843 }
6844
6845 /**
6846 * Pass the touch screen motion event down to the target view, or this
6847 * view if it is the target.
6848 *
6849 * @param event The motion event to be dispatched.
6850 * @return True if the event was handled by the view, false otherwise.
6851 */
6852 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006853 if (mInputEventConsistencyVerifier != null) {
6854 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
6855 }
6856
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006857 if (onFilterTouchEventForSecurity(event)) {
6858 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006859 ListenerInfo li = mListenerInfo;
6860 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6861 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006862 return true;
6863 }
6864
6865 if (onTouchEvent(event)) {
6866 return true;
6867 }
Jeff Brown85a31762010-09-01 17:01:00 -07006868 }
6869
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006870 if (mInputEventConsistencyVerifier != null) {
6871 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006872 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006873 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006874 }
6875
6876 /**
Jeff Brown85a31762010-09-01 17:01:00 -07006877 * Filter the touch event to apply security policies.
6878 *
6879 * @param event The motion event to be filtered.
6880 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08006881 *
Jeff Brown85a31762010-09-01 17:01:00 -07006882 * @see #getFilterTouchesWhenObscured
6883 */
6884 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07006885 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07006886 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
6887 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
6888 // Window is obscured, drop this touch.
6889 return false;
6890 }
6891 return true;
6892 }
6893
6894 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006895 * Pass a trackball motion event down to the focused view.
6896 *
6897 * @param event The motion event to be dispatched.
6898 * @return True if the event was handled by the view, false otherwise.
6899 */
6900 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006901 if (mInputEventConsistencyVerifier != null) {
6902 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
6903 }
6904
Romain Guy02ccac62011-06-24 13:20:23 -07006905 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006906 }
6907
6908 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08006909 * Dispatch a generic motion event.
6910 * <p>
6911 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6912 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08006913 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07006914 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006915 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08006916 *
6917 * @param event The motion event to be dispatched.
6918 * @return True if the event was handled by the view, false otherwise.
6919 */
6920 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006921 if (mInputEventConsistencyVerifier != null) {
6922 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
6923 }
6924
Jeff Browna032cc02011-03-07 16:56:21 -08006925 final int source = event.getSource();
6926 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
6927 final int action = event.getAction();
6928 if (action == MotionEvent.ACTION_HOVER_ENTER
6929 || action == MotionEvent.ACTION_HOVER_MOVE
6930 || action == MotionEvent.ACTION_HOVER_EXIT) {
6931 if (dispatchHoverEvent(event)) {
6932 return true;
6933 }
6934 } else if (dispatchGenericPointerEvent(event)) {
6935 return true;
6936 }
6937 } else if (dispatchGenericFocusedEvent(event)) {
6938 return true;
6939 }
6940
Jeff Brown10b62902011-06-20 16:40:37 -07006941 if (dispatchGenericMotionEventInternal(event)) {
6942 return true;
6943 }
6944
6945 if (mInputEventConsistencyVerifier != null) {
6946 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6947 }
6948 return false;
6949 }
6950
6951 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07006952 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006953 ListenerInfo li = mListenerInfo;
6954 if (li != null && li.mOnGenericMotionListener != null
6955 && (mViewFlags & ENABLED_MASK) == ENABLED
6956 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006957 return true;
6958 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006959
6960 if (onGenericMotionEvent(event)) {
6961 return true;
6962 }
6963
6964 if (mInputEventConsistencyVerifier != null) {
6965 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6966 }
6967 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08006968 }
6969
6970 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006971 * Dispatch a hover event.
6972 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07006973 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07006974 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006975 * </p>
6976 *
6977 * @param event The motion event to be dispatched.
6978 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006979 */
6980 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07006981 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006982 ListenerInfo li = mListenerInfo;
6983 if (li != null && li.mOnHoverListener != null
6984 && (mViewFlags & ENABLED_MASK) == ENABLED
6985 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07006986 return true;
6987 }
6988
Jeff Browna032cc02011-03-07 16:56:21 -08006989 return onHoverEvent(event);
6990 }
6991
6992 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006993 * Returns true if the view has a child to which it has recently sent
6994 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
6995 * it does not have a hovered child, then it must be the innermost hovered view.
6996 * @hide
6997 */
6998 protected boolean hasHoveredChild() {
6999 return false;
7000 }
7001
7002 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007003 * Dispatch a generic motion event to the view under the first pointer.
7004 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007005 * Do not call this method directly.
7006 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007007 * </p>
7008 *
7009 * @param event The motion event to be dispatched.
7010 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007011 */
7012 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
7013 return false;
7014 }
7015
7016 /**
7017 * Dispatch a generic motion event to the currently focused view.
7018 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007019 * Do not call this method directly.
7020 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007021 * </p>
7022 *
7023 * @param event The motion event to be dispatched.
7024 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007025 */
7026 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
7027 return false;
7028 }
7029
7030 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007031 * Dispatch a pointer event.
7032 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007033 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
7034 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
7035 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08007036 * and should not be expected to handle other pointing device features.
7037 * </p>
7038 *
7039 * @param event The motion event to be dispatched.
7040 * @return True if the event was handled by the view, false otherwise.
7041 * @hide
7042 */
7043 public final boolean dispatchPointerEvent(MotionEvent event) {
7044 if (event.isTouchEvent()) {
7045 return dispatchTouchEvent(event);
7046 } else {
7047 return dispatchGenericMotionEvent(event);
7048 }
7049 }
7050
7051 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007052 * Called when the window containing this view gains or loses window focus.
7053 * ViewGroups should override to route to their children.
7054 *
7055 * @param hasFocus True if the window containing this view now has focus,
7056 * false otherwise.
7057 */
7058 public void dispatchWindowFocusChanged(boolean hasFocus) {
7059 onWindowFocusChanged(hasFocus);
7060 }
7061
7062 /**
7063 * Called when the window containing this view gains or loses focus. Note
7064 * that this is separate from view focus: to receive key events, both
7065 * your view and its window must have focus. If a window is displayed
7066 * on top of yours that takes input focus, then your own window will lose
7067 * focus but the view focus will remain unchanged.
7068 *
7069 * @param hasWindowFocus True if the window containing this view now has
7070 * focus, false otherwise.
7071 */
7072 public void onWindowFocusChanged(boolean hasWindowFocus) {
7073 InputMethodManager imm = InputMethodManager.peekInstance();
7074 if (!hasWindowFocus) {
7075 if (isPressed()) {
7076 setPressed(false);
7077 }
7078 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
7079 imm.focusOut(this);
7080 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007081 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08007082 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07007083 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007084 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
7085 imm.focusIn(this);
7086 }
7087 refreshDrawableState();
7088 }
7089
7090 /**
7091 * Returns true if this view is in a window that currently has window focus.
7092 * Note that this is not the same as the view itself having focus.
7093 *
7094 * @return True if this view is in a window that currently has window focus.
7095 */
7096 public boolean hasWindowFocus() {
7097 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
7098 }
7099
7100 /**
Adam Powell326d8082009-12-09 15:10:07 -08007101 * Dispatch a view visibility change down the view hierarchy.
7102 * ViewGroups should override to route to their children.
7103 * @param changedView The view whose visibility changed. Could be 'this' or
7104 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007105 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7106 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007107 */
7108 protected void dispatchVisibilityChanged(View changedView, int visibility) {
7109 onVisibilityChanged(changedView, visibility);
7110 }
7111
7112 /**
7113 * Called when the visibility of the view or an ancestor of the view is changed.
7114 * @param changedView The view whose visibility changed. Could be 'this' or
7115 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007116 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7117 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007118 */
7119 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007120 if (visibility == VISIBLE) {
7121 if (mAttachInfo != null) {
7122 initialAwakenScrollBars();
7123 } else {
7124 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
7125 }
7126 }
Adam Powell326d8082009-12-09 15:10:07 -08007127 }
7128
7129 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08007130 * Dispatch a hint about whether this view is displayed. For instance, when
7131 * a View moves out of the screen, it might receives a display hint indicating
7132 * the view is not displayed. Applications should not <em>rely</em> on this hint
7133 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007134 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007135 * @param hint A hint about whether or not this view is displayed:
7136 * {@link #VISIBLE} or {@link #INVISIBLE}.
7137 */
7138 public void dispatchDisplayHint(int hint) {
7139 onDisplayHint(hint);
7140 }
7141
7142 /**
7143 * Gives this view a hint about whether is displayed or not. For instance, when
7144 * a View moves out of the screen, it might receives a display hint indicating
7145 * the view is not displayed. Applications should not <em>rely</em> on this hint
7146 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007147 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007148 * @param hint A hint about whether or not this view is displayed:
7149 * {@link #VISIBLE} or {@link #INVISIBLE}.
7150 */
7151 protected void onDisplayHint(int hint) {
7152 }
7153
7154 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007155 * Dispatch a window visibility change down the view hierarchy.
7156 * ViewGroups should override to route to their children.
7157 *
7158 * @param visibility The new visibility of the window.
7159 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007160 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007161 */
7162 public void dispatchWindowVisibilityChanged(int visibility) {
7163 onWindowVisibilityChanged(visibility);
7164 }
7165
7166 /**
7167 * Called when the window containing has change its visibility
7168 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
7169 * that this tells you whether or not your window is being made visible
7170 * to the window manager; this does <em>not</em> tell you whether or not
7171 * your window is obscured by other windows on the screen, even if it
7172 * is itself visible.
7173 *
7174 * @param visibility The new visibility of the window.
7175 */
7176 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007177 if (visibility == VISIBLE) {
7178 initialAwakenScrollBars();
7179 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007180 }
7181
7182 /**
7183 * Returns the current visibility of the window this view is attached to
7184 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
7185 *
7186 * @return Returns the current visibility of the view's window.
7187 */
7188 public int getWindowVisibility() {
7189 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
7190 }
7191
7192 /**
7193 * Retrieve the overall visible display size in which the window this view is
7194 * attached to has been positioned in. This takes into account screen
7195 * decorations above the window, for both cases where the window itself
7196 * is being position inside of them or the window is being placed under
7197 * then and covered insets are used for the window to position its content
7198 * inside. In effect, this tells you the available area where content can
7199 * be placed and remain visible to users.
7200 *
7201 * <p>This function requires an IPC back to the window manager to retrieve
7202 * the requested information, so should not be used in performance critical
7203 * code like drawing.
7204 *
7205 * @param outRect Filled in with the visible display frame. If the view
7206 * is not attached to a window, this is simply the raw display size.
7207 */
7208 public void getWindowVisibleDisplayFrame(Rect outRect) {
7209 if (mAttachInfo != null) {
7210 try {
7211 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
7212 } catch (RemoteException e) {
7213 return;
7214 }
7215 // XXX This is really broken, and probably all needs to be done
7216 // in the window manager, and we need to know more about whether
7217 // we want the area behind or in front of the IME.
7218 final Rect insets = mAttachInfo.mVisibleInsets;
7219 outRect.left += insets.left;
7220 outRect.top += insets.top;
7221 outRect.right -= insets.right;
7222 outRect.bottom -= insets.bottom;
7223 return;
7224 }
7225 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07007226 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007227 }
7228
7229 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007230 * Dispatch a notification about a resource configuration change down
7231 * the view hierarchy.
7232 * ViewGroups should override to route to their children.
7233 *
7234 * @param newConfig The new resource configuration.
7235 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007236 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007237 */
7238 public void dispatchConfigurationChanged(Configuration newConfig) {
7239 onConfigurationChanged(newConfig);
7240 }
7241
7242 /**
7243 * Called when the current configuration of the resources being used
7244 * by the application have changed. You can use this to decide when
7245 * to reload resources that can changed based on orientation and other
7246 * configuration characterstics. You only need to use this if you are
7247 * not relying on the normal {@link android.app.Activity} mechanism of
7248 * recreating the activity instance upon a configuration change.
7249 *
7250 * @param newConfig The new resource configuration.
7251 */
7252 protected void onConfigurationChanged(Configuration newConfig) {
7253 }
7254
7255 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007256 * Private function to aggregate all per-view attributes in to the view
7257 * root.
7258 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007259 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7260 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007261 }
7262
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007263 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7264 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08007265 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007266 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007267 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007268 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007269 ListenerInfo li = mListenerInfo;
7270 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007271 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007272 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007273 }
7274 }
7275
7276 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08007277 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007278 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08007279 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
7280 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007281 ai.mRecomputeGlobalAttributes = true;
7282 }
7283 }
7284 }
7285
7286 /**
7287 * Returns whether the device is currently in touch mode. Touch mode is entered
7288 * once the user begins interacting with the device by touch, and affects various
7289 * things like whether focus is always visible to the user.
7290 *
7291 * @return Whether the device is in touch mode.
7292 */
7293 @ViewDebug.ExportedProperty
7294 public boolean isInTouchMode() {
7295 if (mAttachInfo != null) {
7296 return mAttachInfo.mInTouchMode;
7297 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07007298 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007299 }
7300 }
7301
7302 /**
7303 * Returns the context the view is running in, through which it can
7304 * access the current theme, resources, etc.
7305 *
7306 * @return The view's Context.
7307 */
7308 @ViewDebug.CapturedViewProperty
7309 public final Context getContext() {
7310 return mContext;
7311 }
7312
7313 /**
7314 * Handle a key event before it is processed by any input method
7315 * associated with the view hierarchy. This can be used to intercept
7316 * key events in special situations before the IME consumes them; a
7317 * typical example would be handling the BACK key to update the application's
7318 * UI instead of allowing the IME to see it and close itself.
7319 *
7320 * @param keyCode The value in event.getKeyCode().
7321 * @param event Description of the key event.
7322 * @return If you handled the event, return true. If you want to allow the
7323 * event to be handled by the next receiver, return false.
7324 */
7325 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7326 return false;
7327 }
7328
7329 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007330 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7331 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007332 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7333 * is released, if the view is enabled and clickable.
7334 *
Jean Chalard405bc512012-05-29 19:12:34 +09007335 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7336 * although some may elect to do so in some situations. Do not rely on this to
7337 * catch software key presses.
7338 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007339 * @param keyCode A key code that represents the button pressed, from
7340 * {@link android.view.KeyEvent}.
7341 * @param event The KeyEvent object that defines the button action.
7342 */
7343 public boolean onKeyDown(int keyCode, KeyEvent event) {
7344 boolean result = false;
7345
7346 switch (keyCode) {
7347 case KeyEvent.KEYCODE_DPAD_CENTER:
7348 case KeyEvent.KEYCODE_ENTER: {
7349 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7350 return true;
7351 }
7352 // Long clickable items don't necessarily have to be clickable
7353 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7354 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7355 (event.getRepeatCount() == 0)) {
7356 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007357 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007358 return true;
7359 }
7360 break;
7361 }
7362 }
7363 return result;
7364 }
7365
7366 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007367 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7368 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7369 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007370 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7371 * although some may elect to do so in some situations. Do not rely on this to
7372 * catch software key presses.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007373 */
7374 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7375 return false;
7376 }
7377
7378 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007379 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7380 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007381 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7382 * {@link KeyEvent#KEYCODE_ENTER} is released.
Jean Chalard405bc512012-05-29 19:12:34 +09007383 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7384 * although some may elect to do so in some situations. Do not rely on this to
7385 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007386 *
7387 * @param keyCode A key code that represents the button pressed, from
7388 * {@link android.view.KeyEvent}.
7389 * @param event The KeyEvent object that defines the button action.
7390 */
7391 public boolean onKeyUp(int keyCode, KeyEvent event) {
7392 boolean result = false;
7393
7394 switch (keyCode) {
7395 case KeyEvent.KEYCODE_DPAD_CENTER:
7396 case KeyEvent.KEYCODE_ENTER: {
7397 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7398 return true;
7399 }
7400 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7401 setPressed(false);
7402
7403 if (!mHasPerformedLongPress) {
7404 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007405 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007406
7407 result = performClick();
7408 }
7409 }
7410 break;
7411 }
7412 }
7413 return result;
7414 }
7415
7416 /**
7417 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7418 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7419 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007420 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7421 * although some may elect to do so in some situations. Do not rely on this to
7422 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007423 *
7424 * @param keyCode A key code that represents the button pressed, from
7425 * {@link android.view.KeyEvent}.
7426 * @param repeatCount The number of times the action was made.
7427 * @param event The KeyEvent object that defines the button action.
7428 */
7429 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7430 return false;
7431 }
7432
7433 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007434 * Called on the focused view when a key shortcut event is not handled.
7435 * Override this method to implement local key shortcuts for the View.
7436 * Key shortcuts can also be implemented by setting the
7437 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007438 *
7439 * @param keyCode The value in event.getKeyCode().
7440 * @param event Description of the key event.
7441 * @return If you handled the event, return true. If you want to allow the
7442 * event to be handled by the next receiver, return false.
7443 */
7444 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7445 return false;
7446 }
7447
7448 /**
7449 * Check whether the called view is a text editor, in which case it
7450 * would make sense to automatically display a soft input window for
7451 * it. Subclasses should override this if they implement
7452 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007453 * a call on that method would return a non-null InputConnection, and
7454 * they are really a first-class editor that the user would normally
7455 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007456 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007457 * <p>The default implementation always returns false. This does
7458 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7459 * will not be called or the user can not otherwise perform edits on your
7460 * view; it is just a hint to the system that this is not the primary
7461 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007462 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007463 * @return Returns true if this view is a text editor, else false.
7464 */
7465 public boolean onCheckIsTextEditor() {
7466 return false;
7467 }
Romain Guy8506ab42009-06-11 17:35:47 -07007468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007469 /**
7470 * Create a new InputConnection for an InputMethod to interact
7471 * with the view. The default implementation returns null, since it doesn't
7472 * support input methods. You can override this to implement such support.
7473 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007474 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007475 * <p>When implementing this, you probably also want to implement
7476 * {@link #onCheckIsTextEditor()} to indicate you will return a
7477 * non-null InputConnection.
7478 *
7479 * @param outAttrs Fill in with attribute information about the connection.
7480 */
7481 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7482 return null;
7483 }
7484
7485 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007486 * Called by the {@link android.view.inputmethod.InputMethodManager}
7487 * when a view who is not the current
7488 * input connection target is trying to make a call on the manager. The
7489 * default implementation returns false; you can override this to return
7490 * true for certain views if you are performing InputConnection proxying
7491 * to them.
7492 * @param view The View that is making the InputMethodManager call.
7493 * @return Return true to allow the call, false to reject.
7494 */
7495 public boolean checkInputConnectionProxy(View view) {
7496 return false;
7497 }
Romain Guy8506ab42009-06-11 17:35:47 -07007498
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007499 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007500 * Show the context menu for this view. It is not safe to hold on to the
7501 * menu after returning from this method.
7502 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007503 * You should normally not overload this method. Overload
7504 * {@link #onCreateContextMenu(ContextMenu)} or define an
7505 * {@link OnCreateContextMenuListener} to add items to the context menu.
7506 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007507 * @param menu The context menu to populate
7508 */
7509 public void createContextMenu(ContextMenu menu) {
7510 ContextMenuInfo menuInfo = getContextMenuInfo();
7511
7512 // Sets the current menu info so all items added to menu will have
7513 // my extra info set.
7514 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7515
7516 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007517 ListenerInfo li = mListenerInfo;
7518 if (li != null && li.mOnCreateContextMenuListener != null) {
7519 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007520 }
7521
7522 // Clear the extra information so subsequent items that aren't mine don't
7523 // have my extra info.
7524 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7525
7526 if (mParent != null) {
7527 mParent.createContextMenu(menu);
7528 }
7529 }
7530
7531 /**
7532 * Views should implement this if they have extra information to associate
7533 * with the context menu. The return result is supplied as a parameter to
7534 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7535 * callback.
7536 *
7537 * @return Extra information about the item for which the context menu
7538 * should be shown. This information will vary across different
7539 * subclasses of View.
7540 */
7541 protected ContextMenuInfo getContextMenuInfo() {
7542 return null;
7543 }
7544
7545 /**
7546 * Views should implement this if the view itself is going to add items to
7547 * the context menu.
7548 *
7549 * @param menu the context menu to populate
7550 */
7551 protected void onCreateContextMenu(ContextMenu menu) {
7552 }
7553
7554 /**
7555 * Implement this method to handle trackball motion events. The
7556 * <em>relative</em> movement of the trackball since the last event
7557 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
7558 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
7559 * that a movement of 1 corresponds to the user pressing one DPAD key (so
7560 * they will often be fractional values, representing the more fine-grained
7561 * movement information available from a trackball).
7562 *
7563 * @param event The motion event.
7564 * @return True if the event was handled, false otherwise.
7565 */
7566 public boolean onTrackballEvent(MotionEvent event) {
7567 return false;
7568 }
7569
7570 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08007571 * Implement this method to handle generic motion events.
7572 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08007573 * Generic motion events describe joystick movements, mouse hovers, track pad
7574 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08007575 * {@link MotionEvent#getSource() source} of the motion event specifies
7576 * the class of input that was received. Implementations of this method
7577 * must examine the bits in the source before processing the event.
7578 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007579 * </p><p>
7580 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7581 * are delivered to the view under the pointer. All other generic motion events are
7582 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08007583 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07007584 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08007585 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007586 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
7587 * // process the joystick movement...
7588 * return true;
7589 * }
7590 * }
7591 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
7592 * switch (event.getAction()) {
7593 * case MotionEvent.ACTION_HOVER_MOVE:
7594 * // process the mouse hover movement...
7595 * return true;
7596 * case MotionEvent.ACTION_SCROLL:
7597 * // process the scroll wheel movement...
7598 * return true;
7599 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08007600 * }
7601 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07007602 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08007603 *
7604 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08007605 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08007606 */
7607 public boolean onGenericMotionEvent(MotionEvent event) {
7608 return false;
7609 }
7610
7611 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007612 * Implement this method to handle hover events.
7613 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07007614 * This method is called whenever a pointer is hovering into, over, or out of the
7615 * bounds of a view and the view is not currently being touched.
7616 * Hover events are represented as pointer events with action
7617 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
7618 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
7619 * </p>
7620 * <ul>
7621 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
7622 * when the pointer enters the bounds of the view.</li>
7623 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
7624 * when the pointer has already entered the bounds of the view and has moved.</li>
7625 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
7626 * when the pointer has exited the bounds of the view or when the pointer is
7627 * about to go down due to a button click, tap, or similar user action that
7628 * causes the view to be touched.</li>
7629 * </ul>
7630 * <p>
7631 * The view should implement this method to return true to indicate that it is
7632 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08007633 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07007634 * The default implementation calls {@link #setHovered} to update the hovered state
7635 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07007636 * is enabled and is clickable. The default implementation also sends hover
7637 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08007638 * </p>
7639 *
7640 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07007641 * @return True if the view handled the hover event.
7642 *
7643 * @see #isHovered
7644 * @see #setHovered
7645 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007646 */
7647 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007648 // The root view may receive hover (or touch) events that are outside the bounds of
7649 // the window. This code ensures that we only send accessibility events for
7650 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07007651 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007652 if (!mSendingHoverAccessibilityEvents) {
7653 if ((action == MotionEvent.ACTION_HOVER_ENTER
7654 || action == MotionEvent.ACTION_HOVER_MOVE)
7655 && !hasHoveredChild()
7656 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07007657 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007658 mSendingHoverAccessibilityEvents = true;
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007659 }
7660 } else {
7661 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07007662 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007663 && !pointInView(event.getX(), event.getY()))) {
7664 mSendingHoverAccessibilityEvents = false;
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07007665 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007666 // If the window does not have input focus we take away accessibility
7667 // focus as soon as the user stop hovering over the view.
Jeff Brown59a422e2012-04-19 15:19:19 -07007668 if (mAttachInfo != null && !mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07007669 getViewRootImpl().setAccessibilityFocus(null, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007670 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007671 }
Jeff Browna1b24182011-07-28 13:38:24 -07007672 }
7673
Jeff Brown87b7f802011-06-21 18:35:45 -07007674 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007675 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07007676 case MotionEvent.ACTION_HOVER_ENTER:
7677 setHovered(true);
7678 break;
7679 case MotionEvent.ACTION_HOVER_EXIT:
7680 setHovered(false);
7681 break;
7682 }
Jeff Browna1b24182011-07-28 13:38:24 -07007683
7684 // Dispatch the event to onGenericMotionEvent before returning true.
7685 // This is to provide compatibility with existing applications that
7686 // handled HOVER_MOVE events in onGenericMotionEvent and that would
7687 // break because of the new default handling for hoverable views
7688 // in onHoverEvent.
7689 // Note that onGenericMotionEvent will be called by default when
7690 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
7691 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07007692 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08007693 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007694
Svetoslav Ganov736c2752011-04-22 18:30:36 -07007695 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08007696 }
7697
7698 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007699 * Returns true if the view should handle {@link #onHoverEvent}
7700 * by calling {@link #setHovered} to change its hovered state.
7701 *
7702 * @return True if the view is hoverable.
7703 */
7704 private boolean isHoverable() {
7705 final int viewFlags = mViewFlags;
7706 if ((viewFlags & ENABLED_MASK) == DISABLED) {
7707 return false;
7708 }
7709
7710 return (viewFlags & CLICKABLE) == CLICKABLE
7711 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
7712 }
7713
7714 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007715 * Returns true if the view is currently hovered.
7716 *
7717 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007718 *
7719 * @see #setHovered
7720 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007721 */
Jeff Brown10b62902011-06-20 16:40:37 -07007722 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08007723 public boolean isHovered() {
7724 return (mPrivateFlags & HOVERED) != 0;
7725 }
7726
7727 /**
7728 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007729 * <p>
7730 * Calling this method also changes the drawable state of the view. This
7731 * enables the view to react to hover by using different drawable resources
7732 * to change its appearance.
7733 * </p><p>
7734 * The {@link #onHoverChanged} method is called when the hovered state changes.
7735 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08007736 *
7737 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007738 *
7739 * @see #isHovered
7740 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007741 */
7742 public void setHovered(boolean hovered) {
7743 if (hovered) {
7744 if ((mPrivateFlags & HOVERED) == 0) {
7745 mPrivateFlags |= HOVERED;
7746 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007747 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08007748 }
7749 } else {
7750 if ((mPrivateFlags & HOVERED) != 0) {
7751 mPrivateFlags &= ~HOVERED;
7752 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007753 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08007754 }
7755 }
7756 }
7757
7758 /**
Jeff Brown10b62902011-06-20 16:40:37 -07007759 * Implement this method to handle hover state changes.
7760 * <p>
7761 * This method is called whenever the hover state changes as a result of a
7762 * call to {@link #setHovered}.
7763 * </p>
7764 *
7765 * @param hovered The current hover state, as returned by {@link #isHovered}.
7766 *
7767 * @see #isHovered
7768 * @see #setHovered
7769 */
7770 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07007771 }
7772
7773 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007774 * Implement this method to handle touch screen motion events.
7775 *
7776 * @param event The motion event.
7777 * @return True if the event was handled, false otherwise.
7778 */
7779 public boolean onTouchEvent(MotionEvent event) {
7780 final int viewFlags = mViewFlags;
7781
7782 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007783 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08007784 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007785 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007786 // A disabled view that is clickable still consumes the touch
7787 // events, it just doesn't respond to them.
7788 return (((viewFlags & CLICKABLE) == CLICKABLE ||
7789 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
7790 }
7791
7792 if (mTouchDelegate != null) {
7793 if (mTouchDelegate.onTouchEvent(event)) {
7794 return true;
7795 }
7796 }
7797
7798 if (((viewFlags & CLICKABLE) == CLICKABLE ||
7799 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
7800 switch (event.getAction()) {
7801 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08007802 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
7803 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007804 // take focus if we don't have it already and we should in
7805 // touch mode.
7806 boolean focusTaken = false;
7807 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
7808 focusTaken = requestFocus();
7809 }
7810
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007811 if (prepressed) {
7812 // The button is being released before we actually
7813 // showed it as pressed. Make it show the pressed
7814 // state now (before scheduling the click) to ensure
7815 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08007816 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007817 }
Joe Malin32736f02011-01-19 16:14:20 -08007818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007819 if (!mHasPerformedLongPress) {
7820 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007821 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007822
7823 // Only perform take click actions if we were in the pressed state
7824 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08007825 // Use a Runnable and post this rather than calling
7826 // performClick directly. This lets other visual state
7827 // of the view update before click actions start.
7828 if (mPerformClick == null) {
7829 mPerformClick = new PerformClick();
7830 }
7831 if (!post(mPerformClick)) {
7832 performClick();
7833 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007834 }
7835 }
7836
7837 if (mUnsetPressedState == null) {
7838 mUnsetPressedState = new UnsetPressedState();
7839 }
7840
Adam Powelle14579b2009-12-16 18:39:52 -08007841 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08007842 postDelayed(mUnsetPressedState,
7843 ViewConfiguration.getPressedStateDuration());
7844 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007845 // If the post failed, unpress right now
7846 mUnsetPressedState.run();
7847 }
Adam Powelle14579b2009-12-16 18:39:52 -08007848 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007849 }
7850 break;
7851
7852 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08007853 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007854
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07007855 if (performButtonActionOnTouchDown(event)) {
7856 break;
7857 }
7858
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007859 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07007860 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007861
7862 // For views inside a scrolling container, delay the pressed feedback for
7863 // a short period in case this is a scroll.
7864 if (isInScrollingContainer) {
7865 mPrivateFlags |= PREPRESSED;
7866 if (mPendingCheckForTap == null) {
7867 mPendingCheckForTap = new CheckForTap();
7868 }
7869 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
7870 } else {
7871 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08007872 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007873 checkForLongClick(0);
7874 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007875 break;
7876
7877 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08007878 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08007879 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007880 break;
7881
7882 case MotionEvent.ACTION_MOVE:
7883 final int x = (int) event.getX();
7884 final int y = (int) event.getY();
7885
7886 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07007887 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007888 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08007889 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007890 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08007891 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05007892 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007893
Adam Powell4d6f0662012-02-21 15:11:11 -08007894 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007895 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007896 }
7897 break;
7898 }
7899 return true;
7900 }
7901
7902 return false;
7903 }
7904
7905 /**
Adam Powell10298662011-08-14 18:26:30 -07007906 * @hide
7907 */
7908 public boolean isInScrollingContainer() {
7909 ViewParent p = getParent();
7910 while (p != null && p instanceof ViewGroup) {
7911 if (((ViewGroup) p).shouldDelayChildPressedState()) {
7912 return true;
7913 }
7914 p = p.getParent();
7915 }
7916 return false;
7917 }
7918
7919 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05007920 * Remove the longpress detection timer.
7921 */
7922 private void removeLongPressCallback() {
7923 if (mPendingCheckForLongPress != null) {
7924 removeCallbacks(mPendingCheckForLongPress);
7925 }
7926 }
Adam Powell3cb8b632011-01-21 15:34:14 -08007927
7928 /**
7929 * Remove the pending click action
7930 */
7931 private void removePerformClickCallback() {
7932 if (mPerformClick != null) {
7933 removeCallbacks(mPerformClick);
7934 }
7935 }
7936
Adam Powelle14579b2009-12-16 18:39:52 -08007937 /**
Romain Guya440b002010-02-24 15:57:54 -08007938 * Remove the prepress detection timer.
7939 */
7940 private void removeUnsetPressCallback() {
7941 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
7942 setPressed(false);
7943 removeCallbacks(mUnsetPressedState);
7944 }
7945 }
7946
7947 /**
Adam Powelle14579b2009-12-16 18:39:52 -08007948 * Remove the tap detection timer.
7949 */
7950 private void removeTapCallback() {
7951 if (mPendingCheckForTap != null) {
7952 mPrivateFlags &= ~PREPRESSED;
7953 removeCallbacks(mPendingCheckForTap);
7954 }
7955 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007956
7957 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007958 * Cancels a pending long press. Your subclass can use this if you
7959 * want the context menu to come up if the user presses and holds
7960 * at the same place, but you don't want it to come up if they press
7961 * and then move around enough to cause scrolling.
7962 */
7963 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05007964 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08007965
7966 /*
7967 * The prepressed state handled by the tap callback is a display
7968 * construct, but the tap callback will post a long press callback
7969 * less its own timeout. Remove it here.
7970 */
7971 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007972 }
7973
7974 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07007975 * Remove the pending callback for sending a
7976 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
7977 */
7978 private void removeSendViewScrolledAccessibilityEventCallback() {
7979 if (mSendViewScrolledAccessibilityEvent != null) {
7980 removeCallbacks(mSendViewScrolledAccessibilityEvent);
Svetoslav Ganov4a812ae2012-05-29 16:46:10 -07007981 mSendViewScrolledAccessibilityEvent.mIsPending = false;
Svetoslav Ganova0156172011-06-26 17:55:44 -07007982 }
7983 }
7984
7985 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007986 * Sets the TouchDelegate for this View.
7987 */
7988 public void setTouchDelegate(TouchDelegate delegate) {
7989 mTouchDelegate = delegate;
7990 }
7991
7992 /**
7993 * Gets the TouchDelegate for this View.
7994 */
7995 public TouchDelegate getTouchDelegate() {
7996 return mTouchDelegate;
7997 }
7998
7999 /**
8000 * Set flags controlling behavior of this view.
8001 *
8002 * @param flags Constant indicating the value which should be set
8003 * @param mask Constant indicating the bit range that should be changed
8004 */
8005 void setFlags(int flags, int mask) {
8006 int old = mViewFlags;
8007 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
8008
8009 int changed = mViewFlags ^ old;
8010 if (changed == 0) {
8011 return;
8012 }
8013 int privateFlags = mPrivateFlags;
8014
8015 /* Check if the FOCUSABLE bit has changed */
8016 if (((changed & FOCUSABLE_MASK) != 0) &&
8017 ((privateFlags & HAS_BOUNDS) !=0)) {
8018 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
8019 && ((privateFlags & FOCUSED) != 0)) {
8020 /* Give up focus if we are no longer focusable */
8021 clearFocus();
8022 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
8023 && ((privateFlags & FOCUSED) == 0)) {
8024 /*
8025 * Tell the view system that we are now available to take focus
8026 * if no one else already has it.
8027 */
8028 if (mParent != null) mParent.focusableViewAvailable(this);
8029 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008030 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8031 notifyAccessibilityStateChanged();
8032 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008033 }
8034
8035 if ((flags & VISIBILITY_MASK) == VISIBLE) {
8036 if ((changed & VISIBILITY_MASK) != 0) {
8037 /*
Chet Haase4324ead2011-08-24 21:31:03 -07008038 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07008039 * it was not visible. Marking it drawn ensures that the invalidation will
8040 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008041 */
Chet Haaseaceafe62011-08-26 15:44:33 -07008042 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07008043 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008044
8045 needGlobalAttributesUpdate(true);
8046
8047 // a view becoming visible is worth notifying the parent
8048 // about in case nothing has focus. even if this specific view
8049 // isn't focusable, it may contain something that is, so let
8050 // the root view try to give this focus if nothing else does.
8051 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
8052 mParent.focusableViewAvailable(this);
8053 }
8054 }
8055 }
8056
8057 /* Check if the GONE bit has changed */
8058 if ((changed & GONE) != 0) {
8059 needGlobalAttributesUpdate(false);
8060 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008061
Romain Guyecd80ee2009-12-03 17:13:02 -08008062 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
8063 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008064 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08008065 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07008066 if (mParent instanceof View) {
8067 // GONE views noop invalidation, so invalidate the parent
8068 ((View) mParent).invalidate(true);
8069 }
8070 // Mark the view drawn to ensure that it gets invalidated properly the next
8071 // time it is visible and gets invalidated
8072 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008073 }
8074 if (mAttachInfo != null) {
8075 mAttachInfo.mViewVisibilityChanged = true;
8076 }
8077 }
8078
8079 /* Check if the VISIBLE bit has changed */
8080 if ((changed & INVISIBLE) != 0) {
8081 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07008082 /*
8083 * If this view is becoming invisible, set the DRAWN flag so that
8084 * the next invalidate() will not be skipped.
8085 */
8086 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008087
8088 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008089 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008090 if (getRootView() != this) {
8091 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008092 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008093 }
8094 }
8095 if (mAttachInfo != null) {
8096 mAttachInfo.mViewVisibilityChanged = true;
8097 }
8098 }
8099
Adam Powell326d8082009-12-09 15:10:07 -08008100 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07008101 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08008102 ((ViewGroup) mParent).onChildVisibilityChanged(this,
8103 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08008104 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07008105 } else if (mParent != null) {
8106 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07008107 }
Adam Powell326d8082009-12-09 15:10:07 -08008108 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
8109 }
8110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008111 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
8112 destroyDrawingCache();
8113 }
8114
8115 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
8116 destroyDrawingCache();
8117 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008118 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008119 }
8120
8121 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
8122 destroyDrawingCache();
8123 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8124 }
8125
8126 if ((changed & DRAW_MASK) != 0) {
8127 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07008128 if (mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008129 mPrivateFlags &= ~SKIP_DRAW;
8130 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
8131 } else {
8132 mPrivateFlags |= SKIP_DRAW;
8133 }
8134 } else {
8135 mPrivateFlags &= ~SKIP_DRAW;
8136 }
8137 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08008138 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008139 }
8140
8141 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08008142 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008143 mParent.recomputeViewAttributes(this);
8144 }
8145 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008146
8147 if (AccessibilityManager.getInstance(mContext).isEnabled()
8148 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
8149 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
8150 notifyAccessibilityStateChanged();
8151 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008152 }
8153
8154 /**
8155 * Change the view's z order in the tree, so it's on top of other sibling
8156 * views
8157 */
8158 public void bringToFront() {
8159 if (mParent != null) {
8160 mParent.bringChildToFront(this);
8161 }
8162 }
8163
8164 /**
8165 * This is called in response to an internal scroll in this view (i.e., the
8166 * view scrolled its own contents). This is typically as a result of
8167 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
8168 * called.
8169 *
8170 * @param l Current horizontal scroll origin.
8171 * @param t Current vertical scroll origin.
8172 * @param oldl Previous horizontal scroll origin.
8173 * @param oldt Previous vertical scroll origin.
8174 */
8175 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07008176 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8177 postSendViewScrolledAccessibilityEventCallback();
8178 }
8179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008180 mBackgroundSizeChanged = true;
8181
8182 final AttachInfo ai = mAttachInfo;
8183 if (ai != null) {
8184 ai.mViewScrollChanged = true;
8185 }
8186 }
8187
8188 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008189 * Interface definition for a callback to be invoked when the layout bounds of a view
8190 * changes due to layout processing.
8191 */
8192 public interface OnLayoutChangeListener {
8193 /**
8194 * Called when the focus state of a view has changed.
8195 *
8196 * @param v The view whose state has changed.
8197 * @param left The new value of the view's left property.
8198 * @param top The new value of the view's top property.
8199 * @param right The new value of the view's right property.
8200 * @param bottom The new value of the view's bottom property.
8201 * @param oldLeft The previous value of the view's left property.
8202 * @param oldTop The previous value of the view's top property.
8203 * @param oldRight The previous value of the view's right property.
8204 * @param oldBottom The previous value of the view's bottom property.
8205 */
8206 void onLayoutChange(View v, int left, int top, int right, int bottom,
8207 int oldLeft, int oldTop, int oldRight, int oldBottom);
8208 }
8209
8210 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008211 * This is called during layout when the size of this view has changed. If
8212 * you were just added to the view hierarchy, you're called with the old
8213 * values of 0.
8214 *
8215 * @param w Current width of this view.
8216 * @param h Current height of this view.
8217 * @param oldw Old width of this view.
8218 * @param oldh Old height of this view.
8219 */
8220 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
8221 }
8222
8223 /**
8224 * Called by draw to draw the child views. This may be overridden
8225 * by derived classes to gain control just before its children are drawn
8226 * (but after its own view has been drawn).
8227 * @param canvas the canvas on which to draw the view
8228 */
8229 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008231 }
8232
8233 /**
8234 * Gets the parent of this view. Note that the parent is a
8235 * ViewParent and not necessarily a View.
8236 *
8237 * @return Parent of this view.
8238 */
8239 public final ViewParent getParent() {
8240 return mParent;
8241 }
8242
8243 /**
Chet Haasecca2c982011-05-20 14:34:18 -07008244 * Set the horizontal scrolled position of your view. This will cause a call to
8245 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8246 * invalidated.
8247 * @param value the x position to scroll to
8248 */
8249 public void setScrollX(int value) {
8250 scrollTo(value, mScrollY);
8251 }
8252
8253 /**
8254 * Set the vertical scrolled position of your view. This will cause a call to
8255 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8256 * invalidated.
8257 * @param value the y position to scroll to
8258 */
8259 public void setScrollY(int value) {
8260 scrollTo(mScrollX, value);
8261 }
8262
8263 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008264 * Return the scrolled left position of this view. This is the left edge of
8265 * the displayed part of your view. You do not need to draw any pixels
8266 * farther left, since those are outside of the frame of your view on
8267 * screen.
8268 *
8269 * @return The left edge of the displayed part of your view, in pixels.
8270 */
8271 public final int getScrollX() {
8272 return mScrollX;
8273 }
8274
8275 /**
8276 * Return the scrolled top position of this view. This is the top edge of
8277 * the displayed part of your view. You do not need to draw any pixels above
8278 * it, since those are outside of the frame of your view on screen.
8279 *
8280 * @return The top edge of the displayed part of your view, in pixels.
8281 */
8282 public final int getScrollY() {
8283 return mScrollY;
8284 }
8285
8286 /**
8287 * Return the width of the your view.
8288 *
8289 * @return The width of your view, in pixels.
8290 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008291 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008292 public final int getWidth() {
8293 return mRight - mLeft;
8294 }
8295
8296 /**
8297 * Return the height of your view.
8298 *
8299 * @return The height of your view, in pixels.
8300 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008301 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008302 public final int getHeight() {
8303 return mBottom - mTop;
8304 }
8305
8306 /**
8307 * Return the visible drawing bounds of your view. Fills in the output
8308 * rectangle with the values from getScrollX(), getScrollY(),
8309 * getWidth(), and getHeight().
8310 *
8311 * @param outRect The (scrolled) drawing bounds of the view.
8312 */
8313 public void getDrawingRect(Rect outRect) {
8314 outRect.left = mScrollX;
8315 outRect.top = mScrollY;
8316 outRect.right = mScrollX + (mRight - mLeft);
8317 outRect.bottom = mScrollY + (mBottom - mTop);
8318 }
8319
8320 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008321 * Like {@link #getMeasuredWidthAndState()}, but only returns the
8322 * raw width component (that is the result is masked by
8323 * {@link #MEASURED_SIZE_MASK}).
8324 *
8325 * @return The raw measured width of this view.
8326 */
8327 public final int getMeasuredWidth() {
8328 return mMeasuredWidth & MEASURED_SIZE_MASK;
8329 }
8330
8331 /**
8332 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008333 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008334 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008335 * This should be used during measurement and layout calculations only. Use
8336 * {@link #getWidth()} to see how wide a view is after layout.
8337 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008338 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008339 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008340 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008341 return mMeasuredWidth;
8342 }
8343
8344 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008345 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8346 * raw width component (that is the result is masked by
8347 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008348 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008349 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008350 */
8351 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008352 return mMeasuredHeight & MEASURED_SIZE_MASK;
8353 }
8354
8355 /**
8356 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008357 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008358 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8359 * This should be used during measurement and layout calculations only. Use
8360 * {@link #getHeight()} to see how wide a view is after layout.
8361 *
8362 * @return The measured width of this view as a bit mask.
8363 */
8364 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008365 return mMeasuredHeight;
8366 }
8367
8368 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008369 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8370 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8371 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8372 * and the height component is at the shifted bits
8373 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8374 */
8375 public final int getMeasuredState() {
8376 return (mMeasuredWidth&MEASURED_STATE_MASK)
8377 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8378 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8379 }
8380
8381 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008382 * The transform matrix of this view, which is calculated based on the current
8383 * roation, scale, and pivot properties.
8384 *
8385 * @see #getRotation()
8386 * @see #getScaleX()
8387 * @see #getScaleY()
8388 * @see #getPivotX()
8389 * @see #getPivotY()
8390 * @return The current transform matrix for the view
8391 */
8392 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008393 if (mTransformationInfo != null) {
8394 updateMatrix();
8395 return mTransformationInfo.mMatrix;
8396 }
8397 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008398 }
8399
8400 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008401 * Utility function to determine if the value is far enough away from zero to be
8402 * considered non-zero.
8403 * @param value A floating point value to check for zero-ness
8404 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8405 */
8406 private static boolean nonzero(float value) {
8407 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8408 }
8409
8410 /**
Jeff Brown86671742010-09-30 20:00:15 -07008411 * Returns true if the transform matrix is the identity matrix.
8412 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008413 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008414 * @return True if the transform matrix is the identity matrix, false otherwise.
8415 */
Jeff Brown86671742010-09-30 20:00:15 -07008416 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008417 if (mTransformationInfo != null) {
8418 updateMatrix();
8419 return mTransformationInfo.mMatrixIsIdentity;
8420 }
8421 return true;
8422 }
8423
8424 void ensureTransformationInfo() {
8425 if (mTransformationInfo == null) {
8426 mTransformationInfo = new TransformationInfo();
8427 }
Jeff Brown86671742010-09-30 20:00:15 -07008428 }
8429
8430 /**
8431 * Recomputes the transform matrix if necessary.
8432 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008433 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008434 final TransformationInfo info = mTransformationInfo;
8435 if (info == null) {
8436 return;
8437 }
8438 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008439 // transform-related properties have changed since the last time someone
8440 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008441
8442 // Figure out if we need to update the pivot point
8443 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008444 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8445 info.mPrevWidth = mRight - mLeft;
8446 info.mPrevHeight = mBottom - mTop;
8447 info.mPivotX = info.mPrevWidth / 2f;
8448 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008449 }
8450 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008451 info.mMatrix.reset();
8452 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8453 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8454 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8455 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008456 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008457 if (info.mCamera == null) {
8458 info.mCamera = new Camera();
8459 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008460 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008461 info.mCamera.save();
8462 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8463 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8464 info.mCamera.getMatrix(info.matrix3D);
8465 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8466 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8467 info.mPivotY + info.mTranslationY);
8468 info.mMatrix.postConcat(info.matrix3D);
8469 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008470 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008471 info.mMatrixDirty = false;
8472 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8473 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008474 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008475 }
8476
8477 /**
Tobias Duboisdefdb1e2010-12-15 11:35:30 +01008478 * When searching for a view to focus this rectangle is used when considering if this view is
8479 * a good candidate for receiving focus.
8480 *
8481 * By default, the rectangle is the {@link #getDrawingRect}) of the view.
8482 *
8483 * @param r The rectangle to fill in, in this view's coordinates.
8484 */
8485 public void getFocusRect(Rect r) {
8486 getDrawingRect(r);
8487 }
8488
8489 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008490 * Utility method to retrieve the inverse of the current mMatrix property.
8491 * We cache the matrix to avoid recalculating it when transform properties
8492 * have not changed.
8493 *
8494 * @return The inverse of the current matrix of this view.
8495 */
Jeff Brown86671742010-09-30 20:00:15 -07008496 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008497 final TransformationInfo info = mTransformationInfo;
8498 if (info != null) {
8499 updateMatrix();
8500 if (info.mInverseMatrixDirty) {
8501 if (info.mInverseMatrix == null) {
8502 info.mInverseMatrix = new Matrix();
8503 }
8504 info.mMatrix.invert(info.mInverseMatrix);
8505 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008506 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008507 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008508 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008509 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008510 }
8511
8512 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008513 * Gets the distance along the Z axis from the camera to this view.
8514 *
8515 * @see #setCameraDistance(float)
8516 *
8517 * @return The distance along the Z axis.
8518 */
8519 public float getCameraDistance() {
8520 ensureTransformationInfo();
8521 final float dpi = mResources.getDisplayMetrics().densityDpi;
8522 final TransformationInfo info = mTransformationInfo;
8523 if (info.mCamera == null) {
8524 info.mCamera = new Camera();
8525 info.matrix3D = new Matrix();
8526 }
8527 return -(info.mCamera.getLocationZ() * dpi);
8528 }
8529
8530 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008531 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8532 * views are drawn) from the camera to this view. The camera's distance
8533 * affects 3D transformations, for instance rotations around the X and Y
8534 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008535 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008536 * use a camera distance that's greater than the height (X axis rotation) or
8537 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008538 *
Romain Guya5364ee2011-02-24 14:46:04 -08008539 * <p>The distance of the camera from the view plane can have an affect on the
8540 * perspective distortion of the view when it is rotated around the x or y axis.
8541 * For example, a large distance will result in a large viewing angle, and there
8542 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008543 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008544 * also result in some drawing artifacts if the rotated view ends up partially
8545 * behind the camera (which is why the recommendation is to use a distance at
8546 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008547 *
Romain Guya5364ee2011-02-24 14:46:04 -08008548 * <p>The distance is expressed in "depth pixels." The default distance depends
8549 * on the screen density. For instance, on a medium density display, the
8550 * default distance is 1280. On a high density display, the default distance
8551 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008552 *
Romain Guya5364ee2011-02-24 14:46:04 -08008553 * <p>If you want to specify a distance that leads to visually consistent
8554 * results across various densities, use the following formula:</p>
8555 * <pre>
8556 * float scale = context.getResources().getDisplayMetrics().density;
8557 * view.setCameraDistance(distance * scale);
8558 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07008559 *
Romain Guya5364ee2011-02-24 14:46:04 -08008560 * <p>The density scale factor of a high density display is 1.5,
8561 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008562 *
Romain Guya5364ee2011-02-24 14:46:04 -08008563 * @param distance The distance in "depth pixels", if negative the opposite
8564 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07008565 *
8566 * @see #setRotationX(float)
8567 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08008568 */
8569 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008570 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08008571
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008572 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08008573 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008574 final TransformationInfo info = mTransformationInfo;
8575 if (info.mCamera == null) {
8576 info.mCamera = new Camera();
8577 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08008578 }
8579
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008580 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
8581 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08008582
Chet Haase9d1992d2012-03-13 11:03:25 -07008583 invalidateViewProperty(false, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008584 if (mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07008585 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08008586 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008587 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8588 // View was rejected last time it was drawn by its parent; this may have changed
8589 invalidateParentIfNeeded();
8590 }
Romain Guya5364ee2011-02-24 14:46:04 -08008591 }
8592
8593 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008594 * The degrees that the view is rotated around the pivot point.
8595 *
Philip Milne6c8ea062012-04-03 17:38:43 -07008596 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07008597 * @see #getPivotX()
8598 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008599 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008600 * @return The degrees of rotation.
8601 */
Chet Haasea5531132012-02-02 13:41:44 -08008602 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008603 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008604 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008605 }
8606
8607 /**
Chet Haase897247b2010-09-09 14:54:47 -07008608 * Sets the degrees that the view is rotated around the pivot point. Increasing values
8609 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07008610 *
8611 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008612 *
8613 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07008614 * @see #getPivotX()
8615 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008616 * @see #setRotationX(float)
8617 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08008618 *
8619 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07008620 */
8621 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008622 ensureTransformationInfo();
8623 final TransformationInfo info = mTransformationInfo;
8624 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008625 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008626 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008627 info.mRotation = rotation;
8628 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008629 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008630 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008631 mDisplayList.setRotation(rotation);
8632 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008633 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8634 // View was rejected last time it was drawn by its parent; this may have changed
8635 invalidateParentIfNeeded();
8636 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008637 }
8638 }
8639
8640 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008641 * The degrees that the view is rotated around the vertical axis through the pivot point.
8642 *
8643 * @see #getPivotX()
8644 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008645 * @see #setRotationY(float)
8646 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008647 * @return The degrees of Y rotation.
8648 */
Chet Haasea5531132012-02-02 13:41:44 -08008649 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008650 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008651 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008652 }
8653
8654 /**
Chet Haase897247b2010-09-09 14:54:47 -07008655 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
8656 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
8657 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008658 *
Romain Guya5364ee2011-02-24 14:46:04 -08008659 * When rotating large views, it is recommended to adjust the camera distance
8660 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008661 *
8662 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008663 *
8664 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07008665 * @see #getPivotX()
8666 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008667 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008668 * @see #setRotationX(float)
8669 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008670 *
8671 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07008672 */
8673 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008674 ensureTransformationInfo();
8675 final TransformationInfo info = mTransformationInfo;
8676 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008677 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008678 info.mRotationY = rotationY;
8679 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008680 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008681 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008682 mDisplayList.setRotationY(rotationY);
8683 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008684 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8685 // View was rejected last time it was drawn by its parent; this may have changed
8686 invalidateParentIfNeeded();
8687 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008688 }
8689 }
8690
8691 /**
8692 * The degrees that the view is rotated around the horizontal axis through the pivot point.
8693 *
8694 * @see #getPivotX()
8695 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008696 * @see #setRotationX(float)
8697 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008698 * @return The degrees of X rotation.
8699 */
Chet Haasea5531132012-02-02 13:41:44 -08008700 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008701 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008702 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008703 }
8704
8705 /**
Chet Haase897247b2010-09-09 14:54:47 -07008706 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
8707 * Increasing values result in clockwise rotation from the viewpoint of looking down the
8708 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008709 *
Romain Guya5364ee2011-02-24 14:46:04 -08008710 * When rotating large views, it is recommended to adjust the camera distance
8711 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008712 *
8713 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008714 *
8715 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07008716 * @see #getPivotX()
8717 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008718 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008719 * @see #setRotationY(float)
8720 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008721 *
8722 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07008723 */
8724 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008725 ensureTransformationInfo();
8726 final TransformationInfo info = mTransformationInfo;
8727 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008728 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008729 info.mRotationX = rotationX;
8730 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008731 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008732 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008733 mDisplayList.setRotationX(rotationX);
8734 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008735 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8736 // View was rejected last time it was drawn by its parent; this may have changed
8737 invalidateParentIfNeeded();
8738 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008739 }
8740 }
8741
8742 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008743 * The amount that the view is scaled in x around the pivot point, as a proportion of
8744 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
8745 *
Joe Onorato93162322010-09-16 15:42:01 -04008746 * <p>By default, this is 1.0f.
8747 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008748 * @see #getPivotX()
8749 * @see #getPivotY()
8750 * @return The scaling factor.
8751 */
Chet Haasea5531132012-02-02 13:41:44 -08008752 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008753 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008754 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008755 }
8756
8757 /**
8758 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
8759 * the view's unscaled width. A value of 1 means that no scaling is applied.
8760 *
8761 * @param scaleX The scaling factor.
8762 * @see #getPivotX()
8763 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008764 *
8765 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07008766 */
8767 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008768 ensureTransformationInfo();
8769 final TransformationInfo info = mTransformationInfo;
8770 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008771 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008772 info.mScaleX = scaleX;
8773 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008774 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008775 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008776 mDisplayList.setScaleX(scaleX);
8777 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008778 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8779 // View was rejected last time it was drawn by its parent; this may have changed
8780 invalidateParentIfNeeded();
8781 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008782 }
8783 }
8784
8785 /**
8786 * The amount that the view is scaled in y around the pivot point, as a proportion of
8787 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
8788 *
Joe Onorato93162322010-09-16 15:42:01 -04008789 * <p>By default, this is 1.0f.
8790 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008791 * @see #getPivotX()
8792 * @see #getPivotY()
8793 * @return The scaling factor.
8794 */
Chet Haasea5531132012-02-02 13:41:44 -08008795 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008796 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008797 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008798 }
8799
8800 /**
8801 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
8802 * the view's unscaled width. A value of 1 means that no scaling is applied.
8803 *
8804 * @param scaleY The scaling factor.
8805 * @see #getPivotX()
8806 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008807 *
8808 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07008809 */
8810 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008811 ensureTransformationInfo();
8812 final TransformationInfo info = mTransformationInfo;
8813 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008814 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008815 info.mScaleY = scaleY;
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.setScaleY(scaleY);
8820 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008821 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8822 // View was rejected last time it was drawn by its parent; this may have changed
8823 invalidateParentIfNeeded();
8824 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008825 }
8826 }
8827
8828 /**
8829 * The x location of the point around which the view is {@link #setRotation(float) rotated}
8830 * and {@link #setScaleX(float) scaled}.
8831 *
8832 * @see #getRotation()
8833 * @see #getScaleX()
8834 * @see #getScaleY()
8835 * @see #getPivotY()
8836 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008837 *
8838 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008839 */
Chet Haasea5531132012-02-02 13:41:44 -08008840 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008841 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008842 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008843 }
8844
8845 /**
8846 * Sets the x location of the point around which the view is
8847 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07008848 * By default, the pivot point is centered on the object.
8849 * Setting this property disables this behavior and causes the view to use only the
8850 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008851 *
8852 * @param pivotX The x location of the pivot point.
8853 * @see #getRotation()
8854 * @see #getScaleX()
8855 * @see #getScaleY()
8856 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008857 *
8858 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008859 */
8860 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008861 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07008862 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008863 final TransformationInfo info = mTransformationInfo;
8864 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008865 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008866 info.mPivotX = pivotX;
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.setPivotX(pivotX);
8871 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008872 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8873 // View was rejected last time it was drawn by its parent; this may have changed
8874 invalidateParentIfNeeded();
8875 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008876 }
8877 }
8878
8879 /**
8880 * The y location of the point around which the view is {@link #setRotation(float) rotated}
8881 * and {@link #setScaleY(float) scaled}.
8882 *
8883 * @see #getRotation()
8884 * @see #getScaleX()
8885 * @see #getScaleY()
8886 * @see #getPivotY()
8887 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008888 *
8889 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07008890 */
Chet Haasea5531132012-02-02 13:41:44 -08008891 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008892 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008893 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008894 }
8895
8896 /**
8897 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07008898 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
8899 * Setting this property disables this behavior and causes the view to use only the
8900 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008901 *
8902 * @param pivotY The y location of the pivot point.
8903 * @see #getRotation()
8904 * @see #getScaleX()
8905 * @see #getScaleY()
8906 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008907 *
8908 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07008909 */
8910 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008911 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07008912 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008913 final TransformationInfo info = mTransformationInfo;
8914 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008915 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008916 info.mPivotY = pivotY;
8917 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008918 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008919 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008920 mDisplayList.setPivotY(pivotY);
8921 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008922 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8923 // View was rejected last time it was drawn by its parent; this may have changed
8924 invalidateParentIfNeeded();
8925 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008926 }
8927 }
8928
8929 /**
8930 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
8931 * completely transparent and 1 means the view is completely opaque.
8932 *
Joe Onorato93162322010-09-16 15:42:01 -04008933 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07008934 * @return The opacity of the view.
8935 */
Chet Haasea5531132012-02-02 13:41:44 -08008936 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008937 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008938 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008939 }
8940
8941 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07008942 * Returns whether this View has content which overlaps. This function, intended to be
8943 * overridden by specific View types, is an optimization when alpha is set on a view. If
8944 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
8945 * and then composited it into place, which can be expensive. If the view has no overlapping
8946 * rendering, the view can draw each primitive with the appropriate alpha value directly.
8947 * An example of overlapping rendering is a TextView with a background image, such as a
8948 * Button. An example of non-overlapping rendering is a TextView with no background, or
8949 * an ImageView with only the foreground image. The default implementation returns true;
8950 * subclasses should override if they have cases which can be optimized.
8951 *
8952 * @return true if the content in this view might overlap, false otherwise.
8953 */
8954 public boolean hasOverlappingRendering() {
8955 return true;
8956 }
8957
8958 /**
Romain Guy171c5922011-01-06 10:04:23 -08008959 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
8960 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008961 *
Romain Guy171c5922011-01-06 10:04:23 -08008962 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
8963 * responsible for applying the opacity itself. Otherwise, calling this method is
8964 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08008965 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07008966 *
Chet Haasea5531132012-02-02 13:41:44 -08008967 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
8968 * performance implications. It is generally best to use the alpha property sparingly and
8969 * transiently, as in the case of fading animations.</p>
8970 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008971 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08008972 *
Joe Malin32736f02011-01-19 16:14:20 -08008973 * @see #setLayerType(int, android.graphics.Paint)
8974 *
Chet Haase73066682010-11-29 15:55:32 -08008975 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07008976 */
8977 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008978 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008979 if (mTransformationInfo.mAlpha != alpha) {
8980 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008981 if (onSetAlpha((int) (alpha * 255))) {
8982 mPrivateFlags |= ALPHA_SET;
8983 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07008984 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008985 invalidate(true);
8986 } else {
8987 mPrivateFlags &= ~ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07008988 invalidateViewProperty(true, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008989 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008990 mDisplayList.setAlpha(alpha);
8991 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008992 }
Chet Haaseed032702010-10-01 14:05:54 -07008993 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008994 }
8995
8996 /**
Chet Haasea00f3862011-02-22 06:34:40 -08008997 * Faster version of setAlpha() which performs the same steps except there are
8998 * no calls to invalidate(). The caller of this function should perform proper invalidation
8999 * on the parent and this object. The return value indicates whether the subclass handles
9000 * alpha (the return value for onSetAlpha()).
9001 *
9002 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009003 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
9004 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08009005 */
9006 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009007 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009008 if (mTransformationInfo.mAlpha != alpha) {
9009 mTransformationInfo.mAlpha = alpha;
9010 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
9011 if (subclassHandlesAlpha) {
9012 mPrivateFlags |= ALPHA_SET;
9013 return true;
9014 } else {
9015 mPrivateFlags &= ~ALPHA_SET;
Chet Haase1271e2c2012-04-20 09:54:27 -07009016 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009017 mDisplayList.setAlpha(alpha);
9018 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009019 }
Chet Haasea00f3862011-02-22 06:34:40 -08009020 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009021 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08009022 }
9023
9024 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009025 * Top position of this view relative to its parent.
9026 *
9027 * @return The top of this view, in pixels.
9028 */
9029 @ViewDebug.CapturedViewProperty
9030 public final int getTop() {
9031 return mTop;
9032 }
9033
9034 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009035 * Sets the top position of this view relative to its parent. This method is meant to be called
9036 * by the layout system and should not generally be called otherwise, because the property
9037 * may be changed at any time by the layout.
9038 *
9039 * @param top The top of this view, in pixels.
9040 */
9041 public final void setTop(int top) {
9042 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07009043 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009044 final boolean matrixIsIdentity = mTransformationInfo == null
9045 || mTransformationInfo.mMatrixIsIdentity;
9046 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009047 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009048 int minTop;
9049 int yLoc;
9050 if (top < mTop) {
9051 minTop = top;
9052 yLoc = top - mTop;
9053 } else {
9054 minTop = mTop;
9055 yLoc = 0;
9056 }
Chet Haasee9140a72011-02-16 16:23:29 -08009057 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009058 }
9059 } else {
9060 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009061 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009062 }
9063
Chet Haaseed032702010-10-01 14:05:54 -07009064 int width = mRight - mLeft;
9065 int oldHeight = mBottom - mTop;
9066
Chet Haase21cd1382010-09-01 17:42:29 -07009067 mTop = top;
Chet Haase1271e2c2012-04-20 09:54:27 -07009068 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009069 mDisplayList.setTop(mTop);
9070 }
Chet Haase21cd1382010-09-01 17:42:29 -07009071
Chet Haaseed032702010-10-01 14:05:54 -07009072 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9073
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009074 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009075 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9076 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009077 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009078 }
Chet Haase21cd1382010-09-01 17:42:29 -07009079 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009080 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009081 }
Chet Haase55dbb652010-12-21 20:15:08 -08009082 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009083 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009084 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9085 // View was rejected last time it was drawn by its parent; this may have changed
9086 invalidateParentIfNeeded();
9087 }
Chet Haase21cd1382010-09-01 17:42:29 -07009088 }
9089 }
9090
9091 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009092 * Bottom position of this view relative to its parent.
9093 *
9094 * @return The bottom of this view, in pixels.
9095 */
9096 @ViewDebug.CapturedViewProperty
9097 public final int getBottom() {
9098 return mBottom;
9099 }
9100
9101 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08009102 * True if this view has changed since the last time being drawn.
9103 *
9104 * @return The dirty state of this view.
9105 */
9106 public boolean isDirty() {
9107 return (mPrivateFlags & DIRTY_MASK) != 0;
9108 }
9109
9110 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009111 * Sets the bottom position of this view relative to its parent. This method is meant to be
9112 * called by the layout system and should not generally be called otherwise, because the
9113 * property may be changed at any time by the layout.
9114 *
9115 * @param bottom The bottom of this view, in pixels.
9116 */
9117 public final void setBottom(int bottom) {
9118 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07009119 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009120 final boolean matrixIsIdentity = mTransformationInfo == null
9121 || mTransformationInfo.mMatrixIsIdentity;
9122 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009123 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009124 int maxBottom;
9125 if (bottom < mBottom) {
9126 maxBottom = mBottom;
9127 } else {
9128 maxBottom = bottom;
9129 }
Chet Haasee9140a72011-02-16 16:23:29 -08009130 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009131 }
9132 } else {
9133 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009134 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009135 }
9136
Chet Haaseed032702010-10-01 14:05:54 -07009137 int width = mRight - mLeft;
9138 int oldHeight = mBottom - mTop;
9139
Chet Haase21cd1382010-09-01 17:42:29 -07009140 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -07009141 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009142 mDisplayList.setBottom(mBottom);
9143 }
Chet Haase21cd1382010-09-01 17:42:29 -07009144
Chet Haaseed032702010-10-01 14:05:54 -07009145 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9146
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009147 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009148 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9149 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009150 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009151 }
Chet Haase21cd1382010-09-01 17:42:29 -07009152 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009153 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009154 }
Chet Haase55dbb652010-12-21 20:15:08 -08009155 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009156 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009157 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9158 // View was rejected last time it was drawn by its parent; this may have changed
9159 invalidateParentIfNeeded();
9160 }
Chet Haase21cd1382010-09-01 17:42:29 -07009161 }
9162 }
9163
9164 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009165 * Left position of this view relative to its parent.
9166 *
9167 * @return The left edge of this view, in pixels.
9168 */
9169 @ViewDebug.CapturedViewProperty
9170 public final int getLeft() {
9171 return mLeft;
9172 }
9173
9174 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009175 * Sets the left position of this view relative to its parent. This method is meant to be called
9176 * by the layout system and should not generally be called otherwise, because the property
9177 * may be changed at any time by the layout.
9178 *
9179 * @param left The bottom of this view, in pixels.
9180 */
9181 public final void setLeft(int left) {
9182 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07009183 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009184 final boolean matrixIsIdentity = mTransformationInfo == null
9185 || mTransformationInfo.mMatrixIsIdentity;
9186 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009187 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009188 int minLeft;
9189 int xLoc;
9190 if (left < mLeft) {
9191 minLeft = left;
9192 xLoc = left - mLeft;
9193 } else {
9194 minLeft = mLeft;
9195 xLoc = 0;
9196 }
Chet Haasee9140a72011-02-16 16:23:29 -08009197 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009198 }
9199 } else {
9200 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009201 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009202 }
9203
Chet Haaseed032702010-10-01 14:05:54 -07009204 int oldWidth = mRight - mLeft;
9205 int height = mBottom - mTop;
9206
Chet Haase21cd1382010-09-01 17:42:29 -07009207 mLeft = left;
Chet Haase1271e2c2012-04-20 09:54:27 -07009208 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009209 mDisplayList.setLeft(left);
9210 }
Chet Haase21cd1382010-09-01 17:42:29 -07009211
Chet Haaseed032702010-10-01 14:05:54 -07009212 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9213
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009214 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009215 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9216 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009217 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009218 }
Chet Haase21cd1382010-09-01 17:42:29 -07009219 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009220 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009221 }
Chet Haase55dbb652010-12-21 20:15:08 -08009222 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009223 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009224 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9225 // View was rejected last time it was drawn by its parent; this may have changed
9226 invalidateParentIfNeeded();
9227 }
Chet Haase21cd1382010-09-01 17:42:29 -07009228 }
9229 }
9230
9231 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009232 * Right position of this view relative to its parent.
9233 *
9234 * @return The right edge of this view, in pixels.
9235 */
9236 @ViewDebug.CapturedViewProperty
9237 public final int getRight() {
9238 return mRight;
9239 }
9240
9241 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009242 * Sets the right position of this view relative to its parent. This method is meant to be called
9243 * by the layout system and should not generally be called otherwise, because the property
9244 * may be changed at any time by the layout.
9245 *
9246 * @param right The bottom of this view, in pixels.
9247 */
9248 public final void setRight(int right) {
9249 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07009250 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009251 final boolean matrixIsIdentity = mTransformationInfo == null
9252 || mTransformationInfo.mMatrixIsIdentity;
9253 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009254 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009255 int maxRight;
9256 if (right < mRight) {
9257 maxRight = mRight;
9258 } else {
9259 maxRight = right;
9260 }
Chet Haasee9140a72011-02-16 16:23:29 -08009261 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009262 }
9263 } else {
9264 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009265 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009266 }
9267
Chet Haaseed032702010-10-01 14:05:54 -07009268 int oldWidth = mRight - mLeft;
9269 int height = mBottom - mTop;
9270
Chet Haase21cd1382010-09-01 17:42:29 -07009271 mRight = right;
Chet Haase1271e2c2012-04-20 09:54:27 -07009272 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009273 mDisplayList.setRight(mRight);
9274 }
Chet Haase21cd1382010-09-01 17:42:29 -07009275
Chet Haaseed032702010-10-01 14:05:54 -07009276 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9277
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009278 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009279 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9280 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009281 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009282 }
Chet Haase21cd1382010-09-01 17:42:29 -07009283 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009284 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009285 }
Chet Haase55dbb652010-12-21 20:15:08 -08009286 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009287 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009288 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9289 // View was rejected last time it was drawn by its parent; this may have changed
9290 invalidateParentIfNeeded();
9291 }
Chet Haase21cd1382010-09-01 17:42:29 -07009292 }
9293 }
9294
9295 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009296 * The visual x position of this view, in pixels. This is equivalent to the
9297 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08009298 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07009299 *
Chet Haasedf030d22010-07-30 17:22:38 -07009300 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009301 */
Chet Haasea5531132012-02-02 13:41:44 -08009302 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009303 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009304 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009305 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009306
Chet Haasedf030d22010-07-30 17:22:38 -07009307 /**
9308 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
9309 * {@link #setTranslationX(float) translationX} property to be the difference between
9310 * the x value passed in and the current {@link #getLeft() left} property.
9311 *
9312 * @param x The visual x position of this view, in pixels.
9313 */
9314 public void setX(float x) {
9315 setTranslationX(x - mLeft);
9316 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009317
Chet Haasedf030d22010-07-30 17:22:38 -07009318 /**
9319 * The visual y position of this view, in pixels. This is equivalent to the
9320 * {@link #setTranslationY(float) translationY} property plus the current
9321 * {@link #getTop() top} property.
9322 *
9323 * @return The visual y position of this view, in pixels.
9324 */
Chet Haasea5531132012-02-02 13:41:44 -08009325 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009326 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009327 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009328 }
9329
9330 /**
9331 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
9332 * {@link #setTranslationY(float) translationY} property to be the difference between
9333 * the y value passed in and the current {@link #getTop() top} property.
9334 *
9335 * @param y The visual y position of this view, in pixels.
9336 */
9337 public void setY(float y) {
9338 setTranslationY(y - mTop);
9339 }
9340
9341
9342 /**
9343 * The horizontal location of this view relative to its {@link #getLeft() left} position.
9344 * This position is post-layout, in addition to wherever the object's
9345 * layout placed it.
9346 *
9347 * @return The horizontal position of this view relative to its left position, in pixels.
9348 */
Chet Haasea5531132012-02-02 13:41:44 -08009349 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009350 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009351 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07009352 }
9353
9354 /**
9355 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
9356 * This effectively positions the object post-layout, in addition to wherever the object's
9357 * layout placed it.
9358 *
9359 * @param translationX The horizontal position of this view relative to its left position,
9360 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009361 *
9362 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07009363 */
9364 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009365 ensureTransformationInfo();
9366 final TransformationInfo info = mTransformationInfo;
9367 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07009368 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009369 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009370 info.mTranslationX = translationX;
9371 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009372 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009373 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009374 mDisplayList.setTranslationX(translationX);
9375 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009376 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9377 // View was rejected last time it was drawn by its parent; this may have changed
9378 invalidateParentIfNeeded();
9379 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009380 }
9381 }
9382
9383 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009384 * The horizontal location of this view relative to its {@link #getTop() top} position.
9385 * This position is post-layout, in addition to wherever the object's
9386 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009387 *
Chet Haasedf030d22010-07-30 17:22:38 -07009388 * @return The vertical position of this view relative to its top position,
9389 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009390 */
Chet Haasea5531132012-02-02 13:41:44 -08009391 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009392 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009393 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009394 }
9395
9396 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009397 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9398 * This effectively positions the object post-layout, in addition to wherever the object's
9399 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009400 *
Chet Haasedf030d22010-07-30 17:22:38 -07009401 * @param translationY The vertical position of this view relative to its top position,
9402 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009403 *
9404 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009405 */
Chet Haasedf030d22010-07-30 17:22:38 -07009406 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009407 ensureTransformationInfo();
9408 final TransformationInfo info = mTransformationInfo;
9409 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009410 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009411 info.mTranslationY = translationY;
9412 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009413 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009414 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009415 mDisplayList.setTranslationY(translationY);
9416 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009417 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9418 // View was rejected last time it was drawn by its parent; this may have changed
9419 invalidateParentIfNeeded();
9420 }
Chet Haasedf030d22010-07-30 17:22:38 -07009421 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009422 }
9423
9424 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009425 * Hit rectangle in parent's coordinates
9426 *
9427 * @param outRect The hit rectangle of the view.
9428 */
9429 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009430 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009431 final TransformationInfo info = mTransformationInfo;
9432 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009433 outRect.set(mLeft, mTop, mRight, mBottom);
9434 } else {
9435 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009436 tmpRect.set(-info.mPivotX, -info.mPivotY,
9437 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9438 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009439 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9440 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009441 }
9442 }
9443
9444 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009445 * Determines whether the given point, in local coordinates is inside the view.
9446 */
9447 /*package*/ final boolean pointInView(float localX, float localY) {
9448 return localX >= 0 && localX < (mRight - mLeft)
9449 && localY >= 0 && localY < (mBottom - mTop);
9450 }
9451
9452 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009453 * Utility method to determine whether the given point, in local coordinates,
9454 * is inside the view, where the area of the view is expanded by the slop factor.
9455 * This method is called while processing touch-move events to determine if the event
9456 * is still within the view.
9457 */
9458 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009459 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009460 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009461 }
9462
9463 /**
9464 * When a view has focus and the user navigates away from it, the next view is searched for
9465 * starting from the rectangle filled in by this method.
9466 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009467 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
9468 * of the view. However, if your view maintains some idea of internal selection,
9469 * such as a cursor, or a selected row or column, you should override this method and
9470 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009471 *
9472 * @param r The rectangle to fill in, in this view's coordinates.
9473 */
9474 public void getFocusedRect(Rect r) {
9475 getDrawingRect(r);
9476 }
9477
9478 /**
9479 * If some part of this view is not clipped by any of its parents, then
9480 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009481 * coordinates (without taking possible View rotations into account), offset
9482 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9483 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009484 *
9485 * @param r If true is returned, r holds the global coordinates of the
9486 * visible portion of this view.
9487 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9488 * between this view and its root. globalOffet may be null.
9489 * @return true if r is non-empty (i.e. part of the view is visible at the
9490 * root level.
9491 */
9492 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9493 int width = mRight - mLeft;
9494 int height = mBottom - mTop;
9495 if (width > 0 && height > 0) {
9496 r.set(0, 0, width, height);
9497 if (globalOffset != null) {
9498 globalOffset.set(-mScrollX, -mScrollY);
9499 }
9500 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9501 }
9502 return false;
9503 }
9504
9505 public final boolean getGlobalVisibleRect(Rect r) {
9506 return getGlobalVisibleRect(r, null);
9507 }
9508
9509 public final boolean getLocalVisibleRect(Rect r) {
Romain Guyab4c4f4f2012-05-06 13:11:24 -07009510 final Point offset = mAttachInfo != null ? mAttachInfo.mPoint : new Point();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009511 if (getGlobalVisibleRect(r, offset)) {
9512 r.offset(-offset.x, -offset.y); // make r local
9513 return true;
9514 }
9515 return false;
9516 }
9517
9518 /**
9519 * Offset this view's vertical location by the specified number of pixels.
9520 *
9521 * @param offset the number of pixels to offset the view by
9522 */
9523 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009524 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009525 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009526 final boolean matrixIsIdentity = mTransformationInfo == null
9527 || mTransformationInfo.mMatrixIsIdentity;
9528 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009529 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009530 invalidateViewProperty(false, false);
9531 } else {
9532 final ViewParent p = mParent;
9533 if (p != null && mAttachInfo != null) {
9534 final Rect r = mAttachInfo.mTmpInvalRect;
9535 int minTop;
9536 int maxBottom;
9537 int yLoc;
9538 if (offset < 0) {
9539 minTop = mTop + offset;
9540 maxBottom = mBottom;
9541 yLoc = offset;
9542 } else {
9543 minTop = mTop;
9544 maxBottom = mBottom + offset;
9545 yLoc = 0;
9546 }
9547 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
9548 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009549 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009550 }
9551 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009552 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009553 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009554
Chet Haasec3aa3612010-06-17 08:50:37 -07009555 mTop += offset;
9556 mBottom += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009557 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009558 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009559 invalidateViewProperty(false, false);
9560 } else {
9561 if (!matrixIsIdentity) {
9562 invalidateViewProperty(false, true);
9563 }
9564 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009565 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009566 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009567 }
9568
9569 /**
9570 * Offset this view's horizontal location by the specified amount of pixels.
9571 *
9572 * @param offset the numer of pixels to offset the view by
9573 */
9574 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009575 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009576 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009577 final boolean matrixIsIdentity = mTransformationInfo == null
9578 || mTransformationInfo.mMatrixIsIdentity;
9579 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009580 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009581 invalidateViewProperty(false, false);
9582 } else {
9583 final ViewParent p = mParent;
9584 if (p != null && mAttachInfo != null) {
9585 final Rect r = mAttachInfo.mTmpInvalRect;
9586 int minLeft;
9587 int maxRight;
9588 if (offset < 0) {
9589 minLeft = mLeft + offset;
9590 maxRight = mRight;
9591 } else {
9592 minLeft = mLeft;
9593 maxRight = mRight + offset;
9594 }
9595 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
9596 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009597 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009598 }
9599 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009600 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009601 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009602
Chet Haasec3aa3612010-06-17 08:50:37 -07009603 mLeft += offset;
9604 mRight += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009605 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009606 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009607 invalidateViewProperty(false, false);
9608 } else {
9609 if (!matrixIsIdentity) {
9610 invalidateViewProperty(false, true);
9611 }
9612 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009613 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009614 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009615 }
9616
9617 /**
9618 * Get the LayoutParams associated with this view. All views should have
9619 * layout parameters. These supply parameters to the <i>parent</i> of this
9620 * view specifying how it should be arranged. There are many subclasses of
9621 * ViewGroup.LayoutParams, and these correspond to the different subclasses
9622 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08009623 *
9624 * This method may return null if this View is not attached to a parent
9625 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
9626 * was not invoked successfully. When a View is attached to a parent
9627 * ViewGroup, this method must not return null.
9628 *
9629 * @return The LayoutParams associated with this view, or null if no
9630 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009631 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07009632 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009633 public ViewGroup.LayoutParams getLayoutParams() {
9634 return mLayoutParams;
9635 }
9636
9637 /**
9638 * Set the layout parameters associated with this view. These supply
9639 * parameters to the <i>parent</i> of this view specifying how it should be
9640 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
9641 * correspond to the different subclasses of ViewGroup that are responsible
9642 * for arranging their children.
9643 *
Romain Guy01c174b2011-02-22 11:51:06 -08009644 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009645 */
9646 public void setLayoutParams(ViewGroup.LayoutParams params) {
9647 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08009648 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009649 }
9650 mLayoutParams = params;
Philip Milned7dd8902012-01-26 16:55:30 -08009651 if (mParent instanceof ViewGroup) {
9652 ((ViewGroup) mParent).onSetLayoutParams(this, params);
9653 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009654 requestLayout();
9655 }
9656
9657 /**
9658 * Set the scrolled position of your view. This will cause a call to
9659 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9660 * invalidated.
9661 * @param x the x position to scroll to
9662 * @param y the y position to scroll to
9663 */
9664 public void scrollTo(int x, int y) {
9665 if (mScrollX != x || mScrollY != y) {
9666 int oldX = mScrollX;
9667 int oldY = mScrollY;
9668 mScrollX = x;
9669 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009670 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009671 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07009672 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009673 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -07009674 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009675 }
9676 }
9677
9678 /**
9679 * Move the scrolled position of your view. This will cause a call to
9680 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9681 * invalidated.
9682 * @param x the amount of pixels to scroll by horizontally
9683 * @param y the amount of pixels to scroll by vertically
9684 */
9685 public void scrollBy(int x, int y) {
9686 scrollTo(mScrollX + x, mScrollY + y);
9687 }
9688
9689 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009690 * <p>Trigger the scrollbars to draw. When invoked this method starts an
9691 * animation to fade the scrollbars out after a default delay. If a subclass
9692 * provides animated scrolling, the start delay should equal the duration
9693 * of the scrolling animation.</p>
9694 *
9695 * <p>The animation starts only if at least one of the scrollbars is
9696 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
9697 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9698 * this method returns true, and false otherwise. If the animation is
9699 * started, this method calls {@link #invalidate()}; in that case the
9700 * caller should not call {@link #invalidate()}.</p>
9701 *
9702 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07009703 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07009704 *
9705 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
9706 * and {@link #scrollTo(int, int)}.</p>
9707 *
9708 * @return true if the animation is played, false otherwise
9709 *
9710 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07009711 * @see #scrollBy(int, int)
9712 * @see #scrollTo(int, int)
9713 * @see #isHorizontalScrollBarEnabled()
9714 * @see #isVerticalScrollBarEnabled()
9715 * @see #setHorizontalScrollBarEnabled(boolean)
9716 * @see #setVerticalScrollBarEnabled(boolean)
9717 */
9718 protected boolean awakenScrollBars() {
9719 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07009720 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07009721 }
9722
9723 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07009724 * Trigger the scrollbars to draw.
9725 * This method differs from awakenScrollBars() only in its default duration.
9726 * initialAwakenScrollBars() will show the scroll bars for longer than
9727 * usual to give the user more of a chance to notice them.
9728 *
9729 * @return true if the animation is played, false otherwise.
9730 */
9731 private boolean initialAwakenScrollBars() {
9732 return mScrollCache != null &&
9733 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
9734 }
9735
9736 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009737 * <p>
9738 * Trigger the scrollbars to draw. When invoked this method starts an
9739 * animation to fade the scrollbars out after a fixed delay. If a subclass
9740 * provides animated scrolling, the start delay should equal the duration of
9741 * the scrolling animation.
9742 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009743 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009744 * <p>
9745 * The animation starts only if at least one of the scrollbars is enabled,
9746 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9747 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9748 * this method returns true, and false otherwise. If the animation is
9749 * started, this method calls {@link #invalidate()}; in that case the caller
9750 * should not call {@link #invalidate()}.
9751 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009752 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009753 * <p>
9754 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07009755 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07009756 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009757 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009758 * @param startDelay the delay, in milliseconds, after which the animation
9759 * should start; when the delay is 0, the animation starts
9760 * immediately
9761 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009762 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009763 * @see #scrollBy(int, int)
9764 * @see #scrollTo(int, int)
9765 * @see #isHorizontalScrollBarEnabled()
9766 * @see #isVerticalScrollBarEnabled()
9767 * @see #setHorizontalScrollBarEnabled(boolean)
9768 * @see #setVerticalScrollBarEnabled(boolean)
9769 */
9770 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07009771 return awakenScrollBars(startDelay, true);
9772 }
Joe Malin32736f02011-01-19 16:14:20 -08009773
Mike Cleron290947b2009-09-29 18:34:32 -07009774 /**
9775 * <p>
9776 * Trigger the scrollbars to draw. When invoked this method starts an
9777 * animation to fade the scrollbars out after a fixed delay. If a subclass
9778 * provides animated scrolling, the start delay should equal the duration of
9779 * the scrolling animation.
9780 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009781 *
Mike Cleron290947b2009-09-29 18:34:32 -07009782 * <p>
9783 * The animation starts only if at least one of the scrollbars is enabled,
9784 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9785 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9786 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08009787 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07009788 * is set to true; in that case the caller
9789 * should not call {@link #invalidate()}.
9790 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009791 *
Mike Cleron290947b2009-09-29 18:34:32 -07009792 * <p>
9793 * This method should be invoked everytime a subclass directly updates the
9794 * scroll parameters.
9795 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009796 *
Mike Cleron290947b2009-09-29 18:34:32 -07009797 * @param startDelay the delay, in milliseconds, after which the animation
9798 * should start; when the delay is 0, the animation starts
9799 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08009800 *
Mike Cleron290947b2009-09-29 18:34:32 -07009801 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08009802 *
Mike Cleron290947b2009-09-29 18:34:32 -07009803 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009804 *
Mike Cleron290947b2009-09-29 18:34:32 -07009805 * @see #scrollBy(int, int)
9806 * @see #scrollTo(int, int)
9807 * @see #isHorizontalScrollBarEnabled()
9808 * @see #isVerticalScrollBarEnabled()
9809 * @see #setHorizontalScrollBarEnabled(boolean)
9810 * @see #setVerticalScrollBarEnabled(boolean)
9811 */
9812 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07009813 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08009814
Mike Cleronf116bf82009-09-27 19:14:12 -07009815 if (scrollCache == null || !scrollCache.fadeScrollBars) {
9816 return false;
9817 }
9818
9819 if (scrollCache.scrollBar == null) {
9820 scrollCache.scrollBar = new ScrollBarDrawable();
9821 }
9822
9823 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
9824
Mike Cleron290947b2009-09-29 18:34:32 -07009825 if (invalidate) {
9826 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009827 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -07009828 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009829
9830 if (scrollCache.state == ScrollabilityCache.OFF) {
9831 // FIXME: this is copied from WindowManagerService.
9832 // We should get this value from the system when it
9833 // is possible to do so.
9834 final int KEY_REPEAT_FIRST_DELAY = 750;
9835 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
9836 }
9837
9838 // Tell mScrollCache when we should start fading. This may
9839 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07009840 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07009841 scrollCache.fadeStartTime = fadeStartTime;
9842 scrollCache.state = ScrollabilityCache.ON;
9843
9844 // Schedule our fader to run, unscheduling any old ones first
9845 if (mAttachInfo != null) {
9846 mAttachInfo.mHandler.removeCallbacks(scrollCache);
9847 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
9848 }
9849
9850 return true;
9851 }
9852
9853 return false;
9854 }
9855
9856 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07009857 * Do not invalidate views which are not visible and which are not running an animation. They
9858 * will not get drawn and they should not set dirty flags as if they will be drawn
9859 */
9860 private boolean skipInvalidate() {
9861 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
9862 (!(mParent instanceof ViewGroup) ||
9863 !((ViewGroup) mParent).isViewTransitioning(this));
9864 }
9865 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07009866 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009867 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
9868 * in the future. This must be called from a UI thread. To call from a non-UI
9869 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009870 *
9871 * WARNING: This method is destructive to dirty.
9872 * @param dirty the rectangle representing the bounds of the dirty region
9873 */
9874 public void invalidate(Rect dirty) {
Chet Haaseaceafe62011-08-26 15:44:33 -07009875 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009876 return;
9877 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009878 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08009879 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
9880 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009881 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08009882 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07009883 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009884 final ViewParent p = mParent;
9885 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08009886 //noinspection PointlessBooleanExpression,ConstantConditions
9887 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9888 if (p != null && ai != null && ai.mHardwareAccelerated) {
9889 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009890 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009891 p.invalidateChild(this, null);
9892 return;
9893 }
Romain Guyaf636eb2010-12-09 17:47:21 -08009894 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009895 if (p != null && ai != null) {
9896 final int scrollX = mScrollX;
9897 final int scrollY = mScrollY;
9898 final Rect r = ai.mTmpInvalRect;
9899 r.set(dirty.left - scrollX, dirty.top - scrollY,
9900 dirty.right - scrollX, dirty.bottom - scrollY);
9901 mParent.invalidateChild(this, r);
9902 }
9903 }
9904 }
9905
9906 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07009907 * 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 -08009908 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009909 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
9910 * will be called at some point in the future. This must be called from
9911 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009912 * @param l the left position of the dirty region
9913 * @param t the top position of the dirty region
9914 * @param r the right position of the dirty region
9915 * @param b the bottom position of the dirty region
9916 */
9917 public void invalidate(int l, int t, int r, int b) {
Chet Haaseaceafe62011-08-26 15:44:33 -07009918 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009919 return;
9920 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009921 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08009922 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
9923 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009924 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08009925 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07009926 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009927 final ViewParent p = mParent;
9928 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08009929 //noinspection PointlessBooleanExpression,ConstantConditions
9930 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9931 if (p != null && ai != null && ai.mHardwareAccelerated) {
9932 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009933 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009934 p.invalidateChild(this, null);
9935 return;
9936 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08009937 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009938 if (p != null && ai != null && l < r && t < b) {
9939 final int scrollX = mScrollX;
9940 final int scrollY = mScrollY;
9941 final Rect tmpr = ai.mTmpInvalRect;
9942 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
9943 p.invalidateChild(this, tmpr);
9944 }
9945 }
9946 }
9947
9948 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009949 * Invalidate the whole view. If the view is visible,
9950 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
9951 * the future. This must be called from a UI thread. To call from a non-UI thread,
9952 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009953 */
9954 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07009955 invalidate(true);
9956 }
Joe Malin32736f02011-01-19 16:14:20 -08009957
Chet Haaseed032702010-10-01 14:05:54 -07009958 /**
9959 * This is where the invalidate() work actually happens. A full invalidate()
9960 * causes the drawing cache to be invalidated, but this function can be called with
9961 * invalidateCache set to false to skip that invalidation step for cases that do not
9962 * need it (for example, a component that remains at the same dimensions with the same
9963 * content).
9964 *
9965 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
9966 * well. This is usually true for a full invalidate, but may be set to false if the
9967 * View's contents or dimensions have not changed.
9968 */
Romain Guy849d0a32011-02-01 17:20:48 -08009969 void invalidate(boolean invalidateCache) {
Chet Haaseaceafe62011-08-26 15:44:33 -07009970 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009971 return;
9972 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009973 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08009974 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08009975 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
9976 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07009977 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07009978 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -07009979 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08009980 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07009981 mPrivateFlags &= ~DRAWING_CACHE_VALID;
9982 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009983 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07009984 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08009985 //noinspection PointlessBooleanExpression,ConstantConditions
9986 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9987 if (p != null && ai != null && ai.mHardwareAccelerated) {
9988 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009989 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009990 p.invalidateChild(this, null);
9991 return;
9992 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08009993 }
Michael Jurkaebefea42010-11-15 16:04:01 -08009994
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009995 if (p != null && ai != null) {
9996 final Rect r = ai.mTmpInvalRect;
9997 r.set(0, 0, mRight - mLeft, mBottom - mTop);
9998 // Don't call invalidate -- we don't want to internally scroll
9999 // our own bounds
10000 p.invalidateChild(this, r);
10001 }
10002 }
10003 }
10004
10005 /**
Chet Haase9d1992d2012-03-13 11:03:25 -070010006 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
10007 * set any flags or handle all of the cases handled by the default invalidation methods.
10008 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
10009 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
10010 * walk up the hierarchy, transforming the dirty rect as necessary.
10011 *
10012 * The method also handles normal invalidation logic if display list properties are not
10013 * being used in this view. The invalidateParent and forceRedraw flags are used by that
10014 * backup approach, to handle these cases used in the various property-setting methods.
10015 *
10016 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
10017 * are not being used in this view
10018 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
10019 * list properties are not being used in this view
10020 */
10021 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
Chet Haase1271e2c2012-04-20 09:54:27 -070010022 if (mDisplayList == null || (mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
Chet Haase9d1992d2012-03-13 11:03:25 -070010023 if (invalidateParent) {
10024 invalidateParentCaches();
10025 }
10026 if (forceRedraw) {
10027 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
10028 }
10029 invalidate(false);
10030 } else {
10031 final AttachInfo ai = mAttachInfo;
10032 final ViewParent p = mParent;
10033 if (p != null && ai != null) {
10034 final Rect r = ai.mTmpInvalRect;
10035 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10036 if (mParent instanceof ViewGroup) {
10037 ((ViewGroup) mParent).invalidateChildFast(this, r);
10038 } else {
10039 mParent.invalidateChild(this, r);
10040 }
10041 }
10042 }
10043 }
10044
10045 /**
10046 * Utility method to transform a given Rect by the current matrix of this view.
10047 */
10048 void transformRect(final Rect rect) {
10049 if (!getMatrix().isIdentity()) {
10050 RectF boundingRect = mAttachInfo.mTmpTransformRect;
10051 boundingRect.set(rect);
10052 getMatrix().mapRect(boundingRect);
10053 rect.set((int) (boundingRect.left - 0.5f),
10054 (int) (boundingRect.top - 0.5f),
10055 (int) (boundingRect.right + 0.5f),
10056 (int) (boundingRect.bottom + 0.5f));
10057 }
10058 }
10059
10060 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -080010061 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -080010062 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10063 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -080010064 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
10065 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -080010066 *
10067 * @hide
10068 */
Romain Guy0fd89bf2011-01-26 15:41:30 -080010069 protected void invalidateParentCaches() {
10070 if (mParent instanceof View) {
10071 ((View) mParent).mPrivateFlags |= INVALIDATED;
10072 }
10073 }
Joe Malin32736f02011-01-19 16:14:20 -080010074
Romain Guy0fd89bf2011-01-26 15:41:30 -080010075 /**
10076 * Used to indicate that the parent of this view should be invalidated. This functionality
10077 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10078 * which is necessary when various parent-managed properties of the view change, such as
10079 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
10080 * an invalidation event to the parent.
10081 *
10082 * @hide
10083 */
10084 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -080010085 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010086 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -080010087 }
10088 }
10089
10090 /**
Romain Guy24443ea2009-05-11 11:56:30 -070010091 * Indicates whether this View is opaque. An opaque View guarantees that it will
10092 * draw all the pixels overlapping its bounds using a fully opaque color.
10093 *
10094 * Subclasses of View should override this method whenever possible to indicate
10095 * whether an instance is opaque. Opaque Views are treated in a special way by
10096 * the View hierarchy, possibly allowing it to perform optimizations during
10097 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -070010098 *
Romain Guy24443ea2009-05-11 11:56:30 -070010099 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -070010100 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010101 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -070010102 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -070010103 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Romain Guyf8773082012-07-12 18:01:00 -070010104 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1.0f) >= 1.0f);
Romain Guy8f1344f52009-05-15 16:03:59 -070010105 }
10106
Adam Powell20232d02010-12-08 21:08:53 -080010107 /**
10108 * @hide
10109 */
10110 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -070010111 // Opaque if:
10112 // - Has a background
10113 // - Background is opaque
10114 // - Doesn't have scrollbars or scrollbars are inside overlay
10115
Philip Milne6c8ea062012-04-03 17:38:43 -070010116 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Romain Guy8f1344f52009-05-15 16:03:59 -070010117 mPrivateFlags |= OPAQUE_BACKGROUND;
10118 } else {
10119 mPrivateFlags &= ~OPAQUE_BACKGROUND;
10120 }
10121
10122 final int flags = mViewFlags;
10123 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
10124 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
10125 mPrivateFlags |= OPAQUE_SCROLLBARS;
10126 } else {
10127 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
10128 }
10129 }
10130
10131 /**
10132 * @hide
10133 */
10134 protected boolean hasOpaqueScrollbars() {
10135 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -070010136 }
10137
10138 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010139 * @return A handler associated with the thread running the View. This
10140 * handler can be used to pump events in the UI events queue.
10141 */
10142 public Handler getHandler() {
10143 if (mAttachInfo != null) {
10144 return mAttachInfo.mHandler;
10145 }
10146 return null;
10147 }
10148
10149 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080010150 * Gets the view root associated with the View.
10151 * @return The view root, or null if none.
10152 * @hide
10153 */
10154 public ViewRootImpl getViewRootImpl() {
10155 if (mAttachInfo != null) {
10156 return mAttachInfo.mViewRootImpl;
10157 }
10158 return null;
10159 }
10160
10161 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010162 * <p>Causes the Runnable to be added to the message queue.
10163 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010164 *
Romain Guye63a4f32011-08-11 11:33:31 -070010165 * <p>This method can be invoked from outside of the UI thread
10166 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010167 *
10168 * @param action The Runnable that will be executed.
10169 *
10170 * @return Returns true if the Runnable was successfully placed in to the
10171 * message queue. Returns false on failure, usually because the
10172 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010173 *
10174 * @see #postDelayed
10175 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010176 */
10177 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010178 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010179 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010180 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010181 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010182 // Assume that post will succeed later
10183 ViewRootImpl.getRunQueue().post(action);
10184 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010185 }
10186
10187 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010188 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010189 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -070010190 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010191 *
Romain Guye63a4f32011-08-11 11:33:31 -070010192 * <p>This method can be invoked from outside of the UI thread
10193 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010194 *
10195 * @param action The Runnable that will be executed.
10196 * @param delayMillis The delay (in milliseconds) until the Runnable
10197 * will be executed.
10198 *
10199 * @return true if the Runnable was successfully placed in to the
10200 * message queue. Returns false on failure, usually because the
10201 * looper processing the message queue is exiting. Note that a
10202 * result of true does not mean the Runnable will be processed --
10203 * if the looper is quit before the delivery time of the message
10204 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010205 *
10206 * @see #post
10207 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010208 */
10209 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010210 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010211 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010212 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010213 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010214 // Assume that post will succeed later
10215 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10216 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010217 }
10218
10219 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010220 * <p>Causes the Runnable to execute on the next animation time step.
10221 * The runnable will be run on the user interface thread.</p>
10222 *
10223 * <p>This method can be invoked from outside of the UI thread
10224 * only when this View is attached to a window.</p>
10225 *
10226 * @param action The Runnable that will be executed.
10227 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010228 * @see #postOnAnimationDelayed
10229 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010230 */
10231 public void postOnAnimation(Runnable action) {
10232 final AttachInfo attachInfo = mAttachInfo;
10233 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010234 attachInfo.mViewRootImpl.mChoreographer.postCallback(
10235 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010236 } else {
10237 // Assume that post will succeed later
10238 ViewRootImpl.getRunQueue().post(action);
10239 }
10240 }
10241
10242 /**
10243 * <p>Causes the Runnable to execute on the next animation time step,
10244 * after the specified amount of time elapses.
10245 * The runnable will be run on the user interface thread.</p>
10246 *
10247 * <p>This method can be invoked from outside of the UI thread
10248 * only when this View is attached to a window.</p>
10249 *
10250 * @param action The Runnable that will be executed.
10251 * @param delayMillis The delay (in milliseconds) until the Runnable
10252 * will be executed.
10253 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010254 * @see #postOnAnimation
10255 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010256 */
10257 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
10258 final AttachInfo attachInfo = mAttachInfo;
10259 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010260 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
10261 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010262 } else {
10263 // Assume that post will succeed later
10264 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10265 }
10266 }
10267
10268 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010269 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010270 *
Romain Guye63a4f32011-08-11 11:33:31 -070010271 * <p>This method can be invoked from outside of the UI thread
10272 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010273 *
10274 * @param action The Runnable to remove from the message handling queue
10275 *
10276 * @return true if this view could ask the Handler to remove the Runnable,
10277 * false otherwise. When the returned value is true, the Runnable
10278 * may or may not have been actually removed from the message queue
10279 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010280 *
10281 * @see #post
10282 * @see #postDelayed
10283 * @see #postOnAnimation
10284 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010285 */
10286 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080010287 if (action != null) {
10288 final AttachInfo attachInfo = mAttachInfo;
10289 if (attachInfo != null) {
10290 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010291 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
10292 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -080010293 } else {
10294 // Assume that post will succeed later
10295 ViewRootImpl.getRunQueue().removeCallbacks(action);
10296 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010297 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010298 return true;
10299 }
10300
10301 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010302 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
10303 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010304 *
Romain Guye63a4f32011-08-11 11:33:31 -070010305 * <p>This method can be invoked from outside of the UI thread
10306 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010307 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010308 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010309 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010310 */
10311 public void postInvalidate() {
10312 postInvalidateDelayed(0);
10313 }
10314
10315 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010316 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10317 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010318 *
Romain Guye63a4f32011-08-11 11:33:31 -070010319 * <p>This method can be invoked from outside of the UI thread
10320 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010321 *
10322 * @param left The left coordinate of the rectangle to invalidate.
10323 * @param top The top coordinate of the rectangle to invalidate.
10324 * @param right The right coordinate of the rectangle to invalidate.
10325 * @param bottom The bottom coordinate of the rectangle to invalidate.
10326 *
10327 * @see #invalidate(int, int, int, int)
10328 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010329 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010330 */
10331 public void postInvalidate(int left, int top, int right, int bottom) {
10332 postInvalidateDelayed(0, left, top, right, bottom);
10333 }
10334
10335 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010336 * <p>Cause an invalidate to happen on a subsequent cycle through the event
10337 * loop. Waits for the specified amount of time.</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 delayMilliseconds the duration in milliseconds to delay the
10343 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010344 *
10345 * @see #invalidate()
10346 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010347 */
10348 public void postInvalidateDelayed(long delayMilliseconds) {
10349 // We try only with the AttachInfo because there's no point in invalidating
10350 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010351 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010352 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010353 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010354 }
10355 }
10356
10357 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010358 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10359 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010360 *
Romain Guye63a4f32011-08-11 11:33:31 -070010361 * <p>This method can be invoked from outside of the UI thread
10362 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010363 *
10364 * @param delayMilliseconds the duration in milliseconds to delay the
10365 * invalidation by
10366 * @param left The left coordinate of the rectangle to invalidate.
10367 * @param top The top coordinate of the rectangle to invalidate.
10368 * @param right The right coordinate of the rectangle to invalidate.
10369 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010370 *
10371 * @see #invalidate(int, int, int, int)
10372 * @see #invalidate(Rect)
10373 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010374 */
10375 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
10376 int right, int bottom) {
10377
10378 // We try only with the AttachInfo because there's no point in invalidating
10379 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010380 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010381 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010382 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10383 info.target = this;
10384 info.left = left;
10385 info.top = top;
10386 info.right = right;
10387 info.bottom = bottom;
10388
Jeff Browna175a5b2012-02-15 19:18:31 -080010389 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010390 }
10391 }
10392
10393 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010394 * <p>Cause an invalidate to happen on the next animation time step, typically the
10395 * next display frame.</p>
10396 *
10397 * <p>This method can be invoked from outside of the UI thread
10398 * only when this View is attached to a window.</p>
10399 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010400 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010401 */
10402 public void postInvalidateOnAnimation() {
10403 // We try only with the AttachInfo because there's no point in invalidating
10404 // if we are not attached to our window
10405 final AttachInfo attachInfo = mAttachInfo;
10406 if (attachInfo != null) {
10407 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10408 }
10409 }
10410
10411 /**
10412 * <p>Cause an invalidate of the specified area to happen on the next animation
10413 * time step, typically the next display frame.</p>
10414 *
10415 * <p>This method can be invoked from outside of the UI thread
10416 * only when this View is attached to a window.</p>
10417 *
10418 * @param left The left coordinate of the rectangle to invalidate.
10419 * @param top The top coordinate of the rectangle to invalidate.
10420 * @param right The right coordinate of the rectangle to invalidate.
10421 * @param bottom The bottom coordinate of the rectangle to invalidate.
10422 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010423 * @see #invalidate(int, int, int, int)
10424 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010425 */
10426 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10427 // We try only with the AttachInfo because there's no point in invalidating
10428 // if we are not attached to our window
10429 final AttachInfo attachInfo = mAttachInfo;
10430 if (attachInfo != null) {
10431 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10432 info.target = this;
10433 info.left = left;
10434 info.top = top;
10435 info.right = right;
10436 info.bottom = bottom;
10437
10438 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10439 }
10440 }
10441
10442 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010443 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10444 * This event is sent at most once every
10445 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10446 */
10447 private void postSendViewScrolledAccessibilityEventCallback() {
10448 if (mSendViewScrolledAccessibilityEvent == null) {
10449 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10450 }
10451 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10452 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10453 postDelayed(mSendViewScrolledAccessibilityEvent,
10454 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10455 }
10456 }
10457
10458 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010459 * Called by a parent to request that a child update its values for mScrollX
10460 * and mScrollY if necessary. This will typically be done if the child is
10461 * animating a scroll using a {@link android.widget.Scroller Scroller}
10462 * object.
10463 */
10464 public void computeScroll() {
10465 }
10466
10467 /**
10468 * <p>Indicate whether the horizontal edges are faded when the view is
10469 * scrolled horizontally.</p>
10470 *
10471 * @return true if the horizontal edges should are faded on scroll, false
10472 * otherwise
10473 *
10474 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010475 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010476 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010477 */
10478 public boolean isHorizontalFadingEdgeEnabled() {
10479 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10480 }
10481
10482 /**
10483 * <p>Define whether the horizontal edges should be faded when this view
10484 * is scrolled horizontally.</p>
10485 *
10486 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10487 * be faded when the view is scrolled
10488 * horizontally
10489 *
10490 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010491 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010492 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010493 */
10494 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10495 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10496 if (horizontalFadingEdgeEnabled) {
10497 initScrollCache();
10498 }
10499
10500 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10501 }
10502 }
10503
10504 /**
10505 * <p>Indicate whether the vertical edges are faded when the view is
10506 * scrolled horizontally.</p>
10507 *
10508 * @return true if the vertical edges should are faded on scroll, false
10509 * otherwise
10510 *
10511 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010512 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010513 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010514 */
10515 public boolean isVerticalFadingEdgeEnabled() {
10516 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10517 }
10518
10519 /**
10520 * <p>Define whether the vertical edges should be faded when this view
10521 * is scrolled vertically.</p>
10522 *
10523 * @param verticalFadingEdgeEnabled true if the vertical edges should
10524 * be faded when the view is scrolled
10525 * vertically
10526 *
10527 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010528 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010529 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010530 */
10531 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10532 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10533 if (verticalFadingEdgeEnabled) {
10534 initScrollCache();
10535 }
10536
10537 mViewFlags ^= FADING_EDGE_VERTICAL;
10538 }
10539 }
10540
10541 /**
10542 * Returns the strength, or intensity, of the top faded edge. The strength is
10543 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10544 * returns 0.0 or 1.0 but no value in between.
10545 *
10546 * Subclasses should override this method to provide a smoother fade transition
10547 * when scrolling occurs.
10548 *
10549 * @return the intensity of the top fade as a float between 0.0f and 1.0f
10550 */
10551 protected float getTopFadingEdgeStrength() {
10552 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
10553 }
10554
10555 /**
10556 * Returns the strength, or intensity, of the bottom faded edge. The strength is
10557 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10558 * returns 0.0 or 1.0 but no value in between.
10559 *
10560 * Subclasses should override this method to provide a smoother fade transition
10561 * when scrolling occurs.
10562 *
10563 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
10564 */
10565 protected float getBottomFadingEdgeStrength() {
10566 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
10567 computeVerticalScrollRange() ? 1.0f : 0.0f;
10568 }
10569
10570 /**
10571 * Returns the strength, or intensity, of the left faded edge. The strength is
10572 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10573 * returns 0.0 or 1.0 but no value in between.
10574 *
10575 * Subclasses should override this method to provide a smoother fade transition
10576 * when scrolling occurs.
10577 *
10578 * @return the intensity of the left fade as a float between 0.0f and 1.0f
10579 */
10580 protected float getLeftFadingEdgeStrength() {
10581 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
10582 }
10583
10584 /**
10585 * Returns the strength, or intensity, of the right faded edge. The strength is
10586 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10587 * returns 0.0 or 1.0 but no value in between.
10588 *
10589 * Subclasses should override this method to provide a smoother fade transition
10590 * when scrolling occurs.
10591 *
10592 * @return the intensity of the right fade as a float between 0.0f and 1.0f
10593 */
10594 protected float getRightFadingEdgeStrength() {
10595 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
10596 computeHorizontalScrollRange() ? 1.0f : 0.0f;
10597 }
10598
10599 /**
10600 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
10601 * scrollbar is not drawn by default.</p>
10602 *
10603 * @return true if the horizontal scrollbar should be painted, false
10604 * otherwise
10605 *
10606 * @see #setHorizontalScrollBarEnabled(boolean)
10607 */
10608 public boolean isHorizontalScrollBarEnabled() {
10609 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10610 }
10611
10612 /**
10613 * <p>Define whether the horizontal scrollbar should be drawn or not. The
10614 * scrollbar is not drawn by default.</p>
10615 *
10616 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
10617 * be painted
10618 *
10619 * @see #isHorizontalScrollBarEnabled()
10620 */
10621 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
10622 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
10623 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010624 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010625 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010626 }
10627 }
10628
10629 /**
10630 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
10631 * scrollbar is not drawn by default.</p>
10632 *
10633 * @return true if the vertical scrollbar should be painted, false
10634 * otherwise
10635 *
10636 * @see #setVerticalScrollBarEnabled(boolean)
10637 */
10638 public boolean isVerticalScrollBarEnabled() {
10639 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
10640 }
10641
10642 /**
10643 * <p>Define whether the vertical scrollbar should be drawn or not. The
10644 * scrollbar is not drawn by default.</p>
10645 *
10646 * @param verticalScrollBarEnabled true if the vertical scrollbar should
10647 * be painted
10648 *
10649 * @see #isVerticalScrollBarEnabled()
10650 */
10651 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
10652 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
10653 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010654 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010655 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010656 }
10657 }
10658
Adam Powell20232d02010-12-08 21:08:53 -080010659 /**
10660 * @hide
10661 */
10662 protected void recomputePadding() {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070010663 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010664 }
Joe Malin32736f02011-01-19 16:14:20 -080010665
Mike Cleronfe81d382009-09-28 14:22:16 -070010666 /**
10667 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080010668 *
Mike Cleronfe81d382009-09-28 14:22:16 -070010669 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080010670 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010671 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070010672 */
10673 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
10674 initScrollCache();
10675 final ScrollabilityCache scrollabilityCache = mScrollCache;
10676 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010677 if (fadeScrollbars) {
10678 scrollabilityCache.state = ScrollabilityCache.OFF;
10679 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070010680 scrollabilityCache.state = ScrollabilityCache.ON;
10681 }
10682 }
Joe Malin32736f02011-01-19 16:14:20 -080010683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010684 /**
Joe Malin32736f02011-01-19 16:14:20 -080010685 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010686 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080010687 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010688 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070010689 *
10690 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070010691 */
10692 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080010693 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010694 }
Joe Malin32736f02011-01-19 16:14:20 -080010695
Mike Cleron52f0a642009-09-28 18:21:37 -070010696 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070010697 *
10698 * Returns the delay before scrollbars fade.
10699 *
10700 * @return the delay before scrollbars fade
10701 *
10702 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10703 */
10704 public int getScrollBarDefaultDelayBeforeFade() {
10705 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
10706 mScrollCache.scrollBarDefaultDelayBeforeFade;
10707 }
10708
10709 /**
10710 * Define the delay before scrollbars fade.
10711 *
10712 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
10713 *
10714 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10715 */
10716 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
10717 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
10718 }
10719
10720 /**
10721 *
10722 * Returns the scrollbar fade duration.
10723 *
10724 * @return the scrollbar fade duration
10725 *
10726 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10727 */
10728 public int getScrollBarFadeDuration() {
10729 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
10730 mScrollCache.scrollBarFadeDuration;
10731 }
10732
10733 /**
10734 * Define the scrollbar fade duration.
10735 *
10736 * @param scrollBarFadeDuration - the scrollbar fade duration
10737 *
10738 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10739 */
10740 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
10741 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
10742 }
10743
10744 /**
10745 *
10746 * Returns the scrollbar size.
10747 *
10748 * @return the scrollbar size
10749 *
10750 * @attr ref android.R.styleable#View_scrollbarSize
10751 */
10752 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070010753 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070010754 mScrollCache.scrollBarSize;
10755 }
10756
10757 /**
10758 * Define the scrollbar size.
10759 *
10760 * @param scrollBarSize - the scrollbar size
10761 *
10762 * @attr ref android.R.styleable#View_scrollbarSize
10763 */
10764 public void setScrollBarSize(int scrollBarSize) {
10765 getScrollCache().scrollBarSize = scrollBarSize;
10766 }
10767
10768 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010769 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
10770 * inset. When inset, they add to the padding of the view. And the scrollbars
10771 * can be drawn inside the padding area or on the edge of the view. For example,
10772 * if a view has a background drawable and you want to draw the scrollbars
10773 * inside the padding specified by the drawable, you can use
10774 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
10775 * appear at the edge of the view, ignoring the padding, then you can use
10776 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
10777 * @param style the style of the scrollbars. Should be one of
10778 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
10779 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
10780 * @see #SCROLLBARS_INSIDE_OVERLAY
10781 * @see #SCROLLBARS_INSIDE_INSET
10782 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10783 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010784 *
10785 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010786 */
10787 public void setScrollBarStyle(int style) {
10788 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
10789 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070010790 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010791 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010792 }
10793 }
10794
10795 /**
10796 * <p>Returns the current scrollbar style.</p>
10797 * @return the current scrollbar style
10798 * @see #SCROLLBARS_INSIDE_OVERLAY
10799 * @see #SCROLLBARS_INSIDE_INSET
10800 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10801 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010802 *
10803 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010804 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070010805 @ViewDebug.ExportedProperty(mapping = {
10806 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
10807 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
10808 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
10809 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
10810 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010811 public int getScrollBarStyle() {
10812 return mViewFlags & SCROLLBARS_STYLE_MASK;
10813 }
10814
10815 /**
10816 * <p>Compute the horizontal range that the horizontal scrollbar
10817 * represents.</p>
10818 *
10819 * <p>The range is expressed in arbitrary units that must be the same as the
10820 * units used by {@link #computeHorizontalScrollExtent()} and
10821 * {@link #computeHorizontalScrollOffset()}.</p>
10822 *
10823 * <p>The default range is the drawing width of this view.</p>
10824 *
10825 * @return the total horizontal range represented by the horizontal
10826 * scrollbar
10827 *
10828 * @see #computeHorizontalScrollExtent()
10829 * @see #computeHorizontalScrollOffset()
10830 * @see android.widget.ScrollBarDrawable
10831 */
10832 protected int computeHorizontalScrollRange() {
10833 return getWidth();
10834 }
10835
10836 /**
10837 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
10838 * within the horizontal range. This value is used to compute the position
10839 * of the thumb within the scrollbar's track.</p>
10840 *
10841 * <p>The range is expressed in arbitrary units that must be the same as the
10842 * units used by {@link #computeHorizontalScrollRange()} and
10843 * {@link #computeHorizontalScrollExtent()}.</p>
10844 *
10845 * <p>The default offset is the scroll offset of this view.</p>
10846 *
10847 * @return the horizontal offset of the scrollbar's thumb
10848 *
10849 * @see #computeHorizontalScrollRange()
10850 * @see #computeHorizontalScrollExtent()
10851 * @see android.widget.ScrollBarDrawable
10852 */
10853 protected int computeHorizontalScrollOffset() {
10854 return mScrollX;
10855 }
10856
10857 /**
10858 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
10859 * within the horizontal range. This value is used to compute the length
10860 * of the thumb within the scrollbar's track.</p>
10861 *
10862 * <p>The range is expressed in arbitrary units that must be the same as the
10863 * units used by {@link #computeHorizontalScrollRange()} and
10864 * {@link #computeHorizontalScrollOffset()}.</p>
10865 *
10866 * <p>The default extent is the drawing width of this view.</p>
10867 *
10868 * @return the horizontal extent of the scrollbar's thumb
10869 *
10870 * @see #computeHorizontalScrollRange()
10871 * @see #computeHorizontalScrollOffset()
10872 * @see android.widget.ScrollBarDrawable
10873 */
10874 protected int computeHorizontalScrollExtent() {
10875 return getWidth();
10876 }
10877
10878 /**
10879 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
10880 *
10881 * <p>The range is expressed in arbitrary units that must be the same as the
10882 * units used by {@link #computeVerticalScrollExtent()} and
10883 * {@link #computeVerticalScrollOffset()}.</p>
10884 *
10885 * @return the total vertical range represented by the vertical scrollbar
10886 *
10887 * <p>The default range is the drawing height of this view.</p>
10888 *
10889 * @see #computeVerticalScrollExtent()
10890 * @see #computeVerticalScrollOffset()
10891 * @see android.widget.ScrollBarDrawable
10892 */
10893 protected int computeVerticalScrollRange() {
10894 return getHeight();
10895 }
10896
10897 /**
10898 * <p>Compute the vertical offset of the vertical scrollbar's thumb
10899 * within the horizontal range. This value is used to compute the position
10900 * of the thumb within the scrollbar's track.</p>
10901 *
10902 * <p>The range is expressed in arbitrary units that must be the same as the
10903 * units used by {@link #computeVerticalScrollRange()} and
10904 * {@link #computeVerticalScrollExtent()}.</p>
10905 *
10906 * <p>The default offset is the scroll offset of this view.</p>
10907 *
10908 * @return the vertical offset of the scrollbar's thumb
10909 *
10910 * @see #computeVerticalScrollRange()
10911 * @see #computeVerticalScrollExtent()
10912 * @see android.widget.ScrollBarDrawable
10913 */
10914 protected int computeVerticalScrollOffset() {
10915 return mScrollY;
10916 }
10917
10918 /**
10919 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
10920 * within the vertical range. This value is used to compute the length
10921 * of the thumb within the scrollbar's track.</p>
10922 *
10923 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080010924 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010925 * {@link #computeVerticalScrollOffset()}.</p>
10926 *
10927 * <p>The default extent is the drawing height of this view.</p>
10928 *
10929 * @return the vertical extent of the scrollbar's thumb
10930 *
10931 * @see #computeVerticalScrollRange()
10932 * @see #computeVerticalScrollOffset()
10933 * @see android.widget.ScrollBarDrawable
10934 */
10935 protected int computeVerticalScrollExtent() {
10936 return getHeight();
10937 }
10938
10939 /**
Adam Powell69159442011-06-13 17:53:06 -070010940 * Check if this view can be scrolled horizontally in a certain direction.
10941 *
10942 * @param direction Negative to check scrolling left, positive to check scrolling right.
10943 * @return true if this view can be scrolled in the specified direction, false otherwise.
10944 */
10945 public boolean canScrollHorizontally(int direction) {
10946 final int offset = computeHorizontalScrollOffset();
10947 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
10948 if (range == 0) return false;
10949 if (direction < 0) {
10950 return offset > 0;
10951 } else {
10952 return offset < range - 1;
10953 }
10954 }
10955
10956 /**
10957 * Check if this view can be scrolled vertically in a certain direction.
10958 *
10959 * @param direction Negative to check scrolling up, positive to check scrolling down.
10960 * @return true if this view can be scrolled in the specified direction, false otherwise.
10961 */
10962 public boolean canScrollVertically(int direction) {
10963 final int offset = computeVerticalScrollOffset();
10964 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
10965 if (range == 0) return false;
10966 if (direction < 0) {
10967 return offset > 0;
10968 } else {
10969 return offset < range - 1;
10970 }
10971 }
10972
10973 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010974 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
10975 * scrollbars are painted only if they have been awakened first.</p>
10976 *
10977 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080010978 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010979 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010980 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080010981 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010982 // scrollbars are drawn only when the animation is running
10983 final ScrollabilityCache cache = mScrollCache;
10984 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080010985
Mike Cleronf116bf82009-09-27 19:14:12 -070010986 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080010987
Mike Cleronf116bf82009-09-27 19:14:12 -070010988 if (state == ScrollabilityCache.OFF) {
10989 return;
10990 }
Joe Malin32736f02011-01-19 16:14:20 -080010991
Mike Cleronf116bf82009-09-27 19:14:12 -070010992 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080010993
Mike Cleronf116bf82009-09-27 19:14:12 -070010994 if (state == ScrollabilityCache.FADING) {
10995 // We're fading -- get our fade interpolation
10996 if (cache.interpolatorValues == null) {
10997 cache.interpolatorValues = new float[1];
10998 }
Joe Malin32736f02011-01-19 16:14:20 -080010999
Mike Cleronf116bf82009-09-27 19:14:12 -070011000 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080011001
Mike Cleronf116bf82009-09-27 19:14:12 -070011002 // Stops the animation if we're done
11003 if (cache.scrollBarInterpolator.timeToValues(values) ==
11004 Interpolator.Result.FREEZE_END) {
11005 cache.state = ScrollabilityCache.OFF;
11006 } else {
11007 cache.scrollBar.setAlpha(Math.round(values[0]));
11008 }
Joe Malin32736f02011-01-19 16:14:20 -080011009
11010 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070011011 // drawing. We only want this when we're fading so that
11012 // we prevent excessive redraws
11013 invalidate = true;
11014 } else {
11015 // We're just on -- but we may have been fading before so
11016 // reset alpha
11017 cache.scrollBar.setAlpha(255);
11018 }
11019
Joe Malin32736f02011-01-19 16:14:20 -080011020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011021 final int viewFlags = mViewFlags;
11022
11023 final boolean drawHorizontalScrollBar =
11024 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
11025 final boolean drawVerticalScrollBar =
11026 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
11027 && !isVerticalScrollBarHidden();
11028
11029 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
11030 final int width = mRight - mLeft;
11031 final int height = mBottom - mTop;
11032
11033 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011034
Mike Reede8853fc2009-09-04 14:01:48 -040011035 final int scrollX = mScrollX;
11036 final int scrollY = mScrollY;
11037 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
11038
Mike Cleronf116bf82009-09-27 19:14:12 -070011039 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080011040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011041 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011042 int size = scrollBar.getSize(false);
11043 if (size <= 0) {
11044 size = cache.scrollBarSize;
11045 }
11046
Mike Cleronf116bf82009-09-27 19:14:12 -070011047 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040011048 computeHorizontalScrollOffset(),
11049 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040011050 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070011051 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080011052 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070011053 left = scrollX + (mPaddingLeft & inside);
11054 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
11055 bottom = top + size;
11056 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
11057 if (invalidate) {
11058 invalidate(left, top, right, bottom);
11059 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011060 }
11061
11062 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011063 int size = scrollBar.getSize(true);
11064 if (size <= 0) {
11065 size = cache.scrollBarSize;
11066 }
11067
Mike Reede8853fc2009-09-04 14:01:48 -040011068 scrollBar.setParameters(computeVerticalScrollRange(),
11069 computeVerticalScrollOffset(),
11070 computeVerticalScrollExtent(), true);
Fabrice Di Meglioc91b6ca2012-06-22 14:51:15 -070011071 int verticalScrollbarPosition = mVerticalScrollbarPosition;
11072 if (verticalScrollbarPosition == SCROLLBAR_POSITION_DEFAULT) {
11073 verticalScrollbarPosition = isLayoutRtl() ?
11074 SCROLLBAR_POSITION_LEFT : SCROLLBAR_POSITION_RIGHT;
11075 }
11076 switch (verticalScrollbarPosition) {
Adam Powell20232d02010-12-08 21:08:53 -080011077 default:
Adam Powell20232d02010-12-08 21:08:53 -080011078 case SCROLLBAR_POSITION_RIGHT:
11079 left = scrollX + width - size - (mUserPaddingRight & inside);
11080 break;
11081 case SCROLLBAR_POSITION_LEFT:
11082 left = scrollX + (mUserPaddingLeft & inside);
11083 break;
11084 }
Mike Cleronf116bf82009-09-27 19:14:12 -070011085 top = scrollY + (mPaddingTop & inside);
11086 right = left + size;
11087 bottom = scrollY + height - (mUserPaddingBottom & inside);
11088 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
11089 if (invalidate) {
11090 invalidate(left, top, right, bottom);
11091 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011092 }
11093 }
11094 }
11095 }
Romain Guy8506ab42009-06-11 17:35:47 -070011096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011097 /**
Romain Guy8506ab42009-06-11 17:35:47 -070011098 * 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 -080011099 * FastScroller is visible.
11100 * @return whether to temporarily hide the vertical scrollbar
11101 * @hide
11102 */
11103 protected boolean isVerticalScrollBarHidden() {
11104 return false;
11105 }
11106
11107 /**
11108 * <p>Draw the horizontal scrollbar if
11109 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
11110 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011111 * @param canvas the canvas on which to draw the scrollbar
11112 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011113 *
11114 * @see #isHorizontalScrollBarEnabled()
11115 * @see #computeHorizontalScrollRange()
11116 * @see #computeHorizontalScrollExtent()
11117 * @see #computeHorizontalScrollOffset()
11118 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070011119 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011120 */
Romain Guy8fb95422010-08-17 18:38:51 -070011121 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
11122 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011123 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011124 scrollBar.draw(canvas);
11125 }
Mike Reede8853fc2009-09-04 14:01:48 -040011126
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011127 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011128 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
11129 * returns true.</p>
11130 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011131 * @param canvas the canvas on which to draw the scrollbar
11132 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011133 *
11134 * @see #isVerticalScrollBarEnabled()
11135 * @see #computeVerticalScrollRange()
11136 * @see #computeVerticalScrollExtent()
11137 * @see #computeVerticalScrollOffset()
11138 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040011139 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011140 */
Romain Guy8fb95422010-08-17 18:38:51 -070011141 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
11142 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040011143 scrollBar.setBounds(l, t, r, b);
11144 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011145 }
11146
11147 /**
11148 * Implement this to do your drawing.
11149 *
11150 * @param canvas the canvas on which the background will be drawn
11151 */
11152 protected void onDraw(Canvas canvas) {
11153 }
11154
11155 /*
11156 * Caller is responsible for calling requestLayout if necessary.
11157 * (This allows addViewInLayout to not request a new layout.)
11158 */
11159 void assignParent(ViewParent parent) {
11160 if (mParent == null) {
11161 mParent = parent;
11162 } else if (parent == null) {
11163 mParent = null;
11164 } else {
11165 throw new RuntimeException("view " + this + " being added, but"
11166 + " it already has a parent");
11167 }
11168 }
11169
11170 /**
11171 * This is called when the view is attached to a window. At this point it
11172 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011173 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
11174 * however it may be called any time before the first onDraw -- including
11175 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011176 *
11177 * @see #onDetachedFromWindow()
11178 */
11179 protected void onAttachedToWindow() {
11180 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
11181 mParent.requestTransparentRegion(this);
11182 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011183
Adam Powell8568c3a2010-04-19 14:26:11 -070011184 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
11185 initialAwakenScrollBars();
11186 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
11187 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011188
Chet Haasea9b61ac2010-12-20 07:40:25 -080011189 jumpDrawablesToCurrentState();
Romain Guy2a0f2282012-05-08 14:43:12 -070011190
Fabrice Di Meglioa6461452011-08-19 15:42:04 -070011191 // Order is important here: LayoutDirection MUST be resolved before Padding
11192 // and TextDirection
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011193 resolveLayoutDirection();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011194 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011195 resolveTextDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011196 resolveTextAlignment();
Romain Guy2a0f2282012-05-08 14:43:12 -070011197
Svetoslav Ganov42138042012-03-20 11:51:39 -070011198 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070011199 if (isFocused()) {
11200 InputMethodManager imm = InputMethodManager.peekInstance();
11201 imm.focusIn(this);
11202 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011203
11204 if (mAttachInfo != null && mDisplayList != null) {
11205 mAttachInfo.mViewRootImpl.dequeueDisplayList(mDisplayList);
11206 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011207 }
Cibu Johny86666632010-02-22 13:01:02 -080011208
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011209 /**
Romain Guybb9908b2012-03-08 11:14:07 -080011210 * @see #onScreenStateChanged(int)
11211 */
11212 void dispatchScreenStateChanged(int screenState) {
11213 onScreenStateChanged(screenState);
11214 }
11215
11216 /**
11217 * This method is called whenever the state of the screen this view is
11218 * attached to changes. A state change will usually occurs when the screen
11219 * turns on or off (whether it happens automatically or the user does it
11220 * manually.)
11221 *
11222 * @param screenState The new state of the screen. Can be either
11223 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
11224 */
11225 public void onScreenStateChanged(int screenState) {
11226 }
11227
11228 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011229 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
11230 */
11231 private boolean hasRtlSupport() {
11232 return mContext.getApplicationInfo().hasRtlSupport();
11233 }
11234
11235 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011236 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
11237 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011238 * Will call {@link View#onResolvedLayoutDirectionChanged} when resolution is done.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011239 */
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011240 public void resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011241 // Clear any previous layout direction resolution
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070011242 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011243
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011244 if (hasRtlSupport()) {
11245 // Set resolved depending on layout direction
11246 switch (getLayoutDirection()) {
11247 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliob93911f2012-06-26 19:43:15 -070011248 // We cannot resolve yet. LTR is by default and let the resolution happen again
11249 // later to get the correct resolved value
11250 if (!canResolveLayoutDirection()) return;
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011251
Fabrice Di Megliob93911f2012-06-26 19:43:15 -070011252 ViewGroup viewGroup = ((ViewGroup) mParent);
11253
11254 // We cannot resolve yet on the parent too. LTR is by default and let the
11255 // resolution happen again later
11256 if (!viewGroup.canResolveLayoutDirection()) return;
11257
11258 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
11259 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011260 }
11261 break;
11262 case LAYOUT_DIRECTION_RTL:
11263 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11264 break;
11265 case LAYOUT_DIRECTION_LOCALE:
11266 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011267 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11268 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011269 break;
11270 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011271 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011272 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011273 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011274
11275 // Set to resolved
11276 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011277 onResolvedLayoutDirectionChanged();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011278 }
11279
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011280 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011281 * Called when layout direction has been resolved.
11282 *
11283 * The default implementation does nothing.
11284 */
11285 public void onResolvedLayoutDirectionChanged() {
11286 }
11287
11288 /**
11289 * Resolve padding depending on layout direction.
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011290 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011291 public void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011292 // If the user specified the absolute padding (either with android:padding or
11293 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
11294 // use the default padding or the padding from the background drawable
11295 // (stored at this point in mPadding*)
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011296 int resolvedLayoutDirection = getResolvedLayoutDirection();
11297 switch (resolvedLayoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011298 case LAYOUT_DIRECTION_RTL:
11299 // Start user padding override Right user padding. Otherwise, if Right user
11300 // padding is not defined, use the default Right padding. If Right user padding
11301 // is defined, just use it.
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070011302 if (mUserPaddingStart != UNDEFINED_PADDING) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011303 mUserPaddingRight = mUserPaddingStart;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070011304 }
11305 if (mUserPaddingRight == UNDEFINED_PADDING) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011306 mUserPaddingRight = mPaddingRight;
11307 }
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070011308 if (mUserPaddingEnd != UNDEFINED_PADDING) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011309 mUserPaddingLeft = mUserPaddingEnd;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070011310 }
11311 if (mUserPaddingLeft == UNDEFINED_PADDING) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011312 mUserPaddingLeft = mPaddingLeft;
11313 }
11314 break;
11315 case LAYOUT_DIRECTION_LTR:
11316 default:
11317 // Start user padding override Left user padding. Otherwise, if Left user
11318 // padding is not defined, use the default left padding. If Left user padding
11319 // is defined, just use it.
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070011320 if (mUserPaddingStart != UNDEFINED_PADDING) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011321 mUserPaddingLeft = mUserPaddingStart;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070011322 }
11323 if (mUserPaddingLeft == UNDEFINED_PADDING) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011324 mUserPaddingLeft = mPaddingLeft;
11325 }
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070011326 if (mUserPaddingEnd != UNDEFINED_PADDING) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011327 mUserPaddingRight = mUserPaddingEnd;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070011328 }
11329 if (mUserPaddingRight == UNDEFINED_PADDING) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011330 mUserPaddingRight = mPaddingRight;
11331 }
11332 }
11333
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011334 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
11335
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070011336 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011337 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011338 }
11339
11340 /**
11341 * Resolve padding depending on the layout direction. Subclasses that care about
11342 * padding resolution should override this method. The default implementation does
11343 * nothing.
11344 *
11345 * @param layoutDirection the direction of the layout
11346 *
Fabrice Di Meglioe8dc07d2012-03-09 17:10:19 -080011347 * @see {@link #LAYOUT_DIRECTION_LTR}
11348 * @see {@link #LAYOUT_DIRECTION_RTL}
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011349 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011350 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011351 }
11352
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011353 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011354 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011355 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011356 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011357 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011358 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011359 switch (getLayoutDirection()) {
11360 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011361 return (mParent != null) && (mParent instanceof ViewGroup);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011362 default:
11363 return true;
11364 }
11365 }
11366
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011367 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011368 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
11369 * when reset is done.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011370 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011371 public void resetResolvedLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070011372 // Reset the current resolved bits
11373 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011374 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080011375 // Reset also the text direction
11376 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011377 }
11378
11379 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011380 * Called during reset of resolved layout direction.
11381 *
11382 * Subclasses need to override this method to clear cached information that depends on the
11383 * resolved layout direction, or to inform child views that inherit their layout direction.
11384 *
11385 * The default implementation does nothing.
11386 */
11387 public void onResolvedLayoutDirectionReset() {
11388 }
11389
11390 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011391 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011392 *
11393 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011394 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011395 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011396 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -080011397 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011398 }
11399
11400 /**
11401 * This is called when the view is detached from a window. At this point it
11402 * no longer has a surface for drawing.
11403 *
11404 * @see #onAttachedToWindow()
11405 */
11406 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -080011407 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080011408
Romain Guya440b002010-02-24 15:57:54 -080011409 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050011410 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080011411 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070011412 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080011413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011414 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011415
Romain Guya998dff2012-03-23 18:58:36 -070011416 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011417
11418 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011419 if (mDisplayList != null) {
Romain Guy2a0f2282012-05-08 14:43:12 -070011420 mAttachInfo.mViewRootImpl.enqueueDisplayList(mDisplayList);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011421 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011422 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011423 } else {
Romain Guy38c2ece2012-05-24 14:20:56 -070011424 // Should never happen
11425 clearDisplayList();
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011426 }
11427
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011428 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011429
11430 resetResolvedLayoutDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011431 resetResolvedTextAlignment();
Svetoslav Ganov42138042012-03-20 11:51:39 -070011432 resetAccessibilityStateChanged();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011433 }
11434
11435 /**
11436 * @return The number of times this view has been attached to a window
11437 */
11438 protected int getWindowAttachCount() {
11439 return mWindowAttachCount;
11440 }
11441
11442 /**
11443 * Retrieve a unique token identifying the window this view is attached to.
11444 * @return Return the window's token for use in
11445 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11446 */
11447 public IBinder getWindowToken() {
11448 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11449 }
11450
11451 /**
11452 * Retrieve a unique token identifying the top-level "real" window of
11453 * the window that this view is attached to. That is, this is like
11454 * {@link #getWindowToken}, except if the window this view in is a panel
11455 * window (attached to another containing window), then the token of
11456 * the containing window is returned instead.
11457 *
11458 * @return Returns the associated window token, either
11459 * {@link #getWindowToken()} or the containing window's token.
11460 */
11461 public IBinder getApplicationWindowToken() {
11462 AttachInfo ai = mAttachInfo;
11463 if (ai != null) {
11464 IBinder appWindowToken = ai.mPanelParentWindowToken;
11465 if (appWindowToken == null) {
11466 appWindowToken = ai.mWindowToken;
11467 }
11468 return appWindowToken;
11469 }
11470 return null;
11471 }
11472
11473 /**
11474 * Retrieve private session object this view hierarchy is using to
11475 * communicate with the window manager.
11476 * @return the session object to communicate with the window manager
11477 */
11478 /*package*/ IWindowSession getWindowSession() {
11479 return mAttachInfo != null ? mAttachInfo.mSession : null;
11480 }
11481
11482 /**
11483 * @param info the {@link android.view.View.AttachInfo} to associated with
11484 * this view
11485 */
11486 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
11487 //System.out.println("Attached! " + this);
11488 mAttachInfo = info;
11489 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011490 // We will need to evaluate the drawable state at least once.
11491 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011492 if (mFloatingTreeObserver != null) {
11493 info.mTreeObserver.merge(mFloatingTreeObserver);
11494 mFloatingTreeObserver = null;
11495 }
11496 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
11497 mAttachInfo.mScrollContainers.add(this);
11498 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
11499 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070011500 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011501 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080011502
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011503 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011504 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011505 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011506 if (listeners != null && listeners.size() > 0) {
11507 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11508 // perform the dispatching. The iterator is a safe guard against listeners that
11509 // could mutate the list by calling the various add/remove methods. This prevents
11510 // the array from being modified while we iterate it.
11511 for (OnAttachStateChangeListener listener : listeners) {
11512 listener.onViewAttachedToWindow(this);
11513 }
11514 }
11515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011516 int vis = info.mWindowVisibility;
11517 if (vis != GONE) {
11518 onWindowVisibilityChanged(vis);
11519 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011520 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
11521 // If nobody has evaluated the drawable state yet, then do it now.
11522 refreshDrawableState();
11523 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011524 }
11525
11526 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011527 AttachInfo info = mAttachInfo;
11528 if (info != null) {
11529 int vis = info.mWindowVisibility;
11530 if (vis != GONE) {
11531 onWindowVisibilityChanged(GONE);
11532 }
11533 }
11534
11535 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080011536
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011537 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011538 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011539 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011540 if (listeners != null && listeners.size() > 0) {
11541 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11542 // perform the dispatching. The iterator is a safe guard against listeners that
11543 // could mutate the list by calling the various add/remove methods. This prevents
11544 // the array from being modified while we iterate it.
11545 for (OnAttachStateChangeListener listener : listeners) {
11546 listener.onViewDetachedFromWindow(this);
11547 }
11548 }
11549
Romain Guy01d5edc2011-01-28 11:28:53 -080011550 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011551 mAttachInfo.mScrollContainers.remove(this);
11552 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
11553 }
Romain Guy01d5edc2011-01-28 11:28:53 -080011554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011555 mAttachInfo = null;
11556 }
11557
11558 /**
11559 * Store this view hierarchy's frozen state into the given container.
11560 *
11561 * @param container The SparseArray in which to save the view's state.
11562 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011563 * @see #restoreHierarchyState(android.util.SparseArray)
11564 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11565 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011566 */
11567 public void saveHierarchyState(SparseArray<Parcelable> container) {
11568 dispatchSaveInstanceState(container);
11569 }
11570
11571 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011572 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
11573 * this view and its children. May be overridden to modify how freezing happens to a
11574 * 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 -080011575 *
11576 * @param container The SparseArray in which to save the view's state.
11577 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011578 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11579 * @see #saveHierarchyState(android.util.SparseArray)
11580 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011581 */
11582 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
11583 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
11584 mPrivateFlags &= ~SAVE_STATE_CALLED;
11585 Parcelable state = onSaveInstanceState();
11586 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11587 throw new IllegalStateException(
11588 "Derived class did not call super.onSaveInstanceState()");
11589 }
11590 if (state != null) {
11591 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
11592 // + ": " + state);
11593 container.put(mID, state);
11594 }
11595 }
11596 }
11597
11598 /**
11599 * Hook allowing a view to generate a representation of its internal state
11600 * that can later be used to create a new instance with that same state.
11601 * This state should only contain information that is not persistent or can
11602 * not be reconstructed later. For example, you will never store your
11603 * current position on screen because that will be computed again when a
11604 * new instance of the view is placed in its view hierarchy.
11605 * <p>
11606 * Some examples of things you may store here: the current cursor position
11607 * in a text view (but usually not the text itself since that is stored in a
11608 * content provider or other persistent storage), the currently selected
11609 * item in a list view.
11610 *
11611 * @return Returns a Parcelable object containing the view's current dynamic
11612 * state, or null if there is nothing interesting to save. The
11613 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070011614 * @see #onRestoreInstanceState(android.os.Parcelable)
11615 * @see #saveHierarchyState(android.util.SparseArray)
11616 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011617 * @see #setSaveEnabled(boolean)
11618 */
11619 protected Parcelable onSaveInstanceState() {
11620 mPrivateFlags |= SAVE_STATE_CALLED;
11621 return BaseSavedState.EMPTY_STATE;
11622 }
11623
11624 /**
11625 * Restore this view hierarchy's frozen state from the given container.
11626 *
11627 * @param container The SparseArray which holds previously frozen states.
11628 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011629 * @see #saveHierarchyState(android.util.SparseArray)
11630 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11631 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011632 */
11633 public void restoreHierarchyState(SparseArray<Parcelable> container) {
11634 dispatchRestoreInstanceState(container);
11635 }
11636
11637 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011638 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
11639 * state for this view and its children. May be overridden to modify how restoring
11640 * happens to a view's children; for example, some views may want to not store state
11641 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011642 *
11643 * @param container The SparseArray which holds previously saved state.
11644 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011645 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11646 * @see #restoreHierarchyState(android.util.SparseArray)
11647 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011648 */
11649 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
11650 if (mID != NO_ID) {
11651 Parcelable state = container.get(mID);
11652 if (state != null) {
11653 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
11654 // + ": " + state);
11655 mPrivateFlags &= ~SAVE_STATE_CALLED;
11656 onRestoreInstanceState(state);
11657 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11658 throw new IllegalStateException(
11659 "Derived class did not call super.onRestoreInstanceState()");
11660 }
11661 }
11662 }
11663 }
11664
11665 /**
11666 * Hook allowing a view to re-apply a representation of its internal state that had previously
11667 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
11668 * null state.
11669 *
11670 * @param state The frozen state that had previously been returned by
11671 * {@link #onSaveInstanceState}.
11672 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011673 * @see #onSaveInstanceState()
11674 * @see #restoreHierarchyState(android.util.SparseArray)
11675 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011676 */
11677 protected void onRestoreInstanceState(Parcelable state) {
11678 mPrivateFlags |= SAVE_STATE_CALLED;
11679 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080011680 throw new IllegalArgumentException("Wrong state class, expecting View State but "
11681 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080011682 + "when two views of different type have the same id in the same hierarchy. "
11683 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080011684 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011685 }
11686 }
11687
11688 /**
11689 * <p>Return the time at which the drawing of the view hierarchy started.</p>
11690 *
11691 * @return the drawing start time in milliseconds
11692 */
11693 public long getDrawingTime() {
11694 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
11695 }
11696
11697 /**
11698 * <p>Enables or disables the duplication of the parent's state into this view. When
11699 * duplication is enabled, this view gets its drawable state from its parent rather
11700 * than from its own internal properties.</p>
11701 *
11702 * <p>Note: in the current implementation, setting this property to true after the
11703 * view was added to a ViewGroup might have no effect at all. This property should
11704 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
11705 *
11706 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
11707 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011708 *
Gilles Debunnefb817032011-01-13 13:52:49 -080011709 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
11710 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011711 *
11712 * @param enabled True to enable duplication of the parent's drawable state, false
11713 * to disable it.
11714 *
11715 * @see #getDrawableState()
11716 * @see #isDuplicateParentStateEnabled()
11717 */
11718 public void setDuplicateParentStateEnabled(boolean enabled) {
11719 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
11720 }
11721
11722 /**
11723 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
11724 *
11725 * @return True if this view's drawable state is duplicated from the parent,
11726 * false otherwise
11727 *
11728 * @see #getDrawableState()
11729 * @see #setDuplicateParentStateEnabled(boolean)
11730 */
11731 public boolean isDuplicateParentStateEnabled() {
11732 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
11733 }
11734
11735 /**
Romain Guy171c5922011-01-06 10:04:23 -080011736 * <p>Specifies the type of layer backing this view. The layer can be
11737 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
11738 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011739 *
Romain Guy171c5922011-01-06 10:04:23 -080011740 * <p>A layer is associated with an optional {@link android.graphics.Paint}
11741 * instance that controls how the layer is composed on screen. The following
11742 * properties of the paint are taken into account when composing the layer:</p>
11743 * <ul>
11744 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
11745 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
11746 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
11747 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080011748 *
Romain Guy171c5922011-01-06 10:04:23 -080011749 * <p>If this view has an alpha value set to < 1.0 by calling
11750 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
11751 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
11752 * equivalent to setting a hardware layer on this view and providing a paint with
11753 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080011754 *
Romain Guy171c5922011-01-06 10:04:23 -080011755 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
11756 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
11757 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011758 *
Romain Guy171c5922011-01-06 10:04:23 -080011759 * @param layerType The ype of layer to use with this view, must be one of
11760 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11761 * {@link #LAYER_TYPE_HARDWARE}
11762 * @param paint The paint used to compose the layer. This argument is optional
11763 * and can be null. It is ignored when the layer type is
11764 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080011765 *
11766 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080011767 * @see #LAYER_TYPE_NONE
11768 * @see #LAYER_TYPE_SOFTWARE
11769 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080011770 * @see #setAlpha(float)
11771 *
Romain Guy171c5922011-01-06 10:04:23 -080011772 * @attr ref android.R.styleable#View_layerType
11773 */
11774 public void setLayerType(int layerType, Paint paint) {
11775 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080011776 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080011777 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
11778 }
Chet Haasedaf98e92011-01-10 14:10:36 -080011779
Romain Guyd6cd5722011-01-17 14:42:41 -080011780 if (layerType == mLayerType) {
11781 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
11782 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011783 invalidateParentCaches();
11784 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080011785 }
11786 return;
11787 }
Romain Guy171c5922011-01-06 10:04:23 -080011788
11789 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080011790 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070011791 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070011792 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011793 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080011794 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070011795 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011796 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080011797 default:
11798 break;
Romain Guy171c5922011-01-06 10:04:23 -080011799 }
11800
11801 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080011802 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
11803 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
11804 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080011805
Romain Guy0fd89bf2011-01-26 15:41:30 -080011806 invalidateParentCaches();
11807 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080011808 }
11809
11810 /**
Romain Guy59c7f802011-09-29 17:21:45 -070011811 * Indicates whether this view has a static layer. A view with layer type
11812 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
11813 * dynamic.
11814 */
11815 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080011816 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070011817 }
11818
11819 /**
Romain Guy171c5922011-01-06 10:04:23 -080011820 * Indicates what type of layer is currently associated with this view. By default
11821 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
11822 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
11823 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080011824 *
Romain Guy171c5922011-01-06 10:04:23 -080011825 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11826 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080011827 *
11828 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070011829 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080011830 * @see #LAYER_TYPE_NONE
11831 * @see #LAYER_TYPE_SOFTWARE
11832 * @see #LAYER_TYPE_HARDWARE
11833 */
11834 public int getLayerType() {
11835 return mLayerType;
11836 }
Joe Malin32736f02011-01-19 16:14:20 -080011837
Romain Guy6c319ca2011-01-11 14:29:25 -080011838 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080011839 * Forces this view's layer to be created and this view to be rendered
11840 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
11841 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070011842 *
Romain Guyf1ae1062011-03-02 18:16:04 -080011843 * This method can for instance be used to render a view into its layer before
11844 * starting an animation. If this view is complex, rendering into the layer
11845 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070011846 *
Romain Guyf1ae1062011-03-02 18:16:04 -080011847 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070011848 *
11849 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080011850 */
11851 public void buildLayer() {
11852 if (mLayerType == LAYER_TYPE_NONE) return;
11853
11854 if (mAttachInfo == null) {
11855 throw new IllegalStateException("This view must be attached to a window first");
11856 }
11857
11858 switch (mLayerType) {
11859 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080011860 if (mAttachInfo.mHardwareRenderer != null &&
11861 mAttachInfo.mHardwareRenderer.isEnabled() &&
11862 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080011863 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080011864 }
Romain Guyf1ae1062011-03-02 18:16:04 -080011865 break;
11866 case LAYER_TYPE_SOFTWARE:
11867 buildDrawingCache(true);
11868 break;
11869 }
11870 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011871
Romain Guyf1ae1062011-03-02 18:16:04 -080011872 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080011873 * <p>Returns a hardware layer that can be used to draw this view again
11874 * without executing its draw method.</p>
11875 *
11876 * @return A HardwareLayer ready to render, or null if an error occurred.
11877 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080011878 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070011879 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
11880 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080011881 return null;
11882 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011883
Romain Guy9c4b79a2011-11-10 19:23:58 -080011884 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080011885
11886 final int width = mRight - mLeft;
11887 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080011888
Romain Guy6c319ca2011-01-11 14:29:25 -080011889 if (width == 0 || height == 0) {
11890 return null;
11891 }
11892
11893 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
11894 if (mHardwareLayer == null) {
11895 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
11896 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070011897 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy846a5332012-07-11 17:44:57 -070011898 } else {
11899 if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
11900 mHardwareLayer.resize(width, height);
11901 mLocalDirtyRect.set(0, 0, width, height);
11902 }
11903
11904 // This should not be necessary but applications that change
11905 // the parameters of their background drawable without calling
11906 // this.setBackground(Drawable) can leave the view in a bad state
11907 // (for instance isOpaque() returns true, but the background is
11908 // not opaque.)
11909 computeOpaqueFlags();
11910
11911 final boolean opaque = isOpaque();
11912 if (mHardwareLayer.isOpaque() != opaque) {
11913 mHardwareLayer.setOpaque(opaque);
11914 mLocalDirtyRect.set(0, 0, width, height);
11915 }
Romain Guy6c319ca2011-01-11 14:29:25 -080011916 }
11917
Romain Guy5cd5c3f2011-10-17 17:10:02 -070011918 // The layer is not valid if the underlying GPU resources cannot be allocated
11919 if (!mHardwareLayer.isValid()) {
11920 return null;
11921 }
11922
Chet Haasea1cff502012-02-21 13:43:44 -080011923 mHardwareLayer.redraw(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
Michael Jurka7e52caf2012-03-06 15:57:06 -080011924 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080011925 }
11926
11927 return mHardwareLayer;
11928 }
Romain Guy171c5922011-01-06 10:04:23 -080011929
Romain Guy589b0bb2011-10-10 13:57:47 -070011930 /**
11931 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070011932 *
Romain Guy589b0bb2011-10-10 13:57:47 -070011933 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070011934 *
11935 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070011936 * @see #LAYER_TYPE_HARDWARE
11937 */
Romain Guya998dff2012-03-23 18:58:36 -070011938 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070011939 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080011940 AttachInfo info = mAttachInfo;
11941 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070011942 info.mHardwareRenderer.isEnabled() &&
11943 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080011944 mHardwareLayer.destroy();
11945 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080011946
Romain Guy9c4b79a2011-11-10 19:23:58 -080011947 invalidate(true);
11948 invalidateParentCaches();
11949 }
Romain Guy65b345f2011-07-27 18:51:50 -070011950 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070011951 }
Romain Guy65b345f2011-07-27 18:51:50 -070011952 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070011953 }
11954
Romain Guy171c5922011-01-06 10:04:23 -080011955 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080011956 * Destroys all hardware rendering resources. This method is invoked
11957 * when the system needs to reclaim resources. Upon execution of this
11958 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070011959 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080011960 * Note: you <strong>must</strong> call
11961 * <code>super.destroyHardwareResources()</code> when overriding
11962 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070011963 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080011964 * @hide
11965 */
11966 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070011967 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011968 }
11969
11970 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011971 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
11972 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
11973 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
11974 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
11975 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
11976 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011977 *
Romain Guy171c5922011-01-06 10:04:23 -080011978 * <p>Enabling the drawing cache is similar to
11979 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080011980 * acceleration is turned off. When hardware acceleration is turned on, enabling the
11981 * drawing cache has no effect on rendering because the system uses a different mechanism
11982 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
11983 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
11984 * for information on how to enable software and hardware layers.</p>
11985 *
11986 * <p>This API can be used to manually generate
11987 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
11988 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011989 *
11990 * @param enabled true to enable the drawing cache, false otherwise
11991 *
11992 * @see #isDrawingCacheEnabled()
11993 * @see #getDrawingCache()
11994 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080011995 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011996 */
11997 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080011998 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011999 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
12000 }
12001
12002 /**
12003 * <p>Indicates whether the drawing cache is enabled for this view.</p>
12004 *
12005 * @return true if the drawing cache is enabled
12006 *
12007 * @see #setDrawingCacheEnabled(boolean)
12008 * @see #getDrawingCache()
12009 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012010 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012011 public boolean isDrawingCacheEnabled() {
12012 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
12013 }
12014
12015 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080012016 * Debugging utility which recursively outputs the dirty state of a view and its
12017 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080012018 *
Chet Haasedaf98e92011-01-10 14:10:36 -080012019 * @hide
12020 */
Romain Guy676b1732011-02-14 14:45:33 -080012021 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080012022 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
12023 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
12024 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
12025 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
12026 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
12027 if (clear) {
12028 mPrivateFlags &= clearMask;
12029 }
12030 if (this instanceof ViewGroup) {
12031 ViewGroup parent = (ViewGroup) this;
12032 final int count = parent.getChildCount();
12033 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080012034 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080012035 child.outputDirtyFlags(indent + " ", clear, clearMask);
12036 }
12037 }
12038 }
12039
12040 /**
12041 * This method is used by ViewGroup to cause its children to restore or recreate their
12042 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
12043 * to recreate its own display list, which would happen if it went through the normal
12044 * draw/dispatchDraw mechanisms.
12045 *
12046 * @hide
12047 */
12048 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080012049
12050 /**
12051 * A view that is not attached or hardware accelerated cannot create a display list.
12052 * This method checks these conditions and returns the appropriate result.
12053 *
12054 * @return true if view has the ability to create a display list, false otherwise.
12055 *
12056 * @hide
12057 */
12058 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080012059 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080012060 }
Joe Malin32736f02011-01-19 16:14:20 -080012061
Chet Haasedaf98e92011-01-10 14:10:36 -080012062 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080012063 * @return The HardwareRenderer associated with that view or null if hardware rendering
12064 * is not supported or this this has not been attached to a window.
12065 *
12066 * @hide
12067 */
12068 public HardwareRenderer getHardwareRenderer() {
12069 if (mAttachInfo != null) {
12070 return mAttachInfo.mHardwareRenderer;
12071 }
12072 return null;
12073 }
12074
12075 /**
Chet Haasea1cff502012-02-21 13:43:44 -080012076 * Returns a DisplayList. If the incoming displayList is null, one will be created.
12077 * Otherwise, the same display list will be returned (after having been rendered into
12078 * along the way, depending on the invalidation state of the view).
12079 *
12080 * @param displayList The previous version of this displayList, could be null.
12081 * @param isLayer Whether the requester of the display list is a layer. If so,
12082 * the view will avoid creating a layer inside the resulting display list.
12083 * @return A new or reused DisplayList object.
12084 */
12085 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
12086 if (!canHaveDisplayList()) {
12087 return null;
12088 }
12089
12090 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
12091 displayList == null || !displayList.isValid() ||
12092 (!isLayer && mRecreateDisplayList))) {
12093 // Don't need to recreate the display list, just need to tell our
12094 // children to restore/recreate theirs
12095 if (displayList != null && displayList.isValid() &&
12096 !isLayer && !mRecreateDisplayList) {
12097 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12098 mPrivateFlags &= ~DIRTY_MASK;
12099 dispatchGetDisplayList();
12100
12101 return displayList;
12102 }
12103
12104 if (!isLayer) {
12105 // If we got here, we're recreating it. Mark it as such to ensure that
12106 // we copy in child display lists into ours in drawChild()
12107 mRecreateDisplayList = true;
12108 }
12109 if (displayList == null) {
12110 final String name = getClass().getSimpleName();
12111 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
12112 // If we're creating a new display list, make sure our parent gets invalidated
12113 // since they will need to recreate their display list to account for this
12114 // new child display list.
12115 invalidateParentCaches();
12116 }
12117
12118 boolean caching = false;
12119 final HardwareCanvas canvas = displayList.start();
Chet Haasea1cff502012-02-21 13:43:44 -080012120 int width = mRight - mLeft;
12121 int height = mBottom - mTop;
12122
12123 try {
12124 canvas.setViewport(width, height);
12125 // The dirty rect should always be null for a display list
12126 canvas.onPreDraw(null);
Michael Jurkaba649742012-06-28 19:12:58 -070012127 int layerType = getLayerType();
Chet Haase1271e2c2012-04-20 09:54:27 -070012128 if (!isLayer && layerType != LAYER_TYPE_NONE) {
Chet Haaseb85967b2012-03-26 14:37:51 -070012129 if (layerType == LAYER_TYPE_HARDWARE) {
12130 final HardwareLayer layer = getHardwareLayer();
12131 if (layer != null && layer.isValid()) {
12132 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
12133 } else {
12134 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
12135 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
12136 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12137 }
12138 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080012139 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070012140 buildDrawingCache(true);
12141 Bitmap cache = getDrawingCache(true);
12142 if (cache != null) {
12143 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
12144 caching = true;
12145 }
Chet Haasea1cff502012-02-21 13:43:44 -080012146 }
Chet Haasea1cff502012-02-21 13:43:44 -080012147 } else {
12148
12149 computeScroll();
12150
Chet Haasea1cff502012-02-21 13:43:44 -080012151 canvas.translate(-mScrollX, -mScrollY);
12152 if (!isLayer) {
12153 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12154 mPrivateFlags &= ~DIRTY_MASK;
12155 }
12156
12157 // Fast path for layouts with no backgrounds
12158 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12159 dispatchDraw(canvas);
12160 } else {
12161 draw(canvas);
12162 }
12163 }
12164 } finally {
Chet Haasea1cff502012-02-21 13:43:44 -080012165 canvas.onPostDraw();
12166
12167 displayList.end();
Chet Haase1271e2c2012-04-20 09:54:27 -070012168 displayList.setCaching(caching);
12169 if (isLayer) {
Chet Haasea1cff502012-02-21 13:43:44 -080012170 displayList.setLeftTopRightBottom(0, 0, width, height);
12171 } else {
12172 setDisplayListProperties(displayList);
12173 }
12174 }
12175 } else if (!isLayer) {
12176 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12177 mPrivateFlags &= ~DIRTY_MASK;
12178 }
12179
12180 return displayList;
12181 }
12182
12183 /**
12184 * Get the DisplayList for the HardwareLayer
12185 *
12186 * @param layer The HardwareLayer whose DisplayList we want
12187 * @return A DisplayList fopr the specified HardwareLayer
12188 */
12189 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
12190 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
12191 layer.setDisplayList(displayList);
12192 return displayList;
12193 }
12194
12195
12196 /**
Romain Guyb051e892010-09-28 19:09:36 -070012197 * <p>Returns a display list that can be used to draw this view again
12198 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012199 *
Romain Guyb051e892010-09-28 19:09:36 -070012200 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080012201 *
12202 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070012203 */
Chet Haasedaf98e92011-01-10 14:10:36 -080012204 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080012205 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070012206 return mDisplayList;
12207 }
12208
Romain Guy38c2ece2012-05-24 14:20:56 -070012209 private void clearDisplayList() {
12210 if (mDisplayList != null) {
12211 mDisplayList.invalidate();
12212 mDisplayList.clear();
12213 }
12214 }
12215
Romain Guyb051e892010-09-28 19:09:36 -070012216 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012217 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012218 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012219 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012220 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012221 * @see #getDrawingCache(boolean)
12222 */
12223 public Bitmap getDrawingCache() {
12224 return getDrawingCache(false);
12225 }
12226
12227 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012228 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
12229 * is null when caching is disabled. If caching is enabled and the cache is not ready,
12230 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
12231 * draw from the cache when the cache is enabled. To benefit from the cache, you must
12232 * request the drawing cache by calling this method and draw it on screen if the
12233 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012234 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012235 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12236 * this method will create a bitmap of the same size as this view. Because this bitmap
12237 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12238 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12239 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12240 * size than the view. This implies that your application must be able to handle this
12241 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012242 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012243 * @param autoScale Indicates whether the generated bitmap should be scaled based on
12244 * the current density of the screen when the application is in compatibility
12245 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012246 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012247 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012248 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012249 * @see #setDrawingCacheEnabled(boolean)
12250 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070012251 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012252 * @see #destroyDrawingCache()
12253 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012254 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012255 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
12256 return null;
12257 }
12258 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012259 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012260 }
Romain Guy02890fd2010-08-06 17:58:44 -070012261 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012262 }
12263
12264 /**
12265 * <p>Frees the resources used by the drawing cache. If you call
12266 * {@link #buildDrawingCache()} manually without calling
12267 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12268 * should cleanup the cache with this method afterwards.</p>
12269 *
12270 * @see #setDrawingCacheEnabled(boolean)
12271 * @see #buildDrawingCache()
12272 * @see #getDrawingCache()
12273 */
12274 public void destroyDrawingCache() {
12275 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012276 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012277 mDrawingCache = null;
12278 }
Romain Guyfbd8f692009-06-26 14:51:58 -070012279 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012280 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070012281 mUnscaledDrawingCache = null;
12282 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012283 }
12284
12285 /**
12286 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070012287 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012288 * view will always be drawn on top of a solid color.
12289 *
12290 * @param color The background color to use for the drawing cache's bitmap
12291 *
12292 * @see #setDrawingCacheEnabled(boolean)
12293 * @see #buildDrawingCache()
12294 * @see #getDrawingCache()
12295 */
12296 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080012297 if (color != mDrawingCacheBackgroundColor) {
12298 mDrawingCacheBackgroundColor = color;
12299 mPrivateFlags &= ~DRAWING_CACHE_VALID;
12300 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012301 }
12302
12303 /**
12304 * @see #setDrawingCacheBackgroundColor(int)
12305 *
12306 * @return The background color to used for the drawing cache's bitmap
12307 */
12308 public int getDrawingCacheBackgroundColor() {
12309 return mDrawingCacheBackgroundColor;
12310 }
12311
12312 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012313 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012314 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012315 * @see #buildDrawingCache(boolean)
12316 */
12317 public void buildDrawingCache() {
12318 buildDrawingCache(false);
12319 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080012320
Romain Guyfbd8f692009-06-26 14:51:58 -070012321 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012322 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
12323 *
12324 * <p>If you call {@link #buildDrawingCache()} manually without calling
12325 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12326 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012327 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012328 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12329 * this method will create a bitmap of the same size as this view. Because this bitmap
12330 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12331 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12332 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12333 * size than the view. This implies that your application must be able to handle this
12334 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012335 *
Romain Guy0d9275e2010-10-26 14:22:30 -070012336 * <p>You should avoid calling this method when hardware acceleration is enabled. If
12337 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080012338 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070012339 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012340 *
12341 * @see #getDrawingCache()
12342 * @see #destroyDrawingCache()
12343 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012344 public void buildDrawingCache(boolean autoScale) {
12345 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070012346 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012347 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012348
Romain Guy8506ab42009-06-11 17:35:47 -070012349 int width = mRight - mLeft;
12350 int height = mBottom - mTop;
12351
12352 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070012353 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070012354
Romain Guye1123222009-06-29 14:24:56 -070012355 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012356 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
12357 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070012358 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012359
12360 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070012361 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080012362 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012363
12364 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070012365 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080012366 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012367 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
12368 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080012369 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012370 return;
12371 }
12372
12373 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070012374 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012375
12376 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012377 Bitmap.Config quality;
12378 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080012379 // Never pick ARGB_4444 because it looks awful
12380 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012381 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
12382 case DRAWING_CACHE_QUALITY_AUTO:
12383 quality = Bitmap.Config.ARGB_8888;
12384 break;
12385 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080012386 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012387 break;
12388 case DRAWING_CACHE_QUALITY_HIGH:
12389 quality = Bitmap.Config.ARGB_8888;
12390 break;
12391 default:
12392 quality = Bitmap.Config.ARGB_8888;
12393 break;
12394 }
12395 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070012396 // Optimization for translucent windows
12397 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080012398 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012399 }
12400
12401 // Try to cleanup memory
12402 if (bitmap != null) bitmap.recycle();
12403
12404 try {
12405 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070012406 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070012407 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070012408 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012409 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070012410 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012411 }
Adam Powell26153a32010-11-08 15:22:27 -080012412 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012413 } catch (OutOfMemoryError e) {
12414 // If there is not enough memory to create the bitmap cache, just
12415 // ignore the issue as bitmap caches are not required to draw the
12416 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012417 if (autoScale) {
12418 mDrawingCache = null;
12419 } else {
12420 mUnscaledDrawingCache = null;
12421 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012422 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012423 return;
12424 }
12425
12426 clear = drawingCacheBackgroundColor != 0;
12427 }
12428
12429 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012430 if (attachInfo != null) {
12431 canvas = attachInfo.mCanvas;
12432 if (canvas == null) {
12433 canvas = new Canvas();
12434 }
12435 canvas.setBitmap(bitmap);
12436 // Temporarily clobber the cached Canvas in case one of our children
12437 // is also using a drawing cache. Without this, the children would
12438 // steal the canvas by attaching their own bitmap to it and bad, bad
12439 // thing would happen (invisible views, corrupted drawings, etc.)
12440 attachInfo.mCanvas = null;
12441 } else {
12442 // This case should hopefully never or seldom happen
12443 canvas = new Canvas(bitmap);
12444 }
12445
12446 if (clear) {
12447 bitmap.eraseColor(drawingCacheBackgroundColor);
12448 }
12449
12450 computeScroll();
12451 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080012452
Romain Guye1123222009-06-29 14:24:56 -070012453 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012454 final float scale = attachInfo.mApplicationScale;
12455 canvas.scale(scale, scale);
12456 }
Joe Malin32736f02011-01-19 16:14:20 -080012457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012458 canvas.translate(-mScrollX, -mScrollY);
12459
Romain Guy5bcdff42009-05-14 21:27:18 -070012460 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080012461 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
12462 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070012463 mPrivateFlags |= DRAWING_CACHE_VALID;
12464 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012465
12466 // Fast path for layouts with no backgrounds
12467 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Romain Guy5bcdff42009-05-14 21:27:18 -070012468 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012469 dispatchDraw(canvas);
12470 } else {
12471 draw(canvas);
12472 }
12473
12474 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012475 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012476
12477 if (attachInfo != null) {
12478 // Restore the cached Canvas for our siblings
12479 attachInfo.mCanvas = canvas;
12480 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012481 }
12482 }
12483
12484 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012485 * Create a snapshot of the view into a bitmap. We should probably make
12486 * some form of this public, but should think about the API.
12487 */
Romain Guy223ff5c2010-03-02 17:07:47 -080012488 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012489 int width = mRight - mLeft;
12490 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012491
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012492 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070012493 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012494 width = (int) ((width * scale) + 0.5f);
12495 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080012496
Romain Guy8c11e312009-09-14 15:15:30 -070012497 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012498 if (bitmap == null) {
12499 throw new OutOfMemoryError();
12500 }
12501
Romain Guyc529d8d2011-09-06 15:01:39 -070012502 Resources resources = getResources();
12503 if (resources != null) {
12504 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
12505 }
Joe Malin32736f02011-01-19 16:14:20 -080012506
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012507 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012508 if (attachInfo != null) {
12509 canvas = attachInfo.mCanvas;
12510 if (canvas == null) {
12511 canvas = new Canvas();
12512 }
12513 canvas.setBitmap(bitmap);
12514 // Temporarily clobber the cached Canvas in case one of our children
12515 // is also using a drawing cache. Without this, the children would
12516 // steal the canvas by attaching their own bitmap to it and bad, bad
12517 // things would happen (invisible views, corrupted drawings, etc.)
12518 attachInfo.mCanvas = null;
12519 } else {
12520 // This case should hopefully never or seldom happen
12521 canvas = new Canvas(bitmap);
12522 }
12523
Romain Guy5bcdff42009-05-14 21:27:18 -070012524 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012525 bitmap.eraseColor(backgroundColor);
12526 }
12527
12528 computeScroll();
12529 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012530 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012531 canvas.translate(-mScrollX, -mScrollY);
12532
Romain Guy5bcdff42009-05-14 21:27:18 -070012533 // Temporarily remove the dirty mask
12534 int flags = mPrivateFlags;
12535 mPrivateFlags &= ~DIRTY_MASK;
12536
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012537 // Fast path for layouts with no backgrounds
12538 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12539 dispatchDraw(canvas);
12540 } else {
12541 draw(canvas);
12542 }
12543
Romain Guy5bcdff42009-05-14 21:27:18 -070012544 mPrivateFlags = flags;
12545
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012546 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012547 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012548
12549 if (attachInfo != null) {
12550 // Restore the cached Canvas for our siblings
12551 attachInfo.mCanvas = canvas;
12552 }
Romain Guy8506ab42009-06-11 17:35:47 -070012553
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012554 return bitmap;
12555 }
12556
12557 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012558 * Indicates whether this View is currently in edit mode. A View is usually
12559 * in edit mode when displayed within a developer tool. For instance, if
12560 * this View is being drawn by a visual user interface builder, this method
12561 * should return true.
12562 *
12563 * Subclasses should check the return value of this method to provide
12564 * different behaviors if their normal behavior might interfere with the
12565 * host environment. For instance: the class spawns a thread in its
12566 * constructor, the drawing code relies on device-specific features, etc.
12567 *
12568 * This method is usually checked in the drawing code of custom widgets.
12569 *
12570 * @return True if this View is in edit mode, false otherwise.
12571 */
12572 public boolean isInEditMode() {
12573 return false;
12574 }
12575
12576 /**
12577 * If the View draws content inside its padding and enables fading edges,
12578 * it needs to support padding offsets. Padding offsets are added to the
12579 * fading edges to extend the length of the fade so that it covers pixels
12580 * drawn inside the padding.
12581 *
12582 * Subclasses of this class should override this method if they need
12583 * to draw content inside the padding.
12584 *
12585 * @return True if padding offset must be applied, false otherwise.
12586 *
12587 * @see #getLeftPaddingOffset()
12588 * @see #getRightPaddingOffset()
12589 * @see #getTopPaddingOffset()
12590 * @see #getBottomPaddingOffset()
12591 *
12592 * @since CURRENT
12593 */
12594 protected boolean isPaddingOffsetRequired() {
12595 return false;
12596 }
12597
12598 /**
12599 * Amount by which to extend the left fading region. Called only when
12600 * {@link #isPaddingOffsetRequired()} returns true.
12601 *
12602 * @return The left padding offset in pixels.
12603 *
12604 * @see #isPaddingOffsetRequired()
12605 *
12606 * @since CURRENT
12607 */
12608 protected int getLeftPaddingOffset() {
12609 return 0;
12610 }
12611
12612 /**
12613 * Amount by which to extend the right fading region. Called only when
12614 * {@link #isPaddingOffsetRequired()} returns true.
12615 *
12616 * @return The right padding offset in pixels.
12617 *
12618 * @see #isPaddingOffsetRequired()
12619 *
12620 * @since CURRENT
12621 */
12622 protected int getRightPaddingOffset() {
12623 return 0;
12624 }
12625
12626 /**
12627 * Amount by which to extend the top fading region. Called only when
12628 * {@link #isPaddingOffsetRequired()} returns true.
12629 *
12630 * @return The top padding offset in pixels.
12631 *
12632 * @see #isPaddingOffsetRequired()
12633 *
12634 * @since CURRENT
12635 */
12636 protected int getTopPaddingOffset() {
12637 return 0;
12638 }
12639
12640 /**
12641 * Amount by which to extend the bottom fading region. Called only when
12642 * {@link #isPaddingOffsetRequired()} returns true.
12643 *
12644 * @return The bottom padding offset in pixels.
12645 *
12646 * @see #isPaddingOffsetRequired()
12647 *
12648 * @since CURRENT
12649 */
12650 protected int getBottomPaddingOffset() {
12651 return 0;
12652 }
12653
12654 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070012655 * @hide
12656 * @param offsetRequired
12657 */
12658 protected int getFadeTop(boolean offsetRequired) {
12659 int top = mPaddingTop;
12660 if (offsetRequired) top += getTopPaddingOffset();
12661 return top;
12662 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012663
Romain Guyf2fc4602011-07-19 15:20:03 -070012664 /**
12665 * @hide
12666 * @param offsetRequired
12667 */
12668 protected int getFadeHeight(boolean offsetRequired) {
12669 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070012670 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070012671 return mBottom - mTop - mPaddingBottom - padding;
12672 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012673
Romain Guyf2fc4602011-07-19 15:20:03 -070012674 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012675 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070012676 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012677 *
Romain Guy2bffd262010-09-12 17:40:02 -070012678 * <p>Even if this method returns true, it does not mean that every call
12679 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
12680 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012681 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070012682 * window is hardware accelerated,
12683 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
12684 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012685 *
Romain Guy2bffd262010-09-12 17:40:02 -070012686 * @return True if the view is attached to a window and the window is
12687 * hardware accelerated; false in any other case.
12688 */
12689 public boolean isHardwareAccelerated() {
12690 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
12691 }
Joe Malin32736f02011-01-19 16:14:20 -080012692
Romain Guy2bffd262010-09-12 17:40:02 -070012693 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080012694 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
12695 * case of an active Animation being run on the view.
12696 */
12697 private boolean drawAnimation(ViewGroup parent, long drawingTime,
12698 Animation a, boolean scalingRequired) {
12699 Transformation invalidationTransform;
12700 final int flags = parent.mGroupFlags;
12701 final boolean initialized = a.isInitialized();
12702 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070012703 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080012704 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
Romain Guy393a52c2012-05-22 20:21:08 -070012705 if (mAttachInfo != null) a.setListenerHandler(mAttachInfo.mHandler);
Chet Haasebcca79a2012-02-14 08:45:14 -080012706 onAnimationStart();
12707 }
12708
12709 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
12710 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
12711 if (parent.mInvalidationTransformation == null) {
12712 parent.mInvalidationTransformation = new Transformation();
12713 }
12714 invalidationTransform = parent.mInvalidationTransformation;
12715 a.getTransformation(drawingTime, invalidationTransform, 1f);
12716 } else {
12717 invalidationTransform = parent.mChildTransformation;
12718 }
Romain Guy393a52c2012-05-22 20:21:08 -070012719
Chet Haasebcca79a2012-02-14 08:45:14 -080012720 if (more) {
12721 if (!a.willChangeBounds()) {
Romain Guyf0af1d52012-07-11 18:31:21 -070012722 if ((flags & (ViewGroup.FLAG_OPTIMIZE_INVALIDATE | ViewGroup.FLAG_ANIMATION_DONE)) ==
12723 ViewGroup.FLAG_OPTIMIZE_INVALIDATE) {
12724 parent.mGroupFlags |= ViewGroup.FLAG_INVALIDATE_REQUIRED;
12725 } else if ((flags & ViewGroup.FLAG_INVALIDATE_REQUIRED) == 0) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012726 // The child need to draw an animation, potentially offscreen, so
12727 // make sure we do not cancel invalidate requests
12728 parent.mPrivateFlags |= DRAW_ANIMATION;
12729 parent.invalidate(mLeft, mTop, mRight, mBottom);
12730 }
12731 } else {
12732 if (parent.mInvalidateRegion == null) {
12733 parent.mInvalidateRegion = new RectF();
12734 }
12735 final RectF region = parent.mInvalidateRegion;
12736 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
12737 invalidationTransform);
12738
12739 // The child need to draw an animation, potentially offscreen, so
12740 // make sure we do not cancel invalidate requests
12741 parent.mPrivateFlags |= DRAW_ANIMATION;
12742
12743 final int left = mLeft + (int) region.left;
12744 final int top = mTop + (int) region.top;
12745 parent.invalidate(left, top, left + (int) (region.width() + .5f),
12746 top + (int) (region.height() + .5f));
12747 }
12748 }
12749 return more;
12750 }
12751
Chet Haasea1cff502012-02-21 13:43:44 -080012752 /**
12753 * This method is called by getDisplayList() when a display list is created or re-rendered.
12754 * It sets or resets the current value of all properties on that display list (resetting is
12755 * necessary when a display list is being re-created, because we need to make sure that
12756 * previously-set transform values
12757 */
12758 void setDisplayListProperties(DisplayList displayList) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012759 if (displayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012760 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070012761 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080012762 if (mParent instanceof ViewGroup) {
12763 displayList.setClipChildren(
12764 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
12765 }
Chet Haase9420abd2012-03-29 16:28:32 -070012766 float alpha = 1;
12767 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
12768 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
12769 ViewGroup parentVG = (ViewGroup) mParent;
12770 final boolean hasTransform =
12771 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
12772 if (hasTransform) {
12773 Transformation transform = parentVG.mChildTransformation;
12774 final int transformType = parentVG.mChildTransformation.getTransformationType();
12775 if (transformType != Transformation.TYPE_IDENTITY) {
12776 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
12777 alpha = transform.getAlpha();
12778 }
12779 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
12780 displayList.setStaticMatrix(transform.getMatrix());
12781 }
12782 }
12783 }
Chet Haasea1cff502012-02-21 13:43:44 -080012784 }
12785 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070012786 alpha *= mTransformationInfo.mAlpha;
12787 if (alpha < 1) {
12788 final int multipliedAlpha = (int) (255 * alpha);
12789 if (onSetAlpha(multipliedAlpha)) {
12790 alpha = 1;
12791 }
12792 }
12793 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080012794 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
12795 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
12796 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
12797 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070012798 if (mTransformationInfo.mCamera == null) {
12799 mTransformationInfo.mCamera = new Camera();
12800 mTransformationInfo.matrix3D = new Matrix();
12801 }
12802 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Chet Haasea1cff502012-02-21 13:43:44 -080012803 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == PIVOT_EXPLICITLY_SET) {
12804 displayList.setPivotX(getPivotX());
12805 displayList.setPivotY(getPivotY());
12806 }
Chet Haase9420abd2012-03-29 16:28:32 -070012807 } else if (alpha < 1) {
12808 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080012809 }
12810 }
12811 }
12812
Chet Haasebcca79a2012-02-14 08:45:14 -080012813 /**
Chet Haase64a48c12012-02-13 16:33:29 -080012814 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
12815 * This draw() method is an implementation detail and is not intended to be overridden or
12816 * to be called from anywhere else other than ViewGroup.drawChild().
12817 */
12818 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012819 boolean useDisplayListProperties = mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080012820 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080012821 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080012822 final int flags = parent.mGroupFlags;
12823
Chet Haasea1cff502012-02-21 13:43:44 -080012824 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080012825 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080012826 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012827 }
12828
12829 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080012830 boolean concatMatrix = false;
12831
12832 boolean scalingRequired = false;
12833 boolean caching;
Michael Jurkaba649742012-06-28 19:12:58 -070012834 int layerType = getLayerType();
Chet Haase64a48c12012-02-13 16:33:29 -080012835
12836 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080012837 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
12838 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080012839 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070012840 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080012841 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
12842 } else {
12843 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
12844 }
12845
Chet Haasebcca79a2012-02-14 08:45:14 -080012846 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080012847 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012848 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080012849 concatMatrix = a.willChangeTransformationMatrix();
Chet Haaseafd5c3e2012-05-10 13:21:10 -070012850 if (concatMatrix) {
Chet Haase21433372012-06-05 07:54:09 -070012851 mPrivateFlags3 |= VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070012852 }
Chet Haasebcca79a2012-02-14 08:45:14 -080012853 transformToApply = parent.mChildTransformation;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070012854 } else {
Chet Haase21433372012-06-05 07:54:09 -070012855 if ((mPrivateFlags3 & VIEW_IS_ANIMATING_TRANSFORM) == VIEW_IS_ANIMATING_TRANSFORM &&
Chet Haaseafd5c3e2012-05-10 13:21:10 -070012856 mDisplayList != null) {
12857 // No longer animating: clear out old animation matrix
12858 mDisplayList.setAnimationMatrix(null);
Chet Haase21433372012-06-05 07:54:09 -070012859 mPrivateFlags3 &= ~VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070012860 }
12861 if (!useDisplayListProperties &&
12862 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
12863 final boolean hasTransform =
12864 parent.getChildStaticTransformation(this, parent.mChildTransformation);
12865 if (hasTransform) {
12866 final int transformType = parent.mChildTransformation.getTransformationType();
12867 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
12868 parent.mChildTransformation : null;
12869 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
12870 }
Chet Haase64a48c12012-02-13 16:33:29 -080012871 }
12872 }
12873
12874 concatMatrix |= !childHasIdentityMatrix;
12875
12876 // Sets the flag as early as possible to allow draw() implementations
12877 // to call invalidate() successfully when doing animations
12878 mPrivateFlags |= DRAWN;
12879
Chet Haase599913d2012-07-23 16:22:05 -070012880 if (!concatMatrix && (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) == 0 &&
12881 canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Chet Haase64a48c12012-02-13 16:33:29 -080012882 (mPrivateFlags & DRAW_ANIMATION) == 0) {
Chet Haase1a3ab172012-05-11 08:41:20 -070012883 mPrivateFlags2 |= VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080012884 return more;
12885 }
Chet Haase1a3ab172012-05-11 08:41:20 -070012886 mPrivateFlags2 &= ~VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080012887
12888 if (hardwareAccelerated) {
12889 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
12890 // retain the flag's value temporarily in the mRecreateDisplayList flag
12891 mRecreateDisplayList = (mPrivateFlags & INVALIDATED) == INVALIDATED;
12892 mPrivateFlags &= ~INVALIDATED;
12893 }
12894
Chet Haase64a48c12012-02-13 16:33:29 -080012895 DisplayList displayList = null;
12896 Bitmap cache = null;
12897 boolean hasDisplayList = false;
12898 if (caching) {
12899 if (!hardwareAccelerated) {
12900 if (layerType != LAYER_TYPE_NONE) {
12901 layerType = LAYER_TYPE_SOFTWARE;
12902 buildDrawingCache(true);
12903 }
12904 cache = getDrawingCache(true);
12905 } else {
12906 switch (layerType) {
12907 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070012908 if (useDisplayListProperties) {
12909 hasDisplayList = canHaveDisplayList();
12910 } else {
12911 buildDrawingCache(true);
12912 cache = getDrawingCache(true);
12913 }
Chet Haase64a48c12012-02-13 16:33:29 -080012914 break;
Chet Haasea1cff502012-02-21 13:43:44 -080012915 case LAYER_TYPE_HARDWARE:
12916 if (useDisplayListProperties) {
12917 hasDisplayList = canHaveDisplayList();
12918 }
12919 break;
Chet Haase64a48c12012-02-13 16:33:29 -080012920 case LAYER_TYPE_NONE:
12921 // Delay getting the display list until animation-driven alpha values are
12922 // set up and possibly passed on to the view
12923 hasDisplayList = canHaveDisplayList();
12924 break;
12925 }
12926 }
12927 }
Chet Haasea1cff502012-02-21 13:43:44 -080012928 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070012929 if (useDisplayListProperties) {
12930 displayList = getDisplayList();
12931 if (!displayList.isValid()) {
12932 // Uncommon, but possible. If a view is removed from the hierarchy during the call
12933 // to getDisplayList(), the display list will be marked invalid and we should not
12934 // try to use it again.
12935 displayList = null;
12936 hasDisplayList = false;
12937 useDisplayListProperties = false;
12938 }
12939 }
Chet Haase64a48c12012-02-13 16:33:29 -080012940
Chet Haase526057b2012-07-12 17:50:41 -070012941 int sx = 0;
12942 int sy = 0;
12943 if (!hasDisplayList) {
12944 computeScroll();
12945 sx = mScrollX;
12946 sy = mScrollY;
12947 }
12948
Chet Haase64a48c12012-02-13 16:33:29 -080012949 final boolean hasNoCache = cache == null || hasDisplayList;
12950 final boolean offsetForScroll = cache == null && !hasDisplayList &&
12951 layerType != LAYER_TYPE_HARDWARE;
12952
Chet Haasea1cff502012-02-21 13:43:44 -080012953 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070012954 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012955 restoreTo = canvas.save();
12956 }
Chet Haase64a48c12012-02-13 16:33:29 -080012957 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012958 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080012959 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080012960 if (!useDisplayListProperties) {
12961 canvas.translate(mLeft, mTop);
12962 }
Chet Haase64a48c12012-02-13 16:33:29 -080012963 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080012964 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070012965 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080012966 restoreTo = canvas.save();
12967 }
Chet Haase64a48c12012-02-13 16:33:29 -080012968 // mAttachInfo cannot be null, otherwise scalingRequired == false
12969 final float scale = 1.0f / mAttachInfo.mApplicationScale;
12970 canvas.scale(scale, scale);
12971 }
12972 }
12973
Chet Haasea1cff502012-02-21 13:43:44 -080012974 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase21433372012-06-05 07:54:09 -070012975 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix() ||
12976 (mPrivateFlags3 & VIEW_IS_ANIMATING_ALPHA) == VIEW_IS_ANIMATING_ALPHA) {
Chet Haase64a48c12012-02-13 16:33:29 -080012977 if (transformToApply != null || !childHasIdentityMatrix) {
12978 int transX = 0;
12979 int transY = 0;
12980
12981 if (offsetForScroll) {
12982 transX = -sx;
12983 transY = -sy;
12984 }
12985
12986 if (transformToApply != null) {
12987 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070012988 if (useDisplayListProperties) {
12989 displayList.setAnimationMatrix(transformToApply.getMatrix());
12990 } else {
12991 // Undo the scroll translation, apply the transformation matrix,
12992 // then redo the scroll translate to get the correct result.
12993 canvas.translate(-transX, -transY);
12994 canvas.concat(transformToApply.getMatrix());
12995 canvas.translate(transX, transY);
12996 }
Chet Haasea1cff502012-02-21 13:43:44 -080012997 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012998 }
12999
13000 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070013001 if (transformAlpha < 1) {
Chet Haase21433372012-06-05 07:54:09 -070013002 alpha *= transformAlpha;
Chet Haasea1cff502012-02-21 13:43:44 -080013003 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013004 }
13005 }
13006
Chet Haasea1cff502012-02-21 13:43:44 -080013007 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013008 canvas.translate(-transX, -transY);
13009 canvas.concat(getMatrix());
13010 canvas.translate(transX, transY);
13011 }
13012 }
13013
Chet Haase21433372012-06-05 07:54:09 -070013014 // Deal with alpha if it is or used to be <1
13015 if (alpha < 1 ||
13016 (mPrivateFlags3 & VIEW_IS_ANIMATING_ALPHA) == VIEW_IS_ANIMATING_ALPHA) {
13017 if (alpha < 1) {
13018 mPrivateFlags3 |= VIEW_IS_ANIMATING_ALPHA;
13019 } else {
13020 mPrivateFlags3 &= ~VIEW_IS_ANIMATING_ALPHA;
13021 }
Chet Haasea1cff502012-02-21 13:43:44 -080013022 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013023 if (hasNoCache) {
13024 final int multipliedAlpha = (int) (255 * alpha);
13025 if (!onSetAlpha(multipliedAlpha)) {
13026 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080013027 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080013028 layerType != LAYER_TYPE_NONE) {
13029 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
13030 }
Chet Haase9420abd2012-03-29 16:28:32 -070013031 if (useDisplayListProperties) {
13032 displayList.setAlpha(alpha * getAlpha());
13033 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070013034 final int scrollX = hasDisplayList ? 0 : sx;
13035 final int scrollY = hasDisplayList ? 0 : sy;
13036 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
13037 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080013038 }
13039 } else {
13040 // Alpha is handled by the child directly, clobber the layer's alpha
13041 mPrivateFlags |= ALPHA_SET;
13042 }
13043 }
13044 }
13045 } else if ((mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
13046 onSetAlpha(255);
13047 mPrivateFlags &= ~ALPHA_SET;
13048 }
13049
Chet Haasea1cff502012-02-21 13:43:44 -080013050 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
13051 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013052 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013053 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080013054 } else {
13055 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013056 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080013057 } else {
13058 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
13059 }
13060 }
13061 }
13062
Chet Haase9420abd2012-03-29 16:28:32 -070013063 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080013064 displayList = getDisplayList();
13065 if (!displayList.isValid()) {
13066 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13067 // to getDisplayList(), the display list will be marked invalid and we should not
13068 // try to use it again.
13069 displayList = null;
13070 hasDisplayList = false;
13071 }
13072 }
13073
13074 if (hasNoCache) {
13075 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080013076 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080013077 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080013078 if (layer != null && layer.isValid()) {
13079 mLayerPaint.setAlpha((int) (alpha * 255));
13080 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
13081 layerRendered = true;
13082 } else {
13083 final int scrollX = hasDisplayList ? 0 : sx;
13084 final int scrollY = hasDisplayList ? 0 : sy;
13085 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080013086 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080013087 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
13088 }
13089 }
13090
13091 if (!layerRendered) {
13092 if (!hasDisplayList) {
13093 // Fast path for layouts with no backgrounds
13094 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Chet Haase64a48c12012-02-13 16:33:29 -080013095 mPrivateFlags &= ~DIRTY_MASK;
13096 dispatchDraw(canvas);
13097 } else {
13098 draw(canvas);
13099 }
13100 } else {
13101 mPrivateFlags &= ~DIRTY_MASK;
Chet Haase1271e2c2012-04-20 09:54:27 -070013102 ((HardwareCanvas) canvas).drawDisplayList(displayList, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080013103 }
13104 }
13105 } else if (cache != null) {
13106 mPrivateFlags &= ~DIRTY_MASK;
13107 Paint cachePaint;
13108
13109 if (layerType == LAYER_TYPE_NONE) {
13110 cachePaint = parent.mCachePaint;
13111 if (cachePaint == null) {
13112 cachePaint = new Paint();
13113 cachePaint.setDither(false);
13114 parent.mCachePaint = cachePaint;
13115 }
Chet Haase9420abd2012-03-29 16:28:32 -070013116 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080013117 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080013118 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
13119 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013120 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080013121 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080013122 }
13123 } else {
13124 cachePaint = mLayerPaint;
13125 cachePaint.setAlpha((int) (alpha * 255));
13126 }
13127 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
13128 }
13129
Chet Haasea1cff502012-02-21 13:43:44 -080013130 if (restoreTo >= 0) {
13131 canvas.restoreToCount(restoreTo);
13132 }
Chet Haase64a48c12012-02-13 16:33:29 -080013133
13134 if (a != null && !more) {
13135 if (!hardwareAccelerated && !a.getFillAfter()) {
13136 onSetAlpha(255);
13137 }
13138 parent.finishAnimatingView(this, a);
13139 }
13140
13141 if (more && hardwareAccelerated) {
13142 // invalidation is the trigger to recreate display lists, so if we're using
13143 // display lists to render, force an invalidate to allow the animation to
13144 // continue drawing another frame
13145 parent.invalidate(true);
13146 if (a.hasAlpha() && (mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
13147 // alpha animations should cause the child to recreate its display list
13148 invalidate(true);
13149 }
13150 }
13151
13152 mRecreateDisplayList = false;
13153
13154 return more;
13155 }
13156
13157 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013158 * Manually render this view (and all of its children) to the given Canvas.
13159 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070013160 * called. When implementing a view, implement
13161 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
13162 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013163 *
13164 * @param canvas The Canvas to which the View is rendered.
13165 */
13166 public void draw(Canvas canvas) {
Romain Guy5bcdff42009-05-14 21:27:18 -070013167 final int privateFlags = mPrivateFlags;
13168 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
13169 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
13170 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070013171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013172 /*
13173 * Draw traversal performs several drawing steps which must be executed
13174 * in the appropriate order:
13175 *
13176 * 1. Draw the background
13177 * 2. If necessary, save the canvas' layers to prepare for fading
13178 * 3. Draw view's content
13179 * 4. Draw children
13180 * 5. If necessary, draw the fading edges and restore layers
13181 * 6. Draw decorations (scrollbars for instance)
13182 */
13183
13184 // Step 1, draw the background, if needed
13185 int saveCount;
13186
Romain Guy24443ea2009-05-11 11:56:30 -070013187 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013188 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070013189 if (background != null) {
13190 final int scrollX = mScrollX;
13191 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013192
Romain Guy24443ea2009-05-11 11:56:30 -070013193 if (mBackgroundSizeChanged) {
13194 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
13195 mBackgroundSizeChanged = false;
13196 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013197
Romain Guy24443ea2009-05-11 11:56:30 -070013198 if ((scrollX | scrollY) == 0) {
13199 background.draw(canvas);
13200 } else {
13201 canvas.translate(scrollX, scrollY);
13202 background.draw(canvas);
13203 canvas.translate(-scrollX, -scrollY);
13204 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013205 }
13206 }
13207
13208 // skip step 2 & 5 if possible (common case)
13209 final int viewFlags = mViewFlags;
13210 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
13211 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
13212 if (!verticalEdges && !horizontalEdges) {
13213 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013214 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013215
13216 // Step 4, draw the children
13217 dispatchDraw(canvas);
13218
13219 // Step 6, draw decorations (scrollbars)
13220 onDrawScrollBars(canvas);
13221
13222 // we're done...
13223 return;
13224 }
13225
13226 /*
13227 * Here we do the full fledged routine...
13228 * (this is an uncommon case where speed matters less,
13229 * this is why we repeat some of the tests that have been
13230 * done above)
13231 */
13232
13233 boolean drawTop = false;
13234 boolean drawBottom = false;
13235 boolean drawLeft = false;
13236 boolean drawRight = false;
13237
13238 float topFadeStrength = 0.0f;
13239 float bottomFadeStrength = 0.0f;
13240 float leftFadeStrength = 0.0f;
13241 float rightFadeStrength = 0.0f;
13242
13243 // Step 2, save the canvas' layers
13244 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013245
13246 final boolean offsetRequired = isPaddingOffsetRequired();
13247 if (offsetRequired) {
13248 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013249 }
13250
13251 int left = mScrollX + paddingLeft;
13252 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070013253 int top = mScrollY + getFadeTop(offsetRequired);
13254 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013255
13256 if (offsetRequired) {
13257 right += getRightPaddingOffset();
13258 bottom += getBottomPaddingOffset();
13259 }
13260
13261 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070013262 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013263 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013264
13265 // clip the fade length if top and bottom fades overlap
13266 // overlapping fades produce odd-looking artifacts
13267 if (verticalEdges && (top + length > bottom - length)) {
13268 length = (bottom - top) / 2;
13269 }
13270
13271 // also clip horizontal fades if necessary
13272 if (horizontalEdges && (left + length > right - length)) {
13273 length = (right - left) / 2;
13274 }
13275
13276 if (verticalEdges) {
13277 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013278 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013279 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013280 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013281 }
13282
13283 if (horizontalEdges) {
13284 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013285 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013286 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013287 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013288 }
13289
13290 saveCount = canvas.getSaveCount();
13291
13292 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070013293 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013294 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
13295
13296 if (drawTop) {
13297 canvas.saveLayer(left, top, right, top + length, null, flags);
13298 }
13299
13300 if (drawBottom) {
13301 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
13302 }
13303
13304 if (drawLeft) {
13305 canvas.saveLayer(left, top, left + length, bottom, null, flags);
13306 }
13307
13308 if (drawRight) {
13309 canvas.saveLayer(right - length, top, right, bottom, null, flags);
13310 }
13311 } else {
13312 scrollabilityCache.setFadeColor(solidColor);
13313 }
13314
13315 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013316 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013317
13318 // Step 4, draw the children
13319 dispatchDraw(canvas);
13320
13321 // Step 5, draw the fade effect and restore layers
13322 final Paint p = scrollabilityCache.paint;
13323 final Matrix matrix = scrollabilityCache.matrix;
13324 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013325
13326 if (drawTop) {
13327 matrix.setScale(1, fadeHeight * topFadeStrength);
13328 matrix.postTranslate(left, top);
13329 fade.setLocalMatrix(matrix);
13330 canvas.drawRect(left, top, right, top + length, p);
13331 }
13332
13333 if (drawBottom) {
13334 matrix.setScale(1, fadeHeight * bottomFadeStrength);
13335 matrix.postRotate(180);
13336 matrix.postTranslate(left, bottom);
13337 fade.setLocalMatrix(matrix);
13338 canvas.drawRect(left, bottom - length, right, bottom, p);
13339 }
13340
13341 if (drawLeft) {
13342 matrix.setScale(1, fadeHeight * leftFadeStrength);
13343 matrix.postRotate(-90);
13344 matrix.postTranslate(left, top);
13345 fade.setLocalMatrix(matrix);
13346 canvas.drawRect(left, top, left + length, bottom, p);
13347 }
13348
13349 if (drawRight) {
13350 matrix.setScale(1, fadeHeight * rightFadeStrength);
13351 matrix.postRotate(90);
13352 matrix.postTranslate(right, top);
13353 fade.setLocalMatrix(matrix);
13354 canvas.drawRect(right - length, top, right, bottom, p);
13355 }
13356
13357 canvas.restoreToCount(saveCount);
13358
13359 // Step 6, draw decorations (scrollbars)
13360 onDrawScrollBars(canvas);
13361 }
13362
13363 /**
13364 * Override this if your view is known to always be drawn on top of a solid color background,
13365 * and needs to draw fading edges. Returning a non-zero color enables the view system to
13366 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
13367 * should be set to 0xFF.
13368 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013369 * @see #setVerticalFadingEdgeEnabled(boolean)
13370 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013371 *
13372 * @return The known solid color background for this view, or 0 if the color may vary
13373 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013374 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013375 public int getSolidColor() {
13376 return 0;
13377 }
13378
13379 /**
13380 * Build a human readable string representation of the specified view flags.
13381 *
13382 * @param flags the view flags to convert to a string
13383 * @return a String representing the supplied flags
13384 */
13385 private static String printFlags(int flags) {
13386 String output = "";
13387 int numFlags = 0;
13388 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
13389 output += "TAKES_FOCUS";
13390 numFlags++;
13391 }
13392
13393 switch (flags & VISIBILITY_MASK) {
13394 case INVISIBLE:
13395 if (numFlags > 0) {
13396 output += " ";
13397 }
13398 output += "INVISIBLE";
13399 // USELESS HERE numFlags++;
13400 break;
13401 case GONE:
13402 if (numFlags > 0) {
13403 output += " ";
13404 }
13405 output += "GONE";
13406 // USELESS HERE numFlags++;
13407 break;
13408 default:
13409 break;
13410 }
13411 return output;
13412 }
13413
13414 /**
13415 * Build a human readable string representation of the specified private
13416 * view flags.
13417 *
13418 * @param privateFlags the private view flags to convert to a string
13419 * @return a String representing the supplied flags
13420 */
13421 private static String printPrivateFlags(int privateFlags) {
13422 String output = "";
13423 int numFlags = 0;
13424
13425 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
13426 output += "WANTS_FOCUS";
13427 numFlags++;
13428 }
13429
13430 if ((privateFlags & FOCUSED) == FOCUSED) {
13431 if (numFlags > 0) {
13432 output += " ";
13433 }
13434 output += "FOCUSED";
13435 numFlags++;
13436 }
13437
13438 if ((privateFlags & SELECTED) == SELECTED) {
13439 if (numFlags > 0) {
13440 output += " ";
13441 }
13442 output += "SELECTED";
13443 numFlags++;
13444 }
13445
13446 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
13447 if (numFlags > 0) {
13448 output += " ";
13449 }
13450 output += "IS_ROOT_NAMESPACE";
13451 numFlags++;
13452 }
13453
13454 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
13455 if (numFlags > 0) {
13456 output += " ";
13457 }
13458 output += "HAS_BOUNDS";
13459 numFlags++;
13460 }
13461
13462 if ((privateFlags & DRAWN) == DRAWN) {
13463 if (numFlags > 0) {
13464 output += " ";
13465 }
13466 output += "DRAWN";
13467 // USELESS HERE numFlags++;
13468 }
13469 return output;
13470 }
13471
13472 /**
13473 * <p>Indicates whether or not this view's layout will be requested during
13474 * the next hierarchy layout pass.</p>
13475 *
13476 * @return true if the layout will be forced during next layout pass
13477 */
13478 public boolean isLayoutRequested() {
13479 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
13480 }
13481
13482 /**
13483 * Assign a size and position to a view and all of its
13484 * descendants
13485 *
13486 * <p>This is the second phase of the layout mechanism.
13487 * (The first is measuring). In this phase, each parent calls
13488 * layout on all of its children to position them.
13489 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080013490 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013491 *
Chet Haase9c087442011-01-12 16:20:16 -080013492 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013493 * Derived classes with children should override
13494 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080013495 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013496 *
13497 * @param l Left position, relative to parent
13498 * @param t Top position, relative to parent
13499 * @param r Right position, relative to parent
13500 * @param b Bottom position, relative to parent
13501 */
Romain Guy5429e1d2010-09-07 12:38:00 -070013502 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080013503 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070013504 int oldL = mLeft;
13505 int oldT = mTop;
13506 int oldB = mBottom;
13507 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013508 boolean changed = setFrame(l, t, r, b);
13509 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013510 onLayout(changed, l, t, r, b);
13511 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070013512
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013513 ListenerInfo li = mListenerInfo;
13514 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070013515 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013516 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070013517 int numListeners = listenersCopy.size();
13518 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070013519 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070013520 }
13521 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013522 }
13523 mPrivateFlags &= ~FORCE_LAYOUT;
13524 }
13525
13526 /**
13527 * Called from layout when this view should
13528 * assign a size and position to each of its children.
13529 *
13530 * Derived classes with children should override
13531 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070013532 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013533 * @param changed This is a new size or position for this view
13534 * @param left Left position, relative to parent
13535 * @param top Top position, relative to parent
13536 * @param right Right position, relative to parent
13537 * @param bottom Bottom position, relative to parent
13538 */
13539 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
13540 }
13541
13542 /**
13543 * Assign a size and position to this view.
13544 *
13545 * This is called from layout.
13546 *
13547 * @param left Left position, relative to parent
13548 * @param top Top position, relative to parent
13549 * @param right Right position, relative to parent
13550 * @param bottom Bottom position, relative to parent
13551 * @return true if the new size and position are different than the
13552 * previous ones
13553 * {@hide}
13554 */
13555 protected boolean setFrame(int left, int top, int right, int bottom) {
13556 boolean changed = false;
13557
13558 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070013559 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013560 + right + "," + bottom + ")");
13561 }
13562
13563 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
13564 changed = true;
13565
13566 // Remember our drawn bit
13567 int drawn = mPrivateFlags & DRAWN;
13568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013569 int oldWidth = mRight - mLeft;
13570 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070013571 int newWidth = right - left;
13572 int newHeight = bottom - top;
13573 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
13574
13575 // Invalidate our old position
13576 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013577
13578 mLeft = left;
13579 mTop = top;
13580 mRight = right;
13581 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -070013582 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013583 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
13584 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013585
13586 mPrivateFlags |= HAS_BOUNDS;
13587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013588
Chet Haase75755e22011-07-18 17:48:25 -070013589 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013590 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
13591 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013592 if (mTransformationInfo != null) {
13593 mTransformationInfo.mMatrixDirty = true;
13594 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013595 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013596 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
13597 }
13598
13599 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
13600 // If we are visible, force the DRAWN bit to on so that
13601 // this invalidate will go through (at least to our parent).
13602 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013603 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013604 // the DRAWN bit.
13605 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070013606 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080013607 // parent display list may need to be recreated based on a change in the bounds
13608 // of any child
13609 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013610 }
13611
13612 // Reset drawn bit to original value (invalidate turns it off)
13613 mPrivateFlags |= drawn;
13614
13615 mBackgroundSizeChanged = true;
13616 }
13617 return changed;
13618 }
13619
13620 /**
13621 * Finalize inflating a view from XML. This is called as the last phase
13622 * of inflation, after all child views have been added.
13623 *
13624 * <p>Even if the subclass overrides onFinishInflate, they should always be
13625 * sure to call the super method, so that we get called.
13626 */
13627 protected void onFinishInflate() {
13628 }
13629
13630 /**
13631 * Returns the resources associated with this view.
13632 *
13633 * @return Resources object.
13634 */
13635 public Resources getResources() {
13636 return mResources;
13637 }
13638
13639 /**
13640 * Invalidates the specified Drawable.
13641 *
13642 * @param drawable the drawable to invalidate
13643 */
13644 public void invalidateDrawable(Drawable drawable) {
13645 if (verifyDrawable(drawable)) {
13646 final Rect dirty = drawable.getBounds();
13647 final int scrollX = mScrollX;
13648 final int scrollY = mScrollY;
13649
13650 invalidate(dirty.left + scrollX, dirty.top + scrollY,
13651 dirty.right + scrollX, dirty.bottom + scrollY);
13652 }
13653 }
13654
13655 /**
13656 * Schedules an action on a drawable to occur at a specified time.
13657 *
13658 * @param who the recipient of the action
13659 * @param what the action to run on the drawable
13660 * @param when the time at which the action must occur. Uses the
13661 * {@link SystemClock#uptimeMillis} timebase.
13662 */
13663 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080013664 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013665 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080013666 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013667 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
13668 Choreographer.CALLBACK_ANIMATION, what, who,
13669 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080013670 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013671 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080013672 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013673 }
13674 }
13675
13676 /**
13677 * Cancels a scheduled action on a drawable.
13678 *
13679 * @param who the recipient of the action
13680 * @param what the action to cancel
13681 */
13682 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080013683 if (verifyDrawable(who) && what != null) {
13684 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013685 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13686 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080013687 } else {
13688 ViewRootImpl.getRunQueue().removeCallbacks(what);
13689 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013690 }
13691 }
13692
13693 /**
13694 * Unschedule any events associated with the given Drawable. This can be
13695 * used when selecting a new Drawable into a view, so that the previous
13696 * one is completely unscheduled.
13697 *
13698 * @param who The Drawable to unschedule.
13699 *
13700 * @see #drawableStateChanged
13701 */
13702 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080013703 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013704 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13705 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013706 }
13707 }
13708
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013709 /**
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070013710 * Resolve the Drawables depending on the layout direction. This is implicitly supposing
13711 * that the View directionality can and will be resolved before its Drawables.
13712 *
13713 * Will call {@link View#onResolveDrawables} when resolution is done.
13714 */
13715 public void resolveDrawables() {
13716 if (mBackground != null) {
13717 mBackground.setLayoutDirection(getResolvedLayoutDirection());
13718 }
13719 onResolveDrawables(getResolvedLayoutDirection());
13720 }
13721
13722 /**
13723 * Called when layout direction has been resolved.
13724 *
13725 * The default implementation does nothing.
13726 *
13727 * @param layoutDirection The resolved layout direction.
13728 *
13729 * @see {@link #LAYOUT_DIRECTION_LTR}
13730 * @see {@link #LAYOUT_DIRECTION_RTL}
13731 */
13732 public void onResolveDrawables(int layoutDirection) {
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070013733 }
13734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013735 /**
13736 * If your view subclass is displaying its own Drawable objects, it should
13737 * override this function and return true for any Drawable it is
13738 * displaying. This allows animations for those drawables to be
13739 * scheduled.
13740 *
13741 * <p>Be sure to call through to the super class when overriding this
13742 * function.
13743 *
13744 * @param who The Drawable to verify. Return true if it is one you are
13745 * displaying, else return the result of calling through to the
13746 * super class.
13747 *
13748 * @return boolean If true than the Drawable is being displayed in the
13749 * view; else false and it is not allowed to animate.
13750 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013751 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
13752 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013753 */
13754 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013755 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013756 }
13757
13758 /**
13759 * This function is called whenever the state of the view changes in such
13760 * a way that it impacts the state of drawables being shown.
13761 *
13762 * <p>Be sure to call through to the superclass when overriding this
13763 * function.
13764 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013765 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013766 */
13767 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013768 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013769 if (d != null && d.isStateful()) {
13770 d.setState(getDrawableState());
13771 }
13772 }
13773
13774 /**
13775 * Call this to force a view to update its drawable state. This will cause
13776 * drawableStateChanged to be called on this view. Views that are interested
13777 * in the new state should call getDrawableState.
13778 *
13779 * @see #drawableStateChanged
13780 * @see #getDrawableState
13781 */
13782 public void refreshDrawableState() {
13783 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
13784 drawableStateChanged();
13785
13786 ViewParent parent = mParent;
13787 if (parent != null) {
13788 parent.childDrawableStateChanged(this);
13789 }
13790 }
13791
13792 /**
13793 * Return an array of resource IDs of the drawable states representing the
13794 * current state of the view.
13795 *
13796 * @return The current drawable state
13797 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013798 * @see Drawable#setState(int[])
13799 * @see #drawableStateChanged()
13800 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013801 */
13802 public final int[] getDrawableState() {
13803 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
13804 return mDrawableState;
13805 } else {
13806 mDrawableState = onCreateDrawableState(0);
13807 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
13808 return mDrawableState;
13809 }
13810 }
13811
13812 /**
13813 * Generate the new {@link android.graphics.drawable.Drawable} state for
13814 * this view. This is called by the view
13815 * system when the cached Drawable state is determined to be invalid. To
13816 * retrieve the current state, you should use {@link #getDrawableState}.
13817 *
13818 * @param extraSpace if non-zero, this is the number of extra entries you
13819 * would like in the returned array in which you can place your own
13820 * states.
13821 *
13822 * @return Returns an array holding the current {@link Drawable} state of
13823 * the view.
13824 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013825 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013826 */
13827 protected int[] onCreateDrawableState(int extraSpace) {
13828 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
13829 mParent instanceof View) {
13830 return ((View) mParent).onCreateDrawableState(extraSpace);
13831 }
13832
13833 int[] drawableState;
13834
13835 int privateFlags = mPrivateFlags;
13836
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013837 int viewStateIndex = 0;
13838 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
13839 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
13840 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070013841 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013842 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
13843 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070013844 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
13845 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080013846 // This is set if HW acceleration is requested, even if the current
13847 // process doesn't allow it. This is just to allow app preview
13848 // windows to better match their app.
13849 viewStateIndex |= VIEW_STATE_ACCELERATED;
13850 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070013851 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013852
Christopher Tate3d4bf172011-03-28 16:16:46 -070013853 final int privateFlags2 = mPrivateFlags2;
13854 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
13855 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
13856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013857 drawableState = VIEW_STATE_SETS[viewStateIndex];
13858
13859 //noinspection ConstantIfStatement
13860 if (false) {
13861 Log.i("View", "drawableStateIndex=" + viewStateIndex);
13862 Log.i("View", toString()
13863 + " pressed=" + ((privateFlags & PRESSED) != 0)
13864 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
13865 + " fo=" + hasFocus()
13866 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013867 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013868 + ": " + Arrays.toString(drawableState));
13869 }
13870
13871 if (extraSpace == 0) {
13872 return drawableState;
13873 }
13874
13875 final int[] fullState;
13876 if (drawableState != null) {
13877 fullState = new int[drawableState.length + extraSpace];
13878 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
13879 } else {
13880 fullState = new int[extraSpace];
13881 }
13882
13883 return fullState;
13884 }
13885
13886 /**
13887 * Merge your own state values in <var>additionalState</var> into the base
13888 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070013889 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013890 *
13891 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070013892 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013893 * own additional state values.
13894 *
13895 * @param additionalState The additional state values you would like
13896 * added to <var>baseState</var>; this array is not modified.
13897 *
13898 * @return As a convenience, the <var>baseState</var> array you originally
13899 * passed into the function is returned.
13900 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013901 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013902 */
13903 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
13904 final int N = baseState.length;
13905 int i = N - 1;
13906 while (i >= 0 && baseState[i] == 0) {
13907 i--;
13908 }
13909 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
13910 return baseState;
13911 }
13912
13913 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070013914 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
13915 * on all Drawable objects associated with this view.
13916 */
13917 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013918 if (mBackground != null) {
13919 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070013920 }
13921 }
13922
13923 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013924 * Sets the background color for this view.
13925 * @param color the color of the background
13926 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000013927 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013928 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013929 if (mBackground instanceof ColorDrawable) {
13930 ((ColorDrawable) mBackground).setColor(color);
Romain Guy7d3082a2012-07-11 17:52:54 -070013931 computeOpaqueFlags();
Chet Haase70d4ba12010-10-06 09:46:45 -070013932 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070013933 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070013934 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013935 }
13936
13937 /**
13938 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070013939 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013940 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070013941 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013942 * @attr ref android.R.styleable#View_background
13943 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000013944 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013945 public void setBackgroundResource(int resid) {
13946 if (resid != 0 && resid == mBackgroundResource) {
13947 return;
13948 }
13949
13950 Drawable d= null;
13951 if (resid != 0) {
13952 d = mResources.getDrawable(resid);
13953 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013954 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013955
13956 mBackgroundResource = resid;
13957 }
13958
13959 /**
13960 * Set the background to a given Drawable, or remove the background. If the
13961 * background has padding, this View's padding is set to the background's
13962 * padding. However, when a background is removed, this View's padding isn't
13963 * touched. If setting the padding is desired, please use
13964 * {@link #setPadding(int, int, int, int)}.
13965 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013966 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013967 * background
13968 */
Philip Milne6c8ea062012-04-03 17:38:43 -070013969 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070013970 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070013971 setBackgroundDrawable(background);
13972 }
13973
13974 /**
13975 * @deprecated use {@link #setBackground(Drawable)} instead
13976 */
13977 @Deprecated
13978 public void setBackgroundDrawable(Drawable background) {
Romain Guy846a5332012-07-11 17:44:57 -070013979 computeOpaqueFlags();
13980
Philip Milne6c8ea062012-04-03 17:38:43 -070013981 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070013982 return;
13983 }
13984
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013985 boolean requestLayout = false;
13986
13987 mBackgroundResource = 0;
13988
13989 /*
13990 * Regardless of whether we're setting a new background or not, we want
13991 * to clear the previous drawable.
13992 */
Philip Milne6c8ea062012-04-03 17:38:43 -070013993 if (mBackground != null) {
13994 mBackground.setCallback(null);
13995 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013996 }
13997
Philip Milne6c8ea062012-04-03 17:38:43 -070013998 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013999 Rect padding = sThreadLocal.get();
14000 if (padding == null) {
14001 padding = new Rect();
14002 sThreadLocal.set(padding);
14003 }
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014004 background.setLayoutDirection(getResolvedLayoutDirection());
Philip Milne6c8ea062012-04-03 17:38:43 -070014005 if (background.getPadding(padding)) {
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014006 switch (background.getLayoutDirection()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014007 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014008 internalSetPadding(padding.right, padding.top, padding.left, padding.bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014009 break;
14010 case LAYOUT_DIRECTION_LTR:
14011 default:
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014012 internalSetPadding(padding.left, padding.top, padding.right, padding.bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014013 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014014 }
14015
14016 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
14017 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070014018 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
14019 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014020 requestLayout = true;
14021 }
14022
Philip Milne6c8ea062012-04-03 17:38:43 -070014023 background.setCallback(this);
14024 if (background.isStateful()) {
14025 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014026 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014027 background.setVisible(getVisibility() == VISIBLE, false);
14028 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014029
14030 if ((mPrivateFlags & SKIP_DRAW) != 0) {
14031 mPrivateFlags &= ~SKIP_DRAW;
14032 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
14033 requestLayout = true;
14034 }
14035 } else {
14036 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070014037 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014038
14039 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
14040 /*
14041 * This view ONLY drew the background before and we're removing
14042 * the background, so now it won't draw anything
14043 * (hence we SKIP_DRAW)
14044 */
14045 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
14046 mPrivateFlags |= SKIP_DRAW;
14047 }
14048
14049 /*
14050 * When the background is set, we try to apply its padding to this
14051 * View. When the background is removed, we don't touch this View's
14052 * padding. This is noted in the Javadocs. Hence, we don't need to
14053 * requestLayout(), the invalidate() below is sufficient.
14054 */
14055
14056 // The old background's minimum size could have affected this
14057 // View's layout, so let's requestLayout
14058 requestLayout = true;
14059 }
14060
Romain Guy8f1344f52009-05-15 16:03:59 -070014061 computeOpaqueFlags();
14062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014063 if (requestLayout) {
14064 requestLayout();
14065 }
14066
14067 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014068 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014069 }
14070
14071 /**
14072 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070014073 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014074 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070014075 *
14076 * @see #setBackground(Drawable)
14077 *
14078 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014079 */
14080 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014081 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014082 }
14083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014084 /**
14085 * Sets the padding. The view may add on the space required to display
14086 * the scrollbars, depending on the style and visibility of the scrollbars.
14087 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
14088 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
14089 * from the values set in this call.
14090 *
14091 * @attr ref android.R.styleable#View_padding
14092 * @attr ref android.R.styleable#View_paddingBottom
14093 * @attr ref android.R.styleable#View_paddingLeft
14094 * @attr ref android.R.styleable#View_paddingRight
14095 * @attr ref android.R.styleable#View_paddingTop
14096 * @param left the left padding in pixels
14097 * @param top the top padding in pixels
14098 * @param right the right padding in pixels
14099 * @param bottom the bottom padding in pixels
14100 */
14101 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014102 mUserPaddingStart = UNDEFINED_PADDING;
14103 mUserPaddingEnd = UNDEFINED_PADDING;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014104
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014105 internalSetPadding(left, top, right, bottom);
14106 }
14107
14108 private void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080014109 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014110 mUserPaddingRight = right;
14111 mUserPaddingBottom = bottom;
14112
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014113 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014114 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070014115
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014116 // Common case is there are no scroll bars.
14117 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014118 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080014119 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014120 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080014121 switch (mVerticalScrollbarPosition) {
14122 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Meglioc91b6ca2012-06-22 14:51:15 -070014123 if (isLayoutRtl()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014124 left += offset;
14125 } else {
14126 right += offset;
14127 }
14128 break;
Adam Powell20232d02010-12-08 21:08:53 -080014129 case SCROLLBAR_POSITION_RIGHT:
14130 right += offset;
14131 break;
14132 case SCROLLBAR_POSITION_LEFT:
14133 left += offset;
14134 break;
14135 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014136 }
Adam Powell20232d02010-12-08 21:08:53 -080014137 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014138 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
14139 ? 0 : getHorizontalScrollbarHeight();
14140 }
14141 }
Romain Guy8506ab42009-06-11 17:35:47 -070014142
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014143 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014144 changed = true;
14145 mPaddingLeft = left;
14146 }
14147 if (mPaddingTop != top) {
14148 changed = true;
14149 mPaddingTop = top;
14150 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014151 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014152 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014153 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014154 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014155 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014156 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014157 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014158 }
14159
14160 if (changed) {
14161 requestLayout();
14162 }
14163 }
14164
14165 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014166 * Sets the relative padding. The view may add on the space required to display
14167 * the scrollbars, depending on the style and visibility of the scrollbars.
14168 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
14169 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
14170 * from the values set in this call.
14171 *
14172 * @attr ref android.R.styleable#View_padding
14173 * @attr ref android.R.styleable#View_paddingBottom
14174 * @attr ref android.R.styleable#View_paddingStart
14175 * @attr ref android.R.styleable#View_paddingEnd
14176 * @attr ref android.R.styleable#View_paddingTop
14177 * @param start the start padding in pixels
14178 * @param top the top padding in pixels
14179 * @param end the end padding in pixels
14180 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014181 */
14182 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014183 mUserPaddingStart = start;
14184 mUserPaddingEnd = end;
14185
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014186 switch(getResolvedLayoutDirection()) {
14187 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014188 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014189 break;
14190 case LAYOUT_DIRECTION_LTR:
14191 default:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014192 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014193 }
14194 }
14195
14196 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014197 * Returns the top padding of this view.
14198 *
14199 * @return the top padding in pixels
14200 */
14201 public int getPaddingTop() {
14202 return mPaddingTop;
14203 }
14204
14205 /**
14206 * Returns the bottom padding of this view. If there are inset and enabled
14207 * scrollbars, this value may include the space required to display the
14208 * scrollbars as well.
14209 *
14210 * @return the bottom padding in pixels
14211 */
14212 public int getPaddingBottom() {
14213 return mPaddingBottom;
14214 }
14215
14216 /**
14217 * Returns the left padding of this view. If there are inset and enabled
14218 * scrollbars, this value may include the space required to display the
14219 * scrollbars as well.
14220 *
14221 * @return the left padding in pixels
14222 */
14223 public int getPaddingLeft() {
14224 return mPaddingLeft;
14225 }
14226
14227 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014228 * Returns the start padding of this view depending on its resolved layout direction.
14229 * If there are inset and enabled scrollbars, this value may include the space
14230 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014231 *
14232 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014233 */
14234 public int getPaddingStart() {
14235 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14236 mPaddingRight : mPaddingLeft;
14237 }
14238
14239 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014240 * Returns the right padding of this view. If there are inset and enabled
14241 * scrollbars, this value may include the space required to display the
14242 * scrollbars as well.
14243 *
14244 * @return the right padding in pixels
14245 */
14246 public int getPaddingRight() {
14247 return mPaddingRight;
14248 }
14249
14250 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014251 * Returns the end padding of this view depending on its resolved layout direction.
14252 * If there are inset and enabled scrollbars, this value may include the space
14253 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014254 *
14255 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014256 */
14257 public int getPaddingEnd() {
14258 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14259 mPaddingLeft : mPaddingRight;
14260 }
14261
14262 /**
14263 * Return if the padding as been set thru relative values
14264 * {@link #setPaddingRelative(int, int, int, int)} or thru
14265 * @attr ref android.R.styleable#View_paddingStart or
14266 * @attr ref android.R.styleable#View_paddingEnd
14267 *
14268 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014269 */
14270 public boolean isPaddingRelative() {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014271 return (mUserPaddingStart != UNDEFINED_PADDING || mUserPaddingEnd != UNDEFINED_PADDING);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014272 }
14273
14274 /**
Philip Milne1557fd72012-04-04 23:41:34 -070014275 * @hide
14276 */
Philip Milne7a23b492012-04-24 22:12:36 -070014277 public Insets getOpticalInsets() {
Philip Milne1557fd72012-04-04 23:41:34 -070014278 if (mLayoutInsets == null) {
Philip Milnebbd51f12012-04-18 15:09:05 -070014279 mLayoutInsets = (mBackground == null) ? Insets.NONE : mBackground.getLayoutInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070014280 }
14281 return mLayoutInsets;
14282 }
14283
14284 /**
14285 * @hide
14286 */
14287 public void setLayoutInsets(Insets layoutInsets) {
14288 mLayoutInsets = layoutInsets;
14289 }
14290
14291 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014292 * Changes the selection state of this view. A view can be selected or not.
14293 * Note that selection is not the same as focus. Views are typically
14294 * selected in the context of an AdapterView like ListView or GridView;
14295 * the selected view is the view that is highlighted.
14296 *
14297 * @param selected true if the view must be selected, false otherwise
14298 */
14299 public void setSelected(boolean selected) {
14300 if (((mPrivateFlags & SELECTED) != 0) != selected) {
14301 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070014302 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080014303 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014304 refreshDrawableState();
14305 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070014306 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
14307 notifyAccessibilityStateChanged();
14308 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014309 }
14310 }
14311
14312 /**
14313 * Dispatch setSelected to all of this View's children.
14314 *
14315 * @see #setSelected(boolean)
14316 *
14317 * @param selected The new selected state
14318 */
14319 protected void dispatchSetSelected(boolean selected) {
14320 }
14321
14322 /**
14323 * Indicates the selection state of this view.
14324 *
14325 * @return true if the view is selected, false otherwise
14326 */
14327 @ViewDebug.ExportedProperty
14328 public boolean isSelected() {
14329 return (mPrivateFlags & SELECTED) != 0;
14330 }
14331
14332 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014333 * Changes the activated state of this view. A view can be activated or not.
14334 * Note that activation is not the same as selection. Selection is
14335 * a transient property, representing the view (hierarchy) the user is
14336 * currently interacting with. Activation is a longer-term state that the
14337 * user can move views in and out of. For example, in a list view with
14338 * single or multiple selection enabled, the views in the current selection
14339 * set are activated. (Um, yeah, we are deeply sorry about the terminology
14340 * here.) The activated state is propagated down to children of the view it
14341 * is set on.
14342 *
14343 * @param activated true if the view must be activated, false otherwise
14344 */
14345 public void setActivated(boolean activated) {
14346 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
14347 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014348 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014349 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070014350 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014351 }
14352 }
14353
14354 /**
14355 * Dispatch setActivated to all of this View's children.
14356 *
14357 * @see #setActivated(boolean)
14358 *
14359 * @param activated The new activated state
14360 */
14361 protected void dispatchSetActivated(boolean activated) {
14362 }
14363
14364 /**
14365 * Indicates the activation state of this view.
14366 *
14367 * @return true if the view is activated, false otherwise
14368 */
14369 @ViewDebug.ExportedProperty
14370 public boolean isActivated() {
14371 return (mPrivateFlags & ACTIVATED) != 0;
14372 }
14373
14374 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014375 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
14376 * observer can be used to get notifications when global events, like
14377 * layout, happen.
14378 *
14379 * The returned ViewTreeObserver observer is not guaranteed to remain
14380 * valid for the lifetime of this View. If the caller of this method keeps
14381 * a long-lived reference to ViewTreeObserver, it should always check for
14382 * the return value of {@link ViewTreeObserver#isAlive()}.
14383 *
14384 * @return The ViewTreeObserver for this view's hierarchy.
14385 */
14386 public ViewTreeObserver getViewTreeObserver() {
14387 if (mAttachInfo != null) {
14388 return mAttachInfo.mTreeObserver;
14389 }
14390 if (mFloatingTreeObserver == null) {
14391 mFloatingTreeObserver = new ViewTreeObserver();
14392 }
14393 return mFloatingTreeObserver;
14394 }
14395
14396 /**
14397 * <p>Finds the topmost view in the current view hierarchy.</p>
14398 *
14399 * @return the topmost view containing this view
14400 */
14401 public View getRootView() {
14402 if (mAttachInfo != null) {
14403 final View v = mAttachInfo.mRootView;
14404 if (v != null) {
14405 return v;
14406 }
14407 }
Romain Guy8506ab42009-06-11 17:35:47 -070014408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014409 View parent = this;
14410
14411 while (parent.mParent != null && parent.mParent instanceof View) {
14412 parent = (View) parent.mParent;
14413 }
14414
14415 return parent;
14416 }
14417
14418 /**
14419 * <p>Computes the coordinates of this view on the screen. The argument
14420 * must be an array of two integers. After the method returns, the array
14421 * contains the x and y location in that order.</p>
14422 *
14423 * @param location an array of two integers in which to hold the coordinates
14424 */
14425 public void getLocationOnScreen(int[] location) {
14426 getLocationInWindow(location);
14427
14428 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070014429 if (info != null) {
14430 location[0] += info.mWindowLeft;
14431 location[1] += info.mWindowTop;
14432 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014433 }
14434
14435 /**
14436 * <p>Computes the coordinates of this view in its window. The argument
14437 * must be an array of two integers. After the method returns, the array
14438 * contains the x and y location in that order.</p>
14439 *
14440 * @param location an array of two integers in which to hold the coordinates
14441 */
14442 public void getLocationInWindow(int[] location) {
14443 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014444 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014445 }
14446
Gilles Debunne6583ce52011-12-06 18:09:02 -080014447 if (mAttachInfo == null) {
14448 // When the view is not attached to a window, this method does not make sense
14449 location[0] = location[1] = 0;
14450 return;
14451 }
14452
Gilles Debunnecea45132011-11-24 02:19:27 +010014453 float[] position = mAttachInfo.mTmpTransformLocation;
14454 position[0] = position[1] = 0.0f;
14455
14456 if (!hasIdentityMatrix()) {
14457 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014458 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014459
Gilles Debunnecea45132011-11-24 02:19:27 +010014460 position[0] += mLeft;
14461 position[1] += mTop;
14462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014463 ViewParent viewParent = mParent;
14464 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014465 final View view = (View) viewParent;
14466
14467 position[0] -= view.mScrollX;
14468 position[1] -= view.mScrollY;
14469
14470 if (!view.hasIdentityMatrix()) {
14471 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014472 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014473
14474 position[0] += view.mLeft;
14475 position[1] += view.mTop;
14476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014477 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070014478 }
Romain Guy8506ab42009-06-11 17:35:47 -070014479
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014480 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014481 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010014482 final ViewRootImpl vr = (ViewRootImpl) viewParent;
14483 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014484 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014485
14486 location[0] = (int) (position[0] + 0.5f);
14487 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014488 }
14489
14490 /**
14491 * {@hide}
14492 * @param id the id of the view to be found
14493 * @return the view of the specified id, null if cannot be found
14494 */
14495 protected View findViewTraversal(int id) {
14496 if (id == mID) {
14497 return this;
14498 }
14499 return null;
14500 }
14501
14502 /**
14503 * {@hide}
14504 * @param tag the tag of the view to be found
14505 * @return the view of specified tag, null if cannot be found
14506 */
14507 protected View findViewWithTagTraversal(Object tag) {
14508 if (tag != null && tag.equals(mTag)) {
14509 return this;
14510 }
14511 return null;
14512 }
14513
14514 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014515 * {@hide}
14516 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070014517 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080014518 * @return The first view that matches the predicate or null.
14519 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070014520 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080014521 if (predicate.apply(this)) {
14522 return this;
14523 }
14524 return null;
14525 }
14526
14527 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014528 * Look for a child view with the given id. If this view has the given
14529 * id, return this view.
14530 *
14531 * @param id The id to search for.
14532 * @return The view that has the given id in the hierarchy or null
14533 */
14534 public final View findViewById(int id) {
14535 if (id < 0) {
14536 return null;
14537 }
14538 return findViewTraversal(id);
14539 }
14540
14541 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070014542 * Finds a view by its unuque and stable accessibility id.
14543 *
14544 * @param accessibilityId The searched accessibility id.
14545 * @return The found view.
14546 */
14547 final View findViewByAccessibilityId(int accessibilityId) {
14548 if (accessibilityId < 0) {
14549 return null;
14550 }
14551 return findViewByAccessibilityIdTraversal(accessibilityId);
14552 }
14553
14554 /**
14555 * Performs the traversal to find a view by its unuque and stable accessibility id.
14556 *
14557 * <strong>Note:</strong>This method does not stop at the root namespace
14558 * boundary since the user can touch the screen at an arbitrary location
14559 * potentially crossing the root namespace bounday which will send an
14560 * accessibility event to accessibility services and they should be able
14561 * to obtain the event source. Also accessibility ids are guaranteed to be
14562 * unique in the window.
14563 *
14564 * @param accessibilityId The accessibility id.
14565 * @return The found view.
14566 */
14567 View findViewByAccessibilityIdTraversal(int accessibilityId) {
14568 if (getAccessibilityViewId() == accessibilityId) {
14569 return this;
14570 }
14571 return null;
14572 }
14573
14574 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014575 * Look for a child view with the given tag. If this view has the given
14576 * tag, return this view.
14577 *
14578 * @param tag The tag to search for, using "tag.equals(getTag())".
14579 * @return The View that has the given tag in the hierarchy or null
14580 */
14581 public final View findViewWithTag(Object tag) {
14582 if (tag == null) {
14583 return null;
14584 }
14585 return findViewWithTagTraversal(tag);
14586 }
14587
14588 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014589 * {@hide}
14590 * Look for a child view that matches the specified predicate.
14591 * If this view matches the predicate, return this view.
14592 *
14593 * @param predicate The predicate to evaluate.
14594 * @return The first view that matches the predicate or null.
14595 */
14596 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070014597 return findViewByPredicateTraversal(predicate, null);
14598 }
14599
14600 /**
14601 * {@hide}
14602 * Look for a child view that matches the specified predicate,
14603 * starting with the specified view and its descendents and then
14604 * recusively searching the ancestors and siblings of that view
14605 * until this view is reached.
14606 *
14607 * This method is useful in cases where the predicate does not match
14608 * a single unique view (perhaps multiple views use the same id)
14609 * and we are trying to find the view that is "closest" in scope to the
14610 * starting view.
14611 *
14612 * @param start The view to start from.
14613 * @param predicate The predicate to evaluate.
14614 * @return The first view that matches the predicate or null.
14615 */
14616 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
14617 View childToSkip = null;
14618 for (;;) {
14619 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
14620 if (view != null || start == this) {
14621 return view;
14622 }
14623
14624 ViewParent parent = start.getParent();
14625 if (parent == null || !(parent instanceof View)) {
14626 return null;
14627 }
14628
14629 childToSkip = start;
14630 start = (View) parent;
14631 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080014632 }
14633
14634 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014635 * Sets the identifier for this view. The identifier does not have to be
14636 * unique in this view's hierarchy. The identifier should be a positive
14637 * number.
14638 *
14639 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070014640 * @see #getId()
14641 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014642 *
14643 * @param id a number used to identify the view
14644 *
14645 * @attr ref android.R.styleable#View_id
14646 */
14647 public void setId(int id) {
14648 mID = id;
14649 }
14650
14651 /**
14652 * {@hide}
14653 *
14654 * @param isRoot true if the view belongs to the root namespace, false
14655 * otherwise
14656 */
14657 public void setIsRootNamespace(boolean isRoot) {
14658 if (isRoot) {
14659 mPrivateFlags |= IS_ROOT_NAMESPACE;
14660 } else {
14661 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
14662 }
14663 }
14664
14665 /**
14666 * {@hide}
14667 *
14668 * @return true if the view belongs to the root namespace, false otherwise
14669 */
14670 public boolean isRootNamespace() {
14671 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
14672 }
14673
14674 /**
14675 * Returns this view's identifier.
14676 *
14677 * @return a positive integer used to identify the view or {@link #NO_ID}
14678 * if the view has no ID
14679 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014680 * @see #setId(int)
14681 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014682 * @attr ref android.R.styleable#View_id
14683 */
14684 @ViewDebug.CapturedViewProperty
14685 public int getId() {
14686 return mID;
14687 }
14688
14689 /**
14690 * Returns this view's tag.
14691 *
14692 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070014693 *
14694 * @see #setTag(Object)
14695 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014696 */
14697 @ViewDebug.ExportedProperty
14698 public Object getTag() {
14699 return mTag;
14700 }
14701
14702 /**
14703 * Sets the tag associated with this view. A tag can be used to mark
14704 * a view in its hierarchy and does not have to be unique within the
14705 * hierarchy. Tags can also be used to store data within a view without
14706 * resorting to another data structure.
14707 *
14708 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070014709 *
14710 * @see #getTag()
14711 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014712 */
14713 public void setTag(final Object tag) {
14714 mTag = tag;
14715 }
14716
14717 /**
Romain Guyd90a3312009-05-06 14:54:28 -070014718 * Returns the tag associated with this view and the specified key.
14719 *
14720 * @param key The key identifying the tag
14721 *
14722 * @return the Object stored in this view as a tag
14723 *
14724 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070014725 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070014726 */
14727 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014728 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070014729 return null;
14730 }
14731
14732 /**
14733 * Sets a tag associated with this view and a key. A tag can be used
14734 * to mark a view in its hierarchy and does not have to be unique within
14735 * the hierarchy. Tags can also be used to store data within a view
14736 * without resorting to another data structure.
14737 *
14738 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070014739 * application to ensure it is unique (see the <a
14740 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
14741 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070014742 * the Android framework or not associated with any package will cause
14743 * an {@link IllegalArgumentException} to be thrown.
14744 *
14745 * @param key The key identifying the tag
14746 * @param tag An Object to tag the view with
14747 *
14748 * @throws IllegalArgumentException If they specified key is not valid
14749 *
14750 * @see #setTag(Object)
14751 * @see #getTag(int)
14752 */
14753 public void setTag(int key, final Object tag) {
14754 // If the package id is 0x00 or 0x01, it's either an undefined package
14755 // or a framework id
14756 if ((key >>> 24) < 2) {
14757 throw new IllegalArgumentException("The key must be an application-specific "
14758 + "resource id.");
14759 }
14760
Adam Powell2b2f6d62011-09-23 11:15:39 -070014761 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014762 }
14763
14764 /**
14765 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
14766 * framework id.
14767 *
14768 * @hide
14769 */
14770 public void setTagInternal(int key, Object tag) {
14771 if ((key >>> 24) != 0x1) {
14772 throw new IllegalArgumentException("The key must be a framework-specific "
14773 + "resource id.");
14774 }
14775
Adam Powell2b2f6d62011-09-23 11:15:39 -070014776 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014777 }
14778
Adam Powell2b2f6d62011-09-23 11:15:39 -070014779 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014780 if (mKeyedTags == null) {
14781 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070014782 }
14783
Adam Powell7db82ac2011-09-22 19:44:04 -070014784 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014785 }
14786
14787 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014788 * Prints information about this view in the log output, with the tag
14789 * {@link #VIEW_LOG_TAG}.
14790 *
14791 * @hide
14792 */
14793 public void debug() {
14794 debug(0);
14795 }
14796
14797 /**
14798 * Prints information about this view in the log output, with the tag
14799 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
14800 * indentation defined by the <code>depth</code>.
14801 *
14802 * @param depth the indentation level
14803 *
14804 * @hide
14805 */
14806 protected void debug(int depth) {
14807 String output = debugIndent(depth - 1);
14808
14809 output += "+ " + this;
14810 int id = getId();
14811 if (id != -1) {
14812 output += " (id=" + id + ")";
14813 }
14814 Object tag = getTag();
14815 if (tag != null) {
14816 output += " (tag=" + tag + ")";
14817 }
14818 Log.d(VIEW_LOG_TAG, output);
14819
14820 if ((mPrivateFlags & FOCUSED) != 0) {
14821 output = debugIndent(depth) + " FOCUSED";
14822 Log.d(VIEW_LOG_TAG, output);
14823 }
14824
14825 output = debugIndent(depth);
14826 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
14827 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
14828 + "} ";
14829 Log.d(VIEW_LOG_TAG, output);
14830
14831 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
14832 || mPaddingBottom != 0) {
14833 output = debugIndent(depth);
14834 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
14835 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
14836 Log.d(VIEW_LOG_TAG, output);
14837 }
14838
14839 output = debugIndent(depth);
14840 output += "mMeasureWidth=" + mMeasuredWidth +
14841 " mMeasureHeight=" + mMeasuredHeight;
14842 Log.d(VIEW_LOG_TAG, output);
14843
14844 output = debugIndent(depth);
14845 if (mLayoutParams == null) {
14846 output += "BAD! no layout params";
14847 } else {
14848 output = mLayoutParams.debug(output);
14849 }
14850 Log.d(VIEW_LOG_TAG, output);
14851
14852 output = debugIndent(depth);
14853 output += "flags={";
14854 output += View.printFlags(mViewFlags);
14855 output += "}";
14856 Log.d(VIEW_LOG_TAG, output);
14857
14858 output = debugIndent(depth);
14859 output += "privateFlags={";
14860 output += View.printPrivateFlags(mPrivateFlags);
14861 output += "}";
14862 Log.d(VIEW_LOG_TAG, output);
14863 }
14864
14865 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090014866 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014867 *
14868 * @param depth the indentation level
14869 * @return a String containing (depth * 2 + 3) * 2 white spaces
14870 *
14871 * @hide
14872 */
14873 protected static String debugIndent(int depth) {
14874 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
14875 for (int i = 0; i < (depth * 2) + 3; i++) {
14876 spaces.append(' ').append(' ');
14877 }
14878 return spaces.toString();
14879 }
14880
14881 /**
14882 * <p>Return the offset of the widget's text baseline from the widget's top
14883 * boundary. If this widget does not support baseline alignment, this
14884 * method returns -1. </p>
14885 *
14886 * @return the offset of the baseline within the widget's bounds or -1
14887 * if baseline alignment is not supported
14888 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070014889 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014890 public int getBaseline() {
14891 return -1;
14892 }
14893
14894 /**
14895 * Call this when something has changed which has invalidated the
14896 * layout of this view. This will schedule a layout pass of the view
14897 * tree.
14898 */
14899 public void requestLayout() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014900 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080014901 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014902
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070014903 if (mLayoutParams != null) {
14904 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
14905 }
14906
14907 if (mParent != null && !mParent.isLayoutRequested()) {
14908 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014909 }
14910 }
14911
14912 /**
14913 * Forces this view to be laid out during the next layout pass.
14914 * This method does not call requestLayout() or forceLayout()
14915 * on the parent.
14916 */
14917 public void forceLayout() {
14918 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080014919 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014920 }
14921
14922 /**
14923 * <p>
14924 * This is called to find out how big a view should be. The parent
14925 * supplies constraint information in the width and height parameters.
14926 * </p>
14927 *
14928 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080014929 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014930 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080014931 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014932 * </p>
14933 *
14934 *
14935 * @param widthMeasureSpec Horizontal space requirements as imposed by the
14936 * parent
14937 * @param heightMeasureSpec Vertical space requirements as imposed by the
14938 * parent
14939 *
14940 * @see #onMeasure(int, int)
14941 */
14942 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
14943 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
14944 widthMeasureSpec != mOldWidthMeasureSpec ||
14945 heightMeasureSpec != mOldHeightMeasureSpec) {
14946
14947 // first clears the measured dimension flag
14948 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
14949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014950 // measure ourselves, this should set the measured dimension flag back
14951 onMeasure(widthMeasureSpec, heightMeasureSpec);
14952
14953 // flag not set, setMeasuredDimension() was not invoked, we raise
14954 // an exception to warn the developer
14955 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
14956 throw new IllegalStateException("onMeasure() did not set the"
14957 + " measured dimension by calling"
14958 + " setMeasuredDimension()");
14959 }
14960
14961 mPrivateFlags |= LAYOUT_REQUIRED;
14962 }
14963
14964 mOldWidthMeasureSpec = widthMeasureSpec;
14965 mOldHeightMeasureSpec = heightMeasureSpec;
14966 }
14967
14968 /**
14969 * <p>
14970 * Measure the view and its content to determine the measured width and the
14971 * measured height. This method is invoked by {@link #measure(int, int)} and
14972 * should be overriden by subclasses to provide accurate and efficient
14973 * measurement of their contents.
14974 * </p>
14975 *
14976 * <p>
14977 * <strong>CONTRACT:</strong> When overriding this method, you
14978 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
14979 * measured width and height of this view. Failure to do so will trigger an
14980 * <code>IllegalStateException</code>, thrown by
14981 * {@link #measure(int, int)}. Calling the superclass'
14982 * {@link #onMeasure(int, int)} is a valid use.
14983 * </p>
14984 *
14985 * <p>
14986 * The base class implementation of measure defaults to the background size,
14987 * unless a larger size is allowed by the MeasureSpec. Subclasses should
14988 * override {@link #onMeasure(int, int)} to provide better measurements of
14989 * their content.
14990 * </p>
14991 *
14992 * <p>
14993 * If this method is overridden, it is the subclass's responsibility to make
14994 * sure the measured height and width are at least the view's minimum height
14995 * and width ({@link #getSuggestedMinimumHeight()} and
14996 * {@link #getSuggestedMinimumWidth()}).
14997 * </p>
14998 *
14999 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
15000 * The requirements are encoded with
15001 * {@link android.view.View.MeasureSpec}.
15002 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
15003 * The requirements are encoded with
15004 * {@link android.view.View.MeasureSpec}.
15005 *
15006 * @see #getMeasuredWidth()
15007 * @see #getMeasuredHeight()
15008 * @see #setMeasuredDimension(int, int)
15009 * @see #getSuggestedMinimumHeight()
15010 * @see #getSuggestedMinimumWidth()
15011 * @see android.view.View.MeasureSpec#getMode(int)
15012 * @see android.view.View.MeasureSpec#getSize(int)
15013 */
15014 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
15015 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
15016 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
15017 }
15018
15019 /**
15020 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
15021 * measured width and measured height. Failing to do so will trigger an
15022 * exception at measurement time.</p>
15023 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080015024 * @param measuredWidth The measured width of this view. May be a complex
15025 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15026 * {@link #MEASURED_STATE_TOO_SMALL}.
15027 * @param measuredHeight The measured height of this view. May be a complex
15028 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15029 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015030 */
15031 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
15032 mMeasuredWidth = measuredWidth;
15033 mMeasuredHeight = measuredHeight;
15034
15035 mPrivateFlags |= MEASURED_DIMENSION_SET;
15036 }
15037
15038 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080015039 * Merge two states as returned by {@link #getMeasuredState()}.
15040 * @param curState The current state as returned from a view or the result
15041 * of combining multiple views.
15042 * @param newState The new view state to combine.
15043 * @return Returns a new integer reflecting the combination of the two
15044 * states.
15045 */
15046 public static int combineMeasuredStates(int curState, int newState) {
15047 return curState | newState;
15048 }
15049
15050 /**
15051 * Version of {@link #resolveSizeAndState(int, int, int)}
15052 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
15053 */
15054 public static int resolveSize(int size, int measureSpec) {
15055 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
15056 }
15057
15058 /**
15059 * Utility to reconcile a desired size and state, with constraints imposed
15060 * by a MeasureSpec. Will take the desired size, unless a different size
15061 * is imposed by the constraints. The returned value is a compound integer,
15062 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
15063 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
15064 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015065 *
15066 * @param size How big the view wants to be
15067 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080015068 * @return Size information bit mask as defined by
15069 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015070 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080015071 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015072 int result = size;
15073 int specMode = MeasureSpec.getMode(measureSpec);
15074 int specSize = MeasureSpec.getSize(measureSpec);
15075 switch (specMode) {
15076 case MeasureSpec.UNSPECIFIED:
15077 result = size;
15078 break;
15079 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080015080 if (specSize < size) {
15081 result = specSize | MEASURED_STATE_TOO_SMALL;
15082 } else {
15083 result = size;
15084 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015085 break;
15086 case MeasureSpec.EXACTLY:
15087 result = specSize;
15088 break;
15089 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080015090 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015091 }
15092
15093 /**
15094 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070015095 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015096 * by the MeasureSpec.
15097 *
15098 * @param size Default size for this view
15099 * @param measureSpec Constraints imposed by the parent
15100 * @return The size this view should be.
15101 */
15102 public static int getDefaultSize(int size, int measureSpec) {
15103 int result = size;
15104 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070015105 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015106
15107 switch (specMode) {
15108 case MeasureSpec.UNSPECIFIED:
15109 result = size;
15110 break;
15111 case MeasureSpec.AT_MOST:
15112 case MeasureSpec.EXACTLY:
15113 result = specSize;
15114 break;
15115 }
15116 return result;
15117 }
15118
15119 /**
15120 * Returns the suggested minimum height that the view should use. This
15121 * returns the maximum of the view's minimum height
15122 * and the background's minimum height
15123 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
15124 * <p>
15125 * When being used in {@link #onMeasure(int, int)}, the caller should still
15126 * ensure the returned height is within the requirements of the parent.
15127 *
15128 * @return The suggested minimum height of the view.
15129 */
15130 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015131 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015133 }
15134
15135 /**
15136 * Returns the suggested minimum width that the view should use. This
15137 * returns the maximum of the view's minimum width)
15138 * and the background's minimum width
15139 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
15140 * <p>
15141 * When being used in {@link #onMeasure(int, int)}, the caller should still
15142 * ensure the returned width is within the requirements of the parent.
15143 *
15144 * @return The suggested minimum width of the view.
15145 */
15146 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015147 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
15148 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015149
Philip Milne6c8ea062012-04-03 17:38:43 -070015150 /**
15151 * Returns the minimum height of the view.
15152 *
15153 * @return the minimum height the view will try to be.
15154 *
15155 * @see #setMinimumHeight(int)
15156 *
15157 * @attr ref android.R.styleable#View_minHeight
15158 */
15159 public int getMinimumHeight() {
15160 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015161 }
15162
15163 /**
15164 * Sets the minimum height of the view. It is not guaranteed the view will
15165 * be able to achieve this minimum height (for example, if its parent layout
15166 * constrains it with less available height).
15167 *
15168 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015169 *
15170 * @see #getMinimumHeight()
15171 *
15172 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015173 */
15174 public void setMinimumHeight(int minHeight) {
15175 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070015176 requestLayout();
15177 }
15178
15179 /**
15180 * Returns the minimum width of the view.
15181 *
15182 * @return the minimum width the view will try to be.
15183 *
15184 * @see #setMinimumWidth(int)
15185 *
15186 * @attr ref android.R.styleable#View_minWidth
15187 */
15188 public int getMinimumWidth() {
15189 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015190 }
15191
15192 /**
15193 * Sets the minimum width of the view. It is not guaranteed the view will
15194 * be able to achieve this minimum width (for example, if its parent layout
15195 * constrains it with less available width).
15196 *
15197 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015198 *
15199 * @see #getMinimumWidth()
15200 *
15201 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015202 */
15203 public void setMinimumWidth(int minWidth) {
15204 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070015205 requestLayout();
15206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015207 }
15208
15209 /**
15210 * Get the animation currently associated with this view.
15211 *
15212 * @return The animation that is currently playing or
15213 * scheduled to play for this view.
15214 */
15215 public Animation getAnimation() {
15216 return mCurrentAnimation;
15217 }
15218
15219 /**
15220 * Start the specified animation now.
15221 *
15222 * @param animation the animation to start now
15223 */
15224 public void startAnimation(Animation animation) {
15225 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
15226 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080015227 invalidateParentCaches();
15228 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015229 }
15230
15231 /**
15232 * Cancels any animations for this view.
15233 */
15234 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080015235 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080015236 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080015237 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015238 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080015239 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015240 }
15241
15242 /**
15243 * Sets the next animation to play for this view.
15244 * If you want the animation to play immediately, use
Chet Haase42428932012-05-11 15:39:07 -070015245 * {@link #startAnimation(android.view.animation.Animation)} instead.
15246 * This method provides allows fine-grained
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015247 * control over the start time and invalidation, but you
15248 * must make sure that 1) the animation has a start time set, and
Chet Haase42428932012-05-11 15:39:07 -070015249 * 2) the view's parent (which controls animations on its children)
15250 * will be invalidated when the animation is supposed to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015251 * start.
15252 *
15253 * @param animation The next animation, or null.
15254 */
15255 public void setAnimation(Animation animation) {
15256 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070015257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015258 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070015259 // If the screen is off assume the animation start time is now instead of
15260 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
15261 // would cause the animation to start when the screen turns back on
15262 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
15263 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
15264 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
15265 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015266 animation.reset();
15267 }
15268 }
15269
15270 /**
15271 * Invoked by a parent ViewGroup to notify the start of the animation
15272 * currently associated with this view. If you override this method,
15273 * always call super.onAnimationStart();
15274 *
15275 * @see #setAnimation(android.view.animation.Animation)
15276 * @see #getAnimation()
15277 */
15278 protected void onAnimationStart() {
15279 mPrivateFlags |= ANIMATION_STARTED;
15280 }
15281
15282 /**
15283 * Invoked by a parent ViewGroup to notify the end of the animation
15284 * currently associated with this view. If you override this method,
15285 * always call super.onAnimationEnd();
15286 *
15287 * @see #setAnimation(android.view.animation.Animation)
15288 * @see #getAnimation()
15289 */
15290 protected void onAnimationEnd() {
15291 mPrivateFlags &= ~ANIMATION_STARTED;
15292 }
15293
15294 /**
15295 * Invoked if there is a Transform that involves alpha. Subclass that can
15296 * draw themselves with the specified alpha should return true, and then
15297 * respect that alpha when their onDraw() is called. If this returns false
15298 * then the view may be redirected to draw into an offscreen buffer to
15299 * fulfill the request, which will look fine, but may be slower than if the
15300 * subclass handles it internally. The default implementation returns false.
15301 *
15302 * @param alpha The alpha (0..255) to apply to the view's drawing
15303 * @return true if the view can draw with the specified alpha.
15304 */
15305 protected boolean onSetAlpha(int alpha) {
15306 return false;
15307 }
15308
15309 /**
15310 * This is used by the RootView to perform an optimization when
15311 * the view hierarchy contains one or several SurfaceView.
15312 * SurfaceView is always considered transparent, but its children are not,
15313 * therefore all View objects remove themselves from the global transparent
15314 * region (passed as a parameter to this function).
15315 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015316 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015317 *
15318 * @return Returns true if the effective visibility of the view at this
15319 * point is opaque, regardless of the transparent region; returns false
15320 * if it is possible for underlying windows to be seen behind the view.
15321 *
15322 * {@hide}
15323 */
15324 public boolean gatherTransparentRegion(Region region) {
15325 final AttachInfo attachInfo = mAttachInfo;
15326 if (region != null && attachInfo != null) {
15327 final int pflags = mPrivateFlags;
15328 if ((pflags & SKIP_DRAW) == 0) {
15329 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
15330 // remove it from the transparent region.
15331 final int[] location = attachInfo.mTransparentLocation;
15332 getLocationInWindow(location);
15333 region.op(location[0], location[1], location[0] + mRight - mLeft,
15334 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Philip Milne6c8ea062012-04-03 17:38:43 -070015335 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015336 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
15337 // exists, so we remove the background drawable's non-transparent
15338 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070015339 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015340 }
15341 }
15342 return true;
15343 }
15344
15345 /**
15346 * Play a sound effect for this view.
15347 *
15348 * <p>The framework will play sound effects for some built in actions, such as
15349 * clicking, but you may wish to play these effects in your widget,
15350 * for instance, for internal navigation.
15351 *
15352 * <p>The sound effect will only be played if sound effects are enabled by the user, and
15353 * {@link #isSoundEffectsEnabled()} is true.
15354 *
15355 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
15356 */
15357 public void playSoundEffect(int soundConstant) {
15358 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
15359 return;
15360 }
15361 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
15362 }
15363
15364 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015365 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015366 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015367 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015368 *
15369 * <p>The framework will provide haptic feedback for some built in actions,
15370 * such as long presses, but you may wish to provide feedback for your
15371 * own widget.
15372 *
15373 * <p>The feedback will only be performed if
15374 * {@link #isHapticFeedbackEnabled()} is true.
15375 *
15376 * @param feedbackConstant One of the constants defined in
15377 * {@link HapticFeedbackConstants}
15378 */
15379 public boolean performHapticFeedback(int feedbackConstant) {
15380 return performHapticFeedback(feedbackConstant, 0);
15381 }
15382
15383 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015384 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015385 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015386 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015387 *
15388 * @param feedbackConstant One of the constants defined in
15389 * {@link HapticFeedbackConstants}
15390 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
15391 */
15392 public boolean performHapticFeedback(int feedbackConstant, int flags) {
15393 if (mAttachInfo == null) {
15394 return false;
15395 }
Romain Guyf607bdc2010-09-10 19:20:06 -070015396 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070015397 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015398 && !isHapticFeedbackEnabled()) {
15399 return false;
15400 }
Romain Guy812ccbe2010-06-01 14:07:24 -070015401 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
15402 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015403 }
15404
15405 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015406 * Request that the visibility of the status bar or other screen/window
15407 * decorations be changed.
15408 *
15409 * <p>This method is used to put the over device UI into temporary modes
15410 * where the user's attention is focused more on the application content,
15411 * by dimming or hiding surrounding system affordances. This is typically
15412 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
15413 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
15414 * to be placed behind the action bar (and with these flags other system
15415 * affordances) so that smooth transitions between hiding and showing them
15416 * can be done.
15417 *
15418 * <p>Two representative examples of the use of system UI visibility is
15419 * implementing a content browsing application (like a magazine reader)
15420 * and a video playing application.
15421 *
15422 * <p>The first code shows a typical implementation of a View in a content
15423 * browsing application. In this implementation, the application goes
15424 * into a content-oriented mode by hiding the status bar and action bar,
15425 * and putting the navigation elements into lights out mode. The user can
15426 * then interact with content while in this mode. Such an application should
15427 * provide an easy way for the user to toggle out of the mode (such as to
15428 * check information in the status bar or access notifications). In the
15429 * implementation here, this is done simply by tapping on the content.
15430 *
15431 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
15432 * content}
15433 *
15434 * <p>This second code sample shows a typical implementation of a View
15435 * in a video playing application. In this situation, while the video is
15436 * playing the application would like to go into a complete full-screen mode,
15437 * to use as much of the display as possible for the video. When in this state
15438 * the user can not interact with the application; the system intercepts
Dianne Hackborncf675782012-05-10 15:07:24 -070015439 * touching on the screen to pop the UI out of full screen mode. See
15440 * {@link #fitSystemWindows(Rect)} for a sample layout that goes with this code.
Dianne Hackborn98014352012-04-05 18:31:41 -070015441 *
15442 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
15443 * content}
15444 *
15445 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15446 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15447 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15448 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015449 */
15450 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040015451 if (visibility != mSystemUiVisibility) {
15452 mSystemUiVisibility = visibility;
15453 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15454 mParent.recomputeViewAttributes(this);
15455 }
Joe Onorato664644d2011-01-23 17:53:23 -080015456 }
15457 }
15458
15459 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015460 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
15461 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15462 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15463 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15464 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015465 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080015466 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080015467 return mSystemUiVisibility;
15468 }
15469
Scott Mainec6331b2011-05-24 16:55:56 -070015470 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070015471 * Returns the current system UI visibility that is currently set for
15472 * the entire window. This is the combination of the
15473 * {@link #setSystemUiVisibility(int)} values supplied by all of the
15474 * views in the window.
15475 */
15476 public int getWindowSystemUiVisibility() {
15477 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
15478 }
15479
15480 /**
15481 * Override to find out when the window's requested system UI visibility
15482 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
15483 * This is different from the callbacks recieved through
15484 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
15485 * in that this is only telling you about the local request of the window,
15486 * not the actual values applied by the system.
15487 */
15488 public void onWindowSystemUiVisibilityChanged(int visible) {
15489 }
15490
15491 /**
15492 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
15493 * the view hierarchy.
15494 */
15495 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
15496 onWindowSystemUiVisibilityChanged(visible);
15497 }
15498
15499 /**
Scott Mainec6331b2011-05-24 16:55:56 -070015500 * Set a listener to receive callbacks when the visibility of the system bar changes.
15501 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
15502 */
Joe Onorato664644d2011-01-23 17:53:23 -080015503 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015504 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080015505 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15506 mParent.recomputeViewAttributes(this);
15507 }
15508 }
15509
15510 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015511 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
15512 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080015513 */
15514 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015515 ListenerInfo li = mListenerInfo;
15516 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
15517 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080015518 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080015519 }
15520 }
15521
Dianne Hackborncf675782012-05-10 15:07:24 -070015522 boolean updateLocalSystemUiVisibility(int localValue, int localChanges) {
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015523 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
15524 if (val != mSystemUiVisibility) {
15525 setSystemUiVisibility(val);
Dianne Hackborncf675782012-05-10 15:07:24 -070015526 return true;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015527 }
Dianne Hackborncf675782012-05-10 15:07:24 -070015528 return false;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015529 }
15530
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070015531 /** @hide */
15532 public void setDisabledSystemUiVisibility(int flags) {
15533 if (mAttachInfo != null) {
15534 if (mAttachInfo.mDisabledSystemUiVisibility != flags) {
15535 mAttachInfo.mDisabledSystemUiVisibility = flags;
15536 if (mParent != null) {
15537 mParent.recomputeViewAttributes(this);
15538 }
15539 }
15540 }
15541 }
15542
Joe Onorato664644d2011-01-23 17:53:23 -080015543 /**
Joe Malin32736f02011-01-19 16:14:20 -080015544 * Creates an image that the system displays during the drag and drop
15545 * operation. This is called a &quot;drag shadow&quot;. The default implementation
15546 * for a DragShadowBuilder based on a View returns an image that has exactly the same
15547 * appearance as the given View. The default also positions the center of the drag shadow
15548 * directly under the touch point. If no View is provided (the constructor with no parameters
15549 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
15550 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
15551 * default is an invisible drag shadow.
15552 * <p>
15553 * You are not required to use the View you provide to the constructor as the basis of the
15554 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
15555 * anything you want as the drag shadow.
15556 * </p>
15557 * <p>
15558 * You pass a DragShadowBuilder object to the system when you start the drag. The system
15559 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
15560 * size and position of the drag shadow. It uses this data to construct a
15561 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
15562 * so that your application can draw the shadow image in the Canvas.
15563 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070015564 *
15565 * <div class="special reference">
15566 * <h3>Developer Guides</h3>
15567 * <p>For a guide to implementing drag and drop features, read the
15568 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
15569 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070015570 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015571 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070015572 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070015573
15574 /**
Joe Malin32736f02011-01-19 16:14:20 -080015575 * Constructs a shadow image builder based on a View. By default, the resulting drag
15576 * shadow will have the same appearance and dimensions as the View, with the touch point
15577 * over the center of the View.
15578 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070015579 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015580 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070015581 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070015582 }
15583
Christopher Tate17ed60c2011-01-18 12:50:26 -080015584 /**
15585 * Construct a shadow builder object with no associated View. This
15586 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
15587 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
15588 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080015589 * reference to any View object. If they are not overridden, then the result is an
15590 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080015591 */
15592 public DragShadowBuilder() {
15593 mView = new WeakReference<View>(null);
15594 }
15595
15596 /**
15597 * Returns the View object that had been passed to the
15598 * {@link #View.DragShadowBuilder(View)}
15599 * constructor. If that View parameter was {@code null} or if the
15600 * {@link #View.DragShadowBuilder()}
15601 * constructor was used to instantiate the builder object, this method will return
15602 * null.
15603 *
15604 * @return The View object associate with this builder object.
15605 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070015606 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070015607 final public View getView() {
15608 return mView.get();
15609 }
15610
Christopher Tate2c095f32010-10-04 14:13:40 -070015611 /**
Joe Malin32736f02011-01-19 16:14:20 -080015612 * Provides the metrics for the shadow image. These include the dimensions of
15613 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070015614 * be centered under the touch location while dragging.
15615 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015616 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080015617 * same as the dimensions of the View itself and centers the shadow under
15618 * the touch point.
15619 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070015620 *
Joe Malin32736f02011-01-19 16:14:20 -080015621 * @param shadowSize A {@link android.graphics.Point} containing the width and height
15622 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
15623 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
15624 * image.
15625 *
15626 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
15627 * shadow image that should be underneath the touch point during the drag and drop
15628 * operation. Your application must set {@link android.graphics.Point#x} to the
15629 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070015630 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015631 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070015632 final View view = mView.get();
15633 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015634 shadowSize.set(view.getWidth(), view.getHeight());
15635 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070015636 } else {
15637 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
15638 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015639 }
15640
15641 /**
Joe Malin32736f02011-01-19 16:14:20 -080015642 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
15643 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015644 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070015645 *
Joe Malin32736f02011-01-19 16:14:20 -080015646 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070015647 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015648 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070015649 final View view = mView.get();
15650 if (view != null) {
15651 view.draw(canvas);
15652 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015653 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070015654 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015655 }
15656 }
15657
15658 /**
Joe Malin32736f02011-01-19 16:14:20 -080015659 * Starts a drag and drop operation. When your application calls this method, it passes a
15660 * {@link android.view.View.DragShadowBuilder} object to the system. The
15661 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
15662 * to get metrics for the drag shadow, and then calls the object's
15663 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
15664 * <p>
15665 * Once the system has the drag shadow, it begins the drag and drop operation by sending
15666 * drag events to all the View objects in your application that are currently visible. It does
15667 * this either by calling the View object's drag listener (an implementation of
15668 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
15669 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
15670 * Both are passed a {@link android.view.DragEvent} object that has a
15671 * {@link android.view.DragEvent#getAction()} value of
15672 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
15673 * </p>
15674 * <p>
15675 * Your application can invoke startDrag() on any attached View object. The View object does not
15676 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
15677 * be related to the View the user selected for dragging.
15678 * </p>
15679 * @param data A {@link android.content.ClipData} object pointing to the data to be
15680 * transferred by the drag and drop operation.
15681 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
15682 * drag shadow.
15683 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
15684 * drop operation. This Object is put into every DragEvent object sent by the system during the
15685 * current drag.
15686 * <p>
15687 * myLocalState is a lightweight mechanism for the sending information from the dragged View
15688 * to the target Views. For example, it can contain flags that differentiate between a
15689 * a copy operation and a move operation.
15690 * </p>
15691 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
15692 * so the parameter should be set to 0.
15693 * @return {@code true} if the method completes successfully, or
15694 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
15695 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070015696 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015697 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015698 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070015699 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015700 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070015701 }
15702 boolean okay = false;
15703
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015704 Point shadowSize = new Point();
15705 Point shadowTouchPoint = new Point();
15706 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070015707
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015708 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
15709 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
15710 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070015711 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015712
Chris Tatea32dcf72010-10-14 12:13:50 -070015713 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015714 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
15715 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070015716 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015717 Surface surface = new Surface();
15718 try {
15719 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015720 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070015721 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070015722 + " surface=" + surface);
15723 if (token != null) {
15724 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070015725 try {
Chris Tate6b391282010-10-14 15:48:59 -070015726 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015727 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070015728 } finally {
15729 surface.unlockCanvasAndPost(canvas);
15730 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015731
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015732 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080015733
15734 // Cache the local state object for delivery with DragEvents
15735 root.setLocalDragState(myLocalState);
15736
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015737 // repurpose 'shadowSize' for the last touch point
15738 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070015739
Christopher Tatea53146c2010-09-07 11:57:52 -070015740 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015741 shadowSize.x, shadowSize.y,
15742 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070015743 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070015744
15745 // Off and running! Release our local surface instance; the drag
15746 // shadow surface is now managed by the system process.
15747 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070015748 }
15749 } catch (Exception e) {
15750 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
15751 surface.destroy();
15752 }
15753
15754 return okay;
15755 }
15756
Christopher Tatea53146c2010-09-07 11:57:52 -070015757 /**
Joe Malin32736f02011-01-19 16:14:20 -080015758 * Handles drag events sent by the system following a call to
15759 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
15760 *<p>
15761 * When the system calls this method, it passes a
15762 * {@link android.view.DragEvent} object. A call to
15763 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
15764 * in DragEvent. The method uses these to determine what is happening in the drag and drop
15765 * operation.
15766 * @param event The {@link android.view.DragEvent} sent by the system.
15767 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
15768 * in DragEvent, indicating the type of drag event represented by this object.
15769 * @return {@code true} if the method was successful, otherwise {@code false}.
15770 * <p>
15771 * The method should return {@code true} in response to an action type of
15772 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
15773 * operation.
15774 * </p>
15775 * <p>
15776 * The method should also return {@code true} in response to an action type of
15777 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
15778 * {@code false} if it didn't.
15779 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015780 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070015781 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070015782 return false;
15783 }
15784
15785 /**
Joe Malin32736f02011-01-19 16:14:20 -080015786 * Detects if this View is enabled and has a drag event listener.
15787 * If both are true, then it calls the drag event listener with the
15788 * {@link android.view.DragEvent} it received. If the drag event listener returns
15789 * {@code true}, then dispatchDragEvent() returns {@code true}.
15790 * <p>
15791 * For all other cases, the method calls the
15792 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
15793 * method and returns its result.
15794 * </p>
15795 * <p>
15796 * This ensures that a drag event is always consumed, even if the View does not have a drag
15797 * event listener. However, if the View has a listener and the listener returns true, then
15798 * onDragEvent() is not called.
15799 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015800 */
15801 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080015802 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015803 ListenerInfo li = mListenerInfo;
15804 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
15805 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070015806 return true;
15807 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015808 return onDragEvent(event);
15809 }
15810
Christopher Tate3d4bf172011-03-28 16:16:46 -070015811 boolean canAcceptDrag() {
15812 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
15813 }
15814
Christopher Tatea53146c2010-09-07 11:57:52 -070015815 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070015816 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
15817 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070015818 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070015819 */
15820 public void onCloseSystemDialogs(String reason) {
15821 }
Joe Malin32736f02011-01-19 16:14:20 -080015822
Dianne Hackbornffa42482009-09-23 22:20:11 -070015823 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015824 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070015825 * update a Region being computed for
15826 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015827 * that any non-transparent parts of the Drawable are removed from the
15828 * given transparent region.
15829 *
15830 * @param dr The Drawable whose transparency is to be applied to the region.
15831 * @param region A Region holding the current transparency information,
15832 * where any parts of the region that are set are considered to be
15833 * transparent. On return, this region will be modified to have the
15834 * transparency information reduced by the corresponding parts of the
15835 * Drawable that are not transparent.
15836 * {@hide}
15837 */
15838 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
15839 if (DBG) {
15840 Log.i("View", "Getting transparent region for: " + this);
15841 }
15842 final Region r = dr.getTransparentRegion();
15843 final Rect db = dr.getBounds();
15844 final AttachInfo attachInfo = mAttachInfo;
15845 if (r != null && attachInfo != null) {
15846 final int w = getRight()-getLeft();
15847 final int h = getBottom()-getTop();
15848 if (db.left > 0) {
15849 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
15850 r.op(0, 0, db.left, h, Region.Op.UNION);
15851 }
15852 if (db.right < w) {
15853 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
15854 r.op(db.right, 0, w, h, Region.Op.UNION);
15855 }
15856 if (db.top > 0) {
15857 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
15858 r.op(0, 0, w, db.top, Region.Op.UNION);
15859 }
15860 if (db.bottom < h) {
15861 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
15862 r.op(0, db.bottom, w, h, Region.Op.UNION);
15863 }
15864 final int[] location = attachInfo.mTransparentLocation;
15865 getLocationInWindow(location);
15866 r.translate(location[0], location[1]);
15867 region.op(r, Region.Op.INTERSECT);
15868 } else {
15869 region.op(db, Region.Op.DIFFERENCE);
15870 }
15871 }
15872
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015873 private void checkForLongClick(int delayOffset) {
15874 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
15875 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015876
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015877 if (mPendingCheckForLongPress == null) {
15878 mPendingCheckForLongPress = new CheckForLongPress();
15879 }
15880 mPendingCheckForLongPress.rememberWindowAttachCount();
15881 postDelayed(mPendingCheckForLongPress,
15882 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015883 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015884 }
15885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015886 /**
15887 * Inflate a view from an XML resource. This convenience method wraps the {@link
15888 * LayoutInflater} class, which provides a full range of options for view inflation.
15889 *
15890 * @param context The Context object for your activity or application.
15891 * @param resource The resource ID to inflate
15892 * @param root A view group that will be the parent. Used to properly inflate the
15893 * layout_* parameters.
15894 * @see LayoutInflater
15895 */
15896 public static View inflate(Context context, int resource, ViewGroup root) {
15897 LayoutInflater factory = LayoutInflater.from(context);
15898 return factory.inflate(resource, root);
15899 }
Romain Guy33e72ae2010-07-17 12:40:29 -070015900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015901 /**
Adam Powell637d3372010-08-25 14:37:03 -070015902 * Scroll the view with standard behavior for scrolling beyond the normal
15903 * content boundaries. Views that call this method should override
15904 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
15905 * results of an over-scroll operation.
15906 *
15907 * Views can use this method to handle any touch or fling-based scrolling.
15908 *
15909 * @param deltaX Change in X in pixels
15910 * @param deltaY Change in Y in pixels
15911 * @param scrollX Current X scroll value in pixels before applying deltaX
15912 * @param scrollY Current Y scroll value in pixels before applying deltaY
15913 * @param scrollRangeX Maximum content scroll range along the X axis
15914 * @param scrollRangeY Maximum content scroll range along the Y axis
15915 * @param maxOverScrollX Number of pixels to overscroll by in either direction
15916 * along the X axis.
15917 * @param maxOverScrollY Number of pixels to overscroll by in either direction
15918 * along the Y axis.
15919 * @param isTouchEvent true if this scroll operation is the result of a touch event.
15920 * @return true if scrolling was clamped to an over-scroll boundary along either
15921 * axis, false otherwise.
15922 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070015923 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070015924 protected boolean overScrollBy(int deltaX, int deltaY,
15925 int scrollX, int scrollY,
15926 int scrollRangeX, int scrollRangeY,
15927 int maxOverScrollX, int maxOverScrollY,
15928 boolean isTouchEvent) {
15929 final int overScrollMode = mOverScrollMode;
15930 final boolean canScrollHorizontal =
15931 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
15932 final boolean canScrollVertical =
15933 computeVerticalScrollRange() > computeVerticalScrollExtent();
15934 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
15935 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
15936 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
15937 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
15938
15939 int newScrollX = scrollX + deltaX;
15940 if (!overScrollHorizontal) {
15941 maxOverScrollX = 0;
15942 }
15943
15944 int newScrollY = scrollY + deltaY;
15945 if (!overScrollVertical) {
15946 maxOverScrollY = 0;
15947 }
15948
15949 // Clamp values if at the limits and record
15950 final int left = -maxOverScrollX;
15951 final int right = maxOverScrollX + scrollRangeX;
15952 final int top = -maxOverScrollY;
15953 final int bottom = maxOverScrollY + scrollRangeY;
15954
15955 boolean clampedX = false;
15956 if (newScrollX > right) {
15957 newScrollX = right;
15958 clampedX = true;
15959 } else if (newScrollX < left) {
15960 newScrollX = left;
15961 clampedX = true;
15962 }
15963
15964 boolean clampedY = false;
15965 if (newScrollY > bottom) {
15966 newScrollY = bottom;
15967 clampedY = true;
15968 } else if (newScrollY < top) {
15969 newScrollY = top;
15970 clampedY = true;
15971 }
15972
15973 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
15974
15975 return clampedX || clampedY;
15976 }
15977
15978 /**
15979 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
15980 * respond to the results of an over-scroll operation.
15981 *
15982 * @param scrollX New X scroll value in pixels
15983 * @param scrollY New Y scroll value in pixels
15984 * @param clampedX True if scrollX was clamped to an over-scroll boundary
15985 * @param clampedY True if scrollY was clamped to an over-scroll boundary
15986 */
15987 protected void onOverScrolled(int scrollX, int scrollY,
15988 boolean clampedX, boolean clampedY) {
15989 // Intentionally empty.
15990 }
15991
15992 /**
15993 * Returns the over-scroll mode for this view. The result will be
15994 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
15995 * (allow over-scrolling only if the view content is larger than the container),
15996 * or {@link #OVER_SCROLL_NEVER}.
15997 *
15998 * @return This view's over-scroll mode.
15999 */
16000 public int getOverScrollMode() {
16001 return mOverScrollMode;
16002 }
16003
16004 /**
16005 * Set the over-scroll mode for this view. Valid over-scroll modes are
16006 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16007 * (allow over-scrolling only if the view content is larger than the container),
16008 * or {@link #OVER_SCROLL_NEVER}.
16009 *
16010 * Setting the over-scroll mode of a view will have an effect only if the
16011 * view is capable of scrolling.
16012 *
16013 * @param overScrollMode The new over-scroll mode for this view.
16014 */
16015 public void setOverScrollMode(int overScrollMode) {
16016 if (overScrollMode != OVER_SCROLL_ALWAYS &&
16017 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
16018 overScrollMode != OVER_SCROLL_NEVER) {
16019 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
16020 }
16021 mOverScrollMode = overScrollMode;
16022 }
16023
16024 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016025 * Gets a scale factor that determines the distance the view should scroll
16026 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
16027 * @return The vertical scroll scale factor.
16028 * @hide
16029 */
16030 protected float getVerticalScrollFactor() {
16031 if (mVerticalScrollFactor == 0) {
16032 TypedValue outValue = new TypedValue();
16033 if (!mContext.getTheme().resolveAttribute(
16034 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
16035 throw new IllegalStateException(
16036 "Expected theme to define listPreferredItemHeight.");
16037 }
16038 mVerticalScrollFactor = outValue.getDimension(
16039 mContext.getResources().getDisplayMetrics());
16040 }
16041 return mVerticalScrollFactor;
16042 }
16043
16044 /**
16045 * Gets a scale factor that determines the distance the view should scroll
16046 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
16047 * @return The horizontal scroll scale factor.
16048 * @hide
16049 */
16050 protected float getHorizontalScrollFactor() {
16051 // TODO: Should use something else.
16052 return getVerticalScrollFactor();
16053 }
16054
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016055 /**
16056 * Return the value specifying the text direction or policy that was set with
16057 * {@link #setTextDirection(int)}.
16058 *
16059 * @return the defined text direction. It can be one of:
16060 *
16061 * {@link #TEXT_DIRECTION_INHERIT},
16062 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16063 * {@link #TEXT_DIRECTION_ANY_RTL},
16064 * {@link #TEXT_DIRECTION_LTR},
16065 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016066 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016067 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016068 @ViewDebug.ExportedProperty(category = "text", mapping = {
16069 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
16070 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
16071 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
16072 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
16073 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
16074 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
16075 })
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016076 public int getTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016077 return (mPrivateFlags2 & TEXT_DIRECTION_MASK) >> TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016078 }
16079
16080 /**
16081 * Set the text direction.
16082 *
16083 * @param textDirection the direction to set. Should be one of:
16084 *
16085 * {@link #TEXT_DIRECTION_INHERIT},
16086 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16087 * {@link #TEXT_DIRECTION_ANY_RTL},
16088 * {@link #TEXT_DIRECTION_LTR},
16089 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016090 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016091 */
16092 public void setTextDirection(int textDirection) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016093 if (getTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016094 // Reset the current text direction and the resolved one
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016095 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016096 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016097 // Set the new text direction
16098 mPrivateFlags2 |= ((textDirection << TEXT_DIRECTION_MASK_SHIFT) & TEXT_DIRECTION_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016099 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016100 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016101 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016102 }
16103 }
16104
16105 /**
16106 * Return the resolved text direction.
16107 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016108 * This needs resolution if the value is TEXT_DIRECTION_INHERIT. The resolution matches
16109 * {@link #getTextDirection()}if it is not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds
16110 * up the parent chain of the view. if there is no parent, then it will return the default
16111 * {@link #TEXT_DIRECTION_FIRST_STRONG}.
16112 *
16113 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016114 *
Doug Feltcb3791202011-07-07 11:57:48 -070016115 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16116 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016117 * {@link #TEXT_DIRECTION_LTR},
16118 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016119 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016120 */
16121 public int getResolvedTextDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070016122 // The text direction will be resolved only if needed
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016123 if ((mPrivateFlags2 & TEXT_DIRECTION_RESOLVED) != TEXT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016124 resolveTextDirection();
16125 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016126 return (mPrivateFlags2 & TEXT_DIRECTION_RESOLVED_MASK) >> TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016127 }
16128
16129 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016130 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
16131 * resolution is done.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016132 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016133 public void resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016134 // Reset any previous text direction resolution
16135 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
16136
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016137 if (hasRtlSupport()) {
16138 // Set resolved text direction flag depending on text direction flag
16139 final int textDirection = getTextDirection();
16140 switch(textDirection) {
16141 case TEXT_DIRECTION_INHERIT:
16142 if (canResolveTextDirection()) {
16143 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016144
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016145 // Set current resolved direction to the same value as the parent's one
16146 final int parentResolvedDirection = viewGroup.getResolvedTextDirection();
16147 switch (parentResolvedDirection) {
16148 case TEXT_DIRECTION_FIRST_STRONG:
16149 case TEXT_DIRECTION_ANY_RTL:
16150 case TEXT_DIRECTION_LTR:
16151 case TEXT_DIRECTION_RTL:
16152 case TEXT_DIRECTION_LOCALE:
16153 mPrivateFlags2 |=
16154 (parentResolvedDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16155 break;
16156 default:
16157 // Default resolved direction is "first strong" heuristic
16158 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
16159 }
16160 } else {
16161 // We cannot do the resolution if there is no parent, so use the default one
16162 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016163 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016164 break;
16165 case TEXT_DIRECTION_FIRST_STRONG:
16166 case TEXT_DIRECTION_ANY_RTL:
16167 case TEXT_DIRECTION_LTR:
16168 case TEXT_DIRECTION_RTL:
16169 case TEXT_DIRECTION_LOCALE:
16170 // Resolved direction is the same as text direction
16171 mPrivateFlags2 |= (textDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16172 break;
16173 default:
16174 // Default resolved direction is "first strong" heuristic
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016175 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016176 }
16177 } else {
16178 // Default resolved direction is "first strong" heuristic
16179 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016180 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016181
16182 // Set to resolved
16183 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016184 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016185 }
16186
16187 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016188 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016189 * resolution should override this method.
16190 *
16191 * The default implementation does nothing.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016192 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016193 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016194 }
16195
16196 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016197 * Check if text direction resolution can be done.
16198 *
16199 * @return true if text direction resolution can be done otherwise return false.
16200 */
16201 public boolean canResolveTextDirection() {
16202 switch (getTextDirection()) {
16203 case TEXT_DIRECTION_INHERIT:
16204 return (mParent != null) && (mParent instanceof ViewGroup);
16205 default:
16206 return true;
16207 }
16208 }
16209
16210 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016211 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016212 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016213 * reset is done.
16214 */
16215 public void resetResolvedTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016216 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016217 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016218 }
16219
16220 /**
16221 * Called when text direction is reset. Subclasses that care about text direction reset should
16222 * override this method and do a reset of the text direction of their children. The default
16223 * implementation does nothing.
16224 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016225 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016226 }
16227
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016228 /**
16229 * Return the value specifying the text alignment or policy that was set with
16230 * {@link #setTextAlignment(int)}.
16231 *
16232 * @return the defined text alignment. It can be one of:
16233 *
16234 * {@link #TEXT_ALIGNMENT_INHERIT},
16235 * {@link #TEXT_ALIGNMENT_GRAVITY},
16236 * {@link #TEXT_ALIGNMENT_CENTER},
16237 * {@link #TEXT_ALIGNMENT_TEXT_START},
16238 * {@link #TEXT_ALIGNMENT_TEXT_END},
16239 * {@link #TEXT_ALIGNMENT_VIEW_START},
16240 * {@link #TEXT_ALIGNMENT_VIEW_END}
16241 */
16242 @ViewDebug.ExportedProperty(category = "text", mapping = {
16243 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16244 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16245 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16246 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16247 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16248 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16249 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16250 })
16251 public int getTextAlignment() {
16252 return (mPrivateFlags2 & TEXT_ALIGNMENT_MASK) >> TEXT_ALIGNMENT_MASK_SHIFT;
16253 }
16254
16255 /**
16256 * Set the text alignment.
16257 *
16258 * @param textAlignment The text alignment to set. Should be one of
16259 *
16260 * {@link #TEXT_ALIGNMENT_INHERIT},
16261 * {@link #TEXT_ALIGNMENT_GRAVITY},
16262 * {@link #TEXT_ALIGNMENT_CENTER},
16263 * {@link #TEXT_ALIGNMENT_TEXT_START},
16264 * {@link #TEXT_ALIGNMENT_TEXT_END},
16265 * {@link #TEXT_ALIGNMENT_VIEW_START},
16266 * {@link #TEXT_ALIGNMENT_VIEW_END}
16267 *
16268 * @attr ref android.R.styleable#View_textAlignment
16269 */
16270 public void setTextAlignment(int textAlignment) {
16271 if (textAlignment != getTextAlignment()) {
16272 // Reset the current and resolved text alignment
16273 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
16274 resetResolvedTextAlignment();
16275 // Set the new text alignment
16276 mPrivateFlags2 |= ((textAlignment << TEXT_ALIGNMENT_MASK_SHIFT) & TEXT_ALIGNMENT_MASK);
16277 // Refresh
16278 requestLayout();
16279 invalidate(true);
16280 }
16281 }
16282
16283 /**
16284 * Return the resolved text alignment.
16285 *
16286 * The resolved text alignment. This needs resolution if the value is
16287 * TEXT_ALIGNMENT_INHERIT. The resolution matches {@link #setTextAlignment(int)} if it is
16288 * not TEXT_ALIGNMENT_INHERIT, otherwise resolution proceeds up the parent chain of the view.
16289 *
16290 * @return the resolved text alignment. Returns one of:
16291 *
16292 * {@link #TEXT_ALIGNMENT_GRAVITY},
16293 * {@link #TEXT_ALIGNMENT_CENTER},
16294 * {@link #TEXT_ALIGNMENT_TEXT_START},
16295 * {@link #TEXT_ALIGNMENT_TEXT_END},
16296 * {@link #TEXT_ALIGNMENT_VIEW_START},
16297 * {@link #TEXT_ALIGNMENT_VIEW_END}
16298 */
16299 @ViewDebug.ExportedProperty(category = "text", mapping = {
16300 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16301 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16302 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16303 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16304 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16305 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16306 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16307 })
16308 public int getResolvedTextAlignment() {
16309 // If text alignment is not resolved, then resolve it
16310 if ((mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED) != TEXT_ALIGNMENT_RESOLVED) {
16311 resolveTextAlignment();
16312 }
16313 return (mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED_MASK) >> TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
16314 }
16315
16316 /**
16317 * Resolve the text alignment. Will call {@link View#onResolvedTextAlignmentChanged} when
16318 * resolution is done.
16319 */
16320 public void resolveTextAlignment() {
16321 // Reset any previous text alignment resolution
16322 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16323
16324 if (hasRtlSupport()) {
16325 // Set resolved text alignment flag depending on text alignment flag
16326 final int textAlignment = getTextAlignment();
16327 switch (textAlignment) {
16328 case TEXT_ALIGNMENT_INHERIT:
16329 // Check if we can resolve the text alignment
16330 if (canResolveLayoutDirection() && mParent instanceof View) {
16331 View view = (View) mParent;
16332
16333 final int parentResolvedTextAlignment = view.getResolvedTextAlignment();
16334 switch (parentResolvedTextAlignment) {
16335 case TEXT_ALIGNMENT_GRAVITY:
16336 case TEXT_ALIGNMENT_TEXT_START:
16337 case TEXT_ALIGNMENT_TEXT_END:
16338 case TEXT_ALIGNMENT_CENTER:
16339 case TEXT_ALIGNMENT_VIEW_START:
16340 case TEXT_ALIGNMENT_VIEW_END:
16341 // Resolved text alignment is the same as the parent resolved
16342 // text alignment
16343 mPrivateFlags2 |=
16344 (parentResolvedTextAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16345 break;
16346 default:
16347 // Use default resolved text alignment
16348 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16349 }
16350 }
16351 else {
16352 // We cannot do the resolution if there is no parent so use the default
16353 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16354 }
16355 break;
16356 case TEXT_ALIGNMENT_GRAVITY:
16357 case TEXT_ALIGNMENT_TEXT_START:
16358 case TEXT_ALIGNMENT_TEXT_END:
16359 case TEXT_ALIGNMENT_CENTER:
16360 case TEXT_ALIGNMENT_VIEW_START:
16361 case TEXT_ALIGNMENT_VIEW_END:
16362 // Resolved text alignment is the same as text alignment
16363 mPrivateFlags2 |= (textAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16364 break;
16365 default:
16366 // Use default resolved text alignment
16367 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16368 }
16369 } else {
16370 // Use default resolved text alignment
16371 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16372 }
16373
16374 // Set the resolved
16375 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED;
16376 onResolvedTextAlignmentChanged();
16377 }
16378
16379 /**
16380 * Check if text alignment resolution can be done.
16381 *
16382 * @return true if text alignment resolution can be done otherwise return false.
16383 */
16384 public boolean canResolveTextAlignment() {
16385 switch (getTextAlignment()) {
16386 case TEXT_DIRECTION_INHERIT:
16387 return (mParent != null);
16388 default:
16389 return true;
16390 }
16391 }
16392
16393 /**
16394 * Called when text alignment has been resolved. Subclasses that care about text alignment
16395 * resolution should override this method.
16396 *
16397 * The default implementation does nothing.
16398 */
16399 public void onResolvedTextAlignmentChanged() {
16400 }
16401
16402 /**
16403 * Reset resolved text alignment. Text alignment can be resolved with a call to
16404 * getResolvedTextAlignment(). Will call {@link View#onResolvedTextAlignmentReset} when
16405 * reset is done.
16406 */
16407 public void resetResolvedTextAlignment() {
16408 // Reset any previous text alignment resolution
16409 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16410 onResolvedTextAlignmentReset();
16411 }
16412
16413 /**
16414 * Called when text alignment is reset. Subclasses that care about text alignment reset should
16415 * override this method and do a reset of the text alignment of their children. The default
16416 * implementation does nothing.
16417 */
16418 public void onResolvedTextAlignmentReset() {
16419 }
16420
Adam Powella9108a22012-07-18 11:18:09 -070016421 /**
16422 * Generate a value suitable for use in {@link #setId(int)}.
16423 * This value will not collide with ID values generated at build time by aapt for R.id.
16424 *
16425 * @return a generated ID value
16426 */
16427 public static int generateViewId() {
16428 for (;;) {
16429 final int result = sNextGeneratedId.get();
16430 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
16431 int newValue = result + 1;
16432 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
16433 if (sNextGeneratedId.compareAndSet(result, newValue)) {
16434 return result;
16435 }
16436 }
16437 }
16438
Chet Haaseb39f0512011-05-24 14:36:40 -070016439 //
16440 // Properties
16441 //
16442 /**
16443 * A Property wrapper around the <code>alpha</code> functionality handled by the
16444 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
16445 */
Chet Haased47f1532011-12-16 11:18:52 -080016446 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016447 @Override
16448 public void setValue(View object, float value) {
16449 object.setAlpha(value);
16450 }
16451
16452 @Override
16453 public Float get(View object) {
16454 return object.getAlpha();
16455 }
16456 };
16457
16458 /**
16459 * A Property wrapper around the <code>translationX</code> functionality handled by the
16460 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
16461 */
Chet Haased47f1532011-12-16 11:18:52 -080016462 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016463 @Override
16464 public void setValue(View object, float value) {
16465 object.setTranslationX(value);
16466 }
16467
16468 @Override
16469 public Float get(View object) {
16470 return object.getTranslationX();
16471 }
16472 };
16473
16474 /**
16475 * A Property wrapper around the <code>translationY</code> functionality handled by the
16476 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
16477 */
Chet Haased47f1532011-12-16 11:18:52 -080016478 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016479 @Override
16480 public void setValue(View object, float value) {
16481 object.setTranslationY(value);
16482 }
16483
16484 @Override
16485 public Float get(View object) {
16486 return object.getTranslationY();
16487 }
16488 };
16489
16490 /**
16491 * A Property wrapper around the <code>x</code> functionality handled by the
16492 * {@link View#setX(float)} and {@link View#getX()} methods.
16493 */
Chet Haased47f1532011-12-16 11:18:52 -080016494 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016495 @Override
16496 public void setValue(View object, float value) {
16497 object.setX(value);
16498 }
16499
16500 @Override
16501 public Float get(View object) {
16502 return object.getX();
16503 }
16504 };
16505
16506 /**
16507 * A Property wrapper around the <code>y</code> functionality handled by the
16508 * {@link View#setY(float)} and {@link View#getY()} methods.
16509 */
Chet Haased47f1532011-12-16 11:18:52 -080016510 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016511 @Override
16512 public void setValue(View object, float value) {
16513 object.setY(value);
16514 }
16515
16516 @Override
16517 public Float get(View object) {
16518 return object.getY();
16519 }
16520 };
16521
16522 /**
16523 * A Property wrapper around the <code>rotation</code> functionality handled by the
16524 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
16525 */
Chet Haased47f1532011-12-16 11:18:52 -080016526 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016527 @Override
16528 public void setValue(View object, float value) {
16529 object.setRotation(value);
16530 }
16531
16532 @Override
16533 public Float get(View object) {
16534 return object.getRotation();
16535 }
16536 };
16537
16538 /**
16539 * A Property wrapper around the <code>rotationX</code> functionality handled by the
16540 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
16541 */
Chet Haased47f1532011-12-16 11:18:52 -080016542 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016543 @Override
16544 public void setValue(View object, float value) {
16545 object.setRotationX(value);
16546 }
16547
16548 @Override
16549 public Float get(View object) {
16550 return object.getRotationX();
16551 }
16552 };
16553
16554 /**
16555 * A Property wrapper around the <code>rotationY</code> functionality handled by the
16556 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
16557 */
Chet Haased47f1532011-12-16 11:18:52 -080016558 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016559 @Override
16560 public void setValue(View object, float value) {
16561 object.setRotationY(value);
16562 }
16563
16564 @Override
16565 public Float get(View object) {
16566 return object.getRotationY();
16567 }
16568 };
16569
16570 /**
16571 * A Property wrapper around the <code>scaleX</code> functionality handled by the
16572 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
16573 */
Chet Haased47f1532011-12-16 11:18:52 -080016574 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016575 @Override
16576 public void setValue(View object, float value) {
16577 object.setScaleX(value);
16578 }
16579
16580 @Override
16581 public Float get(View object) {
16582 return object.getScaleX();
16583 }
16584 };
16585
16586 /**
16587 * A Property wrapper around the <code>scaleY</code> functionality handled by the
16588 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
16589 */
Chet Haased47f1532011-12-16 11:18:52 -080016590 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016591 @Override
16592 public void setValue(View object, float value) {
16593 object.setScaleY(value);
16594 }
16595
16596 @Override
16597 public Float get(View object) {
16598 return object.getScaleY();
16599 }
16600 };
16601
Jeff Brown33bbfd22011-02-24 20:55:35 -080016602 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016603 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
16604 * Each MeasureSpec represents a requirement for either the width or the height.
16605 * A MeasureSpec is comprised of a size and a mode. There are three possible
16606 * modes:
16607 * <dl>
16608 * <dt>UNSPECIFIED</dt>
16609 * <dd>
16610 * The parent has not imposed any constraint on the child. It can be whatever size
16611 * it wants.
16612 * </dd>
16613 *
16614 * <dt>EXACTLY</dt>
16615 * <dd>
16616 * The parent has determined an exact size for the child. The child is going to be
16617 * given those bounds regardless of how big it wants to be.
16618 * </dd>
16619 *
16620 * <dt>AT_MOST</dt>
16621 * <dd>
16622 * The child can be as large as it wants up to the specified size.
16623 * </dd>
16624 * </dl>
16625 *
16626 * MeasureSpecs are implemented as ints to reduce object allocation. This class
16627 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
16628 */
16629 public static class MeasureSpec {
16630 private static final int MODE_SHIFT = 30;
16631 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
16632
16633 /**
16634 * Measure specification mode: The parent has not imposed any constraint
16635 * on the child. It can be whatever size it wants.
16636 */
16637 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
16638
16639 /**
16640 * Measure specification mode: The parent has determined an exact size
16641 * for the child. The child is going to be given those bounds regardless
16642 * of how big it wants to be.
16643 */
16644 public static final int EXACTLY = 1 << MODE_SHIFT;
16645
16646 /**
16647 * Measure specification mode: The child can be as large as it wants up
16648 * to the specified size.
16649 */
16650 public static final int AT_MOST = 2 << MODE_SHIFT;
16651
16652 /**
16653 * Creates a measure specification based on the supplied size and mode.
16654 *
16655 * The mode must always be one of the following:
16656 * <ul>
16657 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
16658 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
16659 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
16660 * </ul>
16661 *
16662 * @param size the size of the measure specification
16663 * @param mode the mode of the measure specification
16664 * @return the measure specification based on size and mode
16665 */
16666 public static int makeMeasureSpec(int size, int mode) {
16667 return size + mode;
16668 }
16669
16670 /**
16671 * Extracts the mode from the supplied measure specification.
16672 *
16673 * @param measureSpec the measure specification to extract the mode from
16674 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
16675 * {@link android.view.View.MeasureSpec#AT_MOST} or
16676 * {@link android.view.View.MeasureSpec#EXACTLY}
16677 */
16678 public static int getMode(int measureSpec) {
16679 return (measureSpec & MODE_MASK);
16680 }
16681
16682 /**
16683 * Extracts the size from the supplied measure specification.
16684 *
16685 * @param measureSpec the measure specification to extract the size from
16686 * @return the size in pixels defined in the supplied measure specification
16687 */
16688 public static int getSize(int measureSpec) {
16689 return (measureSpec & ~MODE_MASK);
16690 }
16691
16692 /**
16693 * Returns a String representation of the specified measure
16694 * specification.
16695 *
16696 * @param measureSpec the measure specification to convert to a String
16697 * @return a String with the following format: "MeasureSpec: MODE SIZE"
16698 */
16699 public static String toString(int measureSpec) {
16700 int mode = getMode(measureSpec);
16701 int size = getSize(measureSpec);
16702
16703 StringBuilder sb = new StringBuilder("MeasureSpec: ");
16704
16705 if (mode == UNSPECIFIED)
16706 sb.append("UNSPECIFIED ");
16707 else if (mode == EXACTLY)
16708 sb.append("EXACTLY ");
16709 else if (mode == AT_MOST)
16710 sb.append("AT_MOST ");
16711 else
16712 sb.append(mode).append(" ");
16713
16714 sb.append(size);
16715 return sb.toString();
16716 }
16717 }
16718
16719 class CheckForLongPress implements Runnable {
16720
16721 private int mOriginalWindowAttachCount;
16722
16723 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070016724 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016725 && mOriginalWindowAttachCount == mWindowAttachCount) {
16726 if (performLongClick()) {
16727 mHasPerformedLongPress = true;
16728 }
16729 }
16730 }
16731
16732 public void rememberWindowAttachCount() {
16733 mOriginalWindowAttachCount = mWindowAttachCount;
16734 }
16735 }
Joe Malin32736f02011-01-19 16:14:20 -080016736
Adam Powelle14579b2009-12-16 18:39:52 -080016737 private final class CheckForTap implements Runnable {
16738 public void run() {
16739 mPrivateFlags &= ~PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080016740 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016741 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080016742 }
16743 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016744
Adam Powella35d7682010-03-12 14:48:13 -080016745 private final class PerformClick implements Runnable {
16746 public void run() {
16747 performClick();
16748 }
16749 }
16750
Dianne Hackborn63042d62011-01-26 18:56:29 -080016751 /** @hide */
16752 public void hackTurnOffWindowResizeAnim(boolean off) {
16753 mAttachInfo.mTurnOffWindowResizeAnim = off;
16754 }
Joe Malin32736f02011-01-19 16:14:20 -080016755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016756 /**
Chet Haasea00f3862011-02-22 06:34:40 -080016757 * This method returns a ViewPropertyAnimator object, which can be used to animate
16758 * specific properties on this View.
16759 *
16760 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
16761 */
16762 public ViewPropertyAnimator animate() {
16763 if (mAnimator == null) {
16764 mAnimator = new ViewPropertyAnimator(this);
16765 }
16766 return mAnimator;
16767 }
16768
16769 /**
Jean Chalard405bc512012-05-29 19:12:34 +090016770 * Interface definition for a callback to be invoked when a hardware key event is
16771 * dispatched to this view. The callback will be invoked before the key event is
16772 * given to the view. This is only useful for hardware keyboards; a software input
16773 * method has no obligation to trigger this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016774 */
16775 public interface OnKeyListener {
16776 /**
Jean Chalard405bc512012-05-29 19:12:34 +090016777 * Called when a hardware key is dispatched to a view. This allows listeners to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016778 * get a chance to respond before the target view.
Jean Chalard405bc512012-05-29 19:12:34 +090016779 * <p>Key presses in software keyboards will generally NOT trigger this method,
16780 * although some may elect to do so in some situations. Do not assume a
16781 * software input method has to be key-based; even if it is, it may use key presses
16782 * in a different way than you expect, so there is no way to reliably catch soft
16783 * input key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016784 *
16785 * @param v The view the key has been dispatched to.
16786 * @param keyCode The code for the physical key that was pressed
16787 * @param event The KeyEvent object containing full information about
16788 * the event.
16789 * @return True if the listener has consumed the event, false otherwise.
16790 */
16791 boolean onKey(View v, int keyCode, KeyEvent event);
16792 }
16793
16794 /**
16795 * Interface definition for a callback to be invoked when a touch event is
16796 * dispatched to this view. The callback will be invoked before the touch
16797 * event is given to the view.
16798 */
16799 public interface OnTouchListener {
16800 /**
16801 * Called when a touch event is dispatched to a view. This allows listeners to
16802 * get a chance to respond before the target view.
16803 *
16804 * @param v The view the touch event has been dispatched to.
16805 * @param event The MotionEvent object containing full information about
16806 * the event.
16807 * @return True if the listener has consumed the event, false otherwise.
16808 */
16809 boolean onTouch(View v, MotionEvent event);
16810 }
16811
16812 /**
Jeff Brown10b62902011-06-20 16:40:37 -070016813 * Interface definition for a callback to be invoked when a hover event is
16814 * dispatched to this view. The callback will be invoked before the hover
16815 * event is given to the view.
16816 */
16817 public interface OnHoverListener {
16818 /**
16819 * Called when a hover event is dispatched to a view. This allows listeners to
16820 * get a chance to respond before the target view.
16821 *
16822 * @param v The view the hover event has been dispatched to.
16823 * @param event The MotionEvent object containing full information about
16824 * the event.
16825 * @return True if the listener has consumed the event, false otherwise.
16826 */
16827 boolean onHover(View v, MotionEvent event);
16828 }
16829
16830 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016831 * Interface definition for a callback to be invoked when a generic motion event is
16832 * dispatched to this view. The callback will be invoked before the generic motion
16833 * event is given to the view.
16834 */
16835 public interface OnGenericMotionListener {
16836 /**
16837 * Called when a generic motion event is dispatched to a view. This allows listeners to
16838 * get a chance to respond before the target view.
16839 *
16840 * @param v The view the generic motion event has been dispatched to.
16841 * @param event The MotionEvent object containing full information about
16842 * the event.
16843 * @return True if the listener has consumed the event, false otherwise.
16844 */
16845 boolean onGenericMotion(View v, MotionEvent event);
16846 }
16847
16848 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016849 * Interface definition for a callback to be invoked when a view has been clicked and held.
16850 */
16851 public interface OnLongClickListener {
16852 /**
16853 * Called when a view has been clicked and held.
16854 *
16855 * @param v The view that was clicked and held.
16856 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080016857 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016858 */
16859 boolean onLongClick(View v);
16860 }
16861
16862 /**
Chris Tate32affef2010-10-18 15:29:21 -070016863 * Interface definition for a callback to be invoked when a drag is being dispatched
16864 * to this view. The callback will be invoked before the hosting view's own
16865 * onDrag(event) method. If the listener wants to fall back to the hosting view's
16866 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070016867 *
16868 * <div class="special reference">
16869 * <h3>Developer Guides</h3>
16870 * <p>For a guide to implementing drag and drop features, read the
16871 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
16872 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070016873 */
16874 public interface OnDragListener {
16875 /**
16876 * Called when a drag event is dispatched to a view. This allows listeners
16877 * to get a chance to override base View behavior.
16878 *
Joe Malin32736f02011-01-19 16:14:20 -080016879 * @param v The View that received the drag event.
16880 * @param event The {@link android.view.DragEvent} object for the drag event.
16881 * @return {@code true} if the drag event was handled successfully, or {@code false}
16882 * if the drag event was not handled. Note that {@code false} will trigger the View
16883 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070016884 */
16885 boolean onDrag(View v, DragEvent event);
16886 }
16887
16888 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016889 * Interface definition for a callback to be invoked when the focus state of
16890 * a view changed.
16891 */
16892 public interface OnFocusChangeListener {
16893 /**
16894 * Called when the focus state of a view has changed.
16895 *
16896 * @param v The view whose state has changed.
16897 * @param hasFocus The new focus state of v.
16898 */
16899 void onFocusChange(View v, boolean hasFocus);
16900 }
16901
16902 /**
16903 * Interface definition for a callback to be invoked when a view is clicked.
16904 */
16905 public interface OnClickListener {
16906 /**
16907 * Called when a view has been clicked.
16908 *
16909 * @param v The view that was clicked.
16910 */
16911 void onClick(View v);
16912 }
16913
16914 /**
16915 * Interface definition for a callback to be invoked when the context menu
16916 * for this view is being built.
16917 */
16918 public interface OnCreateContextMenuListener {
16919 /**
16920 * Called when the context menu for this view is being built. It is not
16921 * safe to hold onto the menu after this method returns.
16922 *
16923 * @param menu The context menu that is being built
16924 * @param v The view for which the context menu is being built
16925 * @param menuInfo Extra information about the item for which the
16926 * context menu should be shown. This information will vary
16927 * depending on the class of v.
16928 */
16929 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
16930 }
16931
Joe Onorato664644d2011-01-23 17:53:23 -080016932 /**
16933 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016934 * visibility. This reports <strong>global</strong> changes to the system UI
Dianne Hackborncf675782012-05-10 15:07:24 -070016935 * state, not what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080016936 *
Philip Milne6c8ea062012-04-03 17:38:43 -070016937 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080016938 */
16939 public interface OnSystemUiVisibilityChangeListener {
16940 /**
16941 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070016942 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080016943 *
Dianne Hackborncf675782012-05-10 15:07:24 -070016944 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
16945 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, and {@link #SYSTEM_UI_FLAG_FULLSCREEN}.
16946 * This tells you the <strong>global</strong> state of these UI visibility
16947 * flags, not what your app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080016948 */
16949 public void onSystemUiVisibilityChange(int visibility);
16950 }
16951
Adam Powell4afd62b2011-02-18 15:02:18 -080016952 /**
16953 * Interface definition for a callback to be invoked when this view is attached
16954 * or detached from its window.
16955 */
16956 public interface OnAttachStateChangeListener {
16957 /**
16958 * Called when the view is attached to a window.
16959 * @param v The view that was attached
16960 */
16961 public void onViewAttachedToWindow(View v);
16962 /**
16963 * Called when the view is detached from a window.
16964 * @param v The view that was detached
16965 */
16966 public void onViewDetachedFromWindow(View v);
16967 }
16968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016969 private final class UnsetPressedState implements Runnable {
16970 public void run() {
16971 setPressed(false);
16972 }
16973 }
16974
16975 /**
16976 * Base class for derived classes that want to save and restore their own
16977 * state in {@link android.view.View#onSaveInstanceState()}.
16978 */
16979 public static class BaseSavedState extends AbsSavedState {
16980 /**
16981 * Constructor used when reading from a parcel. Reads the state of the superclass.
16982 *
16983 * @param source
16984 */
16985 public BaseSavedState(Parcel source) {
16986 super(source);
16987 }
16988
16989 /**
16990 * Constructor called by derived classes when creating their SavedState objects
16991 *
16992 * @param superState The state of the superclass of this view
16993 */
16994 public BaseSavedState(Parcelable superState) {
16995 super(superState);
16996 }
16997
16998 public static final Parcelable.Creator<BaseSavedState> CREATOR =
16999 new Parcelable.Creator<BaseSavedState>() {
17000 public BaseSavedState createFromParcel(Parcel in) {
17001 return new BaseSavedState(in);
17002 }
17003
17004 public BaseSavedState[] newArray(int size) {
17005 return new BaseSavedState[size];
17006 }
17007 };
17008 }
17009
17010 /**
17011 * A set of information given to a view when it is attached to its parent
17012 * window.
17013 */
17014 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017015 interface Callbacks {
17016 void playSoundEffect(int effectId);
17017 boolean performHapticFeedback(int effectId, boolean always);
17018 }
17019
17020 /**
17021 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
17022 * to a Handler. This class contains the target (View) to invalidate and
17023 * the coordinates of the dirty rectangle.
17024 *
17025 * For performance purposes, this class also implements a pool of up to
17026 * POOL_LIMIT objects that get reused. This reduces memory allocations
17027 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017028 */
Romain Guyd928d682009-03-31 17:52:16 -070017029 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017030 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070017031 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
17032 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070017033 public InvalidateInfo newInstance() {
17034 return new InvalidateInfo();
17035 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017036
Romain Guyd928d682009-03-31 17:52:16 -070017037 public void onAcquired(InvalidateInfo element) {
17038 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017039
Romain Guyd928d682009-03-31 17:52:16 -070017040 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070017041 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070017042 }
17043 }, POOL_LIMIT)
17044 );
17045
17046 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017047 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017048
17049 View target;
17050
17051 int left;
17052 int top;
17053 int right;
17054 int bottom;
17055
Romain Guyd928d682009-03-31 17:52:16 -070017056 public void setNextPoolable(InvalidateInfo element) {
17057 mNext = element;
17058 }
17059
17060 public InvalidateInfo getNextPoolable() {
17061 return mNext;
17062 }
17063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017064 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070017065 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017066 }
17067
17068 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070017069 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017070 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017071
17072 public boolean isPooled() {
17073 return mIsPooled;
17074 }
17075
17076 public void setPooled(boolean isPooled) {
17077 mIsPooled = isPooled;
17078 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017079 }
17080
17081 final IWindowSession mSession;
17082
17083 final IWindow mWindow;
17084
17085 final IBinder mWindowToken;
17086
17087 final Callbacks mRootCallbacks;
17088
Romain Guy59a12ca2011-06-09 17:48:21 -070017089 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080017090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017091 /**
17092 * The top view of the hierarchy.
17093 */
17094 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070017095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017096 IBinder mPanelParentWindowToken;
17097 Surface mSurface;
17098
Romain Guyb051e892010-09-28 19:09:36 -070017099 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080017100 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070017101 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080017102
Romain Guy7e4e5612012-03-05 14:37:29 -080017103 boolean mScreenOn;
17104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017105 /**
Romain Guy8506ab42009-06-11 17:35:47 -070017106 * Scale factor used by the compatibility mode
17107 */
17108 float mApplicationScale;
17109
17110 /**
17111 * Indicates whether the application is in compatibility mode
17112 */
17113 boolean mScalingRequired;
17114
17115 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017116 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080017117 */
17118 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080017119
Dianne Hackborn63042d62011-01-26 18:56:29 -080017120 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017121 * Left position of this view's window
17122 */
17123 int mWindowLeft;
17124
17125 /**
17126 * Top position of this view's window
17127 */
17128 int mWindowTop;
17129
17130 /**
Adam Powell26153a32010-11-08 15:22:27 -080017131 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070017132 */
Adam Powell26153a32010-11-08 15:22:27 -080017133 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070017134
17135 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017136 * For windows that are full-screen but using insets to layout inside
17137 * of the screen decorations, these are the current insets for the
17138 * content of the window.
17139 */
17140 final Rect mContentInsets = new Rect();
17141
17142 /**
17143 * For windows that are full-screen but using insets to layout inside
17144 * of the screen decorations, these are the current insets for the
17145 * actual visible parts of the window.
17146 */
17147 final Rect mVisibleInsets = new Rect();
17148
17149 /**
17150 * The internal insets given by this window. This value is
17151 * supplied by the client (through
17152 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
17153 * be given to the window manager when changed to be used in laying
17154 * out windows behind it.
17155 */
17156 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
17157 = new ViewTreeObserver.InternalInsetsInfo();
17158
17159 /**
17160 * All views in the window's hierarchy that serve as scroll containers,
17161 * used to determine if the window can be resized or must be panned
17162 * to adjust for a soft input area.
17163 */
17164 final ArrayList<View> mScrollContainers = new ArrayList<View>();
17165
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070017166 final KeyEvent.DispatcherState mKeyDispatchState
17167 = new KeyEvent.DispatcherState();
17168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017169 /**
17170 * Indicates whether the view's window currently has the focus.
17171 */
17172 boolean mHasWindowFocus;
17173
17174 /**
17175 * The current visibility of the window.
17176 */
17177 int mWindowVisibility;
17178
17179 /**
17180 * Indicates the time at which drawing started to occur.
17181 */
17182 long mDrawingTime;
17183
17184 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070017185 * Indicates whether or not ignoring the DIRTY_MASK flags.
17186 */
17187 boolean mIgnoreDirtyState;
17188
17189 /**
Romain Guy02ccac62011-06-24 13:20:23 -070017190 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
17191 * to avoid clearing that flag prematurely.
17192 */
17193 boolean mSetIgnoreDirtyState = false;
17194
17195 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017196 * Indicates whether the view's window is currently in touch mode.
17197 */
17198 boolean mInTouchMode;
17199
17200 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017201 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017202 * the next time it performs a traversal
17203 */
17204 boolean mRecomputeGlobalAttributes;
17205
17206 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017207 * Always report new attributes at next traversal.
17208 */
17209 boolean mForceReportNewAttributes;
17210
17211 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017212 * Set during a traveral if any views want to keep the screen on.
17213 */
17214 boolean mKeepScreenOn;
17215
17216 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017217 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
17218 */
17219 int mSystemUiVisibility;
17220
17221 /**
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070017222 * Hack to force certain system UI visibility flags to be cleared.
17223 */
17224 int mDisabledSystemUiVisibility;
17225
17226 /**
Dianne Hackborncf675782012-05-10 15:07:24 -070017227 * Last global system UI visibility reported by the window manager.
17228 */
17229 int mGlobalSystemUiVisibility;
17230
17231 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017232 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
17233 * attached.
17234 */
17235 boolean mHasSystemUiListeners;
17236
17237 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017238 * Set if the visibility of any views has changed.
17239 */
17240 boolean mViewVisibilityChanged;
17241
17242 /**
17243 * Set to true if a view has been scrolled.
17244 */
17245 boolean mViewScrollChanged;
17246
17247 /**
17248 * Global to the view hierarchy used as a temporary for dealing with
17249 * x/y points in the transparent region computations.
17250 */
17251 final int[] mTransparentLocation = new int[2];
17252
17253 /**
17254 * Global to the view hierarchy used as a temporary for dealing with
17255 * x/y points in the ViewGroup.invalidateChild implementation.
17256 */
17257 final int[] mInvalidateChildLocation = new int[2];
17258
Chet Haasec3aa3612010-06-17 08:50:37 -070017259
17260 /**
17261 * Global to the view hierarchy used as a temporary for dealing with
17262 * x/y location when view is transformed.
17263 */
17264 final float[] mTmpTransformLocation = new float[2];
17265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017266 /**
17267 * The view tree observer used to dispatch global events like
17268 * layout, pre-draw, touch mode change, etc.
17269 */
17270 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
17271
17272 /**
17273 * A Canvas used by the view hierarchy to perform bitmap caching.
17274 */
17275 Canvas mCanvas;
17276
17277 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080017278 * The view root impl.
17279 */
17280 final ViewRootImpl mViewRootImpl;
17281
17282 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070017283 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017284 * handler can be used to pump events in the UI events queue.
17285 */
17286 final Handler mHandler;
17287
17288 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017289 * Temporary for use in computing invalidate rectangles while
17290 * calling up the hierarchy.
17291 */
17292 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070017293
17294 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070017295 * Temporary for use in computing hit areas with transformed views
17296 */
17297 final RectF mTmpTransformRect = new RectF();
17298
17299 /**
Chet Haase599913d2012-07-23 16:22:05 -070017300 * Temporary for use in transforming invalidation rect
17301 */
17302 final Matrix mTmpMatrix = new Matrix();
17303
17304 /**
17305 * Temporary for use in transforming invalidation rect
17306 */
17307 final Transformation mTmpTransformation = new Transformation();
17308
17309 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070017310 * Temporary list for use in collecting focusable descendents of a view.
17311 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070017312 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070017313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017314 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017315 * The id of the window for accessibility purposes.
17316 */
17317 int mAccessibilityWindowId = View.NO_ID;
17318
17319 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -070017320 * Whether to ingore not exposed for accessibility Views when
17321 * reporting the view tree to accessibility services.
17322 */
17323 boolean mIncludeNotImportantViews;
17324
17325 /**
17326 * The drawable for highlighting accessibility focus.
17327 */
17328 Drawable mAccessibilityFocusDrawable;
17329
17330 /**
Philip Milne10ca24a2012-04-23 15:38:27 -070017331 * Show where the margins, bounds and layout bounds are for each view.
17332 */
Dianne Hackborna53de062012-05-08 18:53:51 -070017333 boolean mDebugLayout = SystemProperties.getBoolean(DEBUG_LAYOUT_PROPERTY, false);
Philip Milne10ca24a2012-04-23 15:38:27 -070017334
17335 /**
Romain Guyab4c4f4f2012-05-06 13:11:24 -070017336 * Point used to compute visible regions.
17337 */
17338 final Point mPoint = new Point();
17339
17340 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017341 * Creates a new set of attachment information with the specified
17342 * events handler and thread.
17343 *
17344 * @param handler the events handler the view must use
17345 */
17346 AttachInfo(IWindowSession session, IWindow window,
Jeff Browna175a5b2012-02-15 19:18:31 -080017347 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017348 mSession = session;
17349 mWindow = window;
17350 mWindowToken = window.asBinder();
Jeff Browna175a5b2012-02-15 19:18:31 -080017351 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017352 mHandler = handler;
17353 mRootCallbacks = effectPlayer;
17354 }
17355 }
17356
17357 /**
17358 * <p>ScrollabilityCache holds various fields used by a View when scrolling
17359 * is supported. This avoids keeping too many unused fields in most
17360 * instances of View.</p>
17361 */
Mike Cleronf116bf82009-09-27 19:14:12 -070017362 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080017363
Mike Cleronf116bf82009-09-27 19:14:12 -070017364 /**
17365 * Scrollbars are not visible
17366 */
17367 public static final int OFF = 0;
17368
17369 /**
17370 * Scrollbars are visible
17371 */
17372 public static final int ON = 1;
17373
17374 /**
17375 * Scrollbars are fading away
17376 */
17377 public static final int FADING = 2;
17378
17379 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080017380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017381 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070017382 public int scrollBarDefaultDelayBeforeFade;
17383 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017384
17385 public int scrollBarSize;
17386 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070017387 public float[] interpolatorValues;
17388 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017389
17390 public final Paint paint;
17391 public final Matrix matrix;
17392 public Shader shader;
17393
Mike Cleronf116bf82009-09-27 19:14:12 -070017394 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
17395
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017396 private static final float[] OPAQUE = { 255 };
17397 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080017398
Mike Cleronf116bf82009-09-27 19:14:12 -070017399 /**
17400 * When fading should start. This time moves into the future every time
17401 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
17402 */
17403 public long fadeStartTime;
17404
17405
17406 /**
17407 * The current state of the scrollbars: ON, OFF, or FADING
17408 */
17409 public int state = OFF;
17410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017411 private int mLastColor;
17412
Mike Cleronf116bf82009-09-27 19:14:12 -070017413 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017414 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
17415 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070017416 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
17417 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017418
17419 paint = new Paint();
17420 matrix = new Matrix();
17421 // use use a height of 1, and then wack the matrix each time we
17422 // actually use it.
17423 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017425 paint.setShader(shader);
17426 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070017427 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017428 }
Romain Guy8506ab42009-06-11 17:35:47 -070017429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017430 public void setFadeColor(int color) {
17431 if (color != 0 && color != mLastColor) {
17432 mLastColor = color;
17433 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070017434
Romain Guye55e1a72009-08-27 10:42:26 -070017435 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
17436 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017438 paint.setShader(shader);
17439 // Restore the default transfer mode (src_over)
17440 paint.setXfermode(null);
17441 }
17442 }
Joe Malin32736f02011-01-19 16:14:20 -080017443
Mike Cleronf116bf82009-09-27 19:14:12 -070017444 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070017445 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070017446 if (now >= fadeStartTime) {
17447
17448 // the animation fades the scrollbars out by changing
17449 // the opacity (alpha) from fully opaque to fully
17450 // transparent
17451 int nextFrame = (int) now;
17452 int framesCount = 0;
17453
17454 Interpolator interpolator = scrollBarInterpolator;
17455
17456 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017457 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070017458
17459 // End transparent
17460 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017461 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070017462
17463 state = FADING;
17464
17465 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080017466 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070017467 }
17468 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070017469 }
Mike Cleronf116bf82009-09-27 19:14:12 -070017470
Svetoslav Ganova0156172011-06-26 17:55:44 -070017471 /**
17472 * Resuable callback for sending
17473 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
17474 */
17475 private class SendViewScrolledAccessibilityEvent implements Runnable {
17476 public volatile boolean mIsPending;
17477
17478 public void run() {
17479 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
17480 mIsPending = false;
17481 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017482 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017483
17484 /**
17485 * <p>
17486 * This class represents a delegate that can be registered in a {@link View}
17487 * to enhance accessibility support via composition rather via inheritance.
17488 * It is specifically targeted to widget developers that extend basic View
17489 * classes i.e. classes in package android.view, that would like their
17490 * applications to be backwards compatible.
17491 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080017492 * <div class="special reference">
17493 * <h3>Developer Guides</h3>
17494 * <p>For more information about making applications accessible, read the
17495 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
17496 * developer guide.</p>
17497 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017498 * <p>
17499 * A scenario in which a developer would like to use an accessibility delegate
17500 * is overriding a method introduced in a later API version then the minimal API
17501 * version supported by the application. For example, the method
17502 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
17503 * in API version 4 when the accessibility APIs were first introduced. If a
17504 * developer would like his application to run on API version 4 devices (assuming
17505 * all other APIs used by the application are version 4 or lower) and take advantage
17506 * of this method, instead of overriding the method which would break the application's
17507 * backwards compatibility, he can override the corresponding method in this
17508 * delegate and register the delegate in the target View if the API version of
17509 * the system is high enough i.e. the API version is same or higher to the API
17510 * version that introduced
17511 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
17512 * </p>
17513 * <p>
17514 * Here is an example implementation:
17515 * </p>
17516 * <code><pre><p>
17517 * if (Build.VERSION.SDK_INT >= 14) {
17518 * // If the API version is equal of higher than the version in
17519 * // which onInitializeAccessibilityNodeInfo was introduced we
17520 * // register a delegate with a customized implementation.
17521 * View view = findViewById(R.id.view_id);
17522 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
17523 * public void onInitializeAccessibilityNodeInfo(View host,
17524 * AccessibilityNodeInfo info) {
17525 * // Let the default implementation populate the info.
17526 * super.onInitializeAccessibilityNodeInfo(host, info);
17527 * // Set some other information.
17528 * info.setEnabled(host.isEnabled());
17529 * }
17530 * });
17531 * }
17532 * </code></pre></p>
17533 * <p>
17534 * This delegate contains methods that correspond to the accessibility methods
17535 * in View. If a delegate has been specified the implementation in View hands
17536 * off handling to the corresponding method in this delegate. The default
17537 * implementation the delegate methods behaves exactly as the corresponding
17538 * method in View for the case of no accessibility delegate been set. Hence,
17539 * to customize the behavior of a View method, clients can override only the
17540 * corresponding delegate method without altering the behavior of the rest
17541 * accessibility related methods of the host view.
17542 * </p>
17543 */
17544 public static class AccessibilityDelegate {
17545
17546 /**
17547 * Sends an accessibility event of the given type. If accessibility is not
17548 * enabled this method has no effect.
17549 * <p>
17550 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
17551 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
17552 * been set.
17553 * </p>
17554 *
17555 * @param host The View hosting the delegate.
17556 * @param eventType The type of the event to send.
17557 *
17558 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
17559 */
17560 public void sendAccessibilityEvent(View host, int eventType) {
17561 host.sendAccessibilityEventInternal(eventType);
17562 }
17563
17564 /**
alanv8eeefef2012-05-07 16:57:53 -070017565 * Performs the specified accessibility action on the view. For
17566 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
17567 * <p>
17568 * The default implementation behaves as
17569 * {@link View#performAccessibilityAction(int, Bundle)
17570 * View#performAccessibilityAction(int, Bundle)} for the case of
17571 * no accessibility delegate been set.
17572 * </p>
17573 *
17574 * @param action The action to perform.
17575 * @return Whether the action was performed.
17576 *
17577 * @see View#performAccessibilityAction(int, Bundle)
17578 * View#performAccessibilityAction(int, Bundle)
17579 */
17580 public boolean performAccessibilityAction(View host, int action, Bundle args) {
17581 return host.performAccessibilityActionInternal(action, args);
17582 }
17583
17584 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017585 * Sends an accessibility event. This method behaves exactly as
17586 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
17587 * empty {@link AccessibilityEvent} and does not perform a check whether
17588 * accessibility is enabled.
17589 * <p>
17590 * The default implementation behaves as
17591 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17592 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
17593 * the case of no accessibility delegate been set.
17594 * </p>
17595 *
17596 * @param host The View hosting the delegate.
17597 * @param event The event to send.
17598 *
17599 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17600 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17601 */
17602 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
17603 host.sendAccessibilityEventUncheckedInternal(event);
17604 }
17605
17606 /**
17607 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
17608 * to its children for adding their text content to the event.
17609 * <p>
17610 * The default implementation behaves as
17611 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17612 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
17613 * the case of no accessibility delegate been set.
17614 * </p>
17615 *
17616 * @param host The View hosting the delegate.
17617 * @param event The event.
17618 * @return True if the event population was completed.
17619 *
17620 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17621 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17622 */
17623 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17624 return host.dispatchPopulateAccessibilityEventInternal(event);
17625 }
17626
17627 /**
17628 * Gives a chance to the host View to populate the accessibility event with its
17629 * text content.
17630 * <p>
17631 * The default implementation behaves as
17632 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
17633 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
17634 * the case of no accessibility delegate been set.
17635 * </p>
17636 *
17637 * @param host The View hosting the delegate.
17638 * @param event The accessibility event which to populate.
17639 *
17640 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
17641 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
17642 */
17643 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17644 host.onPopulateAccessibilityEventInternal(event);
17645 }
17646
17647 /**
17648 * Initializes an {@link AccessibilityEvent} with information about the
17649 * the host View which is the event source.
17650 * <p>
17651 * The default implementation behaves as
17652 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
17653 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
17654 * the case of no accessibility delegate been set.
17655 * </p>
17656 *
17657 * @param host The View hosting the delegate.
17658 * @param event The event to initialize.
17659 *
17660 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
17661 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
17662 */
17663 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
17664 host.onInitializeAccessibilityEventInternal(event);
17665 }
17666
17667 /**
17668 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
17669 * <p>
17670 * The default implementation behaves as
17671 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17672 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
17673 * the case of no accessibility delegate been set.
17674 * </p>
17675 *
17676 * @param host The View hosting the delegate.
17677 * @param info The instance to initialize.
17678 *
17679 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17680 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17681 */
17682 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
17683 host.onInitializeAccessibilityNodeInfoInternal(info);
17684 }
17685
17686 /**
17687 * Called when a child of the host View has requested sending an
17688 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
17689 * to augment the event.
17690 * <p>
17691 * The default implementation behaves as
17692 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17693 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
17694 * the case of no accessibility delegate been set.
17695 * </p>
17696 *
17697 * @param host The View hosting the delegate.
17698 * @param child The child which requests sending the event.
17699 * @param event The event to be sent.
17700 * @return True if the event should be sent
17701 *
17702 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17703 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17704 */
17705 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
17706 AccessibilityEvent event) {
17707 return host.onRequestSendAccessibilityEventInternal(child, event);
17708 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070017709
17710 /**
17711 * Gets the provider for managing a virtual view hierarchy rooted at this View
17712 * and reported to {@link android.accessibilityservice.AccessibilityService}s
17713 * that explore the window content.
17714 * <p>
17715 * The default implementation behaves as
17716 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
17717 * the case of no accessibility delegate been set.
17718 * </p>
17719 *
17720 * @return The provider.
17721 *
17722 * @see AccessibilityNodeProvider
17723 */
17724 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
17725 return null;
17726 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017727 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017728}