blob: 225030626ed07b02c3c7ddc95a55af86570081a5 [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;
Mike Cleronf116bf82009-09-27 19:14:12 -070027import android.graphics.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.graphics.LinearGradient;
29import android.graphics.Matrix;
30import android.graphics.Paint;
31import android.graphics.PixelFormat;
svetoslavganov75986cf2009-05-14 22:28:01 -070032import android.graphics.Point;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.graphics.PorterDuff;
34import android.graphics.PorterDuffXfermode;
35import android.graphics.Rect;
Adam Powell6e346362010-07-23 10:18:23 -070036import android.graphics.RectF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.graphics.Region;
38import android.graphics.Shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.graphics.drawable.ColorDrawable;
40import android.graphics.drawable.Drawable;
41import android.os.Handler;
42import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.os.Parcel;
44import android.os.Parcelable;
45import android.os.RemoteException;
46import android.os.SystemClock;
Svetoslav Ganovea515ae2011-09-14 18:15:32 -070047import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.util.AttributeSet;
Doug Feltcb3791202011-07-07 11:57:48 -070049import android.util.FloatProperty;
50import android.util.LocaleUtil;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070052import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070053import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070054import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070055import android.util.Pools;
Doug Feltcb3791202011-07-07 11:57:48 -070056import android.util.Property;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080058import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.view.ContextMenu.ContextMenuInfo;
svetoslavganov75986cf2009-05-14 22:28:01 -070060import android.view.accessibility.AccessibilityEvent;
61import android.view.accessibility.AccessibilityEventSource;
62import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070063import android.view.accessibility.AccessibilityNodeInfo;
Svetoslav Ganov02107852011-10-03 17:06:56 -070064import android.view.accessibility.AccessibilityNodeProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070066import android.view.animation.AnimationUtils;
Chet Haase64a48c12012-02-13 16:33:29 -080067import android.view.animation.Transformation;
svetoslavganov75986cf2009-05-14 22:28:01 -070068import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.view.inputmethod.InputConnection;
70import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.widget.ScrollBarDrawable;
72
Romain Guy1ef3fdb2011-09-09 15:30:30 -070073import static android.os.Build.VERSION_CODES.*;
Philip Milne6c8ea062012-04-03 17:38:43 -070074import static java.lang.Math.max;
Romain Guy1ef3fdb2011-09-09 15:30:30 -070075
Doug Feltcb3791202011-07-07 11:57:48 -070076import com.android.internal.R;
77import com.android.internal.util.Predicate;
78import com.android.internal.view.menu.MenuBuilder;
79
Christopher Tatea0374192010-10-05 13:06:41 -070080import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070081import java.lang.reflect.InvocationTargetException;
82import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083import java.util.ArrayList;
84import java.util.Arrays;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070085import java.util.Locale;
Adam Powell4afd62b2011-02-18 15:02:18 -080086import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087
88/**
89 * <p>
90 * This class represents the basic building block for user interface components. A View
91 * occupies a rectangular area on the screen and is responsible for drawing and
92 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070093 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
95 * are invisible containers that hold other Views (or other ViewGroups) and define
96 * their layout properties.
97 * </p>
98 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -070099 * <div class="special reference">
100 * <h3>Developer Guides</h3>
101 * <p>For information about using this class to develop your application's user interface,
102 * 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 -0800103 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700104 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 * <a name="Using"></a>
106 * <h3>Using Views</h3>
107 * <p>
108 * All of the views in a window are arranged in a single tree. You can add views
109 * either from code or by specifying a tree of views in one or more XML layout
110 * files. There are many specialized subclasses of views that act as controls or
111 * are capable of displaying text, images, or other content.
112 * </p>
113 * <p>
114 * Once you have created a tree of views, there are typically a few types of
115 * common operations you may wish to perform:
116 * <ul>
117 * <li><strong>Set properties:</strong> for example setting the text of a
118 * {@link android.widget.TextView}. The available properties and the methods
119 * that set them will vary among the different subclasses of views. Note that
120 * properties that are known at build time can be set in the XML layout
121 * files.</li>
122 * <li><strong>Set focus:</strong> The framework will handled moving focus in
123 * response to user input. To force focus to a specific view, call
124 * {@link #requestFocus}.</li>
125 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
126 * that will be notified when something interesting happens to the view. For
127 * example, all views will let you set a listener to be notified when the view
128 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700129 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
Philip Milne6c8ea062012-04-03 17:38:43 -0700130 * Other view subclasses offer more specialized listeners. For example, a Button
Romain Guy5c22a8c2011-05-13 11:48:45 -0700131 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700133 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 * </ul>
135 * </p>
136 * <p><em>
137 * Note: The Android framework is responsible for measuring, laying out and
138 * drawing views. You should not call methods that perform these actions on
139 * views yourself unless you are actually implementing a
140 * {@link android.view.ViewGroup}.
141 * </em></p>
142 *
143 * <a name="Lifecycle"></a>
144 * <h3>Implementing a Custom View</h3>
145 *
146 * <p>
147 * To implement a custom view, you will usually begin by providing overrides for
148 * some of the standard methods that the framework calls on all views. You do
149 * not need to override all of these methods. In fact, you can start by just
150 * overriding {@link #onDraw(android.graphics.Canvas)}.
151 * <table border="2" width="85%" align="center" cellpadding="5">
152 * <thead>
153 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
154 * </thead>
155 *
156 * <tbody>
157 * <tr>
158 * <td rowspan="2">Creation</td>
159 * <td>Constructors</td>
160 * <td>There is a form of the constructor that are called when the view
161 * is created from code and a form that is called when the view is
162 * inflated from a layout file. The second form should parse and apply
163 * any attributes defined in the layout file.
164 * </td>
165 * </tr>
166 * <tr>
167 * <td><code>{@link #onFinishInflate()}</code></td>
168 * <td>Called after a view and all of its children has been inflated
169 * from XML.</td>
170 * </tr>
171 *
172 * <tr>
173 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700174 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 * <td>Called to determine the size requirements for this view and all
176 * of its children.
177 * </td>
178 * </tr>
179 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700180 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 * <td>Called when this view should assign a size and position to all
182 * of its children.
183 * </td>
184 * </tr>
185 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700186 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 * <td>Called when the size of this view has changed.
188 * </td>
189 * </tr>
190 *
191 * <tr>
192 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700193 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 * <td>Called when the view should render its content.
195 * </td>
196 * </tr>
197 *
198 * <tr>
199 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700200 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 * <td>Called when a new key event occurs.
202 * </td>
203 * </tr>
204 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700205 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 * <td>Called when a key up event occurs.
207 * </td>
208 * </tr>
209 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700210 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 * <td>Called when a trackball motion event occurs.
212 * </td>
213 * </tr>
214 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700215 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 * <td>Called when a touch screen motion event occurs.
217 * </td>
218 * </tr>
219 *
220 * <tr>
221 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700222 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 * <td>Called when the view gains or loses focus.
224 * </td>
225 * </tr>
226 *
227 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700228 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 * <td>Called when the window containing the view gains or loses focus.
230 * </td>
231 * </tr>
232 *
233 * <tr>
234 * <td rowspan="3">Attaching</td>
235 * <td><code>{@link #onAttachedToWindow()}</code></td>
236 * <td>Called when the view is attached to a window.
237 * </td>
238 * </tr>
239 *
240 * <tr>
241 * <td><code>{@link #onDetachedFromWindow}</code></td>
242 * <td>Called when the view is detached from its window.
243 * </td>
244 * </tr>
245 *
246 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700247 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 * <td>Called when the visibility of the window containing the view
249 * has changed.
250 * </td>
251 * </tr>
252 * </tbody>
253 *
254 * </table>
255 * </p>
256 *
257 * <a name="IDs"></a>
258 * <h3>IDs</h3>
259 * Views may have an integer id associated with them. These ids are typically
260 * assigned in the layout XML files, and are used to find specific views within
261 * the view tree. A common pattern is to:
262 * <ul>
263 * <li>Define a Button in the layout file and assign it a unique ID.
264 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700265 * &lt;Button
266 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 * android:layout_width="wrap_content"
268 * android:layout_height="wrap_content"
269 * android:text="@string/my_button_text"/&gt;
270 * </pre></li>
271 * <li>From the onCreate method of an Activity, find the Button
272 * <pre class="prettyprint">
273 * Button myButton = (Button) findViewById(R.id.my_button);
274 * </pre></li>
275 * </ul>
276 * <p>
277 * View IDs need not be unique throughout the tree, but it is good practice to
278 * ensure that they are at least unique within the part of the tree you are
279 * searching.
280 * </p>
281 *
282 * <a name="Position"></a>
283 * <h3>Position</h3>
284 * <p>
285 * The geometry of a view is that of a rectangle. A view has a location,
286 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
287 * two dimensions, expressed as a width and a height. The unit for location
288 * and dimensions is the pixel.
289 * </p>
290 *
291 * <p>
292 * It is possible to retrieve the location of a view by invoking the methods
293 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
294 * coordinate of the rectangle representing the view. The latter returns the
295 * top, or Y, coordinate of the rectangle representing the view. These methods
296 * both return the location of the view relative to its parent. For instance,
297 * when getLeft() returns 20, that means the view is located 20 pixels to the
298 * right of the left edge of its direct parent.
299 * </p>
300 *
301 * <p>
302 * In addition, several convenience methods are offered to avoid unnecessary
303 * computations, namely {@link #getRight()} and {@link #getBottom()}.
304 * These methods return the coordinates of the right and bottom edges of the
305 * rectangle representing the view. For instance, calling {@link #getRight()}
306 * is similar to the following computation: <code>getLeft() + getWidth()</code>
307 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
308 * </p>
309 *
310 * <a name="SizePaddingMargins"></a>
311 * <h3>Size, padding and margins</h3>
312 * <p>
313 * The size of a view is expressed with a width and a height. A view actually
314 * possess two pairs of width and height values.
315 * </p>
316 *
317 * <p>
318 * The first pair is known as <em>measured width</em> and
319 * <em>measured height</em>. These dimensions define how big a view wants to be
320 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
321 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
322 * and {@link #getMeasuredHeight()}.
323 * </p>
324 *
325 * <p>
326 * The second pair is simply known as <em>width</em> and <em>height</em>, or
327 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
328 * dimensions define the actual size of the view on screen, at drawing time and
329 * after layout. These values may, but do not have to, be different from the
330 * measured width and height. The width and height can be obtained by calling
331 * {@link #getWidth()} and {@link #getHeight()}.
332 * </p>
333 *
334 * <p>
335 * To measure its dimensions, a view takes into account its padding. The padding
336 * is expressed in pixels for the left, top, right and bottom parts of the view.
337 * Padding can be used to offset the content of the view by a specific amount of
338 * pixels. For instance, a left padding of 2 will push the view's content by
339 * 2 pixels to the right of the left edge. Padding can be set using the
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -0700340 * {@link #setPadding(int, int, int, int)} or {@link #setPaddingRelative(int, int, int, int)}
341 * method and queried by calling {@link #getPaddingLeft()}, {@link #getPaddingTop()},
342 * {@link #getPaddingRight()}, {@link #getPaddingBottom()}, {@link #getPaddingStart()},
343 * {@link #getPaddingEnd()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 * </p>
345 *
346 * <p>
347 * Even though a view can define a padding, it does not provide any support for
348 * margins. However, view groups provide such a support. Refer to
349 * {@link android.view.ViewGroup} and
350 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
351 * </p>
352 *
353 * <a name="Layout"></a>
354 * <h3>Layout</h3>
355 * <p>
356 * Layout is a two pass process: a measure pass and a layout pass. The measuring
357 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
358 * of the view tree. Each view pushes dimension specifications down the tree
359 * during the recursion. At the end of the measure pass, every view has stored
360 * its measurements. The second pass happens in
361 * {@link #layout(int,int,int,int)} and is also top-down. During
362 * this pass each parent is responsible for positioning all of its children
363 * using the sizes computed in the measure pass.
364 * </p>
365 *
366 * <p>
367 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
368 * {@link #getMeasuredHeight()} values must be set, along with those for all of
369 * that view's descendants. A view's measured width and measured height values
370 * must respect the constraints imposed by the view's parents. This guarantees
371 * that at the end of the measure pass, all parents accept all of their
372 * children's measurements. A parent view may call measure() more than once on
373 * its children. For example, the parent may measure each child once with
374 * unspecified dimensions to find out how big they want to be, then call
375 * measure() on them again with actual numbers if the sum of all the children's
376 * unconstrained sizes is too big or too small.
377 * </p>
378 *
379 * <p>
380 * The measure pass uses two classes to communicate dimensions. The
381 * {@link MeasureSpec} class is used by views to tell their parents how they
382 * want to be measured and positioned. The base LayoutParams class just
383 * describes how big the view wants to be for both width and height. For each
384 * dimension, it can specify one of:
385 * <ul>
386 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800387 * <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 -0800388 * (minus padding)
389 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
390 * enclose its content (plus padding).
391 * </ul>
392 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
393 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
394 * an X and Y value.
395 * </p>
396 *
397 * <p>
398 * MeasureSpecs are used to push requirements down the tree from parent to
399 * child. A MeasureSpec can be in one of three modes:
400 * <ul>
401 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
402 * of a child view. For example, a LinearLayout may call measure() on its child
403 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
404 * tall the child view wants to be given a width of 240 pixels.
405 * <li>EXACTLY: This is used by the parent to impose an exact size on the
406 * child. The child must use this size, and guarantee that all of its
407 * descendants will fit within this size.
408 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
409 * child. The child must gurantee that it and all of its descendants will fit
410 * within this size.
411 * </ul>
412 * </p>
413 *
414 * <p>
415 * To intiate a layout, call {@link #requestLayout}. This method is typically
416 * called by a view on itself when it believes that is can no longer fit within
417 * its current bounds.
418 * </p>
419 *
420 * <a name="Drawing"></a>
421 * <h3>Drawing</h3>
422 * <p>
423 * Drawing is handled by walking the tree and rendering each view that
Joe Fernandez558459f2011-10-13 16:47:36 -0700424 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 * this means that parents will draw before (i.e., behind) their children, with
426 * siblings drawn in the order they appear in the tree.
427 * If you set a background drawable for a View, then the View will draw it for you
428 * before calling back to its <code>onDraw()</code> method.
429 * </p>
430 *
431 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700432 * 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 -0800433 * </p>
434 *
435 * <p>
436 * To force a view to draw, call {@link #invalidate()}.
437 * </p>
438 *
439 * <a name="EventHandlingThreading"></a>
440 * <h3>Event Handling and Threading</h3>
441 * <p>
442 * The basic cycle of a view is as follows:
443 * <ol>
444 * <li>An event comes in and is dispatched to the appropriate view. The view
445 * handles the event and notifies any listeners.</li>
446 * <li>If in the course of processing the event, the view's bounds may need
447 * to be changed, the view will call {@link #requestLayout()}.</li>
448 * <li>Similarly, if in the course of processing the event the view's appearance
449 * may need to be changed, the view will call {@link #invalidate()}.</li>
450 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
451 * the framework will take care of measuring, laying out, and drawing the tree
452 * as appropriate.</li>
453 * </ol>
454 * </p>
455 *
456 * <p><em>Note: The entire view tree is single threaded. You must always be on
457 * the UI thread when calling any method on any view.</em>
458 * If you are doing work on other threads and want to update the state of a view
459 * from that thread, you should use a {@link Handler}.
460 * </p>
461 *
462 * <a name="FocusHandling"></a>
463 * <h3>Focus Handling</h3>
464 * <p>
465 * The framework will handle routine focus movement in response to user input.
466 * This includes changing the focus as views are removed or hidden, or as new
467 * views become available. Views indicate their willingness to take focus
468 * through the {@link #isFocusable} method. To change whether a view can take
469 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
470 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
471 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
472 * </p>
473 * <p>
474 * Focus movement is based on an algorithm which finds the nearest neighbor in a
475 * given direction. In rare cases, the default algorithm may not match the
476 * intended behavior of the developer. In these situations, you can provide
477 * explicit overrides by using these XML attributes in the layout file:
478 * <pre>
479 * nextFocusDown
480 * nextFocusLeft
481 * nextFocusRight
482 * nextFocusUp
483 * </pre>
484 * </p>
485 *
486 *
487 * <p>
488 * To get a particular view to take focus, call {@link #requestFocus()}.
489 * </p>
490 *
491 * <a name="TouchMode"></a>
492 * <h3>Touch Mode</h3>
493 * <p>
494 * When a user is navigating a user interface via directional keys such as a D-pad, it is
495 * necessary to give focus to actionable items such as buttons so the user can see
496 * what will take input. If the device has touch capabilities, however, and the user
497 * begins interacting with the interface by touching it, it is no longer necessary to
498 * always highlight, or give focus to, a particular view. This motivates a mode
499 * for interaction named 'touch mode'.
500 * </p>
501 * <p>
502 * For a touch capable device, once the user touches the screen, the device
503 * will enter touch mode. From this point onward, only views for which
504 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
505 * Other views that are touchable, like buttons, will not take focus when touched; they will
506 * only fire the on click listeners.
507 * </p>
508 * <p>
509 * Any time a user hits a directional key, such as a D-pad direction, the view device will
510 * exit touch mode, and find a view to take focus, so that the user may resume interacting
511 * with the user interface without touching the screen again.
512 * </p>
513 * <p>
514 * The touch mode state is maintained across {@link android.app.Activity}s. Call
515 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
516 * </p>
517 *
518 * <a name="Scrolling"></a>
519 * <h3>Scrolling</h3>
520 * <p>
521 * The framework provides basic support for views that wish to internally
522 * scroll their content. This includes keeping track of the X and Y scroll
523 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800524 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700525 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 * </p>
527 *
528 * <a name="Tags"></a>
529 * <h3>Tags</h3>
530 * <p>
531 * Unlike IDs, tags are not used to identify views. Tags are essentially an
532 * extra piece of information that can be associated with a view. They are most
533 * often used as a convenience to store data related to views in the views
534 * themselves rather than by putting them in a separate structure.
535 * </p>
536 *
537 * <a name="Animation"></a>
538 * <h3>Animation</h3>
539 * <p>
540 * You can attach an {@link Animation} object to a view using
541 * {@link #setAnimation(Animation)} or
542 * {@link #startAnimation(Animation)}. The animation can alter the scale,
543 * rotation, translation and alpha of a view over time. If the animation is
544 * attached to a view that has children, the animation will affect the entire
545 * subtree rooted by that node. When an animation is started, the framework will
546 * take care of redrawing the appropriate views until the animation completes.
547 * </p>
Romain Guy171c5922011-01-06 10:04:23 -0800548 * <p>
549 * Starting with Android 3.0, the preferred way of animating views is to use the
550 * {@link android.animation} package APIs.
551 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 *
Jeff Brown85a31762010-09-01 17:01:00 -0700553 * <a name="Security"></a>
554 * <h3>Security</h3>
555 * <p>
556 * Sometimes it is essential that an application be able to verify that an action
557 * is being performed with the full knowledge and consent of the user, such as
558 * granting a permission request, making a purchase or clicking on an advertisement.
559 * Unfortunately, a malicious application could try to spoof the user into
560 * performing these actions, unaware, by concealing the intended purpose of the view.
561 * As a remedy, the framework offers a touch filtering mechanism that can be used to
562 * improve the security of views that provide access to sensitive functionality.
563 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700564 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800565 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700566 * will discard touches that are received whenever the view's window is obscured by
567 * another visible window. As a result, the view will not receive touches whenever a
568 * toast, dialog or other window appears above the view's window.
569 * </p><p>
570 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700571 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
572 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700573 * </p>
574 *
Romain Guy171c5922011-01-06 10:04:23 -0800575 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700576 * @attr ref android.R.styleable#View_background
577 * @attr ref android.R.styleable#View_clickable
578 * @attr ref android.R.styleable#View_contentDescription
579 * @attr ref android.R.styleable#View_drawingCacheQuality
580 * @attr ref android.R.styleable#View_duplicateParentState
581 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700582 * @attr ref android.R.styleable#View_requiresFadingEdge
Philip Milne6c8ea062012-04-03 17:38:43 -0700583 * @attr ref android.R.styleable#View_fadeScrollbars
Romain Guyd6a463a2009-05-21 23:10:10 -0700584 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700585 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700587 * @attr ref android.R.styleable#View_isScrollContainer
588 * @attr ref android.R.styleable#View_focusable
589 * @attr ref android.R.styleable#View_focusableInTouchMode
590 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
591 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800592 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700593 * @attr ref android.R.styleable#View_longClickable
594 * @attr ref android.R.styleable#View_minHeight
595 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 * @attr ref android.R.styleable#View_nextFocusDown
597 * @attr ref android.R.styleable#View_nextFocusLeft
598 * @attr ref android.R.styleable#View_nextFocusRight
599 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700600 * @attr ref android.R.styleable#View_onClick
601 * @attr ref android.R.styleable#View_padding
602 * @attr ref android.R.styleable#View_paddingBottom
603 * @attr ref android.R.styleable#View_paddingLeft
604 * @attr ref android.R.styleable#View_paddingRight
605 * @attr ref android.R.styleable#View_paddingTop
Fabrice Di Meglio101d5aa2012-02-16 18:36:06 -0800606 * @attr ref android.R.styleable#View_paddingStart
607 * @attr ref android.R.styleable#View_paddingEnd
Romain Guyd6a463a2009-05-21 23:10:10 -0700608 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800609 * @attr ref android.R.styleable#View_rotation
610 * @attr ref android.R.styleable#View_rotationX
611 * @attr ref android.R.styleable#View_rotationY
612 * @attr ref android.R.styleable#View_scaleX
613 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 * @attr ref android.R.styleable#View_scrollX
615 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700616 * @attr ref android.R.styleable#View_scrollbarSize
617 * @attr ref android.R.styleable#View_scrollbarStyle
618 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700619 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
620 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
622 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 * @attr ref android.R.styleable#View_scrollbarThumbVertical
624 * @attr ref android.R.styleable#View_scrollbarTrackVertical
625 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
626 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700627 * @attr ref android.R.styleable#View_soundEffectsEnabled
628 * @attr ref android.R.styleable#View_tag
Chet Haase73066682010-11-29 15:55:32 -0800629 * @attr ref android.R.styleable#View_transformPivotX
630 * @attr ref android.R.styleable#View_transformPivotY
631 * @attr ref android.R.styleable#View_translationX
632 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700633 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 *
635 * @see android.view.ViewGroup
636 */
Adam Powell8fc54f92011-09-07 16:40:45 -0700637public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Callback,
638 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 private static final boolean DBG = false;
640
641 /**
642 * The logging tag used by this class with android.util.Log.
643 */
644 protected static final String VIEW_LOG_TAG = "View";
645
646 /**
647 * Used to mark a View that has no ID.
648 */
649 public static final int NO_ID = -1;
650
651 /**
652 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
653 * calling setFlags.
654 */
655 private static final int NOT_FOCUSABLE = 0x00000000;
656
657 /**
658 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
659 * setFlags.
660 */
661 private static final int FOCUSABLE = 0x00000001;
662
663 /**
664 * Mask for use with setFlags indicating bits used for focus.
665 */
666 private static final int FOCUSABLE_MASK = 0x00000001;
667
668 /**
669 * This view will adjust its padding to fit sytem windows (e.g. status bar)
670 */
671 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
672
673 /**
Scott Main812634c22011-07-27 13:22:35 -0700674 * This view is visible.
675 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
676 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 */
678 public static final int VISIBLE = 0x00000000;
679
680 /**
681 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700682 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
683 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 */
685 public static final int INVISIBLE = 0x00000004;
686
687 /**
688 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700689 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
690 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691 */
692 public static final int GONE = 0x00000008;
693
694 /**
695 * Mask for use with setFlags indicating bits used for visibility.
696 * {@hide}
697 */
698 static final int VISIBILITY_MASK = 0x0000000C;
699
700 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
701
702 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700703 * This view is enabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 * Use with ENABLED_MASK when calling setFlags.
705 * {@hide}
706 */
707 static final int ENABLED = 0x00000000;
708
709 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700710 * This view is disabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 * Use with ENABLED_MASK when calling setFlags.
712 * {@hide}
713 */
714 static final int DISABLED = 0x00000020;
715
716 /**
717 * Mask for use with setFlags indicating bits used for indicating whether
718 * this view is enabled
719 * {@hide}
720 */
721 static final int ENABLED_MASK = 0x00000020;
722
723 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700724 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
725 * called and further optimizations will be performed. It is okay to have
726 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727 * {@hide}
728 */
729 static final int WILL_NOT_DRAW = 0x00000080;
730
731 /**
732 * Mask for use with setFlags indicating bits used for indicating whether
733 * this view is will draw
734 * {@hide}
735 */
736 static final int DRAW_MASK = 0x00000080;
737
738 /**
739 * <p>This view doesn't show scrollbars.</p>
740 * {@hide}
741 */
742 static final int SCROLLBARS_NONE = 0x00000000;
743
744 /**
745 * <p>This view shows horizontal scrollbars.</p>
746 * {@hide}
747 */
748 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
749
750 /**
751 * <p>This view shows vertical scrollbars.</p>
752 * {@hide}
753 */
754 static final int SCROLLBARS_VERTICAL = 0x00000200;
755
756 /**
757 * <p>Mask for use with setFlags indicating bits used for indicating which
758 * scrollbars are enabled.</p>
759 * {@hide}
760 */
761 static final int SCROLLBARS_MASK = 0x00000300;
762
Jeff Brown85a31762010-09-01 17:01:00 -0700763 /**
764 * Indicates that the view should filter touches when its window is obscured.
765 * Refer to the class comments for more information about this security feature.
766 * {@hide}
767 */
768 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
769
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700770 /**
771 * Set for framework elements that use FITS_SYSTEM_WINDOWS, to indicate
772 * that they are optional and should be skipped if the window has
773 * requested system UI flags that ignore those insets for layout.
774 */
775 static final int OPTIONAL_FITS_SYSTEM_WINDOWS = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776
777 /**
778 * <p>This view doesn't show fading edges.</p>
779 * {@hide}
780 */
781 static final int FADING_EDGE_NONE = 0x00000000;
782
783 /**
784 * <p>This view shows horizontal fading edges.</p>
785 * {@hide}
786 */
787 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
788
789 /**
790 * <p>This view shows vertical fading edges.</p>
791 * {@hide}
792 */
793 static final int FADING_EDGE_VERTICAL = 0x00002000;
794
795 /**
796 * <p>Mask for use with setFlags indicating bits used for indicating which
797 * fading edges are enabled.</p>
798 * {@hide}
799 */
800 static final int FADING_EDGE_MASK = 0x00003000;
801
802 /**
803 * <p>Indicates this view can be clicked. When clickable, a View reacts
804 * to clicks by notifying the OnClickListener.<p>
805 * {@hide}
806 */
807 static final int CLICKABLE = 0x00004000;
808
809 /**
810 * <p>Indicates this view is caching its drawing into a bitmap.</p>
811 * {@hide}
812 */
813 static final int DRAWING_CACHE_ENABLED = 0x00008000;
814
815 /**
816 * <p>Indicates that no icicle should be saved for this view.<p>
817 * {@hide}
818 */
819 static final int SAVE_DISABLED = 0x000010000;
820
821 /**
822 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
823 * property.</p>
824 * {@hide}
825 */
826 static final int SAVE_DISABLED_MASK = 0x000010000;
827
828 /**
829 * <p>Indicates that no drawing cache should ever be created for this view.<p>
830 * {@hide}
831 */
832 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
833
834 /**
835 * <p>Indicates this view can take / keep focus when int touch mode.</p>
836 * {@hide}
837 */
838 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
839
840 /**
841 * <p>Enables low quality mode for the drawing cache.</p>
842 */
843 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
844
845 /**
846 * <p>Enables high quality mode for the drawing cache.</p>
847 */
848 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
849
850 /**
851 * <p>Enables automatic quality mode for the drawing cache.</p>
852 */
853 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
854
855 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
856 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
857 };
858
859 /**
860 * <p>Mask for use with setFlags indicating bits used for the cache
861 * quality property.</p>
862 * {@hide}
863 */
864 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
865
866 /**
867 * <p>
868 * Indicates this view can be long clicked. When long clickable, a View
869 * reacts to long clicks by notifying the OnLongClickListener or showing a
870 * context menu.
871 * </p>
872 * {@hide}
873 */
874 static final int LONG_CLICKABLE = 0x00200000;
875
876 /**
877 * <p>Indicates that this view gets its drawable states from its direct parent
878 * and ignores its original internal states.</p>
879 *
880 * @hide
881 */
882 static final int DUPLICATE_PARENT_STATE = 0x00400000;
883
884 /**
885 * The scrollbar style to display the scrollbars inside the content area,
886 * without increasing the padding. The scrollbars will be overlaid with
887 * translucency on the view's content.
888 */
889 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
890
891 /**
892 * The scrollbar style to display the scrollbars inside the padded area,
893 * increasing the padding of the view. The scrollbars will not overlap the
894 * content area of the view.
895 */
896 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
897
898 /**
899 * The scrollbar style to display the scrollbars at the edge of the view,
900 * without increasing the padding. The scrollbars will be overlaid with
901 * translucency.
902 */
903 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
904
905 /**
906 * The scrollbar style to display the scrollbars at the edge of the view,
907 * increasing the padding of the view. The scrollbars will only overlap the
908 * background, if any.
909 */
910 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
911
912 /**
913 * Mask to check if the scrollbar style is overlay or inset.
914 * {@hide}
915 */
916 static final int SCROLLBARS_INSET_MASK = 0x01000000;
917
918 /**
919 * Mask to check if the scrollbar style is inside or outside.
920 * {@hide}
921 */
922 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
923
924 /**
925 * Mask for scrollbar style.
926 * {@hide}
927 */
928 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
929
930 /**
931 * View flag indicating that the screen should remain on while the
932 * window containing this view is visible to the user. This effectively
933 * takes care of automatically setting the WindowManager's
934 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
935 */
936 public static final int KEEP_SCREEN_ON = 0x04000000;
937
938 /**
939 * View flag indicating whether this view should have sound effects enabled
940 * for events such as clicking and touching.
941 */
942 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
943
944 /**
945 * View flag indicating whether this view should have haptic feedback
946 * enabled for events such as long presses.
947 */
948 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
949
950 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700951 * <p>Indicates that the view hierarchy should stop saving state when
952 * it reaches this view. If state saving is initiated immediately at
953 * the view, it will be allowed.
954 * {@hide}
955 */
956 static final int PARENT_SAVE_DISABLED = 0x20000000;
957
958 /**
959 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
960 * {@hide}
961 */
962 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
963
964 /**
svetoslavganov75986cf2009-05-14 22:28:01 -0700965 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
966 * should add all focusable Views regardless if they are focusable in touch mode.
967 */
968 public static final int FOCUSABLES_ALL = 0x00000000;
969
970 /**
971 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
972 * should add only Views focusable in touch mode.
973 */
974 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
975
976 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700977 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800978 * item.
979 */
980 public static final int FOCUS_BACKWARD = 0x00000001;
981
982 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700983 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 * item.
985 */
986 public static final int FOCUS_FORWARD = 0x00000002;
987
988 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700989 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 */
991 public static final int FOCUS_LEFT = 0x00000011;
992
993 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700994 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 */
996 public static final int FOCUS_UP = 0x00000021;
997
998 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700999 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 */
1001 public static final int FOCUS_RIGHT = 0x00000042;
1002
1003 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001004 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005 */
1006 public static final int FOCUS_DOWN = 0x00000082;
1007
1008 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001009 * Bits of {@link #getMeasuredWidthAndState()} and
1010 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1011 */
1012 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1013
1014 /**
1015 * Bits of {@link #getMeasuredWidthAndState()} and
1016 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1017 */
1018 public static final int MEASURED_STATE_MASK = 0xff000000;
1019
1020 /**
1021 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1022 * for functions that combine both width and height into a single int,
1023 * such as {@link #getMeasuredState()} and the childState argument of
1024 * {@link #resolveSizeAndState(int, int, int)}.
1025 */
1026 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1027
1028 /**
1029 * Bit of {@link #getMeasuredWidthAndState()} and
1030 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1031 * is smaller that the space the view would like to have.
1032 */
1033 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1034
1035 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 * Base View state sets
1037 */
1038 // Singles
1039 /**
1040 * Indicates the view has no states set. States are used with
1041 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1042 * view depending on its state.
1043 *
1044 * @see android.graphics.drawable.Drawable
1045 * @see #getDrawableState()
1046 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001047 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 /**
1049 * Indicates the view is enabled. States are used with
1050 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1051 * view depending on its state.
1052 *
1053 * @see android.graphics.drawable.Drawable
1054 * @see #getDrawableState()
1055 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001056 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 /**
1058 * Indicates the view is focused. States are used with
1059 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1060 * view depending on its state.
1061 *
1062 * @see android.graphics.drawable.Drawable
1063 * @see #getDrawableState()
1064 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001065 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 /**
1067 * Indicates the view is selected. States are used with
1068 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1069 * view depending on its state.
1070 *
1071 * @see android.graphics.drawable.Drawable
1072 * @see #getDrawableState()
1073 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001074 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 /**
1076 * Indicates the view is pressed. States are used with
1077 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1078 * view depending on its state.
1079 *
1080 * @see android.graphics.drawable.Drawable
1081 * @see #getDrawableState()
1082 * @hide
1083 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001084 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 /**
1086 * Indicates the view's window has focus. States are used with
1087 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1088 * view depending on its state.
1089 *
1090 * @see android.graphics.drawable.Drawable
1091 * @see #getDrawableState()
1092 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001093 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 // Doubles
1095 /**
1096 * Indicates the view is enabled and has the focus.
1097 *
1098 * @see #ENABLED_STATE_SET
1099 * @see #FOCUSED_STATE_SET
1100 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001101 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 /**
1103 * Indicates the view is enabled and selected.
1104 *
1105 * @see #ENABLED_STATE_SET
1106 * @see #SELECTED_STATE_SET
1107 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001108 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109 /**
1110 * Indicates the view is enabled and that its window has focus.
1111 *
1112 * @see #ENABLED_STATE_SET
1113 * @see #WINDOW_FOCUSED_STATE_SET
1114 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001115 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 /**
1117 * Indicates the view is focused and selected.
1118 *
1119 * @see #FOCUSED_STATE_SET
1120 * @see #SELECTED_STATE_SET
1121 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001122 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 /**
1124 * Indicates the view has the focus and that its window has the focus.
1125 *
1126 * @see #FOCUSED_STATE_SET
1127 * @see #WINDOW_FOCUSED_STATE_SET
1128 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001129 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 /**
1131 * Indicates the view is selected and that its window has the focus.
1132 *
1133 * @see #SELECTED_STATE_SET
1134 * @see #WINDOW_FOCUSED_STATE_SET
1135 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001136 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 // Triples
1138 /**
1139 * Indicates the view is enabled, focused and selected.
1140 *
1141 * @see #ENABLED_STATE_SET
1142 * @see #FOCUSED_STATE_SET
1143 * @see #SELECTED_STATE_SET
1144 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001145 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 /**
1147 * Indicates the view is enabled, focused and its window has the focus.
1148 *
1149 * @see #ENABLED_STATE_SET
1150 * @see #FOCUSED_STATE_SET
1151 * @see #WINDOW_FOCUSED_STATE_SET
1152 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001153 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001154 /**
1155 * Indicates the view is enabled, selected and its window has the focus.
1156 *
1157 * @see #ENABLED_STATE_SET
1158 * @see #SELECTED_STATE_SET
1159 * @see #WINDOW_FOCUSED_STATE_SET
1160 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001161 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162 /**
1163 * Indicates the view is focused, selected and its window has the focus.
1164 *
1165 * @see #FOCUSED_STATE_SET
1166 * @see #SELECTED_STATE_SET
1167 * @see #WINDOW_FOCUSED_STATE_SET
1168 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001169 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001170 /**
1171 * Indicates the view is enabled, focused, selected and its window
1172 * has the focus.
1173 *
1174 * @see #ENABLED_STATE_SET
1175 * @see #FOCUSED_STATE_SET
1176 * @see #SELECTED_STATE_SET
1177 * @see #WINDOW_FOCUSED_STATE_SET
1178 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001179 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 /**
1181 * Indicates the view is pressed and its window has the focus.
1182 *
1183 * @see #PRESSED_STATE_SET
1184 * @see #WINDOW_FOCUSED_STATE_SET
1185 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001186 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 /**
1188 * Indicates the view is pressed and selected.
1189 *
1190 * @see #PRESSED_STATE_SET
1191 * @see #SELECTED_STATE_SET
1192 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001193 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 /**
1195 * Indicates the view is pressed, selected and its window has the focus.
1196 *
1197 * @see #PRESSED_STATE_SET
1198 * @see #SELECTED_STATE_SET
1199 * @see #WINDOW_FOCUSED_STATE_SET
1200 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001201 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 /**
1203 * Indicates the view is pressed and focused.
1204 *
1205 * @see #PRESSED_STATE_SET
1206 * @see #FOCUSED_STATE_SET
1207 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001208 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 /**
1210 * Indicates the view is pressed, focused and its window has the focus.
1211 *
1212 * @see #PRESSED_STATE_SET
1213 * @see #FOCUSED_STATE_SET
1214 * @see #WINDOW_FOCUSED_STATE_SET
1215 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001216 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001217 /**
1218 * Indicates the view is pressed, focused and selected.
1219 *
1220 * @see #PRESSED_STATE_SET
1221 * @see #SELECTED_STATE_SET
1222 * @see #FOCUSED_STATE_SET
1223 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001224 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225 /**
1226 * Indicates the view is pressed, focused, selected and its window has the focus.
1227 *
1228 * @see #PRESSED_STATE_SET
1229 * @see #FOCUSED_STATE_SET
1230 * @see #SELECTED_STATE_SET
1231 * @see #WINDOW_FOCUSED_STATE_SET
1232 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001233 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 /**
1235 * Indicates the view is pressed and enabled.
1236 *
1237 * @see #PRESSED_STATE_SET
1238 * @see #ENABLED_STATE_SET
1239 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001240 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 /**
1242 * Indicates the view is pressed, enabled and its window has the focus.
1243 *
1244 * @see #PRESSED_STATE_SET
1245 * @see #ENABLED_STATE_SET
1246 * @see #WINDOW_FOCUSED_STATE_SET
1247 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001248 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 /**
1250 * Indicates the view is pressed, enabled and selected.
1251 *
1252 * @see #PRESSED_STATE_SET
1253 * @see #ENABLED_STATE_SET
1254 * @see #SELECTED_STATE_SET
1255 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001256 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 /**
1258 * Indicates the view is pressed, enabled, selected and its window has the
1259 * focus.
1260 *
1261 * @see #PRESSED_STATE_SET
1262 * @see #ENABLED_STATE_SET
1263 * @see #SELECTED_STATE_SET
1264 * @see #WINDOW_FOCUSED_STATE_SET
1265 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001266 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 /**
1268 * Indicates the view is pressed, enabled and focused.
1269 *
1270 * @see #PRESSED_STATE_SET
1271 * @see #ENABLED_STATE_SET
1272 * @see #FOCUSED_STATE_SET
1273 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001274 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275 /**
1276 * Indicates the view is pressed, enabled, focused and its window has the
1277 * focus.
1278 *
1279 * @see #PRESSED_STATE_SET
1280 * @see #ENABLED_STATE_SET
1281 * @see #FOCUSED_STATE_SET
1282 * @see #WINDOW_FOCUSED_STATE_SET
1283 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001284 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 /**
1286 * Indicates the view is pressed, enabled, focused and selected.
1287 *
1288 * @see #PRESSED_STATE_SET
1289 * @see #ENABLED_STATE_SET
1290 * @see #SELECTED_STATE_SET
1291 * @see #FOCUSED_STATE_SET
1292 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001293 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 /**
1295 * Indicates the view is pressed, enabled, focused, selected and its window
1296 * has the focus.
1297 *
1298 * @see #PRESSED_STATE_SET
1299 * @see #ENABLED_STATE_SET
1300 * @see #SELECTED_STATE_SET
1301 * @see #FOCUSED_STATE_SET
1302 * @see #WINDOW_FOCUSED_STATE_SET
1303 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001304 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305
1306 /**
1307 * The order here is very important to {@link #getDrawableState()}
1308 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001309 private static final int[][] VIEW_STATE_SETS;
1310
Romain Guyb051e892010-09-28 19:09:36 -07001311 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1312 static final int VIEW_STATE_SELECTED = 1 << 1;
1313 static final int VIEW_STATE_FOCUSED = 1 << 2;
1314 static final int VIEW_STATE_ENABLED = 1 << 3;
1315 static final int VIEW_STATE_PRESSED = 1 << 4;
1316 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001317 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001318 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001319 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1320 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001321
1322 static final int[] VIEW_STATE_IDS = new int[] {
1323 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1324 R.attr.state_selected, VIEW_STATE_SELECTED,
1325 R.attr.state_focused, VIEW_STATE_FOCUSED,
1326 R.attr.state_enabled, VIEW_STATE_ENABLED,
1327 R.attr.state_pressed, VIEW_STATE_PRESSED,
1328 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001329 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001330 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001331 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
1332 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 };
1334
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001335 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001336 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1337 throw new IllegalStateException(
1338 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1339 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001340 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001341 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001342 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001343 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001344 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001345 orderedIds[i * 2] = viewState;
1346 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001347 }
1348 }
1349 }
Romain Guyb051e892010-09-28 19:09:36 -07001350 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1351 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1352 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001353 int numBits = Integer.bitCount(i);
1354 int[] set = new int[numBits];
1355 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001356 for (int j = 0; j < orderedIds.length; j += 2) {
1357 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001358 set[pos++] = orderedIds[j];
1359 }
1360 }
1361 VIEW_STATE_SETS[i] = set;
1362 }
1363
1364 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1365 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1366 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1367 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1368 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1369 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1370 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1371 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1372 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1373 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1374 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1375 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1376 | VIEW_STATE_FOCUSED];
1377 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1378 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1379 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1380 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1381 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1382 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1383 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1384 | VIEW_STATE_ENABLED];
1385 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1386 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1387 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1388 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1389 | VIEW_STATE_ENABLED];
1390 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1391 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1392 | VIEW_STATE_ENABLED];
1393 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1394 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1395 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1396
1397 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1398 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1399 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1400 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1401 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1402 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1403 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1404 | VIEW_STATE_PRESSED];
1405 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1406 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1407 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1408 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1409 | VIEW_STATE_PRESSED];
1410 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1411 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1412 | VIEW_STATE_PRESSED];
1413 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1414 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1415 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1416 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1417 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1418 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1419 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1420 | VIEW_STATE_PRESSED];
1421 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1422 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1423 | VIEW_STATE_PRESSED];
1424 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1425 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1426 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1427 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1428 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1429 | VIEW_STATE_PRESSED];
1430 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1431 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1432 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1433 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1434 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1435 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1436 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1437 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1438 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1439 | VIEW_STATE_PRESSED];
1440 }
1441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001443 * Accessibility event types that are dispatched for text population.
1444 */
1445 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1446 AccessibilityEvent.TYPE_VIEW_CLICKED
1447 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1448 | AccessibilityEvent.TYPE_VIEW_SELECTED
1449 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1450 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1451 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001452 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
Svetoslav Ganov84dd52e2011-11-18 10:24:00 -08001453 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
1454 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001455
1456 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001457 * Temporary Rect currently for use in setBackground(). This will probably
1458 * be extended in the future to hold our own class with more than just
1459 * a Rect. :)
1460 */
1461 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001462
1463 /**
Chet Haasea1cff502012-02-21 13:43:44 -08001464 * Temporary flag, used to enable processing of View properties in the native DisplayList
1465 * object instead of during draw(). Soon to be enabled by default for hardware-accelerated
1466 * apps.
1467 * @hide
1468 */
Chet Haase8d56b0e2012-04-02 16:34:48 -07001469 public static final boolean USE_DISPLAY_LIST_PROPERTIES = true;
Chet Haasea1cff502012-02-21 13:43:44 -08001470
1471 /**
Romain Guyd90a3312009-05-06 14:54:28 -07001472 * Map used to store views' tags.
1473 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001474 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001476 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001477 * The next available accessiiblity id.
1478 */
1479 private static int sNextAccessibilityViewId;
1480
1481 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 * The animation currently associated with this view.
1483 * @hide
1484 */
1485 protected Animation mCurrentAnimation = null;
1486
1487 /**
1488 * Width as measured during measure pass.
1489 * {@hide}
1490 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001491 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001492 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001493
1494 /**
1495 * Height as measured during measure pass.
1496 * {@hide}
1497 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001498 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001499 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500
1501 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001502 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1503 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1504 * its display list. This flag, used only when hw accelerated, allows us to clear the
1505 * flag while retaining this information until it's needed (at getDisplayList() time and
1506 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1507 *
1508 * {@hide}
1509 */
1510 boolean mRecreateDisplayList = false;
1511
1512 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001513 * The view's identifier.
1514 * {@hide}
1515 *
1516 * @see #setId(int)
1517 * @see #getId()
1518 */
1519 @ViewDebug.ExportedProperty(resolveId = true)
1520 int mID = NO_ID;
1521
1522 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001523 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001524 */
1525 int mAccessibilityViewId = NO_ID;
1526
1527 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 * The view's tag.
1529 * {@hide}
1530 *
1531 * @see #setTag(Object)
1532 * @see #getTag()
1533 */
1534 protected Object mTag;
1535
1536 // for mPrivateFlags:
1537 /** {@hide} */
1538 static final int WANTS_FOCUS = 0x00000001;
1539 /** {@hide} */
1540 static final int FOCUSED = 0x00000002;
1541 /** {@hide} */
1542 static final int SELECTED = 0x00000004;
1543 /** {@hide} */
1544 static final int IS_ROOT_NAMESPACE = 0x00000008;
1545 /** {@hide} */
1546 static final int HAS_BOUNDS = 0x00000010;
1547 /** {@hide} */
1548 static final int DRAWN = 0x00000020;
1549 /**
1550 * When this flag is set, this view is running an animation on behalf of its
1551 * children and should therefore not cancel invalidate requests, even if they
1552 * lie outside of this view's bounds.
1553 *
1554 * {@hide}
1555 */
1556 static final int DRAW_ANIMATION = 0x00000040;
1557 /** {@hide} */
1558 static final int SKIP_DRAW = 0x00000080;
1559 /** {@hide} */
1560 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1561 /** {@hide} */
1562 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1563 /** {@hide} */
1564 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1565 /** {@hide} */
1566 static final int MEASURED_DIMENSION_SET = 0x00000800;
1567 /** {@hide} */
1568 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001569 /** {@hide} */
1570 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001571
1572 private static final int PRESSED = 0x00004000;
1573
1574 /** {@hide} */
1575 static final int DRAWING_CACHE_VALID = 0x00008000;
1576 /**
1577 * Flag used to indicate that this view should be drawn once more (and only once
1578 * more) after its animation has completed.
1579 * {@hide}
1580 */
1581 static final int ANIMATION_STARTED = 0x00010000;
1582
1583 private static final int SAVE_STATE_CALLED = 0x00020000;
1584
1585 /**
1586 * Indicates that the View returned true when onSetAlpha() was called and that
1587 * the alpha must be restored.
1588 * {@hide}
1589 */
1590 static final int ALPHA_SET = 0x00040000;
1591
1592 /**
1593 * Set by {@link #setScrollContainer(boolean)}.
1594 */
1595 static final int SCROLL_CONTAINER = 0x00080000;
1596
1597 /**
1598 * Set by {@link #setScrollContainer(boolean)}.
1599 */
1600 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1601
1602 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001603 * View flag indicating whether this view was invalidated (fully or partially.)
1604 *
1605 * @hide
1606 */
1607 static final int DIRTY = 0x00200000;
1608
1609 /**
1610 * View flag indicating whether this view was invalidated by an opaque
1611 * invalidate request.
1612 *
1613 * @hide
1614 */
1615 static final int DIRTY_OPAQUE = 0x00400000;
1616
1617 /**
1618 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1619 *
1620 * @hide
1621 */
1622 static final int DIRTY_MASK = 0x00600000;
1623
1624 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001625 * Indicates whether the background is opaque.
1626 *
1627 * @hide
1628 */
1629 static final int OPAQUE_BACKGROUND = 0x00800000;
1630
1631 /**
1632 * Indicates whether the scrollbars are opaque.
1633 *
1634 * @hide
1635 */
1636 static final int OPAQUE_SCROLLBARS = 0x01000000;
1637
1638 /**
1639 * Indicates whether the view is opaque.
1640 *
1641 * @hide
1642 */
1643 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001644
Adam Powelle14579b2009-12-16 18:39:52 -08001645 /**
1646 * Indicates a prepressed state;
1647 * the short time between ACTION_DOWN and recognizing
1648 * a 'real' press. Prepressed is used to recognize quick taps
1649 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001650 *
Adam Powelle14579b2009-12-16 18:39:52 -08001651 * @hide
1652 */
1653 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001654
Adam Powellc9fbaab2010-02-16 17:16:19 -08001655 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001656 * Indicates whether the view is temporarily detached.
1657 *
1658 * @hide
1659 */
1660 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001661
Adam Powell8568c3a2010-04-19 14:26:11 -07001662 /**
1663 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001664 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001665 * @hide
1666 */
1667 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001668
1669 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001670 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1671 * @hide
1672 */
1673 private static final int HOVERED = 0x10000000;
1674
1675 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001676 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1677 * for transform operations
1678 *
1679 * @hide
1680 */
Adam Powellf37df072010-09-17 16:22:49 -07001681 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001682
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001683 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001684 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001685
Chet Haasefd2b0022010-08-06 13:08:56 -07001686 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001687 * Indicates that this view was specifically invalidated, not just dirtied because some
1688 * child view was invalidated. The flag is used to determine when we need to recreate
1689 * a view's display list (as opposed to just returning a reference to its existing
1690 * display list).
1691 *
1692 * @hide
1693 */
1694 static final int INVALIDATED = 0x80000000;
1695
Christopher Tate3d4bf172011-03-28 16:16:46 -07001696 /* Masks for mPrivateFlags2 */
1697
1698 /**
1699 * Indicates that this view has reported that it can accept the current drag's content.
1700 * Cleared when the drag operation concludes.
1701 * @hide
1702 */
1703 static final int DRAG_CAN_ACCEPT = 0x00000001;
1704
1705 /**
1706 * Indicates that this view is currently directly under the drag location in a
1707 * drag-and-drop operation involving content that it can accept. Cleared when
1708 * the drag exits the view, or when the drag operation concludes.
1709 * @hide
1710 */
1711 static final int DRAG_HOVERED = 0x00000002;
1712
Cibu Johny86666632010-02-22 13:01:02 -08001713 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001714 * Horizontal layout direction of this view is from Left to Right.
1715 * Use with {@link #setLayoutDirection}.
Cibu Johny86666632010-02-22 13:01:02 -08001716 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001717 public static final int LAYOUT_DIRECTION_LTR = 0;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001718
1719 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001720 * Horizontal layout direction of this view is from Right to Left.
1721 * Use with {@link #setLayoutDirection}.
1722 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001723 public static final int LAYOUT_DIRECTION_RTL = 1;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001724
1725 /**
1726 * Horizontal layout direction of this view is inherited from its parent.
1727 * Use with {@link #setLayoutDirection}.
1728 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001729 public static final int LAYOUT_DIRECTION_INHERIT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001730
1731 /**
1732 * Horizontal layout direction of this view is from deduced from the default language
1733 * script for the locale. Use with {@link #setLayoutDirection}.
1734 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001735 public static final int LAYOUT_DIRECTION_LOCALE = 3;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001736
1737 /**
1738 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001739 * @hide
1740 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001741 static final int LAYOUT_DIRECTION_MASK_SHIFT = 2;
1742
1743 /**
1744 * Mask for use with private flags indicating bits used for horizontal layout direction.
1745 * @hide
1746 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001747 static final int LAYOUT_DIRECTION_MASK = 0x00000003 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001748
1749 /**
1750 * Indicates whether the view horizontal layout direction has been resolved and drawn to the
1751 * right-to-left direction.
1752 * @hide
1753 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001754 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 4 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001755
1756 /**
1757 * Indicates whether the view horizontal layout direction has been resolved.
1758 * @hide
1759 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001760 static final int LAYOUT_DIRECTION_RESOLVED = 8 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001761
1762 /**
1763 * Mask for use with private flags indicating bits used for resolved horizontal layout direction.
1764 * @hide
1765 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001766 static final int LAYOUT_DIRECTION_RESOLVED_MASK = 0x0000000C << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001767
1768 /*
1769 * Array of horizontal layout direction flags for mapping attribute "layoutDirection" to correct
1770 * flag value.
1771 * @hide
1772 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001773 private static final int[] LAYOUT_DIRECTION_FLAGS = {
1774 LAYOUT_DIRECTION_LTR,
1775 LAYOUT_DIRECTION_RTL,
1776 LAYOUT_DIRECTION_INHERIT,
1777 LAYOUT_DIRECTION_LOCALE
1778 };
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001779
1780 /**
1781 * Default horizontal layout direction.
1782 * @hide
1783 */
1784 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001785
Cibu Johny86666632010-02-22 13:01:02 -08001786
Adam Powell539ee872012-02-03 19:00:49 -08001787 /**
1788 * Indicates that the view is tracking some sort of transient state
1789 * that the app should not need to be aware of, but that the framework
1790 * should take special care to preserve.
1791 *
1792 * @hide
1793 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001794 static final int HAS_TRANSIENT_STATE = 0x00000100;
Adam Powell539ee872012-02-03 19:00:49 -08001795
1796
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001797 /**
1798 * Text direction is inherited thru {@link ViewGroup}
1799 */
1800 public static final int TEXT_DIRECTION_INHERIT = 0;
1801
1802 /**
1803 * Text direction is using "first strong algorithm". The first strong directional character
1804 * determines the paragraph direction. If there is no strong directional character, the
1805 * paragraph direction is the view's resolved layout direction.
1806 */
1807 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
1808
1809 /**
1810 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
1811 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
1812 * If there are neither, the paragraph direction is the view's resolved layout direction.
1813 */
1814 public static final int TEXT_DIRECTION_ANY_RTL = 2;
1815
1816 /**
1817 * Text direction is forced to LTR.
1818 */
1819 public static final int TEXT_DIRECTION_LTR = 3;
1820
1821 /**
1822 * Text direction is forced to RTL.
1823 */
1824 public static final int TEXT_DIRECTION_RTL = 4;
1825
1826 /**
1827 * Text direction is coming from the system Locale.
1828 */
1829 public static final int TEXT_DIRECTION_LOCALE = 5;
1830
1831 /**
1832 * Bit shift to get the horizontal layout direction. (bits after LAYOUT_DIRECTION_RESOLVED)
1833 * @hide
1834 */
1835 static final int TEXT_DIRECTION_MASK_SHIFT = 6;
1836
1837 /**
1838 * Default text direction is inherited
1839 */
1840 protected static int TEXT_DIRECTION_DEFAULT = TEXT_DIRECTION_INHERIT;
1841
1842 /**
1843 * Mask for use with private flags indicating bits used for text direction.
1844 * @hide
1845 */
1846 static final int TEXT_DIRECTION_MASK = 0x00000007 << TEXT_DIRECTION_MASK_SHIFT;
1847
1848 /**
1849 * Array of text direction flags for mapping attribute "textDirection" to correct
1850 * flag value.
1851 * @hide
1852 */
1853 private static final int[] TEXT_DIRECTION_FLAGS = {
1854 TEXT_DIRECTION_INHERIT << TEXT_DIRECTION_MASK_SHIFT,
1855 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_MASK_SHIFT,
1856 TEXT_DIRECTION_ANY_RTL << TEXT_DIRECTION_MASK_SHIFT,
1857 TEXT_DIRECTION_LTR << TEXT_DIRECTION_MASK_SHIFT,
1858 TEXT_DIRECTION_RTL << TEXT_DIRECTION_MASK_SHIFT,
1859 TEXT_DIRECTION_LOCALE << TEXT_DIRECTION_MASK_SHIFT
1860 };
1861
1862 /**
1863 * Indicates whether the view text direction has been resolved.
1864 * @hide
1865 */
1866 static final int TEXT_DIRECTION_RESOLVED = 0x00000008 << TEXT_DIRECTION_MASK_SHIFT;
1867
1868 /**
1869 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1870 * @hide
1871 */
1872 static final int TEXT_DIRECTION_RESOLVED_MASK_SHIFT = 10;
1873
1874 /**
1875 * Mask for use with private flags indicating bits used for resolved text direction.
1876 * @hide
1877 */
1878 static final int TEXT_DIRECTION_RESOLVED_MASK = 0x00000007 << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1879
1880 /**
1881 * Indicates whether the view text direction has been resolved to the "first strong" heuristic.
1882 * @hide
1883 */
1884 static final int TEXT_DIRECTION_RESOLVED_DEFAULT =
1885 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1886
1887
Christopher Tate3d4bf172011-03-28 16:16:46 -07001888 /* End of masks for mPrivateFlags2 */
1889
1890 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
1891
Chet Haasedaf98e92011-01-10 14:10:36 -08001892 /**
Adam Powell637d3372010-08-25 14:37:03 -07001893 * Always allow a user to over-scroll this view, provided it is a
1894 * view that can scroll.
1895 *
1896 * @see #getOverScrollMode()
1897 * @see #setOverScrollMode(int)
1898 */
1899 public static final int OVER_SCROLL_ALWAYS = 0;
1900
1901 /**
1902 * Allow a user to over-scroll this view only if the content is large
1903 * enough to meaningfully scroll, provided it is a view that can scroll.
1904 *
1905 * @see #getOverScrollMode()
1906 * @see #setOverScrollMode(int)
1907 */
1908 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
1909
1910 /**
1911 * Never allow a user to over-scroll this view.
1912 *
1913 * @see #getOverScrollMode()
1914 * @see #setOverScrollMode(int)
1915 */
1916 public static final int OVER_SCROLL_NEVER = 2;
1917
1918 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001919 * Special constant for {@link #setSystemUiVisibility(int)}: View has
1920 * requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08001921 *
Joe Malin32736f02011-01-19 16:14:20 -08001922 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001923 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001924 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08001925
1926 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001927 * Flag for {@link #setSystemUiVisibility(int)}: View has requested the
1928 * system UI to enter an unobtrusive "low profile" mode.
Daniel Sandler60ee2562011-07-22 12:34:33 -04001929 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001930 * <p>This is for use in games, book readers, video players, or any other
Philip Milne6c8ea062012-04-03 17:38:43 -07001931 * "immersive" application where the usual system chrome is deemed too distracting.
Daniel Sandler60ee2562011-07-22 12:34:33 -04001932 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001933 * <p>In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08001934 *
Joe Malin32736f02011-01-19 16:14:20 -08001935 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001936 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001937 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
1938
1939 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001940 * Flag for {@link #setSystemUiVisibility(int)}: View has requested that the
1941 * system navigation be temporarily hidden.
Daniel Sandler60ee2562011-07-22 12:34:33 -04001942 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001943 * <p>This is an even less obtrusive state than that called for by
Daniel Sandler60ee2562011-07-22 12:34:33 -04001944 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
1945 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
1946 * those to disappear. This is useful (in conjunction with the
Philip Milne6c8ea062012-04-03 17:38:43 -07001947 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
Daniel Sandler60ee2562011-07-22 12:34:33 -04001948 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
1949 * window flags) for displaying content using every last pixel on the display.
1950 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001951 * <p>There is a limitation: because navigation controls are so important, the least user
1952 * interaction will cause them to reappear immediately. When this happens, both
1953 * this flag and {@link #SYSTEM_UI_FLAG_FULLSCREEN} will be cleared automatically,
1954 * so that both elements reappear at the same time.
Daniel Sandler60ee2562011-07-22 12:34:33 -04001955 *
1956 * @see #setSystemUiVisibility(int)
1957 */
1958 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
1959
1960 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07001961 * Flag for {@link #setSystemUiVisibility(int)}: View has requested to go
1962 * into the normal fullscreen mode so that its content can take over the screen
1963 * while still allowing the user to interact with the application.
1964 *
1965 * <p>This has the same visual effect as
1966 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN
1967 * WindowManager.LayoutParams.FLAG_FULLSCREEN},
1968 * meaning that non-critical screen decorations (such as the status bar) will be
1969 * hidden while the user is in the View's window, focusing the experience on
1970 * that content. Unlike the window flag, if you are using ActionBar in
1971 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
1972 * Window.FEATURE_ACTION_BAR_OVERLAY}, then enabling this flag will also
1973 * hide the action bar.
1974 *
1975 * <p>This approach to going fullscreen is best used over the window flag when
1976 * it is a transient state -- that is, the application does this at certain
1977 * points in its user interaction where it wants to allow the user to focus
1978 * on content, but not as a continuous state. For situations where the application
1979 * would like to simply stay full screen the entire time (such as a game that
1980 * wants to take over the screen), the
1981 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN window flag}
1982 * is usually a better approach. The state set here will be removed by the system
1983 * in various situations (such as the user moving to another application) like
1984 * the other system UI states.
1985 *
1986 * <p>When using this flag, the application should provide some easy facility
1987 * for the user to go out of it. A common example would be in an e-book
1988 * reader, where tapping on the screen brings back whatever screen and UI
1989 * decorations that had been hidden while the user was immersed in reading
1990 * the book.
1991 *
1992 * @see #setSystemUiVisibility(int)
1993 */
1994 public static final int SYSTEM_UI_FLAG_FULLSCREEN = 0x00000004;
1995
1996 /**
1997 * Flag for {@link #setSystemUiVisibility(int)}: When using other layout
1998 * flags, we would like a stable view of the content insets given to
1999 * {@link #fitSystemWindows(Rect)}. This means that the insets seen there
2000 * will always represent the worst case that the application can expect
2001 * as a continue state. In practice this means with any of system bar,
2002 * nav bar, and status bar shown, but not the space that would be needed
2003 * for an input method.
2004 *
2005 * <p>If you are using ActionBar in
2006 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2007 * Window.FEATURE_ACTION_BAR_OVERLAY}, this flag will also impact the
2008 * insets it adds to those given to the application.
2009 */
2010 public static final int SYSTEM_UI_FLAG_LAYOUT_STABLE = 0x00000100;
2011
2012 /**
2013 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2014 * to be layed out as if it has requested
2015 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, even if it currently hasn't. This
2016 * allows it to avoid artifacts when switching in and out of that mode, at
2017 * the expense that some of its user interface may be covered by screen
2018 * decorations when they are shown. You can perform layout of your inner
2019 * UI elements to account for the navagation system UI through the
2020 * {@link #fitSystemWindows(Rect)} method.
2021 */
2022 public static final int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 0x00000200;
2023
2024 /**
2025 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2026 * to be layed out as if it has requested
2027 * {@link #SYSTEM_UI_FLAG_FULLSCREEN}, even if it currently hasn't. This
2028 * allows it to avoid artifacts when switching in and out of that mode, at
2029 * the expense that some of its user interface may be covered by screen
2030 * decorations when they are shown. You can perform layout of your inner
2031 * UI elements to account for non-fullscreen system UI through the
2032 * {@link #fitSystemWindows(Rect)} method.
2033 */
2034 public static final int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 0x00000400;
2035
2036 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04002037 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
2038 */
2039 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
2040
2041 /**
2042 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
2043 */
2044 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08002045
2046 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002047 * @hide
2048 *
2049 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2050 * out of the public fields to keep the undefined bits out of the developer's way.
2051 *
2052 * Flag to make the status bar not expandable. Unless you also
2053 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
2054 */
2055 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
2056
2057 /**
2058 * @hide
2059 *
2060 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2061 * out of the public fields to keep the undefined bits out of the developer's way.
2062 *
2063 * Flag to hide notification icons and scrolling ticker text.
2064 */
2065 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
2066
2067 /**
2068 * @hide
2069 *
2070 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2071 * out of the public fields to keep the undefined bits out of the developer's way.
2072 *
2073 * Flag to disable incoming notification alerts. This will not block
2074 * icons, but it will block sound, vibrating and other visual or aural notifications.
2075 */
2076 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
2077
2078 /**
2079 * @hide
2080 *
2081 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2082 * out of the public fields to keep the undefined bits out of the developer's way.
2083 *
2084 * Flag to hide only the scrolling ticker. Note that
2085 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
2086 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
2087 */
2088 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
2089
2090 /**
2091 * @hide
2092 *
2093 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2094 * out of the public fields to keep the undefined bits out of the developer's way.
2095 *
2096 * Flag to hide the center system info area.
2097 */
2098 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
2099
2100 /**
2101 * @hide
2102 *
2103 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2104 * out of the public fields to keep the undefined bits out of the developer's way.
2105 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002106 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002107 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2108 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04002109 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002110
2111 /**
2112 * @hide
2113 *
2114 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2115 * out of the public fields to keep the undefined bits out of the developer's way.
2116 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002117 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08002118 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2119 */
2120 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
2121
2122 /**
2123 * @hide
2124 *
2125 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2126 * out of the public fields to keep the undefined bits out of the developer's way.
2127 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002128 * Flag to hide only the clock. You might use this if your activity has
2129 * its own clock making the status bar's clock redundant.
2130 */
Joe Onorato6478adc2011-01-27 21:15:01 -08002131 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
2132
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002133 /**
2134 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04002135 *
2136 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2137 * out of the public fields to keep the undefined bits out of the developer's way.
2138 *
2139 * Flag to hide only the recent apps button. Don't use this
2140 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2141 */
2142 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
2143
2144 /**
2145 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002146 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002147 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002148
2149 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002150 * These are the system UI flags that can be cleared by events outside
2151 * of an application. Currently this is just the ability to tap on the
2152 * screen while hiding the navigation bar to have it return.
2153 * @hide
2154 */
2155 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002156 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION
2157 | SYSTEM_UI_FLAG_FULLSCREEN;
2158
2159 /**
2160 * Flags that can impact the layout in relation to system UI.
2161 */
2162 public static final int SYSTEM_UI_LAYOUT_FLAGS =
2163 SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
2164 | SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002165
2166 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07002167 * Find views that render the specified text.
2168 *
2169 * @see #findViewsWithText(ArrayList, CharSequence, int)
2170 */
2171 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
2172
2173 /**
2174 * Find find views that contain the specified content description.
2175 *
2176 * @see #findViewsWithText(ArrayList, CharSequence, int)
2177 */
2178 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
2179
2180 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07002181 * Find views that contain {@link AccessibilityNodeProvider}. Such
2182 * a View is a root of virtual view hierarchy and may contain the searched
2183 * text. If this flag is set Views with providers are automatically
2184 * added and it is a responsibility of the client to call the APIs of
2185 * the provider to determine whether the virtual tree rooted at this View
2186 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
2187 * represeting the virtual views with this text.
2188 *
2189 * @see #findViewsWithText(ArrayList, CharSequence, int)
2190 *
2191 * @hide
2192 */
2193 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
2194
2195 /**
Romain Guybb9908b2012-03-08 11:14:07 -08002196 * Indicates that the screen has changed state and is now off.
2197 *
2198 * @see #onScreenStateChanged(int)
2199 */
2200 public static final int SCREEN_STATE_OFF = 0x0;
2201
2202 /**
2203 * Indicates that the screen has changed state and is now on.
2204 *
Romain Guy1e3d3132012-03-08 15:55:56 -08002205 * @see #onScreenStateChanged(int)
Romain Guybb9908b2012-03-08 11:14:07 -08002206 */
2207 public static final int SCREEN_STATE_ON = 0x1;
2208
2209 /**
Adam Powell637d3372010-08-25 14:37:03 -07002210 * Controls the over-scroll mode for this view.
2211 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2212 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2213 * and {@link #OVER_SCROLL_NEVER}.
2214 */
2215 private int mOverScrollMode;
2216
2217 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002218 * The parent this view is attached to.
2219 * {@hide}
2220 *
2221 * @see #getParent()
2222 */
2223 protected ViewParent mParent;
2224
2225 /**
2226 * {@hide}
2227 */
2228 AttachInfo mAttachInfo;
2229
2230 /**
2231 * {@hide}
2232 */
Romain Guy809a7f62009-05-14 15:44:42 -07002233 @ViewDebug.ExportedProperty(flagMapping = {
2234 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
2235 name = "FORCE_LAYOUT"),
2236 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
2237 name = "LAYOUT_REQUIRED"),
2238 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002239 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07002240 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
2241 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
2242 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2243 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
2244 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002245 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002246 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002247
2248 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002249 * This view's request for the visibility of the status bar.
2250 * @hide
2251 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002252 @ViewDebug.ExportedProperty(flagMapping = {
2253 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2254 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2255 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2256 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2257 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2258 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2259 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2260 equals = SYSTEM_UI_FLAG_VISIBLE,
2261 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2262 })
Joe Onorato664644d2011-01-23 17:53:23 -08002263 int mSystemUiVisibility;
2264
2265 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002266 * Count of how many windows this view has been attached to.
2267 */
2268 int mWindowAttachCount;
2269
2270 /**
2271 * The layout parameters associated with this view and used by the parent
2272 * {@link android.view.ViewGroup} to determine how this view should be
2273 * laid out.
2274 * {@hide}
2275 */
2276 protected ViewGroup.LayoutParams mLayoutParams;
2277
2278 /**
2279 * The view flags hold various views states.
2280 * {@hide}
2281 */
2282 @ViewDebug.ExportedProperty
2283 int mViewFlags;
2284
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002285 static class TransformationInfo {
2286 /**
2287 * The transform matrix for the View. This transform is calculated internally
2288 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2289 * is used by default. Do *not* use this variable directly; instead call
2290 * getMatrix(), which will automatically recalculate the matrix if necessary
2291 * to get the correct matrix based on the latest rotation and scale properties.
2292 */
2293 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002294
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002295 /**
2296 * The transform matrix for the View. This transform is calculated internally
2297 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2298 * is used by default. Do *not* use this variable directly; instead call
2299 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2300 * to get the correct matrix based on the latest rotation and scale properties.
2301 */
2302 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002303
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002304 /**
2305 * An internal variable that tracks whether we need to recalculate the
2306 * transform matrix, based on whether the rotation or scaleX/Y properties
2307 * have changed since the matrix was last calculated.
2308 */
2309 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002310
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002311 /**
2312 * An internal variable that tracks whether we need to recalculate the
2313 * transform matrix, based on whether the rotation or scaleX/Y properties
2314 * have changed since the matrix was last calculated.
2315 */
2316 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002317
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002318 /**
2319 * A variable that tracks whether we need to recalculate the
2320 * transform matrix, based on whether the rotation or scaleX/Y properties
2321 * have changed since the matrix was last calculated. This variable
2322 * is only valid after a call to updateMatrix() or to a function that
2323 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2324 */
2325 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002326
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002327 /**
2328 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2329 */
2330 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002331
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002332 /**
2333 * This matrix is used when computing the matrix for 3D rotations.
2334 */
2335 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002336
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002337 /**
2338 * These prev values are used to recalculate a centered pivot point when necessary. The
2339 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2340 * set), so thes values are only used then as well.
2341 */
2342 private int mPrevWidth = -1;
2343 private int mPrevHeight = -1;
Philip Milne6c8ea062012-04-03 17:38:43 -07002344
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002345 /**
2346 * The degrees rotation around the vertical axis through the pivot point.
2347 */
2348 @ViewDebug.ExportedProperty
2349 float mRotationY = 0f;
2350
2351 /**
2352 * The degrees rotation around the horizontal axis through the pivot point.
2353 */
2354 @ViewDebug.ExportedProperty
2355 float mRotationX = 0f;
2356
2357 /**
2358 * The degrees rotation around the pivot point.
2359 */
2360 @ViewDebug.ExportedProperty
2361 float mRotation = 0f;
2362
2363 /**
2364 * The amount of translation of the object away from its left property (post-layout).
2365 */
2366 @ViewDebug.ExportedProperty
2367 float mTranslationX = 0f;
2368
2369 /**
2370 * The amount of translation of the object away from its top property (post-layout).
2371 */
2372 @ViewDebug.ExportedProperty
2373 float mTranslationY = 0f;
2374
2375 /**
2376 * The amount of scale in the x direction around the pivot point. A
2377 * value of 1 means no scaling is applied.
2378 */
2379 @ViewDebug.ExportedProperty
2380 float mScaleX = 1f;
2381
2382 /**
2383 * The amount of scale in the y direction around the pivot point. A
2384 * value of 1 means no scaling is applied.
2385 */
2386 @ViewDebug.ExportedProperty
2387 float mScaleY = 1f;
2388
2389 /**
Chet Haasea33de552012-02-03 16:28:24 -08002390 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002391 */
2392 @ViewDebug.ExportedProperty
2393 float mPivotX = 0f;
2394
2395 /**
Chet Haasea33de552012-02-03 16:28:24 -08002396 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002397 */
2398 @ViewDebug.ExportedProperty
2399 float mPivotY = 0f;
2400
2401 /**
2402 * The opacity of the View. This is a value from 0 to 1, where 0 means
2403 * completely transparent and 1 means completely opaque.
2404 */
2405 @ViewDebug.ExportedProperty
2406 float mAlpha = 1f;
2407 }
2408
2409 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002410
Joe Malin32736f02011-01-19 16:14:20 -08002411 private boolean mLastIsOpaque;
2412
Chet Haasefd2b0022010-08-06 13:08:56 -07002413 /**
2414 * Convenience value to check for float values that are close enough to zero to be considered
2415 * zero.
2416 */
Romain Guy2542d192010-08-18 11:47:12 -07002417 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002418
2419 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002420 * The distance in pixels from the left edge of this view's parent
2421 * to the left edge of this view.
2422 * {@hide}
2423 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002424 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002425 protected int mLeft;
2426 /**
2427 * The distance in pixels from the left edge of this view's parent
2428 * to the right edge of this view.
2429 * {@hide}
2430 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002431 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002432 protected int mRight;
2433 /**
2434 * The distance in pixels from the top edge of this view's parent
2435 * to the top edge of this view.
2436 * {@hide}
2437 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002438 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002439 protected int mTop;
2440 /**
2441 * The distance in pixels from the top edge of this view's parent
2442 * to the bottom edge of this view.
2443 * {@hide}
2444 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002445 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002446 protected int mBottom;
2447
2448 /**
2449 * The offset, in pixels, by which the content of this view is scrolled
2450 * horizontally.
2451 * {@hide}
2452 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002453 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002454 protected int mScrollX;
2455 /**
2456 * The offset, in pixels, by which the content of this view is scrolled
2457 * vertically.
2458 * {@hide}
2459 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002460 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002461 protected int mScrollY;
2462
2463 /**
2464 * The left padding in pixels, that is the distance in pixels between the
2465 * left edge of this view and the left edge of its content.
2466 * {@hide}
2467 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002468 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002469 protected int mPaddingLeft;
2470 /**
2471 * The right padding in pixels, that is the distance in pixels between the
2472 * right edge of this view and the right edge of its content.
2473 * {@hide}
2474 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002475 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002476 protected int mPaddingRight;
2477 /**
2478 * The top padding in pixels, that is the distance in pixels between the
2479 * top edge of this view and the top edge of its content.
2480 * {@hide}
2481 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002482 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002483 protected int mPaddingTop;
2484 /**
2485 * The bottom padding in pixels, that is the distance in pixels between the
2486 * bottom edge of this view and the bottom edge of its content.
2487 * {@hide}
2488 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002489 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002490 protected int mPaddingBottom;
2491
2492 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002493 * Briefly describes the view and is primarily used for accessibility support.
2494 */
2495 private CharSequence mContentDescription;
2496
2497 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002498 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002499 *
2500 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002501 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002502 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002503 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002504
2505 /**
2506 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002507 *
2508 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002509 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002510 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002511 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002512
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002513 /**
Adam Powell20232d02010-12-08 21:08:53 -08002514 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002515 *
2516 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002517 */
2518 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002519 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002520
2521 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002522 * Cache if the user padding is relative.
2523 *
2524 */
2525 @ViewDebug.ExportedProperty(category = "padding")
2526 boolean mUserPaddingRelative;
2527
2528 /**
2529 * Cache the paddingStart set by the user to append to the scrollbar's size.
2530 *
2531 */
2532 @ViewDebug.ExportedProperty(category = "padding")
2533 int mUserPaddingStart;
2534
2535 /**
2536 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2537 *
2538 */
2539 @ViewDebug.ExportedProperty(category = "padding")
2540 int mUserPaddingEnd;
2541
2542 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002543 * @hide
2544 */
2545 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2546 /**
2547 * @hide
2548 */
2549 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002550
Philip Milne6c8ea062012-04-03 17:38:43 -07002551 private Drawable mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002552
2553 private int mBackgroundResource;
2554 private boolean mBackgroundSizeChanged;
2555
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002556 static class ListenerInfo {
2557 /**
2558 * Listener used to dispatch focus change events.
2559 * This field should be made private, so it is hidden from the SDK.
2560 * {@hide}
2561 */
2562 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002563
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002564 /**
2565 * Listeners for layout change events.
2566 */
2567 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002568
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002569 /**
2570 * Listeners for attach events.
2571 */
2572 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002573
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002574 /**
2575 * Listener used to dispatch click events.
2576 * This field should be made private, so it is hidden from the SDK.
2577 * {@hide}
2578 */
2579 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002580
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002581 /**
2582 * Listener used to dispatch long click events.
2583 * This field should be made private, so it is hidden from the SDK.
2584 * {@hide}
2585 */
2586 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002587
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002588 /**
2589 * Listener used to build the context menu.
2590 * This field should be made private, so it is hidden from the SDK.
2591 * {@hide}
2592 */
2593 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002594
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002595 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002596
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002597 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002598
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002599 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002600
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002601 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002602
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002603 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002604
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002605 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2606 }
2607
2608 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002609
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002610 /**
2611 * The application environment this view lives in.
2612 * This field should be made private, so it is hidden from the SDK.
2613 * {@hide}
2614 */
2615 protected Context mContext;
2616
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002617 private final Resources mResources;
2618
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002619 private ScrollabilityCache mScrollCache;
2620
2621 private int[] mDrawableState = null;
2622
Romain Guy0211a0a2011-02-14 16:34:59 -08002623 /**
2624 * Set to true when drawing cache is enabled and cannot be created.
Philip Milne6c8ea062012-04-03 17:38:43 -07002625 *
Romain Guy0211a0a2011-02-14 16:34:59 -08002626 * @hide
2627 */
2628 public boolean mCachingFailed;
2629
Romain Guy02890fd2010-08-06 17:58:44 -07002630 private Bitmap mDrawingCache;
2631 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002632 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002633 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002634
2635 /**
2636 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2637 * the user may specify which view to go to next.
2638 */
2639 private int mNextFocusLeftId = View.NO_ID;
2640
2641 /**
2642 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2643 * the user may specify which view to go to next.
2644 */
2645 private int mNextFocusRightId = View.NO_ID;
2646
2647 /**
2648 * When this view has focus and the next focus is {@link #FOCUS_UP},
2649 * the user may specify which view to go to next.
2650 */
2651 private int mNextFocusUpId = View.NO_ID;
2652
2653 /**
2654 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2655 * the user may specify which view to go to next.
2656 */
2657 private int mNextFocusDownId = View.NO_ID;
2658
Jeff Brown4e6319b2010-12-13 10:36:51 -08002659 /**
2660 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2661 * the user may specify which view to go to next.
2662 */
2663 int mNextFocusForwardId = View.NO_ID;
2664
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002665 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002666 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002667 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002668 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002670 private UnsetPressedState mUnsetPressedState;
2671
2672 /**
2673 * Whether the long press's action has been invoked. The tap's action is invoked on the
2674 * up event while a long press is invoked as soon as the long press duration is reached, so
2675 * a long press could be performed before the tap is checked, in which case the tap's action
2676 * should not be invoked.
2677 */
2678 private boolean mHasPerformedLongPress;
2679
2680 /**
2681 * The minimum height of the view. We'll try our best to have the height
2682 * of this view to at least this amount.
2683 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002684 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002685 private int mMinHeight;
2686
2687 /**
2688 * The minimum width of the view. We'll try our best to have the width
2689 * of this view to at least this amount.
2690 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002691 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002692 private int mMinWidth;
2693
2694 /**
2695 * The delegate to handle touch events that are physically in this view
2696 * but should be handled by another view.
2697 */
2698 private TouchDelegate mTouchDelegate = null;
2699
2700 /**
2701 * Solid color to use as a background when creating the drawing cache. Enables
2702 * the cache to use 16 bit bitmaps instead of 32 bit.
2703 */
2704 private int mDrawingCacheBackgroundColor = 0;
2705
2706 /**
2707 * Special tree observer used when mAttachInfo is null.
2708 */
2709 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002710
Adam Powelle14579b2009-12-16 18:39:52 -08002711 /**
2712 * Cache the touch slop from the context that created the view.
2713 */
2714 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002715
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002716 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002717 * Object that handles automatic animation of view properties.
2718 */
2719 private ViewPropertyAnimator mAnimator = null;
2720
2721 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002722 * Flag indicating that a drag can cross window boundaries. When
2723 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2724 * with this flag set, all visible applications will be able to participate
2725 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002726 *
2727 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002728 */
2729 public static final int DRAG_FLAG_GLOBAL = 1;
2730
2731 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002732 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2733 */
2734 private float mVerticalScrollFactor;
2735
2736 /**
Adam Powell20232d02010-12-08 21:08:53 -08002737 * Position of the vertical scroll bar.
2738 */
2739 private int mVerticalScrollbarPosition;
2740
2741 /**
2742 * Position the scroll bar at the default position as determined by the system.
2743 */
2744 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2745
2746 /**
2747 * Position the scroll bar along the left edge.
2748 */
2749 public static final int SCROLLBAR_POSITION_LEFT = 1;
2750
2751 /**
2752 * Position the scroll bar along the right edge.
2753 */
2754 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2755
2756 /**
Romain Guy171c5922011-01-06 10:04:23 -08002757 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002758 *
2759 * @see #getLayerType()
2760 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002761 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002762 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002763 */
2764 public static final int LAYER_TYPE_NONE = 0;
2765
2766 /**
2767 * <p>Indicates that the view has a software layer. A software layer is backed
2768 * by a bitmap and causes the view to be rendered using Android's software
2769 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002770 *
Romain Guy171c5922011-01-06 10:04:23 -08002771 * <p>Software layers have various usages:</p>
2772 * <p>When the application is not using hardware acceleration, a software layer
2773 * is useful to apply a specific color filter and/or blending mode and/or
2774 * translucency to a view and all its children.</p>
2775 * <p>When the application is using hardware acceleration, a software layer
2776 * is useful to render drawing primitives not supported by the hardware
2777 * accelerated pipeline. It can also be used to cache a complex view tree
2778 * into a texture and reduce the complexity of drawing operations. For instance,
2779 * when animating a complex view tree with a translation, a software layer can
2780 * be used to render the view tree only once.</p>
2781 * <p>Software layers should be avoided when the affected view tree updates
2782 * often. Every update will require to re-render the software layer, which can
2783 * potentially be slow (particularly when hardware acceleration is turned on
2784 * since the layer will have to be uploaded into a hardware texture after every
2785 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002786 *
2787 * @see #getLayerType()
2788 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002789 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002790 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002791 */
2792 public static final int LAYER_TYPE_SOFTWARE = 1;
2793
2794 /**
2795 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2796 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2797 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2798 * rendering pipeline, but only if hardware acceleration is turned on for the
2799 * view hierarchy. When hardware acceleration is turned off, hardware layers
2800 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002801 *
Romain Guy171c5922011-01-06 10:04:23 -08002802 * <p>A hardware layer is useful to apply a specific color filter and/or
2803 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002804 * <p>A hardware layer can be used to cache a complex view tree into a
2805 * texture and reduce the complexity of drawing operations. For instance,
2806 * when animating a complex view tree with a translation, a hardware layer can
2807 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002808 * <p>A hardware layer can also be used to increase the rendering quality when
2809 * rotation transformations are applied on a view. It can also be used to
2810 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002811 *
2812 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002813 * @see #setLayerType(int, android.graphics.Paint)
2814 * @see #LAYER_TYPE_NONE
2815 * @see #LAYER_TYPE_SOFTWARE
2816 */
2817 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002818
Romain Guy3aaff3a2011-01-12 14:18:47 -08002819 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2820 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2821 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2822 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2823 })
Romain Guy171c5922011-01-06 10:04:23 -08002824 int mLayerType = LAYER_TYPE_NONE;
2825 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002826 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002827
2828 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07002829 * Set to true when the view is sending hover accessibility events because it
2830 * is the innermost hovered view.
2831 */
2832 private boolean mSendingHoverAccessibilityEvents;
2833
2834 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002835 * Simple constructor to use when creating a view from code.
2836 *
2837 * @param context The Context the view is running in, through which it can
2838 * access the current theme, resources, etc.
2839 */
2840 public View(Context context) {
2841 mContext = context;
2842 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07002843 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07002844 // Set layout and text direction defaults
2845 mPrivateFlags2 = (LAYOUT_DIRECTION_DEFAULT << LAYOUT_DIRECTION_MASK_SHIFT) |
2846 (TEXT_DIRECTION_DEFAULT << TEXT_DIRECTION_MASK_SHIFT);
Adam Powelle14579b2009-12-16 18:39:52 -08002847 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002848 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07002849 mUserPaddingStart = -1;
2850 mUserPaddingEnd = -1;
2851 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002852 }
2853
2854 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07002855 * Delegate for injecting accessiblity functionality.
2856 */
2857 AccessibilityDelegate mAccessibilityDelegate;
2858
2859 /**
2860 * Consistency verifier for debugging purposes.
2861 * @hide
2862 */
2863 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
2864 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
2865 new InputEventConsistencyVerifier(this, 0) : null;
2866
2867 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002868 * Constructor that is called when inflating a view from XML. This is called
2869 * when a view is being constructed from an XML file, supplying attributes
2870 * that were specified in the XML file. This version uses a default style of
2871 * 0, so the only attribute values applied are those in the Context's Theme
2872 * and the given AttributeSet.
2873 *
2874 * <p>
2875 * The method onFinishInflate() will be called after all children have been
2876 * added.
2877 *
2878 * @param context The Context the view is running in, through which it can
2879 * access the current theme, resources, etc.
2880 * @param attrs The attributes of the XML tag that is inflating the view.
2881 * @see #View(Context, AttributeSet, int)
2882 */
2883 public View(Context context, AttributeSet attrs) {
2884 this(context, attrs, 0);
2885 }
2886
2887 /**
2888 * Perform inflation from XML and apply a class-specific base style. This
2889 * constructor of View allows subclasses to use their own base style when
2890 * they are inflating. For example, a Button class's constructor would call
2891 * this version of the super class constructor and supply
2892 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2893 * the theme's button style to modify all of the base view attributes (in
2894 * particular its background) as well as the Button class's attributes.
2895 *
2896 * @param context The Context the view is running in, through which it can
2897 * access the current theme, resources, etc.
2898 * @param attrs The attributes of the XML tag that is inflating the view.
2899 * @param defStyle The default style to apply to this view. If 0, no style
2900 * will be applied (beyond what is included in the theme). This may
2901 * either be an attribute resource, whose value will be retrieved
2902 * from the current theme, or an explicit style resource.
2903 * @see #View(Context, AttributeSet)
2904 */
2905 public View(Context context, AttributeSet attrs, int defStyle) {
2906 this(context);
2907
2908 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2909 defStyle, 0);
2910
2911 Drawable background = null;
2912
2913 int leftPadding = -1;
2914 int topPadding = -1;
2915 int rightPadding = -1;
2916 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002917 int startPadding = -1;
2918 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002919
2920 int padding = -1;
2921
2922 int viewFlagValues = 0;
2923 int viewFlagMasks = 0;
2924
2925 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002927 int x = 0;
2928 int y = 0;
2929
Chet Haase73066682010-11-29 15:55:32 -08002930 float tx = 0;
2931 float ty = 0;
2932 float rotation = 0;
2933 float rotationX = 0;
2934 float rotationY = 0;
2935 float sx = 1f;
2936 float sy = 1f;
2937 boolean transformSet = false;
2938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002939 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2940
Adam Powell637d3372010-08-25 14:37:03 -07002941 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002942 final int N = a.getIndexCount();
2943 for (int i = 0; i < N; i++) {
2944 int attr = a.getIndex(i);
2945 switch (attr) {
2946 case com.android.internal.R.styleable.View_background:
2947 background = a.getDrawable(attr);
2948 break;
2949 case com.android.internal.R.styleable.View_padding:
2950 padding = a.getDimensionPixelSize(attr, -1);
2951 break;
2952 case com.android.internal.R.styleable.View_paddingLeft:
2953 leftPadding = a.getDimensionPixelSize(attr, -1);
2954 break;
2955 case com.android.internal.R.styleable.View_paddingTop:
2956 topPadding = a.getDimensionPixelSize(attr, -1);
2957 break;
2958 case com.android.internal.R.styleable.View_paddingRight:
2959 rightPadding = a.getDimensionPixelSize(attr, -1);
2960 break;
2961 case com.android.internal.R.styleable.View_paddingBottom:
2962 bottomPadding = a.getDimensionPixelSize(attr, -1);
2963 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002964 case com.android.internal.R.styleable.View_paddingStart:
2965 startPadding = a.getDimensionPixelSize(attr, -1);
2966 break;
2967 case com.android.internal.R.styleable.View_paddingEnd:
2968 endPadding = a.getDimensionPixelSize(attr, -1);
2969 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002970 case com.android.internal.R.styleable.View_scrollX:
2971 x = a.getDimensionPixelOffset(attr, 0);
2972 break;
2973 case com.android.internal.R.styleable.View_scrollY:
2974 y = a.getDimensionPixelOffset(attr, 0);
2975 break;
Chet Haase73066682010-11-29 15:55:32 -08002976 case com.android.internal.R.styleable.View_alpha:
2977 setAlpha(a.getFloat(attr, 1f));
2978 break;
2979 case com.android.internal.R.styleable.View_transformPivotX:
2980 setPivotX(a.getDimensionPixelOffset(attr, 0));
2981 break;
2982 case com.android.internal.R.styleable.View_transformPivotY:
2983 setPivotY(a.getDimensionPixelOffset(attr, 0));
2984 break;
2985 case com.android.internal.R.styleable.View_translationX:
2986 tx = a.getDimensionPixelOffset(attr, 0);
2987 transformSet = true;
2988 break;
2989 case com.android.internal.R.styleable.View_translationY:
2990 ty = a.getDimensionPixelOffset(attr, 0);
2991 transformSet = true;
2992 break;
2993 case com.android.internal.R.styleable.View_rotation:
2994 rotation = a.getFloat(attr, 0);
2995 transformSet = true;
2996 break;
2997 case com.android.internal.R.styleable.View_rotationX:
2998 rotationX = a.getFloat(attr, 0);
2999 transformSet = true;
3000 break;
3001 case com.android.internal.R.styleable.View_rotationY:
3002 rotationY = a.getFloat(attr, 0);
3003 transformSet = true;
3004 break;
3005 case com.android.internal.R.styleable.View_scaleX:
3006 sx = a.getFloat(attr, 1f);
3007 transformSet = true;
3008 break;
3009 case com.android.internal.R.styleable.View_scaleY:
3010 sy = a.getFloat(attr, 1f);
3011 transformSet = true;
3012 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003013 case com.android.internal.R.styleable.View_id:
3014 mID = a.getResourceId(attr, NO_ID);
3015 break;
3016 case com.android.internal.R.styleable.View_tag:
3017 mTag = a.getText(attr);
3018 break;
3019 case com.android.internal.R.styleable.View_fitsSystemWindows:
3020 if (a.getBoolean(attr, false)) {
3021 viewFlagValues |= FITS_SYSTEM_WINDOWS;
3022 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
3023 }
3024 break;
3025 case com.android.internal.R.styleable.View_focusable:
3026 if (a.getBoolean(attr, false)) {
3027 viewFlagValues |= FOCUSABLE;
3028 viewFlagMasks |= FOCUSABLE_MASK;
3029 }
3030 break;
3031 case com.android.internal.R.styleable.View_focusableInTouchMode:
3032 if (a.getBoolean(attr, false)) {
3033 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
3034 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
3035 }
3036 break;
3037 case com.android.internal.R.styleable.View_clickable:
3038 if (a.getBoolean(attr, false)) {
3039 viewFlagValues |= CLICKABLE;
3040 viewFlagMasks |= CLICKABLE;
3041 }
3042 break;
3043 case com.android.internal.R.styleable.View_longClickable:
3044 if (a.getBoolean(attr, false)) {
3045 viewFlagValues |= LONG_CLICKABLE;
3046 viewFlagMasks |= LONG_CLICKABLE;
3047 }
3048 break;
3049 case com.android.internal.R.styleable.View_saveEnabled:
3050 if (!a.getBoolean(attr, true)) {
3051 viewFlagValues |= SAVE_DISABLED;
3052 viewFlagMasks |= SAVE_DISABLED_MASK;
3053 }
3054 break;
3055 case com.android.internal.R.styleable.View_duplicateParentState:
3056 if (a.getBoolean(attr, false)) {
3057 viewFlagValues |= DUPLICATE_PARENT_STATE;
3058 viewFlagMasks |= DUPLICATE_PARENT_STATE;
3059 }
3060 break;
3061 case com.android.internal.R.styleable.View_visibility:
3062 final int visibility = a.getInt(attr, 0);
3063 if (visibility != 0) {
3064 viewFlagValues |= VISIBILITY_FLAGS[visibility];
3065 viewFlagMasks |= VISIBILITY_MASK;
3066 }
3067 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003068 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003069 // Clear any layout direction flags (included resolved bits) already set
3070 mPrivateFlags2 &= ~(LAYOUT_DIRECTION_MASK | LAYOUT_DIRECTION_RESOLVED_MASK);
3071 // Set the layout direction flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003072 final int layoutDirection = a.getInt(attr, -1);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003073 final int value = (layoutDirection != -1) ?
3074 LAYOUT_DIRECTION_FLAGS[layoutDirection] : LAYOUT_DIRECTION_DEFAULT;
3075 mPrivateFlags2 |= (value << LAYOUT_DIRECTION_MASK_SHIFT);
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07003076 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003077 case com.android.internal.R.styleable.View_drawingCacheQuality:
3078 final int cacheQuality = a.getInt(attr, 0);
3079 if (cacheQuality != 0) {
3080 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
3081 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
3082 }
3083 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07003084 case com.android.internal.R.styleable.View_contentDescription:
3085 mContentDescription = a.getString(attr);
3086 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003087 case com.android.internal.R.styleable.View_soundEffectsEnabled:
3088 if (!a.getBoolean(attr, true)) {
3089 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
3090 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
3091 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003092 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003093 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
3094 if (!a.getBoolean(attr, true)) {
3095 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
3096 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
3097 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003098 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003099 case R.styleable.View_scrollbars:
3100 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
3101 if (scrollbars != SCROLLBARS_NONE) {
3102 viewFlagValues |= scrollbars;
3103 viewFlagMasks |= SCROLLBARS_MASK;
3104 initializeScrollbars(a);
3105 }
3106 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07003107 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003108 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07003109 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
3110 // Ignore the attribute starting with ICS
3111 break;
3112 }
3113 // With builds < ICS, fall through and apply fading edges
3114 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003115 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
3116 if (fadingEdge != FADING_EDGE_NONE) {
3117 viewFlagValues |= fadingEdge;
3118 viewFlagMasks |= FADING_EDGE_MASK;
3119 initializeFadingEdge(a);
3120 }
3121 break;
3122 case R.styleable.View_scrollbarStyle:
3123 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3124 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3125 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3126 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3127 }
3128 break;
3129 case R.styleable.View_isScrollContainer:
3130 setScrollContainer = true;
3131 if (a.getBoolean(attr, false)) {
3132 setScrollContainer(true);
3133 }
3134 break;
3135 case com.android.internal.R.styleable.View_keepScreenOn:
3136 if (a.getBoolean(attr, false)) {
3137 viewFlagValues |= KEEP_SCREEN_ON;
3138 viewFlagMasks |= KEEP_SCREEN_ON;
3139 }
3140 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003141 case R.styleable.View_filterTouchesWhenObscured:
3142 if (a.getBoolean(attr, false)) {
3143 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3144 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3145 }
3146 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003147 case R.styleable.View_nextFocusLeft:
3148 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3149 break;
3150 case R.styleable.View_nextFocusRight:
3151 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3152 break;
3153 case R.styleable.View_nextFocusUp:
3154 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3155 break;
3156 case R.styleable.View_nextFocusDown:
3157 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3158 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003159 case R.styleable.View_nextFocusForward:
3160 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3161 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003162 case R.styleable.View_minWidth:
3163 mMinWidth = a.getDimensionPixelSize(attr, 0);
3164 break;
3165 case R.styleable.View_minHeight:
3166 mMinHeight = a.getDimensionPixelSize(attr, 0);
3167 break;
Romain Guy9a817362009-05-01 10:57:14 -07003168 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003169 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003170 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003171 + "be used within a restricted context");
3172 }
3173
Romain Guy9a817362009-05-01 10:57:14 -07003174 final String handlerName = a.getString(attr);
3175 if (handlerName != null) {
3176 setOnClickListener(new OnClickListener() {
3177 private Method mHandler;
3178
3179 public void onClick(View v) {
3180 if (mHandler == null) {
3181 try {
3182 mHandler = getContext().getClass().getMethod(handlerName,
3183 View.class);
3184 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003185 int id = getId();
3186 String idText = id == NO_ID ? "" : " with id '"
3187 + getContext().getResources().getResourceEntryName(
3188 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003189 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003190 handlerName + "(View) in the activity "
3191 + getContext().getClass() + " for onClick handler"
3192 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003193 }
3194 }
3195
3196 try {
3197 mHandler.invoke(getContext(), View.this);
3198 } catch (IllegalAccessException e) {
3199 throw new IllegalStateException("Could not execute non "
3200 + "public method of the activity", e);
3201 } catch (InvocationTargetException e) {
3202 throw new IllegalStateException("Could not execute "
3203 + "method of the activity", e);
3204 }
3205 }
3206 });
3207 }
3208 break;
Adam Powell637d3372010-08-25 14:37:03 -07003209 case R.styleable.View_overScrollMode:
3210 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3211 break;
Adam Powell20232d02010-12-08 21:08:53 -08003212 case R.styleable.View_verticalScrollbarPosition:
3213 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3214 break;
Romain Guy171c5922011-01-06 10:04:23 -08003215 case R.styleable.View_layerType:
3216 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3217 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003218 case R.styleable.View_textDirection:
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003219 // Clear any text direction flag already set
3220 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
3221 // Set the text direction flags depending on the value of the attribute
3222 final int textDirection = a.getInt(attr, -1);
3223 if (textDirection != -1) {
3224 mPrivateFlags2 |= TEXT_DIRECTION_FLAGS[textDirection];
3225 }
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003226 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003227 }
3228 }
3229
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003230 a.recycle();
3231
Adam Powell637d3372010-08-25 14:37:03 -07003232 setOverScrollMode(overScrollMode);
3233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003234 if (background != null) {
Philip Milne6c8ea062012-04-03 17:38:43 -07003235 setBackground(background);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003236 }
3237
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003238 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3239 // layout direction). Those cached values will be used later during padding resolution.
3240 mUserPaddingStart = startPadding;
3241 mUserPaddingEnd = endPadding;
3242
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003243 updateUserPaddingRelative();
3244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003245 if (padding >= 0) {
3246 leftPadding = padding;
3247 topPadding = padding;
3248 rightPadding = padding;
3249 bottomPadding = padding;
3250 }
3251
3252 // If the user specified the padding (either with android:padding or
3253 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3254 // use the default padding or the padding from the background drawable
3255 // (stored at this point in mPadding*)
3256 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3257 topPadding >= 0 ? topPadding : mPaddingTop,
3258 rightPadding >= 0 ? rightPadding : mPaddingRight,
3259 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3260
3261 if (viewFlagMasks != 0) {
3262 setFlags(viewFlagValues, viewFlagMasks);
3263 }
3264
3265 // Needs to be called after mViewFlags is set
3266 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3267 recomputePadding();
3268 }
3269
3270 if (x != 0 || y != 0) {
3271 scrollTo(x, y);
3272 }
3273
Chet Haase73066682010-11-29 15:55:32 -08003274 if (transformSet) {
3275 setTranslationX(tx);
3276 setTranslationY(ty);
3277 setRotation(rotation);
3278 setRotationX(rotationX);
3279 setRotationY(rotationY);
3280 setScaleX(sx);
3281 setScaleY(sy);
3282 }
3283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003284 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3285 setScrollContainer(true);
3286 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003287
3288 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003289 }
3290
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003291 private void updateUserPaddingRelative() {
3292 mUserPaddingRelative = (mUserPaddingStart >= 0 || mUserPaddingEnd >= 0);
3293 }
3294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003295 /**
3296 * Non-public constructor for use in testing
3297 */
3298 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003299 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003300 }
3301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003302 /**
3303 * <p>
3304 * Initializes the fading edges from a given set of styled attributes. This
3305 * method should be called by subclasses that need fading edges and when an
3306 * instance of these subclasses is created programmatically rather than
3307 * being inflated from XML. This method is automatically called when the XML
3308 * is inflated.
3309 * </p>
3310 *
3311 * @param a the styled attributes set to initialize the fading edges from
3312 */
3313 protected void initializeFadingEdge(TypedArray a) {
3314 initScrollCache();
3315
3316 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3317 R.styleable.View_fadingEdgeLength,
3318 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3319 }
3320
3321 /**
3322 * Returns the size of the vertical faded edges used to indicate that more
3323 * content in this view is visible.
3324 *
3325 * @return The size in pixels of the vertical faded edge or 0 if vertical
3326 * faded edges are not enabled for this view.
3327 * @attr ref android.R.styleable#View_fadingEdgeLength
3328 */
3329 public int getVerticalFadingEdgeLength() {
3330 if (isVerticalFadingEdgeEnabled()) {
3331 ScrollabilityCache cache = mScrollCache;
3332 if (cache != null) {
3333 return cache.fadingEdgeLength;
3334 }
3335 }
3336 return 0;
3337 }
3338
3339 /**
3340 * Set the size of the faded edge used to indicate that more content in this
3341 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003342 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3343 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3344 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003345 *
3346 * @param length The size in pixels of the faded edge used to indicate that more
3347 * content in this view is visible.
3348 */
3349 public void setFadingEdgeLength(int length) {
3350 initScrollCache();
3351 mScrollCache.fadingEdgeLength = length;
3352 }
3353
3354 /**
3355 * Returns the size of the horizontal faded edges used to indicate that more
3356 * content in this view is visible.
3357 *
3358 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3359 * faded edges are not enabled for this view.
3360 * @attr ref android.R.styleable#View_fadingEdgeLength
3361 */
3362 public int getHorizontalFadingEdgeLength() {
3363 if (isHorizontalFadingEdgeEnabled()) {
3364 ScrollabilityCache cache = mScrollCache;
3365 if (cache != null) {
3366 return cache.fadingEdgeLength;
3367 }
3368 }
3369 return 0;
3370 }
3371
3372 /**
3373 * Returns the width of the vertical scrollbar.
3374 *
3375 * @return The width in pixels of the vertical scrollbar or 0 if there
3376 * is no vertical scrollbar.
3377 */
3378 public int getVerticalScrollbarWidth() {
3379 ScrollabilityCache cache = mScrollCache;
3380 if (cache != null) {
3381 ScrollBarDrawable scrollBar = cache.scrollBar;
3382 if (scrollBar != null) {
3383 int size = scrollBar.getSize(true);
3384 if (size <= 0) {
3385 size = cache.scrollBarSize;
3386 }
3387 return size;
3388 }
3389 return 0;
3390 }
3391 return 0;
3392 }
3393
3394 /**
3395 * Returns the height of the horizontal scrollbar.
3396 *
3397 * @return The height in pixels of the horizontal scrollbar or 0 if
3398 * there is no horizontal scrollbar.
3399 */
3400 protected int getHorizontalScrollbarHeight() {
3401 ScrollabilityCache cache = mScrollCache;
3402 if (cache != null) {
3403 ScrollBarDrawable scrollBar = cache.scrollBar;
3404 if (scrollBar != null) {
3405 int size = scrollBar.getSize(false);
3406 if (size <= 0) {
3407 size = cache.scrollBarSize;
3408 }
3409 return size;
3410 }
3411 return 0;
3412 }
3413 return 0;
3414 }
3415
3416 /**
3417 * <p>
3418 * Initializes the scrollbars from a given set of styled attributes. This
3419 * method should be called by subclasses that need scrollbars and when an
3420 * instance of these subclasses is created programmatically rather than
3421 * being inflated from XML. This method is automatically called when the XML
3422 * is inflated.
3423 * </p>
3424 *
3425 * @param a the styled attributes set to initialize the scrollbars from
3426 */
3427 protected void initializeScrollbars(TypedArray a) {
3428 initScrollCache();
3429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003430 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003431
Mike Cleronf116bf82009-09-27 19:14:12 -07003432 if (scrollabilityCache.scrollBar == null) {
3433 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3434 }
Joe Malin32736f02011-01-19 16:14:20 -08003435
Romain Guy8bda2482010-03-02 11:42:11 -08003436 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003437
Mike Cleronf116bf82009-09-27 19:14:12 -07003438 if (!fadeScrollbars) {
3439 scrollabilityCache.state = ScrollabilityCache.ON;
3440 }
3441 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003442
3443
Mike Cleronf116bf82009-09-27 19:14:12 -07003444 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3445 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3446 .getScrollBarFadeDuration());
3447 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3448 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3449 ViewConfiguration.getScrollDefaultDelay());
3450
Joe Malin32736f02011-01-19 16:14:20 -08003451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003452 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3453 com.android.internal.R.styleable.View_scrollbarSize,
3454 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3455
3456 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3457 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3458
3459 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3460 if (thumb != null) {
3461 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3462 }
3463
3464 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3465 false);
3466 if (alwaysDraw) {
3467 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3468 }
3469
3470 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3471 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3472
3473 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3474 if (thumb != null) {
3475 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3476 }
3477
3478 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3479 false);
3480 if (alwaysDraw) {
3481 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3482 }
3483
3484 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003485 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003486 }
3487
3488 /**
3489 * <p>
3490 * Initalizes the scrollability cache if necessary.
3491 * </p>
3492 */
3493 private void initScrollCache() {
3494 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003495 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003496 }
3497 }
3498
Philip Milne6c8ea062012-04-03 17:38:43 -07003499 private ScrollabilityCache getScrollCache() {
3500 initScrollCache();
3501 return mScrollCache;
3502 }
3503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003504 /**
Adam Powell20232d02010-12-08 21:08:53 -08003505 * Set the position of the vertical scroll bar. Should be one of
3506 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3507 * {@link #SCROLLBAR_POSITION_RIGHT}.
3508 *
3509 * @param position Where the vertical scroll bar should be positioned.
3510 */
3511 public void setVerticalScrollbarPosition(int position) {
3512 if (mVerticalScrollbarPosition != position) {
3513 mVerticalScrollbarPosition = position;
3514 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003515 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003516 }
3517 }
3518
3519 /**
3520 * @return The position where the vertical scroll bar will show, if applicable.
3521 * @see #setVerticalScrollbarPosition(int)
3522 */
3523 public int getVerticalScrollbarPosition() {
3524 return mVerticalScrollbarPosition;
3525 }
3526
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003527 ListenerInfo getListenerInfo() {
3528 if (mListenerInfo != null) {
3529 return mListenerInfo;
3530 }
3531 mListenerInfo = new ListenerInfo();
3532 return mListenerInfo;
3533 }
3534
Adam Powell20232d02010-12-08 21:08:53 -08003535 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003536 * Register a callback to be invoked when focus of this view changed.
3537 *
3538 * @param l The callback that will run.
3539 */
3540 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003541 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003542 }
3543
3544 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003545 * Add a listener that will be called when the bounds of the view change due to
3546 * layout processing.
3547 *
3548 * @param listener The listener that will be called when layout bounds change.
3549 */
3550 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003551 ListenerInfo li = getListenerInfo();
3552 if (li.mOnLayoutChangeListeners == null) {
3553 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003554 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003555 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3556 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003557 }
Chet Haase21cd1382010-09-01 17:42:29 -07003558 }
3559
3560 /**
3561 * Remove a listener for layout changes.
3562 *
3563 * @param listener The listener for layout bounds change.
3564 */
3565 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003566 ListenerInfo li = mListenerInfo;
3567 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003568 return;
3569 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003570 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003571 }
3572
3573 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003574 * Add a listener for attach state changes.
3575 *
3576 * This listener will be called whenever this view is attached or detached
3577 * from a window. Remove the listener using
3578 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3579 *
3580 * @param listener Listener to attach
3581 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3582 */
3583 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003584 ListenerInfo li = getListenerInfo();
3585 if (li.mOnAttachStateChangeListeners == null) {
3586 li.mOnAttachStateChangeListeners
3587 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003588 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003589 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003590 }
3591
3592 /**
3593 * Remove a listener for attach state changes. The listener will receive no further
3594 * notification of window attach/detach events.
3595 *
3596 * @param listener Listener to remove
3597 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3598 */
3599 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003600 ListenerInfo li = mListenerInfo;
3601 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003602 return;
3603 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003604 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003605 }
3606
3607 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003608 * Returns the focus-change callback registered for this view.
3609 *
3610 * @return The callback, or null if one is not registered.
3611 */
3612 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003613 ListenerInfo li = mListenerInfo;
3614 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003615 }
3616
3617 /**
3618 * Register a callback to be invoked when this view is clicked. If this view is not
3619 * clickable, it becomes clickable.
3620 *
3621 * @param l The callback that will run
3622 *
3623 * @see #setClickable(boolean)
3624 */
3625 public void setOnClickListener(OnClickListener l) {
3626 if (!isClickable()) {
3627 setClickable(true);
3628 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003629 getListenerInfo().mOnClickListener = l;
3630 }
3631
3632 /**
3633 * Return whether this view has an attached OnClickListener. Returns
3634 * true if there is a listener, false if there is none.
3635 */
3636 public boolean hasOnClickListeners() {
3637 ListenerInfo li = mListenerInfo;
3638 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003639 }
3640
3641 /**
3642 * Register a callback to be invoked when this view is clicked and held. If this view is not
3643 * long clickable, it becomes long clickable.
3644 *
3645 * @param l The callback that will run
3646 *
3647 * @see #setLongClickable(boolean)
3648 */
3649 public void setOnLongClickListener(OnLongClickListener l) {
3650 if (!isLongClickable()) {
3651 setLongClickable(true);
3652 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003653 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003654 }
3655
3656 /**
3657 * Register a callback to be invoked when the context menu for this view is
3658 * being built. If this view is not long clickable, it becomes long clickable.
3659 *
3660 * @param l The callback that will run
3661 *
3662 */
3663 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3664 if (!isLongClickable()) {
3665 setLongClickable(true);
3666 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003667 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003668 }
3669
3670 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003671 * Call this view's OnClickListener, if it is defined. Performs all normal
3672 * actions associated with clicking: reporting accessibility event, playing
3673 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003674 *
3675 * @return True there was an assigned OnClickListener that was called, false
3676 * otherwise is returned.
3677 */
3678 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003679 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3680
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003681 ListenerInfo li = mListenerInfo;
3682 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003683 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003684 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003685 return true;
3686 }
3687
3688 return false;
3689 }
3690
3691 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003692 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
3693 * this only calls the listener, and does not do any associated clicking
3694 * actions like reporting an accessibility event.
3695 *
3696 * @return True there was an assigned OnClickListener that was called, false
3697 * otherwise is returned.
3698 */
3699 public boolean callOnClick() {
3700 ListenerInfo li = mListenerInfo;
3701 if (li != null && li.mOnClickListener != null) {
3702 li.mOnClickListener.onClick(this);
3703 return true;
3704 }
3705 return false;
3706 }
3707
3708 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003709 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3710 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003711 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003712 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003713 */
3714 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003715 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003717 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003718 ListenerInfo li = mListenerInfo;
3719 if (li != null && li.mOnLongClickListener != null) {
3720 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003721 }
3722 if (!handled) {
3723 handled = showContextMenu();
3724 }
3725 if (handled) {
3726 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3727 }
3728 return handled;
3729 }
3730
3731 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003732 * Performs button-related actions during a touch down event.
3733 *
3734 * @param event The event.
3735 * @return True if the down was consumed.
3736 *
3737 * @hide
3738 */
3739 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3740 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3741 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3742 return true;
3743 }
3744 }
3745 return false;
3746 }
3747
3748 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003749 * Bring up the context menu for this view.
3750 *
3751 * @return Whether a context menu was displayed.
3752 */
3753 public boolean showContextMenu() {
3754 return getParent().showContextMenuForChild(this);
3755 }
3756
3757 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003758 * Bring up the context menu for this view, referring to the item under the specified point.
3759 *
3760 * @param x The referenced x coordinate.
3761 * @param y The referenced y coordinate.
3762 * @param metaState The keyboard modifiers that were pressed.
3763 * @return Whether a context menu was displayed.
3764 *
3765 * @hide
3766 */
3767 public boolean showContextMenu(float x, float y, int metaState) {
3768 return showContextMenu();
3769 }
3770
3771 /**
Adam Powell6e346362010-07-23 10:18:23 -07003772 * Start an action mode.
3773 *
3774 * @param callback Callback that will control the lifecycle of the action mode
3775 * @return The new action mode if it is started, null otherwise
3776 *
3777 * @see ActionMode
3778 */
3779 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08003780 ViewParent parent = getParent();
3781 if (parent == null) return null;
3782 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07003783 }
3784
3785 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003786 * Register a callback to be invoked when a key is pressed in this view.
3787 * @param l the key listener to attach to this view
3788 */
3789 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003790 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003791 }
3792
3793 /**
3794 * Register a callback to be invoked when a touch event is sent to this view.
3795 * @param l the touch listener to attach to this view
3796 */
3797 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003798 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003799 }
3800
3801 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003802 * Register a callback to be invoked when a generic motion event is sent to this view.
3803 * @param l the generic motion listener to attach to this view
3804 */
3805 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003806 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08003807 }
3808
3809 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07003810 * Register a callback to be invoked when a hover event is sent to this view.
3811 * @param l the hover listener to attach to this view
3812 */
3813 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003814 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07003815 }
3816
3817 /**
Joe Malin32736f02011-01-19 16:14:20 -08003818 * Register a drag event listener callback object for this View. The parameter is
3819 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3820 * View, the system calls the
3821 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3822 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003823 */
3824 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003825 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07003826 }
3827
3828 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07003829 * Give this view focus. This will cause
3830 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003831 *
3832 * Note: this does not check whether this {@link View} should get focus, it just
3833 * gives it focus no matter what. It should only be called internally by framework
3834 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3835 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003836 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3837 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003838 * focus moved when requestFocus() is called. It may not always
3839 * apply, in which case use the default View.FOCUS_DOWN.
3840 * @param previouslyFocusedRect The rectangle of the view that had focus
3841 * prior in this View's coordinate system.
3842 */
3843 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3844 if (DBG) {
3845 System.out.println(this + " requestFocus()");
3846 }
3847
3848 if ((mPrivateFlags & FOCUSED) == 0) {
3849 mPrivateFlags |= FOCUSED;
3850
3851 if (mParent != null) {
3852 mParent.requestChildFocus(this, this);
3853 }
3854
3855 onFocusChanged(true, direction, previouslyFocusedRect);
3856 refreshDrawableState();
3857 }
3858 }
3859
3860 /**
3861 * Request that a rectangle of this view be visible on the screen,
3862 * scrolling if necessary just enough.
3863 *
3864 * <p>A View should call this if it maintains some notion of which part
3865 * of its content is interesting. For example, a text editing view
3866 * should call this when its cursor moves.
3867 *
3868 * @param rectangle The rectangle.
3869 * @return Whether any parent scrolled.
3870 */
3871 public boolean requestRectangleOnScreen(Rect rectangle) {
3872 return requestRectangleOnScreen(rectangle, false);
3873 }
3874
3875 /**
3876 * Request that a rectangle of this view be visible on the screen,
3877 * scrolling if necessary just enough.
3878 *
3879 * <p>A View should call this if it maintains some notion of which part
3880 * of its content is interesting. For example, a text editing view
3881 * should call this when its cursor moves.
3882 *
3883 * <p>When <code>immediate</code> is set to true, scrolling will not be
3884 * animated.
3885 *
3886 * @param rectangle The rectangle.
3887 * @param immediate True to forbid animated scrolling, false otherwise
3888 * @return Whether any parent scrolled.
3889 */
3890 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3891 View child = this;
3892 ViewParent parent = mParent;
3893 boolean scrolled = false;
3894 while (parent != null) {
3895 scrolled |= parent.requestChildRectangleOnScreen(child,
3896 rectangle, immediate);
3897
3898 // offset rect so next call has the rectangle in the
3899 // coordinate system of its direct child.
3900 rectangle.offset(child.getLeft(), child.getTop());
3901 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3902
3903 if (!(parent instanceof View)) {
3904 break;
3905 }
Romain Guy8506ab42009-06-11 17:35:47 -07003906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003907 child = (View) parent;
3908 parent = child.getParent();
3909 }
3910 return scrolled;
3911 }
3912
3913 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08003914 * Called when this view wants to give up focus. If focus is cleared
3915 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
3916 * <p>
3917 * <strong>Note:</strong> When a View clears focus the framework is trying
3918 * to give focus to the first focusable View from the top. Hence, if this
3919 * View is the first from the top that can take focus, then its focus will
3920 * not be cleared nor will the focus change callback be invoked.
3921 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003922 */
3923 public void clearFocus() {
3924 if (DBG) {
3925 System.out.println(this + " clearFocus()");
3926 }
3927
3928 if ((mPrivateFlags & FOCUSED) != 0) {
3929 mPrivateFlags &= ~FOCUSED;
3930
3931 if (mParent != null) {
3932 mParent.clearChildFocus(this);
3933 }
3934
3935 onFocusChanged(false, 0, null);
3936 refreshDrawableState();
3937 }
3938 }
3939
3940 /**
3941 * Called to clear the focus of a view that is about to be removed.
3942 * Doesn't call clearChildFocus, which prevents this view from taking
3943 * focus again before it has been removed from the parent
3944 */
3945 void clearFocusForRemoval() {
3946 if ((mPrivateFlags & FOCUSED) != 0) {
3947 mPrivateFlags &= ~FOCUSED;
3948
3949 onFocusChanged(false, 0, null);
3950 refreshDrawableState();
Svetoslav Ganovb36a0ac2012-02-14 17:46:47 -08003951
3952 // The view cleared focus and invoked the callbacks, so now is the
3953 // time to give focus to the the first focusable from the top to
3954 // ensure that the gain focus is announced after clear focus.
3955 getRootView().requestFocus(FOCUS_FORWARD);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003956 }
3957 }
3958
3959 /**
3960 * Called internally by the view system when a new view is getting focus.
3961 * This is what clears the old focus.
3962 */
3963 void unFocus() {
3964 if (DBG) {
3965 System.out.println(this + " unFocus()");
3966 }
3967
3968 if ((mPrivateFlags & FOCUSED) != 0) {
3969 mPrivateFlags &= ~FOCUSED;
3970
3971 onFocusChanged(false, 0, null);
3972 refreshDrawableState();
3973 }
3974 }
3975
3976 /**
3977 * Returns true if this view has focus iteself, or is the ancestor of the
3978 * view that has focus.
3979 *
3980 * @return True if this view has or contains focus, false otherwise.
3981 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003982 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003983 public boolean hasFocus() {
3984 return (mPrivateFlags & FOCUSED) != 0;
3985 }
3986
3987 /**
3988 * Returns true if this view is focusable or if it contains a reachable View
3989 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3990 * is a View whose parents do not block descendants focus.
3991 *
3992 * Only {@link #VISIBLE} views are considered focusable.
3993 *
3994 * @return True if the view is focusable or if the view contains a focusable
3995 * View, false otherwise.
3996 *
3997 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3998 */
3999 public boolean hasFocusable() {
4000 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4001 }
4002
4003 /**
4004 * Called by the view system when the focus state of this view changes.
4005 * When the focus change event is caused by directional navigation, direction
4006 * and previouslyFocusedRect provide insight into where the focus is coming from.
4007 * When overriding, be sure to call up through to the super class so that
4008 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004009 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004010 * @param gainFocus True if the View has focus; false otherwise.
4011 * @param direction The direction focus has moved when requestFocus()
4012 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004013 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4014 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4015 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004016 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4017 * system, of the previously focused view. If applicable, this will be
4018 * passed in as finer grained information about where the focus is coming
4019 * from (in addition to direction). Will be <code>null</code> otherwise.
4020 */
4021 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004022 if (gainFocus) {
4023 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
4024 }
4025
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004026 InputMethodManager imm = InputMethodManager.peekInstance();
4027 if (!gainFocus) {
4028 if (isPressed()) {
4029 setPressed(false);
4030 }
4031 if (imm != null && mAttachInfo != null
4032 && mAttachInfo.mHasWindowFocus) {
4033 imm.focusOut(this);
4034 }
Romain Guya2431d02009-04-30 16:30:00 -07004035 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004036 } else if (imm != null && mAttachInfo != null
4037 && mAttachInfo.mHasWindowFocus) {
4038 imm.focusIn(this);
4039 }
Romain Guy8506ab42009-06-11 17:35:47 -07004040
Romain Guy0fd89bf2011-01-26 15:41:30 -08004041 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004042 ListenerInfo li = mListenerInfo;
4043 if (li != null && li.mOnFocusChangeListener != null) {
4044 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004045 }
Joe Malin32736f02011-01-19 16:14:20 -08004046
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004047 if (mAttachInfo != null) {
4048 mAttachInfo.mKeyDispatchState.reset(this);
4049 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004050 }
4051
4052 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004053 * Sends an accessibility event of the given type. If accessiiblity is
4054 * not enabled this method has no effect. The default implementation calls
4055 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4056 * to populate information about the event source (this View), then calls
4057 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4058 * populate the text content of the event source including its descendants,
4059 * and last calls
4060 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4061 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004062 * <p>
4063 * If an {@link AccessibilityDelegate} has been specified via calling
4064 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4065 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4066 * responsible for handling this call.
4067 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004068 *
Scott Mainb303d832011-10-12 16:45:18 -07004069 * @param eventType The type of the event to send, as defined by several types from
4070 * {@link android.view.accessibility.AccessibilityEvent}, such as
4071 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4072 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004073 *
4074 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4075 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4076 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004077 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004078 */
4079 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004080 if (mAccessibilityDelegate != null) {
4081 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4082 } else {
4083 sendAccessibilityEventInternal(eventType);
4084 }
4085 }
4086
4087 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004088 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4089 * {@link AccessibilityEvent} to make an announcement which is related to some
4090 * sort of a context change for which none of the events representing UI transitions
4091 * is a good fit. For example, announcing a new page in a book. If accessibility
4092 * is not enabled this method does nothing.
4093 *
4094 * @param text The announcement text.
4095 */
4096 public void announceForAccessibility(CharSequence text) {
4097 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4098 AccessibilityEvent event = AccessibilityEvent.obtain(
4099 AccessibilityEvent.TYPE_ANNOUNCEMENT);
4100 event.getText().add(text);
4101 sendAccessibilityEventUnchecked(event);
4102 }
4103 }
4104
4105 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004106 * @see #sendAccessibilityEvent(int)
4107 *
4108 * Note: Called from the default {@link AccessibilityDelegate}.
4109 */
4110 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004111 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4112 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4113 }
4114 }
4115
4116 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004117 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4118 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004119 * perform a check whether accessibility is enabled.
4120 * <p>
4121 * If an {@link AccessibilityDelegate} has been specified via calling
4122 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4123 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4124 * is responsible for handling this call.
4125 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004126 *
4127 * @param event The event to send.
4128 *
4129 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004130 */
4131 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004132 if (mAccessibilityDelegate != null) {
4133 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
4134 } else {
4135 sendAccessibilityEventUncheckedInternal(event);
4136 }
4137 }
4138
4139 /**
4140 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4141 *
4142 * Note: Called from the default {@link AccessibilityDelegate}.
4143 */
4144 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004145 if (!isShown()) {
4146 return;
4147 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004148 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004149 // Only a subset of accessibility events populates text content.
4150 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4151 dispatchPopulateAccessibilityEvent(event);
4152 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004153 // In the beginning we called #isShown(), so we know that getParent() is not null.
4154 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004155 }
4156
4157 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004158 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4159 * to its children for adding their text content to the event. Note that the
4160 * event text is populated in a separate dispatch path since we add to the
4161 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004162 * A typical implementation will call
4163 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4164 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4165 * on each child. Override this method if custom population of the event text
4166 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004167 * <p>
4168 * If an {@link AccessibilityDelegate} has been specified via calling
4169 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4170 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4171 * is responsible for handling this call.
4172 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004173 * <p>
4174 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4175 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4176 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004177 *
4178 * @param event The event.
4179 *
4180 * @return True if the event population was completed.
4181 */
4182 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004183 if (mAccessibilityDelegate != null) {
4184 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4185 } else {
4186 return dispatchPopulateAccessibilityEventInternal(event);
4187 }
4188 }
4189
4190 /**
4191 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4192 *
4193 * Note: Called from the default {@link AccessibilityDelegate}.
4194 */
4195 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004196 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004197 return false;
4198 }
4199
4200 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004201 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004202 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004203 * text content. While this method is free to modify event
4204 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004205 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4206 * <p>
4207 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004208 * to the text added by the super implementation:
4209 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004210 * super.onPopulateAccessibilityEvent(event);
4211 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4212 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4213 * mCurrentDate.getTimeInMillis(), flags);
4214 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004215 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004216 * <p>
4217 * If an {@link AccessibilityDelegate} has been specified via calling
4218 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4219 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4220 * is responsible for handling this call.
4221 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004222 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4223 * information to the event, in case the default implementation has basic information to add.
4224 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004225 *
4226 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004227 *
4228 * @see #sendAccessibilityEvent(int)
4229 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004230 */
4231 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004232 if (mAccessibilityDelegate != null) {
4233 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4234 } else {
4235 onPopulateAccessibilityEventInternal(event);
4236 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004237 }
4238
4239 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004240 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4241 *
4242 * Note: Called from the default {@link AccessibilityDelegate}.
4243 */
4244 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4245
4246 }
4247
4248 /**
4249 * Initializes an {@link AccessibilityEvent} with information about
4250 * this View which is the event source. In other words, the source of
4251 * an accessibility event is the view whose state change triggered firing
4252 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004253 * <p>
4254 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004255 * to properties set by the super implementation:
4256 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4257 * super.onInitializeAccessibilityEvent(event);
4258 * event.setPassword(true);
4259 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004260 * <p>
4261 * If an {@link AccessibilityDelegate} has been specified via calling
4262 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4263 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4264 * is responsible for handling this call.
4265 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004266 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4267 * information to the event, in case the default implementation has basic information to add.
4268 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004269 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004270 *
4271 * @see #sendAccessibilityEvent(int)
4272 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4273 */
4274 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004275 if (mAccessibilityDelegate != null) {
4276 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4277 } else {
4278 onInitializeAccessibilityEventInternal(event);
4279 }
4280 }
4281
4282 /**
4283 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4284 *
4285 * Note: Called from the default {@link AccessibilityDelegate}.
4286 */
4287 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004288 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004289 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004290 event.setPackageName(getContext().getPackageName());
4291 event.setEnabled(isEnabled());
4292 event.setContentDescription(mContentDescription);
4293
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004294 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
4295 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
4296 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4297 FOCUSABLES_ALL);
4298 event.setItemCount(focusablesTempList.size());
4299 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4300 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004301 }
4302 }
4303
4304 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004305 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4306 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4307 * This method is responsible for obtaining an accessibility node info from a
4308 * pool of reusable instances and calling
4309 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4310 * initialize the former.
4311 * <p>
4312 * Note: The client is responsible for recycling the obtained instance by calling
4313 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4314 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004315 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004316 * @return A populated {@link AccessibilityNodeInfo}.
4317 *
4318 * @see AccessibilityNodeInfo
4319 */
4320 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004321 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4322 if (provider != null) {
4323 return provider.createAccessibilityNodeInfo(View.NO_ID);
4324 } else {
4325 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4326 onInitializeAccessibilityNodeInfo(info);
4327 return info;
4328 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004329 }
4330
4331 /**
4332 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4333 * The base implementation sets:
4334 * <ul>
4335 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004336 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4337 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004338 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4339 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4340 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4341 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4342 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4343 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4344 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4345 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4346 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4347 * </ul>
4348 * <p>
4349 * Subclasses should override this method, call the super implementation,
4350 * and set additional attributes.
4351 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004352 * <p>
4353 * If an {@link AccessibilityDelegate} has been specified via calling
4354 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4355 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4356 * is responsible for handling this call.
4357 * </p>
4358 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004359 * @param info The instance to initialize.
4360 */
4361 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004362 if (mAccessibilityDelegate != null) {
4363 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4364 } else {
4365 onInitializeAccessibilityNodeInfoInternal(info);
4366 }
4367 }
4368
4369 /**
4370 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4371 *
4372 * Note: Called from the default {@link AccessibilityDelegate}.
4373 */
4374 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004375 Rect bounds = mAttachInfo.mTmpInvalRect;
4376 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004377 info.setBoundsInParent(bounds);
4378
4379 int[] locationOnScreen = mAttachInfo.mInvalidateChildLocation;
4380 getLocationOnScreen(locationOnScreen);
Alan Viverette326804f2011-07-22 16:20:41 -07004381 bounds.offsetTo(0, 0);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004382 bounds.offset(locationOnScreen[0], locationOnScreen[1]);
4383 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004384
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004385 if ((mPrivateFlags & IS_ROOT_NAMESPACE) == 0) {
4386 ViewParent parent = getParent();
4387 if (parent instanceof View) {
4388 View parentView = (View) parent;
4389 info.setParent(parentView);
4390 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004391 }
4392
4393 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004394 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004395 info.setContentDescription(getContentDescription());
4396
4397 info.setEnabled(isEnabled());
4398 info.setClickable(isClickable());
4399 info.setFocusable(isFocusable());
4400 info.setFocused(isFocused());
4401 info.setSelected(isSelected());
4402 info.setLongClickable(isLongClickable());
4403
4404 // TODO: These make sense only if we are in an AdapterView but all
4405 // views can be selected. Maybe from accessiiblity perspective
4406 // we should report as selectable view in an AdapterView.
4407 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4408 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4409
4410 if (isFocusable()) {
4411 if (isFocused()) {
4412 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4413 } else {
4414 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4415 }
4416 }
4417 }
4418
4419 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004420 * Sets a delegate for implementing accessibility support via compositon as
4421 * opposed to inheritance. The delegate's primary use is for implementing
4422 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4423 *
4424 * @param delegate The delegate instance.
4425 *
4426 * @see AccessibilityDelegate
4427 */
4428 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4429 mAccessibilityDelegate = delegate;
4430 }
4431
4432 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004433 * Gets the provider for managing a virtual view hierarchy rooted at this View
4434 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4435 * that explore the window content.
4436 * <p>
4437 * If this method returns an instance, this instance is responsible for managing
4438 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4439 * View including the one representing the View itself. Similarly the returned
4440 * instance is responsible for performing accessibility actions on any virtual
4441 * view or the root view itself.
4442 * </p>
4443 * <p>
4444 * If an {@link AccessibilityDelegate} has been specified via calling
4445 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4446 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4447 * is responsible for handling this call.
4448 * </p>
4449 *
4450 * @return The provider.
4451 *
4452 * @see AccessibilityNodeProvider
4453 */
4454 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4455 if (mAccessibilityDelegate != null) {
4456 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4457 } else {
4458 return null;
4459 }
4460 }
4461
4462 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004463 * Gets the unique identifier of this view on the screen for accessibility purposes.
4464 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4465 *
4466 * @return The view accessibility id.
4467 *
4468 * @hide
4469 */
4470 public int getAccessibilityViewId() {
4471 if (mAccessibilityViewId == NO_ID) {
4472 mAccessibilityViewId = sNextAccessibilityViewId++;
4473 }
4474 return mAccessibilityViewId;
4475 }
4476
4477 /**
4478 * Gets the unique identifier of the window in which this View reseides.
4479 *
4480 * @return The window accessibility id.
4481 *
4482 * @hide
4483 */
4484 public int getAccessibilityWindowId() {
4485 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4486 }
4487
4488 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004489 * Gets the {@link View} description. It briefly describes the view and is
4490 * primarily used for accessibility support. Set this property to enable
4491 * better accessibility support for your application. This is especially
4492 * true for views that do not have textual representation (For example,
4493 * ImageButton).
4494 *
4495 * @return The content descriptiopn.
4496 *
4497 * @attr ref android.R.styleable#View_contentDescription
4498 */
4499 public CharSequence getContentDescription() {
4500 return mContentDescription;
4501 }
4502
4503 /**
4504 * Sets the {@link View} description. It briefly describes the view and is
4505 * primarily used for accessibility support. Set this property to enable
4506 * better accessibility support for your application. This is especially
4507 * true for views that do not have textual representation (For example,
4508 * ImageButton).
4509 *
4510 * @param contentDescription The content description.
4511 *
4512 * @attr ref android.R.styleable#View_contentDescription
4513 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07004514 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07004515 public void setContentDescription(CharSequence contentDescription) {
4516 mContentDescription = contentDescription;
4517 }
4518
4519 /**
Romain Guya2431d02009-04-30 16:30:00 -07004520 * Invoked whenever this view loses focus, either by losing window focus or by losing
4521 * focus within its window. This method can be used to clear any state tied to the
4522 * focus. For instance, if a button is held pressed with the trackball and the window
4523 * loses focus, this method can be used to cancel the press.
4524 *
4525 * Subclasses of View overriding this method should always call super.onFocusLost().
4526 *
4527 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004528 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004529 *
4530 * @hide pending API council approval
4531 */
4532 protected void onFocusLost() {
4533 resetPressedState();
4534 }
4535
4536 private void resetPressedState() {
4537 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4538 return;
4539 }
4540
4541 if (isPressed()) {
4542 setPressed(false);
4543
4544 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004545 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004546 }
4547 }
4548 }
4549
4550 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004551 * Returns true if this view has focus
4552 *
4553 * @return True if this view has focus, false otherwise.
4554 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004555 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004556 public boolean isFocused() {
4557 return (mPrivateFlags & FOCUSED) != 0;
4558 }
4559
4560 /**
4561 * Find the view in the hierarchy rooted at this view that currently has
4562 * focus.
4563 *
4564 * @return The view that currently has focus, or null if no focused view can
4565 * be found.
4566 */
4567 public View findFocus() {
4568 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4569 }
4570
4571 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07004572 * Indicates whether this view is one of the set of scrollable containers in
4573 * its window.
4574 *
4575 * @return whether this view is one of the set of scrollable containers in
4576 * its window
4577 *
4578 * @attr ref android.R.styleable#View_isScrollContainer
4579 */
4580 public boolean isScrollContainer() {
4581 return (mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0;
4582 }
4583
4584 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004585 * Change whether this view is one of the set of scrollable containers in
4586 * its window. This will be used to determine whether the window can
4587 * resize or must pan when a soft input area is open -- scrollable
4588 * containers allow the window to use resize mode since the container
4589 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07004590 *
4591 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004592 */
4593 public void setScrollContainer(boolean isScrollContainer) {
4594 if (isScrollContainer) {
4595 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4596 mAttachInfo.mScrollContainers.add(this);
4597 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4598 }
4599 mPrivateFlags |= SCROLL_CONTAINER;
4600 } else {
4601 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4602 mAttachInfo.mScrollContainers.remove(this);
4603 }
4604 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4605 }
4606 }
4607
4608 /**
4609 * Returns the quality of the drawing cache.
4610 *
4611 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4612 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4613 *
4614 * @see #setDrawingCacheQuality(int)
4615 * @see #setDrawingCacheEnabled(boolean)
4616 * @see #isDrawingCacheEnabled()
4617 *
4618 * @attr ref android.R.styleable#View_drawingCacheQuality
4619 */
4620 public int getDrawingCacheQuality() {
4621 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
4622 }
4623
4624 /**
4625 * Set the drawing cache quality of this view. This value is used only when the
4626 * drawing cache is enabled
4627 *
4628 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4629 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4630 *
4631 * @see #getDrawingCacheQuality()
4632 * @see #setDrawingCacheEnabled(boolean)
4633 * @see #isDrawingCacheEnabled()
4634 *
4635 * @attr ref android.R.styleable#View_drawingCacheQuality
4636 */
4637 public void setDrawingCacheQuality(int quality) {
4638 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
4639 }
4640
4641 /**
4642 * Returns whether the screen should remain on, corresponding to the current
4643 * value of {@link #KEEP_SCREEN_ON}.
4644 *
4645 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
4646 *
4647 * @see #setKeepScreenOn(boolean)
4648 *
4649 * @attr ref android.R.styleable#View_keepScreenOn
4650 */
4651 public boolean getKeepScreenOn() {
4652 return (mViewFlags & KEEP_SCREEN_ON) != 0;
4653 }
4654
4655 /**
4656 * Controls whether the screen should remain on, modifying the
4657 * value of {@link #KEEP_SCREEN_ON}.
4658 *
4659 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
4660 *
4661 * @see #getKeepScreenOn()
4662 *
4663 * @attr ref android.R.styleable#View_keepScreenOn
4664 */
4665 public void setKeepScreenOn(boolean keepScreenOn) {
4666 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
4667 }
4668
4669 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004670 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4671 * @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 -08004672 *
4673 * @attr ref android.R.styleable#View_nextFocusLeft
4674 */
4675 public int getNextFocusLeftId() {
4676 return mNextFocusLeftId;
4677 }
4678
4679 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004680 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4681 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
4682 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004683 *
4684 * @attr ref android.R.styleable#View_nextFocusLeft
4685 */
4686 public void setNextFocusLeftId(int nextFocusLeftId) {
4687 mNextFocusLeftId = nextFocusLeftId;
4688 }
4689
4690 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004691 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4692 * @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 -08004693 *
4694 * @attr ref android.R.styleable#View_nextFocusRight
4695 */
4696 public int getNextFocusRightId() {
4697 return mNextFocusRightId;
4698 }
4699
4700 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004701 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4702 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
4703 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004704 *
4705 * @attr ref android.R.styleable#View_nextFocusRight
4706 */
4707 public void setNextFocusRightId(int nextFocusRightId) {
4708 mNextFocusRightId = nextFocusRightId;
4709 }
4710
4711 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004712 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4713 * @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 -08004714 *
4715 * @attr ref android.R.styleable#View_nextFocusUp
4716 */
4717 public int getNextFocusUpId() {
4718 return mNextFocusUpId;
4719 }
4720
4721 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004722 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4723 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
4724 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004725 *
4726 * @attr ref android.R.styleable#View_nextFocusUp
4727 */
4728 public void setNextFocusUpId(int nextFocusUpId) {
4729 mNextFocusUpId = nextFocusUpId;
4730 }
4731
4732 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004733 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4734 * @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 -08004735 *
4736 * @attr ref android.R.styleable#View_nextFocusDown
4737 */
4738 public int getNextFocusDownId() {
4739 return mNextFocusDownId;
4740 }
4741
4742 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004743 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4744 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
4745 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004746 *
4747 * @attr ref android.R.styleable#View_nextFocusDown
4748 */
4749 public void setNextFocusDownId(int nextFocusDownId) {
4750 mNextFocusDownId = nextFocusDownId;
4751 }
4752
4753 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004754 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4755 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
4756 *
4757 * @attr ref android.R.styleable#View_nextFocusForward
4758 */
4759 public int getNextFocusForwardId() {
4760 return mNextFocusForwardId;
4761 }
4762
4763 /**
4764 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4765 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
4766 * decide automatically.
4767 *
4768 * @attr ref android.R.styleable#View_nextFocusForward
4769 */
4770 public void setNextFocusForwardId(int nextFocusForwardId) {
4771 mNextFocusForwardId = nextFocusForwardId;
4772 }
4773
4774 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004775 * Returns the visibility of this view and all of its ancestors
4776 *
4777 * @return True if this view and all of its ancestors are {@link #VISIBLE}
4778 */
4779 public boolean isShown() {
4780 View current = this;
4781 //noinspection ConstantConditions
4782 do {
4783 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4784 return false;
4785 }
4786 ViewParent parent = current.mParent;
4787 if (parent == null) {
4788 return false; // We are not attached to the view root
4789 }
4790 if (!(parent instanceof View)) {
4791 return true;
4792 }
4793 current = (View) parent;
4794 } while (current != null);
4795
4796 return false;
4797 }
4798
4799 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07004800 * Called by the view hierarchy when the content insets for a window have
4801 * changed, to allow it to adjust its content to fit within those windows.
4802 * The content insets tell you the space that the status bar, input method,
4803 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004804 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07004805 * <p>You do not normally need to deal with this function, since the default
4806 * window decoration given to applications takes care of applying it to the
4807 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
4808 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
4809 * and your content can be placed under those system elements. You can then
4810 * use this method within your view hierarchy if you have parts of your UI
4811 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004812 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07004813 * <p>The default implementation of this method simply applies the content
4814 * inset's to the view's padding. This can be enabled through
4815 * {@link #setFitsSystemWindows(boolean)}. Alternatively, you can override
4816 * the method and handle the insets however you would like. Note that the
4817 * insets provided by the framework are always relative to the far edges
4818 * of the window, not accounting for the location of the called view within
4819 * that window. (In fact when this method is called you do not yet know
4820 * where the layout will place the view, as it is done before layout happens.)
4821 *
4822 * <p>Note: unlike many View methods, there is no dispatch phase to this
4823 * call. If you are overriding it in a ViewGroup and want to allow the
4824 * call to continue to your children, you must be sure to call the super
4825 * implementation.
4826 *
4827 * @param insets Current content insets of the window. Prior to
4828 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
4829 * the insets or else you and Android will be unhappy.
4830 *
4831 * @return Return true if this view applied the insets and it should not
4832 * continue propagating further down the hierarchy, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004833 */
4834 protected boolean fitSystemWindows(Rect insets) {
4835 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07004836 mUserPaddingStart = -1;
4837 mUserPaddingEnd = -1;
4838 mUserPaddingRelative = false;
4839 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
4840 || mAttachInfo == null
4841 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
4842 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
4843 return true;
4844 } else {
4845 internalSetPadding(0, 0, 0, 0);
4846 return false;
4847 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004848 }
4849 return false;
4850 }
4851
4852 /**
Adam Powell0bd1d0a2011-07-22 19:35:06 -07004853 * Set whether or not this view should account for system screen decorations
4854 * such as the status bar and inset its content. This allows this view to be
4855 * positioned in absolute screen coordinates and remain visible to the user.
4856 *
4857 * <p>This should only be used by top-level window decor views.
4858 *
4859 * @param fitSystemWindows true to inset content for system screen decorations, false for
4860 * default behavior.
4861 *
4862 * @attr ref android.R.styleable#View_fitsSystemWindows
4863 */
4864 public void setFitsSystemWindows(boolean fitSystemWindows) {
4865 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
4866 }
4867
4868 /**
4869 * Check for the FITS_SYSTEM_WINDOWS flag. If this method returns true, this view
4870 * will account for system screen decorations such as the status bar and inset its
4871 * content. This allows the view to be positioned in absolute screen coordinates
4872 * and remain visible to the user.
4873 *
4874 * @return true if this view will adjust its content bounds for system screen decorations.
4875 *
4876 * @attr ref android.R.styleable#View_fitsSystemWindows
4877 */
4878 public boolean fitsSystemWindows() {
4879 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
4880 }
4881
4882 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07004883 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
4884 */
4885 public void requestFitSystemWindows() {
4886 if (mParent != null) {
4887 mParent.requestFitSystemWindows();
4888 }
4889 }
4890
4891 /**
4892 * For use by PhoneWindow to make its own system window fitting optional.
4893 * @hide
4894 */
4895 public void makeOptionalFitsSystemWindows() {
4896 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
4897 }
4898
4899 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004900 * Returns the visibility status for this view.
4901 *
4902 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4903 * @attr ref android.R.styleable#View_visibility
4904 */
4905 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004906 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
4907 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
4908 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004909 })
4910 public int getVisibility() {
4911 return mViewFlags & VISIBILITY_MASK;
4912 }
4913
4914 /**
4915 * Set the enabled state of this view.
4916 *
4917 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4918 * @attr ref android.R.styleable#View_visibility
4919 */
4920 @RemotableViewMethod
4921 public void setVisibility(int visibility) {
4922 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07004923 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004924 }
4925
4926 /**
4927 * Returns the enabled status for this view. The interpretation of the
4928 * enabled state varies by subclass.
4929 *
4930 * @return True if this view is enabled, false otherwise.
4931 */
4932 @ViewDebug.ExportedProperty
4933 public boolean isEnabled() {
4934 return (mViewFlags & ENABLED_MASK) == ENABLED;
4935 }
4936
4937 /**
4938 * Set the enabled state of this view. The interpretation of the enabled
4939 * state varies by subclass.
4940 *
4941 * @param enabled True if this view is enabled, false otherwise.
4942 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08004943 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004944 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07004945 if (enabled == isEnabled()) return;
4946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004947 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
4948
4949 /*
4950 * The View most likely has to change its appearance, so refresh
4951 * the drawable state.
4952 */
4953 refreshDrawableState();
4954
4955 // Invalidate too, since the default behavior for views is to be
4956 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08004957 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004958 }
4959
4960 /**
4961 * Set whether this view can receive the focus.
4962 *
4963 * Setting this to false will also ensure that this view is not focusable
4964 * in touch mode.
4965 *
4966 * @param focusable If true, this view can receive the focus.
4967 *
4968 * @see #setFocusableInTouchMode(boolean)
4969 * @attr ref android.R.styleable#View_focusable
4970 */
4971 public void setFocusable(boolean focusable) {
4972 if (!focusable) {
4973 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
4974 }
4975 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
4976 }
4977
4978 /**
4979 * Set whether this view can receive focus while in touch mode.
4980 *
4981 * Setting this to true will also ensure that this view is focusable.
4982 *
4983 * @param focusableInTouchMode If true, this view can receive the focus while
4984 * in touch mode.
4985 *
4986 * @see #setFocusable(boolean)
4987 * @attr ref android.R.styleable#View_focusableInTouchMode
4988 */
4989 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
4990 // Focusable in touch mode should always be set before the focusable flag
4991 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
4992 // which, in touch mode, will not successfully request focus on this view
4993 // because the focusable in touch mode flag is not set
4994 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
4995 if (focusableInTouchMode) {
4996 setFlags(FOCUSABLE, FOCUSABLE_MASK);
4997 }
4998 }
4999
5000 /**
5001 * Set whether this view should have sound effects enabled for events such as
5002 * clicking and touching.
5003 *
5004 * <p>You may wish to disable sound effects for a view if you already play sounds,
5005 * for instance, a dial key that plays dtmf tones.
5006 *
5007 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5008 * @see #isSoundEffectsEnabled()
5009 * @see #playSoundEffect(int)
5010 * @attr ref android.R.styleable#View_soundEffectsEnabled
5011 */
5012 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5013 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5014 }
5015
5016 /**
5017 * @return whether this view should have sound effects enabled for events such as
5018 * clicking and touching.
5019 *
5020 * @see #setSoundEffectsEnabled(boolean)
5021 * @see #playSoundEffect(int)
5022 * @attr ref android.R.styleable#View_soundEffectsEnabled
5023 */
5024 @ViewDebug.ExportedProperty
5025 public boolean isSoundEffectsEnabled() {
5026 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5027 }
5028
5029 /**
5030 * Set whether this view should have haptic feedback for events such as
5031 * long presses.
5032 *
5033 * <p>You may wish to disable haptic feedback if your view already controls
5034 * its own haptic feedback.
5035 *
5036 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5037 * @see #isHapticFeedbackEnabled()
5038 * @see #performHapticFeedback(int)
5039 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5040 */
5041 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5042 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5043 }
5044
5045 /**
5046 * @return whether this view should have haptic feedback enabled for events
5047 * long presses.
5048 *
5049 * @see #setHapticFeedbackEnabled(boolean)
5050 * @see #performHapticFeedback(int)
5051 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5052 */
5053 @ViewDebug.ExportedProperty
5054 public boolean isHapticFeedbackEnabled() {
5055 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5056 }
5057
5058 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005059 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005060 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005061 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5062 * {@link #LAYOUT_DIRECTION_RTL},
5063 * {@link #LAYOUT_DIRECTION_INHERIT} or
5064 * {@link #LAYOUT_DIRECTION_LOCALE}.
5065 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005066 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005067 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005068 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5069 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5070 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5071 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005072 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005073 public int getLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005074 return (mPrivateFlags2 & LAYOUT_DIRECTION_MASK) >> LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005075 }
5076
5077 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005078 * Set the layout direction for this view. This will propagate a reset of layout direction
5079 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005080 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005081 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
5082 * {@link #LAYOUT_DIRECTION_RTL},
5083 * {@link #LAYOUT_DIRECTION_INHERIT} or
5084 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005085 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005086 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005087 */
5088 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005089 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005090 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005091 // Reset the current layout direction and the resolved one
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005092 mPrivateFlags2 &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07005093 resetResolvedLayoutDirection();
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005094 // Set the new layout direction (filtered) and ask for a layout pass
5095 mPrivateFlags2 |=
5096 ((layoutDirection << LAYOUT_DIRECTION_MASK_SHIFT) & LAYOUT_DIRECTION_MASK);
5097 requestLayout();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005098 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005099 }
5100
5101 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005102 * Returns the resolved layout direction for this view.
5103 *
5104 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005105 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005106 */
5107 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005108 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5109 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005110 })
5111 public int getResolvedLayoutDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -07005112 // The layout diretion will be resolved only if needed
5113 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) != LAYOUT_DIRECTION_RESOLVED) {
5114 resolveLayoutDirection();
5115 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07005116 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005117 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
5118 }
5119
5120 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005121 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5122 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005123 *
5124 * @return true if the layout is right-to-left.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005125 */
5126 @ViewDebug.ExportedProperty(category = "layout")
5127 public boolean isLayoutRtl() {
5128 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
5129 }
5130
5131 /**
Adam Powell539ee872012-02-03 19:00:49 -08005132 * Indicates whether the view is currently tracking transient state that the
5133 * app should not need to concern itself with saving and restoring, but that
5134 * the framework should take special note to preserve when possible.
5135 *
5136 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005137 */
5138 @ViewDebug.ExportedProperty(category = "layout")
5139 public boolean hasTransientState() {
5140 return (mPrivateFlags2 & HAS_TRANSIENT_STATE) == HAS_TRANSIENT_STATE;
5141 }
5142
5143 /**
5144 * Set whether this view is currently tracking transient state that the
5145 * framework should attempt to preserve when possible.
5146 *
5147 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005148 */
5149 public void setHasTransientState(boolean hasTransientState) {
5150 if (hasTransientState() == hasTransientState) return;
5151
5152 mPrivateFlags2 = (mPrivateFlags2 & ~HAS_TRANSIENT_STATE) |
5153 (hasTransientState ? HAS_TRANSIENT_STATE : 0);
5154 if (mParent != null) {
5155 try {
5156 mParent.childHasTransientStateChanged(this, hasTransientState);
5157 } catch (AbstractMethodError e) {
5158 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5159 " does not fully implement ViewParent", e);
5160 }
5161 }
5162 }
5163
5164 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005165 * If this view doesn't do any drawing on its own, set this flag to
5166 * allow further optimizations. By default, this flag is not set on
5167 * View, but could be set on some View subclasses such as ViewGroup.
5168 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005169 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5170 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005171 *
5172 * @param willNotDraw whether or not this View draw on its own
5173 */
5174 public void setWillNotDraw(boolean willNotDraw) {
5175 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5176 }
5177
5178 /**
5179 * Returns whether or not this View draws on its own.
5180 *
5181 * @return true if this view has nothing to draw, false otherwise
5182 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005183 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005184 public boolean willNotDraw() {
5185 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
5186 }
5187
5188 /**
5189 * When a View's drawing cache is enabled, drawing is redirected to an
5190 * offscreen bitmap. Some views, like an ImageView, must be able to
5191 * bypass this mechanism if they already draw a single bitmap, to avoid
5192 * unnecessary usage of the memory.
5193 *
5194 * @param willNotCacheDrawing true if this view does not cache its
5195 * drawing, false otherwise
5196 */
5197 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
5198 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
5199 }
5200
5201 /**
5202 * Returns whether or not this View can cache its drawing or not.
5203 *
5204 * @return true if this view does not cache its drawing, false otherwise
5205 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005206 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005207 public boolean willNotCacheDrawing() {
5208 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5209 }
5210
5211 /**
5212 * Indicates whether this view reacts to click events or not.
5213 *
5214 * @return true if the view is clickable, false otherwise
5215 *
5216 * @see #setClickable(boolean)
5217 * @attr ref android.R.styleable#View_clickable
5218 */
5219 @ViewDebug.ExportedProperty
5220 public boolean isClickable() {
5221 return (mViewFlags & CLICKABLE) == CLICKABLE;
5222 }
5223
5224 /**
5225 * Enables or disables click events for this view. When a view
5226 * is clickable it will change its state to "pressed" on every click.
5227 * Subclasses should set the view clickable to visually react to
5228 * user's clicks.
5229 *
5230 * @param clickable true to make the view clickable, false otherwise
5231 *
5232 * @see #isClickable()
5233 * @attr ref android.R.styleable#View_clickable
5234 */
5235 public void setClickable(boolean clickable) {
5236 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5237 }
5238
5239 /**
5240 * Indicates whether this view reacts to long click events or not.
5241 *
5242 * @return true if the view is long clickable, false otherwise
5243 *
5244 * @see #setLongClickable(boolean)
5245 * @attr ref android.R.styleable#View_longClickable
5246 */
5247 public boolean isLongClickable() {
5248 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5249 }
5250
5251 /**
5252 * Enables or disables long click events for this view. When a view is long
5253 * clickable it reacts to the user holding down the button for a longer
5254 * duration than a tap. This event can either launch the listener or a
5255 * context menu.
5256 *
5257 * @param longClickable true to make the view long clickable, false otherwise
5258 * @see #isLongClickable()
5259 * @attr ref android.R.styleable#View_longClickable
5260 */
5261 public void setLongClickable(boolean longClickable) {
5262 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5263 }
5264
5265 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005266 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005267 *
5268 * @see #isClickable()
5269 * @see #setClickable(boolean)
5270 *
5271 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5272 * the View's internal state from a previously set "pressed" state.
5273 */
5274 public void setPressed(boolean pressed) {
Adam Powell035a1fc2012-02-27 15:23:50 -08005275 final boolean needsRefresh = pressed != ((mPrivateFlags & PRESSED) == PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005277 if (pressed) {
5278 mPrivateFlags |= PRESSED;
5279 } else {
5280 mPrivateFlags &= ~PRESSED;
5281 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005282
5283 if (needsRefresh) {
5284 refreshDrawableState();
5285 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005286 dispatchSetPressed(pressed);
5287 }
5288
5289 /**
5290 * Dispatch setPressed to all of this View's children.
5291 *
5292 * @see #setPressed(boolean)
5293 *
5294 * @param pressed The new pressed state
5295 */
5296 protected void dispatchSetPressed(boolean pressed) {
5297 }
5298
5299 /**
5300 * Indicates whether the view is currently in pressed state. Unless
5301 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5302 * the pressed state.
5303 *
Philip Milne6c8ea062012-04-03 17:38:43 -07005304 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005305 * @see #isClickable()
5306 * @see #setClickable(boolean)
5307 *
5308 * @return true if the view is currently pressed, false otherwise
5309 */
5310 public boolean isPressed() {
5311 return (mPrivateFlags & PRESSED) == PRESSED;
5312 }
5313
5314 /**
5315 * Indicates whether this view will save its state (that is,
5316 * whether its {@link #onSaveInstanceState} method will be called).
5317 *
5318 * @return Returns true if the view state saving is enabled, else false.
5319 *
5320 * @see #setSaveEnabled(boolean)
5321 * @attr ref android.R.styleable#View_saveEnabled
5322 */
5323 public boolean isSaveEnabled() {
5324 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5325 }
5326
5327 /**
5328 * Controls whether the saving of this view's state is
5329 * enabled (that is, whether its {@link #onSaveInstanceState} method
5330 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005331 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005332 * for its state to be saved. This flag can only disable the
5333 * saving of this view; any child views may still have their state saved.
5334 *
5335 * @param enabled Set to false to <em>disable</em> state saving, or true
5336 * (the default) to allow it.
5337 *
5338 * @see #isSaveEnabled()
5339 * @see #setId(int)
5340 * @see #onSaveInstanceState()
5341 * @attr ref android.R.styleable#View_saveEnabled
5342 */
5343 public void setSaveEnabled(boolean enabled) {
5344 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5345 }
5346
Jeff Brown85a31762010-09-01 17:01:00 -07005347 /**
5348 * Gets whether the framework should discard touches when the view's
5349 * window is obscured by another visible window.
5350 * Refer to the {@link View} security documentation for more details.
5351 *
5352 * @return True if touch filtering is enabled.
5353 *
5354 * @see #setFilterTouchesWhenObscured(boolean)
5355 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5356 */
5357 @ViewDebug.ExportedProperty
5358 public boolean getFilterTouchesWhenObscured() {
5359 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5360 }
5361
5362 /**
5363 * Sets whether the framework should discard touches when the view's
5364 * window is obscured by another visible window.
5365 * Refer to the {@link View} security documentation for more details.
5366 *
5367 * @param enabled True if touch filtering should be enabled.
5368 *
5369 * @see #getFilterTouchesWhenObscured
5370 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5371 */
5372 public void setFilterTouchesWhenObscured(boolean enabled) {
5373 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5374 FILTER_TOUCHES_WHEN_OBSCURED);
5375 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005376
5377 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005378 * Indicates whether the entire hierarchy under this view will save its
5379 * state when a state saving traversal occurs from its parent. The default
5380 * is true; if false, these views will not be saved unless
5381 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5382 *
5383 * @return Returns true if the view state saving from parent is enabled, else false.
5384 *
5385 * @see #setSaveFromParentEnabled(boolean)
5386 */
5387 public boolean isSaveFromParentEnabled() {
5388 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5389 }
5390
5391 /**
5392 * Controls whether the entire hierarchy under this view will save its
5393 * state when a state saving traversal occurs from its parent. The default
5394 * is true; if false, these views will not be saved unless
5395 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5396 *
5397 * @param enabled Set to false to <em>disable</em> state saving, or true
5398 * (the default) to allow it.
5399 *
5400 * @see #isSaveFromParentEnabled()
5401 * @see #setId(int)
5402 * @see #onSaveInstanceState()
5403 */
5404 public void setSaveFromParentEnabled(boolean enabled) {
5405 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5406 }
5407
5408
5409 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005410 * Returns whether this View is able to take focus.
5411 *
5412 * @return True if this view can take focus, or false otherwise.
5413 * @attr ref android.R.styleable#View_focusable
5414 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005415 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005416 public final boolean isFocusable() {
5417 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
5418 }
5419
5420 /**
5421 * When a view is focusable, it may not want to take focus when in touch mode.
5422 * For example, a button would like focus when the user is navigating via a D-pad
5423 * so that the user can click on it, but once the user starts touching the screen,
5424 * the button shouldn't take focus
5425 * @return Whether the view is focusable in touch mode.
5426 * @attr ref android.R.styleable#View_focusableInTouchMode
5427 */
5428 @ViewDebug.ExportedProperty
5429 public final boolean isFocusableInTouchMode() {
5430 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5431 }
5432
5433 /**
5434 * Find the nearest view in the specified direction that can take focus.
5435 * This does not actually give focus to that view.
5436 *
5437 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5438 *
5439 * @return The nearest focusable in the specified direction, or null if none
5440 * can be found.
5441 */
5442 public View focusSearch(int direction) {
5443 if (mParent != null) {
5444 return mParent.focusSearch(this, direction);
5445 } else {
5446 return null;
5447 }
5448 }
5449
5450 /**
5451 * This method is the last chance for the focused view and its ancestors to
5452 * respond to an arrow key. This is called when the focused view did not
5453 * consume the key internally, nor could the view system find a new view in
5454 * the requested direction to give focus to.
5455 *
5456 * @param focused The currently focused view.
5457 * @param direction The direction focus wants to move. One of FOCUS_UP,
5458 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5459 * @return True if the this view consumed this unhandled move.
5460 */
5461 public boolean dispatchUnhandledMove(View focused, int direction) {
5462 return false;
5463 }
5464
5465 /**
5466 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005467 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005468 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005469 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5470 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005471 * @return The user specified next view, or null if there is none.
5472 */
5473 View findUserSetNextFocus(View root, int direction) {
5474 switch (direction) {
5475 case FOCUS_LEFT:
5476 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005477 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005478 case FOCUS_RIGHT:
5479 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005480 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005481 case FOCUS_UP:
5482 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005483 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005484 case FOCUS_DOWN:
5485 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005486 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005487 case FOCUS_FORWARD:
5488 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005489 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005490 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08005491 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08005492 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005493 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005494 @Override
5495 public boolean apply(View t) {
5496 return t.mNextFocusForwardId == id;
5497 }
5498 });
5499 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005500 }
5501 return null;
5502 }
5503
Jeff Brown4dfbec22011-08-15 14:55:37 -07005504 private View findViewInsideOutShouldExist(View root, final int childViewId) {
5505 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
5506 @Override
5507 public boolean apply(View t) {
5508 return t.mID == childViewId;
5509 }
5510 });
5511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005512 if (result == null) {
5513 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
5514 + "by user for id " + childViewId);
5515 }
5516 return result;
5517 }
5518
5519 /**
5520 * Find and return all focusable views that are descendants of this view,
5521 * possibly including this view if it is focusable itself.
5522 *
5523 * @param direction The direction of the focus
5524 * @return A list of focusable views
5525 */
5526 public ArrayList<View> getFocusables(int direction) {
5527 ArrayList<View> result = new ArrayList<View>(24);
5528 addFocusables(result, direction);
5529 return result;
5530 }
5531
5532 /**
5533 * Add any focusable views that are descendants of this view (possibly
5534 * including this view if it is focusable itself) to views. If we are in touch mode,
5535 * only add views that are also focusable in touch mode.
5536 *
5537 * @param views Focusable views found so far
5538 * @param direction The direction of the focus
5539 */
5540 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005541 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
5542 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005543
svetoslavganov75986cf2009-05-14 22:28:01 -07005544 /**
5545 * Adds any focusable views that are descendants of this view (possibly
5546 * including this view if it is focusable itself) to views. This method
5547 * adds all focusable views regardless if we are in touch mode or
5548 * only views focusable in touch mode if we are in touch mode depending on
5549 * the focusable mode paramater.
5550 *
5551 * @param views Focusable views found so far or null if all we are interested is
5552 * the number of focusables.
5553 * @param direction The direction of the focus.
5554 * @param focusableMode The type of focusables to be added.
5555 *
5556 * @see #FOCUSABLES_ALL
5557 * @see #FOCUSABLES_TOUCH_MODE
5558 */
5559 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
5560 if (!isFocusable()) {
5561 return;
5562 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005563
svetoslavganov75986cf2009-05-14 22:28:01 -07005564 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
5565 isInTouchMode() && !isFocusableInTouchMode()) {
5566 return;
5567 }
5568
5569 if (views != null) {
5570 views.add(this);
5571 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005572 }
5573
5574 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005575 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005576 * The search is performed by either the text that the View renders or the content
5577 * description that describes the view for accessibility purposes and the view does
5578 * not render or both. Clients can specify how the search is to be performed via
5579 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
5580 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005581 *
5582 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005583 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07005584 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005585 * @see #FIND_VIEWS_WITH_TEXT
5586 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
5587 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005588 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005589 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07005590 if (getAccessibilityNodeProvider() != null) {
5591 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
5592 outViews.add(this);
5593 }
5594 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
5595 && !TextUtils.isEmpty(searched) && !TextUtils.isEmpty(mContentDescription)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005596 String searchedLowerCase = searched.toString().toLowerCase();
5597 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
5598 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
5599 outViews.add(this);
5600 }
5601 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005602 }
5603
5604 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005605 * Find and return all touchable views that are descendants of this view,
5606 * possibly including this view if it is touchable itself.
5607 *
5608 * @return A list of touchable views
5609 */
5610 public ArrayList<View> getTouchables() {
5611 ArrayList<View> result = new ArrayList<View>();
5612 addTouchables(result);
5613 return result;
5614 }
5615
5616 /**
5617 * Add any touchable views that are descendants of this view (possibly
5618 * including this view if it is touchable itself) to views.
5619 *
5620 * @param views Touchable views found so far
5621 */
5622 public void addTouchables(ArrayList<View> views) {
5623 final int viewFlags = mViewFlags;
5624
5625 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
5626 && (viewFlags & ENABLED_MASK) == ENABLED) {
5627 views.add(this);
5628 }
5629 }
5630
5631 /**
5632 * Call this to try to give focus to a specific view or to one of its
5633 * descendants.
5634 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005635 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5636 * false), or if it is focusable and it is not focusable in touch mode
5637 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005638 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005639 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005640 * have focus, and you want your parent to look for the next one.
5641 *
5642 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
5643 * {@link #FOCUS_DOWN} and <code>null</code>.
5644 *
5645 * @return Whether this view or one of its descendants actually took focus.
5646 */
5647 public final boolean requestFocus() {
5648 return requestFocus(View.FOCUS_DOWN);
5649 }
5650
5651
5652 /**
5653 * Call this to try to give focus to a specific view or to one of its
5654 * descendants and give it a hint about what direction focus is heading.
5655 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005656 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5657 * false), or if it is focusable and it is not focusable in touch mode
5658 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005659 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005660 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005661 * have focus, and you want your parent to look for the next one.
5662 *
5663 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
5664 * <code>null</code> set for the previously focused rectangle.
5665 *
5666 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5667 * @return Whether this view or one of its descendants actually took focus.
5668 */
5669 public final boolean requestFocus(int direction) {
5670 return requestFocus(direction, null);
5671 }
5672
5673 /**
5674 * Call this to try to give focus to a specific view or to one of its descendants
5675 * and give it hints about the direction and a specific rectangle that the focus
5676 * is coming from. The rectangle can help give larger views a finer grained hint
5677 * about where focus is coming from, and therefore, where to show selection, or
5678 * forward focus change internally.
5679 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005680 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5681 * false), or if it is focusable and it is not focusable in touch mode
5682 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005683 *
5684 * A View will not take focus if it is not visible.
5685 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005686 * A View will not take focus if one of its parents has
5687 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
5688 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005689 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005690 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005691 * have focus, and you want your parent to look for the next one.
5692 *
5693 * You may wish to override this method if your custom {@link View} has an internal
5694 * {@link View} that it wishes to forward the request to.
5695 *
5696 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5697 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
5698 * to give a finer grained hint about where focus is coming from. May be null
5699 * if there is no hint.
5700 * @return Whether this view or one of its descendants actually took focus.
5701 */
5702 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
5703 // need to be focusable
5704 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
5705 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5706 return false;
5707 }
5708
5709 // need to be focusable in touch mode if in touch mode
5710 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005711 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
5712 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005713 }
5714
5715 // need to not have any parents blocking us
5716 if (hasAncestorThatBlocksDescendantFocus()) {
5717 return false;
5718 }
5719
5720 handleFocusGainInternal(direction, previouslyFocusedRect);
5721 return true;
5722 }
5723
5724 /**
5725 * Call this to try to give focus to a specific view or to one of its descendants. This is a
5726 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
5727 * touch mode to request focus when they are touched.
5728 *
5729 * @return Whether this view or one of its descendants actually took focus.
5730 *
5731 * @see #isInTouchMode()
5732 *
5733 */
5734 public final boolean requestFocusFromTouch() {
5735 // Leave touch mode if we need to
5736 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005737 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07005738 if (viewRoot != null) {
5739 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005740 }
5741 }
5742 return requestFocus(View.FOCUS_DOWN);
5743 }
5744
5745 /**
5746 * @return Whether any ancestor of this view blocks descendant focus.
5747 */
5748 private boolean hasAncestorThatBlocksDescendantFocus() {
5749 ViewParent ancestor = mParent;
5750 while (ancestor instanceof ViewGroup) {
5751 final ViewGroup vgAncestor = (ViewGroup) ancestor;
5752 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
5753 return true;
5754 } else {
5755 ancestor = vgAncestor.getParent();
5756 }
5757 }
5758 return false;
5759 }
5760
5761 /**
Romain Guya440b002010-02-24 15:57:54 -08005762 * @hide
5763 */
5764 public void dispatchStartTemporaryDetach() {
5765 onStartTemporaryDetach();
5766 }
5767
5768 /**
5769 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005770 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
5771 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08005772 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005773 */
5774 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08005775 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08005776 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08005777 }
5778
5779 /**
5780 * @hide
5781 */
5782 public void dispatchFinishTemporaryDetach() {
5783 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005784 }
Romain Guy8506ab42009-06-11 17:35:47 -07005785
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005786 /**
5787 * Called after {@link #onStartTemporaryDetach} when the container is done
5788 * changing the view.
5789 */
5790 public void onFinishTemporaryDetach() {
5791 }
Romain Guy8506ab42009-06-11 17:35:47 -07005792
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005793 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005794 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
5795 * for this view's window. Returns null if the view is not currently attached
5796 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07005797 * just use the standard high-level event callbacks like
5798 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005799 */
5800 public KeyEvent.DispatcherState getKeyDispatcherState() {
5801 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
5802 }
Joe Malin32736f02011-01-19 16:14:20 -08005803
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005804 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005805 * Dispatch a key event before it is processed by any input method
5806 * associated with the view hierarchy. This can be used to intercept
5807 * key events in special situations before the IME consumes them; a
5808 * typical example would be handling the BACK key to update the application's
5809 * UI instead of allowing the IME to see it and close itself.
5810 *
5811 * @param event The key event to be dispatched.
5812 * @return True if the event was handled, false otherwise.
5813 */
5814 public boolean dispatchKeyEventPreIme(KeyEvent event) {
5815 return onKeyPreIme(event.getKeyCode(), event);
5816 }
5817
5818 /**
5819 * Dispatch a key event to the next view on the focus path. This path runs
5820 * from the top of the view tree down to the currently focused view. If this
5821 * view has focus, it will dispatch to itself. Otherwise it will dispatch
5822 * the next node down the focus path. This method also fires any key
5823 * listeners.
5824 *
5825 * @param event The key event to be dispatched.
5826 * @return True if the event was handled, false otherwise.
5827 */
5828 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005829 if (mInputEventConsistencyVerifier != null) {
5830 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
5831 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005832
Jeff Brown21bc5c92011-02-28 18:27:14 -08005833 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07005834 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005835 ListenerInfo li = mListenerInfo;
5836 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5837 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005838 return true;
5839 }
5840
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005841 if (event.dispatch(this, mAttachInfo != null
5842 ? mAttachInfo.mKeyDispatchState : null, this)) {
5843 return true;
5844 }
5845
5846 if (mInputEventConsistencyVerifier != null) {
5847 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5848 }
5849 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005850 }
5851
5852 /**
5853 * Dispatches a key shortcut event.
5854 *
5855 * @param event The key event to be dispatched.
5856 * @return True if the event was handled by the view, false otherwise.
5857 */
5858 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
5859 return onKeyShortcut(event.getKeyCode(), event);
5860 }
5861
5862 /**
5863 * Pass the touch screen motion event down to the target view, or this
5864 * view if it is the target.
5865 *
5866 * @param event The motion event to be dispatched.
5867 * @return True if the event was handled by the view, false otherwise.
5868 */
5869 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005870 if (mInputEventConsistencyVerifier != null) {
5871 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
5872 }
5873
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005874 if (onFilterTouchEventForSecurity(event)) {
5875 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005876 ListenerInfo li = mListenerInfo;
5877 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5878 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005879 return true;
5880 }
5881
5882 if (onTouchEvent(event)) {
5883 return true;
5884 }
Jeff Brown85a31762010-09-01 17:01:00 -07005885 }
5886
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005887 if (mInputEventConsistencyVerifier != null) {
5888 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005889 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005890 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005891 }
5892
5893 /**
Jeff Brown85a31762010-09-01 17:01:00 -07005894 * Filter the touch event to apply security policies.
5895 *
5896 * @param event The motion event to be filtered.
5897 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08005898 *
Jeff Brown85a31762010-09-01 17:01:00 -07005899 * @see #getFilterTouchesWhenObscured
5900 */
5901 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07005902 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07005903 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
5904 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
5905 // Window is obscured, drop this touch.
5906 return false;
5907 }
5908 return true;
5909 }
5910
5911 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005912 * Pass a trackball motion event down to the focused view.
5913 *
5914 * @param event The motion event to be dispatched.
5915 * @return True if the event was handled by the view, false otherwise.
5916 */
5917 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005918 if (mInputEventConsistencyVerifier != null) {
5919 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
5920 }
5921
Romain Guy02ccac62011-06-24 13:20:23 -07005922 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005923 }
5924
5925 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005926 * Dispatch a generic motion event.
5927 * <p>
5928 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5929 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08005930 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07005931 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005932 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08005933 *
5934 * @param event The motion event to be dispatched.
5935 * @return True if the event was handled by the view, false otherwise.
5936 */
5937 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005938 if (mInputEventConsistencyVerifier != null) {
5939 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
5940 }
5941
Jeff Browna032cc02011-03-07 16:56:21 -08005942 final int source = event.getSource();
5943 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
5944 final int action = event.getAction();
5945 if (action == MotionEvent.ACTION_HOVER_ENTER
5946 || action == MotionEvent.ACTION_HOVER_MOVE
5947 || action == MotionEvent.ACTION_HOVER_EXIT) {
5948 if (dispatchHoverEvent(event)) {
5949 return true;
5950 }
5951 } else if (dispatchGenericPointerEvent(event)) {
5952 return true;
5953 }
5954 } else if (dispatchGenericFocusedEvent(event)) {
5955 return true;
5956 }
5957
Jeff Brown10b62902011-06-20 16:40:37 -07005958 if (dispatchGenericMotionEventInternal(event)) {
5959 return true;
5960 }
5961
5962 if (mInputEventConsistencyVerifier != null) {
5963 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5964 }
5965 return false;
5966 }
5967
5968 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07005969 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005970 ListenerInfo li = mListenerInfo;
5971 if (li != null && li.mOnGenericMotionListener != null
5972 && (mViewFlags & ENABLED_MASK) == ENABLED
5973 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08005974 return true;
5975 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005976
5977 if (onGenericMotionEvent(event)) {
5978 return true;
5979 }
5980
5981 if (mInputEventConsistencyVerifier != null) {
5982 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5983 }
5984 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005985 }
5986
5987 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005988 * Dispatch a hover event.
5989 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07005990 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07005991 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005992 * </p>
5993 *
5994 * @param event The motion event to be dispatched.
5995 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005996 */
5997 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07005998 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005999 ListenerInfo li = mListenerInfo;
6000 if (li != null && li.mOnHoverListener != null
6001 && (mViewFlags & ENABLED_MASK) == ENABLED
6002 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07006003 return true;
6004 }
6005
Jeff Browna032cc02011-03-07 16:56:21 -08006006 return onHoverEvent(event);
6007 }
6008
6009 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006010 * Returns true if the view has a child to which it has recently sent
6011 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
6012 * it does not have a hovered child, then it must be the innermost hovered view.
6013 * @hide
6014 */
6015 protected boolean hasHoveredChild() {
6016 return false;
6017 }
6018
6019 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006020 * Dispatch a generic motion event to the view under the first pointer.
6021 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006022 * Do not call this method directly.
6023 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006024 * </p>
6025 *
6026 * @param event The motion event to be dispatched.
6027 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006028 */
6029 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
6030 return false;
6031 }
6032
6033 /**
6034 * Dispatch a generic motion event to the currently focused view.
6035 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006036 * Do not call this method directly.
6037 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006038 * </p>
6039 *
6040 * @param event The motion event to be dispatched.
6041 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006042 */
6043 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
6044 return false;
6045 }
6046
6047 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08006048 * Dispatch a pointer event.
6049 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006050 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
6051 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
6052 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08006053 * and should not be expected to handle other pointing device features.
6054 * </p>
6055 *
6056 * @param event The motion event to be dispatched.
6057 * @return True if the event was handled by the view, false otherwise.
6058 * @hide
6059 */
6060 public final boolean dispatchPointerEvent(MotionEvent event) {
6061 if (event.isTouchEvent()) {
6062 return dispatchTouchEvent(event);
6063 } else {
6064 return dispatchGenericMotionEvent(event);
6065 }
6066 }
6067
6068 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006069 * Called when the window containing this view gains or loses window focus.
6070 * ViewGroups should override to route to their children.
6071 *
6072 * @param hasFocus True if the window containing this view now has focus,
6073 * false otherwise.
6074 */
6075 public void dispatchWindowFocusChanged(boolean hasFocus) {
6076 onWindowFocusChanged(hasFocus);
6077 }
6078
6079 /**
6080 * Called when the window containing this view gains or loses focus. Note
6081 * that this is separate from view focus: to receive key events, both
6082 * your view and its window must have focus. If a window is displayed
6083 * on top of yours that takes input focus, then your own window will lose
6084 * focus but the view focus will remain unchanged.
6085 *
6086 * @param hasWindowFocus True if the window containing this view now has
6087 * focus, false otherwise.
6088 */
6089 public void onWindowFocusChanged(boolean hasWindowFocus) {
6090 InputMethodManager imm = InputMethodManager.peekInstance();
6091 if (!hasWindowFocus) {
6092 if (isPressed()) {
6093 setPressed(false);
6094 }
6095 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
6096 imm.focusOut(this);
6097 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006098 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08006099 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07006100 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006101 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
6102 imm.focusIn(this);
6103 }
6104 refreshDrawableState();
6105 }
6106
6107 /**
6108 * Returns true if this view is in a window that currently has window focus.
6109 * Note that this is not the same as the view itself having focus.
6110 *
6111 * @return True if this view is in a window that currently has window focus.
6112 */
6113 public boolean hasWindowFocus() {
6114 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
6115 }
6116
6117 /**
Adam Powell326d8082009-12-09 15:10:07 -08006118 * Dispatch a view visibility change down the view hierarchy.
6119 * ViewGroups should override to route to their children.
6120 * @param changedView The view whose visibility changed. Could be 'this' or
6121 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08006122 * @param visibility The new visibility of changedView: {@link #VISIBLE},
6123 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08006124 */
6125 protected void dispatchVisibilityChanged(View changedView, int visibility) {
6126 onVisibilityChanged(changedView, visibility);
6127 }
6128
6129 /**
6130 * Called when the visibility of the view or an ancestor of the view is changed.
6131 * @param changedView The view whose visibility changed. Could be 'this' or
6132 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08006133 * @param visibility The new visibility of changedView: {@link #VISIBLE},
6134 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08006135 */
6136 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07006137 if (visibility == VISIBLE) {
6138 if (mAttachInfo != null) {
6139 initialAwakenScrollBars();
6140 } else {
6141 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
6142 }
6143 }
Adam Powell326d8082009-12-09 15:10:07 -08006144 }
6145
6146 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08006147 * Dispatch a hint about whether this view is displayed. For instance, when
6148 * a View moves out of the screen, it might receives a display hint indicating
6149 * the view is not displayed. Applications should not <em>rely</em> on this hint
6150 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08006151 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08006152 * @param hint A hint about whether or not this view is displayed:
6153 * {@link #VISIBLE} or {@link #INVISIBLE}.
6154 */
6155 public void dispatchDisplayHint(int hint) {
6156 onDisplayHint(hint);
6157 }
6158
6159 /**
6160 * Gives this view a hint about whether is displayed or not. For instance, when
6161 * a View moves out of the screen, it might receives a display hint indicating
6162 * the view is not displayed. Applications should not <em>rely</em> on this hint
6163 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08006164 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08006165 * @param hint A hint about whether or not this view is displayed:
6166 * {@link #VISIBLE} or {@link #INVISIBLE}.
6167 */
6168 protected void onDisplayHint(int hint) {
6169 }
6170
6171 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006172 * Dispatch a window visibility change down the view hierarchy.
6173 * ViewGroups should override to route to their children.
6174 *
6175 * @param visibility The new visibility of the window.
6176 *
Philip Milne6c8ea062012-04-03 17:38:43 -07006177 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006178 */
6179 public void dispatchWindowVisibilityChanged(int visibility) {
6180 onWindowVisibilityChanged(visibility);
6181 }
6182
6183 /**
6184 * Called when the window containing has change its visibility
6185 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
6186 * that this tells you whether or not your window is being made visible
6187 * to the window manager; this does <em>not</em> tell you whether or not
6188 * your window is obscured by other windows on the screen, even if it
6189 * is itself visible.
6190 *
6191 * @param visibility The new visibility of the window.
6192 */
6193 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07006194 if (visibility == VISIBLE) {
6195 initialAwakenScrollBars();
6196 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006197 }
6198
6199 /**
6200 * Returns the current visibility of the window this view is attached to
6201 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
6202 *
6203 * @return Returns the current visibility of the view's window.
6204 */
6205 public int getWindowVisibility() {
6206 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
6207 }
6208
6209 /**
6210 * Retrieve the overall visible display size in which the window this view is
6211 * attached to has been positioned in. This takes into account screen
6212 * decorations above the window, for both cases where the window itself
6213 * is being position inside of them or the window is being placed under
6214 * then and covered insets are used for the window to position its content
6215 * inside. In effect, this tells you the available area where content can
6216 * be placed and remain visible to users.
6217 *
6218 * <p>This function requires an IPC back to the window manager to retrieve
6219 * the requested information, so should not be used in performance critical
6220 * code like drawing.
6221 *
6222 * @param outRect Filled in with the visible display frame. If the view
6223 * is not attached to a window, this is simply the raw display size.
6224 */
6225 public void getWindowVisibleDisplayFrame(Rect outRect) {
6226 if (mAttachInfo != null) {
6227 try {
6228 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
6229 } catch (RemoteException e) {
6230 return;
6231 }
6232 // XXX This is really broken, and probably all needs to be done
6233 // in the window manager, and we need to know more about whether
6234 // we want the area behind or in front of the IME.
6235 final Rect insets = mAttachInfo.mVisibleInsets;
6236 outRect.left += insets.left;
6237 outRect.top += insets.top;
6238 outRect.right -= insets.right;
6239 outRect.bottom -= insets.bottom;
6240 return;
6241 }
6242 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07006243 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006244 }
6245
6246 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006247 * Dispatch a notification about a resource configuration change down
6248 * the view hierarchy.
6249 * ViewGroups should override to route to their children.
6250 *
6251 * @param newConfig The new resource configuration.
6252 *
Philip Milne6c8ea062012-04-03 17:38:43 -07006253 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006254 */
6255 public void dispatchConfigurationChanged(Configuration newConfig) {
6256 onConfigurationChanged(newConfig);
6257 }
6258
6259 /**
6260 * Called when the current configuration of the resources being used
6261 * by the application have changed. You can use this to decide when
6262 * to reload resources that can changed based on orientation and other
6263 * configuration characterstics. You only need to use this if you are
6264 * not relying on the normal {@link android.app.Activity} mechanism of
6265 * recreating the activity instance upon a configuration change.
6266 *
6267 * @param newConfig The new resource configuration.
6268 */
6269 protected void onConfigurationChanged(Configuration newConfig) {
6270 }
6271
6272 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006273 * Private function to aggregate all per-view attributes in to the view
6274 * root.
6275 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006276 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
6277 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006278 }
6279
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006280 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
6281 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08006282 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006283 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08006284 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006285 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006286 ListenerInfo li = mListenerInfo;
6287 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07006288 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08006289 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006290 }
6291 }
6292
6293 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08006294 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006295 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006296 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
6297 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006298 ai.mRecomputeGlobalAttributes = true;
6299 }
6300 }
6301 }
6302
6303 /**
6304 * Returns whether the device is currently in touch mode. Touch mode is entered
6305 * once the user begins interacting with the device by touch, and affects various
6306 * things like whether focus is always visible to the user.
6307 *
6308 * @return Whether the device is in touch mode.
6309 */
6310 @ViewDebug.ExportedProperty
6311 public boolean isInTouchMode() {
6312 if (mAttachInfo != null) {
6313 return mAttachInfo.mInTouchMode;
6314 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006315 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006316 }
6317 }
6318
6319 /**
6320 * Returns the context the view is running in, through which it can
6321 * access the current theme, resources, etc.
6322 *
6323 * @return The view's Context.
6324 */
6325 @ViewDebug.CapturedViewProperty
6326 public final Context getContext() {
6327 return mContext;
6328 }
6329
6330 /**
6331 * Handle a key event before it is processed by any input method
6332 * associated with the view hierarchy. This can be used to intercept
6333 * key events in special situations before the IME consumes them; a
6334 * typical example would be handling the BACK key to update the application's
6335 * UI instead of allowing the IME to see it and close itself.
6336 *
6337 * @param keyCode The value in event.getKeyCode().
6338 * @param event Description of the key event.
6339 * @return If you handled the event, return true. If you want to allow the
6340 * event to be handled by the next receiver, return false.
6341 */
6342 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
6343 return false;
6344 }
6345
6346 /**
Jeff Brown995e7742010-12-22 16:59:36 -08006347 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
6348 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006349 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
6350 * is released, if the view is enabled and clickable.
6351 *
6352 * @param keyCode A key code that represents the button pressed, from
6353 * {@link android.view.KeyEvent}.
6354 * @param event The KeyEvent object that defines the button action.
6355 */
6356 public boolean onKeyDown(int keyCode, KeyEvent event) {
6357 boolean result = false;
6358
6359 switch (keyCode) {
6360 case KeyEvent.KEYCODE_DPAD_CENTER:
6361 case KeyEvent.KEYCODE_ENTER: {
6362 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
6363 return true;
6364 }
6365 // Long clickable items don't necessarily have to be clickable
6366 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
6367 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
6368 (event.getRepeatCount() == 0)) {
6369 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006370 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006371 return true;
6372 }
6373 break;
6374 }
6375 }
6376 return result;
6377 }
6378
6379 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006380 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
6381 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
6382 * the event).
6383 */
6384 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
6385 return false;
6386 }
6387
6388 /**
Jeff Brown995e7742010-12-22 16:59:36 -08006389 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
6390 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006391 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
6392 * {@link KeyEvent#KEYCODE_ENTER} is released.
6393 *
6394 * @param keyCode A key code that represents the button pressed, from
6395 * {@link android.view.KeyEvent}.
6396 * @param event The KeyEvent object that defines the button action.
6397 */
6398 public boolean onKeyUp(int keyCode, KeyEvent event) {
6399 boolean result = false;
6400
6401 switch (keyCode) {
6402 case KeyEvent.KEYCODE_DPAD_CENTER:
6403 case KeyEvent.KEYCODE_ENTER: {
6404 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
6405 return true;
6406 }
6407 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
6408 setPressed(false);
6409
6410 if (!mHasPerformedLongPress) {
6411 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006412 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006413
6414 result = performClick();
6415 }
6416 }
6417 break;
6418 }
6419 }
6420 return result;
6421 }
6422
6423 /**
6424 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
6425 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
6426 * the event).
6427 *
6428 * @param keyCode A key code that represents the button pressed, from
6429 * {@link android.view.KeyEvent}.
6430 * @param repeatCount The number of times the action was made.
6431 * @param event The KeyEvent object that defines the button action.
6432 */
6433 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
6434 return false;
6435 }
6436
6437 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08006438 * Called on the focused view when a key shortcut event is not handled.
6439 * Override this method to implement local key shortcuts for the View.
6440 * Key shortcuts can also be implemented by setting the
6441 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006442 *
6443 * @param keyCode The value in event.getKeyCode().
6444 * @param event Description of the key event.
6445 * @return If you handled the event, return true. If you want to allow the
6446 * event to be handled by the next receiver, return false.
6447 */
6448 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
6449 return false;
6450 }
6451
6452 /**
6453 * Check whether the called view is a text editor, in which case it
6454 * would make sense to automatically display a soft input window for
6455 * it. Subclasses should override this if they implement
6456 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006457 * a call on that method would return a non-null InputConnection, and
6458 * they are really a first-class editor that the user would normally
6459 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07006460 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006461 * <p>The default implementation always returns false. This does
6462 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
6463 * will not be called or the user can not otherwise perform edits on your
6464 * view; it is just a hint to the system that this is not the primary
6465 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07006466 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006467 * @return Returns true if this view is a text editor, else false.
6468 */
6469 public boolean onCheckIsTextEditor() {
6470 return false;
6471 }
Romain Guy8506ab42009-06-11 17:35:47 -07006472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006473 /**
6474 * Create a new InputConnection for an InputMethod to interact
6475 * with the view. The default implementation returns null, since it doesn't
6476 * support input methods. You can override this to implement such support.
6477 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07006478 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006479 * <p>When implementing this, you probably also want to implement
6480 * {@link #onCheckIsTextEditor()} to indicate you will return a
6481 * non-null InputConnection.
6482 *
6483 * @param outAttrs Fill in with attribute information about the connection.
6484 */
6485 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
6486 return null;
6487 }
6488
6489 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006490 * Called by the {@link android.view.inputmethod.InputMethodManager}
6491 * when a view who is not the current
6492 * input connection target is trying to make a call on the manager. The
6493 * default implementation returns false; you can override this to return
6494 * true for certain views if you are performing InputConnection proxying
6495 * to them.
6496 * @param view The View that is making the InputMethodManager call.
6497 * @return Return true to allow the call, false to reject.
6498 */
6499 public boolean checkInputConnectionProxy(View view) {
6500 return false;
6501 }
Romain Guy8506ab42009-06-11 17:35:47 -07006502
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006503 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006504 * Show the context menu for this view. It is not safe to hold on to the
6505 * menu after returning from this method.
6506 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07006507 * You should normally not overload this method. Overload
6508 * {@link #onCreateContextMenu(ContextMenu)} or define an
6509 * {@link OnCreateContextMenuListener} to add items to the context menu.
6510 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006511 * @param menu The context menu to populate
6512 */
6513 public void createContextMenu(ContextMenu menu) {
6514 ContextMenuInfo menuInfo = getContextMenuInfo();
6515
6516 // Sets the current menu info so all items added to menu will have
6517 // my extra info set.
6518 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
6519
6520 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006521 ListenerInfo li = mListenerInfo;
6522 if (li != null && li.mOnCreateContextMenuListener != null) {
6523 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006524 }
6525
6526 // Clear the extra information so subsequent items that aren't mine don't
6527 // have my extra info.
6528 ((MenuBuilder)menu).setCurrentMenuInfo(null);
6529
6530 if (mParent != null) {
6531 mParent.createContextMenu(menu);
6532 }
6533 }
6534
6535 /**
6536 * Views should implement this if they have extra information to associate
6537 * with the context menu. The return result is supplied as a parameter to
6538 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
6539 * callback.
6540 *
6541 * @return Extra information about the item for which the context menu
6542 * should be shown. This information will vary across different
6543 * subclasses of View.
6544 */
6545 protected ContextMenuInfo getContextMenuInfo() {
6546 return null;
6547 }
6548
6549 /**
6550 * Views should implement this if the view itself is going to add items to
6551 * the context menu.
6552 *
6553 * @param menu the context menu to populate
6554 */
6555 protected void onCreateContextMenu(ContextMenu menu) {
6556 }
6557
6558 /**
6559 * Implement this method to handle trackball motion events. The
6560 * <em>relative</em> movement of the trackball since the last event
6561 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
6562 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
6563 * that a movement of 1 corresponds to the user pressing one DPAD key (so
6564 * they will often be fractional values, representing the more fine-grained
6565 * movement information available from a trackball).
6566 *
6567 * @param event The motion event.
6568 * @return True if the event was handled, false otherwise.
6569 */
6570 public boolean onTrackballEvent(MotionEvent event) {
6571 return false;
6572 }
6573
6574 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08006575 * Implement this method to handle generic motion events.
6576 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08006577 * Generic motion events describe joystick movements, mouse hovers, track pad
6578 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08006579 * {@link MotionEvent#getSource() source} of the motion event specifies
6580 * the class of input that was received. Implementations of this method
6581 * must examine the bits in the source before processing the event.
6582 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006583 * </p><p>
6584 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6585 * are delivered to the view under the pointer. All other generic motion events are
6586 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08006587 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07006588 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006589 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006590 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
6591 * // process the joystick movement...
6592 * return true;
6593 * }
6594 * }
6595 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
6596 * switch (event.getAction()) {
6597 * case MotionEvent.ACTION_HOVER_MOVE:
6598 * // process the mouse hover movement...
6599 * return true;
6600 * case MotionEvent.ACTION_SCROLL:
6601 * // process the scroll wheel movement...
6602 * return true;
6603 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08006604 * }
6605 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07006606 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08006607 *
6608 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08006609 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08006610 */
6611 public boolean onGenericMotionEvent(MotionEvent event) {
6612 return false;
6613 }
6614
6615 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006616 * Implement this method to handle hover events.
6617 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07006618 * This method is called whenever a pointer is hovering into, over, or out of the
6619 * bounds of a view and the view is not currently being touched.
6620 * Hover events are represented as pointer events with action
6621 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
6622 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
6623 * </p>
6624 * <ul>
6625 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
6626 * when the pointer enters the bounds of the view.</li>
6627 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
6628 * when the pointer has already entered the bounds of the view and has moved.</li>
6629 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
6630 * when the pointer has exited the bounds of the view or when the pointer is
6631 * about to go down due to a button click, tap, or similar user action that
6632 * causes the view to be touched.</li>
6633 * </ul>
6634 * <p>
6635 * The view should implement this method to return true to indicate that it is
6636 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08006637 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07006638 * The default implementation calls {@link #setHovered} to update the hovered state
6639 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07006640 * is enabled and is clickable. The default implementation also sends hover
6641 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08006642 * </p>
6643 *
6644 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07006645 * @return True if the view handled the hover event.
6646 *
6647 * @see #isHovered
6648 * @see #setHovered
6649 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006650 */
6651 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006652 // The root view may receive hover (or touch) events that are outside the bounds of
6653 // the window. This code ensures that we only send accessibility events for
6654 // hovers that are actually within the bounds of the root view.
6655 final int action = event.getAction();
6656 if (!mSendingHoverAccessibilityEvents) {
6657 if ((action == MotionEvent.ACTION_HOVER_ENTER
6658 || action == MotionEvent.ACTION_HOVER_MOVE)
6659 && !hasHoveredChild()
6660 && pointInView(event.getX(), event.getY())) {
6661 mSendingHoverAccessibilityEvents = true;
6662 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
6663 }
6664 } else {
6665 if (action == MotionEvent.ACTION_HOVER_EXIT
6666 || (action == MotionEvent.ACTION_HOVER_MOVE
6667 && !pointInView(event.getX(), event.getY()))) {
6668 mSendingHoverAccessibilityEvents = false;
6669 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
6670 }
Jeff Browna1b24182011-07-28 13:38:24 -07006671 }
6672
Jeff Brown87b7f802011-06-21 18:35:45 -07006673 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006674 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07006675 case MotionEvent.ACTION_HOVER_ENTER:
6676 setHovered(true);
6677 break;
6678 case MotionEvent.ACTION_HOVER_EXIT:
6679 setHovered(false);
6680 break;
6681 }
Jeff Browna1b24182011-07-28 13:38:24 -07006682
6683 // Dispatch the event to onGenericMotionEvent before returning true.
6684 // This is to provide compatibility with existing applications that
6685 // handled HOVER_MOVE events in onGenericMotionEvent and that would
6686 // break because of the new default handling for hoverable views
6687 // in onHoverEvent.
6688 // Note that onGenericMotionEvent will be called by default when
6689 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
6690 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07006691 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08006692 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07006693 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08006694 }
6695
6696 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006697 * Returns true if the view should handle {@link #onHoverEvent}
6698 * by calling {@link #setHovered} to change its hovered state.
6699 *
6700 * @return True if the view is hoverable.
6701 */
6702 private boolean isHoverable() {
6703 final int viewFlags = mViewFlags;
Romain Guy02ccac62011-06-24 13:20:23 -07006704 //noinspection SimplifiableIfStatement
Jeff Brown87b7f802011-06-21 18:35:45 -07006705 if ((viewFlags & ENABLED_MASK) == DISABLED) {
6706 return false;
6707 }
6708
6709 return (viewFlags & CLICKABLE) == CLICKABLE
6710 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
6711 }
6712
6713 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006714 * Returns true if the view is currently hovered.
6715 *
6716 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006717 *
6718 * @see #setHovered
6719 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006720 */
Jeff Brown10b62902011-06-20 16:40:37 -07006721 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08006722 public boolean isHovered() {
6723 return (mPrivateFlags & HOVERED) != 0;
6724 }
6725
6726 /**
6727 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006728 * <p>
6729 * Calling this method also changes the drawable state of the view. This
6730 * enables the view to react to hover by using different drawable resources
6731 * to change its appearance.
6732 * </p><p>
6733 * The {@link #onHoverChanged} method is called when the hovered state changes.
6734 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08006735 *
6736 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006737 *
6738 * @see #isHovered
6739 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006740 */
6741 public void setHovered(boolean hovered) {
6742 if (hovered) {
6743 if ((mPrivateFlags & HOVERED) == 0) {
6744 mPrivateFlags |= HOVERED;
6745 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006746 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08006747 }
6748 } else {
6749 if ((mPrivateFlags & HOVERED) != 0) {
6750 mPrivateFlags &= ~HOVERED;
6751 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006752 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08006753 }
6754 }
6755 }
6756
6757 /**
Jeff Brown10b62902011-06-20 16:40:37 -07006758 * Implement this method to handle hover state changes.
6759 * <p>
6760 * This method is called whenever the hover state changes as a result of a
6761 * call to {@link #setHovered}.
6762 * </p>
6763 *
6764 * @param hovered The current hover state, as returned by {@link #isHovered}.
6765 *
6766 * @see #isHovered
6767 * @see #setHovered
6768 */
6769 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07006770 }
6771
6772 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006773 * Implement this method to handle touch screen motion events.
6774 *
6775 * @param event The motion event.
6776 * @return True if the event was handled, false otherwise.
6777 */
6778 public boolean onTouchEvent(MotionEvent event) {
6779 final int viewFlags = mViewFlags;
6780
6781 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006782 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08006783 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006784 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006785 // A disabled view that is clickable still consumes the touch
6786 // events, it just doesn't respond to them.
6787 return (((viewFlags & CLICKABLE) == CLICKABLE ||
6788 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
6789 }
6790
6791 if (mTouchDelegate != null) {
6792 if (mTouchDelegate.onTouchEvent(event)) {
6793 return true;
6794 }
6795 }
6796
6797 if (((viewFlags & CLICKABLE) == CLICKABLE ||
6798 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
6799 switch (event.getAction()) {
6800 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08006801 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
6802 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006803 // take focus if we don't have it already and we should in
6804 // touch mode.
6805 boolean focusTaken = false;
6806 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
6807 focusTaken = requestFocus();
6808 }
6809
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006810 if (prepressed) {
6811 // The button is being released before we actually
6812 // showed it as pressed. Make it show the pressed
6813 // state now (before scheduling the click) to ensure
6814 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08006815 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006816 }
Joe Malin32736f02011-01-19 16:14:20 -08006817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006818 if (!mHasPerformedLongPress) {
6819 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006820 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006821
6822 // Only perform take click actions if we were in the pressed state
6823 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08006824 // Use a Runnable and post this rather than calling
6825 // performClick directly. This lets other visual state
6826 // of the view update before click actions start.
6827 if (mPerformClick == null) {
6828 mPerformClick = new PerformClick();
6829 }
6830 if (!post(mPerformClick)) {
6831 performClick();
6832 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006833 }
6834 }
6835
6836 if (mUnsetPressedState == null) {
6837 mUnsetPressedState = new UnsetPressedState();
6838 }
6839
Adam Powelle14579b2009-12-16 18:39:52 -08006840 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08006841 postDelayed(mUnsetPressedState,
6842 ViewConfiguration.getPressedStateDuration());
6843 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006844 // If the post failed, unpress right now
6845 mUnsetPressedState.run();
6846 }
Adam Powelle14579b2009-12-16 18:39:52 -08006847 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006848 }
6849 break;
6850
6851 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08006852 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006853
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006854 if (performButtonActionOnTouchDown(event)) {
6855 break;
6856 }
6857
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006858 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07006859 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006860
6861 // For views inside a scrolling container, delay the pressed feedback for
6862 // a short period in case this is a scroll.
6863 if (isInScrollingContainer) {
6864 mPrivateFlags |= PREPRESSED;
6865 if (mPendingCheckForTap == null) {
6866 mPendingCheckForTap = new CheckForTap();
6867 }
6868 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
6869 } else {
6870 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08006871 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006872 checkForLongClick(0);
6873 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006874 break;
6875
6876 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08006877 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08006878 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006879 break;
6880
6881 case MotionEvent.ACTION_MOVE:
6882 final int x = (int) event.getX();
6883 final int y = (int) event.getY();
6884
6885 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07006886 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006887 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08006888 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006889 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08006890 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05006891 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006892
Adam Powell4d6f0662012-02-21 15:11:11 -08006893 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006894 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006895 }
6896 break;
6897 }
6898 return true;
6899 }
6900
6901 return false;
6902 }
6903
6904 /**
Adam Powell10298662011-08-14 18:26:30 -07006905 * @hide
6906 */
6907 public boolean isInScrollingContainer() {
6908 ViewParent p = getParent();
6909 while (p != null && p instanceof ViewGroup) {
6910 if (((ViewGroup) p).shouldDelayChildPressedState()) {
6911 return true;
6912 }
6913 p = p.getParent();
6914 }
6915 return false;
6916 }
6917
6918 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05006919 * Remove the longpress detection timer.
6920 */
6921 private void removeLongPressCallback() {
6922 if (mPendingCheckForLongPress != null) {
6923 removeCallbacks(mPendingCheckForLongPress);
6924 }
6925 }
Adam Powell3cb8b632011-01-21 15:34:14 -08006926
6927 /**
6928 * Remove the pending click action
6929 */
6930 private void removePerformClickCallback() {
6931 if (mPerformClick != null) {
6932 removeCallbacks(mPerformClick);
6933 }
6934 }
6935
Adam Powelle14579b2009-12-16 18:39:52 -08006936 /**
Romain Guya440b002010-02-24 15:57:54 -08006937 * Remove the prepress detection timer.
6938 */
6939 private void removeUnsetPressCallback() {
6940 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
6941 setPressed(false);
6942 removeCallbacks(mUnsetPressedState);
6943 }
6944 }
6945
6946 /**
Adam Powelle14579b2009-12-16 18:39:52 -08006947 * Remove the tap detection timer.
6948 */
6949 private void removeTapCallback() {
6950 if (mPendingCheckForTap != null) {
6951 mPrivateFlags &= ~PREPRESSED;
6952 removeCallbacks(mPendingCheckForTap);
6953 }
6954 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006955
6956 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006957 * Cancels a pending long press. Your subclass can use this if you
6958 * want the context menu to come up if the user presses and holds
6959 * at the same place, but you don't want it to come up if they press
6960 * and then move around enough to cause scrolling.
6961 */
6962 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05006963 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08006964
6965 /*
6966 * The prepressed state handled by the tap callback is a display
6967 * construct, but the tap callback will post a long press callback
6968 * less its own timeout. Remove it here.
6969 */
6970 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006971 }
6972
6973 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07006974 * Remove the pending callback for sending a
6975 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
6976 */
6977 private void removeSendViewScrolledAccessibilityEventCallback() {
6978 if (mSendViewScrolledAccessibilityEvent != null) {
6979 removeCallbacks(mSendViewScrolledAccessibilityEvent);
6980 }
6981 }
6982
6983 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006984 * Sets the TouchDelegate for this View.
6985 */
6986 public void setTouchDelegate(TouchDelegate delegate) {
6987 mTouchDelegate = delegate;
6988 }
6989
6990 /**
6991 * Gets the TouchDelegate for this View.
6992 */
6993 public TouchDelegate getTouchDelegate() {
6994 return mTouchDelegate;
6995 }
6996
6997 /**
6998 * Set flags controlling behavior of this view.
6999 *
7000 * @param flags Constant indicating the value which should be set
7001 * @param mask Constant indicating the bit range that should be changed
7002 */
7003 void setFlags(int flags, int mask) {
7004 int old = mViewFlags;
7005 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
7006
7007 int changed = mViewFlags ^ old;
7008 if (changed == 0) {
7009 return;
7010 }
7011 int privateFlags = mPrivateFlags;
7012
7013 /* Check if the FOCUSABLE bit has changed */
7014 if (((changed & FOCUSABLE_MASK) != 0) &&
7015 ((privateFlags & HAS_BOUNDS) !=0)) {
7016 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
7017 && ((privateFlags & FOCUSED) != 0)) {
7018 /* Give up focus if we are no longer focusable */
7019 clearFocus();
7020 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
7021 && ((privateFlags & FOCUSED) == 0)) {
7022 /*
7023 * Tell the view system that we are now available to take focus
7024 * if no one else already has it.
7025 */
7026 if (mParent != null) mParent.focusableViewAvailable(this);
7027 }
7028 }
7029
7030 if ((flags & VISIBILITY_MASK) == VISIBLE) {
7031 if ((changed & VISIBILITY_MASK) != 0) {
7032 /*
Chet Haase4324ead2011-08-24 21:31:03 -07007033 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07007034 * it was not visible. Marking it drawn ensures that the invalidation will
7035 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007036 */
Chet Haaseaceafe62011-08-26 15:44:33 -07007037 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07007038 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007039
7040 needGlobalAttributesUpdate(true);
7041
7042 // a view becoming visible is worth notifying the parent
7043 // about in case nothing has focus. even if this specific view
7044 // isn't focusable, it may contain something that is, so let
7045 // the root view try to give this focus if nothing else does.
7046 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
7047 mParent.focusableViewAvailable(this);
7048 }
7049 }
7050 }
7051
7052 /* Check if the GONE bit has changed */
7053 if ((changed & GONE) != 0) {
7054 needGlobalAttributesUpdate(false);
7055 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007056
Romain Guyecd80ee2009-12-03 17:13:02 -08007057 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
7058 if (hasFocus()) clearFocus();
7059 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07007060 if (mParent instanceof View) {
7061 // GONE views noop invalidation, so invalidate the parent
7062 ((View) mParent).invalidate(true);
7063 }
7064 // Mark the view drawn to ensure that it gets invalidated properly the next
7065 // time it is visible and gets invalidated
7066 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007067 }
7068 if (mAttachInfo != null) {
7069 mAttachInfo.mViewVisibilityChanged = true;
7070 }
7071 }
7072
7073 /* Check if the VISIBLE bit has changed */
7074 if ((changed & INVISIBLE) != 0) {
7075 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07007076 /*
7077 * If this view is becoming invisible, set the DRAWN flag so that
7078 * the next invalidate() will not be skipped.
7079 */
7080 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007081
7082 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
7083 // root view becoming invisible shouldn't clear focus
7084 if (getRootView() != this) {
7085 clearFocus();
7086 }
7087 }
7088 if (mAttachInfo != null) {
7089 mAttachInfo.mViewVisibilityChanged = true;
7090 }
7091 }
7092
Adam Powell326d8082009-12-09 15:10:07 -08007093 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07007094 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08007095 ((ViewGroup) mParent).onChildVisibilityChanged(this,
7096 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08007097 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07007098 } else if (mParent != null) {
7099 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07007100 }
Adam Powell326d8082009-12-09 15:10:07 -08007101 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
7102 }
7103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007104 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
7105 destroyDrawingCache();
7106 }
7107
7108 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
7109 destroyDrawingCache();
7110 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007111 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007112 }
7113
7114 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
7115 destroyDrawingCache();
7116 mPrivateFlags &= ~DRAWING_CACHE_VALID;
7117 }
7118
7119 if ((changed & DRAW_MASK) != 0) {
7120 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07007121 if (mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007122 mPrivateFlags &= ~SKIP_DRAW;
7123 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
7124 } else {
7125 mPrivateFlags |= SKIP_DRAW;
7126 }
7127 } else {
7128 mPrivateFlags &= ~SKIP_DRAW;
7129 }
7130 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08007131 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007132 }
7133
7134 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08007135 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007136 mParent.recomputeViewAttributes(this);
7137 }
7138 }
7139 }
7140
7141 /**
7142 * Change the view's z order in the tree, so it's on top of other sibling
7143 * views
7144 */
7145 public void bringToFront() {
7146 if (mParent != null) {
7147 mParent.bringChildToFront(this);
7148 }
7149 }
7150
7151 /**
7152 * This is called in response to an internal scroll in this view (i.e., the
7153 * view scrolled its own contents). This is typically as a result of
7154 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
7155 * called.
7156 *
7157 * @param l Current horizontal scroll origin.
7158 * @param t Current vertical scroll origin.
7159 * @param oldl Previous horizontal scroll origin.
7160 * @param oldt Previous vertical scroll origin.
7161 */
7162 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07007163 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
7164 postSendViewScrolledAccessibilityEventCallback();
7165 }
7166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007167 mBackgroundSizeChanged = true;
7168
7169 final AttachInfo ai = mAttachInfo;
7170 if (ai != null) {
7171 ai.mViewScrollChanged = true;
7172 }
7173 }
7174
7175 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007176 * Interface definition for a callback to be invoked when the layout bounds of a view
7177 * changes due to layout processing.
7178 */
7179 public interface OnLayoutChangeListener {
7180 /**
7181 * Called when the focus state of a view has changed.
7182 *
7183 * @param v The view whose state has changed.
7184 * @param left The new value of the view's left property.
7185 * @param top The new value of the view's top property.
7186 * @param right The new value of the view's right property.
7187 * @param bottom The new value of the view's bottom property.
7188 * @param oldLeft The previous value of the view's left property.
7189 * @param oldTop The previous value of the view's top property.
7190 * @param oldRight The previous value of the view's right property.
7191 * @param oldBottom The previous value of the view's bottom property.
7192 */
7193 void onLayoutChange(View v, int left, int top, int right, int bottom,
7194 int oldLeft, int oldTop, int oldRight, int oldBottom);
7195 }
7196
7197 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007198 * This is called during layout when the size of this view has changed. If
7199 * you were just added to the view hierarchy, you're called with the old
7200 * values of 0.
7201 *
7202 * @param w Current width of this view.
7203 * @param h Current height of this view.
7204 * @param oldw Old width of this view.
7205 * @param oldh Old height of this view.
7206 */
7207 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
7208 }
7209
7210 /**
7211 * Called by draw to draw the child views. This may be overridden
7212 * by derived classes to gain control just before its children are drawn
7213 * (but after its own view has been drawn).
7214 * @param canvas the canvas on which to draw the view
7215 */
7216 protected void dispatchDraw(Canvas canvas) {
7217 }
7218
7219 /**
7220 * Gets the parent of this view. Note that the parent is a
7221 * ViewParent and not necessarily a View.
7222 *
7223 * @return Parent of this view.
7224 */
7225 public final ViewParent getParent() {
7226 return mParent;
7227 }
7228
7229 /**
Chet Haasecca2c982011-05-20 14:34:18 -07007230 * Set the horizontal scrolled position of your view. This will cause a call to
7231 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7232 * invalidated.
7233 * @param value the x position to scroll to
7234 */
7235 public void setScrollX(int value) {
7236 scrollTo(value, mScrollY);
7237 }
7238
7239 /**
7240 * Set the vertical scrolled position of your view. This will cause a call to
7241 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7242 * invalidated.
7243 * @param value the y position to scroll to
7244 */
7245 public void setScrollY(int value) {
7246 scrollTo(mScrollX, value);
7247 }
7248
7249 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007250 * Return the scrolled left position of this view. This is the left edge of
7251 * the displayed part of your view. You do not need to draw any pixels
7252 * farther left, since those are outside of the frame of your view on
7253 * screen.
7254 *
7255 * @return The left edge of the displayed part of your view, in pixels.
7256 */
7257 public final int getScrollX() {
7258 return mScrollX;
7259 }
7260
7261 /**
7262 * Return the scrolled top position of this view. This is the top edge of
7263 * the displayed part of your view. You do not need to draw any pixels above
7264 * it, since those are outside of the frame of your view on screen.
7265 *
7266 * @return The top edge of the displayed part of your view, in pixels.
7267 */
7268 public final int getScrollY() {
7269 return mScrollY;
7270 }
7271
7272 /**
7273 * Return the width of the your view.
7274 *
7275 * @return The width of your view, in pixels.
7276 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007277 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007278 public final int getWidth() {
7279 return mRight - mLeft;
7280 }
7281
7282 /**
7283 * Return the height of your view.
7284 *
7285 * @return The height of your view, in pixels.
7286 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007287 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007288 public final int getHeight() {
7289 return mBottom - mTop;
7290 }
7291
7292 /**
7293 * Return the visible drawing bounds of your view. Fills in the output
7294 * rectangle with the values from getScrollX(), getScrollY(),
7295 * getWidth(), and getHeight().
7296 *
7297 * @param outRect The (scrolled) drawing bounds of the view.
7298 */
7299 public void getDrawingRect(Rect outRect) {
7300 outRect.left = mScrollX;
7301 outRect.top = mScrollY;
7302 outRect.right = mScrollX + (mRight - mLeft);
7303 outRect.bottom = mScrollY + (mBottom - mTop);
7304 }
7305
7306 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007307 * Like {@link #getMeasuredWidthAndState()}, but only returns the
7308 * raw width component (that is the result is masked by
7309 * {@link #MEASURED_SIZE_MASK}).
7310 *
7311 * @return The raw measured width of this view.
7312 */
7313 public final int getMeasuredWidth() {
7314 return mMeasuredWidth & MEASURED_SIZE_MASK;
7315 }
7316
7317 /**
7318 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07007319 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08007320 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007321 * This should be used during measurement and layout calculations only. Use
7322 * {@link #getWidth()} to see how wide a view is after layout.
7323 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08007324 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007325 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08007326 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007327 return mMeasuredWidth;
7328 }
7329
7330 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007331 * Like {@link #getMeasuredHeightAndState()}, but only returns the
7332 * raw width component (that is the result is masked by
7333 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007334 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08007335 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007336 */
7337 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08007338 return mMeasuredHeight & MEASURED_SIZE_MASK;
7339 }
7340
7341 /**
7342 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07007343 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08007344 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
7345 * This should be used during measurement and layout calculations only. Use
7346 * {@link #getHeight()} to see how wide a view is after layout.
7347 *
7348 * @return The measured width of this view as a bit mask.
7349 */
7350 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007351 return mMeasuredHeight;
7352 }
7353
7354 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007355 * Return only the state bits of {@link #getMeasuredWidthAndState()}
7356 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
7357 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
7358 * and the height component is at the shifted bits
7359 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
7360 */
7361 public final int getMeasuredState() {
7362 return (mMeasuredWidth&MEASURED_STATE_MASK)
7363 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
7364 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
7365 }
7366
7367 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007368 * The transform matrix of this view, which is calculated based on the current
7369 * roation, scale, and pivot properties.
7370 *
7371 * @see #getRotation()
7372 * @see #getScaleX()
7373 * @see #getScaleY()
7374 * @see #getPivotX()
7375 * @see #getPivotY()
7376 * @return The current transform matrix for the view
7377 */
7378 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007379 if (mTransformationInfo != null) {
7380 updateMatrix();
7381 return mTransformationInfo.mMatrix;
7382 }
7383 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07007384 }
7385
7386 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007387 * Utility function to determine if the value is far enough away from zero to be
7388 * considered non-zero.
7389 * @param value A floating point value to check for zero-ness
7390 * @return whether the passed-in value is far enough away from zero to be considered non-zero
7391 */
7392 private static boolean nonzero(float value) {
7393 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
7394 }
7395
7396 /**
Jeff Brown86671742010-09-30 20:00:15 -07007397 * Returns true if the transform matrix is the identity matrix.
7398 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08007399 *
Romain Guy33e72ae2010-07-17 12:40:29 -07007400 * @return True if the transform matrix is the identity matrix, false otherwise.
7401 */
Jeff Brown86671742010-09-30 20:00:15 -07007402 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007403 if (mTransformationInfo != null) {
7404 updateMatrix();
7405 return mTransformationInfo.mMatrixIsIdentity;
7406 }
7407 return true;
7408 }
7409
7410 void ensureTransformationInfo() {
7411 if (mTransformationInfo == null) {
7412 mTransformationInfo = new TransformationInfo();
7413 }
Jeff Brown86671742010-09-30 20:00:15 -07007414 }
7415
7416 /**
7417 * Recomputes the transform matrix if necessary.
7418 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007419 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007420 final TransformationInfo info = mTransformationInfo;
7421 if (info == null) {
7422 return;
7423 }
7424 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007425 // transform-related properties have changed since the last time someone
7426 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07007427
7428 // Figure out if we need to update the pivot point
7429 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007430 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
7431 info.mPrevWidth = mRight - mLeft;
7432 info.mPrevHeight = mBottom - mTop;
7433 info.mPivotX = info.mPrevWidth / 2f;
7434 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07007435 }
7436 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007437 info.mMatrix.reset();
7438 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
7439 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
7440 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
7441 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07007442 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007443 if (info.mCamera == null) {
7444 info.mCamera = new Camera();
7445 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07007446 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007447 info.mCamera.save();
7448 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
7449 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
7450 info.mCamera.getMatrix(info.matrix3D);
7451 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
7452 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
7453 info.mPivotY + info.mTranslationY);
7454 info.mMatrix.postConcat(info.matrix3D);
7455 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07007456 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007457 info.mMatrixDirty = false;
7458 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
7459 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007460 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007461 }
7462
7463 /**
7464 * Utility method to retrieve the inverse of the current mMatrix property.
7465 * We cache the matrix to avoid recalculating it when transform properties
7466 * have not changed.
7467 *
7468 * @return The inverse of the current matrix of this view.
7469 */
Jeff Brown86671742010-09-30 20:00:15 -07007470 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007471 final TransformationInfo info = mTransformationInfo;
7472 if (info != null) {
7473 updateMatrix();
7474 if (info.mInverseMatrixDirty) {
7475 if (info.mInverseMatrix == null) {
7476 info.mInverseMatrix = new Matrix();
7477 }
7478 info.mMatrix.invert(info.mInverseMatrix);
7479 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07007480 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007481 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07007482 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007483 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07007484 }
7485
7486 /**
Chet Haasea1cff502012-02-21 13:43:44 -08007487 * Gets the distance along the Z axis from the camera to this view.
7488 *
7489 * @see #setCameraDistance(float)
7490 *
7491 * @return The distance along the Z axis.
7492 */
7493 public float getCameraDistance() {
7494 ensureTransformationInfo();
7495 final float dpi = mResources.getDisplayMetrics().densityDpi;
7496 final TransformationInfo info = mTransformationInfo;
7497 if (info.mCamera == null) {
7498 info.mCamera = new Camera();
7499 info.matrix3D = new Matrix();
7500 }
7501 return -(info.mCamera.getLocationZ() * dpi);
7502 }
7503
7504 /**
Romain Guya5364ee2011-02-24 14:46:04 -08007505 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
7506 * views are drawn) from the camera to this view. The camera's distance
7507 * affects 3D transformations, for instance rotations around the X and Y
7508 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07007509 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08007510 * use a camera distance that's greater than the height (X axis rotation) or
7511 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007512 *
Romain Guya5364ee2011-02-24 14:46:04 -08007513 * <p>The distance of the camera from the view plane can have an affect on the
7514 * perspective distortion of the view when it is rotated around the x or y axis.
7515 * For example, a large distance will result in a large viewing angle, and there
7516 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07007517 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08007518 * also result in some drawing artifacts if the rotated view ends up partially
7519 * behind the camera (which is why the recommendation is to use a distance at
7520 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007521 *
Romain Guya5364ee2011-02-24 14:46:04 -08007522 * <p>The distance is expressed in "depth pixels." The default distance depends
7523 * on the screen density. For instance, on a medium density display, the
7524 * default distance is 1280. On a high density display, the default distance
7525 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007526 *
Romain Guya5364ee2011-02-24 14:46:04 -08007527 * <p>If you want to specify a distance that leads to visually consistent
7528 * results across various densities, use the following formula:</p>
7529 * <pre>
7530 * float scale = context.getResources().getDisplayMetrics().density;
7531 * view.setCameraDistance(distance * scale);
7532 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07007533 *
Romain Guya5364ee2011-02-24 14:46:04 -08007534 * <p>The density scale factor of a high density display is 1.5,
7535 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007536 *
Romain Guya5364ee2011-02-24 14:46:04 -08007537 * @param distance The distance in "depth pixels", if negative the opposite
7538 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07007539 *
7540 * @see #setRotationX(float)
7541 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08007542 */
7543 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007544 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08007545
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007546 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08007547 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007548 final TransformationInfo info = mTransformationInfo;
7549 if (info.mCamera == null) {
7550 info.mCamera = new Camera();
7551 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08007552 }
7553
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007554 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
7555 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08007556
Chet Haase9d1992d2012-03-13 11:03:25 -07007557 invalidateViewProperty(false, false);
Chet Haasea1cff502012-02-21 13:43:44 -08007558 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07007559 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08007560 }
Romain Guya5364ee2011-02-24 14:46:04 -08007561 }
7562
7563 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007564 * The degrees that the view is rotated around the pivot point.
7565 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007566 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07007567 * @see #getPivotX()
7568 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07007569 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007570 * @return The degrees of rotation.
7571 */
Chet Haasea5531132012-02-02 13:41:44 -08007572 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007573 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007574 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007575 }
7576
7577 /**
Chet Haase897247b2010-09-09 14:54:47 -07007578 * Sets the degrees that the view is rotated around the pivot point. Increasing values
7579 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07007580 *
7581 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07007582 *
7583 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07007584 * @see #getPivotX()
7585 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07007586 * @see #setRotationX(float)
7587 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08007588 *
7589 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07007590 */
7591 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007592 ensureTransformationInfo();
7593 final TransformationInfo info = mTransformationInfo;
7594 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007595 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07007596 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007597 info.mRotation = rotation;
7598 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007599 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007600 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7601 mDisplayList.setRotation(rotation);
7602 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007603 }
7604 }
7605
7606 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007607 * The degrees that the view is rotated around the vertical axis through the pivot point.
7608 *
7609 * @see #getPivotX()
7610 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07007611 * @see #setRotationY(float)
7612 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007613 * @return The degrees of Y rotation.
7614 */
Chet Haasea5531132012-02-02 13:41:44 -08007615 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07007616 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007617 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007618 }
7619
7620 /**
Chet Haase897247b2010-09-09 14:54:47 -07007621 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
7622 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
7623 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07007624 *
Romain Guya5364ee2011-02-24 14:46:04 -08007625 * When rotating large views, it is recommended to adjust the camera distance
7626 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007627 *
7628 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07007629 *
7630 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07007631 * @see #getPivotX()
7632 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007633 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07007634 * @see #setRotationX(float)
7635 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007636 *
7637 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07007638 */
7639 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007640 ensureTransformationInfo();
7641 final TransformationInfo info = mTransformationInfo;
7642 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007643 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007644 info.mRotationY = rotationY;
7645 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007646 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007647 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7648 mDisplayList.setRotationY(rotationY);
7649 }
Chet Haasefd2b0022010-08-06 13:08:56 -07007650 }
7651 }
7652
7653 /**
7654 * The degrees that the view is rotated around the horizontal axis through the pivot point.
7655 *
7656 * @see #getPivotX()
7657 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07007658 * @see #setRotationX(float)
7659 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007660 * @return The degrees of X rotation.
7661 */
Chet Haasea5531132012-02-02 13:41:44 -08007662 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07007663 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007664 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007665 }
7666
7667 /**
Chet Haase897247b2010-09-09 14:54:47 -07007668 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
7669 * Increasing values result in clockwise rotation from the viewpoint of looking down the
7670 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07007671 *
Romain Guya5364ee2011-02-24 14:46:04 -08007672 * When rotating large views, it is recommended to adjust the camera distance
7673 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007674 *
7675 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07007676 *
7677 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07007678 * @see #getPivotX()
7679 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007680 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07007681 * @see #setRotationY(float)
7682 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007683 *
7684 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07007685 */
7686 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007687 ensureTransformationInfo();
7688 final TransformationInfo info = mTransformationInfo;
7689 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007690 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007691 info.mRotationX = rotationX;
7692 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007693 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007694 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7695 mDisplayList.setRotationX(rotationX);
7696 }
Chet Haasefd2b0022010-08-06 13:08:56 -07007697 }
7698 }
7699
7700 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007701 * The amount that the view is scaled in x around the pivot point, as a proportion of
7702 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
7703 *
Joe Onorato93162322010-09-16 15:42:01 -04007704 * <p>By default, this is 1.0f.
7705 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007706 * @see #getPivotX()
7707 * @see #getPivotY()
7708 * @return The scaling factor.
7709 */
Chet Haasea5531132012-02-02 13:41:44 -08007710 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007711 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007712 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007713 }
7714
7715 /**
7716 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
7717 * the view's unscaled width. A value of 1 means that no scaling is applied.
7718 *
7719 * @param scaleX The scaling factor.
7720 * @see #getPivotX()
7721 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007722 *
7723 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07007724 */
7725 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007726 ensureTransformationInfo();
7727 final TransformationInfo info = mTransformationInfo;
7728 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007729 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007730 info.mScaleX = scaleX;
7731 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007732 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007733 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7734 mDisplayList.setScaleX(scaleX);
7735 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007736 }
7737 }
7738
7739 /**
7740 * The amount that the view is scaled in y around the pivot point, as a proportion of
7741 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
7742 *
Joe Onorato93162322010-09-16 15:42:01 -04007743 * <p>By default, this is 1.0f.
7744 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007745 * @see #getPivotX()
7746 * @see #getPivotY()
7747 * @return The scaling factor.
7748 */
Chet Haasea5531132012-02-02 13:41:44 -08007749 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007750 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007751 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007752 }
7753
7754 /**
7755 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
7756 * the view's unscaled width. A value of 1 means that no scaling is applied.
7757 *
7758 * @param scaleY The scaling factor.
7759 * @see #getPivotX()
7760 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007761 *
7762 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07007763 */
7764 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007765 ensureTransformationInfo();
7766 final TransformationInfo info = mTransformationInfo;
7767 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007768 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007769 info.mScaleY = scaleY;
7770 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007771 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007772 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7773 mDisplayList.setScaleY(scaleY);
7774 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007775 }
7776 }
7777
7778 /**
7779 * The x location of the point around which the view is {@link #setRotation(float) rotated}
7780 * and {@link #setScaleX(float) scaled}.
7781 *
7782 * @see #getRotation()
7783 * @see #getScaleX()
7784 * @see #getScaleY()
7785 * @see #getPivotY()
7786 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07007787 *
7788 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07007789 */
Chet Haasea5531132012-02-02 13:41:44 -08007790 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007791 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007792 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007793 }
7794
7795 /**
7796 * Sets the x location of the point around which the view is
7797 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07007798 * By default, the pivot point is centered on the object.
7799 * Setting this property disables this behavior and causes the view to use only the
7800 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007801 *
7802 * @param pivotX The x location of the pivot point.
7803 * @see #getRotation()
7804 * @see #getScaleX()
7805 * @see #getScaleY()
7806 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007807 *
7808 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07007809 */
7810 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007811 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007812 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007813 final TransformationInfo info = mTransformationInfo;
7814 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007815 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007816 info.mPivotX = pivotX;
7817 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007818 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007819 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7820 mDisplayList.setPivotX(pivotX);
7821 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007822 }
7823 }
7824
7825 /**
7826 * The y location of the point around which the view is {@link #setRotation(float) rotated}
7827 * and {@link #setScaleY(float) scaled}.
7828 *
7829 * @see #getRotation()
7830 * @see #getScaleX()
7831 * @see #getScaleY()
7832 * @see #getPivotY()
7833 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07007834 *
7835 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07007836 */
Chet Haasea5531132012-02-02 13:41:44 -08007837 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007838 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007839 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007840 }
7841
7842 /**
7843 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07007844 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
7845 * Setting this property disables this behavior and causes the view to use only the
7846 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007847 *
7848 * @param pivotY The y location of the pivot point.
7849 * @see #getRotation()
7850 * @see #getScaleX()
7851 * @see #getScaleY()
7852 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007853 *
7854 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07007855 */
7856 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007857 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007858 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007859 final TransformationInfo info = mTransformationInfo;
7860 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07007861 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007862 info.mPivotY = pivotY;
7863 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07007864 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08007865 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7866 mDisplayList.setPivotY(pivotY);
7867 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007868 }
7869 }
7870
7871 /**
7872 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
7873 * completely transparent and 1 means the view is completely opaque.
7874 *
Joe Onorato93162322010-09-16 15:42:01 -04007875 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07007876 * @return The opacity of the view.
7877 */
Chet Haasea5531132012-02-02 13:41:44 -08007878 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007879 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007880 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007881 }
7882
7883 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07007884 * Returns whether this View has content which overlaps. This function, intended to be
7885 * overridden by specific View types, is an optimization when alpha is set on a view. If
7886 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
7887 * and then composited it into place, which can be expensive. If the view has no overlapping
7888 * rendering, the view can draw each primitive with the appropriate alpha value directly.
7889 * An example of overlapping rendering is a TextView with a background image, such as a
7890 * Button. An example of non-overlapping rendering is a TextView with no background, or
7891 * an ImageView with only the foreground image. The default implementation returns true;
7892 * subclasses should override if they have cases which can be optimized.
7893 *
7894 * @return true if the content in this view might overlap, false otherwise.
7895 */
7896 public boolean hasOverlappingRendering() {
7897 return true;
7898 }
7899
7900 /**
Romain Guy171c5922011-01-06 10:04:23 -08007901 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
7902 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08007903 *
Romain Guy171c5922011-01-06 10:04:23 -08007904 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
7905 * responsible for applying the opacity itself. Otherwise, calling this method is
7906 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08007907 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07007908 *
Chet Haasea5531132012-02-02 13:41:44 -08007909 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
7910 * performance implications. It is generally best to use the alpha property sparingly and
7911 * transiently, as in the case of fading animations.</p>
7912 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007913 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08007914 *
Joe Malin32736f02011-01-19 16:14:20 -08007915 * @see #setLayerType(int, android.graphics.Paint)
7916 *
Chet Haase73066682010-11-29 15:55:32 -08007917 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07007918 */
7919 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007920 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007921 if (mTransformationInfo.mAlpha != alpha) {
7922 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007923 if (onSetAlpha((int) (alpha * 255))) {
7924 mPrivateFlags |= ALPHA_SET;
7925 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07007926 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007927 invalidate(true);
7928 } else {
7929 mPrivateFlags &= ~ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07007930 invalidateViewProperty(true, false);
Chet Haasea1cff502012-02-21 13:43:44 -08007931 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7932 mDisplayList.setAlpha(alpha);
7933 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007934 }
Chet Haaseed032702010-10-01 14:05:54 -07007935 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007936 }
7937
7938 /**
Chet Haasea00f3862011-02-22 06:34:40 -08007939 * Faster version of setAlpha() which performs the same steps except there are
7940 * no calls to invalidate(). The caller of this function should perform proper invalidation
7941 * on the parent and this object. The return value indicates whether the subclass handles
7942 * alpha (the return value for onSetAlpha()).
7943 *
7944 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007945 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
7946 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08007947 */
7948 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007949 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007950 if (mTransformationInfo.mAlpha != alpha) {
7951 mTransformationInfo.mAlpha = alpha;
7952 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
7953 if (subclassHandlesAlpha) {
7954 mPrivateFlags |= ALPHA_SET;
7955 return true;
7956 } else {
7957 mPrivateFlags &= ~ALPHA_SET;
Chet Haasea1cff502012-02-21 13:43:44 -08007958 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
7959 mDisplayList.setAlpha(alpha);
7960 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007961 }
Chet Haasea00f3862011-02-22 06:34:40 -08007962 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007963 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08007964 }
7965
7966 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007967 * Top position of this view relative to its parent.
7968 *
7969 * @return The top of this view, in pixels.
7970 */
7971 @ViewDebug.CapturedViewProperty
7972 public final int getTop() {
7973 return mTop;
7974 }
7975
7976 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007977 * Sets the top position of this view relative to its parent. This method is meant to be called
7978 * by the layout system and should not generally be called otherwise, because the property
7979 * may be changed at any time by the layout.
7980 *
7981 * @param top The top of this view, in pixels.
7982 */
7983 public final void setTop(int top) {
7984 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07007985 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007986 final boolean matrixIsIdentity = mTransformationInfo == null
7987 || mTransformationInfo.mMatrixIsIdentity;
7988 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007989 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007990 int minTop;
7991 int yLoc;
7992 if (top < mTop) {
7993 minTop = top;
7994 yLoc = top - mTop;
7995 } else {
7996 minTop = mTop;
7997 yLoc = 0;
7998 }
Chet Haasee9140a72011-02-16 16:23:29 -08007999 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008000 }
8001 } else {
8002 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008003 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008004 }
8005
Chet Haaseed032702010-10-01 14:05:54 -07008006 int width = mRight - mLeft;
8007 int oldHeight = mBottom - mTop;
8008
Chet Haase21cd1382010-09-01 17:42:29 -07008009 mTop = top;
Chet Haasea1cff502012-02-21 13:43:44 -08008010 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8011 mDisplayList.setTop(mTop);
8012 }
Chet Haase21cd1382010-09-01 17:42:29 -07008013
Chet Haaseed032702010-10-01 14:05:54 -07008014 onSizeChanged(width, mBottom - mTop, width, oldHeight);
8015
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008016 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008017 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8018 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008019 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008020 }
Chet Haase21cd1382010-09-01 17:42:29 -07008021 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008022 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008023 }
Chet Haase55dbb652010-12-21 20:15:08 -08008024 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008025 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008026 }
8027 }
8028
8029 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008030 * Bottom position of this view relative to its parent.
8031 *
8032 * @return The bottom of this view, in pixels.
8033 */
8034 @ViewDebug.CapturedViewProperty
8035 public final int getBottom() {
8036 return mBottom;
8037 }
8038
8039 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08008040 * True if this view has changed since the last time being drawn.
8041 *
8042 * @return The dirty state of this view.
8043 */
8044 public boolean isDirty() {
8045 return (mPrivateFlags & DIRTY_MASK) != 0;
8046 }
8047
8048 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008049 * Sets the bottom position of this view relative to its parent. This method is meant to be
8050 * called by the layout system and should not generally be called otherwise, because the
8051 * property may be changed at any time by the layout.
8052 *
8053 * @param bottom The bottom of this view, in pixels.
8054 */
8055 public final void setBottom(int bottom) {
8056 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07008057 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008058 final boolean matrixIsIdentity = mTransformationInfo == null
8059 || mTransformationInfo.mMatrixIsIdentity;
8060 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008061 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008062 int maxBottom;
8063 if (bottom < mBottom) {
8064 maxBottom = mBottom;
8065 } else {
8066 maxBottom = bottom;
8067 }
Chet Haasee9140a72011-02-16 16:23:29 -08008068 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008069 }
8070 } else {
8071 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008072 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008073 }
8074
Chet Haaseed032702010-10-01 14:05:54 -07008075 int width = mRight - mLeft;
8076 int oldHeight = mBottom - mTop;
8077
Chet Haase21cd1382010-09-01 17:42:29 -07008078 mBottom = bottom;
Chet Haasea1cff502012-02-21 13:43:44 -08008079 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8080 mDisplayList.setBottom(mBottom);
8081 }
Chet Haase21cd1382010-09-01 17:42:29 -07008082
Chet Haaseed032702010-10-01 14:05:54 -07008083 onSizeChanged(width, mBottom - mTop, width, oldHeight);
8084
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008085 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008086 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8087 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008088 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008089 }
Chet Haase21cd1382010-09-01 17:42:29 -07008090 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008091 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008092 }
Chet Haase55dbb652010-12-21 20:15:08 -08008093 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008094 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008095 }
8096 }
8097
8098 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008099 * Left position of this view relative to its parent.
8100 *
8101 * @return The left edge of this view, in pixels.
8102 */
8103 @ViewDebug.CapturedViewProperty
8104 public final int getLeft() {
8105 return mLeft;
8106 }
8107
8108 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008109 * Sets the left position of this view relative to its parent. This method is meant to be called
8110 * by the layout system and should not generally be called otherwise, because the property
8111 * may be changed at any time by the layout.
8112 *
8113 * @param left The bottom of this view, in pixels.
8114 */
8115 public final void setLeft(int left) {
8116 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07008117 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008118 final boolean matrixIsIdentity = mTransformationInfo == null
8119 || mTransformationInfo.mMatrixIsIdentity;
8120 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008121 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008122 int minLeft;
8123 int xLoc;
8124 if (left < mLeft) {
8125 minLeft = left;
8126 xLoc = left - mLeft;
8127 } else {
8128 minLeft = mLeft;
8129 xLoc = 0;
8130 }
Chet Haasee9140a72011-02-16 16:23:29 -08008131 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008132 }
8133 } else {
8134 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008135 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008136 }
8137
Chet Haaseed032702010-10-01 14:05:54 -07008138 int oldWidth = mRight - mLeft;
8139 int height = mBottom - mTop;
8140
Chet Haase21cd1382010-09-01 17:42:29 -07008141 mLeft = left;
Chet Haasea1cff502012-02-21 13:43:44 -08008142 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8143 mDisplayList.setLeft(left);
8144 }
Chet Haase21cd1382010-09-01 17:42:29 -07008145
Chet Haaseed032702010-10-01 14:05:54 -07008146 onSizeChanged(mRight - mLeft, height, oldWidth, height);
8147
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008148 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008149 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8150 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008151 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008152 }
Chet Haase21cd1382010-09-01 17:42:29 -07008153 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008154 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008155 }
Chet Haase55dbb652010-12-21 20:15:08 -08008156 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008157 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08008158 if (USE_DISPLAY_LIST_PROPERTIES) {
8159
8160 }
Chet Haase21cd1382010-09-01 17:42:29 -07008161 }
8162 }
8163
8164 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008165 * Right position of this view relative to its parent.
8166 *
8167 * @return The right edge of this view, in pixels.
8168 */
8169 @ViewDebug.CapturedViewProperty
8170 public final int getRight() {
8171 return mRight;
8172 }
8173
8174 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008175 * Sets the right position of this view relative to its parent. This method is meant to be called
8176 * by the layout system and should not generally be called otherwise, because the property
8177 * may be changed at any time by the layout.
8178 *
8179 * @param right The bottom of this view, in pixels.
8180 */
8181 public final void setRight(int right) {
8182 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07008183 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008184 final boolean matrixIsIdentity = mTransformationInfo == null
8185 || mTransformationInfo.mMatrixIsIdentity;
8186 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008187 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008188 int maxRight;
8189 if (right < mRight) {
8190 maxRight = mRight;
8191 } else {
8192 maxRight = right;
8193 }
Chet Haasee9140a72011-02-16 16:23:29 -08008194 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008195 }
8196 } else {
8197 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008198 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008199 }
8200
Chet Haaseed032702010-10-01 14:05:54 -07008201 int oldWidth = mRight - mLeft;
8202 int height = mBottom - mTop;
8203
Chet Haase21cd1382010-09-01 17:42:29 -07008204 mRight = right;
Chet Haasea1cff502012-02-21 13:43:44 -08008205 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8206 mDisplayList.setRight(mRight);
8207 }
Chet Haase21cd1382010-09-01 17:42:29 -07008208
Chet Haaseed032702010-10-01 14:05:54 -07008209 onSizeChanged(mRight - mLeft, height, oldWidth, height);
8210
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008211 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008212 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8213 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008214 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008215 }
Chet Haase21cd1382010-09-01 17:42:29 -07008216 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008217 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008218 }
Chet Haase55dbb652010-12-21 20:15:08 -08008219 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008220 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008221 }
8222 }
8223
8224 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008225 * The visual x position of this view, in pixels. This is equivalent to the
8226 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08008227 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07008228 *
Chet Haasedf030d22010-07-30 17:22:38 -07008229 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07008230 */
Chet Haasea5531132012-02-02 13:41:44 -08008231 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008232 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008233 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07008234 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008235
Chet Haasedf030d22010-07-30 17:22:38 -07008236 /**
8237 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
8238 * {@link #setTranslationX(float) translationX} property to be the difference between
8239 * the x value passed in and the current {@link #getLeft() left} property.
8240 *
8241 * @param x The visual x position of this view, in pixels.
8242 */
8243 public void setX(float x) {
8244 setTranslationX(x - mLeft);
8245 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008246
Chet Haasedf030d22010-07-30 17:22:38 -07008247 /**
8248 * The visual y position of this view, in pixels. This is equivalent to the
8249 * {@link #setTranslationY(float) translationY} property plus the current
8250 * {@link #getTop() top} property.
8251 *
8252 * @return The visual y position of this view, in pixels.
8253 */
Chet Haasea5531132012-02-02 13:41:44 -08008254 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008255 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008256 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07008257 }
8258
8259 /**
8260 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
8261 * {@link #setTranslationY(float) translationY} property to be the difference between
8262 * the y value passed in and the current {@link #getTop() top} property.
8263 *
8264 * @param y The visual y position of this view, in pixels.
8265 */
8266 public void setY(float y) {
8267 setTranslationY(y - mTop);
8268 }
8269
8270
8271 /**
8272 * The horizontal location of this view relative to its {@link #getLeft() left} position.
8273 * This position is post-layout, in addition to wherever the object's
8274 * layout placed it.
8275 *
8276 * @return The horizontal position of this view relative to its left position, in pixels.
8277 */
Chet Haasea5531132012-02-02 13:41:44 -08008278 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008279 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008280 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07008281 }
8282
8283 /**
8284 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
8285 * This effectively positions the object post-layout, in addition to wherever the object's
8286 * layout placed it.
8287 *
8288 * @param translationX The horizontal position of this view relative to its left position,
8289 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08008290 *
8291 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07008292 */
8293 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008294 ensureTransformationInfo();
8295 final TransformationInfo info = mTransformationInfo;
8296 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07008297 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008298 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008299 info.mTranslationX = translationX;
8300 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008301 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008302 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8303 mDisplayList.setTranslationX(translationX);
8304 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008305 }
8306 }
8307
8308 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008309 * The horizontal location of this view relative to its {@link #getTop() top} position.
8310 * This position is post-layout, in addition to wherever the object's
8311 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07008312 *
Chet Haasedf030d22010-07-30 17:22:38 -07008313 * @return The vertical position of this view relative to its top position,
8314 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07008315 */
Chet Haasea5531132012-02-02 13:41:44 -08008316 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008317 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008318 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008319 }
8320
8321 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008322 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
8323 * This effectively positions the object post-layout, in addition to wherever the object's
8324 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07008325 *
Chet Haasedf030d22010-07-30 17:22:38 -07008326 * @param translationY The vertical position of this view relative to its top position,
8327 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08008328 *
8329 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07008330 */
Chet Haasedf030d22010-07-30 17:22:38 -07008331 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008332 ensureTransformationInfo();
8333 final TransformationInfo info = mTransformationInfo;
8334 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008335 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008336 info.mTranslationY = translationY;
8337 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008338 invalidateViewProperty(false, true);
Chet Haasea1cff502012-02-21 13:43:44 -08008339 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8340 mDisplayList.setTranslationY(translationY);
8341 }
Chet Haasedf030d22010-07-30 17:22:38 -07008342 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008343 }
8344
8345 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008346 * Hit rectangle in parent's coordinates
8347 *
8348 * @param outRect The hit rectangle of the view.
8349 */
8350 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07008351 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008352 final TransformationInfo info = mTransformationInfo;
8353 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008354 outRect.set(mLeft, mTop, mRight, mBottom);
8355 } else {
8356 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008357 tmpRect.set(-info.mPivotX, -info.mPivotY,
8358 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
8359 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07008360 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
8361 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07008362 }
8363 }
8364
8365 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07008366 * Determines whether the given point, in local coordinates is inside the view.
8367 */
8368 /*package*/ final boolean pointInView(float localX, float localY) {
8369 return localX >= 0 && localX < (mRight - mLeft)
8370 && localY >= 0 && localY < (mBottom - mTop);
8371 }
8372
8373 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008374 * Utility method to determine whether the given point, in local coordinates,
8375 * is inside the view, where the area of the view is expanded by the slop factor.
8376 * This method is called while processing touch-move events to determine if the event
8377 * is still within the view.
8378 */
8379 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07008380 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07008381 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008382 }
8383
8384 /**
8385 * When a view has focus and the user navigates away from it, the next view is searched for
8386 * starting from the rectangle filled in by this method.
8387 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008388 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
8389 * of the view. However, if your view maintains some idea of internal selection,
8390 * such as a cursor, or a selected row or column, you should override this method and
8391 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008392 *
8393 * @param r The rectangle to fill in, in this view's coordinates.
8394 */
8395 public void getFocusedRect(Rect r) {
8396 getDrawingRect(r);
8397 }
8398
8399 /**
8400 * If some part of this view is not clipped by any of its parents, then
8401 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01008402 * coordinates (without taking possible View rotations into account), offset
8403 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
8404 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008405 *
8406 * @param r If true is returned, r holds the global coordinates of the
8407 * visible portion of this view.
8408 * @param globalOffset If true is returned, globalOffset holds the dx,dy
8409 * between this view and its root. globalOffet may be null.
8410 * @return true if r is non-empty (i.e. part of the view is visible at the
8411 * root level.
8412 */
8413 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
8414 int width = mRight - mLeft;
8415 int height = mBottom - mTop;
8416 if (width > 0 && height > 0) {
8417 r.set(0, 0, width, height);
8418 if (globalOffset != null) {
8419 globalOffset.set(-mScrollX, -mScrollY);
8420 }
8421 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
8422 }
8423 return false;
8424 }
8425
8426 public final boolean getGlobalVisibleRect(Rect r) {
8427 return getGlobalVisibleRect(r, null);
8428 }
8429
8430 public final boolean getLocalVisibleRect(Rect r) {
8431 Point offset = new Point();
8432 if (getGlobalVisibleRect(r, offset)) {
8433 r.offset(-offset.x, -offset.y); // make r local
8434 return true;
8435 }
8436 return false;
8437 }
8438
8439 /**
8440 * Offset this view's vertical location by the specified number of pixels.
8441 *
8442 * @param offset the number of pixels to offset the view by
8443 */
8444 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008445 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008446 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008447 final boolean matrixIsIdentity = mTransformationInfo == null
8448 || mTransformationInfo.mMatrixIsIdentity;
8449 if (matrixIsIdentity) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008450 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8451 invalidateViewProperty(false, false);
8452 } else {
8453 final ViewParent p = mParent;
8454 if (p != null && mAttachInfo != null) {
8455 final Rect r = mAttachInfo.mTmpInvalRect;
8456 int minTop;
8457 int maxBottom;
8458 int yLoc;
8459 if (offset < 0) {
8460 minTop = mTop + offset;
8461 maxBottom = mBottom;
8462 yLoc = offset;
8463 } else {
8464 minTop = mTop;
8465 maxBottom = mBottom + offset;
8466 yLoc = 0;
8467 }
8468 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
8469 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07008470 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008471 }
8472 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07008473 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008474 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008475
Chet Haasec3aa3612010-06-17 08:50:37 -07008476 mTop += offset;
8477 mBottom += offset;
Chet Haasea1cff502012-02-21 13:43:44 -08008478 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8479 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07008480 invalidateViewProperty(false, false);
8481 } else {
8482 if (!matrixIsIdentity) {
8483 invalidateViewProperty(false, true);
8484 }
8485 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08008486 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008487 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008488 }
8489
8490 /**
8491 * Offset this view's horizontal location by the specified amount of pixels.
8492 *
8493 * @param offset the numer of pixels to offset the view by
8494 */
8495 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008496 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008497 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008498 final boolean matrixIsIdentity = mTransformationInfo == null
8499 || mTransformationInfo.mMatrixIsIdentity;
8500 if (matrixIsIdentity) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008501 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8502 invalidateViewProperty(false, false);
8503 } else {
8504 final ViewParent p = mParent;
8505 if (p != null && mAttachInfo != null) {
8506 final Rect r = mAttachInfo.mTmpInvalRect;
8507 int minLeft;
8508 int maxRight;
8509 if (offset < 0) {
8510 minLeft = mLeft + offset;
8511 maxRight = mRight;
8512 } else {
8513 minLeft = mLeft;
8514 maxRight = mRight + offset;
8515 }
8516 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
8517 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07008518 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008519 }
8520 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07008521 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008522 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008523
Chet Haasec3aa3612010-06-17 08:50:37 -07008524 mLeft += offset;
8525 mRight += offset;
Chet Haasea1cff502012-02-21 13:43:44 -08008526 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
8527 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07008528 invalidateViewProperty(false, false);
8529 } else {
8530 if (!matrixIsIdentity) {
8531 invalidateViewProperty(false, true);
8532 }
8533 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08008534 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008535 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008536 }
8537
8538 /**
8539 * Get the LayoutParams associated with this view. All views should have
8540 * layout parameters. These supply parameters to the <i>parent</i> of this
8541 * view specifying how it should be arranged. There are many subclasses of
8542 * ViewGroup.LayoutParams, and these correspond to the different subclasses
8543 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08008544 *
8545 * This method may return null if this View is not attached to a parent
8546 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
8547 * was not invoked successfully. When a View is attached to a parent
8548 * ViewGroup, this method must not return null.
8549 *
8550 * @return The LayoutParams associated with this view, or null if no
8551 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008552 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07008553 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008554 public ViewGroup.LayoutParams getLayoutParams() {
8555 return mLayoutParams;
8556 }
8557
8558 /**
8559 * Set the layout parameters associated with this view. These supply
8560 * parameters to the <i>parent</i> of this view specifying how it should be
8561 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
8562 * correspond to the different subclasses of ViewGroup that are responsible
8563 * for arranging their children.
8564 *
Romain Guy01c174b2011-02-22 11:51:06 -08008565 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008566 */
8567 public void setLayoutParams(ViewGroup.LayoutParams params) {
8568 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08008569 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008570 }
8571 mLayoutParams = params;
Philip Milned7dd8902012-01-26 16:55:30 -08008572 if (mParent instanceof ViewGroup) {
8573 ((ViewGroup) mParent).onSetLayoutParams(this, params);
8574 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008575 requestLayout();
8576 }
8577
8578 /**
8579 * Set the scrolled position of your view. This will cause a call to
8580 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8581 * invalidated.
8582 * @param x the x position to scroll to
8583 * @param y the y position to scroll to
8584 */
8585 public void scrollTo(int x, int y) {
8586 if (mScrollX != x || mScrollY != y) {
8587 int oldX = mScrollX;
8588 int oldY = mScrollY;
8589 mScrollX = x;
8590 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008591 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008592 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07008593 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008594 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008595 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008596 }
8597 }
8598
8599 /**
8600 * Move the scrolled position of your view. This will cause a call to
8601 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8602 * invalidated.
8603 * @param x the amount of pixels to scroll by horizontally
8604 * @param y the amount of pixels to scroll by vertically
8605 */
8606 public void scrollBy(int x, int y) {
8607 scrollTo(mScrollX + x, mScrollY + y);
8608 }
8609
8610 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008611 * <p>Trigger the scrollbars to draw. When invoked this method starts an
8612 * animation to fade the scrollbars out after a default delay. If a subclass
8613 * provides animated scrolling, the start delay should equal the duration
8614 * of the scrolling animation.</p>
8615 *
8616 * <p>The animation starts only if at least one of the scrollbars is
8617 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
8618 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8619 * this method returns true, and false otherwise. If the animation is
8620 * started, this method calls {@link #invalidate()}; in that case the
8621 * caller should not call {@link #invalidate()}.</p>
8622 *
8623 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07008624 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07008625 *
8626 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
8627 * and {@link #scrollTo(int, int)}.</p>
8628 *
8629 * @return true if the animation is played, false otherwise
8630 *
8631 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07008632 * @see #scrollBy(int, int)
8633 * @see #scrollTo(int, int)
8634 * @see #isHorizontalScrollBarEnabled()
8635 * @see #isVerticalScrollBarEnabled()
8636 * @see #setHorizontalScrollBarEnabled(boolean)
8637 * @see #setVerticalScrollBarEnabled(boolean)
8638 */
8639 protected boolean awakenScrollBars() {
8640 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07008641 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008642 }
8643
8644 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07008645 * Trigger the scrollbars to draw.
8646 * This method differs from awakenScrollBars() only in its default duration.
8647 * initialAwakenScrollBars() will show the scroll bars for longer than
8648 * usual to give the user more of a chance to notice them.
8649 *
8650 * @return true if the animation is played, false otherwise.
8651 */
8652 private boolean initialAwakenScrollBars() {
8653 return mScrollCache != null &&
8654 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
8655 }
8656
8657 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008658 * <p>
8659 * Trigger the scrollbars to draw. When invoked this method starts an
8660 * animation to fade the scrollbars out after a fixed delay. If a subclass
8661 * provides animated scrolling, the start delay should equal the duration of
8662 * the scrolling animation.
8663 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008664 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008665 * <p>
8666 * The animation starts only if at least one of the scrollbars is enabled,
8667 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8668 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8669 * this method returns true, and false otherwise. If the animation is
8670 * started, this method calls {@link #invalidate()}; in that case the caller
8671 * should not call {@link #invalidate()}.
8672 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008673 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008674 * <p>
8675 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07008676 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07008677 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008678 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008679 * @param startDelay the delay, in milliseconds, after which the animation
8680 * should start; when the delay is 0, the animation starts
8681 * immediately
8682 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008683 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008684 * @see #scrollBy(int, int)
8685 * @see #scrollTo(int, int)
8686 * @see #isHorizontalScrollBarEnabled()
8687 * @see #isVerticalScrollBarEnabled()
8688 * @see #setHorizontalScrollBarEnabled(boolean)
8689 * @see #setVerticalScrollBarEnabled(boolean)
8690 */
8691 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07008692 return awakenScrollBars(startDelay, true);
8693 }
Joe Malin32736f02011-01-19 16:14:20 -08008694
Mike Cleron290947b2009-09-29 18:34:32 -07008695 /**
8696 * <p>
8697 * Trigger the scrollbars to draw. When invoked this method starts an
8698 * animation to fade the scrollbars out after a fixed delay. If a subclass
8699 * provides animated scrolling, the start delay should equal the duration of
8700 * the scrolling animation.
8701 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008702 *
Mike Cleron290947b2009-09-29 18:34:32 -07008703 * <p>
8704 * The animation starts only if at least one of the scrollbars is enabled,
8705 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8706 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8707 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08008708 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07008709 * is set to true; in that case the caller
8710 * should not call {@link #invalidate()}.
8711 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008712 *
Mike Cleron290947b2009-09-29 18:34:32 -07008713 * <p>
8714 * This method should be invoked everytime a subclass directly updates the
8715 * scroll parameters.
8716 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008717 *
Mike Cleron290947b2009-09-29 18:34:32 -07008718 * @param startDelay the delay, in milliseconds, after which the animation
8719 * should start; when the delay is 0, the animation starts
8720 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08008721 *
Mike Cleron290947b2009-09-29 18:34:32 -07008722 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08008723 *
Mike Cleron290947b2009-09-29 18:34:32 -07008724 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008725 *
Mike Cleron290947b2009-09-29 18:34:32 -07008726 * @see #scrollBy(int, int)
8727 * @see #scrollTo(int, int)
8728 * @see #isHorizontalScrollBarEnabled()
8729 * @see #isVerticalScrollBarEnabled()
8730 * @see #setHorizontalScrollBarEnabled(boolean)
8731 * @see #setVerticalScrollBarEnabled(boolean)
8732 */
8733 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07008734 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08008735
Mike Cleronf116bf82009-09-27 19:14:12 -07008736 if (scrollCache == null || !scrollCache.fadeScrollBars) {
8737 return false;
8738 }
8739
8740 if (scrollCache.scrollBar == null) {
8741 scrollCache.scrollBar = new ScrollBarDrawable();
8742 }
8743
8744 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
8745
Mike Cleron290947b2009-09-29 18:34:32 -07008746 if (invalidate) {
8747 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08008748 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07008749 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008750
8751 if (scrollCache.state == ScrollabilityCache.OFF) {
8752 // FIXME: this is copied from WindowManagerService.
8753 // We should get this value from the system when it
8754 // is possible to do so.
8755 final int KEY_REPEAT_FIRST_DELAY = 750;
8756 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
8757 }
8758
8759 // Tell mScrollCache when we should start fading. This may
8760 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07008761 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07008762 scrollCache.fadeStartTime = fadeStartTime;
8763 scrollCache.state = ScrollabilityCache.ON;
8764
8765 // Schedule our fader to run, unscheduling any old ones first
8766 if (mAttachInfo != null) {
8767 mAttachInfo.mHandler.removeCallbacks(scrollCache);
8768 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
8769 }
8770
8771 return true;
8772 }
8773
8774 return false;
8775 }
8776
8777 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07008778 * Do not invalidate views which are not visible and which are not running an animation. They
8779 * will not get drawn and they should not set dirty flags as if they will be drawn
8780 */
8781 private boolean skipInvalidate() {
8782 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
8783 (!(mParent instanceof ViewGroup) ||
8784 !((ViewGroup) mParent).isViewTransitioning(this));
8785 }
8786 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07008787 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07008788 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
8789 * in the future. This must be called from a UI thread. To call from a non-UI
8790 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008791 *
8792 * WARNING: This method is destructive to dirty.
8793 * @param dirty the rectangle representing the bounds of the dirty region
8794 */
8795 public void invalidate(Rect dirty) {
8796 if (ViewDebug.TRACE_HIERARCHY) {
8797 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8798 }
8799
Chet Haaseaceafe62011-08-26 15:44:33 -07008800 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008801 return;
8802 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008803 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008804 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8805 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008806 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008807 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008808 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008809 final ViewParent p = mParent;
8810 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008811 //noinspection PointlessBooleanExpression,ConstantConditions
8812 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8813 if (p != null && ai != null && ai.mHardwareAccelerated) {
8814 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008815 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008816 p.invalidateChild(this, null);
8817 return;
8818 }
Romain Guyaf636eb2010-12-09 17:47:21 -08008819 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008820 if (p != null && ai != null) {
8821 final int scrollX = mScrollX;
8822 final int scrollY = mScrollY;
8823 final Rect r = ai.mTmpInvalRect;
8824 r.set(dirty.left - scrollX, dirty.top - scrollY,
8825 dirty.right - scrollX, dirty.bottom - scrollY);
8826 mParent.invalidateChild(this, r);
8827 }
8828 }
8829 }
8830
8831 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07008832 * 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 -08008833 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07008834 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
8835 * will be called at some point in the future. This must be called from
8836 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008837 * @param l the left position of the dirty region
8838 * @param t the top position of the dirty region
8839 * @param r the right position of the dirty region
8840 * @param b the bottom position of the dirty region
8841 */
8842 public void invalidate(int l, int t, int r, int b) {
8843 if (ViewDebug.TRACE_HIERARCHY) {
8844 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8845 }
8846
Chet Haaseaceafe62011-08-26 15:44:33 -07008847 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008848 return;
8849 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008850 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008851 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8852 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008853 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008854 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008855 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008856 final ViewParent p = mParent;
8857 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008858 //noinspection PointlessBooleanExpression,ConstantConditions
8859 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8860 if (p != null && ai != null && ai.mHardwareAccelerated) {
8861 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008862 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008863 p.invalidateChild(this, null);
8864 return;
8865 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008866 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008867 if (p != null && ai != null && l < r && t < b) {
8868 final int scrollX = mScrollX;
8869 final int scrollY = mScrollY;
8870 final Rect tmpr = ai.mTmpInvalRect;
8871 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
8872 p.invalidateChild(this, tmpr);
8873 }
8874 }
8875 }
8876
8877 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008878 * Invalidate the whole view. If the view is visible,
8879 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
8880 * the future. This must be called from a UI thread. To call from a non-UI thread,
8881 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008882 */
8883 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07008884 invalidate(true);
8885 }
Joe Malin32736f02011-01-19 16:14:20 -08008886
Chet Haaseed032702010-10-01 14:05:54 -07008887 /**
8888 * This is where the invalidate() work actually happens. A full invalidate()
8889 * causes the drawing cache to be invalidated, but this function can be called with
8890 * invalidateCache set to false to skip that invalidation step for cases that do not
8891 * need it (for example, a component that remains at the same dimensions with the same
8892 * content).
8893 *
8894 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
8895 * well. This is usually true for a full invalidate, but may be set to false if the
8896 * View's contents or dimensions have not changed.
8897 */
Romain Guy849d0a32011-02-01 17:20:48 -08008898 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008899 if (ViewDebug.TRACE_HIERARCHY) {
8900 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8901 }
8902
Chet Haaseaceafe62011-08-26 15:44:33 -07008903 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008904 return;
8905 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008906 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08008907 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08008908 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
8909 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07008910 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07008911 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -07008912 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08008913 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07008914 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8915 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008916 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07008917 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08008918 //noinspection PointlessBooleanExpression,ConstantConditions
8919 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8920 if (p != null && ai != null && ai.mHardwareAccelerated) {
8921 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008922 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008923 p.invalidateChild(this, null);
8924 return;
8925 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008926 }
Michael Jurkaebefea42010-11-15 16:04:01 -08008927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008928 if (p != null && ai != null) {
8929 final Rect r = ai.mTmpInvalRect;
8930 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8931 // Don't call invalidate -- we don't want to internally scroll
8932 // our own bounds
8933 p.invalidateChild(this, r);
8934 }
8935 }
8936 }
8937
8938 /**
Chet Haase9d1992d2012-03-13 11:03:25 -07008939 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
8940 * set any flags or handle all of the cases handled by the default invalidation methods.
8941 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
8942 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
8943 * walk up the hierarchy, transforming the dirty rect as necessary.
8944 *
8945 * The method also handles normal invalidation logic if display list properties are not
8946 * being used in this view. The invalidateParent and forceRedraw flags are used by that
8947 * backup approach, to handle these cases used in the various property-setting methods.
8948 *
8949 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
8950 * are not being used in this view
8951 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
8952 * list properties are not being used in this view
8953 */
8954 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
8955 if (!USE_DISPLAY_LIST_PROPERTIES || mDisplayList == null ||
8956 (mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
8957 if (invalidateParent) {
8958 invalidateParentCaches();
8959 }
8960 if (forceRedraw) {
8961 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
8962 }
8963 invalidate(false);
8964 } else {
8965 final AttachInfo ai = mAttachInfo;
8966 final ViewParent p = mParent;
8967 if (p != null && ai != null) {
8968 final Rect r = ai.mTmpInvalRect;
8969 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8970 if (mParent instanceof ViewGroup) {
8971 ((ViewGroup) mParent).invalidateChildFast(this, r);
8972 } else {
8973 mParent.invalidateChild(this, r);
8974 }
8975 }
8976 }
8977 }
8978
8979 /**
8980 * Utility method to transform a given Rect by the current matrix of this view.
8981 */
8982 void transformRect(final Rect rect) {
8983 if (!getMatrix().isIdentity()) {
8984 RectF boundingRect = mAttachInfo.mTmpTransformRect;
8985 boundingRect.set(rect);
8986 getMatrix().mapRect(boundingRect);
8987 rect.set((int) (boundingRect.left - 0.5f),
8988 (int) (boundingRect.top - 0.5f),
8989 (int) (boundingRect.right + 0.5f),
8990 (int) (boundingRect.bottom + 0.5f));
8991 }
8992 }
8993
8994 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08008995 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08008996 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8997 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08008998 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
8999 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08009000 *
9001 * @hide
9002 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08009003 protected void invalidateParentCaches() {
9004 if (mParent instanceof View) {
9005 ((View) mParent).mPrivateFlags |= INVALIDATED;
9006 }
9007 }
Joe Malin32736f02011-01-19 16:14:20 -08009008
Romain Guy0fd89bf2011-01-26 15:41:30 -08009009 /**
9010 * Used to indicate that the parent of this view should be invalidated. This functionality
9011 * is used to force the parent to rebuild its display list (when hardware-accelerated),
9012 * which is necessary when various parent-managed properties of the view change, such as
9013 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
9014 * an invalidation event to the parent.
9015 *
9016 * @hide
9017 */
9018 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08009019 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08009020 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08009021 }
9022 }
9023
9024 /**
Romain Guy24443ea2009-05-11 11:56:30 -07009025 * Indicates whether this View is opaque. An opaque View guarantees that it will
9026 * draw all the pixels overlapping its bounds using a fully opaque color.
9027 *
9028 * Subclasses of View should override this method whenever possible to indicate
9029 * whether an instance is opaque. Opaque Views are treated in a special way by
9030 * the View hierarchy, possibly allowing it to perform optimizations during
9031 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07009032 *
Romain Guy24443ea2009-05-11 11:56:30 -07009033 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07009034 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07009035 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07009036 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07009037 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009038 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
9039 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07009040 }
9041
Adam Powell20232d02010-12-08 21:08:53 -08009042 /**
9043 * @hide
9044 */
9045 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07009046 // Opaque if:
9047 // - Has a background
9048 // - Background is opaque
9049 // - Doesn't have scrollbars or scrollbars are inside overlay
9050
Philip Milne6c8ea062012-04-03 17:38:43 -07009051 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Romain Guy8f1344f52009-05-15 16:03:59 -07009052 mPrivateFlags |= OPAQUE_BACKGROUND;
9053 } else {
9054 mPrivateFlags &= ~OPAQUE_BACKGROUND;
9055 }
9056
9057 final int flags = mViewFlags;
9058 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
9059 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
9060 mPrivateFlags |= OPAQUE_SCROLLBARS;
9061 } else {
9062 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
9063 }
9064 }
9065
9066 /**
9067 * @hide
9068 */
9069 protected boolean hasOpaqueScrollbars() {
9070 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07009071 }
9072
9073 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009074 * @return A handler associated with the thread running the View. This
9075 * handler can be used to pump events in the UI events queue.
9076 */
9077 public Handler getHandler() {
9078 if (mAttachInfo != null) {
9079 return mAttachInfo.mHandler;
9080 }
9081 return null;
9082 }
9083
9084 /**
Jeff Browna175a5b2012-02-15 19:18:31 -08009085 * Gets the view root associated with the View.
9086 * @return The view root, or null if none.
9087 * @hide
9088 */
9089 public ViewRootImpl getViewRootImpl() {
9090 if (mAttachInfo != null) {
9091 return mAttachInfo.mViewRootImpl;
9092 }
9093 return null;
9094 }
9095
9096 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009097 * <p>Causes the Runnable to be added to the message queue.
9098 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009099 *
Romain Guye63a4f32011-08-11 11:33:31 -07009100 * <p>This method can be invoked from outside of the UI thread
9101 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009102 *
9103 * @param action The Runnable that will be executed.
9104 *
9105 * @return Returns true if the Runnable was successfully placed in to the
9106 * message queue. Returns false on failure, usually because the
9107 * looper processing the message queue is exiting.
9108 */
9109 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009110 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009111 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009112 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009113 }
Jeff Browna175a5b2012-02-15 19:18:31 -08009114 // Assume that post will succeed later
9115 ViewRootImpl.getRunQueue().post(action);
9116 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009117 }
9118
9119 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009120 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009121 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -07009122 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009123 *
Romain Guye63a4f32011-08-11 11:33:31 -07009124 * <p>This method can be invoked from outside of the UI thread
9125 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009126 *
9127 * @param action The Runnable that will be executed.
9128 * @param delayMillis The delay (in milliseconds) until the Runnable
9129 * will be executed.
9130 *
9131 * @return true if the Runnable was successfully placed in to the
9132 * message queue. Returns false on failure, usually because the
9133 * looper processing the message queue is exiting. Note that a
9134 * result of true does not mean the Runnable will be processed --
9135 * if the looper is quit before the delivery time of the message
9136 * occurs then the message will be dropped.
9137 */
9138 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009139 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009140 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009141 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009142 }
Jeff Browna175a5b2012-02-15 19:18:31 -08009143 // Assume that post will succeed later
9144 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
9145 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009146 }
9147
9148 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009149 * <p>Causes the Runnable to execute on the next animation time step.
9150 * The runnable will be run on the user interface thread.</p>
9151 *
9152 * <p>This method can be invoked from outside of the UI thread
9153 * only when this View is attached to a window.</p>
9154 *
9155 * @param action The Runnable that will be executed.
9156 *
9157 * @hide
9158 */
9159 public void postOnAnimation(Runnable action) {
9160 final AttachInfo attachInfo = mAttachInfo;
9161 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009162 attachInfo.mViewRootImpl.mChoreographer.postCallback(
9163 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009164 } else {
9165 // Assume that post will succeed later
9166 ViewRootImpl.getRunQueue().post(action);
9167 }
9168 }
9169
9170 /**
9171 * <p>Causes the Runnable to execute on the next animation time step,
9172 * after the specified amount of time elapses.
9173 * The runnable will be run on the user interface thread.</p>
9174 *
9175 * <p>This method can be invoked from outside of the UI thread
9176 * only when this View is attached to a window.</p>
9177 *
9178 * @param action The Runnable that will be executed.
9179 * @param delayMillis The delay (in milliseconds) until the Runnable
9180 * will be executed.
9181 *
9182 * @hide
9183 */
9184 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
9185 final AttachInfo attachInfo = mAttachInfo;
9186 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009187 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
9188 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009189 } else {
9190 // Assume that post will succeed later
9191 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
9192 }
9193 }
9194
9195 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009196 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009197 *
Romain Guye63a4f32011-08-11 11:33:31 -07009198 * <p>This method can be invoked from outside of the UI thread
9199 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009200 *
9201 * @param action The Runnable to remove from the message handling queue
9202 *
9203 * @return true if this view could ask the Handler to remove the Runnable,
9204 * false otherwise. When the returned value is true, the Runnable
9205 * may or may not have been actually removed from the message queue
9206 * (for instance, if the Runnable was not in the queue already.)
9207 */
9208 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -08009209 if (action != null) {
9210 final AttachInfo attachInfo = mAttachInfo;
9211 if (attachInfo != null) {
9212 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009213 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
9214 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -08009215 } else {
9216 // Assume that post will succeed later
9217 ViewRootImpl.getRunQueue().removeCallbacks(action);
9218 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009219 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009220 return true;
9221 }
9222
9223 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009224 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
9225 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009226 *
Romain Guye63a4f32011-08-11 11:33:31 -07009227 * <p>This method can be invoked from outside of the UI thread
9228 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009229 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009230 * @see #invalidate()
9231 */
9232 public void postInvalidate() {
9233 postInvalidateDelayed(0);
9234 }
9235
9236 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009237 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
9238 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009239 *
Romain Guye63a4f32011-08-11 11:33:31 -07009240 * <p>This method can be invoked from outside of the UI thread
9241 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009242 *
9243 * @param left The left coordinate of the rectangle to invalidate.
9244 * @param top The top coordinate of the rectangle to invalidate.
9245 * @param right The right coordinate of the rectangle to invalidate.
9246 * @param bottom The bottom coordinate of the rectangle to invalidate.
9247 *
9248 * @see #invalidate(int, int, int, int)
9249 * @see #invalidate(Rect)
9250 */
9251 public void postInvalidate(int left, int top, int right, int bottom) {
9252 postInvalidateDelayed(0, left, top, right, bottom);
9253 }
9254
9255 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009256 * <p>Cause an invalidate to happen on a subsequent cycle through the event
9257 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009258 *
Romain Guye63a4f32011-08-11 11:33:31 -07009259 * <p>This method can be invoked from outside of the UI thread
9260 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009261 *
9262 * @param delayMilliseconds the duration in milliseconds to delay the
9263 * invalidation by
9264 */
9265 public void postInvalidateDelayed(long delayMilliseconds) {
9266 // We try only with the AttachInfo because there's no point in invalidating
9267 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -08009268 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009269 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009270 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009271 }
9272 }
9273
9274 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009275 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
9276 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009277 *
Romain Guye63a4f32011-08-11 11:33:31 -07009278 * <p>This method can be invoked from outside of the UI thread
9279 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009280 *
9281 * @param delayMilliseconds the duration in milliseconds to delay the
9282 * invalidation by
9283 * @param left The left coordinate of the rectangle to invalidate.
9284 * @param top The top coordinate of the rectangle to invalidate.
9285 * @param right The right coordinate of the rectangle to invalidate.
9286 * @param bottom The bottom coordinate of the rectangle to invalidate.
9287 */
9288 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
9289 int right, int bottom) {
9290
9291 // We try only with the AttachInfo because there's no point in invalidating
9292 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -08009293 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009294 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009295 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
9296 info.target = this;
9297 info.left = left;
9298 info.top = top;
9299 info.right = right;
9300 info.bottom = bottom;
9301
Jeff Browna175a5b2012-02-15 19:18:31 -08009302 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009303 }
9304 }
9305
9306 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -08009307 * <p>Cause an invalidate to happen on the next animation time step, typically the
9308 * next display frame.</p>
9309 *
9310 * <p>This method can be invoked from outside of the UI thread
9311 * only when this View is attached to a window.</p>
9312 *
9313 * @hide
9314 */
9315 public void postInvalidateOnAnimation() {
9316 // We try only with the AttachInfo because there's no point in invalidating
9317 // if we are not attached to our window
9318 final AttachInfo attachInfo = mAttachInfo;
9319 if (attachInfo != null) {
9320 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
9321 }
9322 }
9323
9324 /**
9325 * <p>Cause an invalidate of the specified area to happen on the next animation
9326 * time step, typically the next display frame.</p>
9327 *
9328 * <p>This method can be invoked from outside of the UI thread
9329 * only when this View is attached to a window.</p>
9330 *
9331 * @param left The left coordinate of the rectangle to invalidate.
9332 * @param top The top coordinate of the rectangle to invalidate.
9333 * @param right The right coordinate of the rectangle to invalidate.
9334 * @param bottom The bottom coordinate of the rectangle to invalidate.
9335 *
9336 * @hide
9337 */
9338 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
9339 // We try only with the AttachInfo because there's no point in invalidating
9340 // if we are not attached to our window
9341 final AttachInfo attachInfo = mAttachInfo;
9342 if (attachInfo != null) {
9343 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
9344 info.target = this;
9345 info.left = left;
9346 info.top = top;
9347 info.right = right;
9348 info.bottom = bottom;
9349
9350 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
9351 }
9352 }
9353
9354 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07009355 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
9356 * This event is sent at most once every
9357 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
9358 */
9359 private void postSendViewScrolledAccessibilityEventCallback() {
9360 if (mSendViewScrolledAccessibilityEvent == null) {
9361 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
9362 }
9363 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
9364 mSendViewScrolledAccessibilityEvent.mIsPending = true;
9365 postDelayed(mSendViewScrolledAccessibilityEvent,
9366 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
9367 }
9368 }
9369
9370 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009371 * Called by a parent to request that a child update its values for mScrollX
9372 * and mScrollY if necessary. This will typically be done if the child is
9373 * animating a scroll using a {@link android.widget.Scroller Scroller}
9374 * object.
9375 */
9376 public void computeScroll() {
9377 }
9378
9379 /**
9380 * <p>Indicate whether the horizontal edges are faded when the view is
9381 * scrolled horizontally.</p>
9382 *
9383 * @return true if the horizontal edges should are faded on scroll, false
9384 * otherwise
9385 *
9386 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -07009387 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009388 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009389 */
9390 public boolean isHorizontalFadingEdgeEnabled() {
9391 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
9392 }
9393
9394 /**
9395 * <p>Define whether the horizontal edges should be faded when this view
9396 * is scrolled horizontally.</p>
9397 *
9398 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
9399 * be faded when the view is scrolled
9400 * horizontally
9401 *
9402 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -07009403 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009404 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009405 */
9406 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
9407 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
9408 if (horizontalFadingEdgeEnabled) {
9409 initScrollCache();
9410 }
9411
9412 mViewFlags ^= FADING_EDGE_HORIZONTAL;
9413 }
9414 }
9415
9416 /**
9417 * <p>Indicate whether the vertical edges are faded when the view is
9418 * scrolled horizontally.</p>
9419 *
9420 * @return true if the vertical edges should are faded on scroll, false
9421 * otherwise
9422 *
9423 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -07009424 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009425 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009426 */
9427 public boolean isVerticalFadingEdgeEnabled() {
9428 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
9429 }
9430
9431 /**
9432 * <p>Define whether the vertical edges should be faded when this view
9433 * is scrolled vertically.</p>
9434 *
9435 * @param verticalFadingEdgeEnabled true if the vertical edges should
9436 * be faded when the view is scrolled
9437 * vertically
9438 *
9439 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -07009440 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -07009441 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009442 */
9443 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
9444 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
9445 if (verticalFadingEdgeEnabled) {
9446 initScrollCache();
9447 }
9448
9449 mViewFlags ^= FADING_EDGE_VERTICAL;
9450 }
9451 }
9452
9453 /**
9454 * Returns the strength, or intensity, of the top faded edge. The strength is
9455 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9456 * returns 0.0 or 1.0 but no value in between.
9457 *
9458 * Subclasses should override this method to provide a smoother fade transition
9459 * when scrolling occurs.
9460 *
9461 * @return the intensity of the top fade as a float between 0.0f and 1.0f
9462 */
9463 protected float getTopFadingEdgeStrength() {
9464 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
9465 }
9466
9467 /**
9468 * Returns the strength, or intensity, of the bottom faded edge. The strength is
9469 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9470 * returns 0.0 or 1.0 but no value in between.
9471 *
9472 * Subclasses should override this method to provide a smoother fade transition
9473 * when scrolling occurs.
9474 *
9475 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
9476 */
9477 protected float getBottomFadingEdgeStrength() {
9478 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
9479 computeVerticalScrollRange() ? 1.0f : 0.0f;
9480 }
9481
9482 /**
9483 * Returns the strength, or intensity, of the left faded edge. The strength is
9484 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9485 * returns 0.0 or 1.0 but no value in between.
9486 *
9487 * Subclasses should override this method to provide a smoother fade transition
9488 * when scrolling occurs.
9489 *
9490 * @return the intensity of the left fade as a float between 0.0f and 1.0f
9491 */
9492 protected float getLeftFadingEdgeStrength() {
9493 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
9494 }
9495
9496 /**
9497 * Returns the strength, or intensity, of the right faded edge. The strength is
9498 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9499 * returns 0.0 or 1.0 but no value in between.
9500 *
9501 * Subclasses should override this method to provide a smoother fade transition
9502 * when scrolling occurs.
9503 *
9504 * @return the intensity of the right fade as a float between 0.0f and 1.0f
9505 */
9506 protected float getRightFadingEdgeStrength() {
9507 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
9508 computeHorizontalScrollRange() ? 1.0f : 0.0f;
9509 }
9510
9511 /**
9512 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
9513 * scrollbar is not drawn by default.</p>
9514 *
9515 * @return true if the horizontal scrollbar should be painted, false
9516 * otherwise
9517 *
9518 * @see #setHorizontalScrollBarEnabled(boolean)
9519 */
9520 public boolean isHorizontalScrollBarEnabled() {
9521 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9522 }
9523
9524 /**
9525 * <p>Define whether the horizontal scrollbar should be drawn or not. The
9526 * scrollbar is not drawn by default.</p>
9527 *
9528 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
9529 * be painted
9530 *
9531 * @see #isHorizontalScrollBarEnabled()
9532 */
9533 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
9534 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
9535 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07009536 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009537 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009538 }
9539 }
9540
9541 /**
9542 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
9543 * scrollbar is not drawn by default.</p>
9544 *
9545 * @return true if the vertical scrollbar should be painted, false
9546 * otherwise
9547 *
9548 * @see #setVerticalScrollBarEnabled(boolean)
9549 */
9550 public boolean isVerticalScrollBarEnabled() {
9551 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
9552 }
9553
9554 /**
9555 * <p>Define whether the vertical scrollbar should be drawn or not. The
9556 * scrollbar is not drawn by default.</p>
9557 *
9558 * @param verticalScrollBarEnabled true if the vertical scrollbar should
9559 * be painted
9560 *
9561 * @see #isVerticalScrollBarEnabled()
9562 */
9563 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
9564 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
9565 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07009566 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009567 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009568 }
9569 }
9570
Adam Powell20232d02010-12-08 21:08:53 -08009571 /**
9572 * @hide
9573 */
9574 protected void recomputePadding() {
9575 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009576 }
Joe Malin32736f02011-01-19 16:14:20 -08009577
Mike Cleronfe81d382009-09-28 14:22:16 -07009578 /**
9579 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08009580 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009581 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08009582 *
Philip Milne6c8ea062012-04-03 17:38:43 -07009583 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -07009584 */
9585 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
9586 initScrollCache();
9587 final ScrollabilityCache scrollabilityCache = mScrollCache;
9588 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009589 if (fadeScrollbars) {
9590 scrollabilityCache.state = ScrollabilityCache.OFF;
9591 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07009592 scrollabilityCache.state = ScrollabilityCache.ON;
9593 }
9594 }
Joe Malin32736f02011-01-19 16:14:20 -08009595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009596 /**
Joe Malin32736f02011-01-19 16:14:20 -08009597 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009598 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08009599 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009600 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -07009601 *
9602 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -07009603 */
9604 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08009605 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009606 }
Joe Malin32736f02011-01-19 16:14:20 -08009607
Mike Cleron52f0a642009-09-28 18:21:37 -07009608 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07009609 *
9610 * Returns the delay before scrollbars fade.
9611 *
9612 * @return the delay before scrollbars fade
9613 *
9614 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
9615 */
9616 public int getScrollBarDefaultDelayBeforeFade() {
9617 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
9618 mScrollCache.scrollBarDefaultDelayBeforeFade;
9619 }
9620
9621 /**
9622 * Define the delay before scrollbars fade.
9623 *
9624 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
9625 *
9626 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
9627 */
9628 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
9629 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
9630 }
9631
9632 /**
9633 *
9634 * Returns the scrollbar fade duration.
9635 *
9636 * @return the scrollbar fade duration
9637 *
9638 * @attr ref android.R.styleable#View_scrollbarFadeDuration
9639 */
9640 public int getScrollBarFadeDuration() {
9641 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
9642 mScrollCache.scrollBarFadeDuration;
9643 }
9644
9645 /**
9646 * Define the scrollbar fade duration.
9647 *
9648 * @param scrollBarFadeDuration - the scrollbar fade duration
9649 *
9650 * @attr ref android.R.styleable#View_scrollbarFadeDuration
9651 */
9652 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
9653 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
9654 }
9655
9656 /**
9657 *
9658 * Returns the scrollbar size.
9659 *
9660 * @return the scrollbar size
9661 *
9662 * @attr ref android.R.styleable#View_scrollbarSize
9663 */
9664 public int getScrollBarSize() {
9665 return mScrollCache == null ? ViewConfiguration.getScrollBarSize() :
9666 mScrollCache.scrollBarSize;
9667 }
9668
9669 /**
9670 * Define the scrollbar size.
9671 *
9672 * @param scrollBarSize - the scrollbar size
9673 *
9674 * @attr ref android.R.styleable#View_scrollbarSize
9675 */
9676 public void setScrollBarSize(int scrollBarSize) {
9677 getScrollCache().scrollBarSize = scrollBarSize;
9678 }
9679
9680 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009681 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
9682 * inset. When inset, they add to the padding of the view. And the scrollbars
9683 * can be drawn inside the padding area or on the edge of the view. For example,
9684 * if a view has a background drawable and you want to draw the scrollbars
9685 * inside the padding specified by the drawable, you can use
9686 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
9687 * appear at the edge of the view, ignoring the padding, then you can use
9688 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
9689 * @param style the style of the scrollbars. Should be one of
9690 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
9691 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
9692 * @see #SCROLLBARS_INSIDE_OVERLAY
9693 * @see #SCROLLBARS_INSIDE_INSET
9694 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9695 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -07009696 *
9697 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009698 */
9699 public void setScrollBarStyle(int style) {
9700 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
9701 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07009702 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009703 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009704 }
9705 }
9706
9707 /**
9708 * <p>Returns the current scrollbar style.</p>
9709 * @return the current scrollbar style
9710 * @see #SCROLLBARS_INSIDE_OVERLAY
9711 * @see #SCROLLBARS_INSIDE_INSET
9712 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9713 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -07009714 *
9715 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009716 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -07009717 @ViewDebug.ExportedProperty(mapping = {
9718 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
9719 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
9720 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
9721 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
9722 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009723 public int getScrollBarStyle() {
9724 return mViewFlags & SCROLLBARS_STYLE_MASK;
9725 }
9726
9727 /**
9728 * <p>Compute the horizontal range that the horizontal scrollbar
9729 * represents.</p>
9730 *
9731 * <p>The range is expressed in arbitrary units that must be the same as the
9732 * units used by {@link #computeHorizontalScrollExtent()} and
9733 * {@link #computeHorizontalScrollOffset()}.</p>
9734 *
9735 * <p>The default range is the drawing width of this view.</p>
9736 *
9737 * @return the total horizontal range represented by the horizontal
9738 * scrollbar
9739 *
9740 * @see #computeHorizontalScrollExtent()
9741 * @see #computeHorizontalScrollOffset()
9742 * @see android.widget.ScrollBarDrawable
9743 */
9744 protected int computeHorizontalScrollRange() {
9745 return getWidth();
9746 }
9747
9748 /**
9749 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
9750 * within the horizontal range. This value is used to compute the position
9751 * of the thumb within the scrollbar's track.</p>
9752 *
9753 * <p>The range is expressed in arbitrary units that must be the same as the
9754 * units used by {@link #computeHorizontalScrollRange()} and
9755 * {@link #computeHorizontalScrollExtent()}.</p>
9756 *
9757 * <p>The default offset is the scroll offset of this view.</p>
9758 *
9759 * @return the horizontal offset of the scrollbar's thumb
9760 *
9761 * @see #computeHorizontalScrollRange()
9762 * @see #computeHorizontalScrollExtent()
9763 * @see android.widget.ScrollBarDrawable
9764 */
9765 protected int computeHorizontalScrollOffset() {
9766 return mScrollX;
9767 }
9768
9769 /**
9770 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
9771 * within the horizontal range. This value is used to compute the length
9772 * of the thumb within the scrollbar's track.</p>
9773 *
9774 * <p>The range is expressed in arbitrary units that must be the same as the
9775 * units used by {@link #computeHorizontalScrollRange()} and
9776 * {@link #computeHorizontalScrollOffset()}.</p>
9777 *
9778 * <p>The default extent is the drawing width of this view.</p>
9779 *
9780 * @return the horizontal extent of the scrollbar's thumb
9781 *
9782 * @see #computeHorizontalScrollRange()
9783 * @see #computeHorizontalScrollOffset()
9784 * @see android.widget.ScrollBarDrawable
9785 */
9786 protected int computeHorizontalScrollExtent() {
9787 return getWidth();
9788 }
9789
9790 /**
9791 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
9792 *
9793 * <p>The range is expressed in arbitrary units that must be the same as the
9794 * units used by {@link #computeVerticalScrollExtent()} and
9795 * {@link #computeVerticalScrollOffset()}.</p>
9796 *
9797 * @return the total vertical range represented by the vertical scrollbar
9798 *
9799 * <p>The default range is the drawing height of this view.</p>
9800 *
9801 * @see #computeVerticalScrollExtent()
9802 * @see #computeVerticalScrollOffset()
9803 * @see android.widget.ScrollBarDrawable
9804 */
9805 protected int computeVerticalScrollRange() {
9806 return getHeight();
9807 }
9808
9809 /**
9810 * <p>Compute the vertical offset of the vertical scrollbar's thumb
9811 * within the horizontal range. This value is used to compute the position
9812 * of the thumb within the scrollbar's track.</p>
9813 *
9814 * <p>The range is expressed in arbitrary units that must be the same as the
9815 * units used by {@link #computeVerticalScrollRange()} and
9816 * {@link #computeVerticalScrollExtent()}.</p>
9817 *
9818 * <p>The default offset is the scroll offset of this view.</p>
9819 *
9820 * @return the vertical offset of the scrollbar's thumb
9821 *
9822 * @see #computeVerticalScrollRange()
9823 * @see #computeVerticalScrollExtent()
9824 * @see android.widget.ScrollBarDrawable
9825 */
9826 protected int computeVerticalScrollOffset() {
9827 return mScrollY;
9828 }
9829
9830 /**
9831 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
9832 * within the vertical range. This value is used to compute the length
9833 * of the thumb within the scrollbar's track.</p>
9834 *
9835 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08009836 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009837 * {@link #computeVerticalScrollOffset()}.</p>
9838 *
9839 * <p>The default extent is the drawing height of this view.</p>
9840 *
9841 * @return the vertical extent of the scrollbar's thumb
9842 *
9843 * @see #computeVerticalScrollRange()
9844 * @see #computeVerticalScrollOffset()
9845 * @see android.widget.ScrollBarDrawable
9846 */
9847 protected int computeVerticalScrollExtent() {
9848 return getHeight();
9849 }
9850
9851 /**
Adam Powell69159442011-06-13 17:53:06 -07009852 * Check if this view can be scrolled horizontally in a certain direction.
9853 *
9854 * @param direction Negative to check scrolling left, positive to check scrolling right.
9855 * @return true if this view can be scrolled in the specified direction, false otherwise.
9856 */
9857 public boolean canScrollHorizontally(int direction) {
9858 final int offset = computeHorizontalScrollOffset();
9859 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
9860 if (range == 0) return false;
9861 if (direction < 0) {
9862 return offset > 0;
9863 } else {
9864 return offset < range - 1;
9865 }
9866 }
9867
9868 /**
9869 * Check if this view can be scrolled vertically in a certain direction.
9870 *
9871 * @param direction Negative to check scrolling up, positive to check scrolling down.
9872 * @return true if this view can be scrolled in the specified direction, false otherwise.
9873 */
9874 public boolean canScrollVertically(int direction) {
9875 final int offset = computeVerticalScrollOffset();
9876 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
9877 if (range == 0) return false;
9878 if (direction < 0) {
9879 return offset > 0;
9880 } else {
9881 return offset < range - 1;
9882 }
9883 }
9884
9885 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009886 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
9887 * scrollbars are painted only if they have been awakened first.</p>
9888 *
9889 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08009890 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009891 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009892 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08009893 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009894 // scrollbars are drawn only when the animation is running
9895 final ScrollabilityCache cache = mScrollCache;
9896 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08009897
Mike Cleronf116bf82009-09-27 19:14:12 -07009898 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08009899
Mike Cleronf116bf82009-09-27 19:14:12 -07009900 if (state == ScrollabilityCache.OFF) {
9901 return;
9902 }
Joe Malin32736f02011-01-19 16:14:20 -08009903
Mike Cleronf116bf82009-09-27 19:14:12 -07009904 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08009905
Mike Cleronf116bf82009-09-27 19:14:12 -07009906 if (state == ScrollabilityCache.FADING) {
9907 // We're fading -- get our fade interpolation
9908 if (cache.interpolatorValues == null) {
9909 cache.interpolatorValues = new float[1];
9910 }
Joe Malin32736f02011-01-19 16:14:20 -08009911
Mike Cleronf116bf82009-09-27 19:14:12 -07009912 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08009913
Mike Cleronf116bf82009-09-27 19:14:12 -07009914 // Stops the animation if we're done
9915 if (cache.scrollBarInterpolator.timeToValues(values) ==
9916 Interpolator.Result.FREEZE_END) {
9917 cache.state = ScrollabilityCache.OFF;
9918 } else {
9919 cache.scrollBar.setAlpha(Math.round(values[0]));
9920 }
Joe Malin32736f02011-01-19 16:14:20 -08009921
9922 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07009923 // drawing. We only want this when we're fading so that
9924 // we prevent excessive redraws
9925 invalidate = true;
9926 } else {
9927 // We're just on -- but we may have been fading before so
9928 // reset alpha
9929 cache.scrollBar.setAlpha(255);
9930 }
9931
Joe Malin32736f02011-01-19 16:14:20 -08009932
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009933 final int viewFlags = mViewFlags;
9934
9935 final boolean drawHorizontalScrollBar =
9936 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9937 final boolean drawVerticalScrollBar =
9938 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
9939 && !isVerticalScrollBarHidden();
9940
9941 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
9942 final int width = mRight - mLeft;
9943 final int height = mBottom - mTop;
9944
9945 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009946
Mike Reede8853fc2009-09-04 14:01:48 -04009947 final int scrollX = mScrollX;
9948 final int scrollY = mScrollY;
9949 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
9950
Mike Cleronf116bf82009-09-27 19:14:12 -07009951 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08009952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009953 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009954 int size = scrollBar.getSize(false);
9955 if (size <= 0) {
9956 size = cache.scrollBarSize;
9957 }
9958
Mike Cleronf116bf82009-09-27 19:14:12 -07009959 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04009960 computeHorizontalScrollOffset(),
9961 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04009962 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07009963 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08009964 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07009965 left = scrollX + (mPaddingLeft & inside);
9966 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
9967 bottom = top + size;
9968 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
9969 if (invalidate) {
9970 invalidate(left, top, right, bottom);
9971 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009972 }
9973
9974 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009975 int size = scrollBar.getSize(true);
9976 if (size <= 0) {
9977 size = cache.scrollBarSize;
9978 }
9979
Mike Reede8853fc2009-09-04 14:01:48 -04009980 scrollBar.setParameters(computeVerticalScrollRange(),
9981 computeVerticalScrollOffset(),
9982 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08009983 switch (mVerticalScrollbarPosition) {
9984 default:
9985 case SCROLLBAR_POSITION_DEFAULT:
9986 case SCROLLBAR_POSITION_RIGHT:
9987 left = scrollX + width - size - (mUserPaddingRight & inside);
9988 break;
9989 case SCROLLBAR_POSITION_LEFT:
9990 left = scrollX + (mUserPaddingLeft & inside);
9991 break;
9992 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009993 top = scrollY + (mPaddingTop & inside);
9994 right = left + size;
9995 bottom = scrollY + height - (mUserPaddingBottom & inside);
9996 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
9997 if (invalidate) {
9998 invalidate(left, top, right, bottom);
9999 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010000 }
10001 }
10002 }
10003 }
Romain Guy8506ab42009-06-11 17:35:47 -070010004
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010005 /**
Romain Guy8506ab42009-06-11 17:35:47 -070010006 * 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 -080010007 * FastScroller is visible.
10008 * @return whether to temporarily hide the vertical scrollbar
10009 * @hide
10010 */
10011 protected boolean isVerticalScrollBarHidden() {
10012 return false;
10013 }
10014
10015 /**
10016 * <p>Draw the horizontal scrollbar if
10017 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
10018 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010019 * @param canvas the canvas on which to draw the scrollbar
10020 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010021 *
10022 * @see #isHorizontalScrollBarEnabled()
10023 * @see #computeHorizontalScrollRange()
10024 * @see #computeHorizontalScrollExtent()
10025 * @see #computeHorizontalScrollOffset()
10026 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070010027 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010028 */
Romain Guy8fb95422010-08-17 18:38:51 -070010029 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
10030 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010031 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010032 scrollBar.draw(canvas);
10033 }
Mike Reede8853fc2009-09-04 14:01:48 -040010034
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010035 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010036 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
10037 * returns true.</p>
10038 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010039 * @param canvas the canvas on which to draw the scrollbar
10040 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010041 *
10042 * @see #isVerticalScrollBarEnabled()
10043 * @see #computeVerticalScrollRange()
10044 * @see #computeVerticalScrollExtent()
10045 * @see #computeVerticalScrollOffset()
10046 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040010047 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010048 */
Romain Guy8fb95422010-08-17 18:38:51 -070010049 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
10050 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040010051 scrollBar.setBounds(l, t, r, b);
10052 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010053 }
10054
10055 /**
10056 * Implement this to do your drawing.
10057 *
10058 * @param canvas the canvas on which the background will be drawn
10059 */
10060 protected void onDraw(Canvas canvas) {
10061 }
10062
10063 /*
10064 * Caller is responsible for calling requestLayout if necessary.
10065 * (This allows addViewInLayout to not request a new layout.)
10066 */
10067 void assignParent(ViewParent parent) {
10068 if (mParent == null) {
10069 mParent = parent;
10070 } else if (parent == null) {
10071 mParent = null;
10072 } else {
10073 throw new RuntimeException("view " + this + " being added, but"
10074 + " it already has a parent");
10075 }
10076 }
10077
10078 /**
10079 * This is called when the view is attached to a window. At this point it
10080 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010081 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
10082 * however it may be called any time before the first onDraw -- including
10083 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010084 *
10085 * @see #onDetachedFromWindow()
10086 */
10087 protected void onAttachedToWindow() {
10088 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
10089 mParent.requestTransparentRegion(this);
10090 }
Adam Powell8568c3a2010-04-19 14:26:11 -070010091 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
10092 initialAwakenScrollBars();
10093 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
10094 }
Chet Haasea9b61ac2010-12-20 07:40:25 -080010095 jumpDrawablesToCurrentState();
Fabrice Di Meglioa6461452011-08-19 15:42:04 -070010096 // Order is important here: LayoutDirection MUST be resolved before Padding
10097 // and TextDirection
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010098 resolveLayoutDirection();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010099 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070010100 resolveTextDirection();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070010101 if (isFocused()) {
10102 InputMethodManager imm = InputMethodManager.peekInstance();
10103 imm.focusIn(this);
10104 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070010105 }
Cibu Johny86666632010-02-22 13:01:02 -080010106
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070010107 /**
Romain Guybb9908b2012-03-08 11:14:07 -080010108 * @see #onScreenStateChanged(int)
10109 */
10110 void dispatchScreenStateChanged(int screenState) {
10111 onScreenStateChanged(screenState);
10112 }
10113
10114 /**
10115 * This method is called whenever the state of the screen this view is
10116 * attached to changes. A state change will usually occurs when the screen
10117 * turns on or off (whether it happens automatically or the user does it
10118 * manually.)
10119 *
10120 * @param screenState The new state of the screen. Can be either
10121 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
10122 */
10123 public void onScreenStateChanged(int screenState) {
10124 }
10125
10126 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010127 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
10128 */
10129 private boolean hasRtlSupport() {
10130 return mContext.getApplicationInfo().hasRtlSupport();
10131 }
10132
10133 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010134 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
10135 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010136 * Will call {@link View#onResolvedLayoutDirectionChanged} when resolution is done.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070010137 */
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010138 public void resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010139 // Clear any previous layout direction resolution
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070010140 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010141
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010142 if (hasRtlSupport()) {
10143 // Set resolved depending on layout direction
10144 switch (getLayoutDirection()) {
10145 case LAYOUT_DIRECTION_INHERIT:
10146 // If this is root view, no need to look at parent's layout dir.
10147 if (canResolveLayoutDirection()) {
10148 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010149
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010150 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
10151 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
10152 }
10153 } else {
10154 // Nothing to do, LTR by default
10155 }
10156 break;
10157 case LAYOUT_DIRECTION_RTL:
10158 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
10159 break;
10160 case LAYOUT_DIRECTION_LOCALE:
10161 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010162 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
10163 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010164 break;
10165 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010166 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010167 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070010168 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010169
10170 // Set to resolved
10171 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010172 onResolvedLayoutDirectionChanged();
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080010173 // Resolve padding
10174 resolvePadding();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010175 }
10176
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070010177 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010178 * Called when layout direction has been resolved.
10179 *
10180 * The default implementation does nothing.
10181 */
10182 public void onResolvedLayoutDirectionChanged() {
10183 }
10184
10185 /**
10186 * Resolve padding depending on layout direction.
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070010187 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010188 public void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010189 // If the user specified the absolute padding (either with android:padding or
10190 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
10191 // use the default padding or the padding from the background drawable
10192 // (stored at this point in mPadding*)
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010193 int resolvedLayoutDirection = getResolvedLayoutDirection();
10194 switch (resolvedLayoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010195 case LAYOUT_DIRECTION_RTL:
10196 // Start user padding override Right user padding. Otherwise, if Right user
10197 // padding is not defined, use the default Right padding. If Right user padding
10198 // is defined, just use it.
10199 if (mUserPaddingStart >= 0) {
10200 mUserPaddingRight = mUserPaddingStart;
10201 } else if (mUserPaddingRight < 0) {
10202 mUserPaddingRight = mPaddingRight;
10203 }
10204 if (mUserPaddingEnd >= 0) {
10205 mUserPaddingLeft = mUserPaddingEnd;
10206 } else if (mUserPaddingLeft < 0) {
10207 mUserPaddingLeft = mPaddingLeft;
10208 }
10209 break;
10210 case LAYOUT_DIRECTION_LTR:
10211 default:
10212 // Start user padding override Left user padding. Otherwise, if Left user
10213 // padding is not defined, use the default left padding. If Left user padding
10214 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070010215 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010216 mUserPaddingLeft = mUserPaddingStart;
10217 } else if (mUserPaddingLeft < 0) {
10218 mUserPaddingLeft = mPaddingLeft;
10219 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070010220 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010221 mUserPaddingRight = mUserPaddingEnd;
10222 } else if (mUserPaddingRight < 0) {
10223 mUserPaddingRight = mPaddingRight;
10224 }
10225 }
10226
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010227 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
10228
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080010229 if(isPaddingRelative()) {
10230 setPaddingRelative(mUserPaddingStart, mPaddingTop, mUserPaddingEnd, mUserPaddingBottom);
10231 } else {
10232 recomputePadding();
10233 }
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010234 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010235 }
10236
10237 /**
10238 * Resolve padding depending on the layout direction. Subclasses that care about
10239 * padding resolution should override this method. The default implementation does
10240 * nothing.
10241 *
10242 * @param layoutDirection the direction of the layout
10243 *
Fabrice Di Meglioe8dc07d2012-03-09 17:10:19 -080010244 * @see {@link #LAYOUT_DIRECTION_LTR}
10245 * @see {@link #LAYOUT_DIRECTION_RTL}
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080010246 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010247 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010248 }
10249
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070010250 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010251 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070010252 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010253 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070010254 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010255 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010256 switch (getLayoutDirection()) {
10257 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010258 return (mParent != null) && (mParent instanceof ViewGroup);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010259 default:
10260 return true;
10261 }
10262 }
10263
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010264 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010265 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
10266 * when reset is done.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010267 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010268 public void resetResolvedLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070010269 // Reset the current resolved bits
10270 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010271 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080010272 // Reset also the text direction
10273 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070010274 }
10275
10276 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080010277 * Called during reset of resolved layout direction.
10278 *
10279 * Subclasses need to override this method to clear cached information that depends on the
10280 * resolved layout direction, or to inform child views that inherit their layout direction.
10281 *
10282 * The default implementation does nothing.
10283 */
10284 public void onResolvedLayoutDirectionReset() {
10285 }
10286
10287 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010288 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070010289 *
10290 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080010291 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070010292 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -070010293 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -080010294 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010295 }
10296
10297 /**
10298 * This is called when the view is detached from a window. At this point it
10299 * no longer has a surface for drawing.
10300 *
10301 * @see #onAttachedToWindow()
10302 */
10303 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -080010304 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080010305
Romain Guya440b002010-02-24 15:57:54 -080010306 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050010307 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080010308 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070010309 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080010310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010311 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080010312
Romain Guya998dff2012-03-23 18:58:36 -070010313 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080010314
10315 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070010316 if (mDisplayList != null) {
10317 mAttachInfo.mViewRootImpl.invalidateDisplayList(mDisplayList);
10318 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010319 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070010320 } else {
10321 if (mDisplayList != null) {
10322 // Should never happen
10323 mDisplayList.invalidate();
10324 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -080010325 }
10326
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080010327 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070010328
10329 resetResolvedLayoutDirection();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010330 }
10331
10332 /**
10333 * @return The number of times this view has been attached to a window
10334 */
10335 protected int getWindowAttachCount() {
10336 return mWindowAttachCount;
10337 }
10338
10339 /**
10340 * Retrieve a unique token identifying the window this view is attached to.
10341 * @return Return the window's token for use in
10342 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
10343 */
10344 public IBinder getWindowToken() {
10345 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
10346 }
10347
10348 /**
10349 * Retrieve a unique token identifying the top-level "real" window of
10350 * the window that this view is attached to. That is, this is like
10351 * {@link #getWindowToken}, except if the window this view in is a panel
10352 * window (attached to another containing window), then the token of
10353 * the containing window is returned instead.
10354 *
10355 * @return Returns the associated window token, either
10356 * {@link #getWindowToken()} or the containing window's token.
10357 */
10358 public IBinder getApplicationWindowToken() {
10359 AttachInfo ai = mAttachInfo;
10360 if (ai != null) {
10361 IBinder appWindowToken = ai.mPanelParentWindowToken;
10362 if (appWindowToken == null) {
10363 appWindowToken = ai.mWindowToken;
10364 }
10365 return appWindowToken;
10366 }
10367 return null;
10368 }
10369
10370 /**
10371 * Retrieve private session object this view hierarchy is using to
10372 * communicate with the window manager.
10373 * @return the session object to communicate with the window manager
10374 */
10375 /*package*/ IWindowSession getWindowSession() {
10376 return mAttachInfo != null ? mAttachInfo.mSession : null;
10377 }
10378
10379 /**
10380 * @param info the {@link android.view.View.AttachInfo} to associated with
10381 * this view
10382 */
10383 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
10384 //System.out.println("Attached! " + this);
10385 mAttachInfo = info;
10386 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080010387 // We will need to evaluate the drawable state at least once.
10388 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010389 if (mFloatingTreeObserver != null) {
10390 info.mTreeObserver.merge(mFloatingTreeObserver);
10391 mFloatingTreeObserver = null;
10392 }
10393 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
10394 mAttachInfo.mScrollContainers.add(this);
10395 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
10396 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070010397 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010398 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080010399
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070010400 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080010401 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070010402 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080010403 if (listeners != null && listeners.size() > 0) {
10404 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
10405 // perform the dispatching. The iterator is a safe guard against listeners that
10406 // could mutate the list by calling the various add/remove methods. This prevents
10407 // the array from being modified while we iterate it.
10408 for (OnAttachStateChangeListener listener : listeners) {
10409 listener.onViewAttachedToWindow(this);
10410 }
10411 }
10412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010413 int vis = info.mWindowVisibility;
10414 if (vis != GONE) {
10415 onWindowVisibilityChanged(vis);
10416 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080010417 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
10418 // If nobody has evaluated the drawable state yet, then do it now.
10419 refreshDrawableState();
10420 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010421 }
10422
10423 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010424 AttachInfo info = mAttachInfo;
10425 if (info != null) {
10426 int vis = info.mWindowVisibility;
10427 if (vis != GONE) {
10428 onWindowVisibilityChanged(GONE);
10429 }
10430 }
10431
10432 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080010433
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070010434 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080010435 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070010436 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080010437 if (listeners != null && listeners.size() > 0) {
10438 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
10439 // perform the dispatching. The iterator is a safe guard against listeners that
10440 // could mutate the list by calling the various add/remove methods. This prevents
10441 // the array from being modified while we iterate it.
10442 for (OnAttachStateChangeListener listener : listeners) {
10443 listener.onViewDetachedFromWindow(this);
10444 }
10445 }
10446
Romain Guy01d5edc2011-01-28 11:28:53 -080010447 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010448 mAttachInfo.mScrollContainers.remove(this);
10449 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
10450 }
Romain Guy01d5edc2011-01-28 11:28:53 -080010451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010452 mAttachInfo = null;
10453 }
10454
10455 /**
10456 * Store this view hierarchy's frozen state into the given container.
10457 *
10458 * @param container The SparseArray in which to save the view's state.
10459 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010460 * @see #restoreHierarchyState(android.util.SparseArray)
10461 * @see #dispatchSaveInstanceState(android.util.SparseArray)
10462 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010463 */
10464 public void saveHierarchyState(SparseArray<Parcelable> container) {
10465 dispatchSaveInstanceState(container);
10466 }
10467
10468 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010469 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
10470 * this view and its children. May be overridden to modify how freezing happens to a
10471 * 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 -080010472 *
10473 * @param container The SparseArray in which to save the view's state.
10474 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010475 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
10476 * @see #saveHierarchyState(android.util.SparseArray)
10477 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010478 */
10479 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
10480 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
10481 mPrivateFlags &= ~SAVE_STATE_CALLED;
10482 Parcelable state = onSaveInstanceState();
10483 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
10484 throw new IllegalStateException(
10485 "Derived class did not call super.onSaveInstanceState()");
10486 }
10487 if (state != null) {
10488 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
10489 // + ": " + state);
10490 container.put(mID, state);
10491 }
10492 }
10493 }
10494
10495 /**
10496 * Hook allowing a view to generate a representation of its internal state
10497 * that can later be used to create a new instance with that same state.
10498 * This state should only contain information that is not persistent or can
10499 * not be reconstructed later. For example, you will never store your
10500 * current position on screen because that will be computed again when a
10501 * new instance of the view is placed in its view hierarchy.
10502 * <p>
10503 * Some examples of things you may store here: the current cursor position
10504 * in a text view (but usually not the text itself since that is stored in a
10505 * content provider or other persistent storage), the currently selected
10506 * item in a list view.
10507 *
10508 * @return Returns a Parcelable object containing the view's current dynamic
10509 * state, or null if there is nothing interesting to save. The
10510 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070010511 * @see #onRestoreInstanceState(android.os.Parcelable)
10512 * @see #saveHierarchyState(android.util.SparseArray)
10513 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010514 * @see #setSaveEnabled(boolean)
10515 */
10516 protected Parcelable onSaveInstanceState() {
10517 mPrivateFlags |= SAVE_STATE_CALLED;
10518 return BaseSavedState.EMPTY_STATE;
10519 }
10520
10521 /**
10522 * Restore this view hierarchy's frozen state from the given container.
10523 *
10524 * @param container The SparseArray which holds previously frozen states.
10525 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010526 * @see #saveHierarchyState(android.util.SparseArray)
10527 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
10528 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010529 */
10530 public void restoreHierarchyState(SparseArray<Parcelable> container) {
10531 dispatchRestoreInstanceState(container);
10532 }
10533
10534 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010535 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
10536 * state for this view and its children. May be overridden to modify how restoring
10537 * happens to a view's children; for example, some views may want to not store state
10538 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010539 *
10540 * @param container The SparseArray which holds previously saved state.
10541 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010542 * @see #dispatchSaveInstanceState(android.util.SparseArray)
10543 * @see #restoreHierarchyState(android.util.SparseArray)
10544 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010545 */
10546 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
10547 if (mID != NO_ID) {
10548 Parcelable state = container.get(mID);
10549 if (state != null) {
10550 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
10551 // + ": " + state);
10552 mPrivateFlags &= ~SAVE_STATE_CALLED;
10553 onRestoreInstanceState(state);
10554 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
10555 throw new IllegalStateException(
10556 "Derived class did not call super.onRestoreInstanceState()");
10557 }
10558 }
10559 }
10560 }
10561
10562 /**
10563 * Hook allowing a view to re-apply a representation of its internal state that had previously
10564 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
10565 * null state.
10566 *
10567 * @param state The frozen state that had previously been returned by
10568 * {@link #onSaveInstanceState}.
10569 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010570 * @see #onSaveInstanceState()
10571 * @see #restoreHierarchyState(android.util.SparseArray)
10572 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010573 */
10574 protected void onRestoreInstanceState(Parcelable state) {
10575 mPrivateFlags |= SAVE_STATE_CALLED;
10576 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080010577 throw new IllegalArgumentException("Wrong state class, expecting View State but "
10578 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080010579 + "when two views of different type have the same id in the same hierarchy. "
10580 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080010581 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010582 }
10583 }
10584
10585 /**
10586 * <p>Return the time at which the drawing of the view hierarchy started.</p>
10587 *
10588 * @return the drawing start time in milliseconds
10589 */
10590 public long getDrawingTime() {
10591 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
10592 }
10593
10594 /**
10595 * <p>Enables or disables the duplication of the parent's state into this view. When
10596 * duplication is enabled, this view gets its drawable state from its parent rather
10597 * than from its own internal properties.</p>
10598 *
10599 * <p>Note: in the current implementation, setting this property to true after the
10600 * view was added to a ViewGroup might have no effect at all. This property should
10601 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
10602 *
10603 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
10604 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010605 *
Gilles Debunnefb817032011-01-13 13:52:49 -080010606 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
10607 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010608 *
10609 * @param enabled True to enable duplication of the parent's drawable state, false
10610 * to disable it.
10611 *
10612 * @see #getDrawableState()
10613 * @see #isDuplicateParentStateEnabled()
10614 */
10615 public void setDuplicateParentStateEnabled(boolean enabled) {
10616 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
10617 }
10618
10619 /**
10620 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
10621 *
10622 * @return True if this view's drawable state is duplicated from the parent,
10623 * false otherwise
10624 *
10625 * @see #getDrawableState()
10626 * @see #setDuplicateParentStateEnabled(boolean)
10627 */
10628 public boolean isDuplicateParentStateEnabled() {
10629 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
10630 }
10631
10632 /**
Romain Guy171c5922011-01-06 10:04:23 -080010633 * <p>Specifies the type of layer backing this view. The layer can be
10634 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
10635 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010636 *
Romain Guy171c5922011-01-06 10:04:23 -080010637 * <p>A layer is associated with an optional {@link android.graphics.Paint}
10638 * instance that controls how the layer is composed on screen. The following
10639 * properties of the paint are taken into account when composing the layer:</p>
10640 * <ul>
10641 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
10642 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
10643 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
10644 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080010645 *
Romain Guy171c5922011-01-06 10:04:23 -080010646 * <p>If this view has an alpha value set to < 1.0 by calling
10647 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
10648 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
10649 * equivalent to setting a hardware layer on this view and providing a paint with
10650 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080010651 *
Romain Guy171c5922011-01-06 10:04:23 -080010652 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
10653 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
10654 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010655 *
Romain Guy171c5922011-01-06 10:04:23 -080010656 * @param layerType The ype of layer to use with this view, must be one of
10657 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10658 * {@link #LAYER_TYPE_HARDWARE}
10659 * @param paint The paint used to compose the layer. This argument is optional
10660 * and can be null. It is ignored when the layer type is
10661 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080010662 *
10663 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080010664 * @see #LAYER_TYPE_NONE
10665 * @see #LAYER_TYPE_SOFTWARE
10666 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080010667 * @see #setAlpha(float)
10668 *
Romain Guy171c5922011-01-06 10:04:23 -080010669 * @attr ref android.R.styleable#View_layerType
10670 */
10671 public void setLayerType(int layerType, Paint paint) {
10672 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080010673 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080010674 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
10675 }
Chet Haasedaf98e92011-01-10 14:10:36 -080010676
Romain Guyd6cd5722011-01-17 14:42:41 -080010677 if (layerType == mLayerType) {
10678 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
10679 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010680 invalidateParentCaches();
10681 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080010682 }
10683 return;
10684 }
Romain Guy171c5922011-01-06 10:04:23 -080010685
10686 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080010687 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070010688 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070010689 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080010690 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080010691 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070010692 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080010693 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080010694 default:
10695 break;
Romain Guy171c5922011-01-06 10:04:23 -080010696 }
10697
10698 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080010699 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
10700 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
10701 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080010702
Romain Guy0fd89bf2011-01-26 15:41:30 -080010703 invalidateParentCaches();
10704 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080010705 }
10706
10707 /**
Romain Guy59c7f802011-09-29 17:21:45 -070010708 * Indicates whether this view has a static layer. A view with layer type
10709 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
10710 * dynamic.
10711 */
10712 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080010713 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070010714 }
10715
10716 /**
Romain Guy171c5922011-01-06 10:04:23 -080010717 * Indicates what type of layer is currently associated with this view. By default
10718 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
10719 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
10720 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080010721 *
Romain Guy171c5922011-01-06 10:04:23 -080010722 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10723 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080010724 *
10725 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070010726 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080010727 * @see #LAYER_TYPE_NONE
10728 * @see #LAYER_TYPE_SOFTWARE
10729 * @see #LAYER_TYPE_HARDWARE
10730 */
10731 public int getLayerType() {
10732 return mLayerType;
10733 }
Joe Malin32736f02011-01-19 16:14:20 -080010734
Romain Guy6c319ca2011-01-11 14:29:25 -080010735 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080010736 * Forces this view's layer to be created and this view to be rendered
10737 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
10738 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070010739 *
Romain Guyf1ae1062011-03-02 18:16:04 -080010740 * This method can for instance be used to render a view into its layer before
10741 * starting an animation. If this view is complex, rendering into the layer
10742 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070010743 *
Romain Guyf1ae1062011-03-02 18:16:04 -080010744 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070010745 *
10746 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080010747 */
10748 public void buildLayer() {
10749 if (mLayerType == LAYER_TYPE_NONE) return;
10750
10751 if (mAttachInfo == null) {
10752 throw new IllegalStateException("This view must be attached to a window first");
10753 }
10754
10755 switch (mLayerType) {
10756 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080010757 if (mAttachInfo.mHardwareRenderer != null &&
10758 mAttachInfo.mHardwareRenderer.isEnabled() &&
10759 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080010760 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080010761 }
Romain Guyf1ae1062011-03-02 18:16:04 -080010762 break;
10763 case LAYER_TYPE_SOFTWARE:
10764 buildDrawingCache(true);
10765 break;
10766 }
10767 }
Philip Milne6c8ea062012-04-03 17:38:43 -070010768
Romain Guy9c4b79a2011-11-10 19:23:58 -080010769 // Make sure the HardwareRenderer.validate() was invoked before calling this method
10770 void flushLayer() {
10771 if (mLayerType == LAYER_TYPE_HARDWARE && mHardwareLayer != null) {
10772 mHardwareLayer.flush();
10773 }
10774 }
Romain Guyf1ae1062011-03-02 18:16:04 -080010775
10776 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080010777 * <p>Returns a hardware layer that can be used to draw this view again
10778 * without executing its draw method.</p>
10779 *
10780 * @return A HardwareLayer ready to render, or null if an error occurred.
10781 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080010782 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070010783 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
10784 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080010785 return null;
10786 }
Philip Milne6c8ea062012-04-03 17:38:43 -070010787
Romain Guy9c4b79a2011-11-10 19:23:58 -080010788 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080010789
10790 final int width = mRight - mLeft;
10791 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080010792
Romain Guy6c319ca2011-01-11 14:29:25 -080010793 if (width == 0 || height == 0) {
10794 return null;
10795 }
10796
10797 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
10798 if (mHardwareLayer == null) {
10799 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
10800 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070010801 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080010802 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
10803 mHardwareLayer.resize(width, height);
Michael Jurka952e02b2012-03-13 18:34:35 -070010804 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080010805 }
10806
Romain Guy5cd5c3f2011-10-17 17:10:02 -070010807 // The layer is not valid if the underlying GPU resources cannot be allocated
10808 if (!mHardwareLayer.isValid()) {
10809 return null;
10810 }
10811
Chet Haasea1cff502012-02-21 13:43:44 -080010812 mHardwareLayer.redraw(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
Michael Jurka7e52caf2012-03-06 15:57:06 -080010813 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010814 }
10815
10816 return mHardwareLayer;
10817 }
Romain Guy171c5922011-01-06 10:04:23 -080010818
Romain Guy589b0bb2011-10-10 13:57:47 -070010819 /**
10820 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070010821 *
Romain Guy589b0bb2011-10-10 13:57:47 -070010822 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070010823 *
10824 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070010825 * @see #LAYER_TYPE_HARDWARE
10826 */
Romain Guya998dff2012-03-23 18:58:36 -070010827 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070010828 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080010829 AttachInfo info = mAttachInfo;
10830 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070010831 info.mHardwareRenderer.isEnabled() &&
10832 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080010833 mHardwareLayer.destroy();
10834 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080010835
Romain Guy9c4b79a2011-11-10 19:23:58 -080010836 invalidate(true);
10837 invalidateParentCaches();
10838 }
Romain Guy65b345f2011-07-27 18:51:50 -070010839 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070010840 }
Romain Guy65b345f2011-07-27 18:51:50 -070010841 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070010842 }
10843
Romain Guy171c5922011-01-06 10:04:23 -080010844 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080010845 * Destroys all hardware rendering resources. This method is invoked
10846 * when the system needs to reclaim resources. Upon execution of this
10847 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070010848 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080010849 * Note: you <strong>must</strong> call
10850 * <code>super.destroyHardwareResources()</code> when overriding
10851 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070010852 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080010853 * @hide
10854 */
10855 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070010856 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080010857 }
10858
10859 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010860 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
10861 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
10862 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
10863 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
10864 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
10865 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010866 *
Romain Guy171c5922011-01-06 10:04:23 -080010867 * <p>Enabling the drawing cache is similar to
10868 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080010869 * acceleration is turned off. When hardware acceleration is turned on, enabling the
10870 * drawing cache has no effect on rendering because the system uses a different mechanism
10871 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
10872 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
10873 * for information on how to enable software and hardware layers.</p>
10874 *
10875 * <p>This API can be used to manually generate
10876 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
10877 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010878 *
10879 * @param enabled true to enable the drawing cache, false otherwise
10880 *
10881 * @see #isDrawingCacheEnabled()
10882 * @see #getDrawingCache()
10883 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080010884 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010885 */
10886 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010887 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010888 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
10889 }
10890
10891 /**
10892 * <p>Indicates whether the drawing cache is enabled for this view.</p>
10893 *
10894 * @return true if the drawing cache is enabled
10895 *
10896 * @see #setDrawingCacheEnabled(boolean)
10897 * @see #getDrawingCache()
10898 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010899 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010900 public boolean isDrawingCacheEnabled() {
10901 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
10902 }
10903
10904 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080010905 * Debugging utility which recursively outputs the dirty state of a view and its
10906 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080010907 *
Chet Haasedaf98e92011-01-10 14:10:36 -080010908 * @hide
10909 */
Romain Guy676b1732011-02-14 14:45:33 -080010910 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080010911 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
10912 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
10913 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
10914 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
10915 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
10916 if (clear) {
10917 mPrivateFlags &= clearMask;
10918 }
10919 if (this instanceof ViewGroup) {
10920 ViewGroup parent = (ViewGroup) this;
10921 final int count = parent.getChildCount();
10922 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080010923 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080010924 child.outputDirtyFlags(indent + " ", clear, clearMask);
10925 }
10926 }
10927 }
10928
10929 /**
10930 * This method is used by ViewGroup to cause its children to restore or recreate their
10931 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
10932 * to recreate its own display list, which would happen if it went through the normal
10933 * draw/dispatchDraw mechanisms.
10934 *
10935 * @hide
10936 */
10937 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080010938
10939 /**
10940 * A view that is not attached or hardware accelerated cannot create a display list.
10941 * This method checks these conditions and returns the appropriate result.
10942 *
10943 * @return true if view has the ability to create a display list, false otherwise.
10944 *
10945 * @hide
10946 */
10947 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080010948 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080010949 }
Joe Malin32736f02011-01-19 16:14:20 -080010950
Chet Haasedaf98e92011-01-10 14:10:36 -080010951 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080010952 * @return The HardwareRenderer associated with that view or null if hardware rendering
10953 * is not supported or this this has not been attached to a window.
10954 *
10955 * @hide
10956 */
10957 public HardwareRenderer getHardwareRenderer() {
10958 if (mAttachInfo != null) {
10959 return mAttachInfo.mHardwareRenderer;
10960 }
10961 return null;
10962 }
10963
10964 /**
Chet Haasea1cff502012-02-21 13:43:44 -080010965 * Returns a DisplayList. If the incoming displayList is null, one will be created.
10966 * Otherwise, the same display list will be returned (after having been rendered into
10967 * along the way, depending on the invalidation state of the view).
10968 *
10969 * @param displayList The previous version of this displayList, could be null.
10970 * @param isLayer Whether the requester of the display list is a layer. If so,
10971 * the view will avoid creating a layer inside the resulting display list.
10972 * @return A new or reused DisplayList object.
10973 */
10974 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
10975 if (!canHaveDisplayList()) {
10976 return null;
10977 }
10978
10979 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
10980 displayList == null || !displayList.isValid() ||
10981 (!isLayer && mRecreateDisplayList))) {
10982 // Don't need to recreate the display list, just need to tell our
10983 // children to restore/recreate theirs
10984 if (displayList != null && displayList.isValid() &&
10985 !isLayer && !mRecreateDisplayList) {
10986 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10987 mPrivateFlags &= ~DIRTY_MASK;
10988 dispatchGetDisplayList();
10989
10990 return displayList;
10991 }
10992
10993 if (!isLayer) {
10994 // If we got here, we're recreating it. Mark it as such to ensure that
10995 // we copy in child display lists into ours in drawChild()
10996 mRecreateDisplayList = true;
10997 }
10998 if (displayList == null) {
10999 final String name = getClass().getSimpleName();
11000 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
11001 // If we're creating a new display list, make sure our parent gets invalidated
11002 // since they will need to recreate their display list to account for this
11003 // new child display list.
11004 invalidateParentCaches();
11005 }
11006
11007 boolean caching = false;
11008 final HardwareCanvas canvas = displayList.start();
11009 int restoreCount = 0;
11010 int width = mRight - mLeft;
11011 int height = mBottom - mTop;
11012
11013 try {
11014 canvas.setViewport(width, height);
11015 // The dirty rect should always be null for a display list
11016 canvas.onPreDraw(null);
11017 int layerType = (
11018 !(mParent instanceof ViewGroup) || ((ViewGroup)mParent).mDrawLayers) ?
11019 getLayerType() : LAYER_TYPE_NONE;
Chet Haaseb85967b2012-03-26 14:37:51 -070011020 if (!isLayer && layerType != LAYER_TYPE_NONE && USE_DISPLAY_LIST_PROPERTIES) {
11021 if (layerType == LAYER_TYPE_HARDWARE) {
11022 final HardwareLayer layer = getHardwareLayer();
11023 if (layer != null && layer.isValid()) {
11024 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
11025 } else {
11026 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
11027 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
11028 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
11029 }
11030 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080011031 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070011032 buildDrawingCache(true);
11033 Bitmap cache = getDrawingCache(true);
11034 if (cache != null) {
11035 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
11036 caching = true;
11037 }
Chet Haasea1cff502012-02-21 13:43:44 -080011038 }
Chet Haasea1cff502012-02-21 13:43:44 -080011039 } else {
11040
11041 computeScroll();
11042
11043 if (!USE_DISPLAY_LIST_PROPERTIES) {
11044 restoreCount = canvas.save();
11045 }
11046 canvas.translate(-mScrollX, -mScrollY);
11047 if (!isLayer) {
11048 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
11049 mPrivateFlags &= ~DIRTY_MASK;
11050 }
11051
11052 // Fast path for layouts with no backgrounds
11053 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
11054 dispatchDraw(canvas);
11055 } else {
11056 draw(canvas);
11057 }
11058 }
11059 } finally {
11060 if (USE_DISPLAY_LIST_PROPERTIES) {
11061 canvas.restoreToCount(restoreCount);
11062 }
11063 canvas.onPostDraw();
11064
11065 displayList.end();
11066 if (USE_DISPLAY_LIST_PROPERTIES) {
11067 displayList.setCaching(caching);
11068 }
11069 if (isLayer && USE_DISPLAY_LIST_PROPERTIES) {
11070 displayList.setLeftTopRightBottom(0, 0, width, height);
11071 } else {
11072 setDisplayListProperties(displayList);
11073 }
11074 }
11075 } else if (!isLayer) {
11076 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
11077 mPrivateFlags &= ~DIRTY_MASK;
11078 }
11079
11080 return displayList;
11081 }
11082
11083 /**
11084 * Get the DisplayList for the HardwareLayer
11085 *
11086 * @param layer The HardwareLayer whose DisplayList we want
11087 * @return A DisplayList fopr the specified HardwareLayer
11088 */
11089 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
11090 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
11091 layer.setDisplayList(displayList);
11092 return displayList;
11093 }
11094
11095
11096 /**
Romain Guyb051e892010-09-28 19:09:36 -070011097 * <p>Returns a display list that can be used to draw this view again
11098 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011099 *
Romain Guyb051e892010-09-28 19:09:36 -070011100 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080011101 *
11102 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070011103 */
Chet Haasedaf98e92011-01-10 14:10:36 -080011104 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080011105 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070011106 return mDisplayList;
11107 }
11108
11109 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070011110 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011111 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011112 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080011113 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011114 * @see #getDrawingCache(boolean)
11115 */
11116 public Bitmap getDrawingCache() {
11117 return getDrawingCache(false);
11118 }
11119
11120 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011121 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
11122 * is null when caching is disabled. If caching is enabled and the cache is not ready,
11123 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
11124 * draw from the cache when the cache is enabled. To benefit from the cache, you must
11125 * request the drawing cache by calling this method and draw it on screen if the
11126 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011127 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011128 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
11129 * this method will create a bitmap of the same size as this view. Because this bitmap
11130 * will be drawn scaled by the parent ViewGroup, the result on screen might show
11131 * scaling artifacts. To avoid such artifacts, you should call this method by setting
11132 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
11133 * size than the view. This implies that your application must be able to handle this
11134 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011135 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011136 * @param autoScale Indicates whether the generated bitmap should be scaled based on
11137 * the current density of the screen when the application is in compatibility
11138 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011139 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011140 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080011141 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011142 * @see #setDrawingCacheEnabled(boolean)
11143 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070011144 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011145 * @see #destroyDrawingCache()
11146 */
Romain Guyfbd8f692009-06-26 14:51:58 -070011147 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011148 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
11149 return null;
11150 }
11151 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070011152 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011153 }
Romain Guy02890fd2010-08-06 17:58:44 -070011154 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011155 }
11156
11157 /**
11158 * <p>Frees the resources used by the drawing cache. If you call
11159 * {@link #buildDrawingCache()} manually without calling
11160 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
11161 * should cleanup the cache with this method afterwards.</p>
11162 *
11163 * @see #setDrawingCacheEnabled(boolean)
11164 * @see #buildDrawingCache()
11165 * @see #getDrawingCache()
11166 */
11167 public void destroyDrawingCache() {
11168 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070011169 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011170 mDrawingCache = null;
11171 }
Romain Guyfbd8f692009-06-26 14:51:58 -070011172 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070011173 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070011174 mUnscaledDrawingCache = null;
11175 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011176 }
11177
11178 /**
11179 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070011180 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011181 * view will always be drawn on top of a solid color.
11182 *
11183 * @param color The background color to use for the drawing cache's bitmap
11184 *
11185 * @see #setDrawingCacheEnabled(boolean)
11186 * @see #buildDrawingCache()
11187 * @see #getDrawingCache()
11188 */
11189 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080011190 if (color != mDrawingCacheBackgroundColor) {
11191 mDrawingCacheBackgroundColor = color;
11192 mPrivateFlags &= ~DRAWING_CACHE_VALID;
11193 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011194 }
11195
11196 /**
11197 * @see #setDrawingCacheBackgroundColor(int)
11198 *
11199 * @return The background color to used for the drawing cache's bitmap
11200 */
11201 public int getDrawingCacheBackgroundColor() {
11202 return mDrawingCacheBackgroundColor;
11203 }
11204
11205 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070011206 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011207 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011208 * @see #buildDrawingCache(boolean)
11209 */
11210 public void buildDrawingCache() {
11211 buildDrawingCache(false);
11212 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080011213
Romain Guyfbd8f692009-06-26 14:51:58 -070011214 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011215 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
11216 *
11217 * <p>If you call {@link #buildDrawingCache()} manually without calling
11218 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
11219 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011220 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011221 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
11222 * this method will create a bitmap of the same size as this view. Because this bitmap
11223 * will be drawn scaled by the parent ViewGroup, the result on screen might show
11224 * scaling artifacts. To avoid such artifacts, you should call this method by setting
11225 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
11226 * size than the view. This implies that your application must be able to handle this
11227 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011228 *
Romain Guy0d9275e2010-10-26 14:22:30 -070011229 * <p>You should avoid calling this method when hardware acceleration is enabled. If
11230 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080011231 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070011232 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011233 *
11234 * @see #getDrawingCache()
11235 * @see #destroyDrawingCache()
11236 */
Romain Guyfbd8f692009-06-26 14:51:58 -070011237 public void buildDrawingCache(boolean autoScale) {
11238 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070011239 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080011240 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011241
11242 if (ViewDebug.TRACE_HIERARCHY) {
11243 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
11244 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011245
Romain Guy8506ab42009-06-11 17:35:47 -070011246 int width = mRight - mLeft;
11247 int height = mBottom - mTop;
11248
11249 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070011250 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070011251
Romain Guye1123222009-06-29 14:24:56 -070011252 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070011253 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
11254 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070011255 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011256
11257 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070011258 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080011259 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011260
11261 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070011262 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080011263 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011264 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
11265 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080011266 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011267 return;
11268 }
11269
11270 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070011271 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011272
11273 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011274 Bitmap.Config quality;
11275 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080011276 // Never pick ARGB_4444 because it looks awful
11277 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011278 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
11279 case DRAWING_CACHE_QUALITY_AUTO:
11280 quality = Bitmap.Config.ARGB_8888;
11281 break;
11282 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080011283 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011284 break;
11285 case DRAWING_CACHE_QUALITY_HIGH:
11286 quality = Bitmap.Config.ARGB_8888;
11287 break;
11288 default:
11289 quality = Bitmap.Config.ARGB_8888;
11290 break;
11291 }
11292 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070011293 // Optimization for translucent windows
11294 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080011295 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011296 }
11297
11298 // Try to cleanup memory
11299 if (bitmap != null) bitmap.recycle();
11300
11301 try {
11302 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070011303 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070011304 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070011305 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070011306 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070011307 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070011308 }
Adam Powell26153a32010-11-08 15:22:27 -080011309 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011310 } catch (OutOfMemoryError e) {
11311 // If there is not enough memory to create the bitmap cache, just
11312 // ignore the issue as bitmap caches are not required to draw the
11313 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070011314 if (autoScale) {
11315 mDrawingCache = null;
11316 } else {
11317 mUnscaledDrawingCache = null;
11318 }
Romain Guy0211a0a2011-02-14 16:34:59 -080011319 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011320 return;
11321 }
11322
11323 clear = drawingCacheBackgroundColor != 0;
11324 }
11325
11326 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011327 if (attachInfo != null) {
11328 canvas = attachInfo.mCanvas;
11329 if (canvas == null) {
11330 canvas = new Canvas();
11331 }
11332 canvas.setBitmap(bitmap);
11333 // Temporarily clobber the cached Canvas in case one of our children
11334 // is also using a drawing cache. Without this, the children would
11335 // steal the canvas by attaching their own bitmap to it and bad, bad
11336 // thing would happen (invisible views, corrupted drawings, etc.)
11337 attachInfo.mCanvas = null;
11338 } else {
11339 // This case should hopefully never or seldom happen
11340 canvas = new Canvas(bitmap);
11341 }
11342
11343 if (clear) {
11344 bitmap.eraseColor(drawingCacheBackgroundColor);
11345 }
11346
11347 computeScroll();
11348 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080011349
Romain Guye1123222009-06-29 14:24:56 -070011350 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070011351 final float scale = attachInfo.mApplicationScale;
11352 canvas.scale(scale, scale);
11353 }
Joe Malin32736f02011-01-19 16:14:20 -080011354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011355 canvas.translate(-mScrollX, -mScrollY);
11356
Romain Guy5bcdff42009-05-14 21:27:18 -070011357 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080011358 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
11359 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070011360 mPrivateFlags |= DRAWING_CACHE_VALID;
11361 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011362
11363 // Fast path for layouts with no backgrounds
11364 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
11365 if (ViewDebug.TRACE_HIERARCHY) {
11366 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
11367 }
Romain Guy5bcdff42009-05-14 21:27:18 -070011368 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011369 dispatchDraw(canvas);
11370 } else {
11371 draw(canvas);
11372 }
11373
11374 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070011375 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011376
11377 if (attachInfo != null) {
11378 // Restore the cached Canvas for our siblings
11379 attachInfo.mCanvas = canvas;
11380 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011381 }
11382 }
11383
11384 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011385 * Create a snapshot of the view into a bitmap. We should probably make
11386 * some form of this public, but should think about the API.
11387 */
Romain Guy223ff5c2010-03-02 17:07:47 -080011388 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070011389 int width = mRight - mLeft;
11390 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011391
Dianne Hackborn8cae1242009-09-10 14:32:16 -070011392 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070011393 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070011394 width = (int) ((width * scale) + 0.5f);
11395 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080011396
Romain Guy8c11e312009-09-14 15:15:30 -070011397 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011398 if (bitmap == null) {
11399 throw new OutOfMemoryError();
11400 }
11401
Romain Guyc529d8d2011-09-06 15:01:39 -070011402 Resources resources = getResources();
11403 if (resources != null) {
11404 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
11405 }
Joe Malin32736f02011-01-19 16:14:20 -080011406
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011407 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011408 if (attachInfo != null) {
11409 canvas = attachInfo.mCanvas;
11410 if (canvas == null) {
11411 canvas = new Canvas();
11412 }
11413 canvas.setBitmap(bitmap);
11414 // Temporarily clobber the cached Canvas in case one of our children
11415 // is also using a drawing cache. Without this, the children would
11416 // steal the canvas by attaching their own bitmap to it and bad, bad
11417 // things would happen (invisible views, corrupted drawings, etc.)
11418 attachInfo.mCanvas = null;
11419 } else {
11420 // This case should hopefully never or seldom happen
11421 canvas = new Canvas(bitmap);
11422 }
11423
Romain Guy5bcdff42009-05-14 21:27:18 -070011424 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011425 bitmap.eraseColor(backgroundColor);
11426 }
11427
11428 computeScroll();
11429 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070011430 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011431 canvas.translate(-mScrollX, -mScrollY);
11432
Romain Guy5bcdff42009-05-14 21:27:18 -070011433 // Temporarily remove the dirty mask
11434 int flags = mPrivateFlags;
11435 mPrivateFlags &= ~DIRTY_MASK;
11436
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011437 // Fast path for layouts with no backgrounds
11438 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
11439 dispatchDraw(canvas);
11440 } else {
11441 draw(canvas);
11442 }
11443
Romain Guy5bcdff42009-05-14 21:27:18 -070011444 mPrivateFlags = flags;
11445
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011446 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070011447 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011448
11449 if (attachInfo != null) {
11450 // Restore the cached Canvas for our siblings
11451 attachInfo.mCanvas = canvas;
11452 }
Romain Guy8506ab42009-06-11 17:35:47 -070011453
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070011454 return bitmap;
11455 }
11456
11457 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011458 * Indicates whether this View is currently in edit mode. A View is usually
11459 * in edit mode when displayed within a developer tool. For instance, if
11460 * this View is being drawn by a visual user interface builder, this method
11461 * should return true.
11462 *
11463 * Subclasses should check the return value of this method to provide
11464 * different behaviors if their normal behavior might interfere with the
11465 * host environment. For instance: the class spawns a thread in its
11466 * constructor, the drawing code relies on device-specific features, etc.
11467 *
11468 * This method is usually checked in the drawing code of custom widgets.
11469 *
11470 * @return True if this View is in edit mode, false otherwise.
11471 */
11472 public boolean isInEditMode() {
11473 return false;
11474 }
11475
11476 /**
11477 * If the View draws content inside its padding and enables fading edges,
11478 * it needs to support padding offsets. Padding offsets are added to the
11479 * fading edges to extend the length of the fade so that it covers pixels
11480 * drawn inside the padding.
11481 *
11482 * Subclasses of this class should override this method if they need
11483 * to draw content inside the padding.
11484 *
11485 * @return True if padding offset must be applied, false otherwise.
11486 *
11487 * @see #getLeftPaddingOffset()
11488 * @see #getRightPaddingOffset()
11489 * @see #getTopPaddingOffset()
11490 * @see #getBottomPaddingOffset()
11491 *
11492 * @since CURRENT
11493 */
11494 protected boolean isPaddingOffsetRequired() {
11495 return false;
11496 }
11497
11498 /**
11499 * Amount by which to extend the left fading region. Called only when
11500 * {@link #isPaddingOffsetRequired()} returns true.
11501 *
11502 * @return The left padding offset in pixels.
11503 *
11504 * @see #isPaddingOffsetRequired()
11505 *
11506 * @since CURRENT
11507 */
11508 protected int getLeftPaddingOffset() {
11509 return 0;
11510 }
11511
11512 /**
11513 * Amount by which to extend the right fading region. Called only when
11514 * {@link #isPaddingOffsetRequired()} returns true.
11515 *
11516 * @return The right padding offset in pixels.
11517 *
11518 * @see #isPaddingOffsetRequired()
11519 *
11520 * @since CURRENT
11521 */
11522 protected int getRightPaddingOffset() {
11523 return 0;
11524 }
11525
11526 /**
11527 * Amount by which to extend the top fading region. Called only when
11528 * {@link #isPaddingOffsetRequired()} returns true.
11529 *
11530 * @return The top padding offset in pixels.
11531 *
11532 * @see #isPaddingOffsetRequired()
11533 *
11534 * @since CURRENT
11535 */
11536 protected int getTopPaddingOffset() {
11537 return 0;
11538 }
11539
11540 /**
11541 * Amount by which to extend the bottom fading region. Called only when
11542 * {@link #isPaddingOffsetRequired()} returns true.
11543 *
11544 * @return The bottom padding offset in pixels.
11545 *
11546 * @see #isPaddingOffsetRequired()
11547 *
11548 * @since CURRENT
11549 */
11550 protected int getBottomPaddingOffset() {
11551 return 0;
11552 }
11553
11554 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070011555 * @hide
11556 * @param offsetRequired
11557 */
11558 protected int getFadeTop(boolean offsetRequired) {
11559 int top = mPaddingTop;
11560 if (offsetRequired) top += getTopPaddingOffset();
11561 return top;
11562 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011563
Romain Guyf2fc4602011-07-19 15:20:03 -070011564 /**
11565 * @hide
11566 * @param offsetRequired
11567 */
11568 protected int getFadeHeight(boolean offsetRequired) {
11569 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070011570 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070011571 return mBottom - mTop - mPaddingBottom - padding;
11572 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011573
Romain Guyf2fc4602011-07-19 15:20:03 -070011574 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090011575 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070011576 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011577 *
Romain Guy2bffd262010-09-12 17:40:02 -070011578 * <p>Even if this method returns true, it does not mean that every call
11579 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
11580 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090011581 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070011582 * window is hardware accelerated,
11583 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
11584 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011585 *
Romain Guy2bffd262010-09-12 17:40:02 -070011586 * @return True if the view is attached to a window and the window is
11587 * hardware accelerated; false in any other case.
11588 */
11589 public boolean isHardwareAccelerated() {
11590 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
11591 }
Joe Malin32736f02011-01-19 16:14:20 -080011592
Romain Guy2bffd262010-09-12 17:40:02 -070011593 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080011594 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
11595 * case of an active Animation being run on the view.
11596 */
11597 private boolean drawAnimation(ViewGroup parent, long drawingTime,
11598 Animation a, boolean scalingRequired) {
11599 Transformation invalidationTransform;
11600 final int flags = parent.mGroupFlags;
11601 final boolean initialized = a.isInitialized();
11602 if (!initialized) {
11603 a.initialize(mRight - mLeft, mBottom - mTop, getWidth(), getHeight());
11604 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
11605 onAnimationStart();
11606 }
11607
11608 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
11609 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
11610 if (parent.mInvalidationTransformation == null) {
11611 parent.mInvalidationTransformation = new Transformation();
11612 }
11613 invalidationTransform = parent.mInvalidationTransformation;
11614 a.getTransformation(drawingTime, invalidationTransform, 1f);
11615 } else {
11616 invalidationTransform = parent.mChildTransformation;
11617 }
11618 if (more) {
11619 if (!a.willChangeBounds()) {
11620 if ((flags & (parent.FLAG_OPTIMIZE_INVALIDATE | parent.FLAG_ANIMATION_DONE)) ==
11621 parent.FLAG_OPTIMIZE_INVALIDATE) {
11622 parent.mGroupFlags |= parent.FLAG_INVALIDATE_REQUIRED;
11623 } else if ((flags & parent.FLAG_INVALIDATE_REQUIRED) == 0) {
11624 // The child need to draw an animation, potentially offscreen, so
11625 // make sure we do not cancel invalidate requests
11626 parent.mPrivateFlags |= DRAW_ANIMATION;
11627 parent.invalidate(mLeft, mTop, mRight, mBottom);
11628 }
11629 } else {
11630 if (parent.mInvalidateRegion == null) {
11631 parent.mInvalidateRegion = new RectF();
11632 }
11633 final RectF region = parent.mInvalidateRegion;
11634 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
11635 invalidationTransform);
11636
11637 // The child need to draw an animation, potentially offscreen, so
11638 // make sure we do not cancel invalidate requests
11639 parent.mPrivateFlags |= DRAW_ANIMATION;
11640
11641 final int left = mLeft + (int) region.left;
11642 final int top = mTop + (int) region.top;
11643 parent.invalidate(left, top, left + (int) (region.width() + .5f),
11644 top + (int) (region.height() + .5f));
11645 }
11646 }
11647 return more;
11648 }
11649
Chet Haasea1cff502012-02-21 13:43:44 -080011650 void setDisplayListProperties() {
11651 setDisplayListProperties(mDisplayList);
11652 }
11653
11654 /**
11655 * This method is called by getDisplayList() when a display list is created or re-rendered.
11656 * It sets or resets the current value of all properties on that display list (resetting is
11657 * necessary when a display list is being re-created, because we need to make sure that
11658 * previously-set transform values
11659 */
11660 void setDisplayListProperties(DisplayList displayList) {
11661 if (USE_DISPLAY_LIST_PROPERTIES && displayList != null) {
11662 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070011663 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080011664 if (mParent instanceof ViewGroup) {
11665 displayList.setClipChildren(
11666 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
11667 }
Chet Haase9420abd2012-03-29 16:28:32 -070011668 float alpha = 1;
11669 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
11670 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
11671 ViewGroup parentVG = (ViewGroup) mParent;
11672 final boolean hasTransform =
11673 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
11674 if (hasTransform) {
11675 Transformation transform = parentVG.mChildTransformation;
11676 final int transformType = parentVG.mChildTransformation.getTransformationType();
11677 if (transformType != Transformation.TYPE_IDENTITY) {
11678 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
11679 alpha = transform.getAlpha();
11680 }
11681 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
11682 displayList.setStaticMatrix(transform.getMatrix());
11683 }
11684 }
11685 }
Chet Haasea1cff502012-02-21 13:43:44 -080011686 }
11687 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070011688 alpha *= mTransformationInfo.mAlpha;
11689 if (alpha < 1) {
11690 final int multipliedAlpha = (int) (255 * alpha);
11691 if (onSetAlpha(multipliedAlpha)) {
11692 alpha = 1;
11693 }
11694 }
11695 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080011696 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
11697 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
11698 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
11699 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070011700 if (mTransformationInfo.mCamera == null) {
11701 mTransformationInfo.mCamera = new Camera();
11702 mTransformationInfo.matrix3D = new Matrix();
11703 }
11704 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Chet Haasea1cff502012-02-21 13:43:44 -080011705 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == PIVOT_EXPLICITLY_SET) {
11706 displayList.setPivotX(getPivotX());
11707 displayList.setPivotY(getPivotY());
11708 }
Chet Haase9420abd2012-03-29 16:28:32 -070011709 } else if (alpha < 1) {
11710 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080011711 }
11712 }
11713 }
11714
Chet Haasebcca79a2012-02-14 08:45:14 -080011715 /**
Chet Haase64a48c12012-02-13 16:33:29 -080011716 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
11717 * This draw() method is an implementation detail and is not intended to be overridden or
11718 * to be called from anywhere else other than ViewGroup.drawChild().
11719 */
11720 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haasea1cff502012-02-21 13:43:44 -080011721 boolean useDisplayListProperties = USE_DISPLAY_LIST_PROPERTIES && mAttachInfo != null &&
11722 mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080011723 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080011724 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080011725 final int flags = parent.mGroupFlags;
11726
Chet Haasea1cff502012-02-21 13:43:44 -080011727 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080011728 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080011729 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080011730 }
11731
11732 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080011733 boolean concatMatrix = false;
11734
11735 boolean scalingRequired = false;
11736 boolean caching;
11737 int layerType = parent.mDrawLayers ? getLayerType() : LAYER_TYPE_NONE;
11738
11739 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080011740 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
11741 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080011742 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070011743 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080011744 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
11745 } else {
11746 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
11747 }
11748
Chet Haasebcca79a2012-02-14 08:45:14 -080011749 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080011750 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011751 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080011752 concatMatrix = a.willChangeTransformationMatrix();
Chet Haasebcca79a2012-02-14 08:45:14 -080011753 transformToApply = parent.mChildTransformation;
Chet Haase9420abd2012-03-29 16:28:32 -070011754 } else if (!useDisplayListProperties &&
11755 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011756 final boolean hasTransform =
11757 parent.getChildStaticTransformation(this, parent.mChildTransformation);
Chet Haase64a48c12012-02-13 16:33:29 -080011758 if (hasTransform) {
11759 final int transformType = parent.mChildTransformation.getTransformationType();
11760 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
11761 parent.mChildTransformation : null;
11762 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
11763 }
11764 }
11765
11766 concatMatrix |= !childHasIdentityMatrix;
11767
11768 // Sets the flag as early as possible to allow draw() implementations
11769 // to call invalidate() successfully when doing animations
11770 mPrivateFlags |= DRAWN;
11771
Chet Haasebcca79a2012-02-14 08:45:14 -080011772 if (!concatMatrix && canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Chet Haase64a48c12012-02-13 16:33:29 -080011773 (mPrivateFlags & DRAW_ANIMATION) == 0) {
11774 return more;
11775 }
11776
11777 if (hardwareAccelerated) {
11778 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
11779 // retain the flag's value temporarily in the mRecreateDisplayList flag
11780 mRecreateDisplayList = (mPrivateFlags & INVALIDATED) == INVALIDATED;
11781 mPrivateFlags &= ~INVALIDATED;
11782 }
11783
11784 computeScroll();
11785
11786 final int sx = mScrollX;
11787 final int sy = mScrollY;
11788
11789 DisplayList displayList = null;
11790 Bitmap cache = null;
11791 boolean hasDisplayList = false;
11792 if (caching) {
11793 if (!hardwareAccelerated) {
11794 if (layerType != LAYER_TYPE_NONE) {
11795 layerType = LAYER_TYPE_SOFTWARE;
11796 buildDrawingCache(true);
11797 }
11798 cache = getDrawingCache(true);
11799 } else {
11800 switch (layerType) {
11801 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070011802 if (useDisplayListProperties) {
11803 hasDisplayList = canHaveDisplayList();
11804 } else {
11805 buildDrawingCache(true);
11806 cache = getDrawingCache(true);
11807 }
Chet Haase64a48c12012-02-13 16:33:29 -080011808 break;
Chet Haasea1cff502012-02-21 13:43:44 -080011809 case LAYER_TYPE_HARDWARE:
11810 if (useDisplayListProperties) {
11811 hasDisplayList = canHaveDisplayList();
11812 }
11813 break;
Chet Haase64a48c12012-02-13 16:33:29 -080011814 case LAYER_TYPE_NONE:
11815 // Delay getting the display list until animation-driven alpha values are
11816 // set up and possibly passed on to the view
11817 hasDisplayList = canHaveDisplayList();
11818 break;
11819 }
11820 }
11821 }
Chet Haasea1cff502012-02-21 13:43:44 -080011822 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070011823 if (useDisplayListProperties) {
11824 displayList = getDisplayList();
11825 if (!displayList.isValid()) {
11826 // Uncommon, but possible. If a view is removed from the hierarchy during the call
11827 // to getDisplayList(), the display list will be marked invalid and we should not
11828 // try to use it again.
11829 displayList = null;
11830 hasDisplayList = false;
11831 useDisplayListProperties = false;
11832 }
11833 }
Chet Haase64a48c12012-02-13 16:33:29 -080011834
11835 final boolean hasNoCache = cache == null || hasDisplayList;
11836 final boolean offsetForScroll = cache == null && !hasDisplayList &&
11837 layerType != LAYER_TYPE_HARDWARE;
11838
Chet Haasea1cff502012-02-21 13:43:44 -080011839 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070011840 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080011841 restoreTo = canvas.save();
11842 }
Chet Haase64a48c12012-02-13 16:33:29 -080011843 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011844 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080011845 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080011846 if (!useDisplayListProperties) {
11847 canvas.translate(mLeft, mTop);
11848 }
Chet Haase64a48c12012-02-13 16:33:29 -080011849 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080011850 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070011851 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080011852 restoreTo = canvas.save();
11853 }
Chet Haase64a48c12012-02-13 16:33:29 -080011854 // mAttachInfo cannot be null, otherwise scalingRequired == false
11855 final float scale = 1.0f / mAttachInfo.mApplicationScale;
11856 canvas.scale(scale, scale);
11857 }
11858 }
11859
Chet Haasea1cff502012-02-21 13:43:44 -080011860 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070011861 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix()) {
Chet Haase64a48c12012-02-13 16:33:29 -080011862 if (transformToApply != null || !childHasIdentityMatrix) {
11863 int transX = 0;
11864 int transY = 0;
11865
11866 if (offsetForScroll) {
11867 transX = -sx;
11868 transY = -sy;
11869 }
11870
11871 if (transformToApply != null) {
11872 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070011873 if (useDisplayListProperties) {
11874 displayList.setAnimationMatrix(transformToApply.getMatrix());
11875 } else {
11876 // Undo the scroll translation, apply the transformation matrix,
11877 // then redo the scroll translate to get the correct result.
11878 canvas.translate(-transX, -transY);
11879 canvas.concat(transformToApply.getMatrix());
11880 canvas.translate(transX, transY);
11881 }
Chet Haasea1cff502012-02-21 13:43:44 -080011882 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080011883 }
11884
11885 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070011886 if (transformAlpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080011887 alpha *= transformToApply.getAlpha();
Chet Haasea1cff502012-02-21 13:43:44 -080011888 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080011889 }
11890 }
11891
Chet Haasea1cff502012-02-21 13:43:44 -080011892 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080011893 canvas.translate(-transX, -transY);
11894 canvas.concat(getMatrix());
11895 canvas.translate(transX, transY);
11896 }
11897 }
11898
Chet Haase9420abd2012-03-29 16:28:32 -070011899 if (alpha < 1) {
Chet Haasea1cff502012-02-21 13:43:44 -080011900 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080011901 if (hasNoCache) {
11902 final int multipliedAlpha = (int) (255 * alpha);
11903 if (!onSetAlpha(multipliedAlpha)) {
11904 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080011905 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080011906 layerType != LAYER_TYPE_NONE) {
11907 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
11908 }
Chet Haase9420abd2012-03-29 16:28:32 -070011909 if (useDisplayListProperties) {
11910 displayList.setAlpha(alpha * getAlpha());
11911 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070011912 final int scrollX = hasDisplayList ? 0 : sx;
11913 final int scrollY = hasDisplayList ? 0 : sy;
11914 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
11915 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080011916 }
11917 } else {
11918 // Alpha is handled by the child directly, clobber the layer's alpha
11919 mPrivateFlags |= ALPHA_SET;
11920 }
11921 }
11922 }
11923 } else if ((mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
11924 onSetAlpha(255);
11925 mPrivateFlags &= ~ALPHA_SET;
11926 }
11927
Chet Haasea1cff502012-02-21 13:43:44 -080011928 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
11929 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080011930 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011931 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080011932 } else {
11933 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011934 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080011935 } else {
11936 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
11937 }
11938 }
11939 }
11940
Chet Haase9420abd2012-03-29 16:28:32 -070011941 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080011942 displayList = getDisplayList();
11943 if (!displayList.isValid()) {
11944 // Uncommon, but possible. If a view is removed from the hierarchy during the call
11945 // to getDisplayList(), the display list will be marked invalid and we should not
11946 // try to use it again.
11947 displayList = null;
11948 hasDisplayList = false;
11949 }
11950 }
11951
11952 if (hasNoCache) {
11953 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080011954 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080011955 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080011956 if (layer != null && layer.isValid()) {
11957 mLayerPaint.setAlpha((int) (alpha * 255));
11958 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
11959 layerRendered = true;
11960 } else {
11961 final int scrollX = hasDisplayList ? 0 : sx;
11962 final int scrollY = hasDisplayList ? 0 : sy;
11963 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080011964 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080011965 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
11966 }
11967 }
11968
11969 if (!layerRendered) {
11970 if (!hasDisplayList) {
11971 // Fast path for layouts with no backgrounds
11972 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
11973 if (ViewDebug.TRACE_HIERARCHY) {
11974 ViewDebug.trace(parent, ViewDebug.HierarchyTraceType.DRAW);
11975 }
11976 mPrivateFlags &= ~DIRTY_MASK;
11977 dispatchDraw(canvas);
11978 } else {
11979 draw(canvas);
11980 }
11981 } else {
11982 mPrivateFlags &= ~DIRTY_MASK;
Chet Haasebcca79a2012-02-14 08:45:14 -080011983 ((HardwareCanvas) canvas).drawDisplayList(displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -080011984 mRight - mLeft, mBottom - mTop, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080011985 }
11986 }
11987 } else if (cache != null) {
11988 mPrivateFlags &= ~DIRTY_MASK;
11989 Paint cachePaint;
11990
11991 if (layerType == LAYER_TYPE_NONE) {
11992 cachePaint = parent.mCachePaint;
11993 if (cachePaint == null) {
11994 cachePaint = new Paint();
11995 cachePaint.setDither(false);
11996 parent.mCachePaint = cachePaint;
11997 }
Chet Haase9420abd2012-03-29 16:28:32 -070011998 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080011999 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080012000 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
12001 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080012002 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080012003 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080012004 }
12005 } else {
12006 cachePaint = mLayerPaint;
12007 cachePaint.setAlpha((int) (alpha * 255));
12008 }
12009 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
12010 }
12011
Chet Haasea1cff502012-02-21 13:43:44 -080012012 if (restoreTo >= 0) {
12013 canvas.restoreToCount(restoreTo);
12014 }
Chet Haase64a48c12012-02-13 16:33:29 -080012015
12016 if (a != null && !more) {
12017 if (!hardwareAccelerated && !a.getFillAfter()) {
12018 onSetAlpha(255);
12019 }
12020 parent.finishAnimatingView(this, a);
12021 }
12022
12023 if (more && hardwareAccelerated) {
12024 // invalidation is the trigger to recreate display lists, so if we're using
12025 // display lists to render, force an invalidate to allow the animation to
12026 // continue drawing another frame
12027 parent.invalidate(true);
12028 if (a.hasAlpha() && (mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
12029 // alpha animations should cause the child to recreate its display list
12030 invalidate(true);
12031 }
12032 }
12033
12034 mRecreateDisplayList = false;
12035
12036 return more;
12037 }
12038
12039 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012040 * Manually render this view (and all of its children) to the given Canvas.
12041 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070012042 * called. When implementing a view, implement
12043 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
12044 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012045 *
12046 * @param canvas The Canvas to which the View is rendered.
12047 */
12048 public void draw(Canvas canvas) {
12049 if (ViewDebug.TRACE_HIERARCHY) {
12050 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
12051 }
12052
Romain Guy5bcdff42009-05-14 21:27:18 -070012053 final int privateFlags = mPrivateFlags;
12054 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
12055 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
12056 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070012057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012058 /*
12059 * Draw traversal performs several drawing steps which must be executed
12060 * in the appropriate order:
12061 *
12062 * 1. Draw the background
12063 * 2. If necessary, save the canvas' layers to prepare for fading
12064 * 3. Draw view's content
12065 * 4. Draw children
12066 * 5. If necessary, draw the fading edges and restore layers
12067 * 6. Draw decorations (scrollbars for instance)
12068 */
12069
12070 // Step 1, draw the background, if needed
12071 int saveCount;
12072
Romain Guy24443ea2009-05-11 11:56:30 -070012073 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070012074 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070012075 if (background != null) {
12076 final int scrollX = mScrollX;
12077 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012078
Romain Guy24443ea2009-05-11 11:56:30 -070012079 if (mBackgroundSizeChanged) {
12080 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
12081 mBackgroundSizeChanged = false;
12082 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012083
Romain Guy24443ea2009-05-11 11:56:30 -070012084 if ((scrollX | scrollY) == 0) {
12085 background.draw(canvas);
12086 } else {
12087 canvas.translate(scrollX, scrollY);
12088 background.draw(canvas);
12089 canvas.translate(-scrollX, -scrollY);
12090 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012091 }
12092 }
12093
12094 // skip step 2 & 5 if possible (common case)
12095 final int viewFlags = mViewFlags;
12096 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
12097 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
12098 if (!verticalEdges && !horizontalEdges) {
12099 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070012100 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012101
12102 // Step 4, draw the children
12103 dispatchDraw(canvas);
12104
12105 // Step 6, draw decorations (scrollbars)
12106 onDrawScrollBars(canvas);
12107
12108 // we're done...
12109 return;
12110 }
12111
12112 /*
12113 * Here we do the full fledged routine...
12114 * (this is an uncommon case where speed matters less,
12115 * this is why we repeat some of the tests that have been
12116 * done above)
12117 */
12118
12119 boolean drawTop = false;
12120 boolean drawBottom = false;
12121 boolean drawLeft = false;
12122 boolean drawRight = false;
12123
12124 float topFadeStrength = 0.0f;
12125 float bottomFadeStrength = 0.0f;
12126 float leftFadeStrength = 0.0f;
12127 float rightFadeStrength = 0.0f;
12128
12129 // Step 2, save the canvas' layers
12130 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012131
12132 final boolean offsetRequired = isPaddingOffsetRequired();
12133 if (offsetRequired) {
12134 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012135 }
12136
12137 int left = mScrollX + paddingLeft;
12138 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070012139 int top = mScrollY + getFadeTop(offsetRequired);
12140 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012141
12142 if (offsetRequired) {
12143 right += getRightPaddingOffset();
12144 bottom += getBottomPaddingOffset();
12145 }
12146
12147 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070012148 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012149 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012150
12151 // clip the fade length if top and bottom fades overlap
12152 // overlapping fades produce odd-looking artifacts
12153 if (verticalEdges && (top + length > bottom - length)) {
12154 length = (bottom - top) / 2;
12155 }
12156
12157 // also clip horizontal fades if necessary
12158 if (horizontalEdges && (left + length > right - length)) {
12159 length = (right - left) / 2;
12160 }
12161
12162 if (verticalEdges) {
12163 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012164 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012165 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012166 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012167 }
12168
12169 if (horizontalEdges) {
12170 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012171 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012172 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012173 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012174 }
12175
12176 saveCount = canvas.getSaveCount();
12177
12178 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070012179 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012180 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
12181
12182 if (drawTop) {
12183 canvas.saveLayer(left, top, right, top + length, null, flags);
12184 }
12185
12186 if (drawBottom) {
12187 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
12188 }
12189
12190 if (drawLeft) {
12191 canvas.saveLayer(left, top, left + length, bottom, null, flags);
12192 }
12193
12194 if (drawRight) {
12195 canvas.saveLayer(right - length, top, right, bottom, null, flags);
12196 }
12197 } else {
12198 scrollabilityCache.setFadeColor(solidColor);
12199 }
12200
12201 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070012202 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012203
12204 // Step 4, draw the children
12205 dispatchDraw(canvas);
12206
12207 // Step 5, draw the fade effect and restore layers
12208 final Paint p = scrollabilityCache.paint;
12209 final Matrix matrix = scrollabilityCache.matrix;
12210 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012211
12212 if (drawTop) {
12213 matrix.setScale(1, fadeHeight * topFadeStrength);
12214 matrix.postTranslate(left, top);
12215 fade.setLocalMatrix(matrix);
12216 canvas.drawRect(left, top, right, top + length, p);
12217 }
12218
12219 if (drawBottom) {
12220 matrix.setScale(1, fadeHeight * bottomFadeStrength);
12221 matrix.postRotate(180);
12222 matrix.postTranslate(left, bottom);
12223 fade.setLocalMatrix(matrix);
12224 canvas.drawRect(left, bottom - length, right, bottom, p);
12225 }
12226
12227 if (drawLeft) {
12228 matrix.setScale(1, fadeHeight * leftFadeStrength);
12229 matrix.postRotate(-90);
12230 matrix.postTranslate(left, top);
12231 fade.setLocalMatrix(matrix);
12232 canvas.drawRect(left, top, left + length, bottom, p);
12233 }
12234
12235 if (drawRight) {
12236 matrix.setScale(1, fadeHeight * rightFadeStrength);
12237 matrix.postRotate(90);
12238 matrix.postTranslate(right, top);
12239 fade.setLocalMatrix(matrix);
12240 canvas.drawRect(right - length, top, right, bottom, p);
12241 }
12242
12243 canvas.restoreToCount(saveCount);
12244
12245 // Step 6, draw decorations (scrollbars)
12246 onDrawScrollBars(canvas);
12247 }
12248
12249 /**
12250 * Override this if your view is known to always be drawn on top of a solid color background,
12251 * and needs to draw fading edges. Returning a non-zero color enables the view system to
12252 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
12253 * should be set to 0xFF.
12254 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012255 * @see #setVerticalFadingEdgeEnabled(boolean)
12256 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012257 *
12258 * @return The known solid color background for this view, or 0 if the color may vary
12259 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012260 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012261 public int getSolidColor() {
12262 return 0;
12263 }
12264
12265 /**
12266 * Build a human readable string representation of the specified view flags.
12267 *
12268 * @param flags the view flags to convert to a string
12269 * @return a String representing the supplied flags
12270 */
12271 private static String printFlags(int flags) {
12272 String output = "";
12273 int numFlags = 0;
12274 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
12275 output += "TAKES_FOCUS";
12276 numFlags++;
12277 }
12278
12279 switch (flags & VISIBILITY_MASK) {
12280 case INVISIBLE:
12281 if (numFlags > 0) {
12282 output += " ";
12283 }
12284 output += "INVISIBLE";
12285 // USELESS HERE numFlags++;
12286 break;
12287 case GONE:
12288 if (numFlags > 0) {
12289 output += " ";
12290 }
12291 output += "GONE";
12292 // USELESS HERE numFlags++;
12293 break;
12294 default:
12295 break;
12296 }
12297 return output;
12298 }
12299
12300 /**
12301 * Build a human readable string representation of the specified private
12302 * view flags.
12303 *
12304 * @param privateFlags the private view flags to convert to a string
12305 * @return a String representing the supplied flags
12306 */
12307 private static String printPrivateFlags(int privateFlags) {
12308 String output = "";
12309 int numFlags = 0;
12310
12311 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
12312 output += "WANTS_FOCUS";
12313 numFlags++;
12314 }
12315
12316 if ((privateFlags & FOCUSED) == FOCUSED) {
12317 if (numFlags > 0) {
12318 output += " ";
12319 }
12320 output += "FOCUSED";
12321 numFlags++;
12322 }
12323
12324 if ((privateFlags & SELECTED) == SELECTED) {
12325 if (numFlags > 0) {
12326 output += " ";
12327 }
12328 output += "SELECTED";
12329 numFlags++;
12330 }
12331
12332 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
12333 if (numFlags > 0) {
12334 output += " ";
12335 }
12336 output += "IS_ROOT_NAMESPACE";
12337 numFlags++;
12338 }
12339
12340 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
12341 if (numFlags > 0) {
12342 output += " ";
12343 }
12344 output += "HAS_BOUNDS";
12345 numFlags++;
12346 }
12347
12348 if ((privateFlags & DRAWN) == DRAWN) {
12349 if (numFlags > 0) {
12350 output += " ";
12351 }
12352 output += "DRAWN";
12353 // USELESS HERE numFlags++;
12354 }
12355 return output;
12356 }
12357
12358 /**
12359 * <p>Indicates whether or not this view's layout will be requested during
12360 * the next hierarchy layout pass.</p>
12361 *
12362 * @return true if the layout will be forced during next layout pass
12363 */
12364 public boolean isLayoutRequested() {
12365 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
12366 }
12367
12368 /**
12369 * Assign a size and position to a view and all of its
12370 * descendants
12371 *
12372 * <p>This is the second phase of the layout mechanism.
12373 * (The first is measuring). In this phase, each parent calls
12374 * layout on all of its children to position them.
12375 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080012376 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012377 *
Chet Haase9c087442011-01-12 16:20:16 -080012378 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012379 * Derived classes with children should override
12380 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080012381 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012382 *
12383 * @param l Left position, relative to parent
12384 * @param t Top position, relative to parent
12385 * @param r Right position, relative to parent
12386 * @param b Bottom position, relative to parent
12387 */
Romain Guy5429e1d2010-09-07 12:38:00 -070012388 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080012389 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070012390 int oldL = mLeft;
12391 int oldT = mTop;
12392 int oldB = mBottom;
12393 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012394 boolean changed = setFrame(l, t, r, b);
12395 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
12396 if (ViewDebug.TRACE_HIERARCHY) {
12397 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
12398 }
12399
12400 onLayout(changed, l, t, r, b);
12401 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070012402
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070012403 ListenerInfo li = mListenerInfo;
12404 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070012405 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070012406 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070012407 int numListeners = listenersCopy.size();
12408 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070012409 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070012410 }
12411 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012412 }
12413 mPrivateFlags &= ~FORCE_LAYOUT;
12414 }
12415
12416 /**
12417 * Called from layout when this view should
12418 * assign a size and position to each of its children.
12419 *
12420 * Derived classes with children should override
12421 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070012422 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012423 * @param changed This is a new size or position for this view
12424 * @param left Left position, relative to parent
12425 * @param top Top position, relative to parent
12426 * @param right Right position, relative to parent
12427 * @param bottom Bottom position, relative to parent
12428 */
12429 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
12430 }
12431
12432 /**
12433 * Assign a size and position to this view.
12434 *
12435 * This is called from layout.
12436 *
12437 * @param left Left position, relative to parent
12438 * @param top Top position, relative to parent
12439 * @param right Right position, relative to parent
12440 * @param bottom Bottom position, relative to parent
12441 * @return true if the new size and position are different than the
12442 * previous ones
12443 * {@hide}
12444 */
12445 protected boolean setFrame(int left, int top, int right, int bottom) {
12446 boolean changed = false;
12447
12448 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070012449 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012450 + right + "," + bottom + ")");
12451 }
12452
12453 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
12454 changed = true;
12455
12456 // Remember our drawn bit
12457 int drawn = mPrivateFlags & DRAWN;
12458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012459 int oldWidth = mRight - mLeft;
12460 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070012461 int newWidth = right - left;
12462 int newHeight = bottom - top;
12463 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
12464
12465 // Invalidate our old position
12466 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012467
12468 mLeft = left;
12469 mTop = top;
12470 mRight = right;
12471 mBottom = bottom;
Chet Haasea1cff502012-02-21 13:43:44 -080012472 if (USE_DISPLAY_LIST_PROPERTIES && mDisplayList != null) {
12473 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
12474 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012475
12476 mPrivateFlags |= HAS_BOUNDS;
12477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012478
Chet Haase75755e22011-07-18 17:48:25 -070012479 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080012480 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
12481 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012482 if (mTransformationInfo != null) {
12483 mTransformationInfo.mMatrixDirty = true;
12484 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080012485 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012486 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
12487 }
12488
12489 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
12490 // If we are visible, force the DRAWN bit to on so that
12491 // this invalidate will go through (at least to our parent).
12492 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080012493 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012494 // the DRAWN bit.
12495 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070012496 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080012497 // parent display list may need to be recreated based on a change in the bounds
12498 // of any child
12499 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012500 }
12501
12502 // Reset drawn bit to original value (invalidate turns it off)
12503 mPrivateFlags |= drawn;
12504
12505 mBackgroundSizeChanged = true;
12506 }
12507 return changed;
12508 }
12509
12510 /**
12511 * Finalize inflating a view from XML. This is called as the last phase
12512 * of inflation, after all child views have been added.
12513 *
12514 * <p>Even if the subclass overrides onFinishInflate, they should always be
12515 * sure to call the super method, so that we get called.
12516 */
12517 protected void onFinishInflate() {
12518 }
12519
12520 /**
12521 * Returns the resources associated with this view.
12522 *
12523 * @return Resources object.
12524 */
12525 public Resources getResources() {
12526 return mResources;
12527 }
12528
12529 /**
12530 * Invalidates the specified Drawable.
12531 *
12532 * @param drawable the drawable to invalidate
12533 */
12534 public void invalidateDrawable(Drawable drawable) {
12535 if (verifyDrawable(drawable)) {
12536 final Rect dirty = drawable.getBounds();
12537 final int scrollX = mScrollX;
12538 final int scrollY = mScrollY;
12539
12540 invalidate(dirty.left + scrollX, dirty.top + scrollY,
12541 dirty.right + scrollX, dirty.bottom + scrollY);
12542 }
12543 }
12544
12545 /**
12546 * Schedules an action on a drawable to occur at a specified time.
12547 *
12548 * @param who the recipient of the action
12549 * @param what the action to run on the drawable
12550 * @param when the time at which the action must occur. Uses the
12551 * {@link SystemClock#uptimeMillis} timebase.
12552 */
12553 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080012554 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080012555 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080012556 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070012557 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
12558 Choreographer.CALLBACK_ANIMATION, what, who,
12559 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080012560 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080012561 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080012562 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012563 }
12564 }
12565
12566 /**
12567 * Cancels a scheduled action on a drawable.
12568 *
12569 * @param who the recipient of the action
12570 * @param what the action to cancel
12571 */
12572 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080012573 if (verifyDrawable(who) && what != null) {
12574 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070012575 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
12576 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080012577 } else {
12578 ViewRootImpl.getRunQueue().removeCallbacks(what);
12579 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012580 }
12581 }
12582
12583 /**
12584 * Unschedule any events associated with the given Drawable. This can be
12585 * used when selecting a new Drawable into a view, so that the previous
12586 * one is completely unscheduled.
12587 *
12588 * @param who The Drawable to unschedule.
12589 *
12590 * @see #drawableStateChanged
12591 */
12592 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080012593 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070012594 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
12595 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012596 }
12597 }
12598
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070012599 /**
12600 * Return the layout direction of a given Drawable.
12601 *
12602 * @param who the Drawable to query
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070012603 */
12604 public int getResolvedLayoutDirection(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070012605 return (who == mBackground) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070012606 }
12607
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012608 /**
12609 * If your view subclass is displaying its own Drawable objects, it should
12610 * override this function and return true for any Drawable it is
12611 * displaying. This allows animations for those drawables to be
12612 * scheduled.
12613 *
12614 * <p>Be sure to call through to the super class when overriding this
12615 * function.
12616 *
12617 * @param who The Drawable to verify. Return true if it is one you are
12618 * displaying, else return the result of calling through to the
12619 * super class.
12620 *
12621 * @return boolean If true than the Drawable is being displayed in the
12622 * view; else false and it is not allowed to animate.
12623 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012624 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
12625 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012626 */
12627 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070012628 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012629 }
12630
12631 /**
12632 * This function is called whenever the state of the view changes in such
12633 * a way that it impacts the state of drawables being shown.
12634 *
12635 * <p>Be sure to call through to the superclass when overriding this
12636 * function.
12637 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012638 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012639 */
12640 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070012641 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012642 if (d != null && d.isStateful()) {
12643 d.setState(getDrawableState());
12644 }
12645 }
12646
12647 /**
12648 * Call this to force a view to update its drawable state. This will cause
12649 * drawableStateChanged to be called on this view. Views that are interested
12650 * in the new state should call getDrawableState.
12651 *
12652 * @see #drawableStateChanged
12653 * @see #getDrawableState
12654 */
12655 public void refreshDrawableState() {
12656 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
12657 drawableStateChanged();
12658
12659 ViewParent parent = mParent;
12660 if (parent != null) {
12661 parent.childDrawableStateChanged(this);
12662 }
12663 }
12664
12665 /**
12666 * Return an array of resource IDs of the drawable states representing the
12667 * current state of the view.
12668 *
12669 * @return The current drawable state
12670 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012671 * @see Drawable#setState(int[])
12672 * @see #drawableStateChanged()
12673 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012674 */
12675 public final int[] getDrawableState() {
12676 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
12677 return mDrawableState;
12678 } else {
12679 mDrawableState = onCreateDrawableState(0);
12680 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
12681 return mDrawableState;
12682 }
12683 }
12684
12685 /**
12686 * Generate the new {@link android.graphics.drawable.Drawable} state for
12687 * this view. This is called by the view
12688 * system when the cached Drawable state is determined to be invalid. To
12689 * retrieve the current state, you should use {@link #getDrawableState}.
12690 *
12691 * @param extraSpace if non-zero, this is the number of extra entries you
12692 * would like in the returned array in which you can place your own
12693 * states.
12694 *
12695 * @return Returns an array holding the current {@link Drawable} state of
12696 * the view.
12697 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012698 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012699 */
12700 protected int[] onCreateDrawableState(int extraSpace) {
12701 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
12702 mParent instanceof View) {
12703 return ((View) mParent).onCreateDrawableState(extraSpace);
12704 }
12705
12706 int[] drawableState;
12707
12708 int privateFlags = mPrivateFlags;
12709
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012710 int viewStateIndex = 0;
12711 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
12712 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
12713 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070012714 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012715 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
12716 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070012717 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
12718 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080012719 // This is set if HW acceleration is requested, even if the current
12720 // process doesn't allow it. This is just to allow app preview
12721 // windows to better match their app.
12722 viewStateIndex |= VIEW_STATE_ACCELERATED;
12723 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070012724 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012725
Christopher Tate3d4bf172011-03-28 16:16:46 -070012726 final int privateFlags2 = mPrivateFlags2;
12727 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
12728 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
12729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012730 drawableState = VIEW_STATE_SETS[viewStateIndex];
12731
12732 //noinspection ConstantIfStatement
12733 if (false) {
12734 Log.i("View", "drawableStateIndex=" + viewStateIndex);
12735 Log.i("View", toString()
12736 + " pressed=" + ((privateFlags & PRESSED) != 0)
12737 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
12738 + " fo=" + hasFocus()
12739 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012740 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012741 + ": " + Arrays.toString(drawableState));
12742 }
12743
12744 if (extraSpace == 0) {
12745 return drawableState;
12746 }
12747
12748 final int[] fullState;
12749 if (drawableState != null) {
12750 fullState = new int[drawableState.length + extraSpace];
12751 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
12752 } else {
12753 fullState = new int[extraSpace];
12754 }
12755
12756 return fullState;
12757 }
12758
12759 /**
12760 * Merge your own state values in <var>additionalState</var> into the base
12761 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070012762 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012763 *
12764 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070012765 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012766 * own additional state values.
12767 *
12768 * @param additionalState The additional state values you would like
12769 * added to <var>baseState</var>; this array is not modified.
12770 *
12771 * @return As a convenience, the <var>baseState</var> array you originally
12772 * passed into the function is returned.
12773 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012774 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012775 */
12776 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
12777 final int N = baseState.length;
12778 int i = N - 1;
12779 while (i >= 0 && baseState[i] == 0) {
12780 i--;
12781 }
12782 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
12783 return baseState;
12784 }
12785
12786 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070012787 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
12788 * on all Drawable objects associated with this view.
12789 */
12790 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070012791 if (mBackground != null) {
12792 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070012793 }
12794 }
12795
12796 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012797 * Sets the background color for this view.
12798 * @param color the color of the background
12799 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000012800 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012801 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070012802 if (mBackground instanceof ColorDrawable) {
12803 ((ColorDrawable) mBackground).setColor(color);
Chet Haase70d4ba12010-10-06 09:46:45 -070012804 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070012805 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070012806 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012807 }
12808
12809 /**
12810 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070012811 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012812 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070012813 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012814 * @attr ref android.R.styleable#View_background
12815 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000012816 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012817 public void setBackgroundResource(int resid) {
12818 if (resid != 0 && resid == mBackgroundResource) {
12819 return;
12820 }
12821
12822 Drawable d= null;
12823 if (resid != 0) {
12824 d = mResources.getDrawable(resid);
12825 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012826 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012827
12828 mBackgroundResource = resid;
12829 }
12830
12831 /**
12832 * Set the background to a given Drawable, or remove the background. If the
12833 * background has padding, this View's padding is set to the background's
12834 * padding. However, when a background is removed, this View's padding isn't
12835 * touched. If setting the padding is desired, please use
12836 * {@link #setPadding(int, int, int, int)}.
12837 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012838 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012839 * background
12840 */
Philip Milne6c8ea062012-04-03 17:38:43 -070012841 public void setBackground(Drawable background) {
12842 setBackgroundDrawable(background);
12843 }
12844
12845 /**
12846 * @deprecated use {@link #setBackground(Drawable)} instead
12847 */
12848 @Deprecated
12849 public void setBackgroundDrawable(Drawable background) {
12850 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070012851 return;
12852 }
12853
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012854 boolean requestLayout = false;
12855
12856 mBackgroundResource = 0;
12857
12858 /*
12859 * Regardless of whether we're setting a new background or not, we want
12860 * to clear the previous drawable.
12861 */
Philip Milne6c8ea062012-04-03 17:38:43 -070012862 if (mBackground != null) {
12863 mBackground.setCallback(null);
12864 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012865 }
12866
Philip Milne6c8ea062012-04-03 17:38:43 -070012867 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012868 Rect padding = sThreadLocal.get();
12869 if (padding == null) {
12870 padding = new Rect();
12871 sThreadLocal.set(padding);
12872 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012873 if (background.getPadding(padding)) {
12874 switch (background.getResolvedLayoutDirectionSelf()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012875 case LAYOUT_DIRECTION_RTL:
12876 setPadding(padding.right, padding.top, padding.left, padding.bottom);
12877 break;
12878 case LAYOUT_DIRECTION_LTR:
12879 default:
12880 setPadding(padding.left, padding.top, padding.right, padding.bottom);
12881 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012882 }
12883
12884 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
12885 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070012886 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
12887 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012888 requestLayout = true;
12889 }
12890
Philip Milne6c8ea062012-04-03 17:38:43 -070012891 background.setCallback(this);
12892 if (background.isStateful()) {
12893 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012894 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012895 background.setVisible(getVisibility() == VISIBLE, false);
12896 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012897
12898 if ((mPrivateFlags & SKIP_DRAW) != 0) {
12899 mPrivateFlags &= ~SKIP_DRAW;
12900 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
12901 requestLayout = true;
12902 }
12903 } else {
12904 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070012905 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012906
12907 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
12908 /*
12909 * This view ONLY drew the background before and we're removing
12910 * the background, so now it won't draw anything
12911 * (hence we SKIP_DRAW)
12912 */
12913 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
12914 mPrivateFlags |= SKIP_DRAW;
12915 }
12916
12917 /*
12918 * When the background is set, we try to apply its padding to this
12919 * View. When the background is removed, we don't touch this View's
12920 * padding. This is noted in the Javadocs. Hence, we don't need to
12921 * requestLayout(), the invalidate() below is sufficient.
12922 */
12923
12924 // The old background's minimum size could have affected this
12925 // View's layout, so let's requestLayout
12926 requestLayout = true;
12927 }
12928
Romain Guy8f1344f52009-05-15 16:03:59 -070012929 computeOpaqueFlags();
12930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012931 if (requestLayout) {
12932 requestLayout();
12933 }
12934
12935 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012936 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012937 }
12938
12939 /**
12940 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070012941 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012942 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070012943 *
12944 * @see #setBackground(Drawable)
12945 *
12946 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012947 */
12948 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070012949 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012950 }
12951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012952 /**
12953 * Sets the padding. The view may add on the space required to display
12954 * the scrollbars, depending on the style and visibility of the scrollbars.
12955 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
12956 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
12957 * from the values set in this call.
12958 *
12959 * @attr ref android.R.styleable#View_padding
12960 * @attr ref android.R.styleable#View_paddingBottom
12961 * @attr ref android.R.styleable#View_paddingLeft
12962 * @attr ref android.R.styleable#View_paddingRight
12963 * @attr ref android.R.styleable#View_paddingTop
12964 * @param left the left padding in pixels
12965 * @param top the top padding in pixels
12966 * @param right the right padding in pixels
12967 * @param bottom the bottom padding in pixels
12968 */
12969 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080012970 mUserPaddingStart = -1;
12971 mUserPaddingEnd = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012972 mUserPaddingRelative = false;
12973
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080012974 internalSetPadding(left, top, right, bottom);
12975 }
12976
12977 private void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080012978 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012979 mUserPaddingRight = right;
12980 mUserPaddingBottom = bottom;
12981
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012982 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080012983 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070012984
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012985 // Common case is there are no scroll bars.
12986 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012987 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080012988 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012989 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080012990 switch (mVerticalScrollbarPosition) {
12991 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012992 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
12993 left += offset;
12994 } else {
12995 right += offset;
12996 }
12997 break;
Adam Powell20232d02010-12-08 21:08:53 -080012998 case SCROLLBAR_POSITION_RIGHT:
12999 right += offset;
13000 break;
13001 case SCROLLBAR_POSITION_LEFT:
13002 left += offset;
13003 break;
13004 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013005 }
Adam Powell20232d02010-12-08 21:08:53 -080013006 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013007 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
13008 ? 0 : getHorizontalScrollbarHeight();
13009 }
13010 }
Romain Guy8506ab42009-06-11 17:35:47 -070013011
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013012 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013013 changed = true;
13014 mPaddingLeft = left;
13015 }
13016 if (mPaddingTop != top) {
13017 changed = true;
13018 mPaddingTop = top;
13019 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013020 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013021 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013022 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013023 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013024 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013025 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013026 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013027 }
13028
13029 if (changed) {
13030 requestLayout();
13031 }
13032 }
13033
13034 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013035 * Sets the relative padding. The view may add on the space required to display
13036 * the scrollbars, depending on the style and visibility of the scrollbars.
13037 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
13038 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
13039 * from the values set in this call.
13040 *
13041 * @attr ref android.R.styleable#View_padding
13042 * @attr ref android.R.styleable#View_paddingBottom
13043 * @attr ref android.R.styleable#View_paddingStart
13044 * @attr ref android.R.styleable#View_paddingEnd
13045 * @attr ref android.R.styleable#View_paddingTop
13046 * @param start the start padding in pixels
13047 * @param top the top padding in pixels
13048 * @param end the end padding in pixels
13049 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013050 */
13051 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070013052 mUserPaddingStart = start;
13053 mUserPaddingEnd = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013054 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070013055
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013056 switch(getResolvedLayoutDirection()) {
13057 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013058 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013059 break;
13060 case LAYOUT_DIRECTION_LTR:
13061 default:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013062 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013063 }
13064 }
13065
13066 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013067 * Returns the top padding of this view.
13068 *
13069 * @return the top padding in pixels
13070 */
13071 public int getPaddingTop() {
13072 return mPaddingTop;
13073 }
13074
13075 /**
13076 * Returns the bottom padding of this view. If there are inset and enabled
13077 * scrollbars, this value may include the space required to display the
13078 * scrollbars as well.
13079 *
13080 * @return the bottom padding in pixels
13081 */
13082 public int getPaddingBottom() {
13083 return mPaddingBottom;
13084 }
13085
13086 /**
13087 * Returns the left padding of this view. If there are inset and enabled
13088 * scrollbars, this value may include the space required to display the
13089 * scrollbars as well.
13090 *
13091 * @return the left padding in pixels
13092 */
13093 public int getPaddingLeft() {
13094 return mPaddingLeft;
13095 }
13096
13097 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070013098 * Returns the start padding of this view depending on its resolved layout direction.
13099 * If there are inset and enabled scrollbars, this value may include the space
13100 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013101 *
13102 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013103 */
13104 public int getPaddingStart() {
13105 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
13106 mPaddingRight : mPaddingLeft;
13107 }
13108
13109 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013110 * Returns the right padding of this view. If there are inset and enabled
13111 * scrollbars, this value may include the space required to display the
13112 * scrollbars as well.
13113 *
13114 * @return the right padding in pixels
13115 */
13116 public int getPaddingRight() {
13117 return mPaddingRight;
13118 }
13119
13120 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070013121 * Returns the end padding of this view depending on its resolved layout direction.
13122 * If there are inset and enabled scrollbars, this value may include the space
13123 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013124 *
13125 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013126 */
13127 public int getPaddingEnd() {
13128 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
13129 mPaddingLeft : mPaddingRight;
13130 }
13131
13132 /**
13133 * Return if the padding as been set thru relative values
13134 * {@link #setPaddingRelative(int, int, int, int)} or thru
13135 * @attr ref android.R.styleable#View_paddingStart or
13136 * @attr ref android.R.styleable#View_paddingEnd
13137 *
13138 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013139 */
13140 public boolean isPaddingRelative() {
13141 return mUserPaddingRelative;
13142 }
13143
13144 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013145 * Changes the selection state of this view. A view can be selected or not.
13146 * Note that selection is not the same as focus. Views are typically
13147 * selected in the context of an AdapterView like ListView or GridView;
13148 * the selected view is the view that is highlighted.
13149 *
13150 * @param selected true if the view must be selected, false otherwise
13151 */
13152 public void setSelected(boolean selected) {
13153 if (((mPrivateFlags & SELECTED) != 0) != selected) {
13154 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070013155 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080013156 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013157 refreshDrawableState();
13158 dispatchSetSelected(selected);
13159 }
13160 }
13161
13162 /**
13163 * Dispatch setSelected to all of this View's children.
13164 *
13165 * @see #setSelected(boolean)
13166 *
13167 * @param selected The new selected state
13168 */
13169 protected void dispatchSetSelected(boolean selected) {
13170 }
13171
13172 /**
13173 * Indicates the selection state of this view.
13174 *
13175 * @return true if the view is selected, false otherwise
13176 */
13177 @ViewDebug.ExportedProperty
13178 public boolean isSelected() {
13179 return (mPrivateFlags & SELECTED) != 0;
13180 }
13181
13182 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013183 * Changes the activated state of this view. A view can be activated or not.
13184 * Note that activation is not the same as selection. Selection is
13185 * a transient property, representing the view (hierarchy) the user is
13186 * currently interacting with. Activation is a longer-term state that the
13187 * user can move views in and out of. For example, in a list view with
13188 * single or multiple selection enabled, the views in the current selection
13189 * set are activated. (Um, yeah, we are deeply sorry about the terminology
13190 * here.) The activated state is propagated down to children of the view it
13191 * is set on.
13192 *
13193 * @param activated true if the view must be activated, false otherwise
13194 */
13195 public void setActivated(boolean activated) {
13196 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
13197 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080013198 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013199 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070013200 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013201 }
13202 }
13203
13204 /**
13205 * Dispatch setActivated to all of this View's children.
13206 *
13207 * @see #setActivated(boolean)
13208 *
13209 * @param activated The new activated state
13210 */
13211 protected void dispatchSetActivated(boolean activated) {
13212 }
13213
13214 /**
13215 * Indicates the activation state of this view.
13216 *
13217 * @return true if the view is activated, false otherwise
13218 */
13219 @ViewDebug.ExportedProperty
13220 public boolean isActivated() {
13221 return (mPrivateFlags & ACTIVATED) != 0;
13222 }
13223
13224 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013225 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
13226 * observer can be used to get notifications when global events, like
13227 * layout, happen.
13228 *
13229 * The returned ViewTreeObserver observer is not guaranteed to remain
13230 * valid for the lifetime of this View. If the caller of this method keeps
13231 * a long-lived reference to ViewTreeObserver, it should always check for
13232 * the return value of {@link ViewTreeObserver#isAlive()}.
13233 *
13234 * @return The ViewTreeObserver for this view's hierarchy.
13235 */
13236 public ViewTreeObserver getViewTreeObserver() {
13237 if (mAttachInfo != null) {
13238 return mAttachInfo.mTreeObserver;
13239 }
13240 if (mFloatingTreeObserver == null) {
13241 mFloatingTreeObserver = new ViewTreeObserver();
13242 }
13243 return mFloatingTreeObserver;
13244 }
13245
13246 /**
13247 * <p>Finds the topmost view in the current view hierarchy.</p>
13248 *
13249 * @return the topmost view containing this view
13250 */
13251 public View getRootView() {
13252 if (mAttachInfo != null) {
13253 final View v = mAttachInfo.mRootView;
13254 if (v != null) {
13255 return v;
13256 }
13257 }
Romain Guy8506ab42009-06-11 17:35:47 -070013258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013259 View parent = this;
13260
13261 while (parent.mParent != null && parent.mParent instanceof View) {
13262 parent = (View) parent.mParent;
13263 }
13264
13265 return parent;
13266 }
13267
13268 /**
13269 * <p>Computes the coordinates of this view on the screen. The argument
13270 * must be an array of two integers. After the method returns, the array
13271 * contains the x and y location in that order.</p>
13272 *
13273 * @param location an array of two integers in which to hold the coordinates
13274 */
13275 public void getLocationOnScreen(int[] location) {
13276 getLocationInWindow(location);
13277
13278 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070013279 if (info != null) {
13280 location[0] += info.mWindowLeft;
13281 location[1] += info.mWindowTop;
13282 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013283 }
13284
13285 /**
13286 * <p>Computes the coordinates of this view in its window. The argument
13287 * must be an array of two integers. After the method returns, the array
13288 * contains the x and y location in that order.</p>
13289 *
13290 * @param location an array of two integers in which to hold the coordinates
13291 */
13292 public void getLocationInWindow(int[] location) {
13293 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010013294 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013295 }
13296
Gilles Debunne6583ce52011-12-06 18:09:02 -080013297 if (mAttachInfo == null) {
13298 // When the view is not attached to a window, this method does not make sense
13299 location[0] = location[1] = 0;
13300 return;
13301 }
13302
Gilles Debunnecea45132011-11-24 02:19:27 +010013303 float[] position = mAttachInfo.mTmpTransformLocation;
13304 position[0] = position[1] = 0.0f;
13305
13306 if (!hasIdentityMatrix()) {
13307 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013308 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013309
Gilles Debunnecea45132011-11-24 02:19:27 +010013310 position[0] += mLeft;
13311 position[1] += mTop;
13312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013313 ViewParent viewParent = mParent;
13314 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010013315 final View view = (View) viewParent;
13316
13317 position[0] -= view.mScrollX;
13318 position[1] -= view.mScrollY;
13319
13320 if (!view.hasIdentityMatrix()) {
13321 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013322 }
Gilles Debunnecea45132011-11-24 02:19:27 +010013323
13324 position[0] += view.mLeft;
13325 position[1] += view.mTop;
13326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013327 viewParent = view.mParent;
13328 }
Romain Guy8506ab42009-06-11 17:35:47 -070013329
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070013330 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013331 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010013332 final ViewRootImpl vr = (ViewRootImpl) viewParent;
13333 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013334 }
Gilles Debunnecea45132011-11-24 02:19:27 +010013335
13336 location[0] = (int) (position[0] + 0.5f);
13337 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013338 }
13339
13340 /**
13341 * {@hide}
13342 * @param id the id of the view to be found
13343 * @return the view of the specified id, null if cannot be found
13344 */
13345 protected View findViewTraversal(int id) {
13346 if (id == mID) {
13347 return this;
13348 }
13349 return null;
13350 }
13351
13352 /**
13353 * {@hide}
13354 * @param tag the tag of the view to be found
13355 * @return the view of specified tag, null if cannot be found
13356 */
13357 protected View findViewWithTagTraversal(Object tag) {
13358 if (tag != null && tag.equals(mTag)) {
13359 return this;
13360 }
13361 return null;
13362 }
13363
13364 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080013365 * {@hide}
13366 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070013367 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080013368 * @return The first view that matches the predicate or null.
13369 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070013370 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080013371 if (predicate.apply(this)) {
13372 return this;
13373 }
13374 return null;
13375 }
13376
13377 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013378 * Look for a child view with the given id. If this view has the given
13379 * id, return this view.
13380 *
13381 * @param id The id to search for.
13382 * @return The view that has the given id in the hierarchy or null
13383 */
13384 public final View findViewById(int id) {
13385 if (id < 0) {
13386 return null;
13387 }
13388 return findViewTraversal(id);
13389 }
13390
13391 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070013392 * Finds a view by its unuque and stable accessibility id.
13393 *
13394 * @param accessibilityId The searched accessibility id.
13395 * @return The found view.
13396 */
13397 final View findViewByAccessibilityId(int accessibilityId) {
13398 if (accessibilityId < 0) {
13399 return null;
13400 }
13401 return findViewByAccessibilityIdTraversal(accessibilityId);
13402 }
13403
13404 /**
13405 * Performs the traversal to find a view by its unuque and stable accessibility id.
13406 *
13407 * <strong>Note:</strong>This method does not stop at the root namespace
13408 * boundary since the user can touch the screen at an arbitrary location
13409 * potentially crossing the root namespace bounday which will send an
13410 * accessibility event to accessibility services and they should be able
13411 * to obtain the event source. Also accessibility ids are guaranteed to be
13412 * unique in the window.
13413 *
13414 * @param accessibilityId The accessibility id.
13415 * @return The found view.
13416 */
13417 View findViewByAccessibilityIdTraversal(int accessibilityId) {
13418 if (getAccessibilityViewId() == accessibilityId) {
13419 return this;
13420 }
13421 return null;
13422 }
13423
13424 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013425 * Look for a child view with the given tag. If this view has the given
13426 * tag, return this view.
13427 *
13428 * @param tag The tag to search for, using "tag.equals(getTag())".
13429 * @return The View that has the given tag in the hierarchy or null
13430 */
13431 public final View findViewWithTag(Object tag) {
13432 if (tag == null) {
13433 return null;
13434 }
13435 return findViewWithTagTraversal(tag);
13436 }
13437
13438 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080013439 * {@hide}
13440 * Look for a child view that matches the specified predicate.
13441 * If this view matches the predicate, return this view.
13442 *
13443 * @param predicate The predicate to evaluate.
13444 * @return The first view that matches the predicate or null.
13445 */
13446 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070013447 return findViewByPredicateTraversal(predicate, null);
13448 }
13449
13450 /**
13451 * {@hide}
13452 * Look for a child view that matches the specified predicate,
13453 * starting with the specified view and its descendents and then
13454 * recusively searching the ancestors and siblings of that view
13455 * until this view is reached.
13456 *
13457 * This method is useful in cases where the predicate does not match
13458 * a single unique view (perhaps multiple views use the same id)
13459 * and we are trying to find the view that is "closest" in scope to the
13460 * starting view.
13461 *
13462 * @param start The view to start from.
13463 * @param predicate The predicate to evaluate.
13464 * @return The first view that matches the predicate or null.
13465 */
13466 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
13467 View childToSkip = null;
13468 for (;;) {
13469 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
13470 if (view != null || start == this) {
13471 return view;
13472 }
13473
13474 ViewParent parent = start.getParent();
13475 if (parent == null || !(parent instanceof View)) {
13476 return null;
13477 }
13478
13479 childToSkip = start;
13480 start = (View) parent;
13481 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080013482 }
13483
13484 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013485 * Sets the identifier for this view. The identifier does not have to be
13486 * unique in this view's hierarchy. The identifier should be a positive
13487 * number.
13488 *
13489 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070013490 * @see #getId()
13491 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013492 *
13493 * @param id a number used to identify the view
13494 *
13495 * @attr ref android.R.styleable#View_id
13496 */
13497 public void setId(int id) {
13498 mID = id;
13499 }
13500
13501 /**
13502 * {@hide}
13503 *
13504 * @param isRoot true if the view belongs to the root namespace, false
13505 * otherwise
13506 */
13507 public void setIsRootNamespace(boolean isRoot) {
13508 if (isRoot) {
13509 mPrivateFlags |= IS_ROOT_NAMESPACE;
13510 } else {
13511 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
13512 }
13513 }
13514
13515 /**
13516 * {@hide}
13517 *
13518 * @return true if the view belongs to the root namespace, false otherwise
13519 */
13520 public boolean isRootNamespace() {
13521 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
13522 }
13523
13524 /**
13525 * Returns this view's identifier.
13526 *
13527 * @return a positive integer used to identify the view or {@link #NO_ID}
13528 * if the view has no ID
13529 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013530 * @see #setId(int)
13531 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013532 * @attr ref android.R.styleable#View_id
13533 */
13534 @ViewDebug.CapturedViewProperty
13535 public int getId() {
13536 return mID;
13537 }
13538
13539 /**
13540 * Returns this view's tag.
13541 *
13542 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070013543 *
13544 * @see #setTag(Object)
13545 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013546 */
13547 @ViewDebug.ExportedProperty
13548 public Object getTag() {
13549 return mTag;
13550 }
13551
13552 /**
13553 * Sets the tag associated with this view. A tag can be used to mark
13554 * a view in its hierarchy and does not have to be unique within the
13555 * hierarchy. Tags can also be used to store data within a view without
13556 * resorting to another data structure.
13557 *
13558 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070013559 *
13560 * @see #getTag()
13561 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013562 */
13563 public void setTag(final Object tag) {
13564 mTag = tag;
13565 }
13566
13567 /**
Romain Guyd90a3312009-05-06 14:54:28 -070013568 * Returns the tag associated with this view and the specified key.
13569 *
13570 * @param key The key identifying the tag
13571 *
13572 * @return the Object stored in this view as a tag
13573 *
13574 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070013575 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070013576 */
13577 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070013578 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070013579 return null;
13580 }
13581
13582 /**
13583 * Sets a tag associated with this view and a key. A tag can be used
13584 * to mark a view in its hierarchy and does not have to be unique within
13585 * the hierarchy. Tags can also be used to store data within a view
13586 * without resorting to another data structure.
13587 *
13588 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070013589 * application to ensure it is unique (see the <a
13590 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
13591 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070013592 * the Android framework or not associated with any package will cause
13593 * an {@link IllegalArgumentException} to be thrown.
13594 *
13595 * @param key The key identifying the tag
13596 * @param tag An Object to tag the view with
13597 *
13598 * @throws IllegalArgumentException If they specified key is not valid
13599 *
13600 * @see #setTag(Object)
13601 * @see #getTag(int)
13602 */
13603 public void setTag(int key, final Object tag) {
13604 // If the package id is 0x00 or 0x01, it's either an undefined package
13605 // or a framework id
13606 if ((key >>> 24) < 2) {
13607 throw new IllegalArgumentException("The key must be an application-specific "
13608 + "resource id.");
13609 }
13610
Adam Powell2b2f6d62011-09-23 11:15:39 -070013611 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070013612 }
13613
13614 /**
13615 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
13616 * framework id.
13617 *
13618 * @hide
13619 */
13620 public void setTagInternal(int key, Object tag) {
13621 if ((key >>> 24) != 0x1) {
13622 throw new IllegalArgumentException("The key must be a framework-specific "
13623 + "resource id.");
13624 }
13625
Adam Powell2b2f6d62011-09-23 11:15:39 -070013626 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070013627 }
13628
Adam Powell2b2f6d62011-09-23 11:15:39 -070013629 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070013630 if (mKeyedTags == null) {
13631 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070013632 }
13633
Adam Powell7db82ac2011-09-22 19:44:04 -070013634 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070013635 }
13636
13637 /**
Romain Guy13922e02009-05-12 17:56:14 -070013638 * @param consistency The type of consistency. See ViewDebug for more information.
13639 *
13640 * @hide
13641 */
13642 protected boolean dispatchConsistencyCheck(int consistency) {
13643 return onConsistencyCheck(consistency);
13644 }
13645
13646 /**
13647 * Method that subclasses should implement to check their consistency. The type of
13648 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070013649 *
Romain Guy13922e02009-05-12 17:56:14 -070013650 * @param consistency The type of consistency. See ViewDebug for more information.
13651 *
13652 * @throws IllegalStateException if the view is in an inconsistent state.
13653 *
13654 * @hide
13655 */
13656 protected boolean onConsistencyCheck(int consistency) {
13657 boolean result = true;
13658
13659 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
13660 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
13661
13662 if (checkLayout) {
13663 if (getParent() == null) {
13664 result = false;
13665 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
13666 "View " + this + " does not have a parent.");
13667 }
13668
13669 if (mAttachInfo == null) {
13670 result = false;
13671 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
13672 "View " + this + " is not attached to a window.");
13673 }
13674 }
13675
13676 if (checkDrawing) {
13677 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
13678 // from their draw() method
13679
13680 if ((mPrivateFlags & DRAWN) != DRAWN &&
13681 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
13682 result = false;
13683 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
13684 "View " + this + " was invalidated but its drawing cache is valid.");
13685 }
13686 }
13687
13688 return result;
13689 }
13690
13691 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013692 * Prints information about this view in the log output, with the tag
13693 * {@link #VIEW_LOG_TAG}.
13694 *
13695 * @hide
13696 */
13697 public void debug() {
13698 debug(0);
13699 }
13700
13701 /**
13702 * Prints information about this view in the log output, with the tag
13703 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
13704 * indentation defined by the <code>depth</code>.
13705 *
13706 * @param depth the indentation level
13707 *
13708 * @hide
13709 */
13710 protected void debug(int depth) {
13711 String output = debugIndent(depth - 1);
13712
13713 output += "+ " + this;
13714 int id = getId();
13715 if (id != -1) {
13716 output += " (id=" + id + ")";
13717 }
13718 Object tag = getTag();
13719 if (tag != null) {
13720 output += " (tag=" + tag + ")";
13721 }
13722 Log.d(VIEW_LOG_TAG, output);
13723
13724 if ((mPrivateFlags & FOCUSED) != 0) {
13725 output = debugIndent(depth) + " FOCUSED";
13726 Log.d(VIEW_LOG_TAG, output);
13727 }
13728
13729 output = debugIndent(depth);
13730 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
13731 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
13732 + "} ";
13733 Log.d(VIEW_LOG_TAG, output);
13734
13735 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
13736 || mPaddingBottom != 0) {
13737 output = debugIndent(depth);
13738 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
13739 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
13740 Log.d(VIEW_LOG_TAG, output);
13741 }
13742
13743 output = debugIndent(depth);
13744 output += "mMeasureWidth=" + mMeasuredWidth +
13745 " mMeasureHeight=" + mMeasuredHeight;
13746 Log.d(VIEW_LOG_TAG, output);
13747
13748 output = debugIndent(depth);
13749 if (mLayoutParams == null) {
13750 output += "BAD! no layout params";
13751 } else {
13752 output = mLayoutParams.debug(output);
13753 }
13754 Log.d(VIEW_LOG_TAG, output);
13755
13756 output = debugIndent(depth);
13757 output += "flags={";
13758 output += View.printFlags(mViewFlags);
13759 output += "}";
13760 Log.d(VIEW_LOG_TAG, output);
13761
13762 output = debugIndent(depth);
13763 output += "privateFlags={";
13764 output += View.printPrivateFlags(mPrivateFlags);
13765 output += "}";
13766 Log.d(VIEW_LOG_TAG, output);
13767 }
13768
13769 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090013770 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013771 *
13772 * @param depth the indentation level
13773 * @return a String containing (depth * 2 + 3) * 2 white spaces
13774 *
13775 * @hide
13776 */
13777 protected static String debugIndent(int depth) {
13778 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
13779 for (int i = 0; i < (depth * 2) + 3; i++) {
13780 spaces.append(' ').append(' ');
13781 }
13782 return spaces.toString();
13783 }
13784
13785 /**
13786 * <p>Return the offset of the widget's text baseline from the widget's top
13787 * boundary. If this widget does not support baseline alignment, this
13788 * method returns -1. </p>
13789 *
13790 * @return the offset of the baseline within the widget's bounds or -1
13791 * if baseline alignment is not supported
13792 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070013793 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013794 public int getBaseline() {
13795 return -1;
13796 }
13797
13798 /**
13799 * Call this when something has changed which has invalidated the
13800 * layout of this view. This will schedule a layout pass of the view
13801 * tree.
13802 */
13803 public void requestLayout() {
13804 if (ViewDebug.TRACE_HIERARCHY) {
13805 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
13806 }
13807
13808 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080013809 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013810
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070013811 if (mLayoutParams != null) {
13812 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
13813 }
13814
13815 if (mParent != null && !mParent.isLayoutRequested()) {
13816 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013817 }
13818 }
13819
13820 /**
13821 * Forces this view to be laid out during the next layout pass.
13822 * This method does not call requestLayout() or forceLayout()
13823 * on the parent.
13824 */
13825 public void forceLayout() {
13826 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080013827 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013828 }
13829
13830 /**
13831 * <p>
13832 * This is called to find out how big a view should be. The parent
13833 * supplies constraint information in the width and height parameters.
13834 * </p>
13835 *
13836 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080013837 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013838 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080013839 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013840 * </p>
13841 *
13842 *
13843 * @param widthMeasureSpec Horizontal space requirements as imposed by the
13844 * parent
13845 * @param heightMeasureSpec Vertical space requirements as imposed by the
13846 * parent
13847 *
13848 * @see #onMeasure(int, int)
13849 */
13850 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
13851 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
13852 widthMeasureSpec != mOldWidthMeasureSpec ||
13853 heightMeasureSpec != mOldHeightMeasureSpec) {
13854
13855 // first clears the measured dimension flag
13856 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
13857
13858 if (ViewDebug.TRACE_HIERARCHY) {
13859 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
13860 }
13861
13862 // measure ourselves, this should set the measured dimension flag back
13863 onMeasure(widthMeasureSpec, heightMeasureSpec);
13864
13865 // flag not set, setMeasuredDimension() was not invoked, we raise
13866 // an exception to warn the developer
13867 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
13868 throw new IllegalStateException("onMeasure() did not set the"
13869 + " measured dimension by calling"
13870 + " setMeasuredDimension()");
13871 }
13872
13873 mPrivateFlags |= LAYOUT_REQUIRED;
13874 }
13875
13876 mOldWidthMeasureSpec = widthMeasureSpec;
13877 mOldHeightMeasureSpec = heightMeasureSpec;
13878 }
13879
13880 /**
13881 * <p>
13882 * Measure the view and its content to determine the measured width and the
13883 * measured height. This method is invoked by {@link #measure(int, int)} and
13884 * should be overriden by subclasses to provide accurate and efficient
13885 * measurement of their contents.
13886 * </p>
13887 *
13888 * <p>
13889 * <strong>CONTRACT:</strong> When overriding this method, you
13890 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
13891 * measured width and height of this view. Failure to do so will trigger an
13892 * <code>IllegalStateException</code>, thrown by
13893 * {@link #measure(int, int)}. Calling the superclass'
13894 * {@link #onMeasure(int, int)} is a valid use.
13895 * </p>
13896 *
13897 * <p>
13898 * The base class implementation of measure defaults to the background size,
13899 * unless a larger size is allowed by the MeasureSpec. Subclasses should
13900 * override {@link #onMeasure(int, int)} to provide better measurements of
13901 * their content.
13902 * </p>
13903 *
13904 * <p>
13905 * If this method is overridden, it is the subclass's responsibility to make
13906 * sure the measured height and width are at least the view's minimum height
13907 * and width ({@link #getSuggestedMinimumHeight()} and
13908 * {@link #getSuggestedMinimumWidth()}).
13909 * </p>
13910 *
13911 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
13912 * The requirements are encoded with
13913 * {@link android.view.View.MeasureSpec}.
13914 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
13915 * The requirements are encoded with
13916 * {@link android.view.View.MeasureSpec}.
13917 *
13918 * @see #getMeasuredWidth()
13919 * @see #getMeasuredHeight()
13920 * @see #setMeasuredDimension(int, int)
13921 * @see #getSuggestedMinimumHeight()
13922 * @see #getSuggestedMinimumWidth()
13923 * @see android.view.View.MeasureSpec#getMode(int)
13924 * @see android.view.View.MeasureSpec#getSize(int)
13925 */
13926 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
13927 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
13928 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
13929 }
13930
13931 /**
13932 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
13933 * measured width and measured height. Failing to do so will trigger an
13934 * exception at measurement time.</p>
13935 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080013936 * @param measuredWidth The measured width of this view. May be a complex
13937 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
13938 * {@link #MEASURED_STATE_TOO_SMALL}.
13939 * @param measuredHeight The measured height of this view. May be a complex
13940 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
13941 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013942 */
13943 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
13944 mMeasuredWidth = measuredWidth;
13945 mMeasuredHeight = measuredHeight;
13946
13947 mPrivateFlags |= MEASURED_DIMENSION_SET;
13948 }
13949
13950 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080013951 * Merge two states as returned by {@link #getMeasuredState()}.
13952 * @param curState The current state as returned from a view or the result
13953 * of combining multiple views.
13954 * @param newState The new view state to combine.
13955 * @return Returns a new integer reflecting the combination of the two
13956 * states.
13957 */
13958 public static int combineMeasuredStates(int curState, int newState) {
13959 return curState | newState;
13960 }
13961
13962 /**
13963 * Version of {@link #resolveSizeAndState(int, int, int)}
13964 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
13965 */
13966 public static int resolveSize(int size, int measureSpec) {
13967 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
13968 }
13969
13970 /**
13971 * Utility to reconcile a desired size and state, with constraints imposed
13972 * by a MeasureSpec. Will take the desired size, unless a different size
13973 * is imposed by the constraints. The returned value is a compound integer,
13974 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
13975 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
13976 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013977 *
13978 * @param size How big the view wants to be
13979 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080013980 * @return Size information bit mask as defined by
13981 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013982 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080013983 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013984 int result = size;
13985 int specMode = MeasureSpec.getMode(measureSpec);
13986 int specSize = MeasureSpec.getSize(measureSpec);
13987 switch (specMode) {
13988 case MeasureSpec.UNSPECIFIED:
13989 result = size;
13990 break;
13991 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080013992 if (specSize < size) {
13993 result = specSize | MEASURED_STATE_TOO_SMALL;
13994 } else {
13995 result = size;
13996 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013997 break;
13998 case MeasureSpec.EXACTLY:
13999 result = specSize;
14000 break;
14001 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080014002 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014003 }
14004
14005 /**
14006 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070014007 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014008 * by the MeasureSpec.
14009 *
14010 * @param size Default size for this view
14011 * @param measureSpec Constraints imposed by the parent
14012 * @return The size this view should be.
14013 */
14014 public static int getDefaultSize(int size, int measureSpec) {
14015 int result = size;
14016 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070014017 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014018
14019 switch (specMode) {
14020 case MeasureSpec.UNSPECIFIED:
14021 result = size;
14022 break;
14023 case MeasureSpec.AT_MOST:
14024 case MeasureSpec.EXACTLY:
14025 result = specSize;
14026 break;
14027 }
14028 return result;
14029 }
14030
14031 /**
14032 * Returns the suggested minimum height that the view should use. This
14033 * returns the maximum of the view's minimum height
14034 * and the background's minimum height
14035 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
14036 * <p>
14037 * When being used in {@link #onMeasure(int, int)}, the caller should still
14038 * ensure the returned height is within the requirements of the parent.
14039 *
14040 * @return The suggested minimum height of the view.
14041 */
14042 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014043 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014045 }
14046
14047 /**
14048 * Returns the suggested minimum width that the view should use. This
14049 * returns the maximum of the view's minimum width)
14050 * and the background's minimum width
14051 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
14052 * <p>
14053 * When being used in {@link #onMeasure(int, int)}, the caller should still
14054 * ensure the returned width is within the requirements of the parent.
14055 *
14056 * @return The suggested minimum width of the view.
14057 */
14058 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014059 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
14060 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014061
Philip Milne6c8ea062012-04-03 17:38:43 -070014062 /**
14063 * Returns the minimum height of the view.
14064 *
14065 * @return the minimum height the view will try to be.
14066 *
14067 * @see #setMinimumHeight(int)
14068 *
14069 * @attr ref android.R.styleable#View_minHeight
14070 */
14071 public int getMinimumHeight() {
14072 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014073 }
14074
14075 /**
14076 * Sets the minimum height of the view. It is not guaranteed the view will
14077 * be able to achieve this minimum height (for example, if its parent layout
14078 * constrains it with less available height).
14079 *
14080 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070014081 *
14082 * @see #getMinimumHeight()
14083 *
14084 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014085 */
14086 public void setMinimumHeight(int minHeight) {
14087 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070014088 requestLayout();
14089 }
14090
14091 /**
14092 * Returns the minimum width of the view.
14093 *
14094 * @return the minimum width the view will try to be.
14095 *
14096 * @see #setMinimumWidth(int)
14097 *
14098 * @attr ref android.R.styleable#View_minWidth
14099 */
14100 public int getMinimumWidth() {
14101 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014102 }
14103
14104 /**
14105 * Sets the minimum width of the view. It is not guaranteed the view will
14106 * be able to achieve this minimum width (for example, if its parent layout
14107 * constrains it with less available width).
14108 *
14109 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070014110 *
14111 * @see #getMinimumWidth()
14112 *
14113 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014114 */
14115 public void setMinimumWidth(int minWidth) {
14116 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070014117 requestLayout();
14118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014119 }
14120
14121 /**
14122 * Get the animation currently associated with this view.
14123 *
14124 * @return The animation that is currently playing or
14125 * scheduled to play for this view.
14126 */
14127 public Animation getAnimation() {
14128 return mCurrentAnimation;
14129 }
14130
14131 /**
14132 * Start the specified animation now.
14133 *
14134 * @param animation the animation to start now
14135 */
14136 public void startAnimation(Animation animation) {
14137 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
14138 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014139 invalidateParentCaches();
14140 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014141 }
14142
14143 /**
14144 * Cancels any animations for this view.
14145 */
14146 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080014147 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080014148 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080014149 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014150 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014151 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014152 }
14153
14154 /**
14155 * Sets the next animation to play for this view.
14156 * If you want the animation to play immediately, use
14157 * startAnimation. This method provides allows fine-grained
14158 * control over the start time and invalidation, but you
14159 * must make sure that 1) the animation has a start time set, and
14160 * 2) the view will be invalidated when the animation is supposed to
14161 * start.
14162 *
14163 * @param animation The next animation, or null.
14164 */
14165 public void setAnimation(Animation animation) {
14166 mCurrentAnimation = animation;
14167 if (animation != null) {
14168 animation.reset();
14169 }
14170 }
14171
14172 /**
14173 * Invoked by a parent ViewGroup to notify the start of the animation
14174 * currently associated with this view. If you override this method,
14175 * always call super.onAnimationStart();
14176 *
14177 * @see #setAnimation(android.view.animation.Animation)
14178 * @see #getAnimation()
14179 */
14180 protected void onAnimationStart() {
14181 mPrivateFlags |= ANIMATION_STARTED;
14182 }
14183
14184 /**
14185 * Invoked by a parent ViewGroup to notify the end of the animation
14186 * currently associated with this view. If you override this method,
14187 * always call super.onAnimationEnd();
14188 *
14189 * @see #setAnimation(android.view.animation.Animation)
14190 * @see #getAnimation()
14191 */
14192 protected void onAnimationEnd() {
14193 mPrivateFlags &= ~ANIMATION_STARTED;
14194 }
14195
14196 /**
14197 * Invoked if there is a Transform that involves alpha. Subclass that can
14198 * draw themselves with the specified alpha should return true, and then
14199 * respect that alpha when their onDraw() is called. If this returns false
14200 * then the view may be redirected to draw into an offscreen buffer to
14201 * fulfill the request, which will look fine, but may be slower than if the
14202 * subclass handles it internally. The default implementation returns false.
14203 *
14204 * @param alpha The alpha (0..255) to apply to the view's drawing
14205 * @return true if the view can draw with the specified alpha.
14206 */
14207 protected boolean onSetAlpha(int alpha) {
14208 return false;
14209 }
14210
14211 /**
14212 * This is used by the RootView to perform an optimization when
14213 * the view hierarchy contains one or several SurfaceView.
14214 * SurfaceView is always considered transparent, but its children are not,
14215 * therefore all View objects remove themselves from the global transparent
14216 * region (passed as a parameter to this function).
14217 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014218 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014219 *
14220 * @return Returns true if the effective visibility of the view at this
14221 * point is opaque, regardless of the transparent region; returns false
14222 * if it is possible for underlying windows to be seen behind the view.
14223 *
14224 * {@hide}
14225 */
14226 public boolean gatherTransparentRegion(Region region) {
14227 final AttachInfo attachInfo = mAttachInfo;
14228 if (region != null && attachInfo != null) {
14229 final int pflags = mPrivateFlags;
14230 if ((pflags & SKIP_DRAW) == 0) {
14231 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
14232 // remove it from the transparent region.
14233 final int[] location = attachInfo.mTransparentLocation;
14234 getLocationInWindow(location);
14235 region.op(location[0], location[1], location[0] + mRight - mLeft,
14236 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Philip Milne6c8ea062012-04-03 17:38:43 -070014237 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014238 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
14239 // exists, so we remove the background drawable's non-transparent
14240 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070014241 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014242 }
14243 }
14244 return true;
14245 }
14246
14247 /**
14248 * Play a sound effect for this view.
14249 *
14250 * <p>The framework will play sound effects for some built in actions, such as
14251 * clicking, but you may wish to play these effects in your widget,
14252 * for instance, for internal navigation.
14253 *
14254 * <p>The sound effect will only be played if sound effects are enabled by the user, and
14255 * {@link #isSoundEffectsEnabled()} is true.
14256 *
14257 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
14258 */
14259 public void playSoundEffect(int soundConstant) {
14260 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
14261 return;
14262 }
14263 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
14264 }
14265
14266 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070014267 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070014268 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070014269 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014270 *
14271 * <p>The framework will provide haptic feedback for some built in actions,
14272 * such as long presses, but you may wish to provide feedback for your
14273 * own widget.
14274 *
14275 * <p>The feedback will only be performed if
14276 * {@link #isHapticFeedbackEnabled()} is true.
14277 *
14278 * @param feedbackConstant One of the constants defined in
14279 * {@link HapticFeedbackConstants}
14280 */
14281 public boolean performHapticFeedback(int feedbackConstant) {
14282 return performHapticFeedback(feedbackConstant, 0);
14283 }
14284
14285 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070014286 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070014287 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070014288 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014289 *
14290 * @param feedbackConstant One of the constants defined in
14291 * {@link HapticFeedbackConstants}
14292 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
14293 */
14294 public boolean performHapticFeedback(int feedbackConstant, int flags) {
14295 if (mAttachInfo == null) {
14296 return false;
14297 }
Romain Guyf607bdc2010-09-10 19:20:06 -070014298 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070014299 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014300 && !isHapticFeedbackEnabled()) {
14301 return false;
14302 }
Romain Guy812ccbe2010-06-01 14:07:24 -070014303 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
14304 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014305 }
14306
14307 /**
Joe Onorato664644d2011-01-23 17:53:23 -080014308 * Request that the visibility of the status bar be changed.
Daniel Sandler60ee2562011-07-22 12:34:33 -040014309 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
14310 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080014311 */
14312 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040014313 if (visibility != mSystemUiVisibility) {
14314 mSystemUiVisibility = visibility;
14315 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
14316 mParent.recomputeViewAttributes(this);
14317 }
Joe Onorato664644d2011-01-23 17:53:23 -080014318 }
14319 }
14320
14321 /**
14322 * Returns the status bar visibility that this view has requested.
Daniel Sandler60ee2562011-07-22 12:34:33 -040014323 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
14324 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080014325 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080014326 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080014327 return mSystemUiVisibility;
14328 }
14329
Scott Mainec6331b2011-05-24 16:55:56 -070014330 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070014331 * Returns the current system UI visibility that is currently set for
14332 * the entire window. This is the combination of the
14333 * {@link #setSystemUiVisibility(int)} values supplied by all of the
14334 * views in the window.
14335 */
14336 public int getWindowSystemUiVisibility() {
14337 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
14338 }
14339
14340 /**
14341 * Override to find out when the window's requested system UI visibility
14342 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
14343 * This is different from the callbacks recieved through
14344 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
14345 * in that this is only telling you about the local request of the window,
14346 * not the actual values applied by the system.
14347 */
14348 public void onWindowSystemUiVisibilityChanged(int visible) {
14349 }
14350
14351 /**
14352 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
14353 * the view hierarchy.
14354 */
14355 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
14356 onWindowSystemUiVisibilityChanged(visible);
14357 }
14358
14359 /**
Scott Mainec6331b2011-05-24 16:55:56 -070014360 * Set a listener to receive callbacks when the visibility of the system bar changes.
14361 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
14362 */
Joe Onorato664644d2011-01-23 17:53:23 -080014363 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070014364 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080014365 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
14366 mParent.recomputeViewAttributes(this);
14367 }
14368 }
14369
14370 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014371 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
14372 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080014373 */
14374 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070014375 ListenerInfo li = mListenerInfo;
14376 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
14377 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080014378 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080014379 }
14380 }
14381
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014382 void updateLocalSystemUiVisibility(int localValue, int localChanges) {
14383 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
14384 if (val != mSystemUiVisibility) {
14385 setSystemUiVisibility(val);
14386 }
14387 }
14388
Joe Onorato664644d2011-01-23 17:53:23 -080014389 /**
Joe Malin32736f02011-01-19 16:14:20 -080014390 * Creates an image that the system displays during the drag and drop
14391 * operation. This is called a &quot;drag shadow&quot;. The default implementation
14392 * for a DragShadowBuilder based on a View returns an image that has exactly the same
14393 * appearance as the given View. The default also positions the center of the drag shadow
14394 * directly under the touch point. If no View is provided (the constructor with no parameters
14395 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
14396 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
14397 * default is an invisible drag shadow.
14398 * <p>
14399 * You are not required to use the View you provide to the constructor as the basis of the
14400 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
14401 * anything you want as the drag shadow.
14402 * </p>
14403 * <p>
14404 * You pass a DragShadowBuilder object to the system when you start the drag. The system
14405 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
14406 * size and position of the drag shadow. It uses this data to construct a
14407 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
14408 * so that your application can draw the shadow image in the Canvas.
14409 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070014410 *
14411 * <div class="special reference">
14412 * <h3>Developer Guides</h3>
14413 * <p>For a guide to implementing drag and drop features, read the
14414 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
14415 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070014416 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014417 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070014418 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070014419
14420 /**
Joe Malin32736f02011-01-19 16:14:20 -080014421 * Constructs a shadow image builder based on a View. By default, the resulting drag
14422 * shadow will have the same appearance and dimensions as the View, with the touch point
14423 * over the center of the View.
14424 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070014425 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014426 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070014427 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070014428 }
14429
Christopher Tate17ed60c2011-01-18 12:50:26 -080014430 /**
14431 * Construct a shadow builder object with no associated View. This
14432 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
14433 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
14434 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080014435 * reference to any View object. If they are not overridden, then the result is an
14436 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080014437 */
14438 public DragShadowBuilder() {
14439 mView = new WeakReference<View>(null);
14440 }
14441
14442 /**
14443 * Returns the View object that had been passed to the
14444 * {@link #View.DragShadowBuilder(View)}
14445 * constructor. If that View parameter was {@code null} or if the
14446 * {@link #View.DragShadowBuilder()}
14447 * constructor was used to instantiate the builder object, this method will return
14448 * null.
14449 *
14450 * @return The View object associate with this builder object.
14451 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070014452 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070014453 final public View getView() {
14454 return mView.get();
14455 }
14456
Christopher Tate2c095f32010-10-04 14:13:40 -070014457 /**
Joe Malin32736f02011-01-19 16:14:20 -080014458 * Provides the metrics for the shadow image. These include the dimensions of
14459 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070014460 * be centered under the touch location while dragging.
14461 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014462 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080014463 * same as the dimensions of the View itself and centers the shadow under
14464 * the touch point.
14465 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070014466 *
Joe Malin32736f02011-01-19 16:14:20 -080014467 * @param shadowSize A {@link android.graphics.Point} containing the width and height
14468 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
14469 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
14470 * image.
14471 *
14472 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
14473 * shadow image that should be underneath the touch point during the drag and drop
14474 * operation. Your application must set {@link android.graphics.Point#x} to the
14475 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070014476 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014477 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070014478 final View view = mView.get();
14479 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014480 shadowSize.set(view.getWidth(), view.getHeight());
14481 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070014482 } else {
14483 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
14484 }
Christopher Tate2c095f32010-10-04 14:13:40 -070014485 }
14486
14487 /**
Joe Malin32736f02011-01-19 16:14:20 -080014488 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
14489 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014490 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070014491 *
Joe Malin32736f02011-01-19 16:14:20 -080014492 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070014493 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014494 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070014495 final View view = mView.get();
14496 if (view != null) {
14497 view.draw(canvas);
14498 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014499 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070014500 }
Christopher Tate2c095f32010-10-04 14:13:40 -070014501 }
14502 }
14503
14504 /**
Joe Malin32736f02011-01-19 16:14:20 -080014505 * Starts a drag and drop operation. When your application calls this method, it passes a
14506 * {@link android.view.View.DragShadowBuilder} object to the system. The
14507 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
14508 * to get metrics for the drag shadow, and then calls the object's
14509 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
14510 * <p>
14511 * Once the system has the drag shadow, it begins the drag and drop operation by sending
14512 * drag events to all the View objects in your application that are currently visible. It does
14513 * this either by calling the View object's drag listener (an implementation of
14514 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
14515 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
14516 * Both are passed a {@link android.view.DragEvent} object that has a
14517 * {@link android.view.DragEvent#getAction()} value of
14518 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
14519 * </p>
14520 * <p>
14521 * Your application can invoke startDrag() on any attached View object. The View object does not
14522 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
14523 * be related to the View the user selected for dragging.
14524 * </p>
14525 * @param data A {@link android.content.ClipData} object pointing to the data to be
14526 * transferred by the drag and drop operation.
14527 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
14528 * drag shadow.
14529 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
14530 * drop operation. This Object is put into every DragEvent object sent by the system during the
14531 * current drag.
14532 * <p>
14533 * myLocalState is a lightweight mechanism for the sending information from the dragged View
14534 * to the target Views. For example, it can contain flags that differentiate between a
14535 * a copy operation and a move operation.
14536 * </p>
14537 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
14538 * so the parameter should be set to 0.
14539 * @return {@code true} if the method completes successfully, or
14540 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
14541 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070014542 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014543 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080014544 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070014545 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080014546 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070014547 }
14548 boolean okay = false;
14549
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014550 Point shadowSize = new Point();
14551 Point shadowTouchPoint = new Point();
14552 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070014553
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014554 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
14555 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
14556 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070014557 }
Christopher Tatea53146c2010-09-07 11:57:52 -070014558
Chris Tatea32dcf72010-10-14 12:13:50 -070014559 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014560 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
14561 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070014562 }
Christopher Tatea53146c2010-09-07 11:57:52 -070014563 Surface surface = new Surface();
14564 try {
14565 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080014566 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070014567 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070014568 + " surface=" + surface);
14569 if (token != null) {
14570 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070014571 try {
Chris Tate6b391282010-10-14 15:48:59 -070014572 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014573 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070014574 } finally {
14575 surface.unlockCanvasAndPost(canvas);
14576 }
Christopher Tatea53146c2010-09-07 11:57:52 -070014577
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014578 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080014579
14580 // Cache the local state object for delivery with DragEvents
14581 root.setLocalDragState(myLocalState);
14582
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014583 // repurpose 'shadowSize' for the last touch point
14584 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070014585
Christopher Tatea53146c2010-09-07 11:57:52 -070014586 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080014587 shadowSize.x, shadowSize.y,
14588 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070014589 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070014590
14591 // Off and running! Release our local surface instance; the drag
14592 // shadow surface is now managed by the system process.
14593 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070014594 }
14595 } catch (Exception e) {
14596 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
14597 surface.destroy();
14598 }
14599
14600 return okay;
14601 }
14602
Christopher Tatea53146c2010-09-07 11:57:52 -070014603 /**
Joe Malin32736f02011-01-19 16:14:20 -080014604 * Handles drag events sent by the system following a call to
14605 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
14606 *<p>
14607 * When the system calls this method, it passes a
14608 * {@link android.view.DragEvent} object. A call to
14609 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
14610 * in DragEvent. The method uses these to determine what is happening in the drag and drop
14611 * operation.
14612 * @param event The {@link android.view.DragEvent} sent by the system.
14613 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
14614 * in DragEvent, indicating the type of drag event represented by this object.
14615 * @return {@code true} if the method was successful, otherwise {@code false}.
14616 * <p>
14617 * The method should return {@code true} in response to an action type of
14618 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
14619 * operation.
14620 * </p>
14621 * <p>
14622 * The method should also return {@code true} in response to an action type of
14623 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
14624 * {@code false} if it didn't.
14625 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070014626 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070014627 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070014628 return false;
14629 }
14630
14631 /**
Joe Malin32736f02011-01-19 16:14:20 -080014632 * Detects if this View is enabled and has a drag event listener.
14633 * If both are true, then it calls the drag event listener with the
14634 * {@link android.view.DragEvent} it received. If the drag event listener returns
14635 * {@code true}, then dispatchDragEvent() returns {@code true}.
14636 * <p>
14637 * For all other cases, the method calls the
14638 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
14639 * method and returns its result.
14640 * </p>
14641 * <p>
14642 * This ensures that a drag event is always consumed, even if the View does not have a drag
14643 * event listener. However, if the View has a listener and the listener returns true, then
14644 * onDragEvent() is not called.
14645 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070014646 */
14647 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080014648 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070014649 ListenerInfo li = mListenerInfo;
14650 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
14651 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070014652 return true;
14653 }
Christopher Tatea53146c2010-09-07 11:57:52 -070014654 return onDragEvent(event);
14655 }
14656
Christopher Tate3d4bf172011-03-28 16:16:46 -070014657 boolean canAcceptDrag() {
14658 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
14659 }
14660
Christopher Tatea53146c2010-09-07 11:57:52 -070014661 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070014662 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
14663 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070014664 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070014665 */
14666 public void onCloseSystemDialogs(String reason) {
14667 }
Joe Malin32736f02011-01-19 16:14:20 -080014668
Dianne Hackbornffa42482009-09-23 22:20:11 -070014669 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014670 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070014671 * update a Region being computed for
14672 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014673 * that any non-transparent parts of the Drawable are removed from the
14674 * given transparent region.
14675 *
14676 * @param dr The Drawable whose transparency is to be applied to the region.
14677 * @param region A Region holding the current transparency information,
14678 * where any parts of the region that are set are considered to be
14679 * transparent. On return, this region will be modified to have the
14680 * transparency information reduced by the corresponding parts of the
14681 * Drawable that are not transparent.
14682 * {@hide}
14683 */
14684 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
14685 if (DBG) {
14686 Log.i("View", "Getting transparent region for: " + this);
14687 }
14688 final Region r = dr.getTransparentRegion();
14689 final Rect db = dr.getBounds();
14690 final AttachInfo attachInfo = mAttachInfo;
14691 if (r != null && attachInfo != null) {
14692 final int w = getRight()-getLeft();
14693 final int h = getBottom()-getTop();
14694 if (db.left > 0) {
14695 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
14696 r.op(0, 0, db.left, h, Region.Op.UNION);
14697 }
14698 if (db.right < w) {
14699 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
14700 r.op(db.right, 0, w, h, Region.Op.UNION);
14701 }
14702 if (db.top > 0) {
14703 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
14704 r.op(0, 0, w, db.top, Region.Op.UNION);
14705 }
14706 if (db.bottom < h) {
14707 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
14708 r.op(0, db.bottom, w, h, Region.Op.UNION);
14709 }
14710 final int[] location = attachInfo.mTransparentLocation;
14711 getLocationInWindow(location);
14712 r.translate(location[0], location[1]);
14713 region.op(r, Region.Op.INTERSECT);
14714 } else {
14715 region.op(db, Region.Op.DIFFERENCE);
14716 }
14717 }
14718
Patrick Dubroye0a799a2011-05-04 16:19:22 -070014719 private void checkForLongClick(int delayOffset) {
14720 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
14721 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014722
Patrick Dubroye0a799a2011-05-04 16:19:22 -070014723 if (mPendingCheckForLongPress == null) {
14724 mPendingCheckForLongPress = new CheckForLongPress();
14725 }
14726 mPendingCheckForLongPress.rememberWindowAttachCount();
14727 postDelayed(mPendingCheckForLongPress,
14728 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014729 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014730 }
14731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014732 /**
14733 * Inflate a view from an XML resource. This convenience method wraps the {@link
14734 * LayoutInflater} class, which provides a full range of options for view inflation.
14735 *
14736 * @param context The Context object for your activity or application.
14737 * @param resource The resource ID to inflate
14738 * @param root A view group that will be the parent. Used to properly inflate the
14739 * layout_* parameters.
14740 * @see LayoutInflater
14741 */
14742 public static View inflate(Context context, int resource, ViewGroup root) {
14743 LayoutInflater factory = LayoutInflater.from(context);
14744 return factory.inflate(resource, root);
14745 }
Romain Guy33e72ae2010-07-17 12:40:29 -070014746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014747 /**
Adam Powell637d3372010-08-25 14:37:03 -070014748 * Scroll the view with standard behavior for scrolling beyond the normal
14749 * content boundaries. Views that call this method should override
14750 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
14751 * results of an over-scroll operation.
14752 *
14753 * Views can use this method to handle any touch or fling-based scrolling.
14754 *
14755 * @param deltaX Change in X in pixels
14756 * @param deltaY Change in Y in pixels
14757 * @param scrollX Current X scroll value in pixels before applying deltaX
14758 * @param scrollY Current Y scroll value in pixels before applying deltaY
14759 * @param scrollRangeX Maximum content scroll range along the X axis
14760 * @param scrollRangeY Maximum content scroll range along the Y axis
14761 * @param maxOverScrollX Number of pixels to overscroll by in either direction
14762 * along the X axis.
14763 * @param maxOverScrollY Number of pixels to overscroll by in either direction
14764 * along the Y axis.
14765 * @param isTouchEvent true if this scroll operation is the result of a touch event.
14766 * @return true if scrolling was clamped to an over-scroll boundary along either
14767 * axis, false otherwise.
14768 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070014769 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070014770 protected boolean overScrollBy(int deltaX, int deltaY,
14771 int scrollX, int scrollY,
14772 int scrollRangeX, int scrollRangeY,
14773 int maxOverScrollX, int maxOverScrollY,
14774 boolean isTouchEvent) {
14775 final int overScrollMode = mOverScrollMode;
14776 final boolean canScrollHorizontal =
14777 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
14778 final boolean canScrollVertical =
14779 computeVerticalScrollRange() > computeVerticalScrollExtent();
14780 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
14781 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
14782 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
14783 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
14784
14785 int newScrollX = scrollX + deltaX;
14786 if (!overScrollHorizontal) {
14787 maxOverScrollX = 0;
14788 }
14789
14790 int newScrollY = scrollY + deltaY;
14791 if (!overScrollVertical) {
14792 maxOverScrollY = 0;
14793 }
14794
14795 // Clamp values if at the limits and record
14796 final int left = -maxOverScrollX;
14797 final int right = maxOverScrollX + scrollRangeX;
14798 final int top = -maxOverScrollY;
14799 final int bottom = maxOverScrollY + scrollRangeY;
14800
14801 boolean clampedX = false;
14802 if (newScrollX > right) {
14803 newScrollX = right;
14804 clampedX = true;
14805 } else if (newScrollX < left) {
14806 newScrollX = left;
14807 clampedX = true;
14808 }
14809
14810 boolean clampedY = false;
14811 if (newScrollY > bottom) {
14812 newScrollY = bottom;
14813 clampedY = true;
14814 } else if (newScrollY < top) {
14815 newScrollY = top;
14816 clampedY = true;
14817 }
14818
14819 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
14820
14821 return clampedX || clampedY;
14822 }
14823
14824 /**
14825 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
14826 * respond to the results of an over-scroll operation.
14827 *
14828 * @param scrollX New X scroll value in pixels
14829 * @param scrollY New Y scroll value in pixels
14830 * @param clampedX True if scrollX was clamped to an over-scroll boundary
14831 * @param clampedY True if scrollY was clamped to an over-scroll boundary
14832 */
14833 protected void onOverScrolled(int scrollX, int scrollY,
14834 boolean clampedX, boolean clampedY) {
14835 // Intentionally empty.
14836 }
14837
14838 /**
14839 * Returns the over-scroll mode for this view. The result will be
14840 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
14841 * (allow over-scrolling only if the view content is larger than the container),
14842 * or {@link #OVER_SCROLL_NEVER}.
14843 *
14844 * @return This view's over-scroll mode.
14845 */
14846 public int getOverScrollMode() {
14847 return mOverScrollMode;
14848 }
14849
14850 /**
14851 * Set the over-scroll mode for this view. Valid over-scroll modes are
14852 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
14853 * (allow over-scrolling only if the view content is larger than the container),
14854 * or {@link #OVER_SCROLL_NEVER}.
14855 *
14856 * Setting the over-scroll mode of a view will have an effect only if the
14857 * view is capable of scrolling.
14858 *
14859 * @param overScrollMode The new over-scroll mode for this view.
14860 */
14861 public void setOverScrollMode(int overScrollMode) {
14862 if (overScrollMode != OVER_SCROLL_ALWAYS &&
14863 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
14864 overScrollMode != OVER_SCROLL_NEVER) {
14865 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
14866 }
14867 mOverScrollMode = overScrollMode;
14868 }
14869
14870 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080014871 * Gets a scale factor that determines the distance the view should scroll
14872 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
14873 * @return The vertical scroll scale factor.
14874 * @hide
14875 */
14876 protected float getVerticalScrollFactor() {
14877 if (mVerticalScrollFactor == 0) {
14878 TypedValue outValue = new TypedValue();
14879 if (!mContext.getTheme().resolveAttribute(
14880 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
14881 throw new IllegalStateException(
14882 "Expected theme to define listPreferredItemHeight.");
14883 }
14884 mVerticalScrollFactor = outValue.getDimension(
14885 mContext.getResources().getDisplayMetrics());
14886 }
14887 return mVerticalScrollFactor;
14888 }
14889
14890 /**
14891 * Gets a scale factor that determines the distance the view should scroll
14892 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
14893 * @return The horizontal scroll scale factor.
14894 * @hide
14895 */
14896 protected float getHorizontalScrollFactor() {
14897 // TODO: Should use something else.
14898 return getVerticalScrollFactor();
14899 }
14900
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014901 /**
14902 * Return the value specifying the text direction or policy that was set with
14903 * {@link #setTextDirection(int)}.
14904 *
14905 * @return the defined text direction. It can be one of:
14906 *
14907 * {@link #TEXT_DIRECTION_INHERIT},
14908 * {@link #TEXT_DIRECTION_FIRST_STRONG}
14909 * {@link #TEXT_DIRECTION_ANY_RTL},
14910 * {@link #TEXT_DIRECTION_LTR},
14911 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -080014912 * {@link #TEXT_DIRECTION_LOCALE},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014913 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070014914 @ViewDebug.ExportedProperty(category = "text", mapping = {
14915 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
14916 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
14917 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
14918 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
14919 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
14920 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
14921 })
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014922 public int getTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070014923 return (mPrivateFlags2 & TEXT_DIRECTION_MASK) >> TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014924 }
14925
14926 /**
14927 * Set the text direction.
14928 *
14929 * @param textDirection the direction to set. Should be one of:
14930 *
14931 * {@link #TEXT_DIRECTION_INHERIT},
14932 * {@link #TEXT_DIRECTION_FIRST_STRONG}
14933 * {@link #TEXT_DIRECTION_ANY_RTL},
14934 * {@link #TEXT_DIRECTION_LTR},
14935 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -080014936 * {@link #TEXT_DIRECTION_LOCALE},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014937 */
14938 public void setTextDirection(int textDirection) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070014939 if (getTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070014940 // Reset the current text direction and the resolved one
Fabrice Di Megliob934db72012-03-20 14:33:01 -070014941 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070014942 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070014943 // Set the new text direction
14944 mPrivateFlags2 |= ((textDirection << TEXT_DIRECTION_MASK_SHIFT) & TEXT_DIRECTION_MASK);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014945 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070014946 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014947 }
14948 }
14949
14950 /**
14951 * Return the resolved text direction.
14952 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070014953 * This needs resolution if the value is TEXT_DIRECTION_INHERIT. The resolution matches
14954 * {@link #getTextDirection()}if it is not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds
14955 * up the parent chain of the view. if there is no parent, then it will return the default
14956 * {@link #TEXT_DIRECTION_FIRST_STRONG}.
14957 *
14958 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014959 *
Doug Feltcb3791202011-07-07 11:57:48 -070014960 * {@link #TEXT_DIRECTION_FIRST_STRONG}
14961 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014962 * {@link #TEXT_DIRECTION_LTR},
14963 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -080014964 * {@link #TEXT_DIRECTION_LOCALE},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014965 */
14966 public int getResolvedTextDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070014967 // The text direction will be resolved only if needed
Fabrice Di Megliob934db72012-03-20 14:33:01 -070014968 if ((mPrivateFlags2 & TEXT_DIRECTION_RESOLVED) != TEXT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014969 resolveTextDirection();
14970 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070014971 return (mPrivateFlags2 & TEXT_DIRECTION_RESOLVED_MASK) >> TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014972 }
14973
14974 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014975 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
14976 * resolution is done.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014977 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014978 public void resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070014979 // Reset any previous text direction resolution
14980 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
14981
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070014982 if (hasRtlSupport()) {
14983 // Set resolved text direction flag depending on text direction flag
14984 final int textDirection = getTextDirection();
14985 switch(textDirection) {
14986 case TEXT_DIRECTION_INHERIT:
14987 if (canResolveTextDirection()) {
14988 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070014989
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070014990 // Set current resolved direction to the same value as the parent's one
14991 final int parentResolvedDirection = viewGroup.getResolvedTextDirection();
14992 switch (parentResolvedDirection) {
14993 case TEXT_DIRECTION_FIRST_STRONG:
14994 case TEXT_DIRECTION_ANY_RTL:
14995 case TEXT_DIRECTION_LTR:
14996 case TEXT_DIRECTION_RTL:
14997 case TEXT_DIRECTION_LOCALE:
14998 mPrivateFlags2 |=
14999 (parentResolvedDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
15000 break;
15001 default:
15002 // Default resolved direction is "first strong" heuristic
15003 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
15004 }
15005 } else {
15006 // We cannot do the resolution if there is no parent, so use the default one
15007 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015008 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015009 break;
15010 case TEXT_DIRECTION_FIRST_STRONG:
15011 case TEXT_DIRECTION_ANY_RTL:
15012 case TEXT_DIRECTION_LTR:
15013 case TEXT_DIRECTION_RTL:
15014 case TEXT_DIRECTION_LOCALE:
15015 // Resolved direction is the same as text direction
15016 mPrivateFlags2 |= (textDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
15017 break;
15018 default:
15019 // Default resolved direction is "first strong" heuristic
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015020 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015021 }
15022 } else {
15023 // Default resolved direction is "first strong" heuristic
15024 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015025 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015026
15027 // Set to resolved
15028 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015029 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015030 }
15031
15032 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015033 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015034 * resolution should override this method.
15035 *
15036 * The default implementation does nothing.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015037 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015038 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015039 }
15040
15041 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015042 * Check if text direction resolution can be done.
15043 *
15044 * @return true if text direction resolution can be done otherwise return false.
15045 */
15046 public boolean canResolveTextDirection() {
15047 switch (getTextDirection()) {
15048 case TEXT_DIRECTION_INHERIT:
15049 return (mParent != null) && (mParent instanceof ViewGroup);
15050 default:
15051 return true;
15052 }
15053 }
15054
15055 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015056 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015057 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015058 * reset is done.
15059 */
15060 public void resetResolvedTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015061 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015062 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015063 }
15064
15065 /**
15066 * Called when text direction is reset. Subclasses that care about text direction reset should
15067 * override this method and do a reset of the text direction of their children. The default
15068 * implementation does nothing.
15069 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015070 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015071 }
15072
Chet Haaseb39f0512011-05-24 14:36:40 -070015073 //
15074 // Properties
15075 //
15076 /**
15077 * A Property wrapper around the <code>alpha</code> functionality handled by the
15078 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
15079 */
Chet Haased47f1532011-12-16 11:18:52 -080015080 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015081 @Override
15082 public void setValue(View object, float value) {
15083 object.setAlpha(value);
15084 }
15085
15086 @Override
15087 public Float get(View object) {
15088 return object.getAlpha();
15089 }
15090 };
15091
15092 /**
15093 * A Property wrapper around the <code>translationX</code> functionality handled by the
15094 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
15095 */
Chet Haased47f1532011-12-16 11:18:52 -080015096 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015097 @Override
15098 public void setValue(View object, float value) {
15099 object.setTranslationX(value);
15100 }
15101
15102 @Override
15103 public Float get(View object) {
15104 return object.getTranslationX();
15105 }
15106 };
15107
15108 /**
15109 * A Property wrapper around the <code>translationY</code> functionality handled by the
15110 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
15111 */
Chet Haased47f1532011-12-16 11:18:52 -080015112 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015113 @Override
15114 public void setValue(View object, float value) {
15115 object.setTranslationY(value);
15116 }
15117
15118 @Override
15119 public Float get(View object) {
15120 return object.getTranslationY();
15121 }
15122 };
15123
15124 /**
15125 * A Property wrapper around the <code>x</code> functionality handled by the
15126 * {@link View#setX(float)} and {@link View#getX()} methods.
15127 */
Chet Haased47f1532011-12-16 11:18:52 -080015128 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015129 @Override
15130 public void setValue(View object, float value) {
15131 object.setX(value);
15132 }
15133
15134 @Override
15135 public Float get(View object) {
15136 return object.getX();
15137 }
15138 };
15139
15140 /**
15141 * A Property wrapper around the <code>y</code> functionality handled by the
15142 * {@link View#setY(float)} and {@link View#getY()} methods.
15143 */
Chet Haased47f1532011-12-16 11:18:52 -080015144 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015145 @Override
15146 public void setValue(View object, float value) {
15147 object.setY(value);
15148 }
15149
15150 @Override
15151 public Float get(View object) {
15152 return object.getY();
15153 }
15154 };
15155
15156 /**
15157 * A Property wrapper around the <code>rotation</code> functionality handled by the
15158 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
15159 */
Chet Haased47f1532011-12-16 11:18:52 -080015160 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015161 @Override
15162 public void setValue(View object, float value) {
15163 object.setRotation(value);
15164 }
15165
15166 @Override
15167 public Float get(View object) {
15168 return object.getRotation();
15169 }
15170 };
15171
15172 /**
15173 * A Property wrapper around the <code>rotationX</code> functionality handled by the
15174 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
15175 */
Chet Haased47f1532011-12-16 11:18:52 -080015176 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015177 @Override
15178 public void setValue(View object, float value) {
15179 object.setRotationX(value);
15180 }
15181
15182 @Override
15183 public Float get(View object) {
15184 return object.getRotationX();
15185 }
15186 };
15187
15188 /**
15189 * A Property wrapper around the <code>rotationY</code> functionality handled by the
15190 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
15191 */
Chet Haased47f1532011-12-16 11:18:52 -080015192 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015193 @Override
15194 public void setValue(View object, float value) {
15195 object.setRotationY(value);
15196 }
15197
15198 @Override
15199 public Float get(View object) {
15200 return object.getRotationY();
15201 }
15202 };
15203
15204 /**
15205 * A Property wrapper around the <code>scaleX</code> functionality handled by the
15206 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
15207 */
Chet Haased47f1532011-12-16 11:18:52 -080015208 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015209 @Override
15210 public void setValue(View object, float value) {
15211 object.setScaleX(value);
15212 }
15213
15214 @Override
15215 public Float get(View object) {
15216 return object.getScaleX();
15217 }
15218 };
15219
15220 /**
15221 * A Property wrapper around the <code>scaleY</code> functionality handled by the
15222 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
15223 */
Chet Haased47f1532011-12-16 11:18:52 -080015224 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070015225 @Override
15226 public void setValue(View object, float value) {
15227 object.setScaleY(value);
15228 }
15229
15230 @Override
15231 public Float get(View object) {
15232 return object.getScaleY();
15233 }
15234 };
15235
Jeff Brown33bbfd22011-02-24 20:55:35 -080015236 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015237 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
15238 * Each MeasureSpec represents a requirement for either the width or the height.
15239 * A MeasureSpec is comprised of a size and a mode. There are three possible
15240 * modes:
15241 * <dl>
15242 * <dt>UNSPECIFIED</dt>
15243 * <dd>
15244 * The parent has not imposed any constraint on the child. It can be whatever size
15245 * it wants.
15246 * </dd>
15247 *
15248 * <dt>EXACTLY</dt>
15249 * <dd>
15250 * The parent has determined an exact size for the child. The child is going to be
15251 * given those bounds regardless of how big it wants to be.
15252 * </dd>
15253 *
15254 * <dt>AT_MOST</dt>
15255 * <dd>
15256 * The child can be as large as it wants up to the specified size.
15257 * </dd>
15258 * </dl>
15259 *
15260 * MeasureSpecs are implemented as ints to reduce object allocation. This class
15261 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
15262 */
15263 public static class MeasureSpec {
15264 private static final int MODE_SHIFT = 30;
15265 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
15266
15267 /**
15268 * Measure specification mode: The parent has not imposed any constraint
15269 * on the child. It can be whatever size it wants.
15270 */
15271 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
15272
15273 /**
15274 * Measure specification mode: The parent has determined an exact size
15275 * for the child. The child is going to be given those bounds regardless
15276 * of how big it wants to be.
15277 */
15278 public static final int EXACTLY = 1 << MODE_SHIFT;
15279
15280 /**
15281 * Measure specification mode: The child can be as large as it wants up
15282 * to the specified size.
15283 */
15284 public static final int AT_MOST = 2 << MODE_SHIFT;
15285
15286 /**
15287 * Creates a measure specification based on the supplied size and mode.
15288 *
15289 * The mode must always be one of the following:
15290 * <ul>
15291 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
15292 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
15293 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
15294 * </ul>
15295 *
15296 * @param size the size of the measure specification
15297 * @param mode the mode of the measure specification
15298 * @return the measure specification based on size and mode
15299 */
15300 public static int makeMeasureSpec(int size, int mode) {
15301 return size + mode;
15302 }
15303
15304 /**
15305 * Extracts the mode from the supplied measure specification.
15306 *
15307 * @param measureSpec the measure specification to extract the mode from
15308 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
15309 * {@link android.view.View.MeasureSpec#AT_MOST} or
15310 * {@link android.view.View.MeasureSpec#EXACTLY}
15311 */
15312 public static int getMode(int measureSpec) {
15313 return (measureSpec & MODE_MASK);
15314 }
15315
15316 /**
15317 * Extracts the size from the supplied measure specification.
15318 *
15319 * @param measureSpec the measure specification to extract the size from
15320 * @return the size in pixels defined in the supplied measure specification
15321 */
15322 public static int getSize(int measureSpec) {
15323 return (measureSpec & ~MODE_MASK);
15324 }
15325
15326 /**
15327 * Returns a String representation of the specified measure
15328 * specification.
15329 *
15330 * @param measureSpec the measure specification to convert to a String
15331 * @return a String with the following format: "MeasureSpec: MODE SIZE"
15332 */
15333 public static String toString(int measureSpec) {
15334 int mode = getMode(measureSpec);
15335 int size = getSize(measureSpec);
15336
15337 StringBuilder sb = new StringBuilder("MeasureSpec: ");
15338
15339 if (mode == UNSPECIFIED)
15340 sb.append("UNSPECIFIED ");
15341 else if (mode == EXACTLY)
15342 sb.append("EXACTLY ");
15343 else if (mode == AT_MOST)
15344 sb.append("AT_MOST ");
15345 else
15346 sb.append(mode).append(" ");
15347
15348 sb.append(size);
15349 return sb.toString();
15350 }
15351 }
15352
15353 class CheckForLongPress implements Runnable {
15354
15355 private int mOriginalWindowAttachCount;
15356
15357 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070015358 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015359 && mOriginalWindowAttachCount == mWindowAttachCount) {
15360 if (performLongClick()) {
15361 mHasPerformedLongPress = true;
15362 }
15363 }
15364 }
15365
15366 public void rememberWindowAttachCount() {
15367 mOriginalWindowAttachCount = mWindowAttachCount;
15368 }
15369 }
Joe Malin32736f02011-01-19 16:14:20 -080015370
Adam Powelle14579b2009-12-16 18:39:52 -080015371 private final class CheckForTap implements Runnable {
15372 public void run() {
15373 mPrivateFlags &= ~PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080015374 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015375 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080015376 }
15377 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015378
Adam Powella35d7682010-03-12 14:48:13 -080015379 private final class PerformClick implements Runnable {
15380 public void run() {
15381 performClick();
15382 }
15383 }
15384
Dianne Hackborn63042d62011-01-26 18:56:29 -080015385 /** @hide */
15386 public void hackTurnOffWindowResizeAnim(boolean off) {
15387 mAttachInfo.mTurnOffWindowResizeAnim = off;
15388 }
Joe Malin32736f02011-01-19 16:14:20 -080015389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015390 /**
Chet Haasea00f3862011-02-22 06:34:40 -080015391 * This method returns a ViewPropertyAnimator object, which can be used to animate
15392 * specific properties on this View.
15393 *
15394 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
15395 */
15396 public ViewPropertyAnimator animate() {
15397 if (mAnimator == null) {
15398 mAnimator = new ViewPropertyAnimator(this);
15399 }
15400 return mAnimator;
15401 }
15402
15403 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015404 * Interface definition for a callback to be invoked when a key event is
15405 * dispatched to this view. The callback will be invoked before the key
15406 * event is given to the view.
15407 */
15408 public interface OnKeyListener {
15409 /**
15410 * Called when a key is dispatched to a view. This allows listeners to
15411 * get a chance to respond before the target view.
15412 *
15413 * @param v The view the key has been dispatched to.
15414 * @param keyCode The code for the physical key that was pressed
15415 * @param event The KeyEvent object containing full information about
15416 * the event.
15417 * @return True if the listener has consumed the event, false otherwise.
15418 */
15419 boolean onKey(View v, int keyCode, KeyEvent event);
15420 }
15421
15422 /**
15423 * Interface definition for a callback to be invoked when a touch event is
15424 * dispatched to this view. The callback will be invoked before the touch
15425 * event is given to the view.
15426 */
15427 public interface OnTouchListener {
15428 /**
15429 * Called when a touch event is dispatched to a view. This allows listeners to
15430 * get a chance to respond before the target view.
15431 *
15432 * @param v The view the touch event has been dispatched to.
15433 * @param event The MotionEvent object containing full information about
15434 * the event.
15435 * @return True if the listener has consumed the event, false otherwise.
15436 */
15437 boolean onTouch(View v, MotionEvent event);
15438 }
15439
15440 /**
Jeff Brown10b62902011-06-20 16:40:37 -070015441 * Interface definition for a callback to be invoked when a hover event is
15442 * dispatched to this view. The callback will be invoked before the hover
15443 * event is given to the view.
15444 */
15445 public interface OnHoverListener {
15446 /**
15447 * Called when a hover event is dispatched to a view. This allows listeners to
15448 * get a chance to respond before the target view.
15449 *
15450 * @param v The view the hover event has been dispatched to.
15451 * @param event The MotionEvent object containing full information about
15452 * the event.
15453 * @return True if the listener has consumed the event, false otherwise.
15454 */
15455 boolean onHover(View v, MotionEvent event);
15456 }
15457
15458 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080015459 * Interface definition for a callback to be invoked when a generic motion event is
15460 * dispatched to this view. The callback will be invoked before the generic motion
15461 * event is given to the view.
15462 */
15463 public interface OnGenericMotionListener {
15464 /**
15465 * Called when a generic motion event is dispatched to a view. This allows listeners to
15466 * get a chance to respond before the target view.
15467 *
15468 * @param v The view the generic motion event has been dispatched to.
15469 * @param event The MotionEvent object containing full information about
15470 * the event.
15471 * @return True if the listener has consumed the event, false otherwise.
15472 */
15473 boolean onGenericMotion(View v, MotionEvent event);
15474 }
15475
15476 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015477 * Interface definition for a callback to be invoked when a view has been clicked and held.
15478 */
15479 public interface OnLongClickListener {
15480 /**
15481 * Called when a view has been clicked and held.
15482 *
15483 * @param v The view that was clicked and held.
15484 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080015485 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015486 */
15487 boolean onLongClick(View v);
15488 }
15489
15490 /**
Chris Tate32affef2010-10-18 15:29:21 -070015491 * Interface definition for a callback to be invoked when a drag is being dispatched
15492 * to this view. The callback will be invoked before the hosting view's own
15493 * onDrag(event) method. If the listener wants to fall back to the hosting view's
15494 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070015495 *
15496 * <div class="special reference">
15497 * <h3>Developer Guides</h3>
15498 * <p>For a guide to implementing drag and drop features, read the
15499 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
15500 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070015501 */
15502 public interface OnDragListener {
15503 /**
15504 * Called when a drag event is dispatched to a view. This allows listeners
15505 * to get a chance to override base View behavior.
15506 *
Joe Malin32736f02011-01-19 16:14:20 -080015507 * @param v The View that received the drag event.
15508 * @param event The {@link android.view.DragEvent} object for the drag event.
15509 * @return {@code true} if the drag event was handled successfully, or {@code false}
15510 * if the drag event was not handled. Note that {@code false} will trigger the View
15511 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070015512 */
15513 boolean onDrag(View v, DragEvent event);
15514 }
15515
15516 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015517 * Interface definition for a callback to be invoked when the focus state of
15518 * a view changed.
15519 */
15520 public interface OnFocusChangeListener {
15521 /**
15522 * Called when the focus state of a view has changed.
15523 *
15524 * @param v The view whose state has changed.
15525 * @param hasFocus The new focus state of v.
15526 */
15527 void onFocusChange(View v, boolean hasFocus);
15528 }
15529
15530 /**
15531 * Interface definition for a callback to be invoked when a view is clicked.
15532 */
15533 public interface OnClickListener {
15534 /**
15535 * Called when a view has been clicked.
15536 *
15537 * @param v The view that was clicked.
15538 */
15539 void onClick(View v);
15540 }
15541
15542 /**
15543 * Interface definition for a callback to be invoked when the context menu
15544 * for this view is being built.
15545 */
15546 public interface OnCreateContextMenuListener {
15547 /**
15548 * Called when the context menu for this view is being built. It is not
15549 * safe to hold onto the menu after this method returns.
15550 *
15551 * @param menu The context menu that is being built
15552 * @param v The view for which the context menu is being built
15553 * @param menuInfo Extra information about the item for which the
15554 * context menu should be shown. This information will vary
15555 * depending on the class of v.
15556 */
15557 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
15558 }
15559
Joe Onorato664644d2011-01-23 17:53:23 -080015560 /**
15561 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015562 * visibility. This reports <strong>global</strong> changes to the system UI
15563 * state, not just what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080015564 *
Philip Milne6c8ea062012-04-03 17:38:43 -070015565 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080015566 */
15567 public interface OnSystemUiVisibilityChangeListener {
15568 /**
15569 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070015570 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080015571 *
Daniel Sandler60ee2562011-07-22 12:34:33 -040015572 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015573 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}. This tells you the
15574 * <strong>global</strong> state of the UI visibility flags, not what your
15575 * app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080015576 */
15577 public void onSystemUiVisibilityChange(int visibility);
15578 }
15579
Adam Powell4afd62b2011-02-18 15:02:18 -080015580 /**
15581 * Interface definition for a callback to be invoked when this view is attached
15582 * or detached from its window.
15583 */
15584 public interface OnAttachStateChangeListener {
15585 /**
15586 * Called when the view is attached to a window.
15587 * @param v The view that was attached
15588 */
15589 public void onViewAttachedToWindow(View v);
15590 /**
15591 * Called when the view is detached from a window.
15592 * @param v The view that was detached
15593 */
15594 public void onViewDetachedFromWindow(View v);
15595 }
15596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015597 private final class UnsetPressedState implements Runnable {
15598 public void run() {
15599 setPressed(false);
15600 }
15601 }
15602
15603 /**
15604 * Base class for derived classes that want to save and restore their own
15605 * state in {@link android.view.View#onSaveInstanceState()}.
15606 */
15607 public static class BaseSavedState extends AbsSavedState {
15608 /**
15609 * Constructor used when reading from a parcel. Reads the state of the superclass.
15610 *
15611 * @param source
15612 */
15613 public BaseSavedState(Parcel source) {
15614 super(source);
15615 }
15616
15617 /**
15618 * Constructor called by derived classes when creating their SavedState objects
15619 *
15620 * @param superState The state of the superclass of this view
15621 */
15622 public BaseSavedState(Parcelable superState) {
15623 super(superState);
15624 }
15625
15626 public static final Parcelable.Creator<BaseSavedState> CREATOR =
15627 new Parcelable.Creator<BaseSavedState>() {
15628 public BaseSavedState createFromParcel(Parcel in) {
15629 return new BaseSavedState(in);
15630 }
15631
15632 public BaseSavedState[] newArray(int size) {
15633 return new BaseSavedState[size];
15634 }
15635 };
15636 }
15637
15638 /**
15639 * A set of information given to a view when it is attached to its parent
15640 * window.
15641 */
15642 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015643 interface Callbacks {
15644 void playSoundEffect(int effectId);
15645 boolean performHapticFeedback(int effectId, boolean always);
15646 }
15647
15648 /**
15649 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
15650 * to a Handler. This class contains the target (View) to invalidate and
15651 * the coordinates of the dirty rectangle.
15652 *
15653 * For performance purposes, this class also implements a pool of up to
15654 * POOL_LIMIT objects that get reused. This reduces memory allocations
15655 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015656 */
Romain Guyd928d682009-03-31 17:52:16 -070015657 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015658 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070015659 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
15660 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070015661 public InvalidateInfo newInstance() {
15662 return new InvalidateInfo();
15663 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015664
Romain Guyd928d682009-03-31 17:52:16 -070015665 public void onAcquired(InvalidateInfo element) {
15666 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015667
Romain Guyd928d682009-03-31 17:52:16 -070015668 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070015669 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070015670 }
15671 }, POOL_LIMIT)
15672 );
15673
15674 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070015675 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015676
15677 View target;
15678
15679 int left;
15680 int top;
15681 int right;
15682 int bottom;
15683
Romain Guyd928d682009-03-31 17:52:16 -070015684 public void setNextPoolable(InvalidateInfo element) {
15685 mNext = element;
15686 }
15687
15688 public InvalidateInfo getNextPoolable() {
15689 return mNext;
15690 }
15691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015692 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070015693 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015694 }
15695
15696 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070015697 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015698 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070015699
15700 public boolean isPooled() {
15701 return mIsPooled;
15702 }
15703
15704 public void setPooled(boolean isPooled) {
15705 mIsPooled = isPooled;
15706 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015707 }
15708
15709 final IWindowSession mSession;
15710
15711 final IWindow mWindow;
15712
15713 final IBinder mWindowToken;
15714
15715 final Callbacks mRootCallbacks;
15716
Romain Guy59a12ca2011-06-09 17:48:21 -070015717 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080015718
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015719 /**
15720 * The top view of the hierarchy.
15721 */
15722 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070015723
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015724 IBinder mPanelParentWindowToken;
15725 Surface mSurface;
15726
Romain Guyb051e892010-09-28 19:09:36 -070015727 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080015728 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070015729 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080015730
Romain Guy7e4e5612012-03-05 14:37:29 -080015731 boolean mScreenOn;
15732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015733 /**
Romain Guy8506ab42009-06-11 17:35:47 -070015734 * Scale factor used by the compatibility mode
15735 */
15736 float mApplicationScale;
15737
15738 /**
15739 * Indicates whether the application is in compatibility mode
15740 */
15741 boolean mScalingRequired;
15742
15743 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015744 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080015745 */
15746 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080015747
Dianne Hackborn63042d62011-01-26 18:56:29 -080015748 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015749 * Left position of this view's window
15750 */
15751 int mWindowLeft;
15752
15753 /**
15754 * Top position of this view's window
15755 */
15756 int mWindowTop;
15757
15758 /**
Adam Powell26153a32010-11-08 15:22:27 -080015759 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070015760 */
Adam Powell26153a32010-11-08 15:22:27 -080015761 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070015762
15763 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015764 * For windows that are full-screen but using insets to layout inside
15765 * of the screen decorations, these are the current insets for the
15766 * content of the window.
15767 */
15768 final Rect mContentInsets = new Rect();
15769
15770 /**
15771 * For windows that are full-screen but using insets to layout inside
15772 * of the screen decorations, these are the current insets for the
15773 * actual visible parts of the window.
15774 */
15775 final Rect mVisibleInsets = new Rect();
15776
15777 /**
15778 * The internal insets given by this window. This value is
15779 * supplied by the client (through
15780 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
15781 * be given to the window manager when changed to be used in laying
15782 * out windows behind it.
15783 */
15784 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
15785 = new ViewTreeObserver.InternalInsetsInfo();
15786
15787 /**
15788 * All views in the window's hierarchy that serve as scroll containers,
15789 * used to determine if the window can be resized or must be panned
15790 * to adjust for a soft input area.
15791 */
15792 final ArrayList<View> mScrollContainers = new ArrayList<View>();
15793
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070015794 final KeyEvent.DispatcherState mKeyDispatchState
15795 = new KeyEvent.DispatcherState();
15796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015797 /**
15798 * Indicates whether the view's window currently has the focus.
15799 */
15800 boolean mHasWindowFocus;
15801
15802 /**
15803 * The current visibility of the window.
15804 */
15805 int mWindowVisibility;
15806
15807 /**
15808 * Indicates the time at which drawing started to occur.
15809 */
15810 long mDrawingTime;
15811
15812 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070015813 * Indicates whether or not ignoring the DIRTY_MASK flags.
15814 */
15815 boolean mIgnoreDirtyState;
15816
15817 /**
Romain Guy02ccac62011-06-24 13:20:23 -070015818 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
15819 * to avoid clearing that flag prematurely.
15820 */
15821 boolean mSetIgnoreDirtyState = false;
15822
15823 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015824 * Indicates whether the view's window is currently in touch mode.
15825 */
15826 boolean mInTouchMode;
15827
15828 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015829 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015830 * the next time it performs a traversal
15831 */
15832 boolean mRecomputeGlobalAttributes;
15833
15834 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015835 * Always report new attributes at next traversal.
15836 */
15837 boolean mForceReportNewAttributes;
15838
15839 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015840 * Set during a traveral if any views want to keep the screen on.
15841 */
15842 boolean mKeepScreenOn;
15843
15844 /**
Joe Onorato664644d2011-01-23 17:53:23 -080015845 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
15846 */
15847 int mSystemUiVisibility;
15848
15849 /**
15850 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
15851 * attached.
15852 */
15853 boolean mHasSystemUiListeners;
15854
15855 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015856 * Set if the visibility of any views has changed.
15857 */
15858 boolean mViewVisibilityChanged;
15859
15860 /**
15861 * Set to true if a view has been scrolled.
15862 */
15863 boolean mViewScrollChanged;
15864
15865 /**
15866 * Global to the view hierarchy used as a temporary for dealing with
15867 * x/y points in the transparent region computations.
15868 */
15869 final int[] mTransparentLocation = new int[2];
15870
15871 /**
15872 * Global to the view hierarchy used as a temporary for dealing with
15873 * x/y points in the ViewGroup.invalidateChild implementation.
15874 */
15875 final int[] mInvalidateChildLocation = new int[2];
15876
Chet Haasec3aa3612010-06-17 08:50:37 -070015877
15878 /**
15879 * Global to the view hierarchy used as a temporary for dealing with
15880 * x/y location when view is transformed.
15881 */
15882 final float[] mTmpTransformLocation = new float[2];
15883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015884 /**
15885 * The view tree observer used to dispatch global events like
15886 * layout, pre-draw, touch mode change, etc.
15887 */
15888 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
15889
15890 /**
15891 * A Canvas used by the view hierarchy to perform bitmap caching.
15892 */
15893 Canvas mCanvas;
15894
15895 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080015896 * The view root impl.
15897 */
15898 final ViewRootImpl mViewRootImpl;
15899
15900 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015901 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015902 * handler can be used to pump events in the UI events queue.
15903 */
15904 final Handler mHandler;
15905
15906 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015907 * Temporary for use in computing invalidate rectangles while
15908 * calling up the hierarchy.
15909 */
15910 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070015911
15912 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070015913 * Temporary for use in computing hit areas with transformed views
15914 */
15915 final RectF mTmpTransformRect = new RectF();
15916
15917 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070015918 * Temporary list for use in collecting focusable descendents of a view.
15919 */
15920 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
15921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015922 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070015923 * The id of the window for accessibility purposes.
15924 */
15925 int mAccessibilityWindowId = View.NO_ID;
15926
15927 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015928 * Creates a new set of attachment information with the specified
15929 * events handler and thread.
15930 *
15931 * @param handler the events handler the view must use
15932 */
15933 AttachInfo(IWindowSession session, IWindow window,
Jeff Browna175a5b2012-02-15 19:18:31 -080015934 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015935 mSession = session;
15936 mWindow = window;
15937 mWindowToken = window.asBinder();
Jeff Browna175a5b2012-02-15 19:18:31 -080015938 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015939 mHandler = handler;
15940 mRootCallbacks = effectPlayer;
15941 }
15942 }
15943
15944 /**
15945 * <p>ScrollabilityCache holds various fields used by a View when scrolling
15946 * is supported. This avoids keeping too many unused fields in most
15947 * instances of View.</p>
15948 */
Mike Cleronf116bf82009-09-27 19:14:12 -070015949 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080015950
Mike Cleronf116bf82009-09-27 19:14:12 -070015951 /**
15952 * Scrollbars are not visible
15953 */
15954 public static final int OFF = 0;
15955
15956 /**
15957 * Scrollbars are visible
15958 */
15959 public static final int ON = 1;
15960
15961 /**
15962 * Scrollbars are fading away
15963 */
15964 public static final int FADING = 2;
15965
15966 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080015967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015968 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070015969 public int scrollBarDefaultDelayBeforeFade;
15970 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015971
15972 public int scrollBarSize;
15973 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070015974 public float[] interpolatorValues;
15975 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015976
15977 public final Paint paint;
15978 public final Matrix matrix;
15979 public Shader shader;
15980
Mike Cleronf116bf82009-09-27 19:14:12 -070015981 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
15982
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080015983 private static final float[] OPAQUE = { 255 };
15984 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080015985
Mike Cleronf116bf82009-09-27 19:14:12 -070015986 /**
15987 * When fading should start. This time moves into the future every time
15988 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
15989 */
15990 public long fadeStartTime;
15991
15992
15993 /**
15994 * The current state of the scrollbars: ON, OFF, or FADING
15995 */
15996 public int state = OFF;
15997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015998 private int mLastColor;
15999
Mike Cleronf116bf82009-09-27 19:14:12 -070016000 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016001 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
16002 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070016003 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
16004 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016005
16006 paint = new Paint();
16007 matrix = new Matrix();
16008 // use use a height of 1, and then wack the matrix each time we
16009 // actually use it.
16010 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070016011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016012 paint.setShader(shader);
16013 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070016014 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016015 }
Romain Guy8506ab42009-06-11 17:35:47 -070016016
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016017 public void setFadeColor(int color) {
16018 if (color != 0 && color != mLastColor) {
16019 mLastColor = color;
16020 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070016021
Romain Guye55e1a72009-08-27 10:42:26 -070016022 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
16023 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070016024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016025 paint.setShader(shader);
16026 // Restore the default transfer mode (src_over)
16027 paint.setXfermode(null);
16028 }
16029 }
Joe Malin32736f02011-01-19 16:14:20 -080016030
Mike Cleronf116bf82009-09-27 19:14:12 -070016031 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070016032 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070016033 if (now >= fadeStartTime) {
16034
16035 // the animation fades the scrollbars out by changing
16036 // the opacity (alpha) from fully opaque to fully
16037 // transparent
16038 int nextFrame = (int) now;
16039 int framesCount = 0;
16040
16041 Interpolator interpolator = scrollBarInterpolator;
16042
16043 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080016044 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070016045
16046 // End transparent
16047 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080016048 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070016049
16050 state = FADING;
16051
16052 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080016053 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070016054 }
16055 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070016056 }
Mike Cleronf116bf82009-09-27 19:14:12 -070016057
Svetoslav Ganova0156172011-06-26 17:55:44 -070016058 /**
16059 * Resuable callback for sending
16060 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
16061 */
16062 private class SendViewScrolledAccessibilityEvent implements Runnable {
16063 public volatile boolean mIsPending;
16064
16065 public void run() {
16066 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
16067 mIsPending = false;
16068 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016069 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070016070
16071 /**
16072 * <p>
16073 * This class represents a delegate that can be registered in a {@link View}
16074 * to enhance accessibility support via composition rather via inheritance.
16075 * It is specifically targeted to widget developers that extend basic View
16076 * classes i.e. classes in package android.view, that would like their
16077 * applications to be backwards compatible.
16078 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080016079 * <div class="special reference">
16080 * <h3>Developer Guides</h3>
16081 * <p>For more information about making applications accessible, read the
16082 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
16083 * developer guide.</p>
16084 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070016085 * <p>
16086 * A scenario in which a developer would like to use an accessibility delegate
16087 * is overriding a method introduced in a later API version then the minimal API
16088 * version supported by the application. For example, the method
16089 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
16090 * in API version 4 when the accessibility APIs were first introduced. If a
16091 * developer would like his application to run on API version 4 devices (assuming
16092 * all other APIs used by the application are version 4 or lower) and take advantage
16093 * of this method, instead of overriding the method which would break the application's
16094 * backwards compatibility, he can override the corresponding method in this
16095 * delegate and register the delegate in the target View if the API version of
16096 * the system is high enough i.e. the API version is same or higher to the API
16097 * version that introduced
16098 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
16099 * </p>
16100 * <p>
16101 * Here is an example implementation:
16102 * </p>
16103 * <code><pre><p>
16104 * if (Build.VERSION.SDK_INT >= 14) {
16105 * // If the API version is equal of higher than the version in
16106 * // which onInitializeAccessibilityNodeInfo was introduced we
16107 * // register a delegate with a customized implementation.
16108 * View view = findViewById(R.id.view_id);
16109 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
16110 * public void onInitializeAccessibilityNodeInfo(View host,
16111 * AccessibilityNodeInfo info) {
16112 * // Let the default implementation populate the info.
16113 * super.onInitializeAccessibilityNodeInfo(host, info);
16114 * // Set some other information.
16115 * info.setEnabled(host.isEnabled());
16116 * }
16117 * });
16118 * }
16119 * </code></pre></p>
16120 * <p>
16121 * This delegate contains methods that correspond to the accessibility methods
16122 * in View. If a delegate has been specified the implementation in View hands
16123 * off handling to the corresponding method in this delegate. The default
16124 * implementation the delegate methods behaves exactly as the corresponding
16125 * method in View for the case of no accessibility delegate been set. Hence,
16126 * to customize the behavior of a View method, clients can override only the
16127 * corresponding delegate method without altering the behavior of the rest
16128 * accessibility related methods of the host view.
16129 * </p>
16130 */
16131 public static class AccessibilityDelegate {
16132
16133 /**
16134 * Sends an accessibility event of the given type. If accessibility is not
16135 * enabled this method has no effect.
16136 * <p>
16137 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
16138 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
16139 * been set.
16140 * </p>
16141 *
16142 * @param host The View hosting the delegate.
16143 * @param eventType The type of the event to send.
16144 *
16145 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
16146 */
16147 public void sendAccessibilityEvent(View host, int eventType) {
16148 host.sendAccessibilityEventInternal(eventType);
16149 }
16150
16151 /**
16152 * Sends an accessibility event. This method behaves exactly as
16153 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
16154 * empty {@link AccessibilityEvent} and does not perform a check whether
16155 * accessibility is enabled.
16156 * <p>
16157 * The default implementation behaves as
16158 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
16159 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
16160 * the case of no accessibility delegate been set.
16161 * </p>
16162 *
16163 * @param host The View hosting the delegate.
16164 * @param event The event to send.
16165 *
16166 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
16167 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
16168 */
16169 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
16170 host.sendAccessibilityEventUncheckedInternal(event);
16171 }
16172
16173 /**
16174 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
16175 * to its children for adding their text content to the event.
16176 * <p>
16177 * The default implementation behaves as
16178 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
16179 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
16180 * the case of no accessibility delegate been set.
16181 * </p>
16182 *
16183 * @param host The View hosting the delegate.
16184 * @param event The event.
16185 * @return True if the event population was completed.
16186 *
16187 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
16188 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
16189 */
16190 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
16191 return host.dispatchPopulateAccessibilityEventInternal(event);
16192 }
16193
16194 /**
16195 * Gives a chance to the host View to populate the accessibility event with its
16196 * text content.
16197 * <p>
16198 * The default implementation behaves as
16199 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
16200 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
16201 * the case of no accessibility delegate been set.
16202 * </p>
16203 *
16204 * @param host The View hosting the delegate.
16205 * @param event The accessibility event which to populate.
16206 *
16207 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
16208 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
16209 */
16210 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
16211 host.onPopulateAccessibilityEventInternal(event);
16212 }
16213
16214 /**
16215 * Initializes an {@link AccessibilityEvent} with information about the
16216 * the host View which is the event source.
16217 * <p>
16218 * The default implementation behaves as
16219 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
16220 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
16221 * the case of no accessibility delegate been set.
16222 * </p>
16223 *
16224 * @param host The View hosting the delegate.
16225 * @param event The event to initialize.
16226 *
16227 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
16228 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
16229 */
16230 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
16231 host.onInitializeAccessibilityEventInternal(event);
16232 }
16233
16234 /**
16235 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
16236 * <p>
16237 * The default implementation behaves as
16238 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
16239 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
16240 * the case of no accessibility delegate been set.
16241 * </p>
16242 *
16243 * @param host The View hosting the delegate.
16244 * @param info The instance to initialize.
16245 *
16246 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
16247 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
16248 */
16249 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
16250 host.onInitializeAccessibilityNodeInfoInternal(info);
16251 }
16252
16253 /**
16254 * Called when a child of the host View has requested sending an
16255 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
16256 * to augment the event.
16257 * <p>
16258 * The default implementation behaves as
16259 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
16260 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
16261 * the case of no accessibility delegate been set.
16262 * </p>
16263 *
16264 * @param host The View hosting the delegate.
16265 * @param child The child which requests sending the event.
16266 * @param event The event to be sent.
16267 * @return True if the event should be sent
16268 *
16269 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
16270 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
16271 */
16272 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
16273 AccessibilityEvent event) {
16274 return host.onRequestSendAccessibilityEventInternal(child, event);
16275 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070016276
16277 /**
16278 * Gets the provider for managing a virtual view hierarchy rooted at this View
16279 * and reported to {@link android.accessibilityservice.AccessibilityService}s
16280 * that explore the window content.
16281 * <p>
16282 * The default implementation behaves as
16283 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
16284 * the case of no accessibility delegate been set.
16285 * </p>
16286 *
16287 * @return The provider.
16288 *
16289 * @see AccessibilityNodeProvider
16290 */
16291 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
16292 return null;
16293 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070016294 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016295}